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