qcom_smd.c 41 KB

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