qcom_smd.c 36 KB

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