2 * Copyright 2013-2016 Freescale Semiconductor Inc.
4 * I/O services to send MC commands to the MC hardware
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the above-listed copyright holders nor the
14 * names of any contributors may be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * ALTERNATIVELY, this software may be distributed under the terms of the
18 * GNU General Public License ("GPL") as published by the Free Software
19 * Foundation, either version 2 of that License or (at your option) any
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/delay.h>
36 #include <linux/slab.h>
37 #include <linux/ioport.h>
38 #include <linux/device.h>
40 #include <linux/io-64-nonatomic-hi-lo.h>
41 #include "../include/mc.h"
46 * Timeout in milliseconds to wait for the completion of an MC command
48 #define MC_CMD_COMPLETION_TIMEOUT_MS 500
51 * usleep_range() min and max values used to throttle down polling
52 * iterations while waiting for MC command completion
54 #define MC_CMD_COMPLETION_POLLING_MIN_SLEEP_USECS 10
55 #define MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS 500
57 static enum mc_cmd_status mc_cmd_hdr_read_status(struct mc_command *cmd)
59 struct mc_cmd_header *hdr = (struct mc_cmd_header *)&cmd->header;
61 return (enum mc_cmd_status)hdr->status;
64 static u16 mc_cmd_hdr_read_cmdid(struct mc_command *cmd)
66 struct mc_cmd_header *hdr = (struct mc_cmd_header *)&cmd->header;
67 u16 cmd_id = le16_to_cpu(hdr->cmd_id);
72 static int mc_status_to_error(enum mc_cmd_status status)
74 static const int mc_status_to_error_map[] = {
75 [MC_CMD_STATUS_OK] = 0,
76 [MC_CMD_STATUS_AUTH_ERR] = -EACCES,
77 [MC_CMD_STATUS_NO_PRIVILEGE] = -EPERM,
78 [MC_CMD_STATUS_DMA_ERR] = -EIO,
79 [MC_CMD_STATUS_CONFIG_ERR] = -ENXIO,
80 [MC_CMD_STATUS_TIMEOUT] = -ETIMEDOUT,
81 [MC_CMD_STATUS_NO_RESOURCE] = -ENAVAIL,
82 [MC_CMD_STATUS_NO_MEMORY] = -ENOMEM,
83 [MC_CMD_STATUS_BUSY] = -EBUSY,
84 [MC_CMD_STATUS_UNSUPPORTED_OP] = -ENOTSUPP,
85 [MC_CMD_STATUS_INVALID_STATE] = -ENODEV,
88 if (WARN_ON((u32)status >= ARRAY_SIZE(mc_status_to_error_map)))
91 return mc_status_to_error_map[status];
94 static const char *mc_status_to_string(enum mc_cmd_status status)
96 static const char *const status_strings[] = {
97 [MC_CMD_STATUS_OK] = "Command completed successfully",
98 [MC_CMD_STATUS_READY] = "Command ready to be processed",
99 [MC_CMD_STATUS_AUTH_ERR] = "Authentication error",
100 [MC_CMD_STATUS_NO_PRIVILEGE] = "No privilege",
101 [MC_CMD_STATUS_DMA_ERR] = "DMA or I/O error",
102 [MC_CMD_STATUS_CONFIG_ERR] = "Configuration error",
103 [MC_CMD_STATUS_TIMEOUT] = "Operation timed out",
104 [MC_CMD_STATUS_NO_RESOURCE] = "No resources",
105 [MC_CMD_STATUS_NO_MEMORY] = "No memory available",
106 [MC_CMD_STATUS_BUSY] = "Device is busy",
107 [MC_CMD_STATUS_UNSUPPORTED_OP] = "Unsupported operation",
108 [MC_CMD_STATUS_INVALID_STATE] = "Invalid state"
111 if ((unsigned int)status >= ARRAY_SIZE(status_strings))
112 return "Unknown MC error";
114 return status_strings[status];
118 * mc_write_command - writes a command to a Management Complex (MC) portal
120 * @portal: pointer to an MC portal
121 * @cmd: pointer to a filled command
123 static inline void mc_write_command(struct mc_command __iomem *portal,
124 struct mc_command *cmd)
128 /* copy command parameters into the portal */
129 for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
131 * Data is already in the expected LE byte-order. Do an
132 * extra LE -> CPU conversion so that the CPU -> LE done in
133 * the device io write api puts it back in the right order.
135 writeq_relaxed(le64_to_cpu(cmd->params[i]), &portal->params[i]);
137 /* submit the command by writing the header */
138 writeq(le64_to_cpu(cmd->header), &portal->header);
142 * mc_read_response - reads the response for the last MC command from a
143 * Management Complex (MC) portal
145 * @portal: pointer to an MC portal
146 * @resp: pointer to command response buffer
148 * Returns MC_CMD_STATUS_OK on Success; Error code otherwise.
150 static inline enum mc_cmd_status mc_read_response(struct mc_command __iomem *
152 struct mc_command *resp)
155 enum mc_cmd_status status;
157 /* Copy command response header from MC portal: */
158 resp->header = cpu_to_le64(readq_relaxed(&portal->header));
159 status = mc_cmd_hdr_read_status(resp);
160 if (status != MC_CMD_STATUS_OK)
163 /* Copy command response data from MC portal: */
164 for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
166 * Data is expected to be in LE byte-order. Do an
167 * extra CPU -> LE to revert the LE -> CPU done in
168 * the device io read api.
171 cpu_to_le64(readq_relaxed(&portal->params[i]));
177 * Waits for the completion of an MC command doing preemptible polling.
178 * uslepp_range() is called between polling iterations.
180 * @mc_io: MC I/O object to be used
181 * @cmd: command buffer to receive MC response
182 * @mc_status: MC command completion status
184 static int mc_polling_wait_preemptible(struct fsl_mc_io *mc_io,
185 struct mc_command *cmd,
186 enum mc_cmd_status *mc_status)
188 enum mc_cmd_status status;
189 unsigned long jiffies_until_timeout =
190 jiffies + msecs_to_jiffies(MC_CMD_COMPLETION_TIMEOUT_MS);
193 * Wait for response from the MC hardware:
196 status = mc_read_response(mc_io->portal_virt_addr, cmd);
197 if (status != MC_CMD_STATUS_READY)
201 * TODO: When MC command completion interrupts are supported
202 * call wait function here instead of usleep_range()
204 usleep_range(MC_CMD_COMPLETION_POLLING_MIN_SLEEP_USECS,
205 MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS);
207 if (time_after_eq(jiffies, jiffies_until_timeout)) {
209 "MC command timed out (portal: %pa, dprc handle: %#x, command: %#x)\n",
210 &mc_io->portal_phys_addr,
211 (unsigned int)mc_cmd_hdr_read_token(cmd),
212 (unsigned int)mc_cmd_hdr_read_cmdid(cmd));
223 * Waits for the completion of an MC command doing atomic polling.
224 * udelay() is called between polling iterations.
226 * @mc_io: MC I/O object to be used
227 * @cmd: command buffer to receive MC response
228 * @mc_status: MC command completion status
230 static int mc_polling_wait_atomic(struct fsl_mc_io *mc_io,
231 struct mc_command *cmd,
232 enum mc_cmd_status *mc_status)
234 enum mc_cmd_status status;
235 unsigned long timeout_usecs = MC_CMD_COMPLETION_TIMEOUT_MS * 1000;
237 BUILD_BUG_ON((MC_CMD_COMPLETION_TIMEOUT_MS * 1000) %
238 MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS != 0);
241 status = mc_read_response(mc_io->portal_virt_addr, cmd);
242 if (status != MC_CMD_STATUS_READY)
245 udelay(MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS);
246 timeout_usecs -= MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS;
247 if (timeout_usecs == 0) {
249 "MC command timed out (portal: %pa, dprc handle: %#x, command: %#x)\n",
250 &mc_io->portal_phys_addr,
251 (unsigned int)mc_cmd_hdr_read_token(cmd),
252 (unsigned int)mc_cmd_hdr_read_cmdid(cmd));
263 * Sends a command to the MC device using the given MC I/O object
265 * @mc_io: MC I/O object to be used
266 * @cmd: command to be sent
268 * Returns '0' on Success; Error code otherwise.
270 int mc_send_command(struct fsl_mc_io *mc_io, struct mc_command *cmd)
273 enum mc_cmd_status status;
274 unsigned long irq_flags = 0;
276 if (WARN_ON(in_irq() &&
277 !(mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL)))
280 if (mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL)
281 spin_lock_irqsave(&mc_io->spinlock, irq_flags);
283 mutex_lock(&mc_io->mutex);
286 * Send command to the MC hardware:
288 mc_write_command(mc_io->portal_virt_addr, cmd);
291 * Wait for response from the MC hardware:
293 if (!(mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL))
294 error = mc_polling_wait_preemptible(mc_io, cmd, &status);
296 error = mc_polling_wait_atomic(mc_io, cmd, &status);
301 if (status != MC_CMD_STATUS_OK) {
303 "MC command failed: portal: %pa, dprc handle: %#x, command: %#x, status: %s (%#x)\n",
304 &mc_io->portal_phys_addr,
305 (unsigned int)mc_cmd_hdr_read_token(cmd),
306 (unsigned int)mc_cmd_hdr_read_cmdid(cmd),
307 mc_status_to_string(status),
308 (unsigned int)status);
310 error = mc_status_to_error(status);
316 if (mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL)
317 spin_unlock_irqrestore(&mc_io->spinlock, irq_flags);
319 mutex_unlock(&mc_io->mutex);
323 EXPORT_SYMBOL(mc_send_command);