blob: 6fe5fa4eddaf9143e0f8567708048fb6a74c36b8 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Yogesh Gaur1a0c4ae2018-05-09 10:52:17 +05302/* Copyright 2013-2016 Freescale Semiconductor, Inc.
Yogesh Gaur318c32f2017-11-15 11:59:31 +05303 * Copyright 2017 NXP
J. German Rivera43e4ae32015-01-06 13:19:02 -08004 */
5#ifndef __FSL_MC_CMD_H
6#define __FSL_MC_CMD_H
7
8#define MC_CMD_NUM_OF_PARAMS 7
9
10#define MAKE_UMASK64(_width) \
11 ((uint64_t)((_width) < 64 ? ((uint64_t)1 << (_width)) - 1 : -1))
12
Prabhakar Kushwaha9564df62015-07-07 15:40:06 +053013static inline uint64_t mc_enc(int lsoffset, int width, uint64_t val)
J. German Rivera43e4ae32015-01-06 13:19:02 -080014{
15 return (uint64_t)(((uint64_t)val & MAKE_UMASK64(width)) << lsoffset);
16}
Prabhakar Kushwaha9564df62015-07-07 15:40:06 +053017static inline uint64_t mc_dec(uint64_t val, int lsoffset, int width)
J. German Rivera43e4ae32015-01-06 13:19:02 -080018{
19 return (uint64_t)((val >> lsoffset) & MAKE_UMASK64(width));
20}
21
Ioana Ciornei2f232fa2023-05-31 19:04:30 +030022struct mc_cmd_header {
23 u8 src_id;
24 u8 flags_hw;
25 u8 status;
26 u8 flags_sw;
27 __le16 token;
28 __le16 cmd_id;
29};
30
J. German Rivera43e4ae32015-01-06 13:19:02 -080031struct mc_command {
32 uint64_t header;
33 uint64_t params[MC_CMD_NUM_OF_PARAMS];
34};
35
Yogesh Gaur318c32f2017-11-15 11:59:31 +053036struct mc_rsp_create {
37 __le32 object_id;
38};
39
40struct mc_rsp_api_ver {
41 __le16 major_ver;
42 __le16 minor_ver;
43};
44
J. German Rivera43e4ae32015-01-06 13:19:02 -080045enum mc_cmd_status {
46 MC_CMD_STATUS_OK = 0x0, /*!< Completed successfully */
47 MC_CMD_STATUS_READY = 0x1, /*!< Ready to be processed */
48 MC_CMD_STATUS_AUTH_ERR = 0x3, /*!< Authentication error */
49 MC_CMD_STATUS_NO_PRIVILEGE = 0x4, /*!< No privilege */
50 MC_CMD_STATUS_DMA_ERR = 0x5, /*!< DMA or I/O error */
51 MC_CMD_STATUS_CONFIG_ERR = 0x6, /*!< Configuration error */
52 MC_CMD_STATUS_TIMEOUT = 0x7, /*!< Operation timed out */
53 MC_CMD_STATUS_NO_RESOURCE = 0x8, /*!< No resources */
54 MC_CMD_STATUS_NO_MEMORY = 0x9, /*!< No memory available */
55 MC_CMD_STATUS_BUSY = 0xA, /*!< Device is busy */
56 MC_CMD_STATUS_UNSUPPORTED_OP = 0xB, /*!< Unsupported operation */
57 MC_CMD_STATUS_INVALID_STATE = 0xC /*!< Invalid state */
58};
59
Prabhakar Kushwaha9564df62015-07-07 15:40:06 +053060/*
61 * MC command flags
62 */
63
64/* High priority flag */
65#define MC_CMD_FLAG_PRI 0x00008000
66/* No flags */
67#define MC_CMD_NO_FLAGS 0x00000000
68/* Command completion flag */
69#define MC_CMD_FLAG_INTR_DIS 0x01000000
70
71
Yogesh Gaur318c32f2017-11-15 11:59:31 +053072#define MC_CMD_HDR_CMDID_O 48 /* Command ID field offset */
73#define MC_CMD_HDR_CMDID_S 16 /* Command ID field size */
J. German Rivera43e4ae32015-01-06 13:19:02 -080074#define MC_CMD_HDR_STATUS_O 16 /* Status field offset */
Yogesh Gaur318c32f2017-11-15 11:59:31 +053075#define MC_CMD_HDR_TOKEN_O 32 /* Token field offset */
76#define MC_CMD_HDR_TOKEN_S 16 /* Token field size */
J. German Rivera43e4ae32015-01-06 13:19:02 -080077#define MC_CMD_HDR_STATUS_S 8 /* Status field size*/
Prabhakar Kushwaha9564df62015-07-07 15:40:06 +053078#define MC_CMD_HDR_FLAGS_O 0 /* Flags field offset */
79#define MC_CMD_HDR_FLAGS_S 32 /* Flags field size*/
Yogesh Gaur318c32f2017-11-15 11:59:31 +053080#define MC_CMD_HDR_FLAGS_MASK 0x0000FFFF /* Command flags mask */
J. German Rivera43e4ae32015-01-06 13:19:02 -080081
82#define MC_CMD_HDR_READ_STATUS(_hdr) \
Prabhakar Kushwaha9564df62015-07-07 15:40:06 +053083 ((enum mc_cmd_status)mc_dec((_hdr), \
J. German Rivera43e4ae32015-01-06 13:19:02 -080084 MC_CMD_HDR_STATUS_O, MC_CMD_HDR_STATUS_S))
85
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -070086#define MC_CMD_HDR_READ_TOKEN(_hdr) \
Prabhakar Kushwaha9564df62015-07-07 15:40:06 +053087 ((uint16_t)mc_dec((_hdr), MC_CMD_HDR_TOKEN_O, MC_CMD_HDR_TOKEN_S))
J. German Rivera43e4ae32015-01-06 13:19:02 -080088
Prabhakar Kushwahac3d06e32015-12-24 15:32:49 +053089#define MC_PREP_OP(_ext, _param, _offset, _width, _type, _arg) \
90 ((_ext)[_param] |= cpu_to_le64(mc_enc((_offset), (_width), _arg)))
91
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -070092#define MC_EXT_OP(_ext, _param, _offset, _width, _type, _arg) \
Prabhakar Kushwahac3d06e32015-12-24 15:32:49 +053093 (_arg = (_type)mc_dec(cpu_to_le64(_ext[_param]), (_offset), (_width)))
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -070094
J. German Rivera43e4ae32015-01-06 13:19:02 -080095#define MC_CMD_OP(_cmd, _param, _offset, _width, _type, _arg) \
Prabhakar Kushwaha9564df62015-07-07 15:40:06 +053096 ((_cmd).params[_param] |= mc_enc((_offset), (_width), _arg))
J. German Rivera43e4ae32015-01-06 13:19:02 -080097
98#define MC_RSP_OP(_cmd, _param, _offset, _width, _type, _arg) \
Prabhakar Kushwaha9564df62015-07-07 15:40:06 +053099 (_arg = (_type)mc_dec(_cmd.params[_param], (_offset), (_width)))
J. German Rivera43e4ae32015-01-06 13:19:02 -0800100
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530101/* cmd, param, offset, width, type, arg_name */
102#define MC_CMD_READ_OBJ_ID(cmd, obj_id) \
103 MC_RSP_OP(cmd, 0, 0, 32, uint32_t, obj_id)
104
105/* cmd, param, offset, width, type, arg_name */
106#define CMD_DESTROY_SET_OBJ_ID_PARAM0(cmd, object_id) \
107 MC_CMD_OP(cmd, 0, 0, 32, uint32_t, object_id)
108
J. German Rivera43e4ae32015-01-06 13:19:02 -0800109static inline uint64_t mc_encode_cmd_header(uint16_t cmd_id,
Prabhakar Kushwaha9564df62015-07-07 15:40:06 +0530110 uint32_t cmd_flags,
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700111 uint16_t token)
J. German Rivera43e4ae32015-01-06 13:19:02 -0800112{
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530113 uint64_t hdr = 0;
J. German Rivera43e4ae32015-01-06 13:19:02 -0800114
Prabhakar Kushwaha9564df62015-07-07 15:40:06 +0530115 hdr = mc_enc(MC_CMD_HDR_CMDID_O, MC_CMD_HDR_CMDID_S, cmd_id);
116 hdr |= mc_enc(MC_CMD_HDR_FLAGS_O, MC_CMD_HDR_FLAGS_S,
117 (cmd_flags & MC_CMD_HDR_FLAGS_MASK));
118 hdr |= mc_enc(MC_CMD_HDR_TOKEN_O, MC_CMD_HDR_TOKEN_S, token);
119 hdr |= mc_enc(MC_CMD_HDR_STATUS_O, MC_CMD_HDR_STATUS_S,
J. German Rivera43e4ae32015-01-06 13:19:02 -0800120 MC_CMD_STATUS_READY);
121
122 return hdr;
123}
124
125/**
126 * mc_write_command - writes a command to a Management Complex (MC) portal
127 *
128 * @portal: pointer to an MC portal
129 * @cmd: pointer to a filled command
130 */
131static inline void mc_write_command(struct mc_command __iomem *portal,
132 struct mc_command *cmd)
133{
134 int i;
135
136 /* copy command parameters into the portal */
137 for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
138 writeq(cmd->params[i], &portal->params[i]);
139
140 /* submit the command by writing the header */
141 writeq(cmd->header, &portal->header);
142}
143
144/**
145 * mc_read_response - reads the response for the last MC command from a
146 * Management Complex (MC) portal
147 *
148 * @portal: pointer to an MC portal
149 * @resp: pointer to command response buffer
150 *
151 * Returns MC_CMD_STATUS_OK on Success; Error code otherwise.
152 */
153static inline enum mc_cmd_status mc_read_response(
154 struct mc_command __iomem *portal,
155 struct mc_command *resp)
156{
157 int i;
158 enum mc_cmd_status status;
159
160 /* Copy command response header from MC portal: */
161 resp->header = readq(&portal->header);
162 status = MC_CMD_HDR_READ_STATUS(resp->header);
163 if (status != MC_CMD_STATUS_OK)
164 return status;
165
166 /* Copy command response data from MC portal: */
167 for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
168 resp->params[i] = readq(&portal->params[i]);
169
170 return status;
171}
172
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530173/**
174 * mc_read_version - read version of the given cmd
175 *
176 * @cmd: pointer to a filled command
177 * @major_version: major version value for the given cmd
178 * @minor_version: minor version value for the given cmd
179 */
180static inline void mc_cmd_read_api_version(struct mc_command *cmd,
181 u16 *major_ver,
182 u16 *minor_ver)
183{
184 struct mc_rsp_api_ver *rsp_params;
185
186 rsp_params = (struct mc_rsp_api_ver *)cmd->params;
187 *major_ver = le16_to_cpu(rsp_params->major_ver);
188 *minor_ver = le16_to_cpu(rsp_params->minor_ver);
189}
190
Ioana Ciornei2f232fa2023-05-31 19:04:30 +0300191static inline uint16_t mc_cmd_hdr_read_token(struct mc_command *cmd)
192{
193 struct mc_cmd_header *hdr = (struct mc_cmd_header *)&cmd->header;
194 u16 token = le16_to_cpu(hdr->token);
195
196 return token;
197}
198
199static inline uint32_t mc_cmd_read_object_id(struct mc_command *cmd)
200{
201 struct mc_rsp_create *rsp_params;
202
203 rsp_params = (struct mc_rsp_create *)cmd->params;
204 return le32_to_cpu(rsp_params->object_id);
205}
J. German Rivera43e4ae32015-01-06 13:19:02 -0800206#endif /* __FSL_MC_CMD_H */