smd.c 21 KB

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