smd.c 21 KB

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