Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015, Sony Mobile Communications AB. |
| 3 | * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 and |
| 7 | * only version 2 as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/mfd/syscon.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/of_irq.h> |
| 20 | #include <linux/of_platform.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/regmap.h> |
| 23 | #include <linux/sched.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/soc/qcom/smd.h> |
| 26 | #include <linux/soc/qcom/smem.h> |
| 27 | #include <linux/wait.h> |
| 28 | |
| 29 | /* |
| 30 | * The Qualcomm Shared Memory communication solution provides point-to-point |
| 31 | * channels for clients to send and receive streaming or packet based data. |
| 32 | * |
| 33 | * Each channel consists of a control item (channel info) and a ring buffer |
| 34 | * pair. The channel info carry information related to channel state, flow |
| 35 | * control and the offsets within the ring buffer. |
| 36 | * |
| 37 | * All allocated channels are listed in an allocation table, identifying the |
| 38 | * pair of items by name, type and remote processor. |
| 39 | * |
| 40 | * Upon creating a new channel the remote processor allocates channel info and |
| 41 | * ring buffer items from the smem heap and populate the allocation table. An |
| 42 | * interrupt is sent to the other end of the channel and a scan for new |
| 43 | * channels should be done. A channel never goes away, it will only change |
| 44 | * state. |
| 45 | * |
| 46 | * The remote processor signals it intent for bring up the communication |
| 47 | * channel by setting the state of its end of the channel to "opening" and |
| 48 | * sends out an interrupt. We detect this change and register a smd device to |
| 49 | * consume the channel. Upon finding a consumer we finish the handshake and the |
| 50 | * channel is up. |
| 51 | * |
| 52 | * Upon closing a channel, the remote processor will update the state of its |
| 53 | * end of the channel and signal us, we will then unregister any attached |
| 54 | * device and close our end of the channel. |
| 55 | * |
| 56 | * Devices attached to a channel can use the qcom_smd_send function to push |
| 57 | * data to the channel, this is done by copying the data into the tx ring |
| 58 | * buffer, updating the pointers in the channel info and signaling the remote |
| 59 | * processor. |
| 60 | * |
| 61 | * The remote processor does the equivalent when it transfer data and upon |
| 62 | * receiving the interrupt we check the channel info for new data and delivers |
| 63 | * this to the attached device. If the device is not ready to receive the data |
| 64 | * we leave it in the ring buffer for now. |
| 65 | */ |
| 66 | |
| 67 | struct smd_channel_info; |
Stephen Boyd | f02dc82 | 2015-09-02 15:46:46 -0700 | [diff] [blame] | 68 | struct smd_channel_info_pair; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 69 | struct smd_channel_info_word; |
Stephen Boyd | f02dc82 | 2015-09-02 15:46:46 -0700 | [diff] [blame] | 70 | struct smd_channel_info_word_pair; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 71 | |
| 72 | #define SMD_ALLOC_TBL_COUNT 2 |
| 73 | #define SMD_ALLOC_TBL_SIZE 64 |
| 74 | |
| 75 | /* |
| 76 | * This lists the various smem heap items relevant for the allocation table and |
| 77 | * smd channel entries. |
| 78 | */ |
| 79 | static const struct { |
| 80 | unsigned alloc_tbl_id; |
| 81 | unsigned info_base_id; |
| 82 | unsigned fifo_base_id; |
| 83 | } smem_items[SMD_ALLOC_TBL_COUNT] = { |
| 84 | { |
| 85 | .alloc_tbl_id = 13, |
| 86 | .info_base_id = 14, |
| 87 | .fifo_base_id = 338 |
| 88 | }, |
| 89 | { |
Bjorn Andersson | ea4683e | 2015-10-09 22:06:44 -0700 | [diff] [blame] | 90 | .alloc_tbl_id = 266, |
| 91 | .info_base_id = 138, |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 92 | .fifo_base_id = 202, |
| 93 | }, |
| 94 | }; |
| 95 | |
| 96 | /** |
| 97 | * struct qcom_smd_edge - representing a remote processor |
Bjorn Andersson | da05730 | 2016-08-15 11:15:57 -0700 | [diff] [blame] | 98 | * @dev: device for this edge |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 99 | * @of_node: of_node handle for information related to this edge |
| 100 | * @edge_id: identifier of this edge |
Andy Gross | 93dbed9 | 2015-08-26 14:42:45 -0500 | [diff] [blame] | 101 | * @remote_pid: identifier of remote processor |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 102 | * @irq: interrupt for signals on this edge |
| 103 | * @ipc_regmap: regmap handle holding the outgoing ipc register |
| 104 | * @ipc_offset: offset within @ipc_regmap of the register for ipc |
| 105 | * @ipc_bit: bit in the register at @ipc_offset of @ipc_regmap |
| 106 | * @channels: list of all channels detected on this edge |
| 107 | * @channels_lock: guard for modifications of @channels |
| 108 | * @allocated: array of bitmaps representing already allocated channels |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 109 | * @smem_available: last available amount of smem triggering a channel scan |
Bjorn Andersson | 995b170 | 2016-02-17 22:39:03 -0800 | [diff] [blame] | 110 | * @scan_work: work item for discovering new channels |
| 111 | * @state_work: work item for edge state changes |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 112 | */ |
| 113 | struct qcom_smd_edge { |
Bjorn Andersson | da05730 | 2016-08-15 11:15:57 -0700 | [diff] [blame] | 114 | struct device dev; |
| 115 | |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 116 | struct device_node *of_node; |
| 117 | unsigned edge_id; |
Andy Gross | 93dbed9 | 2015-08-26 14:42:45 -0500 | [diff] [blame] | 118 | unsigned remote_pid; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 119 | |
| 120 | int irq; |
| 121 | |
| 122 | struct regmap *ipc_regmap; |
| 123 | int ipc_offset; |
| 124 | int ipc_bit; |
| 125 | |
| 126 | struct list_head channels; |
| 127 | spinlock_t channels_lock; |
| 128 | |
| 129 | DECLARE_BITMAP(allocated[SMD_ALLOC_TBL_COUNT], SMD_ALLOC_TBL_SIZE); |
| 130 | |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 131 | unsigned smem_available; |
| 132 | |
Bjorn Andersson | 028021d | 2016-02-17 22:39:06 -0800 | [diff] [blame] | 133 | wait_queue_head_t new_channel_event; |
| 134 | |
Bjorn Andersson | 995b170 | 2016-02-17 22:39:03 -0800 | [diff] [blame] | 135 | struct work_struct scan_work; |
| 136 | struct work_struct state_work; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 137 | }; |
| 138 | |
Bjorn Andersson | da05730 | 2016-08-15 11:15:57 -0700 | [diff] [blame] | 139 | #define to_smd_edge(d) container_of(d, struct qcom_smd_edge, dev) |
| 140 | |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 141 | /* |
| 142 | * SMD channel states. |
| 143 | */ |
| 144 | enum smd_channel_state { |
| 145 | SMD_CHANNEL_CLOSED, |
| 146 | SMD_CHANNEL_OPENING, |
| 147 | SMD_CHANNEL_OPENED, |
| 148 | SMD_CHANNEL_FLUSHING, |
| 149 | SMD_CHANNEL_CLOSING, |
| 150 | SMD_CHANNEL_RESET, |
| 151 | SMD_CHANNEL_RESET_OPENING |
| 152 | }; |
| 153 | |
| 154 | /** |
| 155 | * struct qcom_smd_channel - smd channel struct |
| 156 | * @edge: qcom_smd_edge this channel is living on |
| 157 | * @qsdev: reference to a associated smd client device |
| 158 | * @name: name of the channel |
| 159 | * @state: local state of the channel |
| 160 | * @remote_state: remote state of the channel |
Stephen Boyd | f02dc82 | 2015-09-02 15:46:46 -0700 | [diff] [blame] | 161 | * @info: byte aligned outgoing/incoming channel info |
| 162 | * @info_word: word aligned outgoing/incoming channel info |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 163 | * @tx_lock: lock to make writes to the channel mutually exclusive |
| 164 | * @fblockread_event: wakeup event tied to tx fBLOCKREADINTR |
| 165 | * @tx_fifo: pointer to the outgoing ring buffer |
| 166 | * @rx_fifo: pointer to the incoming ring buffer |
| 167 | * @fifo_size: size of each ring buffer |
| 168 | * @bounce_buffer: bounce buffer for reading wrapped packets |
| 169 | * @cb: callback function registered for this channel |
| 170 | * @recv_lock: guard for rx info modifications and cb pointer |
| 171 | * @pkt_size: size of the currently handled packet |
| 172 | * @list: lite entry for @channels in qcom_smd_edge |
| 173 | */ |
| 174 | struct qcom_smd_channel { |
| 175 | struct qcom_smd_edge *edge; |
| 176 | |
| 177 | struct qcom_smd_device *qsdev; |
| 178 | |
| 179 | char *name; |
| 180 | enum smd_channel_state state; |
| 181 | enum smd_channel_state remote_state; |
| 182 | |
Stephen Boyd | f02dc82 | 2015-09-02 15:46:46 -0700 | [diff] [blame] | 183 | struct smd_channel_info_pair *info; |
| 184 | struct smd_channel_info_word_pair *info_word; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 185 | |
| 186 | struct mutex tx_lock; |
| 187 | wait_queue_head_t fblockread_event; |
| 188 | |
| 189 | void *tx_fifo; |
| 190 | void *rx_fifo; |
| 191 | int fifo_size; |
| 192 | |
| 193 | void *bounce_buffer; |
Bjorn Andersson | 39f0db2 | 2016-02-17 22:39:02 -0800 | [diff] [blame] | 194 | qcom_smd_cb_t cb; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 195 | |
| 196 | spinlock_t recv_lock; |
| 197 | |
| 198 | int pkt_size; |
| 199 | |
Bjorn Andersson | b853cb9 | 2016-03-28 21:35:22 -0700 | [diff] [blame] | 200 | void *drvdata; |
| 201 | |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 202 | struct list_head list; |
| 203 | }; |
| 204 | |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 205 | /* |
| 206 | * Format of the smd_info smem items, for byte aligned channels. |
| 207 | */ |
| 208 | struct smd_channel_info { |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 209 | __le32 state; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 210 | u8 fDSR; |
| 211 | u8 fCTS; |
| 212 | u8 fCD; |
| 213 | u8 fRI; |
| 214 | u8 fHEAD; |
| 215 | u8 fTAIL; |
| 216 | u8 fSTATE; |
| 217 | u8 fBLOCKREADINTR; |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 218 | __le32 tail; |
| 219 | __le32 head; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 220 | }; |
| 221 | |
Stephen Boyd | f02dc82 | 2015-09-02 15:46:46 -0700 | [diff] [blame] | 222 | struct smd_channel_info_pair { |
| 223 | struct smd_channel_info tx; |
| 224 | struct smd_channel_info rx; |
| 225 | }; |
| 226 | |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 227 | /* |
| 228 | * Format of the smd_info smem items, for word aligned channels. |
| 229 | */ |
| 230 | struct smd_channel_info_word { |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 231 | __le32 state; |
| 232 | __le32 fDSR; |
| 233 | __le32 fCTS; |
| 234 | __le32 fCD; |
| 235 | __le32 fRI; |
| 236 | __le32 fHEAD; |
| 237 | __le32 fTAIL; |
| 238 | __le32 fSTATE; |
| 239 | __le32 fBLOCKREADINTR; |
| 240 | __le32 tail; |
| 241 | __le32 head; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 242 | }; |
| 243 | |
Stephen Boyd | f02dc82 | 2015-09-02 15:46:46 -0700 | [diff] [blame] | 244 | struct smd_channel_info_word_pair { |
| 245 | struct smd_channel_info_word tx; |
| 246 | struct smd_channel_info_word rx; |
| 247 | }; |
| 248 | |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 249 | #define GET_RX_CHANNEL_FLAG(channel, param) \ |
| 250 | ({ \ |
| 251 | BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u8)); \ |
| 252 | channel->info_word ? \ |
| 253 | le32_to_cpu(channel->info_word->rx.param) : \ |
| 254 | channel->info->rx.param; \ |
| 255 | }) |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 256 | |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 257 | #define GET_RX_CHANNEL_INFO(channel, param) \ |
| 258 | ({ \ |
| 259 | BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u32)); \ |
| 260 | le32_to_cpu(channel->info_word ? \ |
| 261 | channel->info_word->rx.param : \ |
| 262 | channel->info->rx.param); \ |
| 263 | }) |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 264 | |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 265 | #define SET_RX_CHANNEL_FLAG(channel, param, value) \ |
| 266 | ({ \ |
| 267 | BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u8)); \ |
| 268 | if (channel->info_word) \ |
| 269 | channel->info_word->rx.param = cpu_to_le32(value); \ |
| 270 | else \ |
| 271 | channel->info->rx.param = value; \ |
| 272 | }) |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 273 | |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 274 | #define SET_RX_CHANNEL_INFO(channel, param, value) \ |
| 275 | ({ \ |
| 276 | BUILD_BUG_ON(sizeof(channel->info->rx.param) != sizeof(u32)); \ |
| 277 | if (channel->info_word) \ |
| 278 | channel->info_word->rx.param = cpu_to_le32(value); \ |
| 279 | else \ |
| 280 | channel->info->rx.param = cpu_to_le32(value); \ |
| 281 | }) |
| 282 | |
| 283 | #define GET_TX_CHANNEL_FLAG(channel, param) \ |
| 284 | ({ \ |
| 285 | BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u8)); \ |
| 286 | channel->info_word ? \ |
| 287 | le32_to_cpu(channel->info_word->tx.param) : \ |
| 288 | channel->info->tx.param; \ |
| 289 | }) |
| 290 | |
| 291 | #define GET_TX_CHANNEL_INFO(channel, param) \ |
| 292 | ({ \ |
| 293 | BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u32)); \ |
| 294 | le32_to_cpu(channel->info_word ? \ |
| 295 | channel->info_word->tx.param : \ |
| 296 | channel->info->tx.param); \ |
| 297 | }) |
| 298 | |
| 299 | #define SET_TX_CHANNEL_FLAG(channel, param, value) \ |
| 300 | ({ \ |
| 301 | BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u8)); \ |
| 302 | if (channel->info_word) \ |
| 303 | channel->info_word->tx.param = cpu_to_le32(value); \ |
| 304 | else \ |
| 305 | channel->info->tx.param = value; \ |
| 306 | }) |
| 307 | |
| 308 | #define SET_TX_CHANNEL_INFO(channel, param, value) \ |
| 309 | ({ \ |
| 310 | BUILD_BUG_ON(sizeof(channel->info->tx.param) != sizeof(u32)); \ |
| 311 | if (channel->info_word) \ |
| 312 | channel->info_word->tx.param = cpu_to_le32(value); \ |
| 313 | else \ |
| 314 | channel->info->tx.param = cpu_to_le32(value); \ |
| 315 | }) |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 316 | |
| 317 | /** |
| 318 | * struct qcom_smd_alloc_entry - channel allocation entry |
| 319 | * @name: channel name |
| 320 | * @cid: channel index |
| 321 | * @flags: channel flags and edge id |
| 322 | * @ref_count: reference count of the channel |
| 323 | */ |
| 324 | struct qcom_smd_alloc_entry { |
| 325 | u8 name[20]; |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 326 | __le32 cid; |
| 327 | __le32 flags; |
| 328 | __le32 ref_count; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 329 | } __packed; |
| 330 | |
| 331 | #define SMD_CHANNEL_FLAGS_EDGE_MASK 0xff |
| 332 | #define SMD_CHANNEL_FLAGS_STREAM BIT(8) |
| 333 | #define SMD_CHANNEL_FLAGS_PACKET BIT(9) |
| 334 | |
| 335 | /* |
| 336 | * Each smd packet contains a 20 byte header, with the first 4 being the length |
| 337 | * of the packet. |
| 338 | */ |
| 339 | #define SMD_PACKET_HEADER_LEN 20 |
| 340 | |
| 341 | /* |
| 342 | * Signal the remote processor associated with 'channel'. |
| 343 | */ |
| 344 | static void qcom_smd_signal_channel(struct qcom_smd_channel *channel) |
| 345 | { |
| 346 | struct qcom_smd_edge *edge = channel->edge; |
| 347 | |
| 348 | regmap_write(edge->ipc_regmap, edge->ipc_offset, BIT(edge->ipc_bit)); |
| 349 | } |
| 350 | |
| 351 | /* |
| 352 | * Initialize the tx channel info |
| 353 | */ |
| 354 | static void qcom_smd_channel_reset(struct qcom_smd_channel *channel) |
| 355 | { |
| 356 | SET_TX_CHANNEL_INFO(channel, state, SMD_CHANNEL_CLOSED); |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 357 | SET_TX_CHANNEL_FLAG(channel, fDSR, 0); |
| 358 | SET_TX_CHANNEL_FLAG(channel, fCTS, 0); |
| 359 | SET_TX_CHANNEL_FLAG(channel, fCD, 0); |
| 360 | SET_TX_CHANNEL_FLAG(channel, fRI, 0); |
| 361 | SET_TX_CHANNEL_FLAG(channel, fHEAD, 0); |
| 362 | SET_TX_CHANNEL_FLAG(channel, fTAIL, 0); |
| 363 | SET_TX_CHANNEL_FLAG(channel, fSTATE, 1); |
| 364 | SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 1); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 365 | SET_TX_CHANNEL_INFO(channel, head, 0); |
Bjorn Andersson | 4e21a95 | 2016-08-26 14:59:17 -0700 | [diff] [blame] | 366 | SET_RX_CHANNEL_INFO(channel, tail, 0); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 367 | |
| 368 | qcom_smd_signal_channel(channel); |
| 369 | |
| 370 | channel->state = SMD_CHANNEL_CLOSED; |
| 371 | channel->pkt_size = 0; |
| 372 | } |
| 373 | |
| 374 | /* |
Bjorn Andersson | 39f0db2 | 2016-02-17 22:39:02 -0800 | [diff] [blame] | 375 | * Set the callback for a channel, with appropriate locking |
| 376 | */ |
| 377 | static void qcom_smd_channel_set_callback(struct qcom_smd_channel *channel, |
| 378 | qcom_smd_cb_t cb) |
| 379 | { |
| 380 | unsigned long flags; |
| 381 | |
| 382 | spin_lock_irqsave(&channel->recv_lock, flags); |
| 383 | channel->cb = cb; |
| 384 | spin_unlock_irqrestore(&channel->recv_lock, flags); |
| 385 | }; |
| 386 | |
| 387 | /* |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 388 | * Calculate the amount of data available in the rx fifo |
| 389 | */ |
| 390 | static size_t qcom_smd_channel_get_rx_avail(struct qcom_smd_channel *channel) |
| 391 | { |
| 392 | unsigned head; |
| 393 | unsigned tail; |
| 394 | |
| 395 | head = GET_RX_CHANNEL_INFO(channel, head); |
| 396 | tail = GET_RX_CHANNEL_INFO(channel, tail); |
| 397 | |
| 398 | return (head - tail) & (channel->fifo_size - 1); |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * Set tx channel state and inform the remote processor |
| 403 | */ |
| 404 | static void qcom_smd_channel_set_state(struct qcom_smd_channel *channel, |
| 405 | int state) |
| 406 | { |
| 407 | struct qcom_smd_edge *edge = channel->edge; |
| 408 | bool is_open = state == SMD_CHANNEL_OPENED; |
| 409 | |
| 410 | if (channel->state == state) |
| 411 | return; |
| 412 | |
Bjorn Andersson | da05730 | 2016-08-15 11:15:57 -0700 | [diff] [blame] | 413 | dev_dbg(&edge->dev, "set_state(%s, %d)\n", channel->name, state); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 414 | |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 415 | SET_TX_CHANNEL_FLAG(channel, fDSR, is_open); |
| 416 | SET_TX_CHANNEL_FLAG(channel, fCTS, is_open); |
| 417 | SET_TX_CHANNEL_FLAG(channel, fCD, is_open); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 418 | |
| 419 | SET_TX_CHANNEL_INFO(channel, state, state); |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 420 | SET_TX_CHANNEL_FLAG(channel, fSTATE, 1); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 421 | |
| 422 | channel->state = state; |
| 423 | qcom_smd_signal_channel(channel); |
| 424 | } |
| 425 | |
| 426 | /* |
| 427 | * Copy count bytes of data using 32bit accesses, if that's required. |
| 428 | */ |
Stephen Boyd | 3b781e5 | 2015-09-02 15:46:47 -0700 | [diff] [blame] | 429 | static void smd_copy_to_fifo(void __iomem *dst, |
| 430 | const void *src, |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 431 | size_t count, |
| 432 | bool word_aligned) |
| 433 | { |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 434 | if (word_aligned) { |
Stephen Boyd | 3b781e5 | 2015-09-02 15:46:47 -0700 | [diff] [blame] | 435 | __iowrite32_copy(dst, src, count / sizeof(u32)); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 436 | } else { |
Stephen Boyd | 3b781e5 | 2015-09-02 15:46:47 -0700 | [diff] [blame] | 437 | memcpy_toio(dst, src, count); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
| 441 | /* |
| 442 | * Copy count bytes of data using 32bit accesses, if that is required. |
| 443 | */ |
Stephen Boyd | c431e67 | 2016-01-20 14:58:38 -0800 | [diff] [blame] | 444 | static void smd_copy_from_fifo(void *dst, |
| 445 | const void __iomem *src, |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 446 | size_t count, |
| 447 | bool word_aligned) |
| 448 | { |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 449 | if (word_aligned) { |
Stephen Boyd | c431e67 | 2016-01-20 14:58:38 -0800 | [diff] [blame] | 450 | __ioread32_copy(dst, src, count / sizeof(u32)); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 451 | } else { |
Stephen Boyd | c431e67 | 2016-01-20 14:58:38 -0800 | [diff] [blame] | 452 | memcpy_fromio(dst, src, count); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
| 456 | /* |
| 457 | * Read count bytes of data from the rx fifo into buf, but don't advance the |
| 458 | * tail. |
| 459 | */ |
| 460 | static size_t qcom_smd_channel_peek(struct qcom_smd_channel *channel, |
| 461 | void *buf, size_t count) |
| 462 | { |
| 463 | bool word_aligned; |
| 464 | unsigned tail; |
| 465 | size_t len; |
| 466 | |
Stephen Boyd | f02dc82 | 2015-09-02 15:46:46 -0700 | [diff] [blame] | 467 | word_aligned = channel->info_word; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 468 | tail = GET_RX_CHANNEL_INFO(channel, tail); |
| 469 | |
| 470 | len = min_t(size_t, count, channel->fifo_size - tail); |
| 471 | if (len) { |
| 472 | smd_copy_from_fifo(buf, |
| 473 | channel->rx_fifo + tail, |
| 474 | len, |
| 475 | word_aligned); |
| 476 | } |
| 477 | |
| 478 | if (len != count) { |
| 479 | smd_copy_from_fifo(buf + len, |
| 480 | channel->rx_fifo, |
| 481 | count - len, |
| 482 | word_aligned); |
| 483 | } |
| 484 | |
| 485 | return count; |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * Advance the rx tail by count bytes. |
| 490 | */ |
| 491 | static void qcom_smd_channel_advance(struct qcom_smd_channel *channel, |
| 492 | size_t count) |
| 493 | { |
| 494 | unsigned tail; |
| 495 | |
| 496 | tail = GET_RX_CHANNEL_INFO(channel, tail); |
| 497 | tail += count; |
| 498 | tail &= (channel->fifo_size - 1); |
| 499 | SET_RX_CHANNEL_INFO(channel, tail, tail); |
| 500 | } |
| 501 | |
| 502 | /* |
| 503 | * Read out a single packet from the rx fifo and deliver it to the device |
| 504 | */ |
| 505 | static int qcom_smd_channel_recv_single(struct qcom_smd_channel *channel) |
| 506 | { |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 507 | unsigned tail; |
| 508 | size_t len; |
| 509 | void *ptr; |
| 510 | int ret; |
| 511 | |
| 512 | if (!channel->cb) |
| 513 | return 0; |
| 514 | |
| 515 | tail = GET_RX_CHANNEL_INFO(channel, tail); |
| 516 | |
| 517 | /* Use bounce buffer if the data wraps */ |
| 518 | if (tail + channel->pkt_size >= channel->fifo_size) { |
| 519 | ptr = channel->bounce_buffer; |
| 520 | len = qcom_smd_channel_peek(channel, ptr, channel->pkt_size); |
| 521 | } else { |
| 522 | ptr = channel->rx_fifo + tail; |
| 523 | len = channel->pkt_size; |
| 524 | } |
| 525 | |
Bjorn Andersson | b853cb9 | 2016-03-28 21:35:22 -0700 | [diff] [blame] | 526 | ret = channel->cb(channel, ptr, len); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 527 | if (ret < 0) |
| 528 | return ret; |
| 529 | |
| 530 | /* Only forward the tail if the client consumed the data */ |
| 531 | qcom_smd_channel_advance(channel, len); |
| 532 | |
| 533 | channel->pkt_size = 0; |
| 534 | |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | /* |
| 539 | * Per channel interrupt handling |
| 540 | */ |
| 541 | static bool qcom_smd_channel_intr(struct qcom_smd_channel *channel) |
| 542 | { |
| 543 | bool need_state_scan = false; |
| 544 | int remote_state; |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 545 | __le32 pktlen; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 546 | int avail; |
| 547 | int ret; |
| 548 | |
| 549 | /* Handle state changes */ |
| 550 | remote_state = GET_RX_CHANNEL_INFO(channel, state); |
| 551 | if (remote_state != channel->remote_state) { |
| 552 | channel->remote_state = remote_state; |
| 553 | need_state_scan = true; |
| 554 | } |
| 555 | /* Indicate that we have seen any state change */ |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 556 | SET_RX_CHANNEL_FLAG(channel, fSTATE, 0); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 557 | |
| 558 | /* Signal waiting qcom_smd_send() about the interrupt */ |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 559 | if (!GET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR)) |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 560 | wake_up_interruptible(&channel->fblockread_event); |
| 561 | |
| 562 | /* Don't consume any data until we've opened the channel */ |
| 563 | if (channel->state != SMD_CHANNEL_OPENED) |
| 564 | goto out; |
| 565 | |
| 566 | /* Indicate that we've seen the new data */ |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 567 | SET_RX_CHANNEL_FLAG(channel, fHEAD, 0); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 568 | |
| 569 | /* Consume data */ |
| 570 | for (;;) { |
| 571 | avail = qcom_smd_channel_get_rx_avail(channel); |
| 572 | |
| 573 | if (!channel->pkt_size && avail >= SMD_PACKET_HEADER_LEN) { |
| 574 | qcom_smd_channel_peek(channel, &pktlen, sizeof(pktlen)); |
| 575 | qcom_smd_channel_advance(channel, SMD_PACKET_HEADER_LEN); |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 576 | channel->pkt_size = le32_to_cpu(pktlen); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 577 | } else if (channel->pkt_size && avail >= channel->pkt_size) { |
| 578 | ret = qcom_smd_channel_recv_single(channel); |
| 579 | if (ret) |
| 580 | break; |
| 581 | } else { |
| 582 | break; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | /* Indicate that we have seen and updated tail */ |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 587 | SET_RX_CHANNEL_FLAG(channel, fTAIL, 1); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 588 | |
| 589 | /* Signal the remote that we've consumed the data (if requested) */ |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 590 | if (!GET_RX_CHANNEL_FLAG(channel, fBLOCKREADINTR)) { |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 591 | /* Ensure ordering of channel info updates */ |
| 592 | wmb(); |
| 593 | |
| 594 | qcom_smd_signal_channel(channel); |
| 595 | } |
| 596 | |
| 597 | out: |
| 598 | return need_state_scan; |
| 599 | } |
| 600 | |
| 601 | /* |
| 602 | * The edge interrupts are triggered by the remote processor on state changes, |
| 603 | * channel info updates or when new channels are created. |
| 604 | */ |
| 605 | static irqreturn_t qcom_smd_edge_intr(int irq, void *data) |
| 606 | { |
| 607 | struct qcom_smd_edge *edge = data; |
| 608 | struct qcom_smd_channel *channel; |
| 609 | unsigned available; |
Bjorn Andersson | 995b170 | 2016-02-17 22:39:03 -0800 | [diff] [blame] | 610 | bool kick_scanner = false; |
| 611 | bool kick_state = false; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 612 | |
| 613 | /* |
| 614 | * Handle state changes or data on each of the channels on this edge |
| 615 | */ |
| 616 | spin_lock(&edge->channels_lock); |
| 617 | list_for_each_entry(channel, &edge->channels, list) { |
| 618 | spin_lock(&channel->recv_lock); |
Bjorn Andersson | 995b170 | 2016-02-17 22:39:03 -0800 | [diff] [blame] | 619 | kick_state |= qcom_smd_channel_intr(channel); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 620 | spin_unlock(&channel->recv_lock); |
| 621 | } |
| 622 | spin_unlock(&edge->channels_lock); |
| 623 | |
| 624 | /* |
| 625 | * Creating a new channel requires allocating an smem entry, so we only |
| 626 | * have to scan if the amount of available space in smem have changed |
| 627 | * since last scan. |
| 628 | */ |
Andy Gross | 93dbed9 | 2015-08-26 14:42:45 -0500 | [diff] [blame] | 629 | available = qcom_smem_get_free_space(edge->remote_pid); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 630 | if (available != edge->smem_available) { |
| 631 | edge->smem_available = available; |
Bjorn Andersson | 995b170 | 2016-02-17 22:39:03 -0800 | [diff] [blame] | 632 | kick_scanner = true; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Bjorn Andersson | 995b170 | 2016-02-17 22:39:03 -0800 | [diff] [blame] | 635 | if (kick_scanner) |
| 636 | schedule_work(&edge->scan_work); |
| 637 | if (kick_state) |
| 638 | schedule_work(&edge->state_work); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 639 | |
| 640 | return IRQ_HANDLED; |
| 641 | } |
| 642 | |
| 643 | /* |
| 644 | * Delivers any outstanding packets in the rx fifo, can be used after probe of |
| 645 | * the clients to deliver any packets that wasn't delivered before the client |
| 646 | * was setup. |
| 647 | */ |
| 648 | static void qcom_smd_channel_resume(struct qcom_smd_channel *channel) |
| 649 | { |
| 650 | unsigned long flags; |
| 651 | |
| 652 | spin_lock_irqsave(&channel->recv_lock, flags); |
| 653 | qcom_smd_channel_intr(channel); |
| 654 | spin_unlock_irqrestore(&channel->recv_lock, flags); |
| 655 | } |
| 656 | |
| 657 | /* |
| 658 | * Calculate how much space is available in the tx fifo. |
| 659 | */ |
| 660 | static size_t qcom_smd_get_tx_avail(struct qcom_smd_channel *channel) |
| 661 | { |
| 662 | unsigned head; |
| 663 | unsigned tail; |
| 664 | unsigned mask = channel->fifo_size - 1; |
| 665 | |
| 666 | head = GET_TX_CHANNEL_INFO(channel, head); |
| 667 | tail = GET_TX_CHANNEL_INFO(channel, tail); |
| 668 | |
| 669 | return mask - ((head - tail) & mask); |
| 670 | } |
| 671 | |
| 672 | /* |
| 673 | * Write count bytes of data into channel, possibly wrapping in the ring buffer |
| 674 | */ |
| 675 | static int qcom_smd_write_fifo(struct qcom_smd_channel *channel, |
| 676 | const void *data, |
| 677 | size_t count) |
| 678 | { |
| 679 | bool word_aligned; |
| 680 | unsigned head; |
| 681 | size_t len; |
| 682 | |
Stephen Boyd | f02dc82 | 2015-09-02 15:46:46 -0700 | [diff] [blame] | 683 | word_aligned = channel->info_word; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 684 | head = GET_TX_CHANNEL_INFO(channel, head); |
| 685 | |
| 686 | len = min_t(size_t, count, channel->fifo_size - head); |
| 687 | if (len) { |
| 688 | smd_copy_to_fifo(channel->tx_fifo + head, |
| 689 | data, |
| 690 | len, |
| 691 | word_aligned); |
| 692 | } |
| 693 | |
| 694 | if (len != count) { |
| 695 | smd_copy_to_fifo(channel->tx_fifo, |
| 696 | data + len, |
| 697 | count - len, |
| 698 | word_aligned); |
| 699 | } |
| 700 | |
| 701 | head += count; |
| 702 | head &= (channel->fifo_size - 1); |
| 703 | SET_TX_CHANNEL_INFO(channel, head, head); |
| 704 | |
| 705 | return count; |
| 706 | } |
| 707 | |
| 708 | /** |
| 709 | * qcom_smd_send - write data to smd channel |
| 710 | * @channel: channel handle |
| 711 | * @data: buffer of data to write |
| 712 | * @len: number of bytes to write |
| 713 | * |
| 714 | * This is a blocking write of len bytes into the channel's tx ring buffer and |
| 715 | * signal the remote end. It will sleep until there is enough space available |
| 716 | * in the tx buffer, utilizing the fBLOCKREADINTR signaling mechanism to avoid |
| 717 | * polling. |
| 718 | */ |
| 719 | int qcom_smd_send(struct qcom_smd_channel *channel, const void *data, int len) |
| 720 | { |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 721 | __le32 hdr[5] = { cpu_to_le32(len), }; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 722 | int tlen = sizeof(hdr) + len; |
| 723 | int ret; |
| 724 | |
| 725 | /* Word aligned channels only accept word size aligned data */ |
Stephen Boyd | f02dc82 | 2015-09-02 15:46:46 -0700 | [diff] [blame] | 726 | if (channel->info_word && len % 4) |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 727 | return -EINVAL; |
| 728 | |
Bjorn Andersson | a208ca9 | 2015-09-24 18:37:18 -0700 | [diff] [blame] | 729 | /* Reject packets that are too big */ |
| 730 | if (tlen >= channel->fifo_size) |
| 731 | return -EINVAL; |
| 732 | |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 733 | ret = mutex_lock_interruptible(&channel->tx_lock); |
| 734 | if (ret) |
| 735 | return ret; |
| 736 | |
| 737 | while (qcom_smd_get_tx_avail(channel) < tlen) { |
| 738 | if (channel->state != SMD_CHANNEL_OPENED) { |
| 739 | ret = -EPIPE; |
| 740 | goto out; |
| 741 | } |
| 742 | |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 743 | SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 0); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 744 | |
| 745 | ret = wait_event_interruptible(channel->fblockread_event, |
| 746 | qcom_smd_get_tx_avail(channel) >= tlen || |
| 747 | channel->state != SMD_CHANNEL_OPENED); |
| 748 | if (ret) |
| 749 | goto out; |
| 750 | |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 751 | SET_TX_CHANNEL_FLAG(channel, fBLOCKREADINTR, 1); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 754 | SET_TX_CHANNEL_FLAG(channel, fTAIL, 0); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 755 | |
| 756 | qcom_smd_write_fifo(channel, hdr, sizeof(hdr)); |
| 757 | qcom_smd_write_fifo(channel, data, len); |
| 758 | |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 759 | SET_TX_CHANNEL_FLAG(channel, fHEAD, 1); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 760 | |
| 761 | /* Ensure ordering of channel info updates */ |
| 762 | wmb(); |
| 763 | |
| 764 | qcom_smd_signal_channel(channel); |
| 765 | |
| 766 | out: |
| 767 | mutex_unlock(&channel->tx_lock); |
| 768 | |
| 769 | return ret; |
| 770 | } |
| 771 | EXPORT_SYMBOL(qcom_smd_send); |
| 772 | |
| 773 | static struct qcom_smd_device *to_smd_device(struct device *dev) |
| 774 | { |
| 775 | return container_of(dev, struct qcom_smd_device, dev); |
| 776 | } |
| 777 | |
| 778 | static struct qcom_smd_driver *to_smd_driver(struct device *dev) |
| 779 | { |
| 780 | struct qcom_smd_device *qsdev = to_smd_device(dev); |
| 781 | |
| 782 | return container_of(qsdev->dev.driver, struct qcom_smd_driver, driver); |
| 783 | } |
| 784 | |
| 785 | static int qcom_smd_dev_match(struct device *dev, struct device_driver *drv) |
| 786 | { |
Bjorn Andersson | 1a7caca | 2015-08-28 10:39:20 -0700 | [diff] [blame] | 787 | struct qcom_smd_device *qsdev = to_smd_device(dev); |
| 788 | struct qcom_smd_driver *qsdrv = container_of(drv, struct qcom_smd_driver, driver); |
| 789 | const struct qcom_smd_id *match = qsdrv->smd_match_table; |
| 790 | const char *name = qsdev->channel->name; |
| 791 | |
| 792 | if (match) { |
| 793 | while (match->name[0]) { |
| 794 | if (!strcmp(match->name, name)) |
| 795 | return 1; |
| 796 | match++; |
| 797 | } |
| 798 | } |
| 799 | |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 800 | return of_driver_match_device(dev, drv); |
| 801 | } |
| 802 | |
| 803 | /* |
Bjorn Andersson | 3fd3f2f | 2016-02-17 22:39:04 -0800 | [diff] [blame] | 804 | * Helper for opening a channel |
| 805 | */ |
| 806 | static int qcom_smd_channel_open(struct qcom_smd_channel *channel, |
| 807 | qcom_smd_cb_t cb) |
| 808 | { |
| 809 | size_t bb_size; |
| 810 | |
| 811 | /* |
| 812 | * Packets are maximum 4k, but reduce if the fifo is smaller |
| 813 | */ |
| 814 | bb_size = min(channel->fifo_size, SZ_4K); |
| 815 | channel->bounce_buffer = kmalloc(bb_size, GFP_KERNEL); |
| 816 | if (!channel->bounce_buffer) |
| 817 | return -ENOMEM; |
| 818 | |
| 819 | qcom_smd_channel_set_callback(channel, cb); |
| 820 | qcom_smd_channel_set_state(channel, SMD_CHANNEL_OPENING); |
| 821 | qcom_smd_channel_set_state(channel, SMD_CHANNEL_OPENED); |
| 822 | |
| 823 | return 0; |
| 824 | } |
| 825 | |
| 826 | /* |
| 827 | * Helper for closing and resetting a channel |
| 828 | */ |
| 829 | static void qcom_smd_channel_close(struct qcom_smd_channel *channel) |
| 830 | { |
| 831 | qcom_smd_channel_set_callback(channel, NULL); |
| 832 | |
| 833 | kfree(channel->bounce_buffer); |
| 834 | channel->bounce_buffer = NULL; |
| 835 | |
| 836 | qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSED); |
| 837 | qcom_smd_channel_reset(channel); |
| 838 | } |
| 839 | |
| 840 | /* |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 841 | * Probe the smd client. |
| 842 | * |
| 843 | * The remote side have indicated that it want the channel to be opened, so |
| 844 | * complete the state handshake and probe our client driver. |
| 845 | */ |
| 846 | static int qcom_smd_dev_probe(struct device *dev) |
| 847 | { |
| 848 | struct qcom_smd_device *qsdev = to_smd_device(dev); |
| 849 | struct qcom_smd_driver *qsdrv = to_smd_driver(dev); |
| 850 | struct qcom_smd_channel *channel = qsdev->channel; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 851 | int ret; |
| 852 | |
Bjorn Andersson | 3fd3f2f | 2016-02-17 22:39:04 -0800 | [diff] [blame] | 853 | ret = qcom_smd_channel_open(channel, qsdrv->callback); |
| 854 | if (ret) |
| 855 | return ret; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 856 | |
| 857 | ret = qsdrv->probe(qsdev); |
| 858 | if (ret) |
| 859 | goto err; |
| 860 | |
| 861 | qcom_smd_channel_resume(channel); |
| 862 | |
| 863 | return 0; |
| 864 | |
| 865 | err: |
| 866 | dev_err(&qsdev->dev, "probe failed\n"); |
| 867 | |
Bjorn Andersson | 3fd3f2f | 2016-02-17 22:39:04 -0800 | [diff] [blame] | 868 | qcom_smd_channel_close(channel); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 869 | return ret; |
| 870 | } |
| 871 | |
| 872 | /* |
| 873 | * Remove the smd client. |
| 874 | * |
| 875 | * The channel is going away, for some reason, so remove the smd client and |
| 876 | * reset the channel state. |
| 877 | */ |
| 878 | static int qcom_smd_dev_remove(struct device *dev) |
| 879 | { |
| 880 | struct qcom_smd_device *qsdev = to_smd_device(dev); |
| 881 | struct qcom_smd_driver *qsdrv = to_smd_driver(dev); |
| 882 | struct qcom_smd_channel *channel = qsdev->channel; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 883 | |
| 884 | qcom_smd_channel_set_state(channel, SMD_CHANNEL_CLOSING); |
| 885 | |
| 886 | /* |
| 887 | * Make sure we don't race with the code receiving data. |
| 888 | */ |
Bjorn Andersson | 39f0db2 | 2016-02-17 22:39:02 -0800 | [diff] [blame] | 889 | qcom_smd_channel_set_callback(channel, NULL); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 890 | |
| 891 | /* Wake up any sleepers in qcom_smd_send() */ |
| 892 | wake_up_interruptible(&channel->fblockread_event); |
| 893 | |
| 894 | /* |
| 895 | * We expect that the client might block in remove() waiting for any |
| 896 | * outstanding calls to qcom_smd_send() to wake up and finish. |
| 897 | */ |
| 898 | if (qsdrv->remove) |
| 899 | qsdrv->remove(qsdev); |
| 900 | |
Bjorn Andersson | 0a0c08c | 2016-08-09 17:39:19 -0700 | [diff] [blame] | 901 | /* The client is now gone, close the primary channel */ |
| 902 | qcom_smd_channel_close(channel); |
| 903 | channel->qsdev = NULL; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 904 | |
| 905 | return 0; |
| 906 | } |
| 907 | |
| 908 | static struct bus_type qcom_smd_bus = { |
| 909 | .name = "qcom_smd", |
| 910 | .match = qcom_smd_dev_match, |
| 911 | .probe = qcom_smd_dev_probe, |
| 912 | .remove = qcom_smd_dev_remove, |
| 913 | }; |
| 914 | |
| 915 | /* |
| 916 | * Release function for the qcom_smd_device object. |
| 917 | */ |
| 918 | static void qcom_smd_release_device(struct device *dev) |
| 919 | { |
| 920 | struct qcom_smd_device *qsdev = to_smd_device(dev); |
| 921 | |
| 922 | kfree(qsdev); |
| 923 | } |
| 924 | |
| 925 | /* |
| 926 | * Finds the device_node for the smd child interested in this channel. |
| 927 | */ |
| 928 | static struct device_node *qcom_smd_match_channel(struct device_node *edge_node, |
| 929 | const char *channel) |
| 930 | { |
| 931 | struct device_node *child; |
| 932 | const char *name; |
| 933 | const char *key; |
| 934 | int ret; |
| 935 | |
| 936 | for_each_available_child_of_node(edge_node, child) { |
| 937 | key = "qcom,smd-channels"; |
| 938 | ret = of_property_read_string(child, key, &name); |
Julia Lawall | 6083096 | 2015-10-12 22:43:15 +0200 | [diff] [blame] | 939 | if (ret) |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 940 | continue; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 941 | |
| 942 | if (strcmp(name, channel) == 0) |
| 943 | return child; |
| 944 | } |
| 945 | |
| 946 | return NULL; |
| 947 | } |
| 948 | |
| 949 | /* |
| 950 | * Create a smd client device for channel that is being opened. |
| 951 | */ |
| 952 | static int qcom_smd_create_device(struct qcom_smd_channel *channel) |
| 953 | { |
| 954 | struct qcom_smd_device *qsdev; |
| 955 | struct qcom_smd_edge *edge = channel->edge; |
| 956 | struct device_node *node; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 957 | int ret; |
| 958 | |
| 959 | if (channel->qsdev) |
| 960 | return -EEXIST; |
| 961 | |
Bjorn Andersson | da05730 | 2016-08-15 11:15:57 -0700 | [diff] [blame] | 962 | dev_dbg(&edge->dev, "registering '%s'\n", channel->name); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 963 | |
| 964 | qsdev = kzalloc(sizeof(*qsdev), GFP_KERNEL); |
| 965 | if (!qsdev) |
| 966 | return -ENOMEM; |
| 967 | |
Bjorn Andersson | 1a7caca | 2015-08-28 10:39:20 -0700 | [diff] [blame] | 968 | node = qcom_smd_match_channel(edge->of_node, channel->name); |
| 969 | dev_set_name(&qsdev->dev, "%s.%s", |
| 970 | edge->of_node->name, |
| 971 | node ? node->name : channel->name); |
| 972 | |
Bjorn Andersson | da05730 | 2016-08-15 11:15:57 -0700 | [diff] [blame] | 973 | qsdev->dev.parent = &edge->dev; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 974 | qsdev->dev.bus = &qcom_smd_bus; |
| 975 | qsdev->dev.release = qcom_smd_release_device; |
| 976 | qsdev->dev.of_node = node; |
| 977 | |
| 978 | qsdev->channel = channel; |
| 979 | |
| 980 | channel->qsdev = qsdev; |
| 981 | |
| 982 | ret = device_register(&qsdev->dev); |
| 983 | if (ret) { |
Bjorn Andersson | da05730 | 2016-08-15 11:15:57 -0700 | [diff] [blame] | 984 | dev_err(&edge->dev, "device_register failed: %d\n", ret); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 985 | put_device(&qsdev->dev); |
| 986 | } |
| 987 | |
| 988 | return ret; |
| 989 | } |
| 990 | |
| 991 | /* |
| 992 | * Destroy a smd client device for a channel that's going away. |
| 993 | */ |
| 994 | static void qcom_smd_destroy_device(struct qcom_smd_channel *channel) |
| 995 | { |
| 996 | struct device *dev; |
| 997 | |
| 998 | BUG_ON(!channel->qsdev); |
| 999 | |
| 1000 | dev = &channel->qsdev->dev; |
| 1001 | |
| 1002 | device_unregister(dev); |
| 1003 | of_node_put(dev->of_node); |
| 1004 | put_device(dev); |
| 1005 | } |
| 1006 | |
| 1007 | /** |
| 1008 | * qcom_smd_driver_register - register a smd driver |
| 1009 | * @qsdrv: qcom_smd_driver struct |
| 1010 | */ |
| 1011 | int qcom_smd_driver_register(struct qcom_smd_driver *qsdrv) |
| 1012 | { |
| 1013 | qsdrv->driver.bus = &qcom_smd_bus; |
| 1014 | return driver_register(&qsdrv->driver); |
| 1015 | } |
| 1016 | EXPORT_SYMBOL(qcom_smd_driver_register); |
| 1017 | |
Bjorn Andersson | b853cb9 | 2016-03-28 21:35:22 -0700 | [diff] [blame] | 1018 | void *qcom_smd_get_drvdata(struct qcom_smd_channel *channel) |
| 1019 | { |
| 1020 | return channel->drvdata; |
| 1021 | } |
| 1022 | EXPORT_SYMBOL(qcom_smd_get_drvdata); |
| 1023 | |
| 1024 | void qcom_smd_set_drvdata(struct qcom_smd_channel *channel, void *data) |
| 1025 | { |
| 1026 | channel->drvdata = data; |
| 1027 | } |
| 1028 | EXPORT_SYMBOL(qcom_smd_set_drvdata); |
| 1029 | |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1030 | /** |
| 1031 | * qcom_smd_driver_unregister - unregister a smd driver |
| 1032 | * @qsdrv: qcom_smd_driver struct |
| 1033 | */ |
| 1034 | void qcom_smd_driver_unregister(struct qcom_smd_driver *qsdrv) |
| 1035 | { |
| 1036 | driver_unregister(&qsdrv->driver); |
| 1037 | } |
| 1038 | EXPORT_SYMBOL(qcom_smd_driver_unregister); |
| 1039 | |
Bjorn Andersson | 028021d | 2016-02-17 22:39:06 -0800 | [diff] [blame] | 1040 | static struct qcom_smd_channel * |
| 1041 | qcom_smd_find_channel(struct qcom_smd_edge *edge, const char *name) |
| 1042 | { |
| 1043 | struct qcom_smd_channel *channel; |
| 1044 | struct qcom_smd_channel *ret = NULL; |
| 1045 | unsigned long flags; |
| 1046 | unsigned state; |
| 1047 | |
| 1048 | spin_lock_irqsave(&edge->channels_lock, flags); |
| 1049 | list_for_each_entry(channel, &edge->channels, list) { |
| 1050 | if (strcmp(channel->name, name)) |
| 1051 | continue; |
| 1052 | |
| 1053 | state = GET_RX_CHANNEL_INFO(channel, state); |
| 1054 | if (state != SMD_CHANNEL_OPENING && |
| 1055 | state != SMD_CHANNEL_OPENED) |
| 1056 | continue; |
| 1057 | |
| 1058 | ret = channel; |
| 1059 | break; |
| 1060 | } |
| 1061 | spin_unlock_irqrestore(&edge->channels_lock, flags); |
| 1062 | |
| 1063 | return ret; |
| 1064 | } |
| 1065 | |
| 1066 | /** |
| 1067 | * qcom_smd_open_channel() - claim additional channels on the same edge |
| 1068 | * @sdev: smd_device handle |
| 1069 | * @name: channel name |
| 1070 | * @cb: callback method to use for incoming data |
| 1071 | * |
| 1072 | * Returns a channel handle on success, or -EPROBE_DEFER if the channel isn't |
| 1073 | * ready. |
Bjorn Andersson | 0a0c08c | 2016-08-09 17:39:19 -0700 | [diff] [blame] | 1074 | * |
| 1075 | * Any channels returned must be closed with a call to qcom_smd_close_channel() |
Bjorn Andersson | 028021d | 2016-02-17 22:39:06 -0800 | [diff] [blame] | 1076 | */ |
Bjorn Andersson | b853cb9 | 2016-03-28 21:35:22 -0700 | [diff] [blame] | 1077 | struct qcom_smd_channel *qcom_smd_open_channel(struct qcom_smd_channel *parent, |
Bjorn Andersson | 028021d | 2016-02-17 22:39:06 -0800 | [diff] [blame] | 1078 | const char *name, |
| 1079 | qcom_smd_cb_t cb) |
| 1080 | { |
| 1081 | struct qcom_smd_channel *channel; |
Bjorn Andersson | b853cb9 | 2016-03-28 21:35:22 -0700 | [diff] [blame] | 1082 | struct qcom_smd_device *sdev = parent->qsdev; |
| 1083 | struct qcom_smd_edge *edge = parent->edge; |
Bjorn Andersson | 028021d | 2016-02-17 22:39:06 -0800 | [diff] [blame] | 1084 | int ret; |
| 1085 | |
| 1086 | /* Wait up to HZ for the channel to appear */ |
| 1087 | ret = wait_event_interruptible_timeout(edge->new_channel_event, |
| 1088 | (channel = qcom_smd_find_channel(edge, name)) != NULL, |
| 1089 | HZ); |
| 1090 | if (!ret) |
| 1091 | return ERR_PTR(-ETIMEDOUT); |
| 1092 | |
| 1093 | if (channel->state != SMD_CHANNEL_CLOSED) { |
| 1094 | dev_err(&sdev->dev, "channel %s is busy\n", channel->name); |
| 1095 | return ERR_PTR(-EBUSY); |
| 1096 | } |
| 1097 | |
| 1098 | channel->qsdev = sdev; |
| 1099 | ret = qcom_smd_channel_open(channel, cb); |
| 1100 | if (ret) { |
| 1101 | channel->qsdev = NULL; |
| 1102 | return ERR_PTR(ret); |
| 1103 | } |
| 1104 | |
Bjorn Andersson | 028021d | 2016-02-17 22:39:06 -0800 | [diff] [blame] | 1105 | return channel; |
| 1106 | } |
| 1107 | EXPORT_SYMBOL(qcom_smd_open_channel); |
| 1108 | |
Bjorn Andersson | 0a0c08c | 2016-08-09 17:39:19 -0700 | [diff] [blame] | 1109 | /** |
| 1110 | * qcom_smd_close_channel() - close an additionally opened channel |
| 1111 | * @channel: channel handle, returned by qcom_smd_open_channel() |
| 1112 | */ |
| 1113 | void qcom_smd_close_channel(struct qcom_smd_channel *channel) |
| 1114 | { |
| 1115 | qcom_smd_channel_close(channel); |
| 1116 | channel->qsdev = NULL; |
| 1117 | } |
| 1118 | EXPORT_SYMBOL(qcom_smd_close_channel); |
| 1119 | |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1120 | /* |
| 1121 | * Allocate the qcom_smd_channel object for a newly found smd channel, |
| 1122 | * retrieving and validating the smem items involved. |
| 1123 | */ |
| 1124 | static struct qcom_smd_channel *qcom_smd_create_channel(struct qcom_smd_edge *edge, |
| 1125 | unsigned smem_info_item, |
| 1126 | unsigned smem_fifo_item, |
| 1127 | char *name) |
| 1128 | { |
| 1129 | struct qcom_smd_channel *channel; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1130 | size_t fifo_size; |
| 1131 | size_t info_size; |
| 1132 | void *fifo_base; |
| 1133 | void *info; |
| 1134 | int ret; |
| 1135 | |
Bjorn Andersson | da05730 | 2016-08-15 11:15:57 -0700 | [diff] [blame] | 1136 | channel = devm_kzalloc(&edge->dev, sizeof(*channel), GFP_KERNEL); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1137 | if (!channel) |
| 1138 | return ERR_PTR(-ENOMEM); |
| 1139 | |
| 1140 | channel->edge = edge; |
Bjorn Andersson | da05730 | 2016-08-15 11:15:57 -0700 | [diff] [blame] | 1141 | channel->name = devm_kstrdup(&edge->dev, name, GFP_KERNEL); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1142 | if (!channel->name) |
| 1143 | return ERR_PTR(-ENOMEM); |
| 1144 | |
| 1145 | mutex_init(&channel->tx_lock); |
| 1146 | spin_lock_init(&channel->recv_lock); |
| 1147 | init_waitqueue_head(&channel->fblockread_event); |
| 1148 | |
Stephen Boyd | 1a03964 | 2015-09-02 15:46:44 -0700 | [diff] [blame] | 1149 | info = qcom_smem_get(edge->remote_pid, smem_info_item, &info_size); |
| 1150 | if (IS_ERR(info)) { |
| 1151 | ret = PTR_ERR(info); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1152 | goto free_name_and_channel; |
Stephen Boyd | 1a03964 | 2015-09-02 15:46:44 -0700 | [diff] [blame] | 1153 | } |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1154 | |
| 1155 | /* |
| 1156 | * Use the size of the item to figure out which channel info struct to |
| 1157 | * use. |
| 1158 | */ |
| 1159 | if (info_size == 2 * sizeof(struct smd_channel_info_word)) { |
Stephen Boyd | f02dc82 | 2015-09-02 15:46:46 -0700 | [diff] [blame] | 1160 | channel->info_word = info; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1161 | } else if (info_size == 2 * sizeof(struct smd_channel_info)) { |
Stephen Boyd | f02dc82 | 2015-09-02 15:46:46 -0700 | [diff] [blame] | 1162 | channel->info = info; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1163 | } else { |
Bjorn Andersson | da05730 | 2016-08-15 11:15:57 -0700 | [diff] [blame] | 1164 | dev_err(&edge->dev, |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1165 | "channel info of size %zu not supported\n", info_size); |
| 1166 | ret = -EINVAL; |
| 1167 | goto free_name_and_channel; |
| 1168 | } |
| 1169 | |
Stephen Boyd | 1a03964 | 2015-09-02 15:46:44 -0700 | [diff] [blame] | 1170 | fifo_base = qcom_smem_get(edge->remote_pid, smem_fifo_item, &fifo_size); |
| 1171 | if (IS_ERR(fifo_base)) { |
| 1172 | ret = PTR_ERR(fifo_base); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1173 | goto free_name_and_channel; |
Stephen Boyd | 1a03964 | 2015-09-02 15:46:44 -0700 | [diff] [blame] | 1174 | } |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1175 | |
| 1176 | /* The channel consist of a rx and tx fifo of equal size */ |
| 1177 | fifo_size /= 2; |
| 1178 | |
Bjorn Andersson | da05730 | 2016-08-15 11:15:57 -0700 | [diff] [blame] | 1179 | dev_dbg(&edge->dev, "new channel '%s' info-size: %zu fifo-size: %zu\n", |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1180 | name, info_size, fifo_size); |
| 1181 | |
| 1182 | channel->tx_fifo = fifo_base; |
| 1183 | channel->rx_fifo = fifo_base + fifo_size; |
| 1184 | channel->fifo_size = fifo_size; |
| 1185 | |
| 1186 | qcom_smd_channel_reset(channel); |
| 1187 | |
| 1188 | return channel; |
| 1189 | |
| 1190 | free_name_and_channel: |
Bjorn Andersson | da05730 | 2016-08-15 11:15:57 -0700 | [diff] [blame] | 1191 | devm_kfree(&edge->dev, channel->name); |
| 1192 | devm_kfree(&edge->dev, channel); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1193 | |
| 1194 | return ERR_PTR(ret); |
| 1195 | } |
| 1196 | |
| 1197 | /* |
| 1198 | * Scans the allocation table for any newly allocated channels, calls |
| 1199 | * qcom_smd_create_channel() to create representations of these and add |
| 1200 | * them to the edge's list of channels. |
| 1201 | */ |
Bjorn Andersson | 995b170 | 2016-02-17 22:39:03 -0800 | [diff] [blame] | 1202 | static void qcom_channel_scan_worker(struct work_struct *work) |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1203 | { |
Bjorn Andersson | 995b170 | 2016-02-17 22:39:03 -0800 | [diff] [blame] | 1204 | struct qcom_smd_edge *edge = container_of(work, struct qcom_smd_edge, scan_work); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1205 | struct qcom_smd_alloc_entry *alloc_tbl; |
| 1206 | struct qcom_smd_alloc_entry *entry; |
| 1207 | struct qcom_smd_channel *channel; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1208 | unsigned long flags; |
| 1209 | unsigned fifo_id; |
| 1210 | unsigned info_id; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1211 | int tbl; |
| 1212 | int i; |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 1213 | u32 eflags, cid; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1214 | |
| 1215 | for (tbl = 0; tbl < SMD_ALLOC_TBL_COUNT; tbl++) { |
Stephen Boyd | 1a03964 | 2015-09-02 15:46:44 -0700 | [diff] [blame] | 1216 | alloc_tbl = qcom_smem_get(edge->remote_pid, |
| 1217 | smem_items[tbl].alloc_tbl_id, NULL); |
| 1218 | if (IS_ERR(alloc_tbl)) |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1219 | continue; |
| 1220 | |
| 1221 | for (i = 0; i < SMD_ALLOC_TBL_SIZE; i++) { |
| 1222 | entry = &alloc_tbl[i]; |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 1223 | eflags = le32_to_cpu(entry->flags); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1224 | if (test_bit(i, edge->allocated[tbl])) |
| 1225 | continue; |
| 1226 | |
| 1227 | if (entry->ref_count == 0) |
| 1228 | continue; |
| 1229 | |
| 1230 | if (!entry->name[0]) |
| 1231 | continue; |
| 1232 | |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 1233 | if (!(eflags & SMD_CHANNEL_FLAGS_PACKET)) |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1234 | continue; |
| 1235 | |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 1236 | if ((eflags & SMD_CHANNEL_FLAGS_EDGE_MASK) != edge->edge_id) |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1237 | continue; |
| 1238 | |
Stephen Boyd | 24f60e3 | 2015-09-17 14:50:53 -0700 | [diff] [blame] | 1239 | cid = le32_to_cpu(entry->cid); |
| 1240 | info_id = smem_items[tbl].info_base_id + cid; |
| 1241 | fifo_id = smem_items[tbl].fifo_base_id + cid; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1242 | |
| 1243 | channel = qcom_smd_create_channel(edge, info_id, fifo_id, entry->name); |
| 1244 | if (IS_ERR(channel)) |
| 1245 | continue; |
| 1246 | |
| 1247 | spin_lock_irqsave(&edge->channels_lock, flags); |
| 1248 | list_add(&channel->list, &edge->channels); |
| 1249 | spin_unlock_irqrestore(&edge->channels_lock, flags); |
| 1250 | |
Bjorn Andersson | da05730 | 2016-08-15 11:15:57 -0700 | [diff] [blame] | 1251 | dev_dbg(&edge->dev, "new channel found: '%s'\n", channel->name); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1252 | set_bit(i, edge->allocated[tbl]); |
Bjorn Andersson | 028021d | 2016-02-17 22:39:06 -0800 | [diff] [blame] | 1253 | |
| 1254 | wake_up_interruptible(&edge->new_channel_event); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1255 | } |
| 1256 | } |
| 1257 | |
Bjorn Andersson | 995b170 | 2016-02-17 22:39:03 -0800 | [diff] [blame] | 1258 | schedule_work(&edge->state_work); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | /* |
| 1262 | * This per edge worker scans smem for any new channels and register these. It |
| 1263 | * then scans all registered channels for state changes that should be handled |
| 1264 | * by creating or destroying smd client devices for the registered channels. |
| 1265 | * |
Bjorn Andersson | 995b170 | 2016-02-17 22:39:03 -0800 | [diff] [blame] | 1266 | * LOCKING: edge->channels_lock only needs to cover the list operations, as the |
| 1267 | * worker is killed before any channels are deallocated |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1268 | */ |
| 1269 | static void qcom_channel_state_worker(struct work_struct *work) |
| 1270 | { |
| 1271 | struct qcom_smd_channel *channel; |
| 1272 | struct qcom_smd_edge *edge = container_of(work, |
| 1273 | struct qcom_smd_edge, |
Bjorn Andersson | 995b170 | 2016-02-17 22:39:03 -0800 | [diff] [blame] | 1274 | state_work); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1275 | unsigned remote_state; |
Bjorn Andersson | 995b170 | 2016-02-17 22:39:03 -0800 | [diff] [blame] | 1276 | unsigned long flags; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1277 | |
| 1278 | /* |
| 1279 | * Register a device for any closed channel where the remote processor |
| 1280 | * is showing interest in opening the channel. |
| 1281 | */ |
Bjorn Andersson | 995b170 | 2016-02-17 22:39:03 -0800 | [diff] [blame] | 1282 | spin_lock_irqsave(&edge->channels_lock, flags); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1283 | list_for_each_entry(channel, &edge->channels, list) { |
| 1284 | if (channel->state != SMD_CHANNEL_CLOSED) |
| 1285 | continue; |
| 1286 | |
| 1287 | remote_state = GET_RX_CHANNEL_INFO(channel, state); |
| 1288 | if (remote_state != SMD_CHANNEL_OPENING && |
| 1289 | remote_state != SMD_CHANNEL_OPENED) |
| 1290 | continue; |
| 1291 | |
Bjorn Andersson | 995b170 | 2016-02-17 22:39:03 -0800 | [diff] [blame] | 1292 | spin_unlock_irqrestore(&edge->channels_lock, flags); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1293 | qcom_smd_create_device(channel); |
Bjorn Andersson | 995b170 | 2016-02-17 22:39:03 -0800 | [diff] [blame] | 1294 | spin_lock_irqsave(&edge->channels_lock, flags); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | /* |
| 1298 | * Unregister the device for any channel that is opened where the |
| 1299 | * remote processor is closing the channel. |
| 1300 | */ |
| 1301 | list_for_each_entry(channel, &edge->channels, list) { |
| 1302 | if (channel->state != SMD_CHANNEL_OPENING && |
| 1303 | channel->state != SMD_CHANNEL_OPENED) |
| 1304 | continue; |
| 1305 | |
| 1306 | remote_state = GET_RX_CHANNEL_INFO(channel, state); |
| 1307 | if (remote_state == SMD_CHANNEL_OPENING || |
| 1308 | remote_state == SMD_CHANNEL_OPENED) |
| 1309 | continue; |
| 1310 | |
Bjorn Andersson | 995b170 | 2016-02-17 22:39:03 -0800 | [diff] [blame] | 1311 | spin_unlock_irqrestore(&edge->channels_lock, flags); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1312 | qcom_smd_destroy_device(channel); |
Bjorn Andersson | 995b170 | 2016-02-17 22:39:03 -0800 | [diff] [blame] | 1313 | spin_lock_irqsave(&edge->channels_lock, flags); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1314 | } |
Bjorn Andersson | 995b170 | 2016-02-17 22:39:03 -0800 | [diff] [blame] | 1315 | spin_unlock_irqrestore(&edge->channels_lock, flags); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | /* |
| 1319 | * Parses an of_node describing an edge. |
| 1320 | */ |
| 1321 | static int qcom_smd_parse_edge(struct device *dev, |
| 1322 | struct device_node *node, |
| 1323 | struct qcom_smd_edge *edge) |
| 1324 | { |
| 1325 | struct device_node *syscon_np; |
| 1326 | const char *key; |
| 1327 | int irq; |
| 1328 | int ret; |
| 1329 | |
| 1330 | INIT_LIST_HEAD(&edge->channels); |
| 1331 | spin_lock_init(&edge->channels_lock); |
| 1332 | |
Bjorn Andersson | 995b170 | 2016-02-17 22:39:03 -0800 | [diff] [blame] | 1333 | INIT_WORK(&edge->scan_work, qcom_channel_scan_worker); |
| 1334 | INIT_WORK(&edge->state_work, qcom_channel_state_worker); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1335 | |
| 1336 | edge->of_node = of_node_get(node); |
| 1337 | |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1338 | key = "qcom,smd-edge"; |
| 1339 | ret = of_property_read_u32(node, key, &edge->edge_id); |
| 1340 | if (ret) { |
| 1341 | dev_err(dev, "edge missing %s property\n", key); |
| 1342 | return -EINVAL; |
| 1343 | } |
| 1344 | |
Andy Gross | 93dbed9 | 2015-08-26 14:42:45 -0500 | [diff] [blame] | 1345 | edge->remote_pid = QCOM_SMEM_HOST_ANY; |
| 1346 | key = "qcom,remote-pid"; |
| 1347 | of_property_read_u32(node, key, &edge->remote_pid); |
| 1348 | |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1349 | syscon_np = of_parse_phandle(node, "qcom,ipc", 0); |
| 1350 | if (!syscon_np) { |
| 1351 | dev_err(dev, "no qcom,ipc node\n"); |
| 1352 | return -ENODEV; |
| 1353 | } |
| 1354 | |
| 1355 | edge->ipc_regmap = syscon_node_to_regmap(syscon_np); |
| 1356 | if (IS_ERR(edge->ipc_regmap)) |
| 1357 | return PTR_ERR(edge->ipc_regmap); |
| 1358 | |
| 1359 | key = "qcom,ipc"; |
| 1360 | ret = of_property_read_u32_index(node, key, 1, &edge->ipc_offset); |
| 1361 | if (ret < 0) { |
| 1362 | dev_err(dev, "no offset in %s\n", key); |
| 1363 | return -EINVAL; |
| 1364 | } |
| 1365 | |
| 1366 | ret = of_property_read_u32_index(node, key, 2, &edge->ipc_bit); |
| 1367 | if (ret < 0) { |
| 1368 | dev_err(dev, "no bit in %s\n", key); |
| 1369 | return -EINVAL; |
| 1370 | } |
| 1371 | |
Bjorn Andersson | 381a0b4 | 2016-08-09 17:42:55 -0700 | [diff] [blame] | 1372 | irq = irq_of_parse_and_map(node, 0); |
| 1373 | if (irq < 0) { |
| 1374 | dev_err(dev, "required smd interrupt missing\n"); |
| 1375 | return -EINVAL; |
| 1376 | } |
| 1377 | |
| 1378 | ret = devm_request_irq(dev, irq, |
| 1379 | qcom_smd_edge_intr, IRQF_TRIGGER_RISING, |
| 1380 | node->name, edge); |
| 1381 | if (ret) { |
| 1382 | dev_err(dev, "failed to request smd irq\n"); |
| 1383 | return ret; |
| 1384 | } |
| 1385 | |
| 1386 | edge->irq = irq; |
| 1387 | |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1388 | return 0; |
| 1389 | } |
| 1390 | |
Bjorn Andersson | da05730 | 2016-08-15 11:15:57 -0700 | [diff] [blame] | 1391 | /* |
| 1392 | * Release function for an edge. |
| 1393 | * Reset the state of each associated channel and free the edge context. |
| 1394 | */ |
| 1395 | static void qcom_smd_edge_release(struct device *dev) |
| 1396 | { |
| 1397 | struct qcom_smd_channel *channel; |
| 1398 | struct qcom_smd_edge *edge = to_smd_edge(dev); |
| 1399 | |
| 1400 | list_for_each_entry(channel, &edge->channels, list) { |
| 1401 | SET_RX_CHANNEL_INFO(channel, state, SMD_CHANNEL_CLOSED); |
| 1402 | SET_RX_CHANNEL_INFO(channel, head, 0); |
| 1403 | SET_RX_CHANNEL_INFO(channel, tail, 0); |
| 1404 | } |
| 1405 | |
| 1406 | kfree(edge); |
| 1407 | } |
| 1408 | |
| 1409 | /** |
| 1410 | * qcom_smd_register_edge() - register an edge based on an device_node |
| 1411 | * @parent: parent device for the edge |
| 1412 | * @node: device_node describing the edge |
| 1413 | * |
| 1414 | * Returns an edge reference, or negative ERR_PTR() on failure. |
| 1415 | */ |
| 1416 | struct qcom_smd_edge *qcom_smd_register_edge(struct device *parent, |
| 1417 | struct device_node *node) |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1418 | { |
| 1419 | struct qcom_smd_edge *edge; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1420 | int ret; |
Bjorn Andersson | da05730 | 2016-08-15 11:15:57 -0700 | [diff] [blame] | 1421 | |
| 1422 | edge = kzalloc(sizeof(*edge), GFP_KERNEL); |
| 1423 | if (!edge) |
| 1424 | return ERR_PTR(-ENOMEM); |
| 1425 | |
| 1426 | init_waitqueue_head(&edge->new_channel_event); |
| 1427 | |
| 1428 | edge->dev.parent = parent; |
| 1429 | edge->dev.release = qcom_smd_edge_release; |
| 1430 | dev_set_name(&edge->dev, "%s:%s", dev_name(parent), node->name); |
| 1431 | ret = device_register(&edge->dev); |
| 1432 | if (ret) { |
| 1433 | pr_err("failed to register smd edge\n"); |
| 1434 | return ERR_PTR(ret); |
| 1435 | } |
| 1436 | |
| 1437 | ret = qcom_smd_parse_edge(&edge->dev, node, edge); |
| 1438 | if (ret) { |
| 1439 | dev_err(&edge->dev, "failed to parse smd edge\n"); |
| 1440 | goto unregister_dev; |
| 1441 | } |
| 1442 | |
| 1443 | schedule_work(&edge->scan_work); |
| 1444 | |
| 1445 | return edge; |
| 1446 | |
| 1447 | unregister_dev: |
| 1448 | put_device(&edge->dev); |
| 1449 | return ERR_PTR(ret); |
| 1450 | } |
| 1451 | EXPORT_SYMBOL(qcom_smd_register_edge); |
| 1452 | |
| 1453 | static int qcom_smd_remove_device(struct device *dev, void *data) |
| 1454 | { |
| 1455 | device_unregister(dev); |
| 1456 | of_node_put(dev->of_node); |
| 1457 | put_device(dev); |
| 1458 | |
| 1459 | return 0; |
| 1460 | } |
| 1461 | |
| 1462 | /** |
| 1463 | * qcom_smd_unregister_edge() - release an edge and its children |
| 1464 | * @edge: edge reference acquired from qcom_smd_register_edge |
| 1465 | */ |
| 1466 | int qcom_smd_unregister_edge(struct qcom_smd_edge *edge) |
| 1467 | { |
| 1468 | int ret; |
| 1469 | |
| 1470 | disable_irq(edge->irq); |
| 1471 | cancel_work_sync(&edge->scan_work); |
| 1472 | cancel_work_sync(&edge->state_work); |
| 1473 | |
| 1474 | ret = device_for_each_child(&edge->dev, NULL, qcom_smd_remove_device); |
| 1475 | if (ret) |
| 1476 | dev_warn(&edge->dev, "can't remove smd device: %d\n", ret); |
| 1477 | |
| 1478 | device_unregister(&edge->dev); |
| 1479 | |
| 1480 | return 0; |
| 1481 | } |
| 1482 | EXPORT_SYMBOL(qcom_smd_unregister_edge); |
| 1483 | |
| 1484 | static int qcom_smd_probe(struct platform_device *pdev) |
| 1485 | { |
| 1486 | struct device_node *node; |
Stephen Boyd | 1a03964 | 2015-09-02 15:46:44 -0700 | [diff] [blame] | 1487 | void *p; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1488 | |
| 1489 | /* Wait for smem */ |
Stephen Boyd | 1a03964 | 2015-09-02 15:46:44 -0700 | [diff] [blame] | 1490 | p = qcom_smem_get(QCOM_SMEM_HOST_ANY, smem_items[0].alloc_tbl_id, NULL); |
| 1491 | if (PTR_ERR(p) == -EPROBE_DEFER) |
| 1492 | return PTR_ERR(p); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1493 | |
Bjorn Andersson | da05730 | 2016-08-15 11:15:57 -0700 | [diff] [blame] | 1494 | for_each_available_child_of_node(pdev->dev.of_node, node) |
| 1495 | qcom_smd_register_edge(&pdev->dev, node); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1496 | |
| 1497 | return 0; |
| 1498 | } |
| 1499 | |
Bjorn Andersson | da05730 | 2016-08-15 11:15:57 -0700 | [diff] [blame] | 1500 | static int qcom_smd_remove_edge(struct device *dev, void *data) |
| 1501 | { |
| 1502 | struct qcom_smd_edge *edge = to_smd_edge(dev); |
| 1503 | |
| 1504 | return qcom_smd_unregister_edge(edge); |
| 1505 | } |
| 1506 | |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1507 | /* |
| 1508 | * Shut down all smd clients by making sure that each edge stops processing |
| 1509 | * events and scanning for new channels, then call destroy on the devices. |
| 1510 | */ |
| 1511 | static int qcom_smd_remove(struct platform_device *pdev) |
| 1512 | { |
Bjorn Andersson | da05730 | 2016-08-15 11:15:57 -0700 | [diff] [blame] | 1513 | int ret; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1514 | |
Bjorn Andersson | da05730 | 2016-08-15 11:15:57 -0700 | [diff] [blame] | 1515 | ret = device_for_each_child(&pdev->dev, NULL, qcom_smd_remove_edge); |
| 1516 | if (ret) |
| 1517 | dev_warn(&pdev->dev, "can't remove smd device: %d\n", ret); |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1518 | |
Bjorn Andersson | da05730 | 2016-08-15 11:15:57 -0700 | [diff] [blame] | 1519 | return ret; |
Bjorn Andersson | f2ab329 | 2015-07-27 20:20:30 -0700 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | static const struct of_device_id qcom_smd_of_match[] = { |
| 1523 | { .compatible = "qcom,smd" }, |
| 1524 | {} |
| 1525 | }; |
| 1526 | MODULE_DEVICE_TABLE(of, qcom_smd_of_match); |
| 1527 | |
| 1528 | static struct platform_driver qcom_smd_driver = { |
| 1529 | .probe = qcom_smd_probe, |
| 1530 | .remove = qcom_smd_remove, |
| 1531 | .driver = { |
| 1532 | .name = "qcom-smd", |
| 1533 | .of_match_table = qcom_smd_of_match, |
| 1534 | }, |
| 1535 | }; |
| 1536 | |
| 1537 | static int __init qcom_smd_init(void) |
| 1538 | { |
| 1539 | int ret; |
| 1540 | |
| 1541 | ret = bus_register(&qcom_smd_bus); |
| 1542 | if (ret) { |
| 1543 | pr_err("failed to register smd bus: %d\n", ret); |
| 1544 | return ret; |
| 1545 | } |
| 1546 | |
| 1547 | return platform_driver_register(&qcom_smd_driver); |
| 1548 | } |
| 1549 | postcore_initcall(qcom_smd_init); |
| 1550 | |
| 1551 | static void __exit qcom_smd_exit(void) |
| 1552 | { |
| 1553 | platform_driver_unregister(&qcom_smd_driver); |
| 1554 | bus_unregister(&qcom_smd_bus); |
| 1555 | } |
| 1556 | module_exit(qcom_smd_exit); |
| 1557 | |
| 1558 | MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>"); |
| 1559 | MODULE_DESCRIPTION("Qualcomm Shared Memory Driver"); |
| 1560 | MODULE_LICENSE("GPL v2"); |