qcom_smd.c 40 KB

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