smd.c 39 KB

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