msm_audio_ion.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /*
  2. * Copyright (c) 2013-2017, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/err.h>
  17. #include <linux/delay.h>
  18. #include <linux/slab.h>
  19. #include <linux/mutex.h>
  20. #include <linux/list.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/dma-buf.h>
  23. #include <linux/iommu.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/qdsp6v2/apr.h>
  26. #include <linux/of_device.h>
  27. #include <linux/msm_audio_ion.h>
  28. #include <linux/export.h>
  29. #include <asm/dma-iommu.h>
  30. #define MSM_AUDIO_ION_PROBED (1 << 0)
  31. #define MSM_AUDIO_ION_PHYS_ADDR(alloc_data) \
  32. alloc_data->table->sgl->dma_address
  33. #define MSM_AUDIO_ION_VA_START 0x10000000
  34. #define MSM_AUDIO_ION_VA_LEN 0x0FFFFFFF
  35. #define MSM_AUDIO_SMMU_SID_OFFSET 32
  36. struct msm_audio_ion_private {
  37. bool smmu_enabled;
  38. bool audioheap_enabled;
  39. struct device *cb_dev;
  40. struct dma_iommu_mapping *mapping;
  41. u8 device_status;
  42. struct list_head alloc_list;
  43. struct mutex list_mutex;
  44. u64 smmu_sid_bits;
  45. u32 smmu_version;
  46. };
  47. struct msm_audio_alloc_data {
  48. struct ion_client *client;
  49. struct ion_handle *handle;
  50. size_t len;
  51. struct dma_buf *dma_buf;
  52. struct dma_buf_attachment *attach;
  53. struct sg_table *table;
  54. struct list_head list;
  55. };
  56. static struct msm_audio_ion_private msm_audio_ion_data = {0,};
  57. static int msm_audio_ion_get_phys(struct ion_client *client,
  58. struct ion_handle *handle,
  59. ion_phys_addr_t *addr, size_t *len);
  60. static int msm_audio_dma_buf_map(struct ion_client *client,
  61. struct ion_handle *handle,
  62. ion_phys_addr_t *addr, size_t *len);
  63. static int msm_audio_dma_buf_unmap(struct ion_client *client,
  64. struct ion_handle *handle);
  65. static void msm_audio_ion_add_allocation(
  66. struct msm_audio_ion_private *msm_audio_ion_data,
  67. struct msm_audio_alloc_data *alloc_data)
  68. {
  69. /*
  70. * Since these APIs can be invoked by multiple
  71. * clients, there is need to make sure the list
  72. * of allocations is always protected
  73. */
  74. mutex_lock(&(msm_audio_ion_data->list_mutex));
  75. list_add_tail(&(alloc_data->list),
  76. &(msm_audio_ion_data->alloc_list));
  77. mutex_unlock(&(msm_audio_ion_data->list_mutex));
  78. }
  79. int msm_audio_ion_alloc(const char *name, struct ion_client **client,
  80. struct ion_handle **handle, size_t bufsz,
  81. ion_phys_addr_t *paddr, size_t *pa_len, void **vaddr)
  82. {
  83. int rc = -EINVAL;
  84. unsigned long err_ion_ptr = 0;
  85. if ((msm_audio_ion_data.smmu_enabled == true) &&
  86. !(msm_audio_ion_data.device_status & MSM_AUDIO_ION_PROBED)) {
  87. pr_debug("%s:probe is not done, deferred\n", __func__);
  88. return -EPROBE_DEFER;
  89. }
  90. if (!name || !client || !handle || !paddr || !vaddr
  91. || !bufsz || !pa_len) {
  92. pr_err("%s: Invalid params\n", __func__);
  93. return -EINVAL;
  94. }
  95. *client = msm_audio_ion_client_create(name);
  96. if (IS_ERR_OR_NULL((void *)(*client))) {
  97. pr_err("%s: ION create client for AUDIO failed\n", __func__);
  98. goto err;
  99. }
  100. *handle = ion_alloc(*client, bufsz, SZ_4K,
  101. ION_HEAP(ION_AUDIO_HEAP_ID), 0);
  102. if (IS_ERR_OR_NULL((void *) (*handle))) {
  103. if (msm_audio_ion_data.smmu_enabled == true) {
  104. pr_debug("system heap is used");
  105. msm_audio_ion_data.audioheap_enabled = 0;
  106. *handle = ion_alloc(*client, bufsz, SZ_4K,
  107. ION_HEAP(ION_SYSTEM_HEAP_ID), 0);
  108. }
  109. if (IS_ERR_OR_NULL((void *) (*handle))) {
  110. if (IS_ERR((void *)(*handle)))
  111. err_ion_ptr = PTR_ERR((int *)(*handle));
  112. pr_err("%s:ION alloc fail err ptr=%ld, smmu_enabled=%d\n",
  113. __func__, err_ion_ptr, msm_audio_ion_data.smmu_enabled);
  114. rc = -ENOMEM;
  115. goto err_ion_client;
  116. }
  117. } else {
  118. pr_debug("audio heap is used");
  119. msm_audio_ion_data.audioheap_enabled = 1;
  120. }
  121. rc = msm_audio_ion_get_phys(*client, *handle, paddr, pa_len);
  122. if (rc) {
  123. pr_err("%s: ION Get Physical for AUDIO failed, rc = %d\n",
  124. __func__, rc);
  125. goto err_ion_handle;
  126. }
  127. *vaddr = ion_map_kernel(*client, *handle);
  128. if (IS_ERR_OR_NULL((void *)*vaddr)) {
  129. pr_err("%s: ION memory mapping for AUDIO failed\n", __func__);
  130. goto err_ion_handle;
  131. }
  132. pr_debug("%s: mapped address = %pK, size=%zd\n", __func__,
  133. *vaddr, bufsz);
  134. if (bufsz != 0) {
  135. pr_debug("%s: memset to 0 %pK %zd\n", __func__, *vaddr, bufsz);
  136. memset((void *)*vaddr, 0, bufsz);
  137. }
  138. return rc;
  139. err_ion_handle:
  140. ion_free(*client, *handle);
  141. err_ion_client:
  142. msm_audio_ion_client_destroy(*client);
  143. *handle = NULL;
  144. *client = NULL;
  145. err:
  146. return rc;
  147. }
  148. EXPORT_SYMBOL(msm_audio_ion_alloc);
  149. int msm_audio_ion_import(const char *name, struct ion_client **client,
  150. struct ion_handle **handle, int fd,
  151. unsigned long *ionflag, size_t bufsz,
  152. ion_phys_addr_t *paddr, size_t *pa_len, void **vaddr)
  153. {
  154. int rc = 0;
  155. if ((msm_audio_ion_data.smmu_enabled == true) &&
  156. !(msm_audio_ion_data.device_status & MSM_AUDIO_ION_PROBED)) {
  157. pr_debug("%s:probe is not done, deferred\n", __func__);
  158. return -EPROBE_DEFER;
  159. }
  160. if (!name || !client || !handle || !paddr || !vaddr || !pa_len) {
  161. pr_err("%s: Invalid params\n", __func__);
  162. rc = -EINVAL;
  163. goto err;
  164. }
  165. *client = msm_audio_ion_client_create(name);
  166. if (IS_ERR_OR_NULL((void *)(*client))) {
  167. pr_err("%s: ION create client for AUDIO failed\n", __func__);
  168. rc = -EINVAL;
  169. goto err;
  170. }
  171. /* name should be audio_acdb_client or Audio_Dec_Client,
  172. * bufsz should be 0 and fd shouldn't be 0 as of now
  173. */
  174. *handle = ion_import_dma_buf_fd(*client, fd);
  175. pr_debug("%s: DMA Buf name=%s, fd=%d handle=%pK\n", __func__,
  176. name, fd, *handle);
  177. if (IS_ERR_OR_NULL((void *) (*handle))) {
  178. pr_err("%s: ion import dma buffer failed\n",
  179. __func__);
  180. rc = -EINVAL;
  181. goto err_destroy_client;
  182. }
  183. if (ionflag != NULL) {
  184. rc = ion_handle_get_flags(*client, *handle, ionflag);
  185. if (rc) {
  186. pr_err("%s: could not get flags for the handle\n",
  187. __func__);
  188. goto err_ion_handle;
  189. }
  190. }
  191. rc = msm_audio_ion_get_phys(*client, *handle, paddr, pa_len);
  192. if (rc) {
  193. pr_err("%s: ION Get Physical for AUDIO failed, rc = %d\n",
  194. __func__, rc);
  195. goto err_ion_handle;
  196. }
  197. *vaddr = ion_map_kernel(*client, *handle);
  198. if (IS_ERR_OR_NULL((void *)*vaddr)) {
  199. pr_err("%s: ION memory mapping for AUDIO failed\n", __func__);
  200. rc = -ENOMEM;
  201. goto err_ion_handle;
  202. }
  203. pr_debug("%s: mapped address = %pK, size=%zd\n", __func__,
  204. *vaddr, bufsz);
  205. return 0;
  206. err_ion_handle:
  207. ion_free(*client, *handle);
  208. err_destroy_client:
  209. msm_audio_ion_client_destroy(*client);
  210. *client = NULL;
  211. *handle = NULL;
  212. err:
  213. return rc;
  214. }
  215. int msm_audio_ion_free(struct ion_client *client, struct ion_handle *handle)
  216. {
  217. if (!client || !handle) {
  218. pr_err("%s Invalid params\n", __func__);
  219. return -EINVAL;
  220. }
  221. if (msm_audio_ion_data.smmu_enabled)
  222. msm_audio_dma_buf_unmap(client, handle);
  223. ion_unmap_kernel(client, handle);
  224. ion_free(client, handle);
  225. msm_audio_ion_client_destroy(client);
  226. return 0;
  227. }
  228. EXPORT_SYMBOL(msm_audio_ion_free);
  229. int msm_audio_ion_mmap(struct audio_buffer *ab,
  230. struct vm_area_struct *vma)
  231. {
  232. struct sg_table *table;
  233. unsigned long addr = vma->vm_start;
  234. unsigned long offset = vma->vm_pgoff * PAGE_SIZE;
  235. struct scatterlist *sg;
  236. unsigned int i;
  237. struct page *page;
  238. int ret;
  239. pr_debug("%s\n", __func__);
  240. table = ion_sg_table(ab->client, ab->handle);
  241. if (IS_ERR(table)) {
  242. pr_err("%s: Unable to get sg_table from ion: %ld\n",
  243. __func__, PTR_ERR(table));
  244. return PTR_ERR(table);
  245. } else if (!table) {
  246. pr_err("%s: sg_list is NULL\n", __func__);
  247. return -EINVAL;
  248. }
  249. /* uncached */
  250. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  251. /* We need to check if a page is associated with this sg list because:
  252. * If the allocation came from a carveout we currently don't have
  253. * pages associated with carved out memory. This might change in the
  254. * future and we can remove this check and the else statement.
  255. */
  256. page = sg_page(table->sgl);
  257. if (page) {
  258. pr_debug("%s: page is NOT null\n", __func__);
  259. for_each_sg(table->sgl, sg, table->nents, i) {
  260. unsigned long remainder = vma->vm_end - addr;
  261. unsigned long len = sg->length;
  262. page = sg_page(sg);
  263. if (offset >= len) {
  264. offset -= len;
  265. continue;
  266. } else if (offset) {
  267. page += offset / PAGE_SIZE;
  268. len -= offset;
  269. offset = 0;
  270. }
  271. len = min(len, remainder);
  272. pr_debug("vma=%pK, addr=%x len=%ld vm_start=%x vm_end=%x vm_page_prot=%lu\n",
  273. vma, (unsigned int)addr, len,
  274. (unsigned int)vma->vm_start,
  275. (unsigned int)vma->vm_end,
  276. (unsigned long)vma->vm_page_prot.pgprot);
  277. remap_pfn_range(vma, addr, page_to_pfn(page), len,
  278. vma->vm_page_prot);
  279. addr += len;
  280. if (addr >= vma->vm_end)
  281. return 0;
  282. }
  283. } else {
  284. ion_phys_addr_t phys_addr;
  285. size_t phys_len;
  286. size_t va_len = 0;
  287. pr_debug("%s: page is NULL\n", __func__);
  288. ret = ion_phys(ab->client, ab->handle, &phys_addr, &phys_len);
  289. if (ret) {
  290. pr_err("%s: Unable to get phys address from ION buffer: %d\n"
  291. , __func__, ret);
  292. return ret;
  293. }
  294. pr_debug("phys=%pKK len=%zd\n", &phys_addr, phys_len);
  295. pr_debug("vma=%pK, vm_start=%x vm_end=%x vm_pgoff=%ld vm_page_prot=%lu\n",
  296. vma, (unsigned int)vma->vm_start,
  297. (unsigned int)vma->vm_end, vma->vm_pgoff,
  298. (unsigned long)vma->vm_page_prot.pgprot);
  299. va_len = vma->vm_end - vma->vm_start;
  300. if ((offset > phys_len) || (va_len > phys_len-offset)) {
  301. pr_err("wrong offset size %ld, lens= %zd, va_len=%zd\n",
  302. offset, phys_len, va_len);
  303. return -EINVAL;
  304. }
  305. ret = remap_pfn_range(vma, vma->vm_start,
  306. __phys_to_pfn(phys_addr) + vma->vm_pgoff,
  307. vma->vm_end - vma->vm_start,
  308. vma->vm_page_prot);
  309. }
  310. return 0;
  311. }
  312. bool msm_audio_ion_is_smmu_available(void)
  313. {
  314. return msm_audio_ion_data.smmu_enabled;
  315. }
  316. /* move to static section again */
  317. struct ion_client *msm_audio_ion_client_create(const char *name)
  318. {
  319. struct ion_client *pclient = NULL;
  320. pclient = msm_ion_client_create(name);
  321. return pclient;
  322. }
  323. void msm_audio_ion_client_destroy(struct ion_client *client)
  324. {
  325. pr_debug("%s: client = %pK smmu_enabled = %d\n", __func__,
  326. client, msm_audio_ion_data.smmu_enabled);
  327. ion_client_destroy(client);
  328. }
  329. int msm_audio_ion_import_legacy(const char *name, struct ion_client *client,
  330. struct ion_handle **handle, int fd,
  331. unsigned long *ionflag, size_t bufsz,
  332. ion_phys_addr_t *paddr, size_t *pa_len, void **vaddr)
  333. {
  334. int rc = 0;
  335. if (!name || !client || !handle || !paddr || !vaddr || !pa_len) {
  336. pr_err("%s: Invalid params\n", __func__);
  337. rc = -EINVAL;
  338. goto err;
  339. }
  340. /* client is already created for legacy and given
  341. * name should be audio_acdb_client or Audio_Dec_Client,
  342. * bufsz should be 0 and fd shouldn't be 0 as of now
  343. */
  344. *handle = ion_import_dma_buf_fd(client, fd);
  345. pr_debug("%s: DMA Buf name=%s, fd=%d handle=%pK\n", __func__,
  346. name, fd, *handle);
  347. if (IS_ERR_OR_NULL((void *)(*handle))) {
  348. pr_err("%s: ion import dma buffer failed\n",
  349. __func__);
  350. rc = -EINVAL;
  351. goto err;
  352. }
  353. if (ionflag != NULL) {
  354. rc = ion_handle_get_flags(client, *handle, ionflag);
  355. if (rc) {
  356. pr_err("%s: could not get flags for the handle\n",
  357. __func__);
  358. rc = -EINVAL;
  359. goto err_ion_handle;
  360. }
  361. }
  362. rc = msm_audio_ion_get_phys(client, *handle, paddr, pa_len);
  363. if (rc) {
  364. pr_err("%s: ION Get Physical for AUDIO failed, rc = %d\n",
  365. __func__, rc);
  366. rc = -EINVAL;
  367. goto err_ion_handle;
  368. }
  369. /*Need to add condition SMMU enable or not */
  370. *vaddr = ion_map_kernel(client, *handle);
  371. if (IS_ERR_OR_NULL((void *)*vaddr)) {
  372. pr_err("%s: ION memory mapping for AUDIO failed\n", __func__);
  373. rc = -EINVAL;
  374. goto err_ion_handle;
  375. }
  376. if (bufsz != 0)
  377. memset((void *)*vaddr, 0, bufsz);
  378. return 0;
  379. err_ion_handle:
  380. ion_free(client, *handle);
  381. err:
  382. return rc;
  383. }
  384. int msm_audio_ion_free_legacy(struct ion_client *client,
  385. struct ion_handle *handle)
  386. {
  387. if (msm_audio_ion_data.smmu_enabled)
  388. msm_audio_dma_buf_unmap(client, handle);
  389. ion_unmap_kernel(client, handle);
  390. ion_free(client, handle);
  391. /* no client_destrody in legacy*/
  392. return 0;
  393. }
  394. int msm_audio_ion_cache_operations(struct audio_buffer *abuff, int cache_op)
  395. {
  396. unsigned long ionflag = 0;
  397. int rc = 0;
  398. int msm_cache_ops = 0;
  399. if (!abuff) {
  400. pr_err("%s: Invalid params: %pK\n", __func__, abuff);
  401. return -EINVAL;
  402. }
  403. rc = ion_handle_get_flags(abuff->client, abuff->handle,
  404. &ionflag);
  405. if (rc) {
  406. pr_err("ion_handle_get_flags failed: %d\n", rc);
  407. goto cache_op_failed;
  408. }
  409. /* has to be CACHED */
  410. if (ION_IS_CACHED(ionflag)) {
  411. /* ION_IOC_INV_CACHES or ION_IOC_CLEAN_CACHES */
  412. msm_cache_ops = cache_op;
  413. rc = msm_ion_do_cache_op(abuff->client,
  414. abuff->handle,
  415. (unsigned long *) abuff->data,
  416. (unsigned long)abuff->size,
  417. msm_cache_ops);
  418. if (rc) {
  419. pr_err("cache operation failed %d\n", rc);
  420. goto cache_op_failed;
  421. }
  422. }
  423. cache_op_failed:
  424. return rc;
  425. }
  426. static int msm_audio_dma_buf_map(struct ion_client *client,
  427. struct ion_handle *handle,
  428. ion_phys_addr_t *addr, size_t *len)
  429. {
  430. struct msm_audio_alloc_data *alloc_data;
  431. struct device *cb_dev;
  432. int rc = 0;
  433. cb_dev = msm_audio_ion_data.cb_dev;
  434. /* Data required per buffer mapping */
  435. alloc_data = kzalloc(sizeof(*alloc_data), GFP_KERNEL);
  436. if (!alloc_data)
  437. return -ENOMEM;
  438. /* Get the ION handle size */
  439. ion_handle_get_size(client, handle, len);
  440. alloc_data->client = client;
  441. alloc_data->handle = handle;
  442. alloc_data->len = *len;
  443. /* Get the dma_buf handle from ion_handle */
  444. alloc_data->dma_buf = ion_share_dma_buf(client, handle);
  445. if (IS_ERR(alloc_data->dma_buf)) {
  446. rc = PTR_ERR(alloc_data->dma_buf);
  447. dev_err(cb_dev,
  448. "%s: Fail to get dma_buf handle, rc = %d\n",
  449. __func__, rc);
  450. goto err_dma_buf;
  451. }
  452. /* Attach the dma_buf to context bank device */
  453. alloc_data->attach = dma_buf_attach(alloc_data->dma_buf,
  454. cb_dev);
  455. if (IS_ERR(alloc_data->attach)) {
  456. rc = PTR_ERR(alloc_data->attach);
  457. dev_err(cb_dev,
  458. "%s: Fail to attach dma_buf to CB, rc = %d\n",
  459. __func__, rc);
  460. goto err_attach;
  461. }
  462. /*
  463. * Get the scatter-gather list.
  464. * There is no info as this is a write buffer or
  465. * read buffer, hence the request is bi-directional
  466. * to accommodate both read and write mappings.
  467. */
  468. alloc_data->table = dma_buf_map_attachment(alloc_data->attach,
  469. DMA_BIDIRECTIONAL);
  470. if (IS_ERR(alloc_data->table)) {
  471. rc = PTR_ERR(alloc_data->table);
  472. dev_err(cb_dev,
  473. "%s: Fail to map attachment, rc = %d\n",
  474. __func__, rc);
  475. goto err_map_attach;
  476. }
  477. rc = dma_map_sg(cb_dev, alloc_data->table->sgl,
  478. alloc_data->table->nents,
  479. DMA_BIDIRECTIONAL);
  480. if (rc != alloc_data->table->nents) {
  481. dev_err(cb_dev,
  482. "%s: Fail to map SG, rc = %d, nents = %d\n",
  483. __func__, rc, alloc_data->table->nents);
  484. goto err_map_sg;
  485. }
  486. /* Make sure not to return rc from dma_map_sg, as it can be nonzero */
  487. rc = 0;
  488. /* physical address from mapping */
  489. *addr = MSM_AUDIO_ION_PHYS_ADDR(alloc_data);
  490. msm_audio_ion_add_allocation(&msm_audio_ion_data,
  491. alloc_data);
  492. return rc;
  493. err_map_sg:
  494. dma_buf_unmap_attachment(alloc_data->attach,
  495. alloc_data->table,
  496. DMA_BIDIRECTIONAL);
  497. err_map_attach:
  498. dma_buf_detach(alloc_data->dma_buf,
  499. alloc_data->attach);
  500. err_attach:
  501. dma_buf_put(alloc_data->dma_buf);
  502. err_dma_buf:
  503. kfree(alloc_data);
  504. return rc;
  505. }
  506. static int msm_audio_dma_buf_unmap(struct ion_client *client,
  507. struct ion_handle *handle)
  508. {
  509. int rc = 0;
  510. struct msm_audio_alloc_data *alloc_data = NULL;
  511. struct list_head *ptr, *next;
  512. struct device *cb_dev = msm_audio_ion_data.cb_dev;
  513. bool found = false;
  514. /*
  515. * Though list_for_each_safe is delete safe, lock
  516. * should be explicitly acquired to avoid race condition
  517. * on adding elements to the list.
  518. */
  519. mutex_lock(&(msm_audio_ion_data.list_mutex));
  520. list_for_each_safe(ptr, next,
  521. &(msm_audio_ion_data.alloc_list)) {
  522. alloc_data = list_entry(ptr, struct msm_audio_alloc_data,
  523. list);
  524. if (alloc_data->handle == handle &&
  525. alloc_data->client == client) {
  526. found = true;
  527. dma_unmap_sg(cb_dev,
  528. alloc_data->table->sgl,
  529. alloc_data->table->nents,
  530. DMA_BIDIRECTIONAL);
  531. dma_buf_unmap_attachment(alloc_data->attach,
  532. alloc_data->table,
  533. DMA_BIDIRECTIONAL);
  534. dma_buf_detach(alloc_data->dma_buf,
  535. alloc_data->attach);
  536. dma_buf_put(alloc_data->dma_buf);
  537. list_del(&(alloc_data->list));
  538. kfree(alloc_data);
  539. break;
  540. }
  541. }
  542. mutex_unlock(&(msm_audio_ion_data.list_mutex));
  543. if (!found) {
  544. dev_err(cb_dev,
  545. "%s: cannot find allocation, ion_handle %pK, ion_client %pK",
  546. __func__, handle, client);
  547. rc = -EINVAL;
  548. }
  549. return rc;
  550. }
  551. static int msm_audio_ion_get_phys(struct ion_client *client,
  552. struct ion_handle *handle,
  553. ion_phys_addr_t *addr, size_t *len)
  554. {
  555. int rc = 0;
  556. pr_debug("%s: smmu_enabled = %d\n", __func__,
  557. msm_audio_ion_data.smmu_enabled);
  558. if (msm_audio_ion_data.smmu_enabled) {
  559. rc = msm_audio_dma_buf_map(client, handle, addr, len);
  560. if (rc) {
  561. pr_err("%s: failed to map DMA buf, err = %d\n",
  562. __func__, rc);
  563. goto err;
  564. }
  565. /* Append the SMMU SID information to the IOVA address */
  566. *addr |= msm_audio_ion_data.smmu_sid_bits;
  567. } else {
  568. rc = ion_phys(client, handle, addr, len);
  569. }
  570. pr_debug("phys=%pK, len=%zd, rc=%d\n", &(*addr), *len, rc);
  571. err:
  572. return rc;
  573. }
  574. static int msm_audio_smmu_init(struct device *dev)
  575. {
  576. struct dma_iommu_mapping *mapping;
  577. int ret;
  578. mapping = arm_iommu_create_mapping(&platform_bus_type,
  579. MSM_AUDIO_ION_VA_START,
  580. MSM_AUDIO_ION_VA_LEN);
  581. if (IS_ERR(mapping))
  582. return PTR_ERR(mapping);
  583. ret = arm_iommu_attach_device(dev, mapping);
  584. if (ret) {
  585. dev_err(dev, "%s: Attach failed, err = %d\n",
  586. __func__, ret);
  587. goto fail_attach;
  588. }
  589. msm_audio_ion_data.cb_dev = dev;
  590. msm_audio_ion_data.mapping = mapping;
  591. INIT_LIST_HEAD(&msm_audio_ion_data.alloc_list);
  592. mutex_init(&(msm_audio_ion_data.list_mutex));
  593. return 0;
  594. fail_attach:
  595. arm_iommu_release_mapping(mapping);
  596. return ret;
  597. }
  598. static const struct of_device_id msm_audio_ion_dt_match[] = {
  599. { .compatible = "qcom,msm-audio-ion" },
  600. { }
  601. };
  602. MODULE_DEVICE_TABLE(of, msm_audio_ion_dt_match);
  603. u32 msm_audio_ion_get_smmu_sid_mode32(void)
  604. {
  605. if (msm_audio_ion_data.smmu_enabled)
  606. return upper_32_bits(msm_audio_ion_data.smmu_sid_bits);
  607. else
  608. return 0;
  609. }
  610. u32 msm_audio_populate_upper_32_bits(ion_phys_addr_t pa)
  611. {
  612. if (sizeof(ion_phys_addr_t) == sizeof(u32))
  613. return msm_audio_ion_get_smmu_sid_mode32();
  614. else
  615. return upper_32_bits(pa);
  616. }
  617. static int msm_audio_ion_probe(struct platform_device *pdev)
  618. {
  619. int rc = 0;
  620. const char *msm_audio_ion_dt = "qcom,smmu-enabled";
  621. const char *msm_audio_ion_smmu = "qcom,smmu-version";
  622. const char *msm_audio_ion_smmu_sid_mask = "qcom,smmu-sid-mask";
  623. bool smmu_enabled;
  624. enum apr_subsys_state q6_state;
  625. struct device *dev = &pdev->dev;
  626. if (dev->of_node == NULL) {
  627. dev_err(dev,
  628. "%s: device tree is not found\n",
  629. __func__);
  630. msm_audio_ion_data.smmu_enabled = 0;
  631. return 0;
  632. }
  633. smmu_enabled = of_property_read_bool(dev->of_node,
  634. msm_audio_ion_dt);
  635. msm_audio_ion_data.smmu_enabled = smmu_enabled;
  636. if (smmu_enabled) {
  637. rc = of_property_read_u32(dev->of_node,
  638. msm_audio_ion_smmu,
  639. &msm_audio_ion_data.smmu_version);
  640. if (rc) {
  641. dev_err(dev,
  642. "%s: qcom,smmu_version missing in DT node\n",
  643. __func__);
  644. return rc;
  645. }
  646. dev_dbg(dev, "%s: SMMU version is (%d)", __func__,
  647. msm_audio_ion_data.smmu_version);
  648. q6_state = apr_get_q6_state();
  649. if (q6_state == APR_SUBSYS_DOWN) {
  650. dev_dbg(dev,
  651. "defering %s, adsp_state %d\n",
  652. __func__, q6_state);
  653. return -EPROBE_DEFER;
  654. }
  655. dev_dbg(dev, "%s: adsp is ready\n", __func__);
  656. }
  657. dev_dbg(dev, "%s: SMMU is %s\n", __func__,
  658. (smmu_enabled) ? "Enabled" : "Disabled");
  659. if (smmu_enabled) {
  660. u64 smmu_sid = 0;
  661. u64 smmu_sid_mask = 0;
  662. struct of_phandle_args iommuspec;
  663. /* Get SMMU SID information from Devicetree */
  664. rc = of_property_read_u64(dev->of_node,
  665. msm_audio_ion_smmu_sid_mask,
  666. &smmu_sid_mask);
  667. if (rc) {
  668. dev_err(dev,
  669. "%s: qcom,smmu-sid-mask missing in DT node, using default\n",
  670. __func__);
  671. smmu_sid_mask = 0xFFFFFFFFFFFFFFFF;
  672. }
  673. rc = of_parse_phandle_with_args(dev->of_node, "iommus",
  674. "#iommu-cells", 0, &iommuspec);
  675. if (rc)
  676. dev_err(dev, "%s: could not get smmu SID, ret = %d\n",
  677. __func__, rc);
  678. else
  679. smmu_sid = (iommuspec.args[0] & smmu_sid_mask);
  680. msm_audio_ion_data.smmu_sid_bits =
  681. smmu_sid << MSM_AUDIO_SMMU_SID_OFFSET;
  682. if (msm_audio_ion_data.smmu_version == 0x2) {
  683. rc = msm_audio_smmu_init(dev);
  684. } else {
  685. dev_err(dev, "%s: smmu version invalid %d\n",
  686. __func__, msm_audio_ion_data.smmu_version);
  687. rc = -EINVAL;
  688. }
  689. if (rc)
  690. dev_err(dev, "%s: smmu init failed, err = %d\n",
  691. __func__, rc);
  692. }
  693. if (!rc)
  694. msm_audio_ion_data.device_status |= MSM_AUDIO_ION_PROBED;
  695. return rc;
  696. }
  697. static int msm_audio_ion_remove(struct platform_device *pdev)
  698. {
  699. struct dma_iommu_mapping *mapping;
  700. struct device *audio_cb_dev;
  701. mapping = msm_audio_ion_data.mapping;
  702. audio_cb_dev = msm_audio_ion_data.cb_dev;
  703. if (audio_cb_dev && mapping) {
  704. arm_iommu_detach_device(audio_cb_dev);
  705. arm_iommu_release_mapping(mapping);
  706. }
  707. msm_audio_ion_data.smmu_enabled = 0;
  708. msm_audio_ion_data.device_status = 0;
  709. return 0;
  710. }
  711. static struct platform_driver msm_audio_ion_driver = {
  712. .driver = {
  713. .name = "msm-audio-ion",
  714. .owner = THIS_MODULE,
  715. .of_match_table = msm_audio_ion_dt_match,
  716. },
  717. .probe = msm_audio_ion_probe,
  718. .remove = msm_audio_ion_remove,
  719. };
  720. static int __init msm_audio_ion_init(void)
  721. {
  722. return platform_driver_register(&msm_audio_ion_driver);
  723. }
  724. module_init(msm_audio_ion_init);
  725. static void __exit msm_audio_ion_exit(void)
  726. {
  727. platform_driver_unregister(&msm_audio_ion_driver);
  728. }
  729. module_exit(msm_audio_ion_exit);
  730. MODULE_DESCRIPTION("MSM Audio ION module");
  731. MODULE_LICENSE("GPL v2");