blob: 07848a0cbe5a618f788f0d4e44a36626cfaefe8a [file] [log] [blame]
J. German Rivera31c88962015-03-05 19:29:09 -06001/* 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"
J. German Riveraffcd52e2015-10-17 11:18:14 -050037#include "../include/mc.h"
J. German Rivera31c88962015-03-05 19:29:09 -060038#include <linux/delay.h>
39#include <linux/slab.h>
40#include <linux/ioport.h>
41#include <linux/device.h>
J. German Riveraffcd52e2015-10-17 11:18:14 -050042#include "dpmcp.h"
J. German Rivera31c88962015-03-05 19:29:09 -060043
44/**
45 * Timeout in jiffies to wait for the completion of an MC command
46 */
47#define MC_CMD_COMPLETION_TIMEOUT_JIFFIES (HZ / 2) /* 500 ms */
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) \
J. German Riveraba72f252015-09-25 11:21:01 -050057 ((u16)mc_dec((_hdr), MC_CMD_HDR_CMDID_O, MC_CMD_HDR_CMDID_S))
J. German Rivera31c88962015-03-05 19:29:09 -060058
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
J. German Riveraffcd52e2015-10-17 11:18:14 -050065 * @dpmcp-dev: Pointer to the DPMCP object associated with this MC I/O
66 * object or NULL if none.
J. German Rivera31c88962015-03-05 19:29:09 -060067 * @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 */
72int __must_check fsl_create_mc_io(struct device *dev,
73 phys_addr_t mc_portal_phys_addr,
J. German Riveraba72f252015-09-25 11:21:01 -050074 u32 mc_portal_size,
J. German Riveraffcd52e2015-10-17 11:18:14 -050075 struct fsl_mc_device *dpmcp_dev,
J. German Riveraba72f252015-09-25 11:21:01 -050076 u32 flags, struct fsl_mc_io **new_mc_io)
J. German Rivera31c88962015-03-05 19:29:09 -060077{
J. German Riverad2f84992015-10-17 11:18:15 -050078 int error;
J. German Rivera31c88962015-03-05 19:29:09 -060079 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;
J. German Rivera31c88962015-03-05 19:29:09 -060091 res = devm_request_mem_region(dev,
92 mc_portal_phys_addr,
93 mc_portal_size,
94 "mc_portal");
95 if (!res) {
96 dev_err(dev,
97 "devm_request_mem_region failed for MC portal %#llx\n",
98 mc_portal_phys_addr);
99 return -EBUSY;
100 }
101
102 mc_portal_virt_addr = devm_ioremap_nocache(dev,
103 mc_portal_phys_addr,
104 mc_portal_size);
105 if (!mc_portal_virt_addr) {
106 dev_err(dev,
107 "devm_ioremap_nocache failed for MC portal %#llx\n",
108 mc_portal_phys_addr);
109 return -ENXIO;
110 }
111
112 mc_io->portal_virt_addr = mc_portal_virt_addr;
J. German Riverad2f84992015-10-17 11:18:15 -0500113 if (dpmcp_dev) {
114 error = fsl_mc_io_set_dpmcp(mc_io, dpmcp_dev);
115 if (error < 0)
116 goto error_destroy_mc_io;
117 }
118
J. German Rivera31c88962015-03-05 19:29:09 -0600119 *new_mc_io = mc_io;
120 return 0;
J. German Riverad2f84992015-10-17 11:18:15 -0500121
122error_destroy_mc_io:
123 fsl_destroy_mc_io(mc_io);
124 return error;
J. German Rivera31c88962015-03-05 19:29:09 -0600125}
126EXPORT_SYMBOL_GPL(fsl_create_mc_io);
127
128/**
129 * Destroys an MC I/O object
130 *
131 * @mc_io: MC I/O object to destroy
132 */
133void fsl_destroy_mc_io(struct fsl_mc_io *mc_io)
134{
J. German Riverad2f84992015-10-17 11:18:15 -0500135 struct fsl_mc_device *dpmcp_dev = mc_io->dpmcp_dev;
136
137 if (dpmcp_dev)
138 fsl_mc_io_unset_dpmcp(mc_io);
139
J. German Rivera31c88962015-03-05 19:29:09 -0600140 devm_iounmap(mc_io->dev, mc_io->portal_virt_addr);
141 devm_release_mem_region(mc_io->dev,
142 mc_io->portal_phys_addr,
143 mc_io->portal_size);
144
145 mc_io->portal_virt_addr = NULL;
J. German Rivera31c88962015-03-05 19:29:09 -0600146 devm_kfree(mc_io->dev, mc_io);
147}
148EXPORT_SYMBOL_GPL(fsl_destroy_mc_io);
149
J. German Riverad2f84992015-10-17 11:18:15 -0500150int fsl_mc_io_set_dpmcp(struct fsl_mc_io *mc_io,
151 struct fsl_mc_device *dpmcp_dev)
152{
153 int error;
154
155 if (WARN_ON(!dpmcp_dev))
156 return -EINVAL;
157
158 if (WARN_ON(mc_io->dpmcp_dev))
159 return -EINVAL;
160
161 if (WARN_ON(dpmcp_dev->mc_io))
162 return -EINVAL;
163
164 error = dpmcp_open(mc_io,
165 0,
166 dpmcp_dev->obj_desc.id,
167 &dpmcp_dev->mc_handle);
168 if (error < 0)
169 return error;
170
171 mc_io->dpmcp_dev = dpmcp_dev;
172 dpmcp_dev->mc_io = mc_io;
173 return 0;
174}
175EXPORT_SYMBOL_GPL(fsl_mc_io_set_dpmcp);
176
177void fsl_mc_io_unset_dpmcp(struct fsl_mc_io *mc_io)
178{
179 int error;
180 struct fsl_mc_device *dpmcp_dev = mc_io->dpmcp_dev;
181
182 if (WARN_ON(!dpmcp_dev))
183 return;
184
185 if (WARN_ON(dpmcp_dev->mc_io != mc_io))
186 return;
187
188 error = dpmcp_close(mc_io,
189 0,
190 dpmcp_dev->mc_handle);
191 if (error < 0) {
192 dev_err(&dpmcp_dev->dev, "dpmcp_close() failed: %d\n",
193 error);
194 }
195
196 mc_io->dpmcp_dev = NULL;
197 dpmcp_dev->mc_io = NULL;
198}
199EXPORT_SYMBOL_GPL(fsl_mc_io_unset_dpmcp);
200
J. German Rivera31c88962015-03-05 19:29:09 -0600201static int mc_status_to_error(enum mc_cmd_status status)
202{
203 static const int mc_status_to_error_map[] = {
204 [MC_CMD_STATUS_OK] = 0,
205 [MC_CMD_STATUS_AUTH_ERR] = -EACCES,
206 [MC_CMD_STATUS_NO_PRIVILEGE] = -EPERM,
207 [MC_CMD_STATUS_DMA_ERR] = -EIO,
208 [MC_CMD_STATUS_CONFIG_ERR] = -ENXIO,
209 [MC_CMD_STATUS_TIMEOUT] = -ETIMEDOUT,
210 [MC_CMD_STATUS_NO_RESOURCE] = -ENAVAIL,
211 [MC_CMD_STATUS_NO_MEMORY] = -ENOMEM,
212 [MC_CMD_STATUS_BUSY] = -EBUSY,
213 [MC_CMD_STATUS_UNSUPPORTED_OP] = -ENOTSUPP,
214 [MC_CMD_STATUS_INVALID_STATE] = -ENODEV,
215 };
216
217 if (WARN_ON((u32)status >= ARRAY_SIZE(mc_status_to_error_map)))
218 return -EINVAL;
219
220 return mc_status_to_error_map[status];
221}
222
223static const char *mc_status_to_string(enum mc_cmd_status status)
224{
225 static const char *const status_strings[] = {
226 [MC_CMD_STATUS_OK] = "Command completed successfully",
227 [MC_CMD_STATUS_READY] = "Command ready to be processed",
228 [MC_CMD_STATUS_AUTH_ERR] = "Authentication error",
229 [MC_CMD_STATUS_NO_PRIVILEGE] = "No privilege",
230 [MC_CMD_STATUS_DMA_ERR] = "DMA or I/O error",
231 [MC_CMD_STATUS_CONFIG_ERR] = "Configuration error",
232 [MC_CMD_STATUS_TIMEOUT] = "Operation timed out",
233 [MC_CMD_STATUS_NO_RESOURCE] = "No resources",
234 [MC_CMD_STATUS_NO_MEMORY] = "No memory available",
235 [MC_CMD_STATUS_BUSY] = "Device is busy",
236 [MC_CMD_STATUS_UNSUPPORTED_OP] = "Unsupported operation",
237 [MC_CMD_STATUS_INVALID_STATE] = "Invalid state"
238 };
239
240 if ((unsigned int)status >= ARRAY_SIZE(status_strings))
241 return "Unknown MC error";
242
243 return status_strings[status];
244}
245
246/**
247 * mc_write_command - writes a command to a Management Complex (MC) portal
248 *
249 * @portal: pointer to an MC portal
250 * @cmd: pointer to a filled command
251 */
252static inline void mc_write_command(struct mc_command __iomem *portal,
253 struct mc_command *cmd)
254{
255 int i;
256
257 /* copy command parameters into the portal */
258 for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
259 writeq(cmd->params[i], &portal->params[i]);
260
261 /* submit the command by writing the header */
262 writeq(cmd->header, &portal->header);
263}
264
265/**
266 * mc_read_response - reads the response for the last MC command from a
267 * Management Complex (MC) portal
268 *
269 * @portal: pointer to an MC portal
270 * @resp: pointer to command response buffer
271 *
272 * Returns MC_CMD_STATUS_OK on Success; Error code otherwise.
273 */
274static inline enum mc_cmd_status mc_read_response(struct mc_command __iomem *
275 portal,
276 struct mc_command *resp)
277{
278 int i;
279 enum mc_cmd_status status;
280
281 /* Copy command response header from MC portal: */
282 resp->header = readq(&portal->header);
283 status = MC_CMD_HDR_READ_STATUS(resp->header);
284 if (status != MC_CMD_STATUS_OK)
285 return status;
286
287 /* Copy command response data from MC portal: */
288 for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
289 resp->params[i] = readq(&portal->params[i]);
290
291 return status;
292}
293
294/**
295 * Sends an command to the MC device using the given MC I/O object
296 *
297 * @mc_io: MC I/O object to be used
298 * @cmd: command to be sent
299 *
300 * Returns '0' on Success; Error code otherwise.
301 *
302 * NOTE: This function cannot be invoked from from atomic contexts.
303 */
304int mc_send_command(struct fsl_mc_io *mc_io, struct mc_command *cmd)
305{
306 enum mc_cmd_status status;
307 unsigned long jiffies_until_timeout =
308 jiffies + MC_CMD_COMPLETION_TIMEOUT_JIFFIES;
309
310 /*
311 * Send command to the MC hardware:
312 */
313 mc_write_command(mc_io->portal_virt_addr, cmd);
314
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)) {
331 pr_debug("MC command timed out (portal: %#llx, obj handle: %#x, command: %#x)\n",
332 mc_io->portal_phys_addr,
333 (unsigned int)
334 MC_CMD_HDR_READ_TOKEN(cmd->header),
335 (unsigned int)
336 MC_CMD_HDR_READ_CMDID(cmd->header));
337
338 return -ETIMEDOUT;
339 }
340 }
341
342 if (status != MC_CMD_STATUS_OK) {
343 pr_debug("MC command failed: portal: %#llx, obj handle: %#x, command: %#x, status: %s (%#x)\n",
344 mc_io->portal_phys_addr,
345 (unsigned int)MC_CMD_HDR_READ_TOKEN(cmd->header),
346 (unsigned int)MC_CMD_HDR_READ_CMDID(cmd->header),
347 mc_status_to_string(status),
348 (unsigned int)status);
349
350 return mc_status_to_error(status);
351 }
352
353 return 0;
354}
355EXPORT_SYMBOL(mc_send_command);