mc-sys.c 8.4 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. 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. * mc_polling_wait_preemptible() - Waits for the completion of an MC
  130. * command doing preemptible polling.
  131. * uslepp_range() is called between
  132. * polling iterations.
  133. * @mc_io: MC I/O object to be used
  134. * @cmd: command buffer to receive MC response
  135. * @mc_status: MC command completion status
  136. */
  137. static int mc_polling_wait_preemptible(struct fsl_mc_io *mc_io,
  138. struct fsl_mc_command *cmd,
  139. enum mc_cmd_status *mc_status)
  140. {
  141. enum mc_cmd_status status;
  142. unsigned long jiffies_until_timeout =
  143. jiffies + msecs_to_jiffies(MC_CMD_COMPLETION_TIMEOUT_MS);
  144. /*
  145. * Wait for response from the MC hardware:
  146. */
  147. for (;;) {
  148. status = mc_read_response(mc_io->portal_virt_addr, cmd);
  149. if (status != MC_CMD_STATUS_READY)
  150. break;
  151. /*
  152. * TODO: When MC command completion interrupts are supported
  153. * call wait function here instead of usleep_range()
  154. */
  155. usleep_range(MC_CMD_COMPLETION_POLLING_MIN_SLEEP_USECS,
  156. MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS);
  157. if (time_after_eq(jiffies, jiffies_until_timeout)) {
  158. dev_dbg(mc_io->dev,
  159. "MC command timed out (portal: %pa, dprc handle: %#x, command: %#x)\n",
  160. &mc_io->portal_phys_addr,
  161. (unsigned int)mc_cmd_hdr_read_token(cmd),
  162. (unsigned int)mc_cmd_hdr_read_cmdid(cmd));
  163. return -ETIMEDOUT;
  164. }
  165. }
  166. *mc_status = status;
  167. return 0;
  168. }
  169. /**
  170. * mc_polling_wait_atomic() - Waits for the completion of an MC command
  171. * doing atomic polling. udelay() is called
  172. * between polling iterations.
  173. * @mc_io: MC I/O object to be used
  174. * @cmd: command buffer to receive MC response
  175. * @mc_status: MC command completion status
  176. */
  177. static int mc_polling_wait_atomic(struct fsl_mc_io *mc_io,
  178. struct fsl_mc_command *cmd,
  179. enum mc_cmd_status *mc_status)
  180. {
  181. enum mc_cmd_status status;
  182. unsigned long timeout_usecs = MC_CMD_COMPLETION_TIMEOUT_MS * 1000;
  183. BUILD_BUG_ON((MC_CMD_COMPLETION_TIMEOUT_MS * 1000) %
  184. MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS != 0);
  185. for (;;) {
  186. status = mc_read_response(mc_io->portal_virt_addr, cmd);
  187. if (status != MC_CMD_STATUS_READY)
  188. break;
  189. udelay(MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS);
  190. timeout_usecs -= MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS;
  191. if (timeout_usecs == 0) {
  192. dev_dbg(mc_io->dev,
  193. "MC command timed out (portal: %pa, dprc handle: %#x, command: %#x)\n",
  194. &mc_io->portal_phys_addr,
  195. (unsigned int)mc_cmd_hdr_read_token(cmd),
  196. (unsigned int)mc_cmd_hdr_read_cmdid(cmd));
  197. return -ETIMEDOUT;
  198. }
  199. }
  200. *mc_status = status;
  201. return 0;
  202. }
  203. /**
  204. * mc_send_command() - Sends a command to the MC device using the given
  205. * MC I/O object
  206. * @mc_io: MC I/O object to be used
  207. * @cmd: command to be sent
  208. *
  209. * Returns '0' on Success; Error code otherwise.
  210. */
  211. int mc_send_command(struct fsl_mc_io *mc_io, struct fsl_mc_command *cmd)
  212. {
  213. int error;
  214. enum mc_cmd_status status;
  215. unsigned long irq_flags = 0;
  216. if (in_irq() && !(mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL))
  217. return -EINVAL;
  218. if (mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL)
  219. raw_spin_lock_irqsave(&mc_io->spinlock, irq_flags);
  220. else
  221. mutex_lock(&mc_io->mutex);
  222. /*
  223. * Send command to the MC hardware:
  224. */
  225. mc_write_command(mc_io->portal_virt_addr, cmd);
  226. /*
  227. * Wait for response from the MC hardware:
  228. */
  229. if (!(mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL))
  230. error = mc_polling_wait_preemptible(mc_io, cmd, &status);
  231. else
  232. error = mc_polling_wait_atomic(mc_io, cmd, &status);
  233. if (error < 0)
  234. goto common_exit;
  235. if (status != MC_CMD_STATUS_OK) {
  236. dev_dbg(mc_io->dev,
  237. "MC command failed: portal: %pa, dprc handle: %#x, command: %#x, status: %s (%#x)\n",
  238. &mc_io->portal_phys_addr,
  239. (unsigned int)mc_cmd_hdr_read_token(cmd),
  240. (unsigned int)mc_cmd_hdr_read_cmdid(cmd),
  241. mc_status_to_string(status),
  242. (unsigned int)status);
  243. error = mc_status_to_error(status);
  244. goto common_exit;
  245. }
  246. error = 0;
  247. common_exit:
  248. if (mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL)
  249. raw_spin_unlock_irqrestore(&mc_io->spinlock, irq_flags);
  250. else
  251. mutex_unlock(&mc_io->mutex);
  252. return error;
  253. }
  254. EXPORT_SYMBOL_GPL(mc_send_command);