smd.c 23 KB

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