mc-sys.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
  2. /*
  3. * Copyright 2013-2016 Freescale Semiconductor Inc.
  4. *
  5. * I/O services to send MC commands to the MC hardware
  6. *
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/slab.h>
  10. #include <linux/ioport.h>
  11. #include <linux/device.h>
  12. #include <linux/io.h>
  13. #include <linux/io-64-nonatomic-hi-lo.h>
  14. #include <linux/fsl/mc.h>
  15. #include "fsl-mc-private.h"
  16. /**
  17. * Timeout in milliseconds to wait for the completion of an MC command
  18. */
  19. #define MC_CMD_COMPLETION_TIMEOUT_MS 500
  20. /*
  21. * usleep_range() min and max values used to throttle down polling
  22. * iterations while waiting for MC command completion
  23. */
  24. #define MC_CMD_COMPLETION_POLLING_MIN_SLEEP_USECS 10
  25. #define MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS 500
  26. static enum mc_cmd_status mc_cmd_hdr_read_status(struct fsl_mc_command *cmd)
  27. {
  28. struct mc_cmd_header *hdr = (struct mc_cmd_header *)&cmd->header;
  29. return (enum mc_cmd_status)hdr->status;
  30. }
  31. static u16 mc_cmd_hdr_read_cmdid(struct fsl_mc_command *cmd)
  32. {
  33. struct mc_cmd_header *hdr = (struct mc_cmd_header *)&cmd->header;
  34. u16 cmd_id = le16_to_cpu(hdr->cmd_id);
  35. return cmd_id;
  36. }
  37. static int mc_status_to_error(enum mc_cmd_status status)
  38. {
  39. static const int mc_status_to_error_map[] = {
  40. [MC_CMD_STATUS_OK] = 0,
  41. [MC_CMD_STATUS_AUTH_ERR] = -EACCES,
  42. [MC_CMD_STATUS_NO_PRIVILEGE] = -EPERM,
  43. [MC_CMD_STATUS_DMA_ERR] = -EIO,
  44. [MC_CMD_STATUS_CONFIG_ERR] = -ENXIO,
  45. [MC_CMD_STATUS_TIMEOUT] = -ETIMEDOUT,
  46. [MC_CMD_STATUS_NO_RESOURCE] = -ENAVAIL,
  47. [MC_CMD_STATUS_NO_MEMORY] = -ENOMEM,
  48. [MC_CMD_STATUS_BUSY] = -EBUSY,
  49. [MC_CMD_STATUS_UNSUPPORTED_OP] = -ENOTSUPP,
  50. [MC_CMD_STATUS_INVALID_STATE] = -ENODEV,
  51. };
  52. if ((u32)status >= ARRAY_SIZE(mc_status_to_error_map))
  53. return -EINVAL;
  54. return mc_status_to_error_map[status];
  55. }
  56. static const char *mc_status_to_string(enum mc_cmd_status status)
  57. {
  58. static const char *const status_strings[] = {
  59. [MC_CMD_STATUS_OK] = "Command completed successfully",
  60. [MC_CMD_STATUS_READY] = "Command ready to be processed",
  61. [MC_CMD_STATUS_AUTH_ERR] = "Authentication error",
  62. [MC_CMD_STATUS_NO_PRIVILEGE] = "No privilege",
  63. [MC_CMD_STATUS_DMA_ERR] = "DMA or I/O error",
  64. [MC_CMD_STATUS_CONFIG_ERR] = "Configuration error",
  65. [MC_CMD_STATUS_TIMEOUT] = "Operation timed out",
  66. [MC_CMD_STATUS_NO_RESOURCE] = "No resources",
  67. [MC_CMD_STATUS_NO_MEMORY] = "No memory available",
  68. [MC_CMD_STATUS_BUSY] = "Device is busy",
  69. [MC_CMD_STATUS_UNSUPPORTED_OP] = "Unsupported operation",
  70. [MC_CMD_STATUS_INVALID_STATE] = "Invalid state"
  71. };
  72. if ((unsigned int)status >= ARRAY_SIZE(status_strings))
  73. return "Unknown MC error";
  74. return status_strings[status];
  75. }
  76. /**
  77. * mc_write_command - writes a command to a Management Complex (MC) portal
  78. *
  79. * @portal: pointer to an MC portal
  80. * @cmd: pointer to a filled command
  81. */
  82. static inline void mc_write_command(struct fsl_mc_command __iomem *portal,
  83. struct fsl_mc_command *cmd)
  84. {
  85. int i;
  86. /* copy command parameters into the portal */
  87. for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
  88. /*
  89. * Data is already in the expected LE byte-order. Do an
  90. * extra LE -> CPU conversion so that the CPU -> LE done in
  91. * the device io write api puts it back in the right order.
  92. */
  93. writeq_relaxed(le64_to_cpu(cmd->params[i]), &portal->params[i]);
  94. /* submit the command by writing the header */
  95. writeq(le64_to_cpu(cmd->header), &portal->header);
  96. }
  97. /**
  98. * mc_read_response - reads the response for the last MC command from a
  99. * Management Complex (MC) portal
  100. *
  101. * @portal: pointer to an MC portal
  102. * @resp: pointer to command response buffer
  103. *
  104. * Returns MC_CMD_STATUS_OK on Success; Error code otherwise.
  105. */
  106. static inline enum mc_cmd_status mc_read_response(struct fsl_mc_command __iomem
  107. *portal,
  108. struct fsl_mc_command *resp)
  109. {
  110. int i;
  111. enum mc_cmd_status status;
  112. /* Copy command response header from MC portal: */
  113. resp->header = cpu_to_le64(readq_relaxed(&portal->header));
  114. status = mc_cmd_hdr_read_status(resp);
  115. if (status != MC_CMD_STATUS_OK)
  116. return status;
  117. /* Copy command response data from MC portal: */
  118. for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
  119. /*
  120. * Data is expected to be in LE byte-order. Do an
  121. * extra CPU -> LE to revert the LE -> CPU done in
  122. * the device io read api.
  123. */
  124. resp->params[i] =
  125. cpu_to_le64(readq_relaxed(&portal->params[i]));
  126. return status;
  127. }
  128. /**
  129. * Waits for the completion of an MC command doing preemptible polling.
  130. * uslepp_range() is called between polling iterations.
  131. *
  132. * @mc_io: MC I/O object to be used
  133. * @cmd: command buffer to receive MC response
  134. * @mc_status: MC command completion status
  135. */
  136. static int mc_polling_wait_preemptible(struct fsl_mc_io *mc_io,
  137. struct fsl_mc_command *cmd,
  138. enum mc_cmd_status *mc_status)
  139. {
  140. enum mc_cmd_status status;
  141. unsigned long jiffies_until_timeout =
  142. jiffies + msecs_to_jiffies(MC_CMD_COMPLETION_TIMEOUT_MS);
  143. /*
  144. * Wait for response from the MC hardware:
  145. */
  146. for (;;) {
  147. status = mc_read_response(mc_io->portal_virt_addr, cmd);
  148. if (status != MC_CMD_STATUS_READY)
  149. break;
  150. /*
  151. * TODO: When MC command completion interrupts are supported
  152. * call wait function here instead of usleep_range()
  153. */
  154. usleep_range(MC_CMD_COMPLETION_POLLING_MIN_SLEEP_USECS,
  155. MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS);
  156. if (time_after_eq(jiffies, jiffies_until_timeout)) {
  157. dev_dbg(mc_io->dev,
  158. "MC command timed out (portal: %pa, dprc handle: %#x, command: %#x)\n",
  159. &mc_io->portal_phys_addr,
  160. (unsigned int)mc_cmd_hdr_read_token(cmd),
  161. (unsigned int)mc_cmd_hdr_read_cmdid(cmd));
  162. return -ETIMEDOUT;
  163. }
  164. }
  165. *mc_status = status;
  166. return 0;
  167. }
  168. /**
  169. * Waits for the completion of an MC command doing atomic polling.
  170. * udelay() is called between polling iterations.
  171. *
  172. * @mc_io: MC I/O object to be used
  173. * @cmd: command buffer to receive MC response
  174. * @mc_status: MC command completion status
  175. */
  176. static int mc_polling_wait_atomic(struct fsl_mc_io *mc_io,
  177. struct fsl_mc_command *cmd,
  178. enum mc_cmd_status *mc_status)
  179. {
  180. enum mc_cmd_status status;
  181. unsigned long timeout_usecs = MC_CMD_COMPLETION_TIMEOUT_MS * 1000;
  182. BUILD_BUG_ON((MC_CMD_COMPLETION_TIMEOUT_MS * 1000) %
  183. MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS != 0);
  184. for (;;) {
  185. status = mc_read_response(mc_io->portal_virt_addr, cmd);
  186. if (status != MC_CMD_STATUS_READY)
  187. break;
  188. udelay(MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS);
  189. timeout_usecs -= MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS;
  190. if (timeout_usecs == 0) {
  191. dev_dbg(mc_io->dev,
  192. "MC command timed out (portal: %pa, dprc handle: %#x, command: %#x)\n",
  193. &mc_io->portal_phys_addr,
  194. (unsigned int)mc_cmd_hdr_read_token(cmd),
  195. (unsigned int)mc_cmd_hdr_read_cmdid(cmd));
  196. return -ETIMEDOUT;
  197. }
  198. }
  199. *mc_status = status;
  200. return 0;
  201. }
  202. /**
  203. * Sends a command to the MC device using the given MC I/O object
  204. *
  205. * @mc_io: MC I/O object to be used
  206. * @cmd: command to be sent
  207. *
  208. * Returns '0' on Success; Error code otherwise.
  209. */
  210. int mc_send_command(struct fsl_mc_io *mc_io, struct fsl_mc_command *cmd)
  211. {
  212. int error;
  213. enum mc_cmd_status status;
  214. unsigned long irq_flags = 0;
  215. if (in_irq() && !(mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL))
  216. return -EINVAL;
  217. if (mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL)
  218. spin_lock_irqsave(&mc_io->spinlock, irq_flags);
  219. else
  220. mutex_lock(&mc_io->mutex);
  221. /*
  222. * Send command to the MC hardware:
  223. */
  224. mc_write_command(mc_io->portal_virt_addr, cmd);
  225. /*
  226. * Wait for response from the MC hardware:
  227. */
  228. if (!(mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL))
  229. error = mc_polling_wait_preemptible(mc_io, cmd, &status);
  230. else
  231. error = mc_polling_wait_atomic(mc_io, cmd, &status);
  232. if (error < 0)
  233. goto common_exit;
  234. if (status != MC_CMD_STATUS_OK) {
  235. dev_dbg(mc_io->dev,
  236. "MC command failed: portal: %pa, dprc handle: %#x, command: %#x, status: %s (%#x)\n",
  237. &mc_io->portal_phys_addr,
  238. (unsigned int)mc_cmd_hdr_read_token(cmd),
  239. (unsigned int)mc_cmd_hdr_read_cmdid(cmd),
  240. mc_status_to_string(status),
  241. (unsigned int)status);
  242. error = mc_status_to_error(status);
  243. goto common_exit;
  244. }
  245. error = 0;
  246. common_exit:
  247. if (mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL)
  248. spin_unlock_irqrestore(&mc_io->spinlock, irq_flags);
  249. else
  250. mutex_unlock(&mc_io->mutex);
  251. return error;
  252. }
  253. EXPORT_SYMBOL_GPL(mc_send_command);