smd.c 21 KB

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