Commit | Line | Data |
---|---|---|
31c88965 GR |
1 | /* Copyright 2013-2014 Freescale Semiconductor Inc. |
2 | * | |
3 | * I/O services to send MC commands to the MC hardware | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions are met: | |
7 | * * Redistributions of source code must retain the above copyright | |
8 | * notice, this list of conditions and the following disclaimer. | |
9 | * * Redistributions in binary form must reproduce the above copyright | |
10 | * notice, this list of conditions and the following disclaimer in the | |
11 | * documentation and/or other materials provided with the distribution. | |
12 | * * Neither the name of the above-listed copyright holders nor the | |
13 | * names of any contributors may be used to endorse or promote products | |
14 | * derived from this software without specific prior written permission. | |
15 | * | |
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 "../include/mc-sys.h" | |
36 | #include "../include/mc-cmd.h" | |
ffcd52ef | 37 | #include "../include/mc.h" |
31c88965 GR |
38 | #include <linux/delay.h> |
39 | #include <linux/slab.h> | |
40 | #include <linux/ioport.h> | |
41 | #include <linux/device.h> | |
ffcd52ef | 42 | #include "dpmcp.h" |
31c88965 GR |
43 | |
44 | /** | |
c6a3363c | 45 | * Timeout in milliseconds to wait for the completion of an MC command |
31c88965 | 46 | */ |
c6a3363c | 47 | #define MC_CMD_COMPLETION_TIMEOUT_MS 500 |
31c88965 GR |
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 | #define MC_CMD_HDR_READ_CMDID(_hdr) \ | |
ba72f25b | 57 | ((u16)mc_dec((_hdr), MC_CMD_HDR_CMDID_O, MC_CMD_HDR_CMDID_S)) |
31c88965 GR |
58 | |
59 | /** | |
60 | * Creates an MC I/O object | |
61 | * | |
62 | * @dev: device to be associated with the MC I/O object | |
63 | * @mc_portal_phys_addr: physical address of the MC portal to use | |
64 | * @mc_portal_size: size in bytes of the MC portal | |
ffcd52ef GR |
65 | * @dpmcp-dev: Pointer to the DPMCP object associated with this MC I/O |
66 | * object or NULL if none. | |
31c88965 GR |
67 | * @flags: flags for the new MC I/O object |
68 | * @new_mc_io: Area to return pointer to newly created MC I/O object | |
69 | * | |
70 | * Returns '0' on Success; Error code otherwise. | |
71 | */ | |
72 | int __must_check fsl_create_mc_io(struct device *dev, | |
73 | phys_addr_t mc_portal_phys_addr, | |
ba72f25b | 74 | u32 mc_portal_size, |
ffcd52ef | 75 | struct fsl_mc_device *dpmcp_dev, |
ba72f25b | 76 | u32 flags, struct fsl_mc_io **new_mc_io) |
31c88965 | 77 | { |
d2f84991 | 78 | int error; |
31c88965 GR |
79 | struct fsl_mc_io *mc_io; |
80 | void __iomem *mc_portal_virt_addr; | |
81 | struct resource *res; | |
82 | ||
83 | mc_io = devm_kzalloc(dev, sizeof(*mc_io), GFP_KERNEL); | |
84 | if (!mc_io) | |
85 | return -ENOMEM; | |
86 | ||
87 | mc_io->dev = dev; | |
88 | mc_io->flags = flags; | |
89 | mc_io->portal_phys_addr = mc_portal_phys_addr; | |
90 | mc_io->portal_size = mc_portal_size; | |
63f2be5c GR |
91 | if (flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL) |
92 | spin_lock_init(&mc_io->spinlock); | |
93 | else | |
94 | mutex_init(&mc_io->mutex); | |
95 | ||
31c88965 GR |
96 | res = devm_request_mem_region(dev, |
97 | mc_portal_phys_addr, | |
98 | mc_portal_size, | |
99 | "mc_portal"); | |
100 | if (!res) { | |
101 | dev_err(dev, | |
102 | "devm_request_mem_region failed for MC portal %#llx\n", | |
103 | mc_portal_phys_addr); | |
104 | return -EBUSY; | |
105 | } | |
106 | ||
107 | mc_portal_virt_addr = devm_ioremap_nocache(dev, | |
108 | mc_portal_phys_addr, | |
109 | mc_portal_size); | |
110 | if (!mc_portal_virt_addr) { | |
111 | dev_err(dev, | |
112 | "devm_ioremap_nocache failed for MC portal %#llx\n", | |
113 | mc_portal_phys_addr); | |
114 | return -ENXIO; | |
115 | } | |
116 | ||
117 | mc_io->portal_virt_addr = mc_portal_virt_addr; | |
d2f84991 GR |
118 | if (dpmcp_dev) { |
119 | error = fsl_mc_io_set_dpmcp(mc_io, dpmcp_dev); | |
120 | if (error < 0) | |
121 | goto error_destroy_mc_io; | |
122 | } | |
123 | ||
31c88965 GR |
124 | *new_mc_io = mc_io; |
125 | return 0; | |
d2f84991 GR |
126 | |
127 | error_destroy_mc_io: | |
128 | fsl_destroy_mc_io(mc_io); | |
129 | return error; | |
31c88965 GR |
130 | } |
131 | EXPORT_SYMBOL_GPL(fsl_create_mc_io); | |
132 | ||
133 | /** | |
134 | * Destroys an MC I/O object | |
135 | * | |
136 | * @mc_io: MC I/O object to destroy | |
137 | */ | |
138 | void fsl_destroy_mc_io(struct fsl_mc_io *mc_io) | |
139 | { | |
d2f84991 GR |
140 | struct fsl_mc_device *dpmcp_dev = mc_io->dpmcp_dev; |
141 | ||
142 | if (dpmcp_dev) | |
143 | fsl_mc_io_unset_dpmcp(mc_io); | |
144 | ||
31c88965 GR |
145 | devm_iounmap(mc_io->dev, mc_io->portal_virt_addr); |
146 | devm_release_mem_region(mc_io->dev, | |
147 | mc_io->portal_phys_addr, | |
148 | mc_io->portal_size); | |
149 | ||
150 | mc_io->portal_virt_addr = NULL; | |
151 | devm_kfree(mc_io->dev, mc_io); | |
152 | } | |
153 | EXPORT_SYMBOL_GPL(fsl_destroy_mc_io); | |
154 | ||
d2f84991 GR |
155 | int fsl_mc_io_set_dpmcp(struct fsl_mc_io *mc_io, |
156 | struct fsl_mc_device *dpmcp_dev) | |
157 | { | |
158 | int error; | |
159 | ||
160 | if (WARN_ON(!dpmcp_dev)) | |
161 | return -EINVAL; | |
162 | ||
163 | if (WARN_ON(mc_io->dpmcp_dev)) | |
164 | return -EINVAL; | |
165 | ||
166 | if (WARN_ON(dpmcp_dev->mc_io)) | |
167 | return -EINVAL; | |
168 | ||
169 | error = dpmcp_open(mc_io, | |
170 | 0, | |
171 | dpmcp_dev->obj_desc.id, | |
172 | &dpmcp_dev->mc_handle); | |
173 | if (error < 0) | |
174 | return error; | |
175 | ||
176 | mc_io->dpmcp_dev = dpmcp_dev; | |
177 | dpmcp_dev->mc_io = mc_io; | |
178 | return 0; | |
179 | } | |
180 | EXPORT_SYMBOL_GPL(fsl_mc_io_set_dpmcp); | |
181 | ||
182 | void fsl_mc_io_unset_dpmcp(struct fsl_mc_io *mc_io) | |
183 | { | |
184 | int error; | |
185 | struct fsl_mc_device *dpmcp_dev = mc_io->dpmcp_dev; | |
186 | ||
187 | if (WARN_ON(!dpmcp_dev)) | |
188 | return; | |
189 | ||
190 | if (WARN_ON(dpmcp_dev->mc_io != mc_io)) | |
191 | return; | |
192 | ||
193 | error = dpmcp_close(mc_io, | |
194 | 0, | |
195 | dpmcp_dev->mc_handle); | |
196 | if (error < 0) { | |
197 | dev_err(&dpmcp_dev->dev, "dpmcp_close() failed: %d\n", | |
198 | error); | |
199 | } | |
200 | ||
201 | mc_io->dpmcp_dev = NULL; | |
202 | dpmcp_dev->mc_io = NULL; | |
203 | } | |
204 | EXPORT_SYMBOL_GPL(fsl_mc_io_unset_dpmcp); | |
205 | ||
31c88965 GR |
206 | static int mc_status_to_error(enum mc_cmd_status status) |
207 | { | |
208 | static const int mc_status_to_error_map[] = { | |
209 | [MC_CMD_STATUS_OK] = 0, | |
210 | [MC_CMD_STATUS_AUTH_ERR] = -EACCES, | |
211 | [MC_CMD_STATUS_NO_PRIVILEGE] = -EPERM, | |
212 | [MC_CMD_STATUS_DMA_ERR] = -EIO, | |
213 | [MC_CMD_STATUS_CONFIG_ERR] = -ENXIO, | |
214 | [MC_CMD_STATUS_TIMEOUT] = -ETIMEDOUT, | |
215 | [MC_CMD_STATUS_NO_RESOURCE] = -ENAVAIL, | |
216 | [MC_CMD_STATUS_NO_MEMORY] = -ENOMEM, | |
217 | [MC_CMD_STATUS_BUSY] = -EBUSY, | |
218 | [MC_CMD_STATUS_UNSUPPORTED_OP] = -ENOTSUPP, | |
219 | [MC_CMD_STATUS_INVALID_STATE] = -ENODEV, | |
220 | }; | |
221 | ||
222 | if (WARN_ON((u32)status >= ARRAY_SIZE(mc_status_to_error_map))) | |
223 | return -EINVAL; | |
224 | ||
225 | return mc_status_to_error_map[status]; | |
226 | } | |
227 | ||
228 | static const char *mc_status_to_string(enum mc_cmd_status status) | |
229 | { | |
230 | static const char *const status_strings[] = { | |
231 | [MC_CMD_STATUS_OK] = "Command completed successfully", | |
232 | [MC_CMD_STATUS_READY] = "Command ready to be processed", | |
233 | [MC_CMD_STATUS_AUTH_ERR] = "Authentication error", | |
234 | [MC_CMD_STATUS_NO_PRIVILEGE] = "No privilege", | |
235 | [MC_CMD_STATUS_DMA_ERR] = "DMA or I/O error", | |
236 | [MC_CMD_STATUS_CONFIG_ERR] = "Configuration error", | |
237 | [MC_CMD_STATUS_TIMEOUT] = "Operation timed out", | |
238 | [MC_CMD_STATUS_NO_RESOURCE] = "No resources", | |
239 | [MC_CMD_STATUS_NO_MEMORY] = "No memory available", | |
240 | [MC_CMD_STATUS_BUSY] = "Device is busy", | |
241 | [MC_CMD_STATUS_UNSUPPORTED_OP] = "Unsupported operation", | |
242 | [MC_CMD_STATUS_INVALID_STATE] = "Invalid state" | |
243 | }; | |
244 | ||
245 | if ((unsigned int)status >= ARRAY_SIZE(status_strings)) | |
246 | return "Unknown MC error"; | |
247 | ||
248 | return status_strings[status]; | |
249 | } | |
250 | ||
251 | /** | |
252 | * mc_write_command - writes a command to a Management Complex (MC) portal | |
253 | * | |
254 | * @portal: pointer to an MC portal | |
255 | * @cmd: pointer to a filled command | |
256 | */ | |
257 | static inline void mc_write_command(struct mc_command __iomem *portal, | |
258 | struct mc_command *cmd) | |
259 | { | |
260 | int i; | |
261 | ||
262 | /* copy command parameters into the portal */ | |
263 | for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++) | |
264 | writeq(cmd->params[i], &portal->params[i]); | |
265 | ||
266 | /* submit the command by writing the header */ | |
267 | writeq(cmd->header, &portal->header); | |
268 | } | |
269 | ||
270 | /** | |
271 | * mc_read_response - reads the response for the last MC command from a | |
272 | * Management Complex (MC) portal | |
273 | * | |
274 | * @portal: pointer to an MC portal | |
275 | * @resp: pointer to command response buffer | |
276 | * | |
277 | * Returns MC_CMD_STATUS_OK on Success; Error code otherwise. | |
278 | */ | |
279 | static inline enum mc_cmd_status mc_read_response(struct mc_command __iomem * | |
280 | portal, | |
281 | struct mc_command *resp) | |
282 | { | |
283 | int i; | |
284 | enum mc_cmd_status status; | |
285 | ||
286 | /* Copy command response header from MC portal: */ | |
287 | resp->header = readq(&portal->header); | |
288 | status = MC_CMD_HDR_READ_STATUS(resp->header); | |
289 | if (status != MC_CMD_STATUS_OK) | |
290 | return status; | |
291 | ||
292 | /* Copy command response data from MC portal: */ | |
293 | for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++) | |
294 | resp->params[i] = readq(&portal->params[i]); | |
295 | ||
296 | return status; | |
297 | } | |
298 | ||
299 | /** | |
140305e7 GR |
300 | * Waits for the completion of an MC command doing preemptible polling. |
301 | * uslepp_range() is called between polling iterations. | |
31c88965 GR |
302 | * |
303 | * @mc_io: MC I/O object to be used | |
140305e7 GR |
304 | * @cmd: command buffer to receive MC response |
305 | * @mc_status: MC command completion status | |
31c88965 | 306 | */ |
140305e7 GR |
307 | static int mc_polling_wait_preemptible(struct fsl_mc_io *mc_io, |
308 | struct mc_command *cmd, | |
309 | enum mc_cmd_status *mc_status) | |
31c88965 GR |
310 | { |
311 | enum mc_cmd_status status; | |
312 | unsigned long jiffies_until_timeout = | |
c6a3363c | 313 | jiffies + msecs_to_jiffies(MC_CMD_COMPLETION_TIMEOUT_MS); |
31c88965 | 314 | |
31c88965 GR |
315 | /* |
316 | * Wait for response from the MC hardware: | |
317 | */ | |
318 | for (;;) { | |
319 | status = mc_read_response(mc_io->portal_virt_addr, cmd); | |
320 | if (status != MC_CMD_STATUS_READY) | |
321 | break; | |
322 | ||
323 | /* | |
324 | * TODO: When MC command completion interrupts are supported | |
325 | * call wait function here instead of usleep_range() | |
326 | */ | |
327 | usleep_range(MC_CMD_COMPLETION_POLLING_MIN_SLEEP_USECS, | |
328 | MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS); | |
329 | ||
330 | if (time_after_eq(jiffies, jiffies_until_timeout)) { | |
e79e344a | 331 | dev_dbg(mc_io->dev, |
de71daf5 | 332 | "MC command timed out (portal: %#llx, obj handle: %#x, command: %#x)\n", |
31c88965 GR |
333 | mc_io->portal_phys_addr, |
334 | (unsigned int) | |
335 | MC_CMD_HDR_READ_TOKEN(cmd->header), | |
336 | (unsigned int) | |
337 | MC_CMD_HDR_READ_CMDID(cmd->header)); | |
338 | ||
339 | return -ETIMEDOUT; | |
340 | } | |
341 | } | |
342 | ||
140305e7 GR |
343 | *mc_status = status; |
344 | return 0; | |
345 | } | |
346 | ||
3f95ad21 GR |
347 | /** |
348 | * Waits for the completion of an MC command doing atomic polling. | |
349 | * udelay() is called between polling iterations. | |
350 | * | |
351 | * @mc_io: MC I/O object to be used | |
352 | * @cmd: command buffer to receive MC response | |
353 | * @mc_status: MC command completion status | |
354 | */ | |
355 | static int mc_polling_wait_atomic(struct fsl_mc_io *mc_io, | |
356 | struct mc_command *cmd, | |
357 | enum mc_cmd_status *mc_status) | |
358 | { | |
359 | enum mc_cmd_status status; | |
360 | unsigned long timeout_usecs = MC_CMD_COMPLETION_TIMEOUT_MS * 1000; | |
361 | ||
362 | BUILD_BUG_ON((MC_CMD_COMPLETION_TIMEOUT_MS * 1000) % | |
363 | MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS != 0); | |
364 | ||
365 | for (;;) { | |
366 | status = mc_read_response(mc_io->portal_virt_addr, cmd); | |
367 | if (status != MC_CMD_STATUS_READY) | |
368 | break; | |
369 | ||
370 | udelay(MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS); | |
371 | timeout_usecs -= MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS; | |
372 | if (timeout_usecs == 0) { | |
e79e344a | 373 | dev_dbg(mc_io->dev, |
de71daf5 | 374 | "MC command timed out (portal: %#llx, obj handle: %#x, command: %#x)\n", |
3f95ad21 GR |
375 | mc_io->portal_phys_addr, |
376 | (unsigned int) | |
377 | MC_CMD_HDR_READ_TOKEN(cmd->header), | |
378 | (unsigned int) | |
379 | MC_CMD_HDR_READ_CMDID(cmd->header)); | |
380 | ||
381 | return -ETIMEDOUT; | |
382 | } | |
383 | } | |
384 | ||
385 | *mc_status = status; | |
386 | return 0; | |
387 | } | |
388 | ||
140305e7 GR |
389 | /** |
390 | * Sends a command to the MC device using the given MC I/O object | |
391 | * | |
392 | * @mc_io: MC I/O object to be used | |
393 | * @cmd: command to be sent | |
394 | * | |
395 | * Returns '0' on Success; Error code otherwise. | |
140305e7 GR |
396 | */ |
397 | int mc_send_command(struct fsl_mc_io *mc_io, struct mc_command *cmd) | |
398 | { | |
399 | int error; | |
400 | enum mc_cmd_status status; | |
63f2be5c | 401 | unsigned long irq_flags = 0; |
140305e7 | 402 | |
3f95ad21 GR |
403 | if (WARN_ON(in_irq() && |
404 | !(mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL))) | |
405 | return -EINVAL; | |
406 | ||
63f2be5c GR |
407 | if (mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL) |
408 | spin_lock_irqsave(&mc_io->spinlock, irq_flags); | |
409 | else | |
410 | mutex_lock(&mc_io->mutex); | |
411 | ||
140305e7 GR |
412 | /* |
413 | * Send command to the MC hardware: | |
414 | */ | |
415 | mc_write_command(mc_io->portal_virt_addr, cmd); | |
416 | ||
417 | /* | |
418 | * Wait for response from the MC hardware: | |
419 | */ | |
3f95ad21 GR |
420 | if (!(mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL)) |
421 | error = mc_polling_wait_preemptible(mc_io, cmd, &status); | |
422 | else | |
423 | error = mc_polling_wait_atomic(mc_io, cmd, &status); | |
424 | ||
140305e7 | 425 | if (error < 0) |
63f2be5c | 426 | goto common_exit; |
140305e7 | 427 | |
31c88965 | 428 | if (status != MC_CMD_STATUS_OK) { |
e79e344a | 429 | dev_dbg(mc_io->dev, |
de71daf5 | 430 | "MC command failed: portal: %#llx, obj handle: %#x, command: %#x, status: %s (%#x)\n", |
31c88965 GR |
431 | mc_io->portal_phys_addr, |
432 | (unsigned int)MC_CMD_HDR_READ_TOKEN(cmd->header), | |
433 | (unsigned int)MC_CMD_HDR_READ_CMDID(cmd->header), | |
434 | mc_status_to_string(status), | |
435 | (unsigned int)status); | |
436 | ||
63f2be5c GR |
437 | error = mc_status_to_error(status); |
438 | goto common_exit; | |
31c88965 GR |
439 | } |
440 | ||
63f2be5c GR |
441 | error = 0; |
442 | common_exit: | |
443 | if (mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL) | |
444 | spin_unlock_irqrestore(&mc_io->spinlock, irq_flags); | |
445 | else | |
446 | mutex_unlock(&mc_io->mutex); | |
447 | ||
448 | return error; | |
31c88965 GR |
449 | } |
450 | EXPORT_SYMBOL(mc_send_command); |