msm_audio_ion_vm.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2013-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/init.h>
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/err.h>
  9. #include <linux/delay.h>
  10. #include <linux/slab.h>
  11. #include <linux/mutex.h>
  12. #include <linux/list.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/dma-buf.h>
  15. #include <linux/iommu.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/of_device.h>
  18. #include <linux/export.h>
  19. #include <linux/ion_kernel.h>
  20. #include <ipc/apr.h>
  21. #include <asm/dma-iommu.h>
  22. #include <dsp/msm_audio_ion.h>
  23. #include <linux/habmm.h>
  24. #define MSM_AUDIO_ION_PROBED (1 << 0)
  25. #define MSM_AUDIO_ION_PHYS_ADDR(alloc_data) \
  26. alloc_data->table->sgl->dma_address
  27. #define MSM_AUDIO_SMMU_VM_CMD_MAP 0x00000001
  28. #define MSM_AUDIO_SMMU_VM_CMD_UNMAP 0x00000002
  29. #define MSM_AUDIO_SMMU_VM_HAB_MINOR_ID 1
  30. struct msm_audio_ion_private {
  31. bool smmu_enabled;
  32. struct device *cb_dev;
  33. u8 device_status;
  34. struct list_head alloc_list;
  35. struct mutex list_mutex;
  36. };
  37. struct msm_audio_alloc_data {
  38. size_t len;
  39. void *vaddr;
  40. struct dma_buf *dma_buf;
  41. struct dma_buf_attachment *attach;
  42. struct sg_table *table;
  43. struct list_head list;
  44. u32 export_id;
  45. };
  46. struct msm_audio_smmu_vm_map_cmd {
  47. int cmd_id;
  48. u32 export_id;
  49. u32 buf_size;
  50. };
  51. struct msm_audio_smmu_vm_map_cmd_rsp {
  52. int status;
  53. u64 addr;
  54. };
  55. struct msm_audio_smmu_vm_unmap_cmd {
  56. int cmd_id;
  57. u32 export_id;
  58. };
  59. struct msm_audio_smmu_vm_unmap_cmd_rsp {
  60. int status;
  61. };
  62. static struct msm_audio_ion_private msm_audio_ion_data = {0,};
  63. static u32 msm_audio_ion_hab_handle;
  64. static void msm_audio_ion_add_allocation(
  65. struct msm_audio_ion_private *msm_audio_ion_data,
  66. struct msm_audio_alloc_data *alloc_data)
  67. {
  68. /*
  69. * Since these APIs can be invoked by multiple
  70. * clients, there is need to make sure the list
  71. * of allocations is always protected
  72. */
  73. mutex_lock(&(msm_audio_ion_data->list_mutex));
  74. list_add_tail(&(alloc_data->list),
  75. &(msm_audio_ion_data->alloc_list));
  76. mutex_unlock(&(msm_audio_ion_data->list_mutex));
  77. }
  78. static int msm_audio_dma_buf_map(struct dma_buf *dma_buf,
  79. dma_addr_t *addr, size_t *len)
  80. {
  81. struct msm_audio_alloc_data *alloc_data;
  82. struct device *cb_dev;
  83. unsigned long ionflag = 0;
  84. int rc = 0;
  85. cb_dev = msm_audio_ion_data.cb_dev;
  86. /* Data required per buffer mapping */
  87. alloc_data = kzalloc(sizeof(*alloc_data), GFP_KERNEL);
  88. if (!alloc_data)
  89. return -ENOMEM;
  90. alloc_data->dma_buf = dma_buf;
  91. alloc_data->len = dma_buf->size;
  92. *len = dma_buf->size;
  93. /* Attach the dma_buf to context bank device */
  94. alloc_data->attach = dma_buf_attach(alloc_data->dma_buf,
  95. cb_dev);
  96. if (IS_ERR(alloc_data->attach)) {
  97. rc = PTR_ERR(alloc_data->attach);
  98. dev_err(cb_dev,
  99. "%s: Fail to attach dma_buf to CB, rc = %d\n",
  100. __func__, rc);
  101. goto free_alloc_data;
  102. }
  103. /* For uncached buffers, avoid cache maintanance */
  104. rc = dma_buf_get_flags(alloc_data->dma_buf, &ionflag);
  105. if (rc) {
  106. dev_err(cb_dev, "%s: dma_buf_get_flags failed: %d\n",
  107. __func__, rc);
  108. goto detach_dma_buf;
  109. }
  110. if (!(ionflag & ION_FLAG_CACHED))
  111. alloc_data->attach->dma_map_attrs |= DMA_ATTR_SKIP_CPU_SYNC;
  112. /*
  113. * Get the scatter-gather list.
  114. * There is no info as this is a write buffer or
  115. * read buffer, hence the request is bi-directional
  116. * to accommodate both read and write mappings.
  117. */
  118. alloc_data->table = dma_buf_map_attachment(alloc_data->attach,
  119. DMA_BIDIRECTIONAL);
  120. if (IS_ERR(alloc_data->table)) {
  121. rc = PTR_ERR(alloc_data->table);
  122. dev_err(cb_dev,
  123. "%s: Fail to map attachment, rc = %d\n",
  124. __func__, rc);
  125. goto detach_dma_buf;
  126. }
  127. /* physical address from mapping */
  128. *addr = MSM_AUDIO_ION_PHYS_ADDR(alloc_data);
  129. msm_audio_ion_add_allocation(&msm_audio_ion_data,
  130. alloc_data);
  131. return rc;
  132. detach_dma_buf:
  133. dma_buf_detach(alloc_data->dma_buf,
  134. alloc_data->attach);
  135. free_alloc_data:
  136. kfree(alloc_data);
  137. return rc;
  138. }
  139. static int msm_audio_dma_buf_unmap(struct dma_buf *dma_buf)
  140. {
  141. int rc = 0;
  142. struct msm_audio_alloc_data *alloc_data = NULL;
  143. struct list_head *ptr, *next;
  144. struct device *cb_dev = msm_audio_ion_data.cb_dev;
  145. bool found = false;
  146. /*
  147. * Though list_for_each_safe is delete safe, lock
  148. * should be explicitly acquired to avoid race condition
  149. * on adding elements to the list.
  150. */
  151. mutex_lock(&(msm_audio_ion_data.list_mutex));
  152. list_for_each_safe(ptr, next,
  153. &(msm_audio_ion_data.alloc_list)) {
  154. alloc_data = list_entry(ptr, struct msm_audio_alloc_data,
  155. list);
  156. if (alloc_data->dma_buf == dma_buf) {
  157. found = true;
  158. dma_buf_unmap_attachment(alloc_data->attach,
  159. alloc_data->table,
  160. DMA_BIDIRECTIONAL);
  161. dma_buf_detach(alloc_data->dma_buf,
  162. alloc_data->attach);
  163. dma_buf_put(alloc_data->dma_buf);
  164. list_del(&(alloc_data->list));
  165. kfree(alloc_data);
  166. break;
  167. }
  168. }
  169. mutex_unlock(&(msm_audio_ion_data.list_mutex));
  170. if (!found) {
  171. dev_err(cb_dev,
  172. "%s: cannot find allocation, dma_buf %pK",
  173. __func__, dma_buf);
  174. rc = -EINVAL;
  175. }
  176. return rc;
  177. }
  178. static int msm_audio_ion_smmu_map(struct dma_buf *dma_buf,
  179. dma_addr_t *paddr, size_t *len)
  180. {
  181. int rc;
  182. u32 export_id;
  183. u32 cmd_rsp_size;
  184. bool found = false;
  185. bool exported = false;
  186. struct msm_audio_smmu_vm_map_cmd smmu_map_cmd;
  187. struct msm_audio_smmu_vm_map_cmd_rsp cmd_rsp;
  188. struct msm_audio_alloc_data *alloc_data = NULL;
  189. unsigned long delay = jiffies + (HZ / 2);
  190. void *vaddr;
  191. *len = dma_buf->size;
  192. mutex_lock(&(msm_audio_ion_data.list_mutex));
  193. list_for_each_entry(alloc_data, &(msm_audio_ion_data.alloc_list),
  194. list) {
  195. if (alloc_data->dma_buf == dma_buf) {
  196. found = true;
  197. vaddr = alloc_data->vaddr;
  198. /* Export the buffer to physical VM */
  199. rc = habmm_export(msm_audio_ion_hab_handle, vaddr, *len,
  200. &export_id, 0);
  201. if (rc) {
  202. pr_err("%s: habmm_export failed vaddr = %pK, len = %zd, rc = %d\n",
  203. __func__, vaddr, *len, rc);
  204. goto err;
  205. }
  206. exported = true;
  207. smmu_map_cmd.cmd_id = MSM_AUDIO_SMMU_VM_CMD_MAP;
  208. smmu_map_cmd.export_id = export_id;
  209. smmu_map_cmd.buf_size = *len;
  210. rc = habmm_socket_send(msm_audio_ion_hab_handle,
  211. (void *)&smmu_map_cmd, sizeof(smmu_map_cmd), 0);
  212. if (rc) {
  213. pr_err("%s: habmm_socket_send failed %d\n",
  214. __func__, rc);
  215. goto err;
  216. }
  217. do {
  218. cmd_rsp_size = sizeof(cmd_rsp);
  219. rc = habmm_socket_recv(msm_audio_ion_hab_handle,
  220. (void *)&cmd_rsp,
  221. &cmd_rsp_size,
  222. 0xFFFFFFFF,
  223. 0);
  224. } while (time_before(jiffies, delay) && (rc == -EINTR) &&
  225. (cmd_rsp_size == 0));
  226. if (rc) {
  227. pr_err("%s: habmm_socket_recv failed %d\n",
  228. __func__, rc);
  229. goto err;
  230. }
  231. if (cmd_rsp_size != sizeof(cmd_rsp)) {
  232. pr_err("%s: invalid size for cmd rsp %u, expected %zu\n",
  233. __func__, cmd_rsp_size, sizeof(cmd_rsp));
  234. rc = -EIO;
  235. goto err;
  236. }
  237. if (cmd_rsp.status) {
  238. pr_err("%s: SMMU map command failed %d\n",
  239. __func__, cmd_rsp.status);
  240. rc = cmd_rsp.status;
  241. goto err;
  242. }
  243. *paddr = (dma_addr_t)cmd_rsp.addr;
  244. alloc_data->export_id = export_id;
  245. break;
  246. }
  247. }
  248. mutex_unlock(&(msm_audio_ion_data.list_mutex));
  249. if (!found) {
  250. pr_err("%s: cannot find allocation, dma_buf %pK", __func__, dma_buf);
  251. return -EINVAL;
  252. }
  253. return 0;
  254. err:
  255. if (exported)
  256. (void)habmm_unexport(msm_audio_ion_hab_handle, export_id, 0);
  257. mutex_unlock(&(msm_audio_ion_data.list_mutex));
  258. return rc;
  259. }
  260. static int msm_audio_ion_smmu_unmap(struct dma_buf *dma_buf)
  261. {
  262. int rc;
  263. bool found = false;
  264. u32 cmd_rsp_size;
  265. struct msm_audio_smmu_vm_unmap_cmd smmu_unmap_cmd;
  266. struct msm_audio_smmu_vm_unmap_cmd_rsp cmd_rsp;
  267. struct msm_audio_alloc_data *alloc_data, *next;
  268. unsigned long delay = jiffies + (HZ / 2);
  269. /*
  270. * Though list_for_each_entry_safe is delete safe, lock
  271. * should be explicitly acquired to avoid race condition
  272. * on adding elements to the list.
  273. */
  274. mutex_lock(&(msm_audio_ion_data.list_mutex));
  275. list_for_each_entry_safe(alloc_data, next,
  276. &(msm_audio_ion_data.alloc_list), list) {
  277. if (alloc_data->dma_buf == dma_buf) {
  278. found = true;
  279. smmu_unmap_cmd.cmd_id = MSM_AUDIO_SMMU_VM_CMD_UNMAP;
  280. smmu_unmap_cmd.export_id = alloc_data->export_id;
  281. rc = habmm_socket_send(msm_audio_ion_hab_handle,
  282. (void *)&smmu_unmap_cmd,
  283. sizeof(smmu_unmap_cmd), 0);
  284. if (rc) {
  285. pr_err("%s: habmm_socket_send failed %d\n",
  286. __func__, rc);
  287. goto err;
  288. }
  289. do {
  290. cmd_rsp_size = sizeof(cmd_rsp);
  291. rc = habmm_socket_recv(msm_audio_ion_hab_handle,
  292. (void *)&cmd_rsp,
  293. &cmd_rsp_size,
  294. 0xFFFFFFFF,
  295. 0);
  296. } while (time_before(jiffies, delay) &&
  297. (rc == -EINTR) && (cmd_rsp_size == 0));
  298. if (rc) {
  299. pr_err("%s: habmm_socket_recv failed %d\n",
  300. __func__, rc);
  301. goto err;
  302. }
  303. if (cmd_rsp_size != sizeof(cmd_rsp)) {
  304. pr_err("%s: invalid size for cmd rsp %u\n",
  305. __func__, cmd_rsp_size);
  306. rc = -EIO;
  307. goto err;
  308. }
  309. if (cmd_rsp.status) {
  310. pr_err("%s: SMMU unmap command failed %d\n",
  311. __func__, cmd_rsp.status);
  312. rc = cmd_rsp.status;
  313. goto err;
  314. }
  315. rc = habmm_unexport(msm_audio_ion_hab_handle,
  316. alloc_data->export_id, 0xFFFFFFFF);
  317. if (rc) {
  318. pr_err("%s: habmm_unexport failed export_id = %d, rc = %d\n",
  319. __func__, alloc_data->export_id, rc);
  320. }
  321. break;
  322. }
  323. }
  324. mutex_unlock(&(msm_audio_ion_data.list_mutex));
  325. if (!found) {
  326. pr_err("%s: cannot find allocation, dma_buf %pK\n", __func__, dma_buf);
  327. rc = -EINVAL;
  328. }
  329. return rc;
  330. err:
  331. if (found) {
  332. (void)habmm_unexport(msm_audio_ion_hab_handle,
  333. alloc_data->export_id, 0xFFFFFFFF);
  334. list_del(&(alloc_data->list));
  335. kfree(alloc_data);
  336. }
  337. mutex_unlock(&(msm_audio_ion_data.list_mutex));
  338. return rc;
  339. }
  340. static int msm_audio_ion_get_phys(struct dma_buf *dma_buf,
  341. dma_addr_t *addr, size_t *len)
  342. {
  343. int rc = 0;
  344. rc = msm_audio_dma_buf_map(dma_buf, addr, len);
  345. if (rc) {
  346. pr_err("%s: failed to map DMA buf, err = %d\n",
  347. __func__, rc);
  348. goto err;
  349. }
  350. pr_debug("phys=%pK, len=%zd, rc=%d\n", &(*addr), *len, rc);
  351. err:
  352. return rc;
  353. }
  354. static void *msm_audio_ion_map_kernel(struct dma_buf *dma_buf)
  355. {
  356. int rc = 0;
  357. void *addr = NULL;
  358. struct msm_audio_alloc_data *alloc_data = NULL;
  359. rc = dma_buf_begin_cpu_access(dma_buf, DMA_BIDIRECTIONAL);
  360. if (rc) {
  361. pr_err("%s: kmap dma_buf_begin_cpu_access fail\n", __func__);
  362. goto exit;
  363. }
  364. addr = dma_buf_vmap(dma_buf);
  365. if (!addr) {
  366. pr_err("%s: kernel mapping of dma_buf failed\n",
  367. __func__);
  368. goto exit;
  369. }
  370. /*
  371. * TBD: remove the below section once new API
  372. * for mapping kernel virtual address is available.
  373. */
  374. mutex_lock(&(msm_audio_ion_data.list_mutex));
  375. list_for_each_entry(alloc_data, &(msm_audio_ion_data.alloc_list),
  376. list) {
  377. if (alloc_data->dma_buf == dma_buf) {
  378. alloc_data->vaddr = addr;
  379. break;
  380. }
  381. }
  382. mutex_unlock(&(msm_audio_ion_data.list_mutex));
  383. exit:
  384. return addr;
  385. }
  386. static int msm_audio_ion_unmap_kernel(struct dma_buf *dma_buf)
  387. {
  388. int rc = 0;
  389. void *vaddr = NULL;
  390. struct msm_audio_alloc_data *alloc_data = NULL;
  391. struct device *cb_dev = msm_audio_ion_data.cb_dev;
  392. /*
  393. * TBD: remove the below section once new API
  394. * for unmapping kernel virtual address is available.
  395. */
  396. mutex_lock(&(msm_audio_ion_data.list_mutex));
  397. list_for_each_entry(alloc_data, &(msm_audio_ion_data.alloc_list),
  398. list) {
  399. if (alloc_data->dma_buf == dma_buf) {
  400. vaddr = alloc_data->vaddr;
  401. break;
  402. }
  403. }
  404. mutex_unlock(&(msm_audio_ion_data.list_mutex));
  405. if (!vaddr) {
  406. dev_err(cb_dev,
  407. "%s: cannot find allocation for dma_buf %pK",
  408. __func__, dma_buf);
  409. rc = -EINVAL;
  410. goto err;
  411. }
  412. dma_buf_vunmap(dma_buf, vaddr);
  413. rc = dma_buf_end_cpu_access(dma_buf, DMA_BIDIRECTIONAL);
  414. if (rc) {
  415. dev_err(cb_dev, "%s: kmap dma_buf_end_cpu_access fail\n",
  416. __func__);
  417. goto err;
  418. }
  419. err:
  420. return rc;
  421. }
  422. static int msm_audio_ion_map_buf(struct dma_buf *dma_buf, dma_addr_t *paddr,
  423. size_t *plen, void **vaddr)
  424. {
  425. int rc = 0;
  426. rc = msm_audio_ion_get_phys(dma_buf, paddr, plen);
  427. if (rc) {
  428. pr_err("%s: ION Get Physical for AUDIO failed, rc = %d\n",
  429. __func__, rc);
  430. goto err;
  431. }
  432. *vaddr = msm_audio_ion_map_kernel(dma_buf);
  433. if (IS_ERR_OR_NULL(*vaddr)) {
  434. pr_err("%s: ION memory mapping for AUDIO failed\n", __func__);
  435. rc = -ENOMEM;
  436. goto err;
  437. }
  438. if (msm_audio_ion_data.smmu_enabled) {
  439. rc = msm_audio_ion_smmu_map(dma_buf, paddr, plen);
  440. if (rc) {
  441. pr_err("%s: failed to do smmu map, err = %d\n",
  442. __func__, rc);
  443. goto err;
  444. }
  445. }
  446. err:
  447. return rc;
  448. }
  449. /**
  450. * msm_audio_ion_alloc -
  451. * Allocs ION memory for given client name
  452. *
  453. * @dma_buf: dma_buf for the ION memory
  454. * @bufsz: buffer size
  455. * @paddr: Physical address to be assigned with allocated region
  456. * @plen: length of allocated region to be assigned
  457. * vaddr: virtual address to be assigned
  458. *
  459. * Returns 0 on success or error on failure
  460. */
  461. int msm_audio_ion_alloc(struct dma_buf **dma_buf, size_t bufsz,
  462. dma_addr_t *paddr, size_t *plen, void **vaddr)
  463. {
  464. int rc = -EINVAL;
  465. unsigned long err_ion_ptr = 0;
  466. if (!(msm_audio_ion_data.device_status & MSM_AUDIO_ION_PROBED)) {
  467. pr_debug("%s:probe is not done, deferred\n", __func__);
  468. return -EPROBE_DEFER;
  469. }
  470. if (!dma_buf || !paddr || !vaddr || !bufsz || !plen) {
  471. pr_err("%s: Invalid params\n", __func__);
  472. return -EINVAL;
  473. }
  474. if (msm_audio_ion_data.smmu_enabled == true) {
  475. pr_debug("%s: system heap is used\n", __func__);
  476. *dma_buf = ion_alloc(bufsz, ION_HEAP(ION_SYSTEM_HEAP_ID), 0);
  477. } else {
  478. pr_debug("%s: audio heap is used\n", __func__);
  479. *dma_buf = ion_alloc(bufsz, ION_HEAP(ION_AUDIO_HEAP_ID), 0);
  480. }
  481. if (IS_ERR_OR_NULL((void *)(*dma_buf))) {
  482. if (IS_ERR((void *)(*dma_buf)))
  483. err_ion_ptr = PTR_ERR((int *)(*dma_buf));
  484. pr_err("%s: ION alloc fail err ptr=%ld, smmu_enabled=%d\n",
  485. __func__, err_ion_ptr, msm_audio_ion_data.smmu_enabled);
  486. rc = -ENOMEM;
  487. goto err;
  488. }
  489. rc = msm_audio_ion_map_buf(*dma_buf, paddr, plen, vaddr);
  490. if (rc) {
  491. pr_err("%s: failed to map ION buf, rc = %d\n", __func__, rc);
  492. goto err_dma_buf;
  493. }
  494. pr_debug("%s: mapped address = %pK, size=%zd\n", __func__,
  495. *vaddr, bufsz);
  496. memset(*vaddr, 0, bufsz);
  497. return rc;
  498. err_dma_buf:
  499. dma_buf_put(*dma_buf);
  500. err:
  501. return rc;
  502. }
  503. EXPORT_SYMBOL(msm_audio_ion_alloc);
  504. /**
  505. * msm_audio_ion_import-
  506. * Import ION buffer with given file descriptor
  507. *
  508. * @dma_buf: dma_buf for the ION memory
  509. * @fd: file descriptor for the ION memory
  510. * @ionflag: flags associated with ION buffer
  511. * @bufsz: buffer size
  512. * @paddr: Physical address to be assigned with allocated region
  513. * @plen: length of allocated region to be assigned
  514. * vaddr: virtual address to be assigned
  515. *
  516. * Returns 0 on success or error on failure
  517. */
  518. int msm_audio_ion_import(struct dma_buf **dma_buf, int fd,
  519. unsigned long *ionflag, size_t bufsz,
  520. dma_addr_t *paddr, size_t *plen, void **vaddr)
  521. {
  522. int rc = 0;
  523. if (!(msm_audio_ion_data.device_status & MSM_AUDIO_ION_PROBED)) {
  524. pr_debug("%s: probe is not done, deferred\n", __func__);
  525. return -EPROBE_DEFER;
  526. }
  527. if (!dma_buf || !paddr || !vaddr || !plen) {
  528. pr_err("%s: Invalid params\n", __func__);
  529. return -EINVAL;
  530. }
  531. /* bufsz should be 0 and fd shouldn't be 0 as of now */
  532. *dma_buf = dma_buf_get(fd);
  533. pr_debug("%s: dma_buf =%pK, fd=%d\n", __func__, *dma_buf, fd);
  534. if (IS_ERR_OR_NULL((void *)(*dma_buf))) {
  535. pr_err("%s: dma_buf_get failed\n", __func__);
  536. rc = -EINVAL;
  537. goto err;
  538. }
  539. if (ionflag != NULL) {
  540. rc = dma_buf_get_flags(*dma_buf, ionflag);
  541. if (rc) {
  542. pr_err("%s: could not get flags for the dma_buf\n",
  543. __func__);
  544. goto err_ion_flag;
  545. }
  546. }
  547. rc = msm_audio_ion_map_buf(*dma_buf, paddr, plen, vaddr);
  548. if (rc) {
  549. pr_err("%s: failed to map ION buf, rc = %d\n", __func__, rc);
  550. goto err_ion_flag;
  551. }
  552. pr_debug("%s: mapped address = %pK, size=%zd\n", __func__,
  553. *vaddr, bufsz);
  554. return 0;
  555. err_ion_flag:
  556. dma_buf_put(*dma_buf);
  557. err:
  558. *dma_buf = NULL;
  559. return rc;
  560. }
  561. EXPORT_SYMBOL(msm_audio_ion_import);
  562. /**
  563. * msm_audio_ion_free -
  564. * fress ION memory for given client and handle
  565. *
  566. * @dma_buf: dma_buf for the ION memory
  567. *
  568. * Returns 0 on success or error on failure
  569. */
  570. int msm_audio_ion_free(struct dma_buf *dma_buf)
  571. {
  572. int ret = 0;
  573. if (!dma_buf) {
  574. pr_err("%s: dma_buf invalid\n", __func__);
  575. return -EINVAL;
  576. }
  577. ret = msm_audio_ion_unmap_kernel(dma_buf);
  578. if (ret)
  579. return ret;
  580. if (msm_audio_ion_data.smmu_enabled) {
  581. ret = msm_audio_ion_smmu_unmap(dma_buf);
  582. if (ret)
  583. pr_err("%s: smmu unmap failed with ret %d\n",
  584. __func__, ret);
  585. }
  586. msm_audio_dma_buf_unmap(dma_buf);
  587. return 0;
  588. }
  589. EXPORT_SYMBOL(msm_audio_ion_free);
  590. /**
  591. * msm_audio_ion_mmap -
  592. * Audio ION memory map
  593. *
  594. * @abuff: audio buf pointer
  595. * @vma: virtual mem area
  596. *
  597. * Returns 0 on success or error on failure
  598. */
  599. int msm_audio_ion_mmap(struct audio_buffer *abuff,
  600. struct vm_area_struct *vma)
  601. {
  602. struct msm_audio_alloc_data *alloc_data = NULL;
  603. struct sg_table *table;
  604. unsigned long addr = vma->vm_start;
  605. unsigned long offset = vma->vm_pgoff * PAGE_SIZE;
  606. struct scatterlist *sg;
  607. unsigned int i;
  608. struct page *page;
  609. int ret = 0;
  610. bool found = false;
  611. struct device *cb_dev = msm_audio_ion_data.cb_dev;
  612. mutex_lock(&(msm_audio_ion_data.list_mutex));
  613. list_for_each_entry(alloc_data, &(msm_audio_ion_data.alloc_list),
  614. list) {
  615. if (alloc_data->dma_buf == abuff->dma_buf) {
  616. found = true;
  617. table = alloc_data->table;
  618. break;
  619. }
  620. }
  621. mutex_unlock(&(msm_audio_ion_data.list_mutex));
  622. if (!found) {
  623. dev_err(cb_dev,
  624. "%s: cannot find allocation, dma_buf %pK",
  625. __func__, abuff->dma_buf);
  626. return -EINVAL;
  627. }
  628. /* uncached */
  629. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  630. /* We need to check if a page is associated with this sg list because:
  631. * If the allocation came from a carveout we currently don't have
  632. * pages associated with carved out memory. This might change in the
  633. * future and we can remove this check and the else statement.
  634. */
  635. page = sg_page(table->sgl);
  636. if (page) {
  637. pr_debug("%s: page is NOT null\n", __func__);
  638. for_each_sg(table->sgl, sg, table->nents, i) {
  639. unsigned long remainder = vma->vm_end - addr;
  640. unsigned long len = sg->length;
  641. page = sg_page(sg);
  642. if (offset >= len) {
  643. offset -= len;
  644. continue;
  645. } else if (offset) {
  646. page += offset / PAGE_SIZE;
  647. len -= offset;
  648. offset = 0;
  649. }
  650. len = min(len, remainder);
  651. pr_debug("vma=%pK, addr=%x len=%ld vm_start=%x vm_end=%x vm_page_prot=%lu\n",
  652. vma, (unsigned int)addr, len,
  653. (unsigned int)vma->vm_start,
  654. (unsigned int)vma->vm_end,
  655. (unsigned long)pgprot_val(vma->vm_page_prot));
  656. remap_pfn_range(vma, addr, page_to_pfn(page), len,
  657. vma->vm_page_prot);
  658. addr += len;
  659. if (addr >= vma->vm_end)
  660. return 0;
  661. }
  662. } else {
  663. pr_debug("%s: page is NULL\n", __func__);
  664. ret = -EINVAL;
  665. }
  666. return ret;
  667. }
  668. EXPORT_SYMBOL(msm_audio_ion_mmap);
  669. /**
  670. * msm_audio_populate_upper_32_bits -
  671. * retrieve upper 32bits of 64bit address
  672. *
  673. * @pa: 64bit physical address
  674. *
  675. */
  676. u32 msm_audio_populate_upper_32_bits(dma_addr_t pa)
  677. {
  678. return upper_32_bits(pa);
  679. }
  680. EXPORT_SYMBOL(msm_audio_populate_upper_32_bits);
  681. static const struct of_device_id msm_audio_ion_dt_match[] = {
  682. { .compatible = "qcom,msm-audio-ion" },
  683. { }
  684. };
  685. MODULE_DEVICE_TABLE(of, msm_audio_ion_dt_match);
  686. static int msm_audio_ion_probe(struct platform_device *pdev)
  687. {
  688. int rc = 0;
  689. const char *msm_audio_ion_dt = "qcom,smmu-enabled";
  690. bool smmu_enabled;
  691. struct device *dev = &pdev->dev;
  692. if (dev->of_node == NULL) {
  693. dev_err(dev,
  694. "%s: device tree is not found\n",
  695. __func__);
  696. msm_audio_ion_data.smmu_enabled = 0;
  697. return 0;
  698. }
  699. smmu_enabled = of_property_read_bool(dev->of_node,
  700. msm_audio_ion_dt);
  701. msm_audio_ion_data.smmu_enabled = smmu_enabled;
  702. if (!smmu_enabled) {
  703. dev_dbg(dev, "%s: SMMU is Disabled\n", __func__);
  704. goto exit;
  705. }
  706. rc = habmm_socket_open(&msm_audio_ion_hab_handle,
  707. HAB_MMID_CREATE(MM_AUD_3,
  708. MSM_AUDIO_SMMU_VM_HAB_MINOR_ID),
  709. 0xFFFFFFFF,
  710. HABMM_SOCKET_OPEN_FLAGS_SINGLE_BE_SINGLE_FE);
  711. if (rc) {
  712. dev_err(dev, "%s: habmm_socket_open failed %d\n",
  713. __func__, rc);
  714. return rc;
  715. }
  716. dev_info(dev, "%s: msm_audio_ion_hab_handle %x\n",
  717. __func__, msm_audio_ion_hab_handle);
  718. INIT_LIST_HEAD(&msm_audio_ion_data.alloc_list);
  719. mutex_init(&(msm_audio_ion_data.list_mutex));
  720. exit:
  721. if (!rc)
  722. msm_audio_ion_data.device_status |= MSM_AUDIO_ION_PROBED;
  723. msm_audio_ion_data.cb_dev = dev;
  724. return rc;
  725. }
  726. static int msm_audio_ion_remove(struct platform_device *pdev)
  727. {
  728. if (msm_audio_ion_data.smmu_enabled) {
  729. if (msm_audio_ion_hab_handle)
  730. habmm_socket_close(msm_audio_ion_hab_handle);
  731. mutex_destroy(&(msm_audio_ion_data.list_mutex));
  732. }
  733. msm_audio_ion_data.smmu_enabled = 0;
  734. msm_audio_ion_data.device_status = 0;
  735. return 0;
  736. }
  737. static struct platform_driver msm_audio_ion_driver = {
  738. .driver = {
  739. .name = "msm-audio-ion",
  740. .owner = THIS_MODULE,
  741. .of_match_table = msm_audio_ion_dt_match,
  742. },
  743. .probe = msm_audio_ion_probe,
  744. .remove = msm_audio_ion_remove,
  745. };
  746. int __init msm_audio_ion_init(void)
  747. {
  748. return platform_driver_register(&msm_audio_ion_driver);
  749. }
  750. void msm_audio_ion_exit(void)
  751. {
  752. platform_driver_unregister(&msm_audio_ion_driver);
  753. }
  754. MODULE_DESCRIPTION("MSM Audio ION VM module");
  755. MODULE_LICENSE("GPL v2");