mmc_hsq.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. *
  4. * MMC software queue support based on command queue interfaces
  5. *
  6. * Copyright (C) 2019 Linaro, Inc.
  7. * Author: Baolin Wang <baolin.wang@linaro.org>
  8. */
  9. #include <linux/mmc/card.h>
  10. #include <linux/mmc/host.h>
  11. #include <linux/module.h>
  12. #include "mmc_hsq.h"
  13. static void mmc_hsq_retry_handler(struct work_struct *work)
  14. {
  15. struct mmc_hsq *hsq = container_of(work, struct mmc_hsq, retry_work);
  16. struct mmc_host *mmc = hsq->mmc;
  17. mmc->ops->request(mmc, hsq->mrq);
  18. }
  19. static void mmc_hsq_modify_threshold(struct mmc_hsq *hsq)
  20. {
  21. struct mmc_host *mmc = hsq->mmc;
  22. struct mmc_request *mrq;
  23. unsigned int tag, need_change = 0;
  24. mmc->hsq_depth = HSQ_NORMAL_DEPTH;
  25. for (tag = 0; tag < HSQ_NUM_SLOTS; tag++) {
  26. mrq = hsq->slot[tag].mrq;
  27. if (mrq && mrq->data &&
  28. (mrq->data->blksz * mrq->data->blocks == 4096) &&
  29. (mrq->data->flags & MMC_DATA_WRITE) &&
  30. (++need_change == 2)) {
  31. mmc->hsq_depth = HSQ_PERFORMANCE_DEPTH;
  32. break;
  33. }
  34. }
  35. }
  36. static void mmc_hsq_pump_requests(struct mmc_hsq *hsq)
  37. {
  38. struct mmc_host *mmc = hsq->mmc;
  39. struct hsq_slot *slot;
  40. unsigned long flags;
  41. int ret = 0;
  42. spin_lock_irqsave(&hsq->lock, flags);
  43. /* Make sure we are not already running a request now */
  44. if (hsq->mrq || hsq->recovery_halt) {
  45. spin_unlock_irqrestore(&hsq->lock, flags);
  46. return;
  47. }
  48. /* Make sure there are remain requests need to pump */
  49. if (!hsq->qcnt || !hsq->enabled) {
  50. spin_unlock_irqrestore(&hsq->lock, flags);
  51. return;
  52. }
  53. mmc_hsq_modify_threshold(hsq);
  54. slot = &hsq->slot[hsq->next_tag];
  55. hsq->mrq = slot->mrq;
  56. hsq->qcnt--;
  57. spin_unlock_irqrestore(&hsq->lock, flags);
  58. if (mmc->ops->request_atomic)
  59. ret = mmc->ops->request_atomic(mmc, hsq->mrq);
  60. else
  61. mmc->ops->request(mmc, hsq->mrq);
  62. /*
  63. * If returning BUSY from request_atomic(), which means the card
  64. * may be busy now, and we should change to non-atomic context to
  65. * try again for this unusual case, to avoid time-consuming operations
  66. * in the atomic context.
  67. *
  68. * Note: we just give a warning for other error cases, since the host
  69. * driver will handle them.
  70. */
  71. if (ret == -EBUSY)
  72. schedule_work(&hsq->retry_work);
  73. else
  74. WARN_ON_ONCE(ret);
  75. }
  76. static void mmc_hsq_update_next_tag(struct mmc_hsq *hsq, int remains)
  77. {
  78. int tag;
  79. /*
  80. * If there are no remain requests in software queue, then set a invalid
  81. * tag.
  82. */
  83. if (!remains) {
  84. hsq->next_tag = HSQ_INVALID_TAG;
  85. hsq->tail_tag = HSQ_INVALID_TAG;
  86. return;
  87. }
  88. tag = hsq->tag_slot[hsq->next_tag];
  89. hsq->tag_slot[hsq->next_tag] = HSQ_INVALID_TAG;
  90. hsq->next_tag = tag;
  91. }
  92. static void mmc_hsq_post_request(struct mmc_hsq *hsq)
  93. {
  94. unsigned long flags;
  95. int remains;
  96. spin_lock_irqsave(&hsq->lock, flags);
  97. remains = hsq->qcnt;
  98. hsq->mrq = NULL;
  99. /* Update the next available tag to be queued. */
  100. mmc_hsq_update_next_tag(hsq, remains);
  101. if (hsq->waiting_for_idle && !remains) {
  102. hsq->waiting_for_idle = false;
  103. wake_up(&hsq->wait_queue);
  104. }
  105. /* Do not pump new request in recovery mode. */
  106. if (hsq->recovery_halt) {
  107. spin_unlock_irqrestore(&hsq->lock, flags);
  108. return;
  109. }
  110. spin_unlock_irqrestore(&hsq->lock, flags);
  111. /*
  112. * Try to pump new request to host controller as fast as possible,
  113. * after completing previous request.
  114. */
  115. if (remains > 0)
  116. mmc_hsq_pump_requests(hsq);
  117. }
  118. /**
  119. * mmc_hsq_finalize_request - finalize one request if the request is done
  120. * @mmc: the host controller
  121. * @mrq: the request need to be finalized
  122. *
  123. * Return true if we finalized the corresponding request in software queue,
  124. * otherwise return false.
  125. */
  126. bool mmc_hsq_finalize_request(struct mmc_host *mmc, struct mmc_request *mrq)
  127. {
  128. struct mmc_hsq *hsq = mmc->cqe_private;
  129. unsigned long flags;
  130. spin_lock_irqsave(&hsq->lock, flags);
  131. if (!hsq->enabled || !hsq->mrq || hsq->mrq != mrq) {
  132. spin_unlock_irqrestore(&hsq->lock, flags);
  133. return false;
  134. }
  135. /*
  136. * Clear current completed slot request to make a room for new request.
  137. */
  138. hsq->slot[hsq->next_tag].mrq = NULL;
  139. spin_unlock_irqrestore(&hsq->lock, flags);
  140. mmc_cqe_request_done(mmc, hsq->mrq);
  141. mmc_hsq_post_request(hsq);
  142. return true;
  143. }
  144. EXPORT_SYMBOL_GPL(mmc_hsq_finalize_request);
  145. static void mmc_hsq_recovery_start(struct mmc_host *mmc)
  146. {
  147. struct mmc_hsq *hsq = mmc->cqe_private;
  148. unsigned long flags;
  149. spin_lock_irqsave(&hsq->lock, flags);
  150. hsq->recovery_halt = true;
  151. spin_unlock_irqrestore(&hsq->lock, flags);
  152. }
  153. static void mmc_hsq_recovery_finish(struct mmc_host *mmc)
  154. {
  155. struct mmc_hsq *hsq = mmc->cqe_private;
  156. int remains;
  157. spin_lock_irq(&hsq->lock);
  158. hsq->recovery_halt = false;
  159. remains = hsq->qcnt;
  160. spin_unlock_irq(&hsq->lock);
  161. /*
  162. * Try to pump new request if there are request pending in software
  163. * queue after finishing recovery.
  164. */
  165. if (remains > 0)
  166. mmc_hsq_pump_requests(hsq);
  167. }
  168. static int mmc_hsq_request(struct mmc_host *mmc, struct mmc_request *mrq)
  169. {
  170. struct mmc_hsq *hsq = mmc->cqe_private;
  171. int tag = mrq->tag;
  172. spin_lock_irq(&hsq->lock);
  173. if (!hsq->enabled) {
  174. spin_unlock_irq(&hsq->lock);
  175. return -ESHUTDOWN;
  176. }
  177. /* Do not queue any new requests in recovery mode. */
  178. if (hsq->recovery_halt) {
  179. spin_unlock_irq(&hsq->lock);
  180. return -EBUSY;
  181. }
  182. hsq->slot[tag].mrq = mrq;
  183. /*
  184. * Set the next tag as current request tag if no available
  185. * next tag.
  186. */
  187. if (hsq->next_tag == HSQ_INVALID_TAG) {
  188. hsq->next_tag = tag;
  189. hsq->tail_tag = tag;
  190. hsq->tag_slot[hsq->tail_tag] = HSQ_INVALID_TAG;
  191. } else {
  192. hsq->tag_slot[hsq->tail_tag] = tag;
  193. hsq->tail_tag = tag;
  194. }
  195. hsq->qcnt++;
  196. spin_unlock_irq(&hsq->lock);
  197. mmc_hsq_pump_requests(hsq);
  198. return 0;
  199. }
  200. static void mmc_hsq_post_req(struct mmc_host *mmc, struct mmc_request *mrq)
  201. {
  202. if (mmc->ops->post_req)
  203. mmc->ops->post_req(mmc, mrq, 0);
  204. }
  205. static bool mmc_hsq_queue_is_idle(struct mmc_hsq *hsq, int *ret)
  206. {
  207. bool is_idle;
  208. spin_lock_irq(&hsq->lock);
  209. is_idle = (!hsq->mrq && !hsq->qcnt) ||
  210. hsq->recovery_halt;
  211. *ret = hsq->recovery_halt ? -EBUSY : 0;
  212. hsq->waiting_for_idle = !is_idle;
  213. spin_unlock_irq(&hsq->lock);
  214. return is_idle;
  215. }
  216. static int mmc_hsq_wait_for_idle(struct mmc_host *mmc)
  217. {
  218. struct mmc_hsq *hsq = mmc->cqe_private;
  219. int ret;
  220. wait_event(hsq->wait_queue,
  221. mmc_hsq_queue_is_idle(hsq, &ret));
  222. return ret;
  223. }
  224. static void mmc_hsq_disable(struct mmc_host *mmc)
  225. {
  226. struct mmc_hsq *hsq = mmc->cqe_private;
  227. u32 timeout = 500;
  228. int ret;
  229. spin_lock_irq(&hsq->lock);
  230. if (!hsq->enabled) {
  231. spin_unlock_irq(&hsq->lock);
  232. return;
  233. }
  234. spin_unlock_irq(&hsq->lock);
  235. ret = wait_event_timeout(hsq->wait_queue,
  236. mmc_hsq_queue_is_idle(hsq, &ret),
  237. msecs_to_jiffies(timeout));
  238. if (ret == 0) {
  239. pr_warn("could not stop mmc software queue\n");
  240. return;
  241. }
  242. spin_lock_irq(&hsq->lock);
  243. hsq->enabled = false;
  244. spin_unlock_irq(&hsq->lock);
  245. }
  246. static int mmc_hsq_enable(struct mmc_host *mmc, struct mmc_card *card)
  247. {
  248. struct mmc_hsq *hsq = mmc->cqe_private;
  249. spin_lock_irq(&hsq->lock);
  250. if (hsq->enabled) {
  251. spin_unlock_irq(&hsq->lock);
  252. return -EBUSY;
  253. }
  254. hsq->enabled = true;
  255. spin_unlock_irq(&hsq->lock);
  256. return 0;
  257. }
  258. static const struct mmc_cqe_ops mmc_hsq_ops = {
  259. .cqe_enable = mmc_hsq_enable,
  260. .cqe_disable = mmc_hsq_disable,
  261. .cqe_request = mmc_hsq_request,
  262. .cqe_post_req = mmc_hsq_post_req,
  263. .cqe_wait_for_idle = mmc_hsq_wait_for_idle,
  264. .cqe_recovery_start = mmc_hsq_recovery_start,
  265. .cqe_recovery_finish = mmc_hsq_recovery_finish,
  266. };
  267. int mmc_hsq_init(struct mmc_hsq *hsq, struct mmc_host *mmc)
  268. {
  269. int i;
  270. hsq->num_slots = HSQ_NUM_SLOTS;
  271. hsq->next_tag = HSQ_INVALID_TAG;
  272. hsq->tail_tag = HSQ_INVALID_TAG;
  273. hsq->slot = devm_kcalloc(mmc_dev(mmc), hsq->num_slots,
  274. sizeof(struct hsq_slot), GFP_KERNEL);
  275. if (!hsq->slot)
  276. return -ENOMEM;
  277. hsq->mmc = mmc;
  278. hsq->mmc->cqe_private = hsq;
  279. mmc->cqe_ops = &mmc_hsq_ops;
  280. mmc->hsq_depth = HSQ_NORMAL_DEPTH;
  281. for (i = 0; i < HSQ_NUM_SLOTS; i++)
  282. hsq->tag_slot[i] = HSQ_INVALID_TAG;
  283. INIT_WORK(&hsq->retry_work, mmc_hsq_retry_handler);
  284. spin_lock_init(&hsq->lock);
  285. init_waitqueue_head(&hsq->wait_queue);
  286. return 0;
  287. }
  288. EXPORT_SYMBOL_GPL(mmc_hsq_init);
  289. void mmc_hsq_suspend(struct mmc_host *mmc)
  290. {
  291. mmc_hsq_disable(mmc);
  292. }
  293. EXPORT_SYMBOL_GPL(mmc_hsq_suspend);
  294. int mmc_hsq_resume(struct mmc_host *mmc)
  295. {
  296. return mmc_hsq_enable(mmc, NULL);
  297. }
  298. EXPORT_SYMBOL_GPL(mmc_hsq_resume);
  299. MODULE_DESCRIPTION("MMC Host Software Queue support");
  300. MODULE_LICENSE("GPL v2");