msm_audio_ion_vm.c 21 KB

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