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