123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269 |
- // SPDX-License-Identifier: GPL-2.0-only
- /*
- * Copyright (c) 2013-2020, The Linux Foundation. All rights reserved.
- */
- #include <linux/init.h>
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/err.h>
- #include <linux/delay.h>
- #include <linux/slab.h>
- #include <linux/mutex.h>
- #include <linux/list.h>
- #include <linux/dma-mapping.h>
- #include <linux/dma-buf.h>
- #include <linux/platform_device.h>
- #include <linux/of_device.h>
- #include <linux/export.h>
- #include <linux/ion.h>
- #include <linux/ioctl.h>
- #include <linux/cdev.h>
- #include <linux/fs.h>
- #include <linux/device.h>
- #ifndef CONFIG_SPF_CORE
- #include <ipc/apr.h>
- #endif
- #include <dsp/msm_audio_ion.h>
- #include <linux/msm_audio.h>
- #define MSM_AUDIO_ION_PROBED (1 << 0)
- #define MSM_AUDIO_ION_PHYS_ADDR(alloc_data) \
- alloc_data->table->sgl->dma_address
- #define MSM_AUDIO_SMMU_SID_OFFSET 32
- #define TZ_PIL_PROTECT_MEM_SUBSYS_ID 0x0C
- #define TZ_PIL_CLEAR_PROTECT_MEM_SUBSYS_ID 0x0D
- #define MSM_AUDIO_ION_DRIVER_NAME "msm_audio_ion"
- #define MINOR_NUMBER_COUNT 1
- struct msm_audio_ion_private {
- bool smmu_enabled;
- struct device *cb_dev;
- struct device *cb_cma_dev;
- u8 device_status;
- struct list_head alloc_list;
- struct mutex list_mutex;
- u64 smmu_sid_bits;
- u32 smmu_version;
- bool is_non_hypervisor;
- /*list to store fd, phy. addr and handle data */
- struct list_head fd_list;
- /*char dev related data */
- dev_t ion_major;
- struct class *ion_class;
- struct device *chardev;
- struct cdev cdev;
- };
- struct msm_audio_alloc_data {
- size_t len;
- void *vaddr;
- struct dma_buf *dma_buf;
- struct dma_buf_attachment *attach;
- struct sg_table *table;
- struct list_head list;
- };
- static struct msm_audio_ion_private msm_audio_ion_data = {0,};
- struct msm_audio_fd_data {
- int fd;
- void *handle;
- dma_addr_t paddr;
- struct list_head list;
- };
- static void msm_audio_ion_add_allocation(
- struct msm_audio_ion_private *msm_audio_ion_data,
- struct msm_audio_alloc_data *alloc_data)
- {
- /*
- * Since these APIs can be invoked by multiple
- * clients, there is need to make sure the list
- * of allocations is always protected
- */
- mutex_lock(&(msm_audio_ion_data->list_mutex));
- list_add_tail(&(alloc_data->list),
- &(msm_audio_ion_data->alloc_list));
- mutex_unlock(&(msm_audio_ion_data->list_mutex));
- }
- static void *msm_audio_ion_map_kernel(struct dma_buf *dma_buf)
- {
- int rc = 0;
- void *addr = NULL;
- struct msm_audio_alloc_data *alloc_data = NULL;
- rc = dma_buf_begin_cpu_access(dma_buf, DMA_BIDIRECTIONAL);
- if (rc) {
- pr_err("%s: kmap dma_buf_begin_cpu_access fail\n", __func__);
- goto exit;
- }
- addr = dma_buf_vmap(dma_buf);
- if (!addr) {
- pr_err("%s: kernel mapping of dma_buf failed\n",
- __func__);
- goto exit;
- }
- /*
- * TBD: remove the below section once new API
- * for mapping kernel virtual address is available.
- */
- mutex_lock(&(msm_audio_ion_data.list_mutex));
- list_for_each_entry(alloc_data, &(msm_audio_ion_data.alloc_list),
- list) {
- if (alloc_data->dma_buf == dma_buf) {
- alloc_data->vaddr = addr;
- break;
- }
- }
- mutex_unlock(&(msm_audio_ion_data.list_mutex));
- exit:
- return addr;
- }
- static int msm_audio_dma_buf_map(struct dma_buf *dma_buf,
- dma_addr_t *addr, size_t *len, bool is_iova,
- bool cma_mem)
- {
- struct msm_audio_alloc_data *alloc_data = NULL;
- struct device *cb_dev;
- unsigned long ionflag = 0;
- int rc = 0;
- void *vaddr = NULL;
- if (cma_mem)
- cb_dev = msm_audio_ion_data.cb_cma_dev;
- else
- cb_dev = msm_audio_ion_data.cb_dev;
- /* Data required per buffer mapping */
- alloc_data = kzalloc(sizeof(*alloc_data), GFP_KERNEL);
- if (!alloc_data)
- return -ENOMEM;
- alloc_data->dma_buf = dma_buf;
- alloc_data->len = dma_buf->size;
- *len = dma_buf->size;
- /* Attach the dma_buf to context bank device */
- alloc_data->attach = dma_buf_attach(alloc_data->dma_buf,
- cb_dev);
- if (IS_ERR(alloc_data->attach)) {
- rc = PTR_ERR(alloc_data->attach);
- dev_err(cb_dev,
- "%s: Fail to attach dma_buf to CB, rc = %d\n",
- __func__, rc);
- goto free_alloc_data;
- }
- /* For uncached buffers, avoid cache maintanance */
- rc = dma_buf_get_flags(alloc_data->dma_buf, &ionflag);
- if (rc) {
- dev_err(cb_dev, "%s: dma_buf_get_flags failed: %d\n",
- __func__, rc);
- goto detach_dma_buf;
- }
- if (!(ionflag & ION_FLAG_CACHED))
- alloc_data->attach->dma_map_attrs |= DMA_ATTR_SKIP_CPU_SYNC;
- /*
- * Get the scatter-gather list.
- * There is no info as this is a write buffer or
- * read buffer, hence the request is bi-directional
- * to accommodate both read and write mappings.
- */
- alloc_data->table = dma_buf_map_attachment(alloc_data->attach,
- DMA_BIDIRECTIONAL);
- if (IS_ERR(alloc_data->table)) {
- rc = PTR_ERR(alloc_data->table);
- dev_err(cb_dev,
- "%s: Fail to map attachment, rc = %d\n",
- __func__, rc);
- goto detach_dma_buf;
- }
- /* physical address from mapping */
- if (!is_iova) {
- *addr = sg_phys(alloc_data->table->sgl);
- vaddr = msm_audio_ion_map_kernel((void *)dma_buf);
- if (IS_ERR_OR_NULL(vaddr)) {
- pr_err("%s: ION memory mapping for AUDIO failed\n",
- __func__);
- rc = -ENOMEM;
- goto detach_dma_buf;
- }
- alloc_data->vaddr = vaddr;
- } else {
- *addr = MSM_AUDIO_ION_PHYS_ADDR(alloc_data);
- }
- msm_audio_ion_add_allocation(&msm_audio_ion_data,
- alloc_data);
- return rc;
- detach_dma_buf:
- dma_buf_detach(alloc_data->dma_buf,
- alloc_data->attach);
- free_alloc_data:
- kfree(alloc_data);
- alloc_data = NULL;
- return rc;
- }
- static int msm_audio_dma_buf_unmap(struct dma_buf *dma_buf, bool cma_mem)
- {
- int rc = 0;
- struct msm_audio_alloc_data *alloc_data = NULL;
- struct list_head *ptr, *next;
- struct device *cb_dev;
- bool found = false;
- if (cma_mem)
- cb_dev = msm_audio_ion_data.cb_cma_dev;
- else
- cb_dev = msm_audio_ion_data.cb_dev;
- /*
- * Though list_for_each_safe is delete safe, lock
- * should be explicitly acquired to avoid race condition
- * on adding elements to the list.
- */
- mutex_lock(&(msm_audio_ion_data.list_mutex));
- list_for_each_safe(ptr, next,
- &(msm_audio_ion_data.alloc_list)) {
- alloc_data = list_entry(ptr, struct msm_audio_alloc_data,
- list);
- if (alloc_data->dma_buf == dma_buf) {
- found = true;
- dma_buf_unmap_attachment(alloc_data->attach,
- alloc_data->table,
- DMA_BIDIRECTIONAL);
- dma_buf_detach(alloc_data->dma_buf,
- alloc_data->attach);
- dma_buf_put(alloc_data->dma_buf);
- list_del(&(alloc_data->list));
- kfree(alloc_data);
- alloc_data = NULL;
- break;
- }
- }
- mutex_unlock(&(msm_audio_ion_data.list_mutex));
- if (!found) {
- dev_err(cb_dev,
- "%s: cannot find allocation, dma_buf %pK",
- __func__, dma_buf);
- rc = -EINVAL;
- }
- return rc;
- }
- static int msm_audio_ion_get_phys(struct dma_buf *dma_buf,
- dma_addr_t *addr, size_t *len, bool is_iova)
- {
- int rc = 0;
- rc = msm_audio_dma_buf_map(dma_buf, addr, len, is_iova, false);
- if (rc) {
- pr_err("%s: failed to map DMA buf, err = %d\n",
- __func__, rc);
- goto err;
- }
- if (msm_audio_ion_data.smmu_enabled && is_iova) {
- /* Append the SMMU SID information to the IOVA address */
- *addr |= msm_audio_ion_data.smmu_sid_bits;
- }
- pr_debug("phys=%pK, len=%zd, rc=%d\n", &(*addr), *len, rc);
- err:
- return rc;
- }
- int msm_audio_ion_get_smmu_info(struct device **cb_dev,
- u64 *smmu_sid)
- {
- if (!cb_dev || !smmu_sid) {
- pr_err("%s: Invalid params\n",
- __func__);
- return -EINVAL;
- }
- if (!msm_audio_ion_data.cb_dev ||
- !msm_audio_ion_data.smmu_sid_bits) {
- pr_err("%s: Params not initialized\n",
- __func__);
- return -EINVAL;
- }
- *cb_dev = msm_audio_ion_data.cb_dev;
- *smmu_sid = msm_audio_ion_data.smmu_sid_bits;
- return 0;
- }
- static int msm_audio_ion_unmap_kernel(struct dma_buf *dma_buf)
- {
- int rc = 0;
- void *vaddr = NULL;
- struct msm_audio_alloc_data *alloc_data = NULL;
- struct device *cb_dev = msm_audio_ion_data.cb_dev;
- /*
- * TBD: remove the below section once new API
- * for unmapping kernel virtual address is available.
- */
- mutex_lock(&(msm_audio_ion_data.list_mutex));
- list_for_each_entry(alloc_data, &(msm_audio_ion_data.alloc_list),
- list) {
- if (alloc_data->dma_buf == dma_buf) {
- vaddr = alloc_data->vaddr;
- break;
- }
- }
- mutex_unlock(&(msm_audio_ion_data.list_mutex));
- if (!vaddr) {
- dev_err(cb_dev,
- "%s: cannot find allocation for dma_buf %pK",
- __func__, dma_buf);
- rc = -EINVAL;
- goto err;
- }
- dma_buf_vunmap(dma_buf, vaddr);
- rc = dma_buf_end_cpu_access(dma_buf, DMA_BIDIRECTIONAL);
- if (rc) {
- dev_err(cb_dev, "%s: kmap dma_buf_end_cpu_access fail\n",
- __func__);
- goto err;
- }
- err:
- return rc;
- }
- static int msm_audio_ion_map_buf(struct dma_buf *dma_buf, dma_addr_t *paddr,
- size_t *plen, void **vaddr)
- {
- int rc = 0;
- bool is_iova = true;
- if (!dma_buf || !paddr || !vaddr || !plen) {
- pr_err("%s: Invalid params\n", __func__);
- return -EINVAL;
- }
- rc = msm_audio_ion_get_phys(dma_buf, paddr, plen, is_iova);
- if (rc) {
- pr_err("%s: ION Get Physical for AUDIO failed, rc = %d\n",
- __func__, rc);
- dma_buf_put(dma_buf);
- goto err;
- }
- *vaddr = msm_audio_ion_map_kernel(dma_buf);
- if (IS_ERR_OR_NULL(*vaddr)) {
- pr_err("%s: ION memory mapping for AUDIO failed\n", __func__);
- rc = -ENOMEM;
- msm_audio_dma_buf_unmap(dma_buf, false);
- goto err;
- }
- err:
- return rc;
- }
- static u32 msm_audio_ion_get_smmu_sid_mode32(void)
- {
- if (msm_audio_ion_data.smmu_enabled)
- return upper_32_bits(msm_audio_ion_data.smmu_sid_bits);
- else
- return 0;
- }
- /**
- * msm_audio_ion_alloc -
- * Allocs ION memory for given client name
- *
- * @dma_buf: dma_buf for the ION memory
- * @bufsz: buffer size
- * @paddr: Physical address to be assigned with allocated region
- * @plen: length of allocated region to be assigned
- * vaddr: virtual address to be assigned
- *
- * Returns 0 on success or error on failure
- */
- int msm_audio_ion_alloc(struct dma_buf **dma_buf, size_t bufsz,
- dma_addr_t *paddr, size_t *plen, void **vaddr)
- {
- int rc = -EINVAL;
- unsigned long err_ion_ptr = 0;
- if (!(msm_audio_ion_data.device_status & MSM_AUDIO_ION_PROBED)) {
- pr_debug("%s:probe is not done, deferred\n", __func__);
- return -EPROBE_DEFER;
- }
- if (!dma_buf || !paddr || !vaddr || !bufsz || !plen) {
- pr_err("%s: Invalid params\n", __func__);
- return -EINVAL;
- }
- if (msm_audio_ion_data.smmu_enabled == true) {
- pr_debug("%s: system heap is used\n", __func__);
- *dma_buf = ion_alloc(bufsz, ION_HEAP(ION_SYSTEM_HEAP_ID), 0);
- } else {
- pr_debug("%s: audio heap is used\n", __func__);
- *dma_buf = ion_alloc(bufsz, ION_HEAP(ION_AUDIO_HEAP_ID), 0);
- }
- if (IS_ERR_OR_NULL((void *)(*dma_buf))) {
- if (IS_ERR((void *)(*dma_buf)))
- err_ion_ptr = PTR_ERR((int *)(*dma_buf));
- pr_err("%s: ION alloc fail err ptr=%ld, smmu_enabled=%d\n",
- __func__, err_ion_ptr, msm_audio_ion_data.smmu_enabled);
- rc = -ENOMEM;
- goto err;
- }
- rc = msm_audio_ion_map_buf(*dma_buf, paddr, plen, vaddr);
- if (rc) {
- pr_err("%s: failed to map ION buf, rc = %d\n", __func__, rc);
- goto err;
- }
- pr_debug("%s: mapped address = %pK, size=%zd\n", __func__,
- *vaddr, bufsz);
- memset(*vaddr, 0, bufsz);
- // Cleanp dmabuf if error?
- err:
- return rc;
- }
- EXPORT_SYMBOL(msm_audio_ion_alloc);
- int msm_audio_ion_phys_free(void *handle,
- dma_addr_t *paddr,
- size_t *pa_len,
- u8 assign_type,
- int id,
- int key)
- {
- handle = NULL;
- return 0;
- }
- EXPORT_SYMBOL(msm_audio_ion_phys_free);
- int msm_audio_ion_phys_assign(void **handle, int fd,
- dma_addr_t *paddr, size_t *pa_len, u8 assign_type, int id)
- {
- *handle = NULL;
- return 0;
- }
- EXPORT_SYMBOL(msm_audio_ion_phys_assign);
- bool msm_audio_is_hypervisor_supported(void)
- {
- return !(msm_audio_ion_data.is_non_hypervisor);
- }
- EXPORT_SYMBOL(msm_audio_is_hypervisor_supported);
- /**
- * msm_audio_ion_dma_map -
- * Memory maps for a given DMA buffer
- *
- * @phys_addr: Physical address of DMA buffer to be mapped
- * @iova_base: IOVA address of memory mapped DMA buffer
- * @size: buffer size
- * @dir: DMA direction
- * Returns 0 on success or error on failure
- */
- int msm_audio_ion_dma_map(dma_addr_t *phys_addr, dma_addr_t *iova_base,
- u32 size, enum dma_data_direction dir)
- {
- dma_addr_t iova;
- struct device *cb_dev = msm_audio_ion_data.cb_dev;
- if (!phys_addr || !iova_base || !size)
- return -EINVAL;
- iova = dma_map_resource(cb_dev, *phys_addr, size,
- dir, 0);
- if (dma_mapping_error(cb_dev, iova)) {
- pr_err("%s: dma_mapping_error\n", __func__);
- return -EIO;
- }
- pr_debug("%s: dma_mapping_success iova:0x%lx\n", __func__,
- (unsigned long)iova);
- if (msm_audio_ion_data.smmu_enabled)
- /* Append the SMMU SID information to the IOVA address */
- iova |= msm_audio_ion_data.smmu_sid_bits;
- *iova_base = iova;
- return 0;
- }
- EXPORT_SYMBOL(msm_audio_ion_dma_map);
- void msm_audio_fd_list_debug(void)
- {
- struct msm_audio_fd_data *msm_audio_fd_data = NULL;
- list_for_each_entry(msm_audio_fd_data,
- &msm_audio_ion_data.fd_list, list) {
- pr_debug("%s fd %d handle %pK phy. addr %pK\n", __func__,
- msm_audio_fd_data->fd, msm_audio_fd_data->handle,
- (void *)msm_audio_fd_data->paddr);
- }
- }
- void msm_audio_update_fd_list(struct msm_audio_fd_data *msm_audio_fd_data)
- {
- struct msm_audio_fd_data *msm_audio_fd_data1 = NULL;
- mutex_lock(&(msm_audio_ion_data.list_mutex));
- list_for_each_entry(msm_audio_fd_data1,
- &msm_audio_ion_data.fd_list, list) {
- if (msm_audio_fd_data1->fd == msm_audio_fd_data->fd) {
- pr_err("%s fd already present, not updating the list",
- __func__);
- mutex_unlock(&(msm_audio_ion_data.list_mutex));
- return;
- }
- }
- list_add_tail(&msm_audio_fd_data->list, &msm_audio_ion_data.fd_list);
- mutex_unlock(&(msm_audio_ion_data.list_mutex));
- }
- void msm_audio_delete_fd_entry(void *handle)
- {
- struct msm_audio_fd_data *msm_audio_fd_data = NULL;
- struct list_head *ptr, *next;
- mutex_lock(&(msm_audio_ion_data.list_mutex));
- list_for_each_safe(ptr, next,
- &msm_audio_ion_data.fd_list) {
- msm_audio_fd_data = list_entry(ptr, struct msm_audio_fd_data,
- list);
- if (msm_audio_fd_data->handle == handle) {
- pr_debug("%s deleting handle %pK entry from list\n",
- __func__, handle);
- list_del(&(msm_audio_fd_data->list));
- kfree(msm_audio_fd_data);
- break;
- }
- }
- mutex_unlock(&(msm_audio_ion_data.list_mutex));
- }
- int msm_audio_get_phy_addr(int fd, dma_addr_t *paddr)
- {
- struct msm_audio_fd_data *msm_audio_fd_data = NULL;
- int status = -EINVAL;
- if (!paddr) {
- pr_err("%s Invalid paddr param status %d\n", __func__, status);
- return status;
- }
- pr_debug("%s, fd %d\n", __func__, fd);
- mutex_lock(&(msm_audio_ion_data.list_mutex));
- list_for_each_entry(msm_audio_fd_data,
- &msm_audio_ion_data.fd_list, list) {
- if (msm_audio_fd_data->fd == fd) {
- *paddr = msm_audio_fd_data->paddr;
- status = 0;
- pr_debug("%s Found fd %d paddr %pK\n",
- __func__, fd, paddr);
- mutex_unlock(&(msm_audio_ion_data.list_mutex));
- return status;
- }
- }
- mutex_unlock(&(msm_audio_ion_data.list_mutex));
- return status;
- }
- EXPORT_SYMBOL(msm_audio_get_phy_addr);
- void msm_audio_get_handle(int fd, void **handle)
- {
- struct msm_audio_fd_data *msm_audio_fd_data = NULL;
- pr_debug("%s fd %d\n", __func__, fd);
- mutex_lock(&(msm_audio_ion_data.list_mutex));
- list_for_each_entry(msm_audio_fd_data,
- &msm_audio_ion_data.fd_list, list) {
- if (msm_audio_fd_data->fd == fd) {
- *handle = (struct dma_buf *)msm_audio_fd_data->handle;
- pr_debug("%s handle %pK\n", __func__, *handle);
- break;
- }
- }
- mutex_unlock(&(msm_audio_ion_data.list_mutex));
- }
- /**
- * msm_audio_ion_import-
- * Import ION buffer with given file descriptor
- *
- * @dma_buf: dma_buf for the ION memory
- * @fd: file descriptor for the ION memory
- * @ionflag: flags associated with ION buffer
- * @bufsz: buffer size
- * @paddr: Physical address to be assigned with allocated region
- * @plen: length of allocated region to be assigned
- * @vaddr: virtual address to be assigned
- *
- * Returns 0 on success or error on failure
- */
- int msm_audio_ion_import(struct dma_buf **dma_buf, int fd,
- unsigned long *ionflag, size_t bufsz,
- dma_addr_t *paddr, size_t *plen, void **vaddr)
- {
- int rc = 0;
- if (!(msm_audio_ion_data.device_status & MSM_AUDIO_ION_PROBED)) {
- pr_debug("%s: probe is not done, deferred\n", __func__);
- return -EPROBE_DEFER;
- }
- if (!dma_buf || !paddr || !vaddr || !plen) {
- pr_err("%s: Invalid params\n", __func__);
- return -EINVAL;
- }
- /* bufsz should be 0 and fd shouldn't be 0 as of now */
- *dma_buf = dma_buf_get(fd);
- pr_debug("%s: dma_buf =%pK, fd=%d\n", __func__, *dma_buf, fd);
- if (IS_ERR_OR_NULL((void *)(*dma_buf))) {
- pr_err("%s: dma_buf_get failed\n", __func__);
- rc = -EINVAL;
- goto err;
- }
- if (ionflag != NULL) {
- rc = dma_buf_get_flags(*dma_buf, ionflag);
- if (rc) {
- pr_err("%s: could not get flags for the dma_buf\n",
- __func__);
- goto err_ion_flag;
- }
- }
- rc = msm_audio_ion_map_buf(*dma_buf, paddr, plen, vaddr);
- if (rc) {
- pr_err("%s: failed to map ION buf, rc = %d\n", __func__, rc);
- goto err;
- }
- pr_debug("%s: mapped address = %pK, size=%zd\n", __func__,
- *vaddr, bufsz);
- return 0;
- err_ion_flag:
- dma_buf_put(*dma_buf);
- err:
- *dma_buf = NULL;
- return rc;
- }
- EXPORT_SYMBOL(msm_audio_ion_import);
- /**
- * msm_audio_ion_import_cma-
- * Import ION buffer with given file descriptor
- *
- * @dma_buf: dma_buf for the ION memory
- * @fd: file descriptor for the ION memory
- * @ionflag: flags associated with ION buffer
- * @bufsz: buffer size
- * @paddr: Physical address to be assigned with allocated region
- * @plen: length of allocated region to be assigned
- * vaddr: virtual address to be assigned
- *
- * Returns 0 on success or error on failure
- */
- int msm_audio_ion_import_cma(struct dma_buf **dma_buf, int fd,
- unsigned long *ionflag, size_t bufsz,
- dma_addr_t *paddr, size_t *plen, void **vaddr)
- {
- int rc = 0;
- if (!(msm_audio_ion_data.device_status & MSM_AUDIO_ION_PROBED)) {
- pr_debug("%s: probe is not done, deferred\n", __func__);
- return -EPROBE_DEFER;
- }
- if (!dma_buf || !paddr || !vaddr || !plen ||
- !msm_audio_ion_data.cb_cma_dev) {
- pr_err("%s: Invalid params\n", __func__);
- return -EINVAL;
- }
- /* bufsz should be 0 and fd shouldn't be 0 as of now */
- *dma_buf = dma_buf_get(fd);
- pr_debug("%s: dma_buf =%pK, fd=%d\n", __func__, *dma_buf, fd);
- if (IS_ERR_OR_NULL((void *)(*dma_buf))) {
- pr_err("%s: dma_buf_get failed\n", __func__);
- rc = -EINVAL;
- goto err;
- }
- if (ionflag != NULL) {
- rc = dma_buf_get_flags(*dma_buf, ionflag);
- if (rc) {
- pr_err("%s: could not get flags for the dma_buf\n",
- __func__);
- goto err_ion_flag;
- }
- }
- msm_audio_dma_buf_map(*dma_buf, paddr, plen, true, true);
- return 0;
- err_ion_flag:
- dma_buf_put(*dma_buf);
- err:
- *dma_buf = NULL;
- return rc;
- }
- EXPORT_SYMBOL(msm_audio_ion_import_cma);
- /**
- * msm_audio_ion_free -
- * fress ION memory for given client and handle
- *
- * @dma_buf: dma_buf for the ION memory
- *
- * Returns 0 on success or error on failure
- */
- int msm_audio_ion_free(struct dma_buf *dma_buf)
- {
- int ret = 0;
- if (!dma_buf) {
- pr_err("%s: dma_buf invalid\n", __func__);
- return -EINVAL;
- }
- ret = msm_audio_ion_unmap_kernel(dma_buf);
- if (ret)
- return ret;
- msm_audio_dma_buf_unmap(dma_buf, false);
- return 0;
- }
- EXPORT_SYMBOL(msm_audio_ion_free);
- /**
- * msm_audio_ion_crash_handler -
- * handles cleanup after userspace crashes.
- *
- * To be called from machine driver.
- */
- void msm_audio_ion_crash_handler(void)
- {
- struct msm_audio_fd_data *msm_audio_fd_data = NULL;
- struct list_head *ptr, *next;
- void *handle = NULL;
- pr_debug("Inside %s\n", __func__);
- list_for_each_entry(msm_audio_fd_data,
- &msm_audio_ion_data.fd_list, list) {
- handle = msm_audio_fd_data->handle;
- msm_audio_ion_free(handle);
- }
- list_for_each_safe(ptr, next,
- &msm_audio_ion_data.fd_list) {
- msm_audio_fd_data = list_entry(ptr, struct msm_audio_fd_data,
- list);
- list_del(&(msm_audio_fd_data->list));
- kfree(msm_audio_fd_data);
- }
- }
- EXPORT_SYMBOL(msm_audio_ion_crash_handler);
- /**
- * msm_audio_ion_free_cma -
- * fress ION memory for given client and handle
- *
- * @dma_buf: dma_buf for the ION memory
- *
- * Returns 0 on success or error on failure
- */
- int msm_audio_ion_free_cma(struct dma_buf *dma_buf)
- {
- if (!dma_buf) {
- pr_err("%s: dma_buf invalid\n", __func__);
- return -EINVAL;
- }
- msm_audio_dma_buf_unmap(dma_buf, true);
- return 0;
- }
- EXPORT_SYMBOL(msm_audio_ion_free_cma);
- /**
- * msm_audio_ion_mmap -
- * Audio ION memory map
- *
- * @abuff: audio buf pointer
- * @vma: virtual mem area
- *
- * Returns 0 on success or error on failure
- */
- int msm_audio_ion_mmap(struct audio_buffer *abuff,
- struct vm_area_struct *vma)
- {
- struct msm_audio_alloc_data *alloc_data = NULL;
- struct sg_table *table;
- unsigned long addr = vma->vm_start;
- unsigned long offset = vma->vm_pgoff * PAGE_SIZE;
- struct scatterlist *sg;
- unsigned int i;
- struct page *page;
- int ret = 0;
- bool found = false;
- struct device *cb_dev = msm_audio_ion_data.cb_dev;
- mutex_lock(&(msm_audio_ion_data.list_mutex));
- list_for_each_entry(alloc_data, &(msm_audio_ion_data.alloc_list),
- list) {
- if (alloc_data->dma_buf == abuff->dma_buf) {
- found = true;
- table = alloc_data->table;
- break;
- }
- }
- mutex_unlock(&(msm_audio_ion_data.list_mutex));
- if (!found) {
- dev_err(cb_dev,
- "%s: cannot find allocation, dma_buf %pK",
- __func__, abuff->dma_buf);
- return -EINVAL;
- }
- /* uncached */
- vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
- /* We need to check if a page is associated with this sg list because:
- * If the allocation came from a carveout we currently don't have
- * pages associated with carved out memory. This might change in the
- * future and we can remove this check and the else statement.
- */
- page = sg_page(table->sgl);
- if (page) {
- pr_debug("%s: page is NOT null\n", __func__);
- for_each_sg(table->sgl, sg, table->nents, i) {
- unsigned long remainder = vma->vm_end - addr;
- unsigned long len = sg->length;
- page = sg_page(sg);
- if (offset >= len) {
- offset -= len;
- continue;
- } else if (offset) {
- page += offset / PAGE_SIZE;
- len -= offset;
- offset = 0;
- }
- len = min(len, remainder);
- pr_debug("vma=%pK, addr=%x len=%ld vm_start=%x vm_end=%x vm_page_prot=%lu\n",
- vma, (unsigned int)addr, len,
- (unsigned int)vma->vm_start,
- (unsigned int)vma->vm_end,
- (unsigned long)pgprot_val(vma->vm_page_prot));
- remap_pfn_range(vma, addr, page_to_pfn(page), len,
- vma->vm_page_prot);
- addr += len;
- if (addr >= vma->vm_end)
- return 0;
- }
- } else {
- pr_debug("%s: page is NULL\n", __func__);
- ret = -EINVAL;
- }
- return ret;
- }
- EXPORT_SYMBOL(msm_audio_ion_mmap);
- /**
- * msm_audio_ion_cache_operations-
- * Cache operations on cached Audio ION buffers
- *
- * @abuff: audio buf pointer
- * @cache_op: cache operation to be performed
- *
- * Returns 0 on success or error on failure
- */
- int msm_audio_ion_cache_operations(struct audio_buffer *abuff, int cache_op)
- {
- unsigned long ionflag = 0;
- int rc = 0;
- if (!abuff) {
- pr_err("%s: Invalid params: %pK\n", __func__, abuff);
- return -EINVAL;
- }
- rc = dma_buf_get_flags(abuff->dma_buf, &ionflag);
- if (rc) {
- pr_err("%s: dma_buf_get_flags failed: %d\n", __func__, rc);
- goto cache_op_failed;
- }
- /* Has to be CACHED */
- if (ionflag & ION_FLAG_CACHED) {
- /* MSM_AUDIO_ION_INV_CACHES or MSM_AUDIO_ION_CLEAN_CACHES */
- switch (cache_op) {
- case MSM_AUDIO_ION_INV_CACHES:
- case MSM_AUDIO_ION_CLEAN_CACHES:
- dma_buf_begin_cpu_access(abuff->dma_buf,
- DMA_BIDIRECTIONAL);
- dma_buf_end_cpu_access(abuff->dma_buf,
- DMA_BIDIRECTIONAL);
- break;
- default:
- pr_err("%s: Invalid cache operation %d\n",
- __func__, cache_op);
- }
- } else {
- pr_err("%s: Cache ops called on uncached buffer: %pK\n",
- __func__, abuff->dma_buf);
- rc = -EINVAL;
- }
- cache_op_failed:
- return rc;
- }
- EXPORT_SYMBOL(msm_audio_ion_cache_operations);
- /**
- * msm_audio_populate_upper_32_bits -
- * retrieve upper 32bits of 64bit address
- *
- * @pa: 64bit physical address
- *
- */
- u32 msm_audio_populate_upper_32_bits(dma_addr_t pa)
- {
- if (sizeof(dma_addr_t) == sizeof(u32))
- return msm_audio_ion_get_smmu_sid_mode32();
- else
- return upper_32_bits(pa);
- }
- EXPORT_SYMBOL(msm_audio_populate_upper_32_bits);
- static int msm_audio_ion_open(struct inode *inode, struct file *file)
- {
- int ret = 0;
- struct msm_audio_ion_private *ion_data = container_of(inode->i_cdev,
- struct msm_audio_ion_private,
- cdev);
- struct device *dev = ion_data->chardev;
- pr_debug("Inside %s\n", __func__);
- get_device(dev);
- return ret;
- }
- static int msm_audio_ion_release(struct inode *inode, struct file *file)
- {
- struct msm_audio_ion_private *ion_data = container_of(inode->i_cdev,
- struct msm_audio_ion_private,
- cdev);
- struct device *dev = ion_data->chardev;
- pr_debug("Inside %s\n", __func__);
- put_device(dev);
- return 0;
- }
- static long msm_audio_ion_ioctl(struct file *file, unsigned int ioctl_num,
- unsigned long __user ioctl_param)
- {
- void *mem_handle;
- dma_addr_t paddr;
- size_t pa_len = 0;
- void *vaddr;
- int ret = 0;
- struct msm_audio_fd_data *msm_audio_fd_data = NULL;
- pr_debug("%s ioctl num %u\n", __func__, ioctl_num);
- switch (ioctl_num) {
- case IOCTL_MAP_PHYS_ADDR:
- msm_audio_fd_data = kzalloc((sizeof(struct msm_audio_fd_data)),
- GFP_KERNEL);
- if (!msm_audio_fd_data) {
- ret = -ENOMEM;
- pr_err("%s : Out of memory ret %d\n", __func__, ret);
- return ret;
- }
- ret = msm_audio_ion_import((struct dma_buf **)&mem_handle, (int)ioctl_param,
- NULL, 0, &paddr, &pa_len, &vaddr);
- if (ret < 0) {
- pr_err("%s Memory map Failed %d\n", __func__, ret);
- kfree(msm_audio_fd_data);
- return ret;
- }
- msm_audio_fd_data->fd = (int)ioctl_param;
- msm_audio_fd_data->handle = mem_handle;
- msm_audio_fd_data->paddr = paddr;
- msm_audio_update_fd_list(msm_audio_fd_data);
- break;
- case IOCTL_UNMAP_PHYS_ADDR:
- msm_audio_get_handle((int)ioctl_param, &mem_handle);
- ret = msm_audio_ion_free(mem_handle);
- if (ret < 0) {
- pr_err("%s Ion free failed %d\n", __func__, ret);
- return ret;
- }
- msm_audio_delete_fd_entry(mem_handle);
- break;
- default:
- pr_err("%s Entered default. Invalid ioctl num %u",
- __func__, ioctl_num);
- ret = -EINVAL;
- break;
- }
- return ret;
- }
- static int msm_audio_smmu_init(struct device *dev)
- {
- INIT_LIST_HEAD(&msm_audio_ion_data.alloc_list);
- mutex_init(&(msm_audio_ion_data.list_mutex));
- return 0;
- }
- static const struct of_device_id msm_audio_ion_dt_match[] = {
- { .compatible = "qcom,msm-audio-ion" },
- { .compatible = "qcom,msm-audio-ion-cma"},
- { }
- };
- MODULE_DEVICE_TABLE(of, msm_audio_ion_dt_match);
- static const struct file_operations msm_audio_ion_fops = {
- .owner = THIS_MODULE,
- .open = msm_audio_ion_open,
- .release = msm_audio_ion_release,
- .unlocked_ioctl = msm_audio_ion_ioctl,
- };
- static int msm_audio_ion_reg_chrdev(struct msm_audio_ion_private *ion_data)
- {
- int ret = 0;
- ret = alloc_chrdev_region(&ion_data->ion_major, 0,
- MINOR_NUMBER_COUNT, MSM_AUDIO_ION_DRIVER_NAME);
- if (ret < 0) {
- pr_err("%s alloc_chr_dev_region failed ret : %d\n",
- __func__, ret);
- return ret;
- }
- pr_debug("%s major number %d", __func__, MAJOR(ion_data->ion_major));
- ion_data->ion_class = class_create(THIS_MODULE,
- MSM_AUDIO_ION_DRIVER_NAME);
- if (IS_ERR(ion_data->ion_class)) {
- ret = PTR_ERR(ion_data->ion_class);
- pr_err("%s class create failed. ret : %d", __func__, ret);
- goto err_class;
- }
- ion_data->chardev = device_create(ion_data->ion_class, NULL,
- ion_data->ion_major, NULL,
- MSM_AUDIO_ION_DRIVER_NAME);
- if (IS_ERR(ion_data->chardev)) {
- ret = PTR_ERR(ion_data->chardev);
- pr_err("%s device create failed ret : %d\n", __func__, ret);
- goto err_device;
- }
- cdev_init(&ion_data->cdev, &msm_audio_ion_fops);
- ret = cdev_add(&ion_data->cdev, ion_data->ion_major, 1);
- if (ret) {
- pr_err("%s cdev add failed, ret : %d\n", __func__, ret);
- goto err_cdev;
- }
- return ret;
- err_cdev:
- device_destroy(ion_data->ion_class, ion_data->ion_major);
- err_device:
- class_destroy(ion_data->ion_class);
- err_class:
- unregister_chrdev_region(0, MINOR_NUMBER_COUNT);
- return ret;
- }
- static int msm_audio_ion_unreg_chrdev(struct msm_audio_ion_private *ion_data)
- {
- cdev_del(&ion_data->cdev);
- device_destroy(ion_data->ion_class, ion_data->ion_major);
- class_destroy(ion_data->ion_class);
- unregister_chrdev_region(0, MINOR_NUMBER_COUNT);
- return 0;
- }
- static int msm_audio_ion_probe(struct platform_device *pdev)
- {
- int rc = 0;
- u64 smmu_sid = 0;
- u64 smmu_sid_mask = 0;
- const char *msm_audio_ion_dt = "qcom,smmu-enabled";
- const char *msm_audio_ion_non_hyp = "qcom,non-hyp-assign";
- const char *msm_audio_ion_smmu = "qcom,smmu-version";
- const char *msm_audio_ion_smmu_sid_mask = "qcom,smmu-sid-mask";
- bool smmu_enabled;
- bool is_non_hypervisor_en;
- struct device *dev = &pdev->dev;
- struct of_phandle_args iommuspec;
- #ifndef CONFIG_SPF_CORE
- enum apr_subsys_state q6_state;
- #endif
- dev_err(dev, "%s: msm_audio_ion_probe\n", __func__);
- if (dev->of_node == NULL) {
- dev_err(dev,
- "%s: device tree is not found\n",
- __func__);
- msm_audio_ion_data.smmu_enabled = 0;
- return 0;
- }
- is_non_hypervisor_en = of_property_read_bool(dev->of_node,
- msm_audio_ion_non_hyp);
- msm_audio_ion_data.is_non_hypervisor = is_non_hypervisor_en;
- if (of_device_is_compatible(dev->of_node, "qcom,msm-audio-ion-cma")) {
- msm_audio_ion_data.cb_cma_dev = dev;
- return 0;
- }
- smmu_enabled = of_property_read_bool(dev->of_node,
- msm_audio_ion_dt);
- msm_audio_ion_data.smmu_enabled = smmu_enabled;
- if (!smmu_enabled) {
- dev_dbg(dev, "%s: SMMU is Disabled\n", __func__);
- goto exit;
- }
- #ifndef CONFIG_SPF_CORE
- q6_state = apr_get_q6_state();
- if (q6_state == APR_SUBSYS_DOWN) {
- dev_info(dev,
- "defering %s, adsp_state %d\n",
- __func__, q6_state);
- return -EPROBE_DEFER;
- }
- #endif
- dev_dbg(dev, "%s: adsp is ready\n", __func__);
- rc = of_property_read_u32(dev->of_node,
- msm_audio_ion_smmu,
- &msm_audio_ion_data.smmu_version);
- if (rc) {
- dev_err(dev,
- "%s: qcom,smmu_version missing in DT node\n",
- __func__);
- return rc;
- }
- dev_dbg(dev, "%s: SMMU is Enabled. SMMU version is (%d)",
- __func__, msm_audio_ion_data.smmu_version);
- /* Get SMMU SID information from Devicetree */
- rc = of_property_read_u64(dev->of_node,
- msm_audio_ion_smmu_sid_mask,
- &smmu_sid_mask);
- if (rc) {
- dev_err(dev,
- "%s: qcom,smmu-sid-mask missing in DT node, using default\n",
- __func__);
- smmu_sid_mask = 0xFFFFFFFFFFFFFFFF;
- }
- rc = of_parse_phandle_with_args(dev->of_node, "iommus",
- "#iommu-cells", 0, &iommuspec);
- if (rc)
- dev_err(dev, "%s: could not get smmu SID, ret = %d\n",
- __func__, rc);
- else
- smmu_sid = (iommuspec.args[0] & smmu_sid_mask);
- msm_audio_ion_data.smmu_sid_bits =
- smmu_sid << MSM_AUDIO_SMMU_SID_OFFSET;
- if (msm_audio_ion_data.smmu_version == 0x2) {
- rc = msm_audio_smmu_init(dev);
- } else {
- dev_err(dev, "%s: smmu version invalid %d\n",
- __func__, msm_audio_ion_data.smmu_version);
- rc = -EINVAL;
- }
- if (rc)
- dev_err(dev, "%s: smmu init failed, err = %d\n",
- __func__, rc);
- exit:
- if (!rc)
- msm_audio_ion_data.device_status |= MSM_AUDIO_ION_PROBED;
- msm_audio_ion_data.cb_dev = dev;
- INIT_LIST_HEAD(&msm_audio_ion_data.fd_list);
- rc = msm_audio_ion_reg_chrdev(&msm_audio_ion_data);
- if (rc) {
- pr_err("%s register char dev failed, rc : %d", __func__, rc);
- return rc;
- }
- return rc;
- }
- static int msm_audio_ion_remove(struct platform_device *pdev)
- {
- struct device *audio_cb_dev;
- audio_cb_dev = msm_audio_ion_data.cb_dev;
- msm_audio_ion_data.smmu_enabled = 0;
- msm_audio_ion_data.device_status = 0;
- msm_audio_ion_unreg_chrdev(&msm_audio_ion_data);
- return 0;
- }
- static struct platform_driver msm_audio_ion_driver = {
- .driver = {
- .name = "msm-audio-ion",
- .owner = THIS_MODULE,
- .of_match_table = msm_audio_ion_dt_match,
- .suppress_bind_attrs = true,
- },
- .probe = msm_audio_ion_probe,
- .remove = msm_audio_ion_remove,
- };
- int __init msm_audio_ion_init(void)
- {
- pr_debug("%s: msm_audio_ion_init called \n",__func__);
- return platform_driver_register(&msm_audio_ion_driver);
- }
- void msm_audio_ion_exit(void)
- {
- platform_driver_unregister(&msm_audio_ion_driver);
- }
- module_init(msm_audio_ion_init);
- module_exit(msm_audio_ion_exit);
- MODULE_DESCRIPTION("MSM Audio ION module");
- MODULE_LICENSE("GPL v2");
|