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