qcom_smd.c 38 KB

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