linux/drivers/staging/fsl-mc/bus/mc-sys.c
<<
>>
Prefs
   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 <linux/io-64-nonatomic-hi-lo.h>
  41#include "../include/mc.h"
  42
  43#include "dpmcp.h"
  44
  45/**
  46 * Timeout in milliseconds to wait for the completion of an MC command
  47 */
  48#define MC_CMD_COMPLETION_TIMEOUT_MS    500
  49
  50/*
  51 * usleep_range() min and max values used to throttle down polling
  52 * iterations while waiting for MC command completion
  53 */
  54#define MC_CMD_COMPLETION_POLLING_MIN_SLEEP_USECS    10
  55#define MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS    500
  56
  57static enum mc_cmd_status mc_cmd_hdr_read_status(struct mc_command *cmd)
  58{
  59        struct mc_cmd_header *hdr = (struct mc_cmd_header *)&cmd->header;
  60
  61        return (enum mc_cmd_status)hdr->status;
  62}
  63
  64static u16 mc_cmd_hdr_read_cmdid(struct mc_command *cmd)
  65{
  66        struct mc_cmd_header *hdr = (struct mc_cmd_header *)&cmd->header;
  67        u16 cmd_id = le16_to_cpu(hdr->cmd_id);
  68
  69        return cmd_id;
  70}
  71
  72static int mc_status_to_error(enum mc_cmd_status status)
  73{
  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,
  86        };
  87
  88        if (WARN_ON((u32)status >= ARRAY_SIZE(mc_status_to_error_map)))
  89                return -EINVAL;
  90
  91        return mc_status_to_error_map[status];
  92}
  93
  94static const char *mc_status_to_string(enum mc_cmd_status status)
  95{
  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"
 109        };
 110
 111        if ((unsigned int)status >= ARRAY_SIZE(status_strings))
 112                return "Unknown MC error";
 113
 114        return status_strings[status];
 115}
 116
 117/**
 118 * mc_write_command - writes a command to a Management Complex (MC) portal
 119 *
 120 * @portal: pointer to an MC portal
 121 * @cmd: pointer to a filled command
 122 */
 123static inline void mc_write_command(struct mc_command __iomem *portal,
 124                                    struct mc_command *cmd)
 125{
 126        int i;
 127
 128        /* copy command parameters into the portal */
 129        for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
 130                /*
 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.
 134                 */
 135                writeq_relaxed(le64_to_cpu(cmd->params[i]), &portal->params[i]);
 136
 137        /* submit the command by writing the header */
 138        writeq(le64_to_cpu(cmd->header), &portal->header);
 139}
 140
 141/**
 142 * mc_read_response - reads the response for the last MC command from a
 143 * Management Complex (MC) portal
 144 *
 145 * @portal: pointer to an MC portal
 146 * @resp: pointer to command response buffer
 147 *
 148 * Returns MC_CMD_STATUS_OK on Success; Error code otherwise.
 149 */
 150static inline enum mc_cmd_status mc_read_response(struct mc_command __iomem *
 151                                                  portal,
 152                                                  struct mc_command *resp)
 153{
 154        int i;
 155        enum mc_cmd_status status;
 156
 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)
 161                return status;
 162
 163        /* Copy command response data from MC portal: */
 164        for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
 165                /*
 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.
 169                 */
 170                resp->params[i] =
 171                        cpu_to_le64(readq_relaxed(&portal->params[i]));
 172
 173        return status;
 174}
 175
 176/**
 177 * Waits for the completion of an MC command doing preemptible polling.
 178 * uslepp_range() is called between polling iterations.
 179 *
 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
 183 */
 184static int mc_polling_wait_preemptible(struct fsl_mc_io *mc_io,
 185                                       struct mc_command *cmd,
 186                                       enum mc_cmd_status *mc_status)
 187{
 188        enum mc_cmd_status status;
 189        unsigned long jiffies_until_timeout =
 190                jiffies + msecs_to_jiffies(MC_CMD_COMPLETION_TIMEOUT_MS);
 191
 192        /*
 193         * Wait for response from the MC hardware:
 194         */
 195        for (;;) {
 196                status = mc_read_response(mc_io->portal_virt_addr, cmd);
 197                if (status != MC_CMD_STATUS_READY)
 198                        break;
 199
 200                /*
 201                 * TODO: When MC command completion interrupts are supported
 202                 * call wait function here instead of usleep_range()
 203                 */
 204                usleep_range(MC_CMD_COMPLETION_POLLING_MIN_SLEEP_USECS,
 205                             MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS);
 206
 207                if (time_after_eq(jiffies, jiffies_until_timeout)) {
 208                        dev_dbg(mc_io->dev,
 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));
 213
 214                        return -ETIMEDOUT;
 215                }
 216        }
 217
 218        *mc_status = status;
 219        return 0;
 220}
 221
 222/**
 223 * Waits for the completion of an MC command doing atomic polling.
 224 * udelay() is called between polling iterations.
 225 *
 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
 229 */
 230static int mc_polling_wait_atomic(struct fsl_mc_io *mc_io,
 231                                  struct mc_command *cmd,
 232                                  enum mc_cmd_status *mc_status)
 233{
 234        enum mc_cmd_status status;
 235        unsigned long timeout_usecs = MC_CMD_COMPLETION_TIMEOUT_MS * 1000;
 236
 237        BUILD_BUG_ON((MC_CMD_COMPLETION_TIMEOUT_MS * 1000) %
 238                     MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS != 0);
 239
 240        for (;;) {
 241                status = mc_read_response(mc_io->portal_virt_addr, cmd);
 242                if (status != MC_CMD_STATUS_READY)
 243                        break;
 244
 245                udelay(MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS);
 246                timeout_usecs -= MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS;
 247                if (timeout_usecs == 0) {
 248                        dev_dbg(mc_io->dev,
 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));
 253
 254                        return -ETIMEDOUT;
 255                }
 256        }
 257
 258        *mc_status = status;
 259        return 0;
 260}
 261
 262/**
 263 * Sends a command to the MC device using the given MC I/O object
 264 *
 265 * @mc_io: MC I/O object to be used
 266 * @cmd: command to be sent
 267 *
 268 * Returns '0' on Success; Error code otherwise.
 269 */
 270int mc_send_command(struct fsl_mc_io *mc_io, struct mc_command *cmd)
 271{
 272        int error;
 273        enum mc_cmd_status status;
 274        unsigned long irq_flags = 0;
 275
 276        if (WARN_ON(in_irq() &&
 277                    !(mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL)))
 278                return -EINVAL;
 279
 280        if (mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL)
 281                spin_lock_irqsave(&mc_io->spinlock, irq_flags);
 282        else
 283                mutex_lock(&mc_io->mutex);
 284
 285        /*
 286         * Send command to the MC hardware:
 287         */
 288        mc_write_command(mc_io->portal_virt_addr, cmd);
 289
 290        /*
 291         * Wait for response from the MC hardware:
 292         */
 293        if (!(mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL))
 294                error = mc_polling_wait_preemptible(mc_io, cmd, &status);
 295        else
 296                error = mc_polling_wait_atomic(mc_io, cmd, &status);
 297
 298        if (error < 0)
 299                goto common_exit;
 300
 301        if (status != MC_CMD_STATUS_OK) {
 302                dev_dbg(mc_io->dev,
 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);
 309
 310                error = mc_status_to_error(status);
 311                goto common_exit;
 312        }
 313
 314        error = 0;
 315common_exit:
 316        if (mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL)
 317                spin_unlock_irqrestore(&mc_io->spinlock, irq_flags);
 318        else
 319                mutex_unlock(&mc_io->mutex);
 320
 321        return error;
 322}
 323EXPORT_SYMBOL(mc_send_command);
 324