smd.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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 <mach/system.h>
  31. #include "smd_private.h"
  32. #include "proc_comm.h"
  33. #if defined(CONFIG_ARCH_QSD8X50)
  34. #define CONFIG_QDSP6 1
  35. #endif
  36. void (*msm_hw_reset_hook)(void);
  37. #define MODULE_NAME "msm_smd"
  38. enum {
  39. MSM_SMD_DEBUG = 1U << 0,
  40. MSM_SMSM_DEBUG = 1U << 0,
  41. };
  42. static int msm_smd_debug_mask;
  43. struct shared_info {
  44. int ready;
  45. unsigned state;
  46. };
  47. static unsigned dummy_state[SMSM_STATE_COUNT];
  48. static struct shared_info smd_info = {
  49. .state = (unsigned) &dummy_state,
  50. };
  51. module_param_named(debug_mask, msm_smd_debug_mask,
  52. int, S_IRUGO | S_IWUSR | S_IWGRP);
  53. static unsigned last_heap_free = 0xffffffff;
  54. static inline void notify_other_smsm(void)
  55. {
  56. msm_a2m_int(5);
  57. #ifdef CONFIG_QDSP6
  58. msm_a2m_int(8);
  59. #endif
  60. }
  61. static inline void notify_modem_smd(void)
  62. {
  63. msm_a2m_int(0);
  64. }
  65. static inline void notify_dsp_smd(void)
  66. {
  67. msm_a2m_int(8);
  68. }
  69. static void smd_diag(void)
  70. {
  71. char *x;
  72. x = smem_find(ID_DIAG_ERR_MSG, SZ_DIAG_ERR_MSG);
  73. if (x != 0) {
  74. x[SZ_DIAG_ERR_MSG - 1] = 0;
  75. pr_debug("DIAG '%s'\n", x);
  76. }
  77. }
  78. /* call when SMSM_RESET flag is set in the A9's smsm_state */
  79. static void handle_modem_crash(void)
  80. {
  81. pr_err("ARM9 has CRASHED\n");
  82. smd_diag();
  83. /* hard reboot if possible */
  84. if (msm_hw_reset_hook)
  85. msm_hw_reset_hook();
  86. /* in this case the modem or watchdog should reboot us */
  87. for (;;)
  88. ;
  89. }
  90. uint32_t raw_smsm_get_state(enum smsm_state_item item)
  91. {
  92. return readl(smd_info.state + item * 4);
  93. }
  94. static int check_for_modem_crash(void)
  95. {
  96. if (raw_smsm_get_state(SMSM_STATE_MODEM) & SMSM_RESET) {
  97. handle_modem_crash();
  98. return -1;
  99. }
  100. return 0;
  101. }
  102. /* the spinlock is used to synchronize between the
  103. * irq handler and code that mutates the channel
  104. * list or fiddles with channel state
  105. */
  106. DEFINE_SPINLOCK(smd_lock);
  107. DEFINE_SPINLOCK(smem_lock);
  108. /* the mutex is used during open() and close()
  109. * operations to avoid races while creating or
  110. * destroying smd_channel structures
  111. */
  112. static DEFINE_MUTEX(smd_creation_mutex);
  113. static int smd_initialized;
  114. LIST_HEAD(smd_ch_closed_list);
  115. LIST_HEAD(smd_ch_list_modem);
  116. LIST_HEAD(smd_ch_list_dsp);
  117. static unsigned char smd_ch_allocated[64];
  118. static struct work_struct probe_work;
  119. /* how many bytes are available for reading */
  120. static int smd_stream_read_avail(struct smd_channel *ch)
  121. {
  122. return (ch->recv->head - ch->recv->tail) & ch->fifo_mask;
  123. }
  124. /* how many bytes we are free to write */
  125. static int smd_stream_write_avail(struct smd_channel *ch)
  126. {
  127. return ch->fifo_mask -
  128. ((ch->send->head - ch->send->tail) & ch->fifo_mask);
  129. }
  130. static int smd_packet_read_avail(struct smd_channel *ch)
  131. {
  132. if (ch->current_packet) {
  133. int n = smd_stream_read_avail(ch);
  134. if (n > ch->current_packet)
  135. n = ch->current_packet;
  136. return n;
  137. } else {
  138. return 0;
  139. }
  140. }
  141. static int smd_packet_write_avail(struct smd_channel *ch)
  142. {
  143. int n = smd_stream_write_avail(ch);
  144. return n > SMD_HEADER_SIZE ? n - SMD_HEADER_SIZE : 0;
  145. }
  146. static int ch_is_open(struct smd_channel *ch)
  147. {
  148. return (ch->recv->state == SMD_SS_OPENED) &&
  149. (ch->send->state == SMD_SS_OPENED);
  150. }
  151. /* provide a pointer and length to readable data in the fifo */
  152. static unsigned ch_read_buffer(struct smd_channel *ch, void **ptr)
  153. {
  154. unsigned head = ch->recv->head;
  155. unsigned tail = ch->recv->tail;
  156. *ptr = (void *) (ch->recv_data + tail);
  157. if (tail <= head)
  158. return head - tail;
  159. else
  160. return ch->fifo_size - tail;
  161. }
  162. /* advance the fifo read pointer after data from ch_read_buffer is consumed */
  163. static void ch_read_done(struct smd_channel *ch, unsigned count)
  164. {
  165. BUG_ON(count > smd_stream_read_avail(ch));
  166. ch->recv->tail = (ch->recv->tail + count) & ch->fifo_mask;
  167. ch->send->fTAIL = 1;
  168. }
  169. /* basic read interface to ch_read_{buffer,done} used
  170. * by smd_*_read() and update_packet_state()
  171. * will read-and-discard if the _data pointer is null
  172. */
  173. static int ch_read(struct smd_channel *ch, void *_data, int len)
  174. {
  175. void *ptr;
  176. unsigned n;
  177. unsigned char *data = _data;
  178. int orig_len = len;
  179. while (len > 0) {
  180. n = ch_read_buffer(ch, &ptr);
  181. if (n == 0)
  182. break;
  183. if (n > len)
  184. n = len;
  185. if (_data)
  186. memcpy(data, ptr, n);
  187. data += n;
  188. len -= n;
  189. ch_read_done(ch, n);
  190. }
  191. return orig_len - len;
  192. }
  193. static void update_stream_state(struct smd_channel *ch)
  194. {
  195. /* streams have no special state requiring updating */
  196. }
  197. static void update_packet_state(struct smd_channel *ch)
  198. {
  199. unsigned hdr[5];
  200. int r;
  201. /* can't do anything if we're in the middle of a packet */
  202. if (ch->current_packet != 0)
  203. return;
  204. /* don't bother unless we can get the full header */
  205. if (smd_stream_read_avail(ch) < SMD_HEADER_SIZE)
  206. return;
  207. r = ch_read(ch, hdr, SMD_HEADER_SIZE);
  208. BUG_ON(r != SMD_HEADER_SIZE);
  209. ch->current_packet = hdr[0];
  210. }
  211. /* provide a pointer and length to next free space in the fifo */
  212. static unsigned ch_write_buffer(struct smd_channel *ch, void **ptr)
  213. {
  214. unsigned head = ch->send->head;
  215. unsigned tail = ch->send->tail;
  216. *ptr = (void *) (ch->send_data + head);
  217. if (head < tail) {
  218. return tail - head - 1;
  219. } else {
  220. if (tail == 0)
  221. return ch->fifo_size - head - 1;
  222. else
  223. return ch->fifo_size - head;
  224. }
  225. }
  226. /* advace the fifo write pointer after freespace
  227. * from ch_write_buffer is filled
  228. */
  229. static void ch_write_done(struct smd_channel *ch, unsigned count)
  230. {
  231. BUG_ON(count > smd_stream_write_avail(ch));
  232. ch->send->head = (ch->send->head + count) & ch->fifo_mask;
  233. ch->send->fHEAD = 1;
  234. }
  235. static void ch_set_state(struct smd_channel *ch, unsigned n)
  236. {
  237. if (n == SMD_SS_OPENED) {
  238. ch->send->fDSR = 1;
  239. ch->send->fCTS = 1;
  240. ch->send->fCD = 1;
  241. } else {
  242. ch->send->fDSR = 0;
  243. ch->send->fCTS = 0;
  244. ch->send->fCD = 0;
  245. }
  246. ch->send->state = n;
  247. ch->send->fSTATE = 1;
  248. ch->notify_other_cpu();
  249. }
  250. static void do_smd_probe(void)
  251. {
  252. struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
  253. if (shared->heap_info.free_offset != last_heap_free) {
  254. last_heap_free = shared->heap_info.free_offset;
  255. schedule_work(&probe_work);
  256. }
  257. }
  258. static void smd_state_change(struct smd_channel *ch,
  259. unsigned last, unsigned next)
  260. {
  261. ch->last_state = next;
  262. pr_debug("ch %d %d -> %d\n", ch->n, last, next);
  263. switch (next) {
  264. case SMD_SS_OPENING:
  265. ch->recv->tail = 0;
  266. case SMD_SS_OPENED:
  267. if (ch->send->state != SMD_SS_OPENED)
  268. ch_set_state(ch, SMD_SS_OPENED);
  269. ch->notify(ch->priv, SMD_EVENT_OPEN);
  270. break;
  271. case SMD_SS_FLUSHING:
  272. case SMD_SS_RESET:
  273. /* we should force them to close? */
  274. default:
  275. ch->notify(ch->priv, SMD_EVENT_CLOSE);
  276. }
  277. }
  278. static void handle_smd_irq(struct list_head *list, void (*notify)(void))
  279. {
  280. unsigned long flags;
  281. struct smd_channel *ch;
  282. int do_notify = 0;
  283. unsigned ch_flags;
  284. unsigned tmp;
  285. spin_lock_irqsave(&smd_lock, flags);
  286. list_for_each_entry(ch, list, ch_list) {
  287. ch_flags = 0;
  288. if (ch_is_open(ch)) {
  289. if (ch->recv->fHEAD) {
  290. ch->recv->fHEAD = 0;
  291. ch_flags |= 1;
  292. do_notify |= 1;
  293. }
  294. if (ch->recv->fTAIL) {
  295. ch->recv->fTAIL = 0;
  296. ch_flags |= 2;
  297. do_notify |= 1;
  298. }
  299. if (ch->recv->fSTATE) {
  300. ch->recv->fSTATE = 0;
  301. ch_flags |= 4;
  302. do_notify |= 1;
  303. }
  304. }
  305. tmp = ch->recv->state;
  306. if (tmp != ch->last_state)
  307. smd_state_change(ch, ch->last_state, tmp);
  308. if (ch_flags) {
  309. ch->update_state(ch);
  310. ch->notify(ch->priv, SMD_EVENT_DATA);
  311. }
  312. }
  313. if (do_notify)
  314. notify();
  315. spin_unlock_irqrestore(&smd_lock, flags);
  316. do_smd_probe();
  317. }
  318. static irqreturn_t smd_modem_irq_handler(int irq, void *data)
  319. {
  320. handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
  321. return IRQ_HANDLED;
  322. }
  323. #if defined(CONFIG_QDSP6)
  324. static irqreturn_t smd_dsp_irq_handler(int irq, void *data)
  325. {
  326. handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
  327. return IRQ_HANDLED;
  328. }
  329. #endif
  330. static void smd_fake_irq_handler(unsigned long arg)
  331. {
  332. handle_smd_irq(&smd_ch_list_modem, notify_modem_smd);
  333. handle_smd_irq(&smd_ch_list_dsp, notify_dsp_smd);
  334. }
  335. static DECLARE_TASKLET(smd_fake_irq_tasklet, smd_fake_irq_handler, 0);
  336. static inline int smd_need_int(struct smd_channel *ch)
  337. {
  338. if (ch_is_open(ch)) {
  339. if (ch->recv->fHEAD || ch->recv->fTAIL || ch->recv->fSTATE)
  340. return 1;
  341. if (ch->recv->state != ch->last_state)
  342. return 1;
  343. }
  344. return 0;
  345. }
  346. void smd_sleep_exit(void)
  347. {
  348. unsigned long flags;
  349. struct smd_channel *ch;
  350. int need_int = 0;
  351. spin_lock_irqsave(&smd_lock, flags);
  352. list_for_each_entry(ch, &smd_ch_list_modem, ch_list) {
  353. if (smd_need_int(ch)) {
  354. need_int = 1;
  355. break;
  356. }
  357. }
  358. list_for_each_entry(ch, &smd_ch_list_dsp, ch_list) {
  359. if (smd_need_int(ch)) {
  360. need_int = 1;
  361. break;
  362. }
  363. }
  364. spin_unlock_irqrestore(&smd_lock, flags);
  365. do_smd_probe();
  366. if (need_int) {
  367. if (msm_smd_debug_mask & MSM_SMD_DEBUG)
  368. pr_info("smd_sleep_exit need interrupt\n");
  369. tasklet_schedule(&smd_fake_irq_tasklet);
  370. }
  371. }
  372. void smd_kick(smd_channel_t *ch)
  373. {
  374. unsigned long flags;
  375. unsigned tmp;
  376. spin_lock_irqsave(&smd_lock, flags);
  377. ch->update_state(ch);
  378. tmp = ch->recv->state;
  379. if (tmp != ch->last_state) {
  380. ch->last_state = tmp;
  381. if (tmp == SMD_SS_OPENED)
  382. ch->notify(ch->priv, SMD_EVENT_OPEN);
  383. else
  384. ch->notify(ch->priv, SMD_EVENT_CLOSE);
  385. }
  386. ch->notify(ch->priv, SMD_EVENT_DATA);
  387. ch->notify_other_cpu();
  388. spin_unlock_irqrestore(&smd_lock, flags);
  389. }
  390. static int smd_is_packet(int chn, unsigned type)
  391. {
  392. type &= SMD_KIND_MASK;
  393. if (type == SMD_KIND_PACKET)
  394. return 1;
  395. if (type == SMD_KIND_STREAM)
  396. return 0;
  397. /* older AMSS reports SMD_KIND_UNKNOWN always */
  398. if ((chn > 4) || (chn == 1))
  399. return 1;
  400. else
  401. return 0;
  402. }
  403. static int smd_stream_write(smd_channel_t *ch, const void *_data, int len)
  404. {
  405. void *ptr;
  406. const unsigned char *buf = _data;
  407. unsigned xfer;
  408. int orig_len = len;
  409. if (len < 0)
  410. return -EINVAL;
  411. while ((xfer = ch_write_buffer(ch, &ptr)) != 0) {
  412. if (!ch_is_open(ch))
  413. break;
  414. if (xfer > len)
  415. xfer = len;
  416. memcpy(ptr, buf, xfer);
  417. ch_write_done(ch, xfer);
  418. len -= xfer;
  419. buf += xfer;
  420. if (len == 0)
  421. break;
  422. }
  423. ch->notify_other_cpu();
  424. return orig_len - len;
  425. }
  426. static int smd_packet_write(smd_channel_t *ch, const void *_data, int len)
  427. {
  428. unsigned hdr[5];
  429. if (len < 0)
  430. return -EINVAL;
  431. if (smd_stream_write_avail(ch) < (len + SMD_HEADER_SIZE))
  432. return -ENOMEM;
  433. hdr[0] = len;
  434. hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
  435. smd_stream_write(ch, hdr, sizeof(hdr));
  436. smd_stream_write(ch, _data, len);
  437. return len;
  438. }
  439. static int smd_stream_read(smd_channel_t *ch, void *data, int len)
  440. {
  441. int r;
  442. if (len < 0)
  443. return -EINVAL;
  444. r = ch_read(ch, data, len);
  445. if (r > 0)
  446. ch->notify_other_cpu();
  447. return r;
  448. }
  449. static int smd_packet_read(smd_channel_t *ch, void *data, int len)
  450. {
  451. unsigned long flags;
  452. int r;
  453. if (len < 0)
  454. return -EINVAL;
  455. if (len > ch->current_packet)
  456. len = ch->current_packet;
  457. r = ch_read(ch, data, len);
  458. if (r > 0)
  459. ch->notify_other_cpu();
  460. spin_lock_irqsave(&smd_lock, flags);
  461. ch->current_packet -= r;
  462. update_packet_state(ch);
  463. spin_unlock_irqrestore(&smd_lock, flags);
  464. return r;
  465. }
  466. static int smd_alloc_channel(const char *name, uint32_t cid, uint32_t type)
  467. {
  468. struct smd_channel *ch;
  469. ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
  470. if (ch == 0) {
  471. pr_err("smd_alloc_channel() out of memory\n");
  472. return -1;
  473. }
  474. ch->n = cid;
  475. if (_smd_alloc_channel(ch)) {
  476. kfree(ch);
  477. return -1;
  478. }
  479. ch->fifo_mask = ch->fifo_size - 1;
  480. ch->type = type;
  481. if ((type & SMD_TYPE_MASK) == SMD_TYPE_APPS_MODEM)
  482. ch->notify_other_cpu = notify_modem_smd;
  483. else
  484. ch->notify_other_cpu = notify_dsp_smd;
  485. if (smd_is_packet(cid, type)) {
  486. ch->read = smd_packet_read;
  487. ch->write = smd_packet_write;
  488. ch->read_avail = smd_packet_read_avail;
  489. ch->write_avail = smd_packet_write_avail;
  490. ch->update_state = update_packet_state;
  491. } else {
  492. ch->read = smd_stream_read;
  493. ch->write = smd_stream_write;
  494. ch->read_avail = smd_stream_read_avail;
  495. ch->write_avail = smd_stream_write_avail;
  496. ch->update_state = update_stream_state;
  497. }
  498. if ((type & 0xff) == 0)
  499. memcpy(ch->name, "SMD_", 4);
  500. else
  501. memcpy(ch->name, "DSP_", 4);
  502. memcpy(ch->name + 4, name, 20);
  503. ch->name[23] = 0;
  504. ch->pdev.name = ch->name;
  505. ch->pdev.id = -1;
  506. pr_debug("smd_alloc_channel() cid=%02d size=%05d '%s'\n",
  507. ch->n, ch->fifo_size, ch->name);
  508. mutex_lock(&smd_creation_mutex);
  509. list_add(&ch->ch_list, &smd_ch_closed_list);
  510. mutex_unlock(&smd_creation_mutex);
  511. platform_device_register(&ch->pdev);
  512. return 0;
  513. }
  514. static void smd_channel_probe_worker(struct work_struct *work)
  515. {
  516. struct smd_alloc_elm *shared;
  517. unsigned ctype;
  518. unsigned type;
  519. unsigned n;
  520. shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
  521. if (!shared) {
  522. pr_err("cannot find allocation table\n");
  523. return;
  524. }
  525. for (n = 0; n < 64; n++) {
  526. if (smd_ch_allocated[n])
  527. continue;
  528. if (!shared[n].ref_count)
  529. continue;
  530. if (!shared[n].name[0])
  531. continue;
  532. ctype = shared[n].ctype;
  533. type = ctype & SMD_TYPE_MASK;
  534. /* DAL channels are stream but neither the modem,
  535. * nor the DSP correctly indicate this. Fixup manually.
  536. */
  537. if (!memcmp(shared[n].name, "DAL", 3))
  538. ctype = (ctype & (~SMD_KIND_MASK)) | SMD_KIND_STREAM;
  539. type = shared[n].ctype & SMD_TYPE_MASK;
  540. if ((type == SMD_TYPE_APPS_MODEM) ||
  541. (type == SMD_TYPE_APPS_DSP))
  542. if (!smd_alloc_channel(shared[n].name, shared[n].cid, ctype))
  543. smd_ch_allocated[n] = 1;
  544. }
  545. }
  546. static void do_nothing_notify(void *priv, unsigned flags)
  547. {
  548. }
  549. struct smd_channel *smd_get_channel(const char *name)
  550. {
  551. struct smd_channel *ch;
  552. mutex_lock(&smd_creation_mutex);
  553. list_for_each_entry(ch, &smd_ch_closed_list, ch_list) {
  554. if (!strcmp(name, ch->name)) {
  555. list_del(&ch->ch_list);
  556. mutex_unlock(&smd_creation_mutex);
  557. return ch;
  558. }
  559. }
  560. mutex_unlock(&smd_creation_mutex);
  561. return NULL;
  562. }
  563. int smd_open(const char *name, smd_channel_t **_ch,
  564. void *priv, void (*notify)(void *, unsigned))
  565. {
  566. struct smd_channel *ch;
  567. unsigned long flags;
  568. if (smd_initialized == 0) {
  569. pr_info("smd_open() before smd_init()\n");
  570. return -ENODEV;
  571. }
  572. ch = smd_get_channel(name);
  573. if (!ch)
  574. return -ENODEV;
  575. if (notify == 0)
  576. notify = do_nothing_notify;
  577. ch->notify = notify;
  578. ch->current_packet = 0;
  579. ch->last_state = SMD_SS_CLOSED;
  580. ch->priv = priv;
  581. *_ch = ch;
  582. spin_lock_irqsave(&smd_lock, flags);
  583. if ((ch->type & SMD_TYPE_MASK) == SMD_TYPE_APPS_MODEM)
  584. list_add(&ch->ch_list, &smd_ch_list_modem);
  585. else
  586. list_add(&ch->ch_list, &smd_ch_list_dsp);
  587. /* If the remote side is CLOSING, we need to get it to
  588. * move to OPENING (which we'll do by moving from CLOSED to
  589. * OPENING) and then get it to move from OPENING to
  590. * OPENED (by doing the same state change ourselves).
  591. *
  592. * Otherwise, it should be OPENING and we can move directly
  593. * to OPENED so that it will follow.
  594. */
  595. if (ch->recv->state == SMD_SS_CLOSING) {
  596. ch->send->head = 0;
  597. ch_set_state(ch, SMD_SS_OPENING);
  598. } else {
  599. ch_set_state(ch, SMD_SS_OPENED);
  600. }
  601. spin_unlock_irqrestore(&smd_lock, flags);
  602. smd_kick(ch);
  603. return 0;
  604. }
  605. int smd_close(smd_channel_t *ch)
  606. {
  607. unsigned long flags;
  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. /* wait for essential items to be initialized */
  771. for (;;) {
  772. unsigned size;
  773. void *state;
  774. state = smem_item(SMEM_SMSM_SHARED_STATE, &size);
  775. if (size == SMSM_V1_SIZE || size == SMSM_V2_SIZE) {
  776. smd_info.state = (unsigned)state;
  777. break;
  778. }
  779. }
  780. smd_info.ready = 1;
  781. r = request_irq(INT_A9_M2A_0, smd_modem_irq_handler,
  782. IRQF_TRIGGER_RISING, "smd_dev", 0);
  783. if (r < 0)
  784. return r;
  785. r = enable_irq_wake(INT_A9_M2A_0);
  786. if (r < 0)
  787. pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_0\n");
  788. r = request_irq(INT_A9_M2A_5, smsm_irq_handler,
  789. IRQF_TRIGGER_RISING, "smsm_dev", 0);
  790. if (r < 0) {
  791. free_irq(INT_A9_M2A_0, 0);
  792. return r;
  793. }
  794. r = enable_irq_wake(INT_A9_M2A_5);
  795. if (r < 0)
  796. pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_5\n");
  797. #if defined(CONFIG_QDSP6)
  798. r = request_irq(INT_ADSP_A11, smd_dsp_irq_handler,
  799. IRQF_TRIGGER_RISING, "smd_dsp", 0);
  800. if (r < 0) {
  801. free_irq(INT_A9_M2A_0, 0);
  802. free_irq(INT_A9_M2A_5, 0);
  803. return r;
  804. }
  805. #endif
  806. /* check for any SMD channels that may already exist */
  807. do_smd_probe();
  808. /* indicate that we're up and running */
  809. smsm_change_state(SMSM_STATE_APPS,
  810. ~0, SMSM_INIT | SMSM_SMDINIT | SMSM_RPCINIT | SMSM_RUN);
  811. #ifdef CONFIG_ARCH_MSM_SCORPION
  812. smsm_change_state(SMSM_STATE_APPS_DEM, ~0, 0);
  813. #endif
  814. return 0;
  815. }
  816. static int __devinit msm_smd_probe(struct platform_device *pdev)
  817. {
  818. /*
  819. * If we haven't waited for the ARM9 to boot up till now,
  820. * then we need to wait here. Otherwise this should just
  821. * return immediately.
  822. */
  823. proc_comm_boot_wait();
  824. INIT_WORK(&probe_work, smd_channel_probe_worker);
  825. if (smd_core_init()) {
  826. pr_err("smd_core_init() failed\n");
  827. return -1;
  828. }
  829. do_smd_probe();
  830. msm_check_for_modem_crash = check_for_modem_crash;
  831. msm_init_last_radio_log(THIS_MODULE);
  832. smd_initialized = 1;
  833. return 0;
  834. }
  835. static struct platform_driver msm_smd_driver = {
  836. .probe = msm_smd_probe,
  837. .driver = {
  838. .name = MODULE_NAME,
  839. .owner = THIS_MODULE,
  840. },
  841. };
  842. static int __init msm_smd_init(void)
  843. {
  844. return platform_driver_register(&msm_smd_driver);
  845. }
  846. module_init(msm_smd_init);
  847. MODULE_DESCRIPTION("MSM Shared Memory Core");
  848. MODULE_AUTHOR("Brian Swetland <swetland@google.com>");
  849. MODULE_LICENSE("GPL");