smd.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. /* arch/arm/mach-msm/smd.c
  2. *
  3. * Copyright (C) 2007 Google, Inc.
  4. * Author: Brian Swetland <swetland@google.com>
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/platform_device.h>
  17. #include <linux/module.h>
  18. #include <linux/fs.h>
  19. #include <linux/cdev.h>
  20. #include <linux/device.h>
  21. #include <linux/wait.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/irq.h>
  24. #include <linux/list.h>
  25. #include <linux/slab.h>
  26. #include <linux/debugfs.h>
  27. #include <linux/delay.h>
  28. #include <mach/msm_smd.h>
  29. #include <mach/system.h>
  30. #include "smd_private.h"
  31. #include "proc_comm.h"
  32. #if defined(CONFIG_ARCH_QSD8X50)
  33. #define CONFIG_QDSP6 1
  34. #endif
  35. void (*msm_hw_reset_hook)(void);
  36. #define MODULE_NAME "msm_smd"
  37. enum {
  38. MSM_SMD_DEBUG = 1U << 0,
  39. MSM_SMSM_DEBUG = 1U << 0,
  40. };
  41. static int msm_smd_debug_mask;
  42. struct shared_info {
  43. int ready;
  44. unsigned state;
  45. };
  46. static unsigned dummy_state[SMSM_STATE_COUNT];
  47. static struct shared_info smd_info = {
  48. .state = (unsigned) &dummy_state,
  49. };
  50. module_param_named(debug_mask, msm_smd_debug_mask,
  51. int, S_IRUGO | S_IWUSR | S_IWGRP);
  52. static unsigned last_heap_free = 0xffffffff;
  53. static inline void notify_other_smsm(void)
  54. {
  55. msm_a2m_int(5);
  56. #ifdef CONFIG_QDSP6
  57. msm_a2m_int(8);
  58. #endif
  59. }
  60. static inline void notify_modem_smd(void)
  61. {
  62. msm_a2m_int(0);
  63. }
  64. static inline void notify_dsp_smd(void)
  65. {
  66. msm_a2m_int(8);
  67. }
  68. static void smd_diag(void)
  69. {
  70. char *x;
  71. x = smem_find(ID_DIAG_ERR_MSG, SZ_DIAG_ERR_MSG);
  72. if (x != 0) {
  73. x[SZ_DIAG_ERR_MSG - 1] = 0;
  74. pr_info("smem: DIAG '%s'\n", x);
  75. }
  76. }
  77. /* call when SMSM_RESET flag is set in the A9's smsm_state */
  78. static void handle_modem_crash(void)
  79. {
  80. pr_err("ARM9 has CRASHED\n");
  81. smd_diag();
  82. /* hard reboot if possible */
  83. if (msm_hw_reset_hook)
  84. msm_hw_reset_hook();
  85. /* in this case the modem or watchdog should reboot us */
  86. for (;;)
  87. ;
  88. }
  89. uint32_t raw_smsm_get_state(enum smsm_state_item item)
  90. {
  91. return readl(smd_info.state + item * 4);
  92. }
  93. static int check_for_modem_crash(void)
  94. {
  95. if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET) {
  96. handle_modem_crash();
  97. return -1;
  98. }
  99. return 0;
  100. }
  101. /* the spinlock is used to synchronize between the
  102. * irq handler and code that mutates the channel
  103. * list or fiddles with channel state
  104. */
  105. DEFINE_SPINLOCK(smd_lock);
  106. DEFINE_SPINLOCK(smem_lock);
  107. /* the mutex is used during open() and close()
  108. * operations to avoid races while creating or
  109. * destroying smd_channel structures
  110. */
  111. static DEFINE_MUTEX(smd_creation_mutex);
  112. static int smd_initialized;
  113. LIST_HEAD(smd_ch_closed_list);
  114. LIST_HEAD(smd_ch_list_modem);
  115. LIST_HEAD(smd_ch_list_dsp);
  116. static unsigned char smd_ch_allocated[64];
  117. static struct work_struct probe_work;
  118. /* how many bytes are available for reading */
  119. static int smd_stream_read_avail(struct smd_channel *ch)
  120. {
  121. return (ch->recv->head - ch->recv->tail) & ch->fifo_mask;
  122. }
  123. /* how many bytes we are free to write */
  124. static int smd_stream_write_avail(struct smd_channel *ch)
  125. {
  126. return ch->fifo_mask -
  127. ((ch->send->head - ch->send->tail) & ch->fifo_mask);
  128. }
  129. static int smd_packet_read_avail(struct smd_channel *ch)
  130. {
  131. if (ch->current_packet) {
  132. int n = smd_stream_read_avail(ch);
  133. if (n > ch->current_packet)
  134. n = ch->current_packet;
  135. return n;
  136. } else {
  137. return 0;
  138. }
  139. }
  140. static int smd_packet_write_avail(struct smd_channel *ch)
  141. {
  142. int n = smd_stream_write_avail(ch);
  143. return n > SMD_HEADER_SIZE ? n - SMD_HEADER_SIZE : 0;
  144. }
  145. static int ch_is_open(struct smd_channel *ch)
  146. {
  147. return (ch->recv->state == SMD_SS_OPENED) &&
  148. (ch->send->state == SMD_SS_OPENED);
  149. }
  150. /* provide a pointer and length to readable data in the fifo */
  151. static unsigned ch_read_buffer(struct smd_channel *ch, void **ptr)
  152. {
  153. unsigned head = ch->recv->head;
  154. unsigned tail = ch->recv->tail;
  155. *ptr = (void *) (ch->recv_data + tail);
  156. if (tail <= head)
  157. return head - tail;
  158. else
  159. return ch->fifo_size - tail;
  160. }
  161. /* advance the fifo read pointer after data from ch_read_buffer is consumed */
  162. static void ch_read_done(struct smd_channel *ch, unsigned count)
  163. {
  164. BUG_ON(count > smd_stream_read_avail(ch));
  165. ch->recv->tail = (ch->recv->tail + count) & ch->fifo_mask;
  166. ch->send->fTAIL = 1;
  167. }
  168. /* basic read interface to ch_read_{buffer,done} used
  169. * by smd_*_read() and update_packet_state()
  170. * will read-and-discard if the _data pointer is null
  171. */
  172. static int ch_read(struct smd_channel *ch, void *_data, int len)
  173. {
  174. void *ptr;
  175. unsigned n;
  176. unsigned char *data = _data;
  177. int orig_len = len;
  178. while (len > 0) {
  179. n = ch_read_buffer(ch, &ptr);
  180. if (n == 0)
  181. break;
  182. if (n > len)
  183. n = len;
  184. if (_data)
  185. memcpy(data, ptr, n);
  186. data += n;
  187. len -= n;
  188. ch_read_done(ch, n);
  189. }
  190. return orig_len - len;
  191. }
  192. static void update_stream_state(struct smd_channel *ch)
  193. {
  194. /* streams have no special state requiring updating */
  195. }
  196. static void update_packet_state(struct smd_channel *ch)
  197. {
  198. unsigned hdr[5];
  199. int r;
  200. /* can't do anything if we're in the middle of a packet */
  201. if (ch->current_packet != 0)
  202. return;
  203. /* don't bother unless we can get the full header */
  204. if (smd_stream_read_avail(ch) < SMD_HEADER_SIZE)
  205. return;
  206. r = ch_read(ch, hdr, SMD_HEADER_SIZE);
  207. BUG_ON(r != SMD_HEADER_SIZE);
  208. ch->current_packet = hdr[0];
  209. }
  210. /* provide a pointer and length to next free space in the fifo */
  211. static unsigned ch_write_buffer(struct smd_channel *ch, void **ptr)
  212. {
  213. unsigned head = ch->send->head;
  214. unsigned tail = ch->send->tail;
  215. *ptr = (void *) (ch->send_data + head);
  216. if (head < tail) {
  217. return tail - head - 1;
  218. } else {
  219. if (tail == 0)
  220. return ch->fifo_size - head - 1;
  221. else
  222. return ch->fifo_size - head;
  223. }
  224. }
  225. /* advace the fifo write pointer after freespace
  226. * from ch_write_buffer is filled
  227. */
  228. static void ch_write_done(struct smd_channel *ch, unsigned count)
  229. {
  230. BUG_ON(count > smd_stream_write_avail(ch));
  231. ch->send->head = (ch->send->head + count) & ch->fifo_mask;
  232. ch->send->fHEAD = 1;
  233. }
  234. static void ch_set_state(struct smd_channel *ch, unsigned n)
  235. {
  236. if (n == SMD_SS_OPENED) {
  237. ch->send->fDSR = 1;
  238. ch->send->fCTS = 1;
  239. ch->send->fCD = 1;
  240. } else {
  241. ch->send->fDSR = 0;
  242. ch->send->fCTS = 0;
  243. ch->send->fCD = 0;
  244. }
  245. ch->send->state = n;
  246. ch->send->fSTATE = 1;
  247. ch->notify_other_cpu();
  248. }
  249. static void do_smd_probe(void)
  250. {
  251. struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
  252. if (shared->heap_info.free_offset != last_heap_free) {
  253. last_heap_free = shared->heap_info.free_offset;
  254. schedule_work(&probe_work);
  255. }
  256. }
  257. static void smd_state_change(struct smd_channel *ch,
  258. unsigned last, unsigned next)
  259. {
  260. ch->last_state = next;
  261. pr_info("SMD: ch %d %d -> %d\n", ch->n, last, next);
  262. switch (next) {
  263. case SMD_SS_OPENING:
  264. ch->recv->tail = 0;
  265. case SMD_SS_OPENED:
  266. if (ch->send->state != SMD_SS_OPENED)
  267. ch_set_state(ch, SMD_SS_OPENED);
  268. ch->notify(ch->priv, SMD_EVENT_OPEN);
  269. break;
  270. case SMD_SS_FLUSHING:
  271. case SMD_SS_RESET:
  272. /* we should force them to close? */
  273. default:
  274. ch->notify(ch->priv, SMD_EVENT_CLOSE);
  275. }
  276. }
  277. static void handle_smd_irq(struct list_head *list, void (*notify)(void))
  278. {
  279. unsigned long flags;
  280. struct smd_channel *ch;
  281. int do_notify = 0;
  282. unsigned ch_flags;
  283. unsigned tmp;
  284. spin_lock_irqsave(&smd_lock, flags);
  285. list_for_each_entry(ch, list, ch_list) {
  286. ch_flags = 0;
  287. if (ch_is_open(ch)) {
  288. if (ch->recv->fHEAD) {
  289. ch->recv->fHEAD = 0;
  290. ch_flags |= 1;
  291. do_notify |= 1;
  292. }
  293. if (ch->recv->fTAIL) {
  294. ch->recv->fTAIL = 0;
  295. ch_flags |= 2;
  296. do_notify |= 1;
  297. }
  298. if (ch->recv->fSTATE) {
  299. ch->recv->fSTATE = 0;
  300. ch_flags |= 4;
  301. do_notify |= 1;
  302. }
  303. }
  304. tmp = ch->recv->state;
  305. if (tmp != ch->last_state)
  306. smd_state_change(ch, ch->last_state, tmp);
  307. if (ch_flags) {
  308. ch->update_state(ch);
  309. ch->notify(ch->priv, SMD_EVENT_DATA);
  310. }
  311. }
  312. if (do_notify)
  313. notify();
  314. spin_unlock_irqrestore(&smd_lock, flags);
  315. do_smd_probe();
  316. }
  317. static irqreturn_t smd_modem_irq_handler(int irq, void *data)
  318. {
  319. handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
  320. return IRQ_HANDLED;
  321. }
  322. #if defined(CONFIG_QDSP6)
  323. static irqreturn_t smd_dsp_irq_handler(int irq, void *data)
  324. {
  325. handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
  326. return IRQ_HANDLED;
  327. }
  328. #endif
  329. static void smd_fake_irq_handler(unsigned long arg)
  330. {
  331. handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
  332. handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
  333. }
  334. static DECLARE_TASKLET(smd_fake_irq_tasklet, smd_fake_irq_handler, 0);
  335. static inline int smd_need_int(struct smd_channel *ch)
  336. {
  337. if (ch_is_open(ch)) {
  338. if (ch->recv->fHEAD || ch->recv->fTAIL || ch->recv->fSTATE)
  339. return 1;
  340. if (ch->recv->state != ch->last_state)
  341. return 1;
  342. }
  343. return 0;
  344. }
  345. void smd_sleep_exit(void)
  346. {
  347. unsigned long flags;
  348. struct smd_channel *ch;
  349. int need_int = 0;
  350. spin_lock_irqsave(&smd_lock, flags);
  351. list_for_each_entry(ch, &smd_ch_list_modem, ch_list) {
  352. if (smd_need_int(ch)) {
  353. need_int = 1;
  354. break;
  355. }
  356. }
  357. list_for_each_entry(ch, &smd_ch_list_dsp, ch_list) {
  358. if (smd_need_int(ch)) {
  359. need_int = 1;
  360. break;
  361. }
  362. }
  363. spin_unlock_irqrestore(&smd_lock, flags);
  364. do_smd_probe();
  365. if (need_int) {
  366. if (msm_smd_debug_mask & MSM_SMD_DEBUG)
  367. pr_info("smd_sleep_exit need interrupt\n");
  368. tasklet_schedule(&smd_fake_irq_tasklet);
  369. }
  370. }
  371. void smd_kick(smd_channel_t *ch)
  372. {
  373. unsigned long flags;
  374. unsigned tmp;
  375. spin_lock_irqsave(&smd_lock, flags);
  376. ch->update_state(ch);
  377. tmp = ch->recv->state;
  378. if (tmp != ch->last_state) {
  379. ch->last_state = tmp;
  380. if (tmp == SMD_SS_OPENED)
  381. ch->notify(ch->priv, SMD_EVENT_OPEN);
  382. else
  383. ch->notify(ch->priv, SMD_EVENT_CLOSE);
  384. }
  385. ch->notify(ch->priv, SMD_EVENT_DATA);
  386. ch->notify_other_cpu();
  387. spin_unlock_irqrestore(&smd_lock, flags);
  388. }
  389. static int smd_is_packet(int chn, unsigned type)
  390. {
  391. type &= SMD_KIND_MASK;
  392. if (type == SMD_KIND_PACKET)
  393. return 1;
  394. if (type == SMD_KIND_STREAM)
  395. return 0;
  396. /* older AMSS reports SMD_KIND_UNKNOWN always */
  397. if ((chn > 4) || (chn == 1))
  398. return 1;
  399. else
  400. return 0;
  401. }
  402. static int smd_stream_write(smd_channel_t *ch, const void *_data, int len)
  403. {
  404. void *ptr;
  405. const unsigned char *buf = _data;
  406. unsigned xfer;
  407. int orig_len = len;
  408. if (len < 0)
  409. return -EINVAL;
  410. while ((xfer = ch_write_buffer(ch, &ptr)) != 0) {
  411. if (!ch_is_open(ch))
  412. break;
  413. if (xfer > len)
  414. xfer = len;
  415. memcpy(ptr, buf, xfer);
  416. ch_write_done(ch, xfer);
  417. len -= xfer;
  418. buf += xfer;
  419. if (len == 0)
  420. break;
  421. }
  422. ch->notify_other_cpu();
  423. return orig_len - len;
  424. }
  425. static int smd_packet_write(smd_channel_t *ch, const void *_data, int len)
  426. {
  427. unsigned hdr[5];
  428. if (len < 0)
  429. return -EINVAL;
  430. if (smd_stream_write_avail(ch) < (len + SMD_HEADER_SIZE))
  431. return -ENOMEM;
  432. hdr[0] = len;
  433. hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
  434. smd_stream_write(ch, hdr, sizeof(hdr));
  435. smd_stream_write(ch, _data, len);
  436. return len;
  437. }
  438. static int smd_stream_read(smd_channel_t *ch, void *data, int len)
  439. {
  440. int r;
  441. if (len < 0)
  442. return -EINVAL;
  443. r = ch_read(ch, data, len);
  444. if (r > 0)
  445. ch->notify_other_cpu();
  446. return r;
  447. }
  448. static int smd_packet_read(smd_channel_t *ch, void *data, int len)
  449. {
  450. unsigned long flags;
  451. int r;
  452. if (len < 0)
  453. return -EINVAL;
  454. if (len > ch->current_packet)
  455. len = ch->current_packet;
  456. r = ch_read(ch, data, len);
  457. if (r > 0)
  458. ch->notify_other_cpu();
  459. spin_lock_irqsave(&smd_lock, flags);
  460. ch->current_packet -= r;
  461. update_packet_state(ch);
  462. spin_unlock_irqrestore(&smd_lock, flags);
  463. return r;
  464. }
  465. static int smd_alloc_channel(const char *name, uint32_t cid, uint32_t type)
  466. {
  467. struct smd_channel *ch;
  468. ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
  469. if (ch == 0) {
  470. pr_err("smd_alloc_channel() out of memory\n");
  471. return -1;
  472. }
  473. ch->n = cid;
  474. if (_smd_alloc_channel(ch)) {
  475. kfree(ch);
  476. return -1;
  477. }
  478. ch->fifo_mask = ch->fifo_size - 1;
  479. ch->type = type;
  480. if ((type & SMD_TYPE_MASK) == SMD_TYPE_APPS_MODEM)
  481. ch->notify_other_cpu = notify_modem_smd;
  482. else
  483. ch->notify_other_cpu = notify_dsp_smd;
  484. if (smd_is_packet(cid, type)) {
  485. ch->read = smd_packet_read;
  486. ch->write = smd_packet_write;
  487. ch->read_avail = smd_packet_read_avail;
  488. ch->write_avail = smd_packet_write_avail;
  489. ch->update_state = update_packet_state;
  490. } else {
  491. ch->read = smd_stream_read;
  492. ch->write = smd_stream_write;
  493. ch->read_avail = smd_stream_read_avail;
  494. ch->write_avail = smd_stream_write_avail;
  495. ch->update_state = update_stream_state;
  496. }
  497. if ((type & 0xff) == 0)
  498. memcpy(ch->name, "SMD_", 4);
  499. else
  500. memcpy(ch->name, "DSP_", 4);
  501. memcpy(ch->name + 4, name, 20);
  502. ch->name[23] = 0;
  503. ch->pdev.name = ch->name;
  504. ch->pdev.id = -1;
  505. pr_info("smd_alloc_channel() cid=%02d size=%05d '%s'\n",
  506. ch->n, ch->fifo_size, ch->name);
  507. mutex_lock(&smd_creation_mutex);
  508. list_add(&ch->ch_list, &smd_ch_closed_list);
  509. mutex_unlock(&smd_creation_mutex);
  510. platform_device_register(&ch->pdev);
  511. return 0;
  512. }
  513. static void smd_channel_probe_worker(struct work_struct *work)
  514. {
  515. struct smd_alloc_elm *shared;
  516. unsigned ctype;
  517. unsigned type;
  518. unsigned n;
  519. shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
  520. if (!shared) {
  521. pr_err("smd: cannot find allocation table\n");
  522. return;
  523. }
  524. for (n = 0; n < 64; n++) {
  525. if (smd_ch_allocated[n])
  526. continue;
  527. if (!shared[n].ref_count)
  528. continue;
  529. if (!shared[n].name[0])
  530. continue;
  531. ctype = shared[n].ctype;
  532. type = ctype & SMD_TYPE_MASK;
  533. /* DAL channels are stream but neither the modem,
  534. * nor the DSP correctly indicate this. Fixup manually.
  535. */
  536. if (!memcmp(shared[n].name, "DAL", 3))
  537. ctype = (ctype & (~SMD_KIND_MASK)) | SMD_KIND_STREAM;
  538. type = shared[n].ctype & SMD_TYPE_MASK;
  539. if ((type == SMD_TYPE_APPS_MODEM) ||
  540. (type == SMD_TYPE_APPS_DSP))
  541. if (!smd_alloc_channel(shared[n].name, shared[n].cid, ctype))
  542. smd_ch_allocated[n] = 1;
  543. }
  544. }
  545. static void do_nothing_notify(void *priv, unsigned flags)
  546. {
  547. }
  548. struct smd_channel *smd_get_channel(const char *name)
  549. {
  550. struct smd_channel *ch;
  551. mutex_lock(&smd_creation_mutex);
  552. list_for_each_entry(ch, &smd_ch_closed_list, ch_list) {
  553. if (!strcmp(name, ch->name)) {
  554. list_del(&ch->ch_list);
  555. mutex_unlock(&smd_creation_mutex);
  556. return ch;
  557. }
  558. }
  559. mutex_unlock(&smd_creation_mutex);
  560. return NULL;
  561. }
  562. int smd_open(const char *name, smd_channel_t **_ch,
  563. void *priv, void (*notify)(void *, unsigned))
  564. {
  565. struct smd_channel *ch;
  566. unsigned long flags;
  567. if (smd_initialized == 0) {
  568. pr_info("smd_open() before smd_init()\n");
  569. return -ENODEV;
  570. }
  571. ch = smd_get_channel(name);
  572. if (!ch)
  573. return -ENODEV;
  574. if (notify == 0)
  575. notify = do_nothing_notify;
  576. ch->notify = notify;
  577. ch->current_packet = 0;
  578. ch->last_state = SMD_SS_CLOSED;
  579. ch->priv = priv;
  580. *_ch = ch;
  581. spin_lock_irqsave(&smd_lock, flags);
  582. if ((ch->type & SMD_TYPE_MASK) == SMD_TYPE_APPS_MODEM)
  583. list_add(&ch->ch_list, &smd_ch_list_modem);
  584. else
  585. list_add(&ch->ch_list, &smd_ch_list_dsp);
  586. /* If the remote side is CLOSING, we need to get it to
  587. * move to OPENING (which we'll do by moving from CLOSED to
  588. * OPENING) and then get it to move from OPENING to
  589. * OPENED (by doing the same state change ourselves).
  590. *
  591. * Otherwise, it should be OPENING and we can move directly
  592. * to OPENED so that it will follow.
  593. */
  594. if (ch->recv->state == SMD_SS_CLOSING) {
  595. ch->send->head = 0;
  596. ch_set_state(ch, SMD_SS_OPENING);
  597. } else {
  598. ch_set_state(ch, SMD_SS_OPENED);
  599. }
  600. spin_unlock_irqrestore(&smd_lock, flags);
  601. smd_kick(ch);
  602. return 0;
  603. }
  604. int smd_close(smd_channel_t *ch)
  605. {
  606. unsigned long flags;
  607. pr_info("smd_close(%p)\n", ch);
  608. if (ch == 0)
  609. return -1;
  610. spin_lock_irqsave(&smd_lock, flags);
  611. ch->notify = do_nothing_notify;
  612. list_del(&ch->ch_list);
  613. ch_set_state(ch, SMD_SS_CLOSED);
  614. spin_unlock_irqrestore(&smd_lock, flags);
  615. mutex_lock(&smd_creation_mutex);
  616. list_add(&ch->ch_list, &smd_ch_closed_list);
  617. mutex_unlock(&smd_creation_mutex);
  618. return 0;
  619. }
  620. int smd_read(smd_channel_t *ch, void *data, int len)
  621. {
  622. return ch->read(ch, data, len);
  623. }
  624. int smd_write(smd_channel_t *ch, const void *data, int len)
  625. {
  626. return ch->write(ch, data, len);
  627. }
  628. int smd_write_atomic(smd_channel_t *ch, const void *data, int len)
  629. {
  630. unsigned long flags;
  631. int res;
  632. spin_lock_irqsave(&smd_lock, flags);
  633. res = ch->write(ch, data, len);
  634. spin_unlock_irqrestore(&smd_lock, flags);
  635. return res;
  636. }
  637. int smd_read_avail(smd_channel_t *ch)
  638. {
  639. return ch->read_avail(ch);
  640. }
  641. int smd_write_avail(smd_channel_t *ch)
  642. {
  643. return ch->write_avail(ch);
  644. }
  645. int smd_wait_until_readable(smd_channel_t *ch, int bytes)
  646. {
  647. return -1;
  648. }
  649. int smd_wait_until_writable(smd_channel_t *ch, int bytes)
  650. {
  651. return -1;
  652. }
  653. int smd_cur_packet_size(smd_channel_t *ch)
  654. {
  655. return ch->current_packet;
  656. }
  657. /* ------------------------------------------------------------------------- */
  658. void *smem_alloc(unsigned id, unsigned size)
  659. {
  660. return smem_find(id, size);
  661. }
  662. void *smem_item(unsigned id, unsigned *size)
  663. {
  664. struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
  665. struct smem_heap_entry *toc = shared->heap_toc;
  666. if (id >= SMEM_NUM_ITEMS)
  667. return 0;
  668. if (toc[id].allocated) {
  669. *size = toc[id].size;
  670. return (void *) (MSM_SHARED_RAM_BASE + toc[id].offset);
  671. } else {
  672. *size = 0;
  673. }
  674. return 0;
  675. }
  676. void *smem_find(unsigned id, unsigned size_in)
  677. {
  678. unsigned size;
  679. void *ptr;
  680. ptr = smem_item(id, &size);
  681. if (!ptr)
  682. return 0;
  683. size_in = ALIGN(size_in, 8);
  684. if (size_in != size) {
  685. pr_err("smem_find(%d, %d): wrong size %d\n",
  686. id, size_in, size);
  687. return 0;
  688. }
  689. return ptr;
  690. }
  691. static irqreturn_t smsm_irq_handler(int irq, void *data)
  692. {
  693. unsigned long flags;
  694. unsigned apps, modm;
  695. spin_lock_irqsave(&smem_lock, flags);
  696. apps = raw_smsm_get_state(SMSM_STATE_APPS);
  697. modm = raw_smsm_get_state(SMSM_STATE_MODEM);
  698. if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
  699. pr_info("<SM %08x %08x>\n", apps, modm);
  700. if (modm & SMSM_RESET)
  701. handle_modem_crash();
  702. do_smd_probe();
  703. spin_unlock_irqrestore(&smem_lock, flags);
  704. return IRQ_HANDLED;
  705. }
  706. int smsm_change_state(enum smsm_state_item item,
  707. uint32_t clear_mask, uint32_t set_mask)
  708. {
  709. unsigned long addr = smd_info.state + item * 4;
  710. unsigned long flags;
  711. unsigned state;
  712. if (!smd_info.ready)
  713. return -EIO;
  714. spin_lock_irqsave(&smem_lock, flags);
  715. if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET)
  716. handle_modem_crash();
  717. state = (readl(addr) & ~clear_mask) | set_mask;
  718. writel(state, addr);
  719. if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
  720. pr_info("smsm_change_state %d %x\n", item, state);
  721. notify_other_smsm();
  722. spin_unlock_irqrestore(&smem_lock, flags);
  723. return 0;
  724. }
  725. uint32_t smsm_get_state(enum smsm_state_item item)
  726. {
  727. unsigned long flags;
  728. uint32_t rv;
  729. spin_lock_irqsave(&smem_lock, flags);
  730. rv = readl(smd_info.state + item * 4);
  731. if (item == SMSM_STATE_MODEM && (rv & SMSM_RESET))
  732. handle_modem_crash();
  733. spin_unlock_irqrestore(&smem_lock, flags);
  734. return rv;
  735. }
  736. #ifdef CONFIG_ARCH_MSM_SCORPION
  737. int smsm_set_sleep_duration(uint32_t delay)
  738. {
  739. struct msm_dem_slave_data *ptr;
  740. ptr = smem_find(SMEM_APPS_DEM_SLAVE_DATA, sizeof(*ptr));
  741. if (ptr == NULL) {
  742. pr_err("smsm_set_sleep_duration <SM NO APPS_DEM_SLAVE_DATA>\n");
  743. return -EIO;
  744. }
  745. if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
  746. pr_info("smsm_set_sleep_duration %d -> %d\n",
  747. ptr->sleep_time, delay);
  748. ptr->sleep_time = delay;
  749. return 0;
  750. }
  751. #else
  752. int smsm_set_sleep_duration(uint32_t delay)
  753. {
  754. uint32_t *ptr;
  755. ptr = smem_find(SMEM_SMSM_SLEEP_DELAY, sizeof(*ptr));
  756. if (ptr == NULL) {
  757. pr_err("smsm_set_sleep_duration <SM NO SLEEP_DELAY>\n");
  758. return -EIO;
  759. }
  760. if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
  761. pr_info("smsm_set_sleep_duration %d -> %d\n",
  762. *ptr, delay);
  763. *ptr = delay;
  764. return 0;
  765. }
  766. #endif
  767. int smd_core_init(void)
  768. {
  769. int r;
  770. pr_info("smd_core_init()\n");
  771. /* wait for essential items to be initialized */
  772. for (;;) {
  773. unsigned size;
  774. void *state;
  775. state = smem_item(SMEM_SMSM_SHARED_STATE, &size);
  776. if (size == SMSM_V1_SIZE || size == SMSM_V2_SIZE) {
  777. smd_info.state = (unsigned)state;
  778. break;
  779. }
  780. }
  781. smd_info.ready = 1;
  782. r = request_irq(INT_A9_M2A_0, smd_modem_irq_handler,
  783. IRQF_TRIGGER_RISING, "smd_dev", 0);
  784. if (r < 0)
  785. return r;
  786. r = enable_irq_wake(INT_A9_M2A_0);
  787. if (r < 0)
  788. pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_0\n");
  789. r = request_irq(INT_A9_M2A_5, smsm_irq_handler,
  790. IRQF_TRIGGER_RISING, "smsm_dev", 0);
  791. if (r < 0) {
  792. free_irq(INT_A9_M2A_0, 0);
  793. return r;
  794. }
  795. r = enable_irq_wake(INT_A9_M2A_5);
  796. if (r < 0)
  797. pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_5\n");
  798. #if defined(CONFIG_QDSP6)
  799. r = request_irq(INT_ADSP_A11, smd_dsp_irq_handler,
  800. IRQF_TRIGGER_RISING, "smd_dsp", 0);
  801. if (r < 0) {
  802. free_irq(INT_A9_M2A_0, 0);
  803. free_irq(INT_A9_M2A_5, 0);
  804. return r;
  805. }
  806. #endif
  807. /* check for any SMD channels that may already exist */
  808. do_smd_probe();
  809. /* indicate that we're up and running */
  810. smsm_change_state(SMSM_STATE_APPS,
  811. ~0, SMSM_INIT | SMSM_SMDINIT | SMSM_RPCINIT | SMSM_RUN);
  812. #ifdef CONFIG_ARCH_MSM_SCORPION
  813. smsm_change_state(SMSM_STATE_APPS_DEM, ~0, 0);
  814. #endif
  815. pr_info("smd_core_init() done\n");
  816. return 0;
  817. }
  818. static int __devinit msm_smd_probe(struct platform_device *pdev)
  819. {
  820. pr_info("smd_init()\n");
  821. /*
  822. * If we haven't waited for the ARM9 to boot up till now,
  823. * then we need to wait here. Otherwise this should just
  824. * return immediately.
  825. */
  826. proc_comm_boot_wait();
  827. INIT_WORK(&probe_work, smd_channel_probe_worker);
  828. if (smd_core_init()) {
  829. pr_err("smd_core_init() failed\n");
  830. return -1;
  831. }
  832. do_smd_probe();
  833. msm_check_for_modem_crash = check_for_modem_crash;
  834. msm_init_last_radio_log(THIS_MODULE);
  835. smd_initialized = 1;
  836. return 0;
  837. }
  838. static struct platform_driver msm_smd_driver = {
  839. .probe = msm_smd_probe,
  840. .driver = {
  841. .name = MODULE_NAME,
  842. .owner = THIS_MODULE,
  843. },
  844. };
  845. static int __init msm_smd_init(void)
  846. {
  847. return platform_driver_register(&msm_smd_driver);
  848. }
  849. module_init(msm_smd_init);
  850. MODULE_DESCRIPTION("MSM Shared Memory Core");
  851. MODULE_AUTHOR("Brian Swetland <swetland@google.com>");
  852. MODULE_LICENSE("GPL");