qcom_smd.c 36 KB

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