audio_ocmem.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. /* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/types.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/mutex.h>
  18. #include <linux/sched.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/device.h>
  21. #include <linux/slab.h>
  22. #include <linux/io.h>
  23. #include <linux/of_device.h>
  24. #include <linux/memory_alloc.h>
  25. #include <asm/mach-types.h>
  26. #include <mach/msm_bus.h>
  27. #include <mach/msm_bus_board.h>
  28. #include <mach/ocmem.h>
  29. #include <mach/subsystem_notif.h>
  30. #include <mach/subsystem_restart.h>
  31. #include <mach/msm_memtypes.h>
  32. #include <mach/ramdump.h>
  33. #include <sound/q6core.h>
  34. #include "audio_ocmem.h"
  35. #define AUDIO_OCMEM_BUF_SIZE (512 * SZ_1K)
  36. /**
  37. * Exercise OCMEM Dump if audio OCMEM state is
  38. * one of the following. All other states indicate
  39. * audio data is not mapped from DDR to OCMEM and
  40. * therefore no need of dump.
  41. */
  42. #define _DO_OCMEM_DUMP_BIT_MASK_\
  43. ((1 << OCMEM_STATE_MAP_COMPL) |\
  44. (1 << OCMEM_STATE_MAP_TRANSITION) |\
  45. (1 << OCMEM_STATE_UNMAP_TRANSITION) |\
  46. (1 << OCMEM_STATE_SHRINK) |\
  47. (1 << OCMEM_STATE_GROW))
  48. /**
  49. * Wait for OCMEM driver to process and respond for
  50. * ongoing map/unmap request before calling OCMEM dump.
  51. */
  52. #define _WAIT_BFR_DUMP_BIT_MASK_\
  53. ((1 << OCMEM_STATE_MAP_COMPL) |\
  54. (1 << OCMEM_STATE_UNMAP_COMPL) |\
  55. (1 << OCMEM_STATE_MAP_FAIL) |\
  56. (1 << OCMEM_STATE_UNMAP_FAIL))
  57. #define _MAP_RESPONSE_BIT_MASK_\
  58. ((1 << OCMEM_STATE_MAP_COMPL) |\
  59. (1 << OCMEM_STATE_MAP_FAIL))
  60. #define _UNMAP_RESPONSE_BIT_MASK_\
  61. ((1 << OCMEM_STATE_UNMAP_COMPL) |\
  62. (1 << OCMEM_STATE_UNMAP_FAIL))
  63. #define _BIT_MASK_\
  64. ((1 << OCMEM_STATE_SSR) |\
  65. (1 << OCMEM_STATE_EXIT) |\
  66. (1 << OCMEM_STATE_GROW) |\
  67. (1 << OCMEM_STATE_SHRINK))
  68. #define set_bit_pos(x, y) (atomic_set(&x, (atomic_read(&x) | (1 << y))))
  69. #define clear_bit_pos(x, y) (atomic_set(&x, (atomic_read(&x) & (~(1 << y)))))
  70. #define test_bit_pos(x, y) ((atomic_read(&x)) & (1 << y))
  71. static int enable_ocmem_audio_voice;
  72. module_param(enable_ocmem_audio_voice, int,
  73. S_IRUGO | S_IWUSR | S_IWGRP);
  74. MODULE_PARM_DESC(enable_ocmem_audio_voice, "control OCMEM usage for audio/voice");
  75. enum {
  76. OCMEM_STATE_DEFAULT = 0,
  77. OCMEM_STATE_ALLOC = 1,
  78. OCMEM_STATE_MAP_TRANSITION,
  79. OCMEM_STATE_MAP_COMPL,
  80. OCMEM_STATE_UNMAP_TRANSITION,
  81. OCMEM_STATE_UNMAP_COMPL,
  82. OCMEM_STATE_SHRINK,
  83. OCMEM_STATE_GROW,
  84. OCMEM_STATE_FREE,
  85. OCMEM_STATE_MAP_FAIL,
  86. OCMEM_STATE_UNMAP_FAIL,
  87. OCMEM_STATE_EXIT,
  88. OCMEM_STATE_SSR,
  89. OCMEM_STATE_DISABLE,
  90. };
  91. static void audio_ocmem_process_workdata(struct work_struct *work);
  92. struct audio_ocmem_workdata {
  93. int id;
  94. bool en;
  95. struct work_struct work;
  96. };
  97. struct voice_ocmem_workdata {
  98. int id;
  99. bool en;
  100. struct work_struct work;
  101. };
  102. struct audio_ocmem_prv {
  103. atomic_t audio_state;
  104. struct ocmem_notifier *audio_hdl;
  105. struct ocmem_buf *buf;
  106. uint32_t audio_ocmem_bus_client;
  107. struct ocmem_map_list mlist;
  108. struct avcs_cmd_rsp_get_low_power_segments_info_t *lp_memseg_ptr;
  109. wait_queue_head_t audio_wait;
  110. atomic_t audio_cond;
  111. atomic_t audio_exit;
  112. spinlock_t audio_lock;
  113. struct mutex state_process_lock;
  114. struct workqueue_struct *audio_ocmem_workqueue;
  115. struct workqueue_struct *voice_ocmem_workqueue;
  116. bool ocmem_en;
  117. bool audio_ocmem_running;
  118. void *ocmem_ramdump_dev;
  119. struct ramdump_segment ocmem_ramdump_segment;
  120. unsigned long ocmem_dump_addr;
  121. };
  122. static struct audio_ocmem_prv audio_ocmem_lcl;
  123. static int audio_ocmem_client_cb(struct notifier_block *this,
  124. unsigned long event1, void *data)
  125. {
  126. int rc = NOTIFY_DONE;
  127. unsigned long flags;
  128. struct ocmem_buf *rbuf;
  129. int vwait = 0;
  130. pr_debug("%s: event[%ld] cur state[%x]\n", __func__,
  131. event1, atomic_read(&audio_ocmem_lcl.audio_state));
  132. spin_lock_irqsave(&audio_ocmem_lcl.audio_lock, flags);
  133. switch (event1) {
  134. case OCMEM_MAP_DONE:
  135. pr_debug("%s: map done\n", __func__);
  136. clear_bit_pos(audio_ocmem_lcl.audio_state,
  137. OCMEM_STATE_MAP_TRANSITION);
  138. set_bit_pos(audio_ocmem_lcl.audio_state, OCMEM_STATE_MAP_COMPL);
  139. break;
  140. case OCMEM_MAP_FAIL:
  141. pr_debug("%s: map fail\n", __func__);
  142. clear_bit_pos(audio_ocmem_lcl.audio_state,
  143. OCMEM_STATE_MAP_TRANSITION);
  144. set_bit_pos(audio_ocmem_lcl.audio_state, OCMEM_STATE_MAP_FAIL);
  145. break;
  146. case OCMEM_UNMAP_DONE:
  147. pr_debug("%s: unmap done\n", __func__);
  148. clear_bit_pos(audio_ocmem_lcl.audio_state,
  149. OCMEM_STATE_UNMAP_TRANSITION);
  150. set_bit_pos(audio_ocmem_lcl.audio_state,
  151. OCMEM_STATE_UNMAP_COMPL);
  152. break;
  153. case OCMEM_UNMAP_FAIL:
  154. pr_debug("%s: unmap fail\n", __func__);
  155. clear_bit_pos(audio_ocmem_lcl.audio_state,
  156. OCMEM_STATE_UNMAP_TRANSITION);
  157. set_bit_pos(audio_ocmem_lcl.audio_state,
  158. OCMEM_STATE_UNMAP_FAIL);
  159. break;
  160. case OCMEM_ALLOC_GROW:
  161. rbuf = data;
  162. if ((rbuf->len == AUDIO_OCMEM_BUF_SIZE)) {
  163. audio_ocmem_lcl.buf = data;
  164. pr_debug("%s: Alloc grow request received buf->addr: 0x%08lx\n",
  165. __func__,
  166. (audio_ocmem_lcl.buf)->addr);
  167. set_bit_pos(audio_ocmem_lcl.audio_state,
  168. OCMEM_STATE_GROW);
  169. } else {
  170. pr_debug("%s: Alloc grow request with size: %ld",
  171. __func__,
  172. rbuf->len);
  173. vwait = 1;
  174. }
  175. break;
  176. case OCMEM_ALLOC_SHRINK:
  177. pr_debug("%s: Alloc shrink request received\n", __func__);
  178. set_bit_pos(audio_ocmem_lcl.audio_state, OCMEM_STATE_SHRINK);
  179. break;
  180. default:
  181. pr_err("%s: Invalid event[%ld]\n", __func__, event1);
  182. break;
  183. }
  184. spin_unlock_irqrestore(&audio_ocmem_lcl.audio_lock, flags);
  185. atomic_set(&audio_ocmem_lcl.audio_cond, 0);
  186. wake_up(&audio_ocmem_lcl.audio_wait);
  187. return rc;
  188. }
  189. int get_state_to_process(atomic_t *state)
  190. {
  191. if (test_bit_pos((*state), OCMEM_STATE_SHRINK)) {
  192. pr_debug("%s: returning shrink state\n", __func__);
  193. return OCMEM_STATE_SHRINK;
  194. } else if (test_bit_pos((*state), OCMEM_STATE_GROW)) {
  195. pr_debug("%s: returning grow state\n", __func__);
  196. return OCMEM_STATE_GROW;
  197. } else if (test_bit_pos((*state), OCMEM_STATE_EXIT)) {
  198. pr_debug("%s: returning exit state\n", __func__);
  199. return OCMEM_STATE_EXIT;
  200. } else if (test_bit_pos((*state), OCMEM_STATE_SSR)) {
  201. pr_debug("%s: returning ssr state\n", __func__);
  202. return OCMEM_STATE_SSR;
  203. } else
  204. return -EINVAL;
  205. }
  206. /**
  207. * audio_ocmem_enable() - Exercise OCMEM for audio
  208. * @cid: client id - OCMEM_LP_AUDIO
  209. *
  210. * OCMEM gets allocated for audio usecase and the low power
  211. * segments obtained from the DSP will be moved from/to main
  212. * memory to OCMEM. Shrink and grow requests will be received
  213. * and processed accordingly based on the current audio state.
  214. */
  215. int audio_ocmem_enable(int cid)
  216. {
  217. int ret;
  218. int i, j;
  219. int state_bit;
  220. struct ocmem_buf *buf = NULL;
  221. struct avcs_cmd_rsp_get_low_power_segments_info_t *lp_segptr;
  222. pr_debug("%s, %p\n", __func__, &audio_ocmem_lcl);
  223. atomic_set(&audio_ocmem_lcl.audio_state, OCMEM_STATE_DEFAULT);
  224. if (audio_ocmem_lcl.lp_memseg_ptr == NULL) {
  225. /* Retrieve low power segments */
  226. ret = core_get_low_power_segments(
  227. &audio_ocmem_lcl.lp_memseg_ptr);
  228. if (ret != 0) {
  229. pr_err("%s: get low power segments from DSP failed, rc=%d\n",
  230. __func__, ret);
  231. mutex_unlock(&audio_ocmem_lcl.state_process_lock);
  232. goto fail_cmd;
  233. }
  234. }
  235. lp_segptr = audio_ocmem_lcl.lp_memseg_ptr;
  236. audio_ocmem_lcl.mlist.num_chunks = lp_segptr->num_segments;
  237. for (i = 0, j = 0; j < audio_ocmem_lcl.mlist.num_chunks; j++, i++) {
  238. audio_ocmem_lcl.mlist.chunks[j].ro =
  239. (lp_segptr->mem_segment[i].type == READ_ONLY_SEGMENT);
  240. audio_ocmem_lcl.mlist.chunks[j].ddr_paddr =
  241. lp_segptr->mem_segment[i].start_address_lsw;
  242. audio_ocmem_lcl.mlist.chunks[j].size =
  243. lp_segptr->mem_segment[i].size;
  244. pr_debug("%s: ro:%d, ddr_paddr[0x%08x], size[0x%x]\n", __func__,
  245. audio_ocmem_lcl.mlist.chunks[j].ro,
  246. (uint32_t)audio_ocmem_lcl.mlist.chunks[j].ddr_paddr,
  247. (uint32_t)audio_ocmem_lcl.mlist.chunks[j].size);
  248. }
  249. /* Non-blocking ocmem allocate (asynchronous) */
  250. buf = ocmem_allocate_nb(cid, AUDIO_OCMEM_BUF_SIZE);
  251. if (IS_ERR_OR_NULL(buf)) {
  252. pr_err("%s: failed: %d\n", __func__, cid);
  253. ret = -ENOMEM;
  254. mutex_unlock(&audio_ocmem_lcl.state_process_lock);
  255. goto fail_cmd;
  256. }
  257. set_bit_pos(audio_ocmem_lcl.audio_state, OCMEM_STATE_ALLOC);
  258. audio_ocmem_lcl.buf = buf;
  259. atomic_set(&audio_ocmem_lcl.audio_exit, 0);
  260. atomic_set(&audio_ocmem_lcl.audio_cond, 1);
  261. pr_debug("%s: buf->len: %ld\n", __func__, buf->len);
  262. if (!buf->len) {
  263. pr_debug("%s: buf.len is 0, waiting for ocmem region\n",
  264. __func__);
  265. mutex_unlock(&audio_ocmem_lcl.state_process_lock);
  266. wait_event_interruptible(audio_ocmem_lcl.audio_wait,
  267. (atomic_read(&audio_ocmem_lcl.audio_cond) == 0) ||
  268. (atomic_read(&audio_ocmem_lcl.audio_exit) == 1));
  269. if (atomic_read(&audio_ocmem_lcl.audio_exit)) {
  270. ret = ocmem_free(OCMEM_LP_AUDIO, audio_ocmem_lcl.buf);
  271. if (ret) {
  272. pr_err("%s: ocmem_free failed, state[%d]\n",
  273. __func__,
  274. atomic_read(&audio_ocmem_lcl.audio_state));
  275. }
  276. pr_info("%s: audio playback ended while waiting for ocmem\n",
  277. __func__);
  278. ret = 0;
  279. goto fail_cmd;
  280. }
  281. clear_bit_pos(audio_ocmem_lcl.audio_state, OCMEM_STATE_GROW);
  282. mutex_trylock(&audio_ocmem_lcl.state_process_lock);
  283. }
  284. pr_debug("%s: buf->len: %ld\n", __func__, (audio_ocmem_lcl.buf)->len);
  285. /* vote for ocmem bus bandwidth */
  286. ret = msm_bus_scale_client_update_request(
  287. audio_ocmem_lcl.audio_ocmem_bus_client,
  288. 1);
  289. if (ret)
  290. pr_err("%s: failed to vote for bus bandwidth\n", __func__);
  291. set_bit_pos(audio_ocmem_lcl.audio_state, OCMEM_STATE_MAP_TRANSITION);
  292. pr_debug("%s: buf->addr: 0x%08lx, len: %ld, audio_state[0x%x]\n",
  293. __func__,
  294. audio_ocmem_lcl.buf->addr,
  295. audio_ocmem_lcl.buf->len,
  296. atomic_read(&audio_ocmem_lcl.audio_state));
  297. atomic_set(&audio_ocmem_lcl.audio_cond, 1);
  298. ret = ocmem_map(cid, audio_ocmem_lcl.buf, &audio_ocmem_lcl.mlist);
  299. if (ret) {
  300. pr_err("%s: ocmem_map failed\n", __func__);
  301. atomic_set(&audio_ocmem_lcl.audio_state, OCMEM_STATE_MAP_FAIL);
  302. goto fail_cmd1;
  303. }
  304. wait_event_interruptible(audio_ocmem_lcl.audio_wait,
  305. (atomic_read(&audio_ocmem_lcl.audio_state) &
  306. _MAP_RESPONSE_BIT_MASK_) != 0);
  307. atomic_set(&audio_ocmem_lcl.audio_cond, 1);
  308. mutex_unlock(&audio_ocmem_lcl.state_process_lock);
  309. pr_debug("%s: audio_cond[%d] audio_state[0x%x]\n", __func__,
  310. atomic_read(&audio_ocmem_lcl.audio_cond),
  311. atomic_read(&audio_ocmem_lcl.audio_state));
  312. while ((test_bit_pos(audio_ocmem_lcl.audio_state,
  313. OCMEM_STATE_DISABLE)) == 0) {
  314. wait_event_interruptible(audio_ocmem_lcl.audio_wait,
  315. (atomic_read(&audio_ocmem_lcl.audio_state) &
  316. _BIT_MASK_) != 0);
  317. mutex_lock(&audio_ocmem_lcl.state_process_lock);
  318. state_bit = get_state_to_process(&audio_ocmem_lcl.audio_state);
  319. switch (state_bit) {
  320. case OCMEM_STATE_MAP_COMPL:
  321. pr_debug("%s: audio_cond[0x%x], audio_state[0x%x]\n",
  322. __func__, atomic_read(&audio_ocmem_lcl.audio_cond),
  323. atomic_read(&audio_ocmem_lcl.audio_state));
  324. atomic_set(&audio_ocmem_lcl.audio_state,
  325. OCMEM_STATE_MAP_COMPL);
  326. atomic_set(&audio_ocmem_lcl.audio_cond, 1);
  327. break;
  328. case OCMEM_STATE_SHRINK:
  329. pr_debug("%s: ocmem shrink request process\n",
  330. __func__);
  331. if (test_bit_pos(audio_ocmem_lcl.audio_state,
  332. OCMEM_STATE_MAP_COMPL)) {
  333. atomic_set(&audio_ocmem_lcl.audio_cond, 1);
  334. clear_bit_pos(audio_ocmem_lcl.audio_state,
  335. OCMEM_STATE_MAP_COMPL);
  336. set_bit_pos(audio_ocmem_lcl.audio_state,
  337. OCMEM_STATE_UNMAP_TRANSITION);
  338. ret = ocmem_unmap(cid, audio_ocmem_lcl.buf,
  339. &audio_ocmem_lcl.mlist);
  340. if (ret) {
  341. pr_err("%s: ocmem_unmap failed, state[%d]\n",
  342. __func__,
  343. atomic_read(&audio_ocmem_lcl.audio_state));
  344. goto fail_cmd1;
  345. }
  346. wait_event_interruptible(audio_ocmem_lcl.audio_wait,
  347. (atomic_read(&audio_ocmem_lcl.audio_state) &
  348. _UNMAP_RESPONSE_BIT_MASK_)
  349. != 0);
  350. ret = ocmem_shrink(cid, audio_ocmem_lcl.buf, 0);
  351. if (ret) {
  352. pr_err("%s: ocmem_shrink failed, state[%d]\n",
  353. __func__,
  354. atomic_read(&audio_ocmem_lcl.audio_state));
  355. goto fail_cmd1;
  356. }
  357. atomic_set(&audio_ocmem_lcl.audio_cond, 1);
  358. clear_bit_pos(audio_ocmem_lcl.audio_state,
  359. OCMEM_STATE_SHRINK);
  360. }
  361. pr_debug("%s:shrink process complete\n", __func__);
  362. break;
  363. case OCMEM_STATE_GROW:
  364. pr_debug("%s: ocmem grow request process\n",
  365. __func__);
  366. if (test_bit_pos(audio_ocmem_lcl.audio_state,
  367. OCMEM_STATE_UNMAP_COMPL)) {
  368. atomic_set(&audio_ocmem_lcl.audio_cond, 1);
  369. clear_bit_pos(audio_ocmem_lcl.audio_state,
  370. OCMEM_STATE_UNMAP_COMPL);
  371. set_bit_pos(audio_ocmem_lcl.audio_state,
  372. OCMEM_STATE_MAP_TRANSITION);
  373. ret = ocmem_map(cid, audio_ocmem_lcl.buf,
  374. &audio_ocmem_lcl.mlist);
  375. if (ret) {
  376. pr_err("%s: ocmem_map failed, state[%d]\n",
  377. __func__,
  378. atomic_read(&audio_ocmem_lcl.audio_state));
  379. goto fail_cmd1;
  380. }
  381. wait_event_interruptible(audio_ocmem_lcl.audio_wait,
  382. (atomic_read(&audio_ocmem_lcl.audio_state) &
  383. _MAP_RESPONSE_BIT_MASK_) != 0);
  384. clear_bit_pos(audio_ocmem_lcl.audio_state,
  385. OCMEM_STATE_GROW);
  386. atomic_set(&audio_ocmem_lcl.audio_cond, 1);
  387. }
  388. break;
  389. case OCMEM_STATE_EXIT:
  390. if (test_bit_pos(audio_ocmem_lcl.audio_state,
  391. OCMEM_STATE_MAP_COMPL)) {
  392. clear_bit_pos(audio_ocmem_lcl.audio_state,
  393. OCMEM_STATE_MAP_COMPL);
  394. set_bit_pos(audio_ocmem_lcl.audio_state,
  395. OCMEM_STATE_UNMAP_TRANSITION);
  396. ret = ocmem_unmap(cid, audio_ocmem_lcl.buf,
  397. &audio_ocmem_lcl.mlist);
  398. if (ret) {
  399. pr_err("%s: ocmem_unmap failed, state[0x%x]\n",
  400. __func__,
  401. atomic_read(&audio_ocmem_lcl.audio_state));
  402. goto fail_cmd1;
  403. }
  404. wait_event_interruptible(
  405. audio_ocmem_lcl.audio_wait,
  406. (atomic_read(&audio_ocmem_lcl.audio_state) &
  407. _UNMAP_RESPONSE_BIT_MASK_) != 0);
  408. }
  409. if (test_bit_pos(audio_ocmem_lcl.audio_state,
  410. OCMEM_STATE_SHRINK)) {
  411. pr_debug("%s: SHRINK while exiting\n",
  412. __func__);
  413. ret = ocmem_shrink(cid, audio_ocmem_lcl.buf,
  414. 0);
  415. if (ret) {
  416. pr_err("%s: ocmem_shrink failed, state[0x%x]\n",
  417. __func__,
  418. atomic_read(&audio_ocmem_lcl.audio_state));
  419. goto fail_cmd1;
  420. }
  421. clear_bit_pos(audio_ocmem_lcl.audio_state,
  422. OCMEM_STATE_SHRINK);
  423. }
  424. if (test_bit_pos(audio_ocmem_lcl.audio_state,
  425. OCMEM_STATE_DISABLE) ||
  426. test_bit_pos(audio_ocmem_lcl.audio_state,
  427. OCMEM_STATE_FREE)) {
  428. pr_info("%s: audio already freed from ocmem, state[0x%x]\n",
  429. __func__,
  430. atomic_read(&audio_ocmem_lcl.audio_state));
  431. goto fail_cmd2;
  432. }
  433. pr_debug("%s: calling ocmem free, state:0x%x\n",
  434. __func__,
  435. atomic_read(&audio_ocmem_lcl.audio_state));
  436. ret = ocmem_free(OCMEM_LP_AUDIO, audio_ocmem_lcl.buf);
  437. if (ret == -EAGAIN) {
  438. pr_debug("%s: received EAGAIN\n", __func__);
  439. if (test_bit_pos(audio_ocmem_lcl.audio_state,
  440. OCMEM_STATE_SHRINK)) {
  441. ret = ocmem_shrink(cid,
  442. audio_ocmem_lcl.buf,
  443. 0);
  444. if (ret) {
  445. pr_err("%s: ocmem_shrink failed, state[0x%x]\n",
  446. __func__,
  447. atomic_read(&audio_ocmem_lcl.audio_state));
  448. goto fail_cmd1;
  449. }
  450. pr_debug("calling free after EAGAIN");
  451. ret = ocmem_free(OCMEM_LP_AUDIO,
  452. audio_ocmem_lcl.buf);
  453. if (ret) {
  454. pr_err("%s: ocmem_free failed\n",
  455. __func__);
  456. goto fail_cmd2;
  457. }
  458. } else {
  459. pr_debug("%s: shrink callback already processed\n",
  460. __func__);
  461. goto fail_cmd1;
  462. }
  463. } else if (ret) {
  464. pr_err("%s: ocmem_free failed, state[0x%x], ret:%d\n",
  465. __func__,
  466. atomic_read(&audio_ocmem_lcl.audio_state),
  467. ret);
  468. goto fail_cmd2;
  469. }
  470. set_bit_pos(audio_ocmem_lcl.audio_state,
  471. OCMEM_STATE_FREE);
  472. pr_debug("%s: ocmem_free success, state[0x%x]\n",
  473. __func__,
  474. atomic_read(&audio_ocmem_lcl.audio_state));
  475. /* Fall through */
  476. case OCMEM_STATE_SSR:
  477. msm_bus_scale_client_update_request(
  478. audio_ocmem_lcl.audio_ocmem_bus_client,
  479. 0);
  480. set_bit_pos(audio_ocmem_lcl.audio_state,
  481. OCMEM_STATE_DISABLE);
  482. break;
  483. case -EINVAL:
  484. pr_info("%s: audio_cond[%d] audio_state[0x%x]\n",
  485. __func__,
  486. atomic_read(&audio_ocmem_lcl.audio_cond),
  487. atomic_read(&audio_ocmem_lcl.audio_state));
  488. break;
  489. }
  490. mutex_unlock(&audio_ocmem_lcl.state_process_lock);
  491. }
  492. ret = 0;
  493. goto fail_cmd;
  494. fail_cmd1:
  495. ret = ocmem_free(OCMEM_LP_AUDIO, audio_ocmem_lcl.buf);
  496. if (ret)
  497. pr_err("%s: ocmem_free failed\n", __func__);
  498. set_bit_pos(audio_ocmem_lcl.audio_state,
  499. OCMEM_STATE_FREE);
  500. fail_cmd2:
  501. mutex_unlock(&audio_ocmem_lcl.state_process_lock);
  502. fail_cmd:
  503. pr_debug("%s: exit\n", __func__);
  504. audio_ocmem_lcl.buf = NULL;
  505. audio_ocmem_lcl.audio_ocmem_running = false;
  506. return ret;
  507. }
  508. /**
  509. * audio_ocmem_disable() - Disable OCMEM for audio
  510. * @cid: client id - OCMEM_LP_AUDIO
  511. *
  512. * OCMEM gets deallocated for audio usecase. Depending on
  513. * current audio state, OCMEM will be freed from using audio
  514. * segments.
  515. */
  516. int audio_ocmem_disable(int cid)
  517. {
  518. pr_debug("%s: audio_cond[0x%x], audio_state[0x%x]\n", __func__,
  519. atomic_read(&audio_ocmem_lcl.audio_cond),
  520. atomic_read(&audio_ocmem_lcl.audio_state));
  521. if (!test_bit_pos(audio_ocmem_lcl.audio_state,
  522. OCMEM_STATE_SSR))
  523. set_bit_pos(audio_ocmem_lcl.audio_state,
  524. OCMEM_STATE_EXIT);
  525. wake_up(&audio_ocmem_lcl.audio_wait);
  526. mutex_unlock(&audio_ocmem_lcl.state_process_lock);
  527. pr_debug("%s: exit\n", __func__);
  528. return 0;
  529. }
  530. static void voice_ocmem_process_workdata(struct work_struct *work)
  531. {
  532. int cid;
  533. bool en;
  534. int rc = 0;
  535. struct voice_ocmem_workdata *voice_ocm_work =
  536. container_of(work, struct voice_ocmem_workdata, work);
  537. en = voice_ocm_work->en;
  538. switch (voice_ocm_work->id) {
  539. case VOICE:
  540. cid = OCMEM_VOICE;
  541. if (en)
  542. disable_ocmem_for_voice(cid);
  543. else
  544. enable_ocmem_after_voice(cid);
  545. break;
  546. default:
  547. pr_err("%s: Invalid client id[%d]\n", __func__,
  548. voice_ocm_work->id);
  549. rc = -EINVAL;
  550. }
  551. kfree(voice_ocm_work);
  552. return;
  553. }
  554. /**
  555. * voice_ocmem_process_req() - disable/enable OCMEM during voice call
  556. * @cid: client id - VOICE
  557. * @enable: 1 - enable
  558. * 0 - disable
  559. *
  560. * This configures OCMEM during start of voice call. If any
  561. * audio clients are already using OCMEM, they will be evicted
  562. * out of OCMEM during voice call and get restored after voice
  563. * call.
  564. */
  565. int voice_ocmem_process_req(int cid, bool enable)
  566. {
  567. struct voice_ocmem_workdata *workdata = NULL;
  568. if (enable) {
  569. if (!enable_ocmem_audio_voice)
  570. audio_ocmem_lcl.ocmem_en = false;
  571. else
  572. audio_ocmem_lcl.ocmem_en = true;
  573. }
  574. if (audio_ocmem_lcl.ocmem_en) {
  575. if (audio_ocmem_lcl.voice_ocmem_workqueue == NULL) {
  576. pr_err("%s: voice ocmem workqueue is NULL\n",
  577. __func__);
  578. return -EINVAL;
  579. }
  580. workdata = kzalloc(sizeof(struct voice_ocmem_workdata),
  581. GFP_ATOMIC);
  582. if (workdata == NULL) {
  583. pr_err("%s: mem failure\n", __func__);
  584. return -ENOMEM;
  585. }
  586. workdata->id = cid;
  587. workdata->en = enable;
  588. INIT_WORK(&workdata->work, voice_ocmem_process_workdata);
  589. queue_work(audio_ocmem_lcl.voice_ocmem_workqueue,
  590. &workdata->work);
  591. }
  592. return 0;
  593. }
  594. /**
  595. * disable_ocmem_for_voice() - disable OCMEM during voice call
  596. * @cid: client id - OCMEM_VOICE
  597. *
  598. * This configures OCMEM during start of voice call. If any
  599. * audio clients are already using OCMEM, they will be evicted
  600. */
  601. int disable_ocmem_for_voice(int cid)
  602. {
  603. int ret;
  604. ret = ocmem_evict(cid);
  605. if (ret)
  606. pr_err("%s: ocmem_evict is not successful\n", __func__);
  607. return ret;
  608. }
  609. /**
  610. * enable_ocmem_for_voice() - To enable OCMEM after voice call
  611. * @cid: client id - OCMEM_VOICE
  612. *
  613. * OCMEM gets re-enabled after OCMEM voice call. If other client
  614. * is evicted out of OCMEM, that gets restored and remapped in
  615. * OCMEM after the voice call.
  616. */
  617. int enable_ocmem_after_voice(int cid)
  618. {
  619. int ret;
  620. ret = ocmem_restore(cid);
  621. if (ret)
  622. pr_err("%s: ocmem_restore is not successful\n", __func__);
  623. return ret;
  624. }
  625. static void audio_ocmem_process_workdata(struct work_struct *work)
  626. {
  627. int cid;
  628. bool en;
  629. int rc = 0;
  630. struct audio_ocmem_workdata *audio_ocm_work =
  631. container_of(work, struct audio_ocmem_workdata, work);
  632. en = audio_ocm_work->en;
  633. mutex_lock(&audio_ocmem_lcl.state_process_lock);
  634. /* if previous work waiting for ocmem - signal it to exit */
  635. atomic_set(&audio_ocmem_lcl.audio_exit, 1);
  636. pr_debug("%s: acquired mutex for %d\n", __func__, en);
  637. switch (audio_ocm_work->id) {
  638. case AUDIO:
  639. cid = OCMEM_LP_AUDIO;
  640. if (en)
  641. audio_ocmem_enable(cid);
  642. else
  643. audio_ocmem_disable(cid);
  644. break;
  645. default:
  646. pr_err("%s: Invalid client id[%d]\n", __func__,
  647. audio_ocm_work->id);
  648. rc = -EINVAL;
  649. }
  650. kfree(audio_ocm_work);
  651. return;
  652. }
  653. /**
  654. * audio_ocmem_process_req() - process audio request to use OCMEM
  655. * @id: client id - OCMEM_LP_AUDIO
  656. * @enable: enable or disable OCMEM
  657. *
  658. * A workqueue gets created and initialized to use OCMEM for
  659. * audio clients.
  660. */
  661. int audio_ocmem_process_req(int id, bool enable)
  662. {
  663. struct audio_ocmem_workdata *workdata = NULL;
  664. if (enable) {
  665. if (!enable_ocmem_audio_voice)
  666. audio_ocmem_lcl.ocmem_en = false;
  667. else
  668. audio_ocmem_lcl.ocmem_en = true;
  669. }
  670. if (audio_ocmem_lcl.ocmem_en &&
  671. (!enable || !audio_ocmem_lcl.audio_ocmem_running)) {
  672. if (audio_ocmem_lcl.audio_ocmem_workqueue == NULL) {
  673. pr_err("%s: audio ocmem workqueue is NULL\n",
  674. __func__);
  675. return -EINVAL;
  676. }
  677. workdata = kzalloc(sizeof(struct audio_ocmem_workdata),
  678. GFP_ATOMIC);
  679. if (workdata == NULL) {
  680. pr_err("%s: mem failure\n", __func__);
  681. return -ENOMEM;
  682. }
  683. workdata->id = id;
  684. workdata->en = enable;
  685. audio_ocmem_lcl.audio_ocmem_running = true;
  686. INIT_WORK(&workdata->work, audio_ocmem_process_workdata);
  687. queue_work(audio_ocmem_lcl.audio_ocmem_workqueue,
  688. &workdata->work);
  689. }
  690. return 0;
  691. }
  692. static struct notifier_block audio_ocmem_client_nb = {
  693. .notifier_call = audio_ocmem_client_cb,
  694. };
  695. static int audio_ocmem_platform_data_populate(struct platform_device *pdev)
  696. {
  697. struct msm_bus_scale_pdata *audio_ocmem_adata = NULL;
  698. if (!pdev->dev.of_node) {
  699. pr_err("%s: device tree information missing\n", __func__);
  700. return -ENODEV;
  701. }
  702. audio_ocmem_adata = msm_bus_cl_get_pdata(pdev);
  703. if (!audio_ocmem_adata) {
  704. pr_err("%s: bus device tree allocation failed\n", __func__);
  705. return -EINVAL;
  706. }
  707. dev_set_drvdata(&pdev->dev, audio_ocmem_adata);
  708. return 0;
  709. }
  710. static void do_ocmem_ramdump(void)
  711. {
  712. int ret = 0;
  713. void *virt = NULL;
  714. virt = ioremap(audio_ocmem_lcl.ocmem_dump_addr, AUDIO_OCMEM_BUF_SIZE);
  715. ret = ocmem_dump(OCMEM_LP_AUDIO,
  716. audio_ocmem_lcl.buf,
  717. (unsigned long)virt);
  718. iounmap(virt);
  719. if (ret)
  720. pr_err("%s: ocmem_dump failed\n", __func__);
  721. audio_ocmem_lcl.ocmem_ramdump_segment.address
  722. = (unsigned long)audio_ocmem_lcl.ocmem_dump_addr;
  723. audio_ocmem_lcl.ocmem_ramdump_segment.size
  724. = AUDIO_OCMEM_BUF_SIZE;
  725. ret = do_ramdump(audio_ocmem_lcl.ocmem_ramdump_dev,
  726. &audio_ocmem_lcl.ocmem_ramdump_segment,
  727. 1);
  728. if (ret < 0)
  729. pr_err("%s: do_ramdump failed\n", __func__);
  730. }
  731. static void process_ocmem_dump(void)
  732. {
  733. int ret = 0;
  734. set_bit_pos(audio_ocmem_lcl.audio_state, OCMEM_STATE_SSR);
  735. if (atomic_read(&audio_ocmem_lcl.audio_state) &
  736. _DO_OCMEM_DUMP_BIT_MASK_) {
  737. wait_event_interruptible(audio_ocmem_lcl.audio_wait,
  738. (atomic_read(&audio_ocmem_lcl.audio_state) &
  739. _WAIT_BFR_DUMP_BIT_MASK_) != 0);
  740. if (test_bit_pos(audio_ocmem_lcl.audio_state,
  741. OCMEM_STATE_MAP_COMPL) ||
  742. test_bit_pos(audio_ocmem_lcl.audio_state,
  743. OCMEM_STATE_UNMAP_FAIL)) {
  744. if (audio_ocmem_lcl.ocmem_dump_addr &&
  745. audio_ocmem_lcl.ocmem_ramdump_dev)
  746. do_ocmem_ramdump();
  747. else
  748. pr_err("%s: Error calling ocmem ramdump\n",
  749. __func__);
  750. ret = ocmem_drop(OCMEM_LP_AUDIO, audio_ocmem_lcl.buf,
  751. &audio_ocmem_lcl.mlist);
  752. if (ret)
  753. pr_err("%s: ocmem_drop failed\n", __func__);
  754. }
  755. }
  756. ret = ocmem_free(OCMEM_LP_AUDIO, audio_ocmem_lcl.buf);
  757. if (ret)
  758. pr_err("%s: ocmem_free failed\n", __func__);
  759. audio_ocmem_lcl.buf = NULL;
  760. }
  761. static int lpass_notifier_cb(struct notifier_block *this, unsigned long code,
  762. void *_cmd)
  763. {
  764. int ret = NOTIFY_DONE;
  765. switch (code) {
  766. case SUBSYS_BEFORE_SHUTDOWN:
  767. pr_debug("AO-Notify: Shutdown started\n");
  768. break;
  769. case SUBSYS_AFTER_SHUTDOWN:
  770. pr_debug("AO-Notify: Shutdown Completed\n");
  771. break;
  772. case SUBSYS_RAMDUMP_NOTIFICATION:
  773. pr_debug("AO-Notify: OCMEM dump\n");
  774. if (audio_ocmem_lcl.ocmem_en &&
  775. audio_ocmem_lcl.audio_ocmem_running)
  776. process_ocmem_dump();
  777. pr_debug("AO-Notify: OCMEM dump done\n");
  778. break;
  779. case SUBSYS_BEFORE_POWERUP:
  780. pr_debug("AO-Notify: Powerup started\n");
  781. break;
  782. case SUBSYS_AFTER_POWERUP:
  783. pr_debug("AO-Notify: Powerup completed\n");
  784. break;
  785. default:
  786. pr_err("AO-Notify: Generel: %lu\n", code);
  787. break;
  788. }
  789. return ret;
  790. }
  791. static struct notifier_block anb = {
  792. .notifier_call = lpass_notifier_cb,
  793. };
  794. static int ocmem_audio_client_probe(struct platform_device *pdev)
  795. {
  796. int ret;
  797. struct msm_bus_scale_pdata *audio_ocmem_bus_scale_pdata = NULL;
  798. pr_debug("%s\n", __func__);
  799. audio_ocmem_lcl.audio_hdl = ocmem_notifier_register(OCMEM_LP_AUDIO,
  800. &audio_ocmem_client_nb);
  801. if (PTR_RET(audio_ocmem_lcl.audio_hdl) == -EPROBE_DEFER)
  802. return -EPROBE_DEFER;
  803. else if (!audio_ocmem_lcl.audio_hdl) {
  804. pr_err("%s: Failed to get ocmem handle %d\n", __func__,
  805. OCMEM_LP_AUDIO);
  806. return -ENODEV;
  807. }
  808. subsys_notif_register_notifier("adsp", &anb);
  809. audio_ocmem_lcl.ocmem_dump_addr =
  810. allocate_contiguous_memory_nomap(AUDIO_OCMEM_BUF_SIZE,
  811. MEMTYPE_EBI1,
  812. AUDIO_OCMEM_BUF_SIZE);
  813. if (audio_ocmem_lcl.ocmem_dump_addr) {
  814. audio_ocmem_lcl.ocmem_ramdump_dev =
  815. create_ramdump_device("audio-ocmem", &pdev->dev);
  816. if (!audio_ocmem_lcl.ocmem_ramdump_dev)
  817. pr_info("%s: audio-ocmem ramdump device failed\n",
  818. __func__);
  819. } else {
  820. pr_err("%s: ocmem dump memory alloc failed\n", __func__);
  821. }
  822. audio_ocmem_lcl.audio_ocmem_workqueue =
  823. alloc_workqueue("ocmem_audio_client_driver_audio",
  824. WQ_NON_REENTRANT | WQ_UNBOUND, 0);
  825. if (!audio_ocmem_lcl.audio_ocmem_workqueue) {
  826. pr_err("%s: Failed to create ocmem audio work queue\n",
  827. __func__);
  828. ret = -ENOMEM;
  829. goto destroy_ramdump;
  830. }
  831. audio_ocmem_lcl.voice_ocmem_workqueue =
  832. alloc_workqueue("ocmem_audio_client_driver_voice",
  833. WQ_NON_REENTRANT, 0);
  834. if (!audio_ocmem_lcl.voice_ocmem_workqueue) {
  835. pr_info("%s: Failed to create ocmem voice work queue\n",
  836. __func__);
  837. ret = -ENOMEM;
  838. goto destroy_audio_wq;
  839. }
  840. init_waitqueue_head(&audio_ocmem_lcl.audio_wait);
  841. atomic_set(&audio_ocmem_lcl.audio_cond, 1);
  842. atomic_set(&audio_ocmem_lcl.audio_state, OCMEM_STATE_DEFAULT);
  843. atomic_set(&audio_ocmem_lcl.audio_exit, 0);
  844. spin_lock_init(&audio_ocmem_lcl.audio_lock);
  845. mutex_init(&audio_ocmem_lcl.state_process_lock);
  846. audio_ocmem_lcl.ocmem_en = true;
  847. audio_ocmem_lcl.audio_ocmem_running = false;
  848. /* populate platform data */
  849. ret = audio_ocmem_platform_data_populate(pdev);
  850. if (ret) {
  851. dev_err(&pdev->dev, "%s: failed to populate platform data, rc = %d\n",
  852. __func__, ret);
  853. goto destroy_voice_wq;
  854. }
  855. audio_ocmem_bus_scale_pdata = dev_get_drvdata(&pdev->dev);
  856. audio_ocmem_lcl.audio_ocmem_bus_client =
  857. msm_bus_scale_register_client(audio_ocmem_bus_scale_pdata);
  858. if (!audio_ocmem_lcl.audio_ocmem_bus_client) {
  859. pr_err("%s: msm_bus_scale_register_client() failed\n",
  860. __func__);
  861. ret = -EFAULT;
  862. goto destroy_voice_wq;
  863. }
  864. audio_ocmem_lcl.lp_memseg_ptr = NULL;
  865. return 0;
  866. destroy_voice_wq:
  867. if (audio_ocmem_lcl.voice_ocmem_workqueue) {
  868. destroy_workqueue(audio_ocmem_lcl.voice_ocmem_workqueue);
  869. audio_ocmem_lcl.voice_ocmem_workqueue = NULL;
  870. }
  871. destroy_audio_wq:
  872. if (audio_ocmem_lcl.audio_ocmem_workqueue) {
  873. destroy_workqueue(audio_ocmem_lcl.audio_ocmem_workqueue);
  874. audio_ocmem_lcl.audio_ocmem_workqueue = NULL;
  875. }
  876. destroy_ramdump:
  877. if (audio_ocmem_lcl.ocmem_ramdump_dev)
  878. destroy_ramdump_device(audio_ocmem_lcl.ocmem_ramdump_dev);
  879. if (audio_ocmem_lcl.ocmem_dump_addr)
  880. free_contiguous_memory_by_paddr(
  881. audio_ocmem_lcl.ocmem_dump_addr);
  882. return ret;
  883. }
  884. static int ocmem_audio_client_remove(struct platform_device *pdev)
  885. {
  886. struct msm_bus_scale_pdata *audio_ocmem_bus_scale_pdata = NULL;
  887. audio_ocmem_bus_scale_pdata = (struct msm_bus_scale_pdata *)
  888. dev_get_drvdata(&pdev->dev);
  889. msm_bus_cl_clear_pdata(audio_ocmem_bus_scale_pdata);
  890. ocmem_notifier_unregister(audio_ocmem_lcl.audio_hdl,
  891. &audio_ocmem_client_nb);
  892. if (audio_ocmem_lcl.ocmem_ramdump_dev)
  893. destroy_ramdump_device(audio_ocmem_lcl.ocmem_ramdump_dev);
  894. if (audio_ocmem_lcl.ocmem_dump_addr)
  895. free_contiguous_memory_by_paddr(
  896. audio_ocmem_lcl.ocmem_dump_addr);
  897. return 0;
  898. }
  899. static const struct of_device_id msm_ocmem_audio_dt_match[] = {
  900. {.compatible = "qcom,msm-ocmem-audio"},
  901. {}
  902. };
  903. MODULE_DEVICE_TABLE(of, msm_ocmem_audio_dt_match);
  904. static struct platform_driver audio_ocmem_driver = {
  905. .driver = {
  906. .name = "audio-ocmem",
  907. .owner = THIS_MODULE,
  908. .of_match_table = msm_ocmem_audio_dt_match,
  909. },
  910. .probe = ocmem_audio_client_probe,
  911. .remove = ocmem_audio_client_remove,
  912. };
  913. static int __init ocmem_audio_client_init(void)
  914. {
  915. int rc;
  916. rc = platform_driver_register(&audio_ocmem_driver);
  917. if (rc)
  918. pr_err("%s: Failed to register audio ocmem driver\n", __func__);
  919. return rc;
  920. }
  921. module_init(ocmem_audio_client_init);
  922. static void __exit ocmem_audio_client_exit(void)
  923. {
  924. platform_driver_unregister(&audio_ocmem_driver);
  925. }
  926. module_exit(ocmem_audio_client_exit);