qcom_smd.c 40 KB

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