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 "../include/mc.h"
45 * Timeout in milliseconds to wait for the completion of an MC command
47 #define MC_CMD_COMPLETION_TIMEOUT_MS 500
50 * usleep_range() min and max values used to throttle down polling
51 * iterations while waiting for MC command completion
53 #define MC_CMD_COMPLETION_POLLING_MIN_SLEEP_USECS 10
54 #define MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS 500
56 static enum mc_cmd_status
mc_cmd_hdr_read_status(struct mc_command
*cmd
)
58 struct mc_cmd_header
*hdr
= (struct mc_cmd_header
*)&cmd
->header
;
60 return (enum mc_cmd_status
)hdr
->status
;
63 static u16
mc_cmd_hdr_read_cmdid(struct mc_command
*cmd
)
65 struct mc_cmd_header
*hdr
= (struct mc_cmd_header
*)&cmd
->header
;
66 u16 cmd_id
= le16_to_cpu(hdr
->cmd_id
);
71 static int mc_status_to_error(enum mc_cmd_status status
)
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
,
87 if (WARN_ON((u32
)status
>= ARRAY_SIZE(mc_status_to_error_map
)))
90 return mc_status_to_error_map
[status
];
93 static const char *mc_status_to_string(enum mc_cmd_status status
)
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"
110 if ((unsigned int)status
>= ARRAY_SIZE(status_strings
))
111 return "Unknown MC error";
113 return status_strings
[status
];
117 * mc_write_command - writes a command to a Management Complex (MC) portal
119 * @portal: pointer to an MC portal
120 * @cmd: pointer to a filled command
122 static inline void mc_write_command(struct mc_command __iomem
*portal
,
123 struct mc_command
*cmd
)
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
]);
132 /* submit the command by writing the header */
133 __raw_writeq(cmd
->header
, &portal
->header
);
137 * mc_read_response - reads the response for the last MC command from a
138 * Management Complex (MC) portal
140 * @portal: pointer to an MC portal
141 * @resp: pointer to command response buffer
143 * Returns MC_CMD_STATUS_OK on Success; Error code otherwise.
145 static inline enum mc_cmd_status
mc_read_response(struct mc_command __iomem
*
147 struct mc_command
*resp
)
150 enum mc_cmd_status status
;
152 /* Copy command response header from MC portal: */
154 resp
->header
= __raw_readq(&portal
->header
);
156 status
= mc_cmd_hdr_read_status(resp
);
157 if (status
!= MC_CMD_STATUS_OK
)
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
]);
169 * Waits for the completion of an MC command doing preemptible polling.
170 * uslepp_range() is called between polling iterations.
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
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
)
180 enum mc_cmd_status status
;
181 unsigned long jiffies_until_timeout
=
182 jiffies
+ msecs_to_jiffies(MC_CMD_COMPLETION_TIMEOUT_MS
);
185 * Wait for response from the MC hardware:
188 status
= mc_read_response(mc_io
->portal_virt_addr
, cmd
);
189 if (status
!= MC_CMD_STATUS_READY
)
193 * TODO: When MC command completion interrupts are supported
194 * call wait function here instead of usleep_range()
196 usleep_range(MC_CMD_COMPLETION_POLLING_MIN_SLEEP_USECS
,
197 MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS
);
199 if (time_after_eq(jiffies
, jiffies_until_timeout
)) {
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
));
215 * Waits for the completion of an MC command doing atomic polling.
216 * udelay() is called between polling iterations.
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
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
)
226 enum mc_cmd_status status
;
227 unsigned long timeout_usecs
= MC_CMD_COMPLETION_TIMEOUT_MS
* 1000;
229 BUILD_BUG_ON((MC_CMD_COMPLETION_TIMEOUT_MS
* 1000) %
230 MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS
!= 0);
233 status
= mc_read_response(mc_io
->portal_virt_addr
, cmd
);
234 if (status
!= MC_CMD_STATUS_READY
)
237 udelay(MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS
);
238 timeout_usecs
-= MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS
;
239 if (timeout_usecs
== 0) {
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
));
255 * Sends a command to the MC device using the given MC I/O object
257 * @mc_io: MC I/O object to be used
258 * @cmd: command to be sent
260 * Returns '0' on Success; Error code otherwise.
262 int mc_send_command(struct fsl_mc_io
*mc_io
, struct mc_command
*cmd
)
265 enum mc_cmd_status status
;
266 unsigned long irq_flags
= 0;
268 if (WARN_ON(in_irq() &&
269 !(mc_io
->flags
& FSL_MC_IO_ATOMIC_CONTEXT_PORTAL
)))
272 if (mc_io
->flags
& FSL_MC_IO_ATOMIC_CONTEXT_PORTAL
)
273 spin_lock_irqsave(&mc_io
->spinlock
, irq_flags
);
275 mutex_lock(&mc_io
->mutex
);
278 * Send command to the MC hardware:
280 mc_write_command(mc_io
->portal_virt_addr
, cmd
);
283 * Wait for response from the MC hardware:
285 if (!(mc_io
->flags
& FSL_MC_IO_ATOMIC_CONTEXT_PORTAL
))
286 error
= mc_polling_wait_preemptible(mc_io
, cmd
, &status
);
288 error
= mc_polling_wait_atomic(mc_io
, cmd
, &status
);
293 if (status
!= MC_CMD_STATUS_OK
) {
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
);
302 error
= mc_status_to_error(status
);
308 if (mc_io
->flags
& FSL_MC_IO_ATOMIC_CONTEXT_PORTAL
)
309 spin_unlock_irqrestore(&mc_io
->spinlock
, irq_flags
);
311 mutex_unlock(&mc_io
->mutex
);
315 EXPORT_SYMBOL(mc_send_command
);