smd.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  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. void (*msm_hw_reset_hook)(void);
  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. module_param_named(debug_mask, msm_smd_debug_mask,
  42. int, S_IRUGO | S_IWUSR | S_IWGRP);
  43. void *smem_find(unsigned id, unsigned size);
  44. static void smd_diag(void);
  45. static unsigned last_heap_free = 0xffffffff;
  46. #define MSM_A2M_INT(n) (MSM_CSR_BASE + 0x400 + (n) * 4)
  47. static inline void notify_other_smsm(void)
  48. {
  49. writel(1, MSM_A2M_INT(5));
  50. }
  51. static inline void notify_other_smd(void)
  52. {
  53. writel(1, MSM_A2M_INT(0));
  54. }
  55. static void smd_diag(void)
  56. {
  57. char *x;
  58. x = smem_find(ID_DIAG_ERR_MSG, SZ_DIAG_ERR_MSG);
  59. if (x != 0) {
  60. x[SZ_DIAG_ERR_MSG - 1] = 0;
  61. pr_info("smem: DIAG '%s'\n", x);
  62. }
  63. }
  64. /* call when SMSM_RESET flag is set in the A9's smsm_state */
  65. static void handle_modem_crash(void)
  66. {
  67. pr_err("ARM9 has CRASHED\n");
  68. smd_diag();
  69. /* hard reboot if possible */
  70. if (msm_hw_reset_hook)
  71. msm_hw_reset_hook();
  72. /* in this case the modem or watchdog should reboot us */
  73. for (;;)
  74. ;
  75. }
  76. extern int (*msm_check_for_modem_crash)(void);
  77. static int check_for_modem_crash(void)
  78. {
  79. struct smsm_shared *smsm;
  80. smsm = smem_find(ID_SHARED_STATE, 2 * sizeof(struct smsm_shared));
  81. /* if the modem's not ready yet, we have to hope for the best */
  82. if (!smsm)
  83. return 0;
  84. if (smsm[1].state & SMSM_RESET) {
  85. handle_modem_crash();
  86. return -1;
  87. } else {
  88. return 0;
  89. }
  90. }
  91. #define SMD_SS_CLOSED 0x00000000
  92. #define SMD_SS_OPENING 0x00000001
  93. #define SMD_SS_OPENED 0x00000002
  94. #define SMD_SS_FLUSHING 0x00000003
  95. #define SMD_SS_CLOSING 0x00000004
  96. #define SMD_SS_RESET 0x00000005
  97. #define SMD_SS_RESET_OPENING 0x00000006
  98. #define SMD_BUF_SIZE 8192
  99. #define SMD_CHANNELS 64
  100. #define SMD_HEADER_SIZE 20
  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. static DEFINE_SPINLOCK(smd_lock);
  106. static 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. struct smd_alloc_elm {
  114. char name[20];
  115. uint32_t cid;
  116. uint32_t ctype;
  117. uint32_t ref_count;
  118. };
  119. struct smd_half_channel {
  120. unsigned state;
  121. unsigned char fDSR;
  122. unsigned char fCTS;
  123. unsigned char fCD;
  124. unsigned char fRI;
  125. unsigned char fHEAD;
  126. unsigned char fTAIL;
  127. unsigned char fSTATE;
  128. unsigned char fUNUSED;
  129. unsigned tail;
  130. unsigned head;
  131. unsigned char data[SMD_BUF_SIZE];
  132. };
  133. struct smd_shared {
  134. struct smd_half_channel ch0;
  135. struct smd_half_channel ch1;
  136. };
  137. struct smd_channel {
  138. volatile struct smd_half_channel *send;
  139. volatile struct smd_half_channel *recv;
  140. struct list_head ch_list;
  141. unsigned current_packet;
  142. unsigned n;
  143. void *priv;
  144. void (*notify)(void *priv, unsigned flags);
  145. int (*read)(smd_channel_t *ch, void *data, int len);
  146. int (*write)(smd_channel_t *ch, const void *data, int len);
  147. int (*read_avail)(smd_channel_t *ch);
  148. int (*write_avail)(smd_channel_t *ch);
  149. void (*update_state)(smd_channel_t *ch);
  150. unsigned last_state;
  151. char name[32];
  152. struct platform_device pdev;
  153. };
  154. static LIST_HEAD(smd_ch_closed_list);
  155. static LIST_HEAD(smd_ch_list);
  156. static unsigned char smd_ch_allocated[64];
  157. static struct work_struct probe_work;
  158. static void smd_alloc_channel(const char *name, uint32_t cid, uint32_t type);
  159. static void smd_channel_probe_worker(struct work_struct *work)
  160. {
  161. struct smd_alloc_elm *shared;
  162. unsigned n;
  163. shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
  164. if (!shared) {
  165. pr_err("smd: cannot find allocation table\n");
  166. return;
  167. }
  168. for (n = 0; n < 64; n++) {
  169. if (smd_ch_allocated[n])
  170. continue;
  171. if (!shared[n].ref_count)
  172. continue;
  173. if (!shared[n].name[0])
  174. continue;
  175. smd_alloc_channel(shared[n].name,
  176. shared[n].cid,
  177. shared[n].ctype);
  178. smd_ch_allocated[n] = 1;
  179. }
  180. }
  181. static char *chstate(unsigned n)
  182. {
  183. switch (n) {
  184. case SMD_SS_CLOSED:
  185. return "CLOSED";
  186. case SMD_SS_OPENING:
  187. return "OPENING";
  188. case SMD_SS_OPENED:
  189. return "OPENED";
  190. case SMD_SS_FLUSHING:
  191. return "FLUSHING";
  192. case SMD_SS_CLOSING:
  193. return "CLOSING";
  194. case SMD_SS_RESET:
  195. return "RESET";
  196. case SMD_SS_RESET_OPENING:
  197. return "ROPENING";
  198. default:
  199. return "UNKNOWN";
  200. }
  201. }
  202. /* how many bytes are available for reading */
  203. static int smd_stream_read_avail(struct smd_channel *ch)
  204. {
  205. return (ch->recv->head - ch->recv->tail) & (SMD_BUF_SIZE - 1);
  206. }
  207. /* how many bytes we are free to write */
  208. static int smd_stream_write_avail(struct smd_channel *ch)
  209. {
  210. return (SMD_BUF_SIZE - 1) -
  211. ((ch->send->head - ch->send->tail) & (SMD_BUF_SIZE - 1));
  212. }
  213. static int smd_packet_read_avail(struct smd_channel *ch)
  214. {
  215. if (ch->current_packet) {
  216. int n = smd_stream_read_avail(ch);
  217. if (n > ch->current_packet)
  218. n = ch->current_packet;
  219. return n;
  220. } else {
  221. return 0;
  222. }
  223. }
  224. static int smd_packet_write_avail(struct smd_channel *ch)
  225. {
  226. int n = smd_stream_write_avail(ch);
  227. return n > SMD_HEADER_SIZE ? n - SMD_HEADER_SIZE : 0;
  228. }
  229. static int ch_is_open(struct smd_channel *ch)
  230. {
  231. return (ch->recv->state == SMD_SS_OPENED) &&
  232. (ch->send->state == SMD_SS_OPENED);
  233. }
  234. /* provide a pointer and length to readable data in the fifo */
  235. static unsigned ch_read_buffer(struct smd_channel *ch, void **ptr)
  236. {
  237. unsigned head = ch->recv->head;
  238. unsigned tail = ch->recv->tail;
  239. *ptr = (void *) (ch->recv->data + tail);
  240. if (tail <= head)
  241. return head - tail;
  242. else
  243. return SMD_BUF_SIZE - tail;
  244. }
  245. /* advance the fifo read pointer after data from ch_read_buffer is consumed */
  246. static void ch_read_done(struct smd_channel *ch, unsigned count)
  247. {
  248. BUG_ON(count > smd_stream_read_avail(ch));
  249. ch->recv->tail = (ch->recv->tail + count) & (SMD_BUF_SIZE - 1);
  250. ch->recv->fTAIL = 1;
  251. }
  252. /* basic read interface to ch_read_{buffer,done} used
  253. ** by smd_*_read() and update_packet_state()
  254. ** will read-and-discard if the _data pointer is null
  255. */
  256. static int ch_read(struct smd_channel *ch, void *_data, int len)
  257. {
  258. void *ptr;
  259. unsigned n;
  260. unsigned char *data = _data;
  261. int orig_len = len;
  262. while (len > 0) {
  263. n = ch_read_buffer(ch, &ptr);
  264. if (n == 0)
  265. break;
  266. if (n > len)
  267. n = len;
  268. if (_data)
  269. memcpy(data, ptr, n);
  270. data += n;
  271. len -= n;
  272. ch_read_done(ch, n);
  273. }
  274. return orig_len - len;
  275. }
  276. static void update_stream_state(struct smd_channel *ch)
  277. {
  278. /* streams have no special state requiring updating */
  279. }
  280. static void update_packet_state(struct smd_channel *ch)
  281. {
  282. unsigned hdr[5];
  283. int r;
  284. /* can't do anything if we're in the middle of a packet */
  285. if (ch->current_packet != 0)
  286. return;
  287. /* don't bother unless we can get the full header */
  288. if (smd_stream_read_avail(ch) < SMD_HEADER_SIZE)
  289. return;
  290. r = ch_read(ch, hdr, SMD_HEADER_SIZE);
  291. BUG_ON(r != SMD_HEADER_SIZE);
  292. ch->current_packet = hdr[0];
  293. }
  294. /* provide a pointer and length to next free space in the fifo */
  295. static unsigned ch_write_buffer(struct smd_channel *ch, void **ptr)
  296. {
  297. unsigned head = ch->send->head;
  298. unsigned tail = ch->send->tail;
  299. *ptr = (void *) (ch->send->data + head);
  300. if (head < tail) {
  301. return tail - head - 1;
  302. } else {
  303. if (tail == 0)
  304. return SMD_BUF_SIZE - head - 1;
  305. else
  306. return SMD_BUF_SIZE - head;
  307. }
  308. }
  309. /* advace the fifo write pointer after freespace
  310. * from ch_write_buffer is filled
  311. */
  312. static void ch_write_done(struct smd_channel *ch, unsigned count)
  313. {
  314. BUG_ON(count > smd_stream_write_avail(ch));
  315. ch->send->head = (ch->send->head + count) & (SMD_BUF_SIZE - 1);
  316. ch->send->fHEAD = 1;
  317. }
  318. static void hc_set_state(volatile struct smd_half_channel *hc, unsigned n)
  319. {
  320. if (n == SMD_SS_OPENED) {
  321. hc->fDSR = 1;
  322. hc->fCTS = 1;
  323. hc->fCD = 1;
  324. } else {
  325. hc->fDSR = 0;
  326. hc->fCTS = 0;
  327. hc->fCD = 0;
  328. }
  329. hc->state = n;
  330. hc->fSTATE = 1;
  331. notify_other_smd();
  332. }
  333. static void do_smd_probe(void)
  334. {
  335. struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
  336. if (shared->heap_info.free_offset != last_heap_free) {
  337. last_heap_free = shared->heap_info.free_offset;
  338. schedule_work(&probe_work);
  339. }
  340. }
  341. static void smd_state_change(struct smd_channel *ch,
  342. unsigned last, unsigned next)
  343. {
  344. ch->last_state = next;
  345. pr_info("SMD: ch %d %s -> %s\n", ch->n,
  346. chstate(last), chstate(next));
  347. switch (next) {
  348. case SMD_SS_OPENING:
  349. ch->recv->tail = 0;
  350. case SMD_SS_OPENED:
  351. if (ch->send->state != SMD_SS_OPENED)
  352. hc_set_state(ch->send, SMD_SS_OPENED);
  353. ch->notify(ch->priv, SMD_EVENT_OPEN);
  354. break;
  355. case SMD_SS_FLUSHING:
  356. case SMD_SS_RESET:
  357. /* we should force them to close? */
  358. default:
  359. ch->notify(ch->priv, SMD_EVENT_CLOSE);
  360. }
  361. }
  362. static irqreturn_t smd_irq_handler(int irq, void *data)
  363. {
  364. unsigned long flags;
  365. struct smd_channel *ch;
  366. int do_notify = 0;
  367. unsigned ch_flags;
  368. unsigned tmp;
  369. spin_lock_irqsave(&smd_lock, flags);
  370. list_for_each_entry(ch, &smd_ch_list, ch_list) {
  371. ch_flags = 0;
  372. if (ch_is_open(ch)) {
  373. if (ch->recv->fHEAD) {
  374. ch->recv->fHEAD = 0;
  375. ch_flags |= 1;
  376. do_notify |= 1;
  377. }
  378. if (ch->recv->fTAIL) {
  379. ch->recv->fTAIL = 0;
  380. ch_flags |= 2;
  381. do_notify |= 1;
  382. }
  383. if (ch->recv->fSTATE) {
  384. ch->recv->fSTATE = 0;
  385. ch_flags |= 4;
  386. do_notify |= 1;
  387. }
  388. }
  389. tmp = ch->recv->state;
  390. if (tmp != ch->last_state)
  391. smd_state_change(ch, ch->last_state, tmp);
  392. if (ch_flags) {
  393. ch->update_state(ch);
  394. ch->notify(ch->priv, SMD_EVENT_DATA);
  395. }
  396. }
  397. if (do_notify)
  398. notify_other_smd();
  399. spin_unlock_irqrestore(&smd_lock, flags);
  400. do_smd_probe();
  401. return IRQ_HANDLED;
  402. }
  403. static void smd_fake_irq_handler(unsigned long arg)
  404. {
  405. smd_irq_handler(0, NULL);
  406. }
  407. static DECLARE_TASKLET(smd_fake_irq_tasklet, smd_fake_irq_handler, 0);
  408. void smd_sleep_exit(void)
  409. {
  410. unsigned long flags;
  411. struct smd_channel *ch;
  412. unsigned tmp;
  413. int need_int = 0;
  414. spin_lock_irqsave(&smd_lock, flags);
  415. list_for_each_entry(ch, &smd_ch_list, ch_list) {
  416. if (ch_is_open(ch)) {
  417. if (ch->recv->fHEAD) {
  418. if (msm_smd_debug_mask & MSM_SMD_DEBUG)
  419. pr_info("smd_sleep_exit ch %d fHEAD "
  420. "%x %x %x\n",
  421. ch->n, ch->recv->fHEAD,
  422. ch->recv->head, ch->recv->tail);
  423. need_int = 1;
  424. break;
  425. }
  426. if (ch->recv->fTAIL) {
  427. if (msm_smd_debug_mask & MSM_SMD_DEBUG)
  428. pr_info("smd_sleep_exit ch %d fTAIL "
  429. "%x %x %x\n",
  430. ch->n, ch->recv->fTAIL,
  431. ch->send->head, ch->send->tail);
  432. need_int = 1;
  433. break;
  434. }
  435. if (ch->recv->fSTATE) {
  436. if (msm_smd_debug_mask & MSM_SMD_DEBUG)
  437. pr_info("smd_sleep_exit ch %d fSTATE %x"
  438. "\n", ch->n, ch->recv->fSTATE);
  439. need_int = 1;
  440. break;
  441. }
  442. tmp = ch->recv->state;
  443. if (tmp != ch->last_state) {
  444. if (msm_smd_debug_mask & MSM_SMD_DEBUG)
  445. pr_info("smd_sleep_exit ch %d "
  446. "state %x != %x\n",
  447. ch->n, tmp, ch->last_state);
  448. need_int = 1;
  449. break;
  450. }
  451. }
  452. }
  453. spin_unlock_irqrestore(&smd_lock, flags);
  454. do_smd_probe();
  455. if (need_int) {
  456. if (msm_smd_debug_mask & MSM_SMD_DEBUG)
  457. pr_info("smd_sleep_exit need interrupt\n");
  458. tasklet_schedule(&smd_fake_irq_tasklet);
  459. }
  460. }
  461. void smd_kick(smd_channel_t *ch)
  462. {
  463. unsigned long flags;
  464. unsigned tmp;
  465. spin_lock_irqsave(&smd_lock, flags);
  466. ch->update_state(ch);
  467. tmp = ch->recv->state;
  468. if (tmp != ch->last_state) {
  469. ch->last_state = tmp;
  470. if (tmp == SMD_SS_OPENED)
  471. ch->notify(ch->priv, SMD_EVENT_OPEN);
  472. else
  473. ch->notify(ch->priv, SMD_EVENT_CLOSE);
  474. }
  475. ch->notify(ch->priv, SMD_EVENT_DATA);
  476. notify_other_smd();
  477. spin_unlock_irqrestore(&smd_lock, flags);
  478. }
  479. static int smd_is_packet(int chn)
  480. {
  481. if ((chn > 4) || (chn == 1))
  482. return 1;
  483. else
  484. return 0;
  485. }
  486. static int smd_stream_write(smd_channel_t *ch, const void *_data, int len)
  487. {
  488. void *ptr;
  489. const unsigned char *buf = _data;
  490. unsigned xfer;
  491. int orig_len = len;
  492. if (len < 0)
  493. return -EINVAL;
  494. while ((xfer = ch_write_buffer(ch, &ptr)) != 0) {
  495. if (!ch_is_open(ch))
  496. break;
  497. if (xfer > len)
  498. xfer = len;
  499. memcpy(ptr, buf, xfer);
  500. ch_write_done(ch, xfer);
  501. len -= xfer;
  502. buf += xfer;
  503. if (len == 0)
  504. break;
  505. }
  506. notify_other_smd();
  507. return orig_len - len;
  508. }
  509. static int smd_packet_write(smd_channel_t *ch, const void *_data, int len)
  510. {
  511. unsigned hdr[5];
  512. if (len < 0)
  513. return -EINVAL;
  514. if (smd_stream_write_avail(ch) < (len + SMD_HEADER_SIZE))
  515. return -ENOMEM;
  516. hdr[0] = len;
  517. hdr[1] = hdr[2] = hdr[3] = hdr[4] = 0;
  518. smd_stream_write(ch, hdr, sizeof(hdr));
  519. smd_stream_write(ch, _data, len);
  520. return len;
  521. }
  522. static int smd_stream_read(smd_channel_t *ch, void *data, int len)
  523. {
  524. int r;
  525. if (len < 0)
  526. return -EINVAL;
  527. r = ch_read(ch, data, len);
  528. if (r > 0)
  529. notify_other_smd();
  530. return r;
  531. }
  532. static int smd_packet_read(smd_channel_t *ch, void *data, int len)
  533. {
  534. unsigned long flags;
  535. int r;
  536. if (len < 0)
  537. return -EINVAL;
  538. if (len > ch->current_packet)
  539. len = ch->current_packet;
  540. r = ch_read(ch, data, len);
  541. if (r > 0)
  542. notify_other_smd();
  543. spin_lock_irqsave(&smd_lock, flags);
  544. ch->current_packet -= r;
  545. update_packet_state(ch);
  546. spin_unlock_irqrestore(&smd_lock, flags);
  547. return r;
  548. }
  549. static void smd_alloc_channel(const char *name, uint32_t cid, uint32_t type)
  550. {
  551. struct smd_channel *ch;
  552. struct smd_shared *shared;
  553. shared = smem_alloc(ID_SMD_CHANNELS + cid, sizeof(*shared));
  554. if (!shared) {
  555. pr_err("smd_alloc_channel() cid %d does not exist\n", cid);
  556. return;
  557. }
  558. ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
  559. if (ch == 0) {
  560. pr_err("smd_alloc_channel() out of memory\n");
  561. return;
  562. }
  563. ch->send = &shared->ch0;
  564. ch->recv = &shared->ch1;
  565. ch->n = cid;
  566. if (smd_is_packet(cid)) {
  567. ch->read = smd_packet_read;
  568. ch->write = smd_packet_write;
  569. ch->read_avail = smd_packet_read_avail;
  570. ch->write_avail = smd_packet_write_avail;
  571. ch->update_state = update_packet_state;
  572. } else {
  573. ch->read = smd_stream_read;
  574. ch->write = smd_stream_write;
  575. ch->read_avail = smd_stream_read_avail;
  576. ch->write_avail = smd_stream_write_avail;
  577. ch->update_state = update_stream_state;
  578. }
  579. memcpy(ch->name, "SMD_", 4);
  580. memcpy(ch->name + 4, name, 20);
  581. ch->name[23] = 0;
  582. ch->pdev.name = ch->name;
  583. ch->pdev.id = -1;
  584. pr_info("smd_alloc_channel() '%s' cid=%d, shared=%p\n",
  585. ch->name, ch->n, shared);
  586. mutex_lock(&smd_creation_mutex);
  587. list_add(&ch->ch_list, &smd_ch_closed_list);
  588. mutex_unlock(&smd_creation_mutex);
  589. platform_device_register(&ch->pdev);
  590. }
  591. static void do_nothing_notify(void *priv, unsigned flags)
  592. {
  593. }
  594. struct smd_channel *smd_get_channel(const char *name)
  595. {
  596. struct smd_channel *ch;
  597. mutex_lock(&smd_creation_mutex);
  598. list_for_each_entry(ch, &smd_ch_closed_list, ch_list) {
  599. if (!strcmp(name, ch->name)) {
  600. list_del(&ch->ch_list);
  601. mutex_unlock(&smd_creation_mutex);
  602. return ch;
  603. }
  604. }
  605. mutex_unlock(&smd_creation_mutex);
  606. return NULL;
  607. }
  608. int smd_open(const char *name, smd_channel_t **_ch,
  609. void *priv, void (*notify)(void *, unsigned))
  610. {
  611. struct smd_channel *ch;
  612. unsigned long flags;
  613. if (smd_initialized == 0) {
  614. pr_info("smd_open() before smd_init()\n");
  615. return -ENODEV;
  616. }
  617. ch = smd_get_channel(name);
  618. if (!ch)
  619. return -ENODEV;
  620. if (notify == 0)
  621. notify = do_nothing_notify;
  622. ch->notify = notify;
  623. ch->current_packet = 0;
  624. ch->last_state = SMD_SS_CLOSED;
  625. ch->priv = priv;
  626. *_ch = ch;
  627. spin_lock_irqsave(&smd_lock, flags);
  628. list_add(&ch->ch_list, &smd_ch_list);
  629. /* If the remote side is CLOSING, we need to get it to
  630. * move to OPENING (which we'll do by moving from CLOSED to
  631. * OPENING) and then get it to move from OPENING to
  632. * OPENED (by doing the same state change ourselves).
  633. *
  634. * Otherwise, it should be OPENING and we can move directly
  635. * to OPENED so that it will follow.
  636. */
  637. if (ch->recv->state == SMD_SS_CLOSING) {
  638. ch->send->head = 0;
  639. hc_set_state(ch->send, SMD_SS_OPENING);
  640. } else {
  641. hc_set_state(ch->send, SMD_SS_OPENED);
  642. }
  643. spin_unlock_irqrestore(&smd_lock, flags);
  644. smd_kick(ch);
  645. return 0;
  646. }
  647. int smd_close(smd_channel_t *ch)
  648. {
  649. unsigned long flags;
  650. pr_info("smd_close(%p)\n", ch);
  651. if (ch == 0)
  652. return -1;
  653. spin_lock_irqsave(&smd_lock, flags);
  654. ch->notify = do_nothing_notify;
  655. list_del(&ch->ch_list);
  656. hc_set_state(ch->send, SMD_SS_CLOSED);
  657. spin_unlock_irqrestore(&smd_lock, flags);
  658. mutex_lock(&smd_creation_mutex);
  659. list_add(&ch->ch_list, &smd_ch_closed_list);
  660. mutex_unlock(&smd_creation_mutex);
  661. return 0;
  662. }
  663. int smd_read(smd_channel_t *ch, void *data, int len)
  664. {
  665. return ch->read(ch, data, len);
  666. }
  667. int smd_write(smd_channel_t *ch, const void *data, int len)
  668. {
  669. return ch->write(ch, data, len);
  670. }
  671. int smd_read_avail(smd_channel_t *ch)
  672. {
  673. return ch->read_avail(ch);
  674. }
  675. int smd_write_avail(smd_channel_t *ch)
  676. {
  677. return ch->write_avail(ch);
  678. }
  679. int smd_wait_until_readable(smd_channel_t *ch, int bytes)
  680. {
  681. return -1;
  682. }
  683. int smd_wait_until_writable(smd_channel_t *ch, int bytes)
  684. {
  685. return -1;
  686. }
  687. int smd_cur_packet_size(smd_channel_t *ch)
  688. {
  689. return ch->current_packet;
  690. }
  691. /* ------------------------------------------------------------------------- */
  692. void *smem_alloc(unsigned id, unsigned size)
  693. {
  694. return smem_find(id, size);
  695. }
  696. static void *_smem_find(unsigned id, unsigned *size)
  697. {
  698. struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
  699. struct smem_heap_entry *toc = shared->heap_toc;
  700. if (id >= SMEM_NUM_ITEMS)
  701. return 0;
  702. if (toc[id].allocated) {
  703. *size = toc[id].size;
  704. return (void *) (MSM_SHARED_RAM_BASE + toc[id].offset);
  705. }
  706. return 0;
  707. }
  708. void *smem_find(unsigned id, unsigned size_in)
  709. {
  710. unsigned size;
  711. void *ptr;
  712. ptr = _smem_find(id, &size);
  713. if (!ptr)
  714. return 0;
  715. size_in = ALIGN(size_in, 8);
  716. if (size_in != size) {
  717. pr_err("smem_find(%d, %d): wrong size %d\n",
  718. id, size_in, size);
  719. return 0;
  720. }
  721. return ptr;
  722. }
  723. static irqreturn_t smsm_irq_handler(int irq, void *data)
  724. {
  725. unsigned long flags;
  726. struct smsm_shared *smsm;
  727. spin_lock_irqsave(&smem_lock, flags);
  728. smsm = smem_alloc(ID_SHARED_STATE,
  729. 2 * sizeof(struct smsm_shared));
  730. if (smsm == 0) {
  731. pr_info("<SM NO STATE>\n");
  732. } else {
  733. unsigned apps = smsm[0].state;
  734. unsigned modm = smsm[1].state;
  735. if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
  736. pr_info("<SM %08x %08x>\n", apps, modm);
  737. if (modm & SMSM_RESET) {
  738. handle_modem_crash();
  739. } else {
  740. apps |= SMSM_INIT;
  741. if (modm & SMSM_SMDINIT)
  742. apps |= SMSM_SMDINIT;
  743. if (modm & SMSM_RPCINIT)
  744. apps |= SMSM_RPCINIT;
  745. }
  746. if (smsm[0].state != apps) {
  747. if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
  748. pr_info("<SM %08x NOTIFY>\n", apps);
  749. smsm[0].state = apps;
  750. do_smd_probe();
  751. notify_other_smsm();
  752. }
  753. }
  754. spin_unlock_irqrestore(&smem_lock, flags);
  755. return IRQ_HANDLED;
  756. }
  757. int smsm_change_state(uint32_t clear_mask, uint32_t set_mask)
  758. {
  759. unsigned long flags;
  760. struct smsm_shared *smsm;
  761. spin_lock_irqsave(&smem_lock, flags);
  762. smsm = smem_alloc(ID_SHARED_STATE,
  763. 2 * sizeof(struct smsm_shared));
  764. if (smsm) {
  765. if (smsm[1].state & SMSM_RESET)
  766. handle_modem_crash();
  767. smsm[0].state = (smsm[0].state & ~clear_mask) | set_mask;
  768. if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
  769. pr_info("smsm_change_state %x\n",
  770. smsm[0].state);
  771. notify_other_smsm();
  772. }
  773. spin_unlock_irqrestore(&smem_lock, flags);
  774. if (smsm == NULL) {
  775. pr_err("smsm_change_state <SM NO STATE>\n");
  776. return -EIO;
  777. }
  778. return 0;
  779. }
  780. uint32_t smsm_get_state(void)
  781. {
  782. unsigned long flags;
  783. struct smsm_shared *smsm;
  784. uint32_t rv;
  785. spin_lock_irqsave(&smem_lock, flags);
  786. smsm = smem_alloc(ID_SHARED_STATE,
  787. 2 * sizeof(struct smsm_shared));
  788. if (smsm)
  789. rv = smsm[1].state;
  790. else
  791. rv = 0;
  792. if (rv & SMSM_RESET)
  793. handle_modem_crash();
  794. spin_unlock_irqrestore(&smem_lock, flags);
  795. if (smsm == NULL)
  796. pr_err("smsm_get_state <SM NO STATE>\n");
  797. return rv;
  798. }
  799. int smsm_set_sleep_duration(uint32_t delay)
  800. {
  801. uint32_t *ptr;
  802. ptr = smem_alloc(SMEM_SMSM_SLEEP_DELAY, sizeof(*ptr));
  803. if (ptr == NULL) {
  804. pr_err("smsm_set_sleep_duration <SM NO SLEEP_DELAY>\n");
  805. return -EIO;
  806. }
  807. if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
  808. pr_info("smsm_set_sleep_duration %d -> %d\n",
  809. *ptr, delay);
  810. *ptr = delay;
  811. return 0;
  812. }
  813. int smsm_set_interrupt_info(struct smsm_interrupt_info *info)
  814. {
  815. struct smsm_interrupt_info *ptr;
  816. ptr = smem_alloc(SMEM_SMSM_INT_INFO, sizeof(*ptr));
  817. if (ptr == NULL) {
  818. pr_err("smsm_set_sleep_duration <SM NO INT_INFO>\n");
  819. return -EIO;
  820. }
  821. if (msm_smd_debug_mask & MSM_SMSM_DEBUG)
  822. pr_info("smsm_set_interrupt_info %x %x -> %x %x\n",
  823. ptr->aArm_en_mask, ptr->aArm_interrupts_pending,
  824. info->aArm_en_mask, info->aArm_interrupts_pending);
  825. *ptr = *info;
  826. return 0;
  827. }
  828. #define MAX_NUM_SLEEP_CLIENTS 64
  829. #define MAX_SLEEP_NAME_LEN 8
  830. #define NUM_GPIO_INT_REGISTERS 6
  831. #define GPIO_SMEM_NUM_GROUPS 2
  832. #define GPIO_SMEM_MAX_PC_INTERRUPTS 8
  833. struct tramp_gpio_save {
  834. unsigned int enable;
  835. unsigned int detect;
  836. unsigned int polarity;
  837. };
  838. struct tramp_gpio_smem {
  839. uint16_t num_fired[GPIO_SMEM_NUM_GROUPS];
  840. uint16_t fired[GPIO_SMEM_NUM_GROUPS][GPIO_SMEM_MAX_PC_INTERRUPTS];
  841. uint32_t enabled[NUM_GPIO_INT_REGISTERS];
  842. uint32_t detection[NUM_GPIO_INT_REGISTERS];
  843. uint32_t polarity[NUM_GPIO_INT_REGISTERS];
  844. };
  845. void smsm_print_sleep_info(void)
  846. {
  847. unsigned long flags;
  848. uint32_t *ptr;
  849. struct tramp_gpio_smem *gpio;
  850. struct smsm_interrupt_info *int_info;
  851. spin_lock_irqsave(&smem_lock, flags);
  852. ptr = smem_alloc(SMEM_SMSM_SLEEP_DELAY, sizeof(*ptr));
  853. if (ptr)
  854. pr_info("SMEM_SMSM_SLEEP_DELAY: %x\n", *ptr);
  855. ptr = smem_alloc(SMEM_SMSM_LIMIT_SLEEP, sizeof(*ptr));
  856. if (ptr)
  857. pr_info("SMEM_SMSM_LIMIT_SLEEP: %x\n", *ptr);
  858. ptr = smem_alloc(SMEM_SLEEP_POWER_COLLAPSE_DISABLED, sizeof(*ptr));
  859. if (ptr)
  860. pr_info("SMEM_SLEEP_POWER_COLLAPSE_DISABLED: %x\n", *ptr);
  861. int_info = smem_alloc(SMEM_SMSM_INT_INFO, sizeof(*int_info));
  862. if (int_info)
  863. pr_info("SMEM_SMSM_INT_INFO %x %x %x\n",
  864. int_info->aArm_en_mask,
  865. int_info->aArm_interrupts_pending,
  866. int_info->aArm_wakeup_reason);
  867. gpio = smem_alloc(SMEM_GPIO_INT, sizeof(*gpio));
  868. if (gpio) {
  869. int i;
  870. for (i = 0; i < NUM_GPIO_INT_REGISTERS; i++)
  871. pr_info("SMEM_GPIO_INT: %d: e %x d %x p %x\n",
  872. i, gpio->enabled[i], gpio->detection[i],
  873. gpio->polarity[i]);
  874. for (i = 0; i < GPIO_SMEM_NUM_GROUPS; i++)
  875. pr_info("SMEM_GPIO_INT: %d: f %d: %d %d...\n",
  876. i, gpio->num_fired[i], gpio->fired[i][0],
  877. gpio->fired[i][1]);
  878. }
  879. spin_unlock_irqrestore(&smem_lock, flags);
  880. }
  881. int smd_core_init(void)
  882. {
  883. int r;
  884. pr_info("smd_core_init()\n");
  885. r = request_irq(INT_A9_M2A_0, smd_irq_handler,
  886. IRQF_TRIGGER_RISING, "smd_dev", 0);
  887. if (r < 0)
  888. return r;
  889. r = enable_irq_wake(INT_A9_M2A_0);
  890. if (r < 0)
  891. pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_0\n");
  892. r = request_irq(INT_A9_M2A_5, smsm_irq_handler,
  893. IRQF_TRIGGER_RISING, "smsm_dev", 0);
  894. if (r < 0) {
  895. free_irq(INT_A9_M2A_0, 0);
  896. return r;
  897. }
  898. r = enable_irq_wake(INT_A9_M2A_5);
  899. if (r < 0)
  900. pr_err("smd_core_init: enable_irq_wake failed for A9_M2A_5\n");
  901. /* we may have missed a signal while booting -- fake
  902. * an interrupt to make sure we process any existing
  903. * state
  904. */
  905. smsm_irq_handler(0, 0);
  906. pr_info("smd_core_init() done\n");
  907. return 0;
  908. }
  909. #if defined(CONFIG_DEBUG_FS)
  910. static int dump_ch(char *buf, int max, int n,
  911. struct smd_half_channel *s,
  912. struct smd_half_channel *r)
  913. {
  914. return scnprintf(
  915. buf, max,
  916. "ch%02d:"
  917. " %8s(%04d/%04d) %c%c%c%c%c%c%c <->"
  918. " %8s(%04d/%04d) %c%c%c%c%c%c%c\n", n,
  919. chstate(s->state), s->tail, s->head,
  920. s->fDSR ? 'D' : 'd',
  921. s->fCTS ? 'C' : 'c',
  922. s->fCD ? 'C' : 'c',
  923. s->fRI ? 'I' : 'i',
  924. s->fHEAD ? 'W' : 'w',
  925. s->fTAIL ? 'R' : 'r',
  926. s->fSTATE ? 'S' : 's',
  927. chstate(r->state), r->tail, r->head,
  928. r->fDSR ? 'D' : 'd',
  929. r->fCTS ? 'R' : 'r',
  930. r->fCD ? 'C' : 'c',
  931. r->fRI ? 'I' : 'i',
  932. r->fHEAD ? 'W' : 'w',
  933. r->fTAIL ? 'R' : 'r',
  934. r->fSTATE ? 'S' : 's'
  935. );
  936. }
  937. static int debug_read_stat(char *buf, int max)
  938. {
  939. struct smsm_shared *smsm;
  940. char *msg;
  941. int i = 0;
  942. smsm = smem_find(ID_SHARED_STATE,
  943. 2 * sizeof(struct smsm_shared));
  944. msg = smem_find(ID_DIAG_ERR_MSG, SZ_DIAG_ERR_MSG);
  945. if (smsm) {
  946. if (smsm[1].state & SMSM_RESET)
  947. i += scnprintf(buf + i, max - i,
  948. "smsm: ARM9 HAS CRASHED\n");
  949. i += scnprintf(buf + i, max - i, "smsm: a9: %08x a11: %08x\n",
  950. smsm[0].state, smsm[1].state);
  951. } else {
  952. i += scnprintf(buf + i, max - i, "smsm: cannot find\n");
  953. }
  954. if (msg) {
  955. msg[SZ_DIAG_ERR_MSG - 1] = 0;
  956. i += scnprintf(buf + i, max - i, "diag: '%s'\n", msg);
  957. }
  958. return i;
  959. }
  960. static int debug_read_mem(char *buf, int max)
  961. {
  962. unsigned n;
  963. struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
  964. struct smem_heap_entry *toc = shared->heap_toc;
  965. int i = 0;
  966. i += scnprintf(buf + i, max - i,
  967. "heap: init=%d free=%d remain=%d\n",
  968. shared->heap_info.initialized,
  969. shared->heap_info.free_offset,
  970. shared->heap_info.heap_remaining);
  971. for (n = 0; n < SMEM_NUM_ITEMS; n++) {
  972. if (toc[n].allocated == 0)
  973. continue;
  974. i += scnprintf(buf + i, max - i,
  975. "%04d: offsed %08x size %08x\n",
  976. n, toc[n].offset, toc[n].size);
  977. }
  978. return i;
  979. }
  980. static int debug_read_ch(char *buf, int max)
  981. {
  982. struct smd_shared *shared;
  983. int n, i = 0;
  984. for (n = 0; n < SMD_CHANNELS; n++) {
  985. shared = smem_find(ID_SMD_CHANNELS + n,
  986. sizeof(struct smd_shared));
  987. if (shared == 0)
  988. continue;
  989. i += dump_ch(buf + i, max - i, n, &shared->ch0, &shared->ch1);
  990. }
  991. return i;
  992. }
  993. static int debug_read_version(char *buf, int max)
  994. {
  995. struct smem_shared *shared = (void *) MSM_SHARED_RAM_BASE;
  996. unsigned version = shared->version[VERSION_MODEM];
  997. return sprintf(buf, "%d.%d\n", version >> 16, version & 0xffff);
  998. }
  999. static int debug_read_build_id(char *buf, int max)
  1000. {
  1001. unsigned size;
  1002. void *data;
  1003. data = _smem_find(SMEM_HW_SW_BUILD_ID, &size);
  1004. if (!data)
  1005. return 0;
  1006. if (size >= max)
  1007. size = max;
  1008. memcpy(buf, data, size);
  1009. return size;
  1010. }
  1011. static int debug_read_alloc_tbl(char *buf, int max)
  1012. {
  1013. struct smd_alloc_elm *shared;
  1014. int n, i = 0;
  1015. shared = smem_find(ID_CH_ALLOC_TBL, sizeof(*shared) * 64);
  1016. for (n = 0; n < 64; n++) {
  1017. if (shared[n].ref_count == 0)
  1018. continue;
  1019. i += scnprintf(buf + i, max - i,
  1020. "%03d: %20s cid=%02d ctype=%d ref_count=%d\n",
  1021. n, shared[n].name, shared[n].cid,
  1022. shared[n].ctype, shared[n].ref_count);
  1023. }
  1024. return i;
  1025. }
  1026. static int debug_boom(char *buf, int max)
  1027. {
  1028. unsigned ms = 5000;
  1029. msm_proc_comm(PCOM_RESET_MODEM, &ms, 0);
  1030. return 0;
  1031. }
  1032. #define DEBUG_BUFMAX 4096
  1033. static char debug_buffer[DEBUG_BUFMAX];
  1034. static ssize_t debug_read(struct file *file, char __user *buf,
  1035. size_t count, loff_t *ppos)
  1036. {
  1037. int (*fill)(char *buf, int max) = file->private_data;
  1038. int bsize = fill(debug_buffer, DEBUG_BUFMAX);
  1039. return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
  1040. }
  1041. static int debug_open(struct inode *inode, struct file *file)
  1042. {
  1043. file->private_data = inode->i_private;
  1044. return 0;
  1045. }
  1046. static const struct file_operations debug_ops = {
  1047. .read = debug_read,
  1048. .open = debug_open,
  1049. };
  1050. static void debug_create(const char *name, mode_t mode,
  1051. struct dentry *dent,
  1052. int (*fill)(char *buf, int max))
  1053. {
  1054. debugfs_create_file(name, mode, dent, fill, &debug_ops);
  1055. }
  1056. static void smd_debugfs_init(void)
  1057. {
  1058. struct dentry *dent;
  1059. dent = debugfs_create_dir("smd", 0);
  1060. if (IS_ERR(dent))
  1061. return;
  1062. debug_create("ch", 0444, dent, debug_read_ch);
  1063. debug_create("stat", 0444, dent, debug_read_stat);
  1064. debug_create("mem", 0444, dent, debug_read_mem);
  1065. debug_create("version", 0444, dent, debug_read_version);
  1066. debug_create("tbl", 0444, dent, debug_read_alloc_tbl);
  1067. debug_create("build", 0444, dent, debug_read_build_id);
  1068. debug_create("boom", 0444, dent, debug_boom);
  1069. }
  1070. #else
  1071. static void smd_debugfs_init(void) {}
  1072. #endif
  1073. static int __init msm_smd_probe(struct platform_device *pdev)
  1074. {
  1075. pr_info("smd_init()\n");
  1076. INIT_WORK(&probe_work, smd_channel_probe_worker);
  1077. if (smd_core_init()) {
  1078. pr_err("smd_core_init() failed\n");
  1079. return -1;
  1080. }
  1081. do_smd_probe();
  1082. msm_check_for_modem_crash = check_for_modem_crash;
  1083. smd_debugfs_init();
  1084. smd_initialized = 1;
  1085. return 0;
  1086. }
  1087. static struct platform_driver msm_smd_driver = {
  1088. .probe = msm_smd_probe,
  1089. .driver = {
  1090. .name = MODULE_NAME,
  1091. .owner = THIS_MODULE,
  1092. },
  1093. };
  1094. static int __init msm_smd_init(void)
  1095. {
  1096. return platform_driver_register(&msm_smd_driver);
  1097. }
  1098. module_init(msm_smd_init);
  1099. MODULE_DESCRIPTION("MSM Shared Memory Core");
  1100. MODULE_AUTHOR("Brian Swetland <swetland@google.com>");
  1101. MODULE_LICENSE("GPL");