smd.c 38 KB

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