]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/staging/fsl-mc/bus/mc-sys.c
Merge tag 'iio-for-4.13b' of git://git.kernel.org/pub/scm/linux/kernel/git/jic23...
[mirror_ubuntu-artful-kernel.git] / drivers / staging / fsl-mc / bus / mc-sys.c
1 /*
2 * Copyright 2013-2016 Freescale Semiconductor Inc.
3 *
4 * I/O services to send MC commands to the MC hardware
5 *
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.
16 *
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
20 * later version.
21 *
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.
33 */
34
35 #include <linux/delay.h>
36 #include <linux/slab.h>
37 #include <linux/ioport.h>
38 #include <linux/device.h>
39 #include <linux/io.h>
40 #include "../include/mc.h"
41
42 #include "dpmcp.h"
43
44 /**
45 * Timeout in milliseconds to wait for the completion of an MC command
46 */
47 #define MC_CMD_COMPLETION_TIMEOUT_MS 500
48
49 /*
50 * usleep_range() min and max values used to throttle down polling
51 * iterations while waiting for MC command completion
52 */
53 #define MC_CMD_COMPLETION_POLLING_MIN_SLEEP_USECS 10
54 #define MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS 500
55
56 static enum mc_cmd_status mc_cmd_hdr_read_status(struct mc_command *cmd)
57 {
58 struct mc_cmd_header *hdr = (struct mc_cmd_header *)&cmd->header;
59
60 return (enum mc_cmd_status)hdr->status;
61 }
62
63 static u16 mc_cmd_hdr_read_cmdid(struct mc_command *cmd)
64 {
65 struct mc_cmd_header *hdr = (struct mc_cmd_header *)&cmd->header;
66 u16 cmd_id = le16_to_cpu(hdr->cmd_id);
67
68 return cmd_id;
69 }
70
71 static int mc_status_to_error(enum mc_cmd_status status)
72 {
73 static const int mc_status_to_error_map[] = {
74 [MC_CMD_STATUS_OK] = 0,
75 [MC_CMD_STATUS_AUTH_ERR] = -EACCES,
76 [MC_CMD_STATUS_NO_PRIVILEGE] = -EPERM,
77 [MC_CMD_STATUS_DMA_ERR] = -EIO,
78 [MC_CMD_STATUS_CONFIG_ERR] = -ENXIO,
79 [MC_CMD_STATUS_TIMEOUT] = -ETIMEDOUT,
80 [MC_CMD_STATUS_NO_RESOURCE] = -ENAVAIL,
81 [MC_CMD_STATUS_NO_MEMORY] = -ENOMEM,
82 [MC_CMD_STATUS_BUSY] = -EBUSY,
83 [MC_CMD_STATUS_UNSUPPORTED_OP] = -ENOTSUPP,
84 [MC_CMD_STATUS_INVALID_STATE] = -ENODEV,
85 };
86
87 if (WARN_ON((u32)status >= ARRAY_SIZE(mc_status_to_error_map)))
88 return -EINVAL;
89
90 return mc_status_to_error_map[status];
91 }
92
93 static const char *mc_status_to_string(enum mc_cmd_status status)
94 {
95 static const char *const status_strings[] = {
96 [MC_CMD_STATUS_OK] = "Command completed successfully",
97 [MC_CMD_STATUS_READY] = "Command ready to be processed",
98 [MC_CMD_STATUS_AUTH_ERR] = "Authentication error",
99 [MC_CMD_STATUS_NO_PRIVILEGE] = "No privilege",
100 [MC_CMD_STATUS_DMA_ERR] = "DMA or I/O error",
101 [MC_CMD_STATUS_CONFIG_ERR] = "Configuration error",
102 [MC_CMD_STATUS_TIMEOUT] = "Operation timed out",
103 [MC_CMD_STATUS_NO_RESOURCE] = "No resources",
104 [MC_CMD_STATUS_NO_MEMORY] = "No memory available",
105 [MC_CMD_STATUS_BUSY] = "Device is busy",
106 [MC_CMD_STATUS_UNSUPPORTED_OP] = "Unsupported operation",
107 [MC_CMD_STATUS_INVALID_STATE] = "Invalid state"
108 };
109
110 if ((unsigned int)status >= ARRAY_SIZE(status_strings))
111 return "Unknown MC error";
112
113 return status_strings[status];
114 }
115
116 /**
117 * mc_write_command - writes a command to a Management Complex (MC) portal
118 *
119 * @portal: pointer to an MC portal
120 * @cmd: pointer to a filled command
121 */
122 static inline void mc_write_command(struct mc_command __iomem *portal,
123 struct mc_command *cmd)
124 {
125 int i;
126
127 /* copy command parameters into the portal */
128 for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
129 __raw_writeq(cmd->params[i], &portal->params[i]);
130 __iowmb();
131
132 /* submit the command by writing the header */
133 __raw_writeq(cmd->header, &portal->header);
134 }
135
136 /**
137 * mc_read_response - reads the response for the last MC command from a
138 * Management Complex (MC) portal
139 *
140 * @portal: pointer to an MC portal
141 * @resp: pointer to command response buffer
142 *
143 * Returns MC_CMD_STATUS_OK on Success; Error code otherwise.
144 */
145 static inline enum mc_cmd_status mc_read_response(struct mc_command __iomem *
146 portal,
147 struct mc_command *resp)
148 {
149 int i;
150 enum mc_cmd_status status;
151
152 /* Copy command response header from MC portal: */
153 __iormb();
154 resp->header = __raw_readq(&portal->header);
155 __iormb();
156 status = mc_cmd_hdr_read_status(resp);
157 if (status != MC_CMD_STATUS_OK)
158 return status;
159
160 /* Copy command response data from MC portal: */
161 for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
162 resp->params[i] = __raw_readq(&portal->params[i]);
163 __iormb();
164
165 return status;
166 }
167
168 /**
169 * Waits for the completion of an MC command doing preemptible polling.
170 * uslepp_range() 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_preemptible(struct fsl_mc_io *mc_io,
177 struct mc_command *cmd,
178 enum mc_cmd_status *mc_status)
179 {
180 enum mc_cmd_status status;
181 unsigned long jiffies_until_timeout =
182 jiffies + msecs_to_jiffies(MC_CMD_COMPLETION_TIMEOUT_MS);
183
184 /*
185 * Wait for response from the MC hardware:
186 */
187 for (;;) {
188 status = mc_read_response(mc_io->portal_virt_addr, cmd);
189 if (status != MC_CMD_STATUS_READY)
190 break;
191
192 /*
193 * TODO: When MC command completion interrupts are supported
194 * call wait function here instead of usleep_range()
195 */
196 usleep_range(MC_CMD_COMPLETION_POLLING_MIN_SLEEP_USECS,
197 MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS);
198
199 if (time_after_eq(jiffies, jiffies_until_timeout)) {
200 dev_dbg(mc_io->dev,
201 "MC command timed out (portal: %#llx, dprc handle: %#x, command: %#x)\n",
202 mc_io->portal_phys_addr,
203 (unsigned int)mc_cmd_hdr_read_token(cmd),
204 (unsigned int)mc_cmd_hdr_read_cmdid(cmd));
205
206 return -ETIMEDOUT;
207 }
208 }
209
210 *mc_status = status;
211 return 0;
212 }
213
214 /**
215 * Waits for the completion of an MC command doing atomic polling.
216 * udelay() is called between polling iterations.
217 *
218 * @mc_io: MC I/O object to be used
219 * @cmd: command buffer to receive MC response
220 * @mc_status: MC command completion status
221 */
222 static int mc_polling_wait_atomic(struct fsl_mc_io *mc_io,
223 struct mc_command *cmd,
224 enum mc_cmd_status *mc_status)
225 {
226 enum mc_cmd_status status;
227 unsigned long timeout_usecs = MC_CMD_COMPLETION_TIMEOUT_MS * 1000;
228
229 BUILD_BUG_ON((MC_CMD_COMPLETION_TIMEOUT_MS * 1000) %
230 MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS != 0);
231
232 for (;;) {
233 status = mc_read_response(mc_io->portal_virt_addr, cmd);
234 if (status != MC_CMD_STATUS_READY)
235 break;
236
237 udelay(MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS);
238 timeout_usecs -= MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS;
239 if (timeout_usecs == 0) {
240 dev_dbg(mc_io->dev,
241 "MC command timed out (portal: %#llx, dprc handle: %#x, command: %#x)\n",
242 mc_io->portal_phys_addr,
243 (unsigned int)mc_cmd_hdr_read_token(cmd),
244 (unsigned int)mc_cmd_hdr_read_cmdid(cmd));
245
246 return -ETIMEDOUT;
247 }
248 }
249
250 *mc_status = status;
251 return 0;
252 }
253
254 /**
255 * Sends a command to the MC device using the given MC I/O object
256 *
257 * @mc_io: MC I/O object to be used
258 * @cmd: command to be sent
259 *
260 * Returns '0' on Success; Error code otherwise.
261 */
262 int mc_send_command(struct fsl_mc_io *mc_io, struct mc_command *cmd)
263 {
264 int error;
265 enum mc_cmd_status status;
266 unsigned long irq_flags = 0;
267
268 if (WARN_ON(in_irq() &&
269 !(mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL)))
270 return -EINVAL;
271
272 if (mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL)
273 spin_lock_irqsave(&mc_io->spinlock, irq_flags);
274 else
275 mutex_lock(&mc_io->mutex);
276
277 /*
278 * Send command to the MC hardware:
279 */
280 mc_write_command(mc_io->portal_virt_addr, cmd);
281
282 /*
283 * Wait for response from the MC hardware:
284 */
285 if (!(mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL))
286 error = mc_polling_wait_preemptible(mc_io, cmd, &status);
287 else
288 error = mc_polling_wait_atomic(mc_io, cmd, &status);
289
290 if (error < 0)
291 goto common_exit;
292
293 if (status != MC_CMD_STATUS_OK) {
294 dev_dbg(mc_io->dev,
295 "MC command failed: portal: %#llx, dprc handle: %#x, command: %#x, status: %s (%#x)\n",
296 mc_io->portal_phys_addr,
297 (unsigned int)mc_cmd_hdr_read_token(cmd),
298 (unsigned int)mc_cmd_hdr_read_cmdid(cmd),
299 mc_status_to_string(status),
300 (unsigned int)status);
301
302 error = mc_status_to_error(status);
303 goto common_exit;
304 }
305
306 error = 0;
307 common_exit:
308 if (mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL)
309 spin_unlock_irqrestore(&mc_io->spinlock, irq_flags);
310 else
311 mutex_unlock(&mc_io->mutex);
312
313 return error;
314 }
315 EXPORT_SYMBOL(mc_send_command);