cc_request_mgr.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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. bool is_runtime_suspended;
  40. };
  41. struct cc_bl_item {
  42. struct cc_crypto_req creq;
  43. struct cc_hw_desc desc[CC_MAX_DESC_SEQ_LEN];
  44. unsigned int len;
  45. struct list_head list;
  46. bool notif;
  47. };
  48. static void comp_handler(unsigned long devarg);
  49. #ifdef COMP_IN_WQ
  50. static void comp_work_handler(struct work_struct *work);
  51. #endif
  52. void cc_req_mgr_fini(struct cc_drvdata *drvdata)
  53. {
  54. struct cc_req_mgr_handle *req_mgr_h = drvdata->request_mgr_handle;
  55. struct device *dev = drvdata_to_dev(drvdata);
  56. if (!req_mgr_h)
  57. return; /* Not allocated */
  58. if (req_mgr_h->dummy_comp_buff_dma) {
  59. dma_free_coherent(dev, sizeof(u32), req_mgr_h->dummy_comp_buff,
  60. req_mgr_h->dummy_comp_buff_dma);
  61. }
  62. dev_dbg(dev, "max_used_hw_slots=%d\n", (req_mgr_h->hw_queue_size -
  63. req_mgr_h->min_free_hw_slots));
  64. dev_dbg(dev, "max_used_sw_slots=%d\n", req_mgr_h->max_used_sw_slots);
  65. #ifdef COMP_IN_WQ
  66. flush_workqueue(req_mgr_h->workq);
  67. destroy_workqueue(req_mgr_h->workq);
  68. #else
  69. /* Kill tasklet */
  70. tasklet_kill(&req_mgr_h->comptask);
  71. #endif
  72. kzfree(req_mgr_h);
  73. drvdata->request_mgr_handle = NULL;
  74. }
  75. int cc_req_mgr_init(struct cc_drvdata *drvdata)
  76. {
  77. struct cc_req_mgr_handle *req_mgr_h;
  78. struct device *dev = drvdata_to_dev(drvdata);
  79. int rc = 0;
  80. req_mgr_h = kzalloc(sizeof(*req_mgr_h), GFP_KERNEL);
  81. if (!req_mgr_h) {
  82. rc = -ENOMEM;
  83. goto req_mgr_init_err;
  84. }
  85. drvdata->request_mgr_handle = req_mgr_h;
  86. spin_lock_init(&req_mgr_h->hw_lock);
  87. spin_lock_init(&req_mgr_h->bl_lock);
  88. INIT_LIST_HEAD(&req_mgr_h->backlog);
  89. #ifdef COMP_IN_WQ
  90. dev_dbg(dev, "Initializing completion workqueue\n");
  91. req_mgr_h->workq = create_singlethread_workqueue("ccree");
  92. if (!req_mgr_h->workq) {
  93. dev_err(dev, "Failed creating work queue\n");
  94. rc = -ENOMEM;
  95. goto req_mgr_init_err;
  96. }
  97. INIT_DELAYED_WORK(&req_mgr_h->compwork, comp_work_handler);
  98. #else
  99. dev_dbg(dev, "Initializing completion tasklet\n");
  100. tasklet_init(&req_mgr_h->comptask, comp_handler,
  101. (unsigned long)drvdata);
  102. #endif
  103. req_mgr_h->hw_queue_size = cc_ioread(drvdata,
  104. CC_REG(DSCRPTR_QUEUE_SRAM_SIZE));
  105. dev_dbg(dev, "hw_queue_size=0x%08X\n", req_mgr_h->hw_queue_size);
  106. if (req_mgr_h->hw_queue_size < MIN_HW_QUEUE_SIZE) {
  107. dev_err(dev, "Invalid HW queue size = %u (Min. required is %u)\n",
  108. req_mgr_h->hw_queue_size, MIN_HW_QUEUE_SIZE);
  109. rc = -ENOMEM;
  110. goto req_mgr_init_err;
  111. }
  112. req_mgr_h->min_free_hw_slots = req_mgr_h->hw_queue_size;
  113. req_mgr_h->max_used_sw_slots = 0;
  114. /* Allocate DMA word for "dummy" completion descriptor use */
  115. req_mgr_h->dummy_comp_buff =
  116. dma_alloc_coherent(dev, sizeof(u32),
  117. &req_mgr_h->dummy_comp_buff_dma,
  118. GFP_KERNEL);
  119. if (!req_mgr_h->dummy_comp_buff) {
  120. dev_err(dev, "Not enough memory to allocate DMA (%zu) dropped buffer\n",
  121. sizeof(u32));
  122. rc = -ENOMEM;
  123. goto req_mgr_init_err;
  124. }
  125. /* Init. "dummy" completion descriptor */
  126. hw_desc_init(&req_mgr_h->compl_desc);
  127. set_din_const(&req_mgr_h->compl_desc, 0, sizeof(u32));
  128. set_dout_dlli(&req_mgr_h->compl_desc, req_mgr_h->dummy_comp_buff_dma,
  129. sizeof(u32), NS_BIT, 1);
  130. set_flow_mode(&req_mgr_h->compl_desc, BYPASS);
  131. set_queue_last_ind(drvdata, &req_mgr_h->compl_desc);
  132. return 0;
  133. req_mgr_init_err:
  134. cc_req_mgr_fini(drvdata);
  135. return rc;
  136. }
  137. static void enqueue_seq(struct cc_drvdata *drvdata, struct cc_hw_desc seq[],
  138. unsigned int seq_len)
  139. {
  140. int i, w;
  141. void __iomem *reg = drvdata->cc_base + CC_REG(DSCRPTR_QUEUE_WORD0);
  142. struct device *dev = drvdata_to_dev(drvdata);
  143. /*
  144. * We do indeed write all 6 command words to the same
  145. * register. The HW supports this.
  146. */
  147. for (i = 0; i < seq_len; i++) {
  148. for (w = 0; w <= 5; w++)
  149. writel_relaxed(seq[i].word[w], reg);
  150. if (cc_dump_desc)
  151. dev_dbg(dev, "desc[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n",
  152. i, seq[i].word[0], seq[i].word[1],
  153. seq[i].word[2], seq[i].word[3],
  154. seq[i].word[4], seq[i].word[5]);
  155. }
  156. }
  157. /*!
  158. * Completion will take place if and only if user requested completion
  159. * by cc_send_sync_request().
  160. *
  161. * \param dev
  162. * \param dx_compl_h The completion event to signal
  163. */
  164. static void request_mgr_complete(struct device *dev, void *dx_compl_h,
  165. int dummy)
  166. {
  167. struct completion *this_compl = dx_compl_h;
  168. complete(this_compl);
  169. }
  170. static int cc_queues_status(struct cc_drvdata *drvdata,
  171. struct cc_req_mgr_handle *req_mgr_h,
  172. unsigned int total_seq_len)
  173. {
  174. unsigned long poll_queue;
  175. struct device *dev = drvdata_to_dev(drvdata);
  176. /* SW queue is checked only once as it will not
  177. * be chaned during the poll because the spinlock_bh
  178. * is held by the thread
  179. */
  180. if (((req_mgr_h->req_queue_head + 1) & (MAX_REQUEST_QUEUE_SIZE - 1)) ==
  181. req_mgr_h->req_queue_tail) {
  182. dev_err(dev, "SW FIFO is full. req_queue_head=%d sw_fifo_len=%d\n",
  183. req_mgr_h->req_queue_head, MAX_REQUEST_QUEUE_SIZE);
  184. return -ENOSPC;
  185. }
  186. if (req_mgr_h->q_free_slots >= total_seq_len)
  187. return 0;
  188. /* Wait for space in HW queue. Poll constant num of iterations. */
  189. for (poll_queue = 0; poll_queue < CC_MAX_POLL_ITER ; poll_queue++) {
  190. req_mgr_h->q_free_slots =
  191. cc_ioread(drvdata, CC_REG(DSCRPTR_QUEUE_CONTENT));
  192. if (req_mgr_h->q_free_slots < req_mgr_h->min_free_hw_slots)
  193. req_mgr_h->min_free_hw_slots = req_mgr_h->q_free_slots;
  194. if (req_mgr_h->q_free_slots >= total_seq_len) {
  195. /* If there is enough place return */
  196. return 0;
  197. }
  198. dev_dbg(dev, "HW FIFO is full. q_free_slots=%d total_seq_len=%d\n",
  199. req_mgr_h->q_free_slots, total_seq_len);
  200. }
  201. /* No room in the HW queue try again later */
  202. dev_dbg(dev, "HW FIFO full, timeout. req_queue_head=%d sw_fifo_len=%d q_free_slots=%d total_seq_len=%d\n",
  203. req_mgr_h->req_queue_head, MAX_REQUEST_QUEUE_SIZE,
  204. req_mgr_h->q_free_slots, total_seq_len);
  205. return -ENOSPC;
  206. }
  207. /*!
  208. * Enqueue caller request to crypto hardware.
  209. * Need to be called with HW lock held and PM running
  210. *
  211. * \param drvdata
  212. * \param cc_req The request to enqueue
  213. * \param desc The crypto sequence
  214. * \param len The crypto sequence length
  215. * \param add_comp If "true": add an artificial dout DMA to mark completion
  216. *
  217. * \return int Returns -EINPROGRESS or error code
  218. */
  219. static int cc_do_send_request(struct cc_drvdata *drvdata,
  220. struct cc_crypto_req *cc_req,
  221. struct cc_hw_desc *desc, unsigned int len,
  222. bool add_comp, bool ivgen)
  223. {
  224. struct cc_req_mgr_handle *req_mgr_h = drvdata->request_mgr_handle;
  225. unsigned int used_sw_slots;
  226. unsigned int iv_seq_len = 0;
  227. unsigned int total_seq_len = len; /*initial sequence length*/
  228. struct cc_hw_desc iv_seq[CC_IVPOOL_SEQ_LEN];
  229. struct device *dev = drvdata_to_dev(drvdata);
  230. int rc;
  231. if (ivgen) {
  232. dev_dbg(dev, "Acquire IV from pool into %d DMA addresses %pad, %pad, %pad, IV-size=%u\n",
  233. cc_req->ivgen_dma_addr_len,
  234. &cc_req->ivgen_dma_addr[0],
  235. &cc_req->ivgen_dma_addr[1],
  236. &cc_req->ivgen_dma_addr[2],
  237. cc_req->ivgen_size);
  238. /* Acquire IV from pool */
  239. rc = cc_get_iv(drvdata, cc_req->ivgen_dma_addr,
  240. cc_req->ivgen_dma_addr_len,
  241. cc_req->ivgen_size, iv_seq, &iv_seq_len);
  242. if (rc) {
  243. dev_err(dev, "Failed to generate IV (rc=%d)\n", rc);
  244. return rc;
  245. }
  246. total_seq_len += iv_seq_len;
  247. }
  248. used_sw_slots = ((req_mgr_h->req_queue_head -
  249. req_mgr_h->req_queue_tail) &
  250. (MAX_REQUEST_QUEUE_SIZE - 1));
  251. if (used_sw_slots > req_mgr_h->max_used_sw_slots)
  252. req_mgr_h->max_used_sw_slots = used_sw_slots;
  253. /* Enqueue request - must be locked with HW lock*/
  254. req_mgr_h->req_queue[req_mgr_h->req_queue_head] = *cc_req;
  255. req_mgr_h->req_queue_head = (req_mgr_h->req_queue_head + 1) &
  256. (MAX_REQUEST_QUEUE_SIZE - 1);
  257. /* TODO: Use circ_buf.h ? */
  258. dev_dbg(dev, "Enqueue request head=%u\n", req_mgr_h->req_queue_head);
  259. /*
  260. * We are about to push command to the HW via the command registers
  261. * that may refernece hsot memory. We need to issue a memory barrier
  262. * to make sure there are no outstnading memory writes
  263. */
  264. wmb();
  265. /* STAT_PHASE_4: Push sequence */
  266. if (ivgen)
  267. enqueue_seq(drvdata, iv_seq, iv_seq_len);
  268. enqueue_seq(drvdata, desc, len);
  269. if (add_comp) {
  270. enqueue_seq(drvdata, &req_mgr_h->compl_desc, 1);
  271. total_seq_len++;
  272. }
  273. if (req_mgr_h->q_free_slots < total_seq_len) {
  274. /* This situation should never occur. Maybe indicating problem
  275. * with resuming power. Set the free slot count to 0 and hope
  276. * for the best.
  277. */
  278. dev_err(dev, "HW free slot count mismatch.");
  279. req_mgr_h->q_free_slots = 0;
  280. } else {
  281. /* Update the free slots in HW queue */
  282. req_mgr_h->q_free_slots -= total_seq_len;
  283. }
  284. /* Operation still in process */
  285. return -EINPROGRESS;
  286. }
  287. static void cc_enqueue_backlog(struct cc_drvdata *drvdata,
  288. struct cc_bl_item *bli)
  289. {
  290. struct cc_req_mgr_handle *mgr = drvdata->request_mgr_handle;
  291. spin_lock_bh(&mgr->bl_lock);
  292. list_add_tail(&bli->list, &mgr->backlog);
  293. ++mgr->bl_len;
  294. spin_unlock_bh(&mgr->bl_lock);
  295. tasklet_schedule(&mgr->comptask);
  296. }
  297. static void cc_proc_backlog(struct cc_drvdata *drvdata)
  298. {
  299. struct cc_req_mgr_handle *mgr = drvdata->request_mgr_handle;
  300. struct cc_bl_item *bli;
  301. struct cc_crypto_req *creq;
  302. struct crypto_async_request *req;
  303. bool ivgen;
  304. unsigned int total_len;
  305. struct device *dev = drvdata_to_dev(drvdata);
  306. int rc;
  307. spin_lock(&mgr->bl_lock);
  308. while (mgr->bl_len) {
  309. bli = list_first_entry(&mgr->backlog, struct cc_bl_item, list);
  310. spin_unlock(&mgr->bl_lock);
  311. creq = &bli->creq;
  312. req = (struct crypto_async_request *)creq->user_arg;
  313. /*
  314. * Notify the request we're moving out of the backlog
  315. * but only if we haven't done so already.
  316. */
  317. if (!bli->notif) {
  318. req->complete(req, -EINPROGRESS);
  319. bli->notif = true;
  320. }
  321. ivgen = !!creq->ivgen_dma_addr_len;
  322. total_len = bli->len + (ivgen ? CC_IVPOOL_SEQ_LEN : 0);
  323. spin_lock(&mgr->hw_lock);
  324. rc = cc_queues_status(drvdata, mgr, total_len);
  325. if (rc) {
  326. /*
  327. * There is still not room in the FIFO for
  328. * this request. Bail out. We'll return here
  329. * on the next completion irq.
  330. */
  331. spin_unlock(&mgr->hw_lock);
  332. return;
  333. }
  334. rc = cc_do_send_request(drvdata, &bli->creq, bli->desc,
  335. bli->len, false, ivgen);
  336. spin_unlock(&mgr->hw_lock);
  337. if (rc != -EINPROGRESS) {
  338. cc_pm_put_suspend(dev);
  339. creq->user_cb(dev, req, rc);
  340. }
  341. /* Remove ourselves from the backlog list */
  342. spin_lock(&mgr->bl_lock);
  343. list_del(&bli->list);
  344. --mgr->bl_len;
  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. }
  558. /*
  559. * resume the queue configuration - no need to take the lock as this happens
  560. * inside the spin lock protection
  561. */
  562. #if defined(CONFIG_PM)
  563. int cc_resume_req_queue(struct cc_drvdata *drvdata)
  564. {
  565. struct cc_req_mgr_handle *request_mgr_handle =
  566. drvdata->request_mgr_handle;
  567. spin_lock_bh(&request_mgr_handle->hw_lock);
  568. request_mgr_handle->is_runtime_suspended = false;
  569. spin_unlock_bh(&request_mgr_handle->hw_lock);
  570. return 0;
  571. }
  572. /*
  573. * suspend the queue configuration. Since it is used for the runtime suspend
  574. * only verify that the queue can be suspended.
  575. */
  576. int cc_suspend_req_queue(struct cc_drvdata *drvdata)
  577. {
  578. struct cc_req_mgr_handle *request_mgr_handle =
  579. drvdata->request_mgr_handle;
  580. /* lock the send_request */
  581. spin_lock_bh(&request_mgr_handle->hw_lock);
  582. if (request_mgr_handle->req_queue_head !=
  583. request_mgr_handle->req_queue_tail) {
  584. spin_unlock_bh(&request_mgr_handle->hw_lock);
  585. return -EBUSY;
  586. }
  587. request_mgr_handle->is_runtime_suspended = true;
  588. spin_unlock_bh(&request_mgr_handle->hw_lock);
  589. return 0;
  590. }
  591. bool cc_req_queue_suspended(struct cc_drvdata *drvdata)
  592. {
  593. struct cc_req_mgr_handle *request_mgr_handle =
  594. drvdata->request_mgr_handle;
  595. return request_mgr_handle->is_runtime_suspended;
  596. }
  597. #endif