qcom_smd.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  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. return -EINVAL;
  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. return PTR_ERR(edge->mbox_chan);
  1123. edge->mbox_chan = NULL;
  1124. syscon_np = of_parse_phandle(node, "qcom,ipc", 0);
  1125. if (!syscon_np) {
  1126. dev_err(dev, "no qcom,ipc node\n");
  1127. return -ENODEV;
  1128. }
  1129. edge->ipc_regmap = syscon_node_to_regmap(syscon_np);
  1130. if (IS_ERR(edge->ipc_regmap))
  1131. return PTR_ERR(edge->ipc_regmap);
  1132. key = "qcom,ipc";
  1133. ret = of_property_read_u32_index(node, key, 1, &edge->ipc_offset);
  1134. if (ret < 0) {
  1135. dev_err(dev, "no offset in %s\n", key);
  1136. return -EINVAL;
  1137. }
  1138. ret = of_property_read_u32_index(node, key, 2, &edge->ipc_bit);
  1139. if (ret < 0) {
  1140. dev_err(dev, "no bit in %s\n", key);
  1141. return -EINVAL;
  1142. }
  1143. }
  1144. ret = of_property_read_string(node, "label", &edge->name);
  1145. if (ret < 0)
  1146. edge->name = node->name;
  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, *tmp;
  1169. struct qcom_smd_edge *edge = to_smd_edge(dev);
  1170. list_for_each_entry_safe(channel, tmp, &edge->channels, list) {
  1171. list_del(&channel->list);
  1172. kfree(channel->name);
  1173. kfree(channel);
  1174. }
  1175. kfree(edge);
  1176. }
  1177. static ssize_t rpmsg_name_show(struct device *dev,
  1178. struct device_attribute *attr, char *buf)
  1179. {
  1180. struct qcom_smd_edge *edge = to_smd_edge(dev);
  1181. return sprintf(buf, "%s\n", edge->name);
  1182. }
  1183. static DEVICE_ATTR_RO(rpmsg_name);
  1184. static struct attribute *qcom_smd_edge_attrs[] = {
  1185. &dev_attr_rpmsg_name.attr,
  1186. NULL
  1187. };
  1188. ATTRIBUTE_GROUPS(qcom_smd_edge);
  1189. /**
  1190. * qcom_smd_register_edge() - register an edge based on an device_node
  1191. * @parent: parent device for the edge
  1192. * @node: device_node describing the edge
  1193. *
  1194. * Returns an edge reference, or negative ERR_PTR() on failure.
  1195. */
  1196. struct qcom_smd_edge *qcom_smd_register_edge(struct device *parent,
  1197. struct device_node *node)
  1198. {
  1199. struct qcom_smd_edge *edge;
  1200. int ret;
  1201. edge = kzalloc(sizeof(*edge), GFP_KERNEL);
  1202. if (!edge)
  1203. return ERR_PTR(-ENOMEM);
  1204. init_waitqueue_head(&edge->new_channel_event);
  1205. edge->dev.parent = parent;
  1206. edge->dev.release = qcom_smd_edge_release;
  1207. edge->dev.of_node = node;
  1208. edge->dev.groups = qcom_smd_edge_groups;
  1209. dev_set_name(&edge->dev, "%s:%s", dev_name(parent), node->name);
  1210. ret = device_register(&edge->dev);
  1211. if (ret) {
  1212. pr_err("failed to register smd edge\n");
  1213. put_device(&edge->dev);
  1214. return ERR_PTR(ret);
  1215. }
  1216. ret = qcom_smd_parse_edge(&edge->dev, node, edge);
  1217. if (ret) {
  1218. dev_err(&edge->dev, "failed to parse smd edge\n");
  1219. goto unregister_dev;
  1220. }
  1221. ret = qcom_smd_create_chrdev(edge);
  1222. if (ret) {
  1223. dev_err(&edge->dev, "failed to register chrdev for edge\n");
  1224. goto unregister_dev;
  1225. }
  1226. schedule_work(&edge->scan_work);
  1227. return edge;
  1228. unregister_dev:
  1229. if (!IS_ERR_OR_NULL(edge->mbox_chan))
  1230. mbox_free_channel(edge->mbox_chan);
  1231. device_unregister(&edge->dev);
  1232. return ERR_PTR(ret);
  1233. }
  1234. EXPORT_SYMBOL(qcom_smd_register_edge);
  1235. static int qcom_smd_remove_device(struct device *dev, void *data)
  1236. {
  1237. device_unregister(dev);
  1238. return 0;
  1239. }
  1240. /**
  1241. * qcom_smd_unregister_edge() - release an edge and its children
  1242. * @edge: edge reference acquired from qcom_smd_register_edge
  1243. */
  1244. int qcom_smd_unregister_edge(struct qcom_smd_edge *edge)
  1245. {
  1246. int ret;
  1247. disable_irq(edge->irq);
  1248. cancel_work_sync(&edge->scan_work);
  1249. cancel_work_sync(&edge->state_work);
  1250. ret = device_for_each_child(&edge->dev, NULL, qcom_smd_remove_device);
  1251. if (ret)
  1252. dev_warn(&edge->dev, "can't remove smd device: %d\n", ret);
  1253. mbox_free_channel(edge->mbox_chan);
  1254. device_unregister(&edge->dev);
  1255. return 0;
  1256. }
  1257. EXPORT_SYMBOL(qcom_smd_unregister_edge);
  1258. static int qcom_smd_probe(struct platform_device *pdev)
  1259. {
  1260. struct device_node *node;
  1261. void *p;
  1262. /* Wait for smem */
  1263. p = qcom_smem_get(QCOM_SMEM_HOST_ANY, smem_items[0].alloc_tbl_id, NULL);
  1264. if (PTR_ERR(p) == -EPROBE_DEFER)
  1265. return PTR_ERR(p);
  1266. for_each_available_child_of_node(pdev->dev.of_node, node)
  1267. qcom_smd_register_edge(&pdev->dev, node);
  1268. return 0;
  1269. }
  1270. static int qcom_smd_remove_edge(struct device *dev, void *data)
  1271. {
  1272. struct qcom_smd_edge *edge = to_smd_edge(dev);
  1273. return qcom_smd_unregister_edge(edge);
  1274. }
  1275. /*
  1276. * Shut down all smd clients by making sure that each edge stops processing
  1277. * events and scanning for new channels, then call destroy on the devices.
  1278. */
  1279. static int qcom_smd_remove(struct platform_device *pdev)
  1280. {
  1281. int ret;
  1282. ret = device_for_each_child(&pdev->dev, NULL, qcom_smd_remove_edge);
  1283. if (ret)
  1284. dev_warn(&pdev->dev, "can't remove smd device: %d\n", ret);
  1285. return ret;
  1286. }
  1287. static const struct of_device_id qcom_smd_of_match[] = {
  1288. { .compatible = "qcom,smd" },
  1289. {}
  1290. };
  1291. MODULE_DEVICE_TABLE(of, qcom_smd_of_match);
  1292. static struct platform_driver qcom_smd_driver = {
  1293. .probe = qcom_smd_probe,
  1294. .remove = qcom_smd_remove,
  1295. .driver = {
  1296. .name = "qcom-smd",
  1297. .of_match_table = qcom_smd_of_match,
  1298. },
  1299. };
  1300. static int __init qcom_smd_init(void)
  1301. {
  1302. return platform_driver_register(&qcom_smd_driver);
  1303. }
  1304. subsys_initcall(qcom_smd_init);
  1305. static void __exit qcom_smd_exit(void)
  1306. {
  1307. platform_driver_unregister(&qcom_smd_driver);
  1308. }
  1309. module_exit(qcom_smd_exit);
  1310. MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
  1311. MODULE_DESCRIPTION("Qualcomm Shared Memory Driver");
  1312. MODULE_LICENSE("GPL v2");