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