smd.c 23 KB

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