123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778 |
- // SPDX-License-Identifier: GPL-2.0-only
- /*
- * Copyright (c) 2016-2021, The Linux Foundation. All rights reserved.
- * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved.
- */
- #include <linux/module.h>
- #include <linux/types.h>
- #include <linux/mutex.h>
- #include <linux/slab.h>
- #include <linux/dma-buf.h>
- #include <linux/version.h>
- #include <linux/debugfs.h>
- #if IS_REACHABLE(CONFIG_DMABUF_HEAPS)
- #include <linux/mem-buf.h>
- #include <soc/qcom/secure_buffer.h>
- #endif
- #include "cam_compat.h"
- #include "cam_req_mgr_util.h"
- #include "cam_mem_mgr.h"
- #include "cam_smmu_api.h"
- #include "cam_debug_util.h"
- #include "cam_trace.h"
- #include "cam_common_util.h"
- #include "cam_presil_hw_access.h"
- #define CAM_MEM_SHARED_BUFFER_PAD_4K (4 * 1024)
- static struct cam_mem_table tbl;
- static atomic_t cam_mem_mgr_state = ATOMIC_INIT(CAM_MEM_MGR_UNINITIALIZED);
- /* Number of words for dumping req state info */
- #define CAM_MEM_MGR_DUMP_BUF_NUM_WORDS 29
- /* cam_mem_mgr_debug - global struct to keep track of debug settings for mem mgr
- *
- * @dentry : Directory entry to the mem mgr root folder
- * @alloc_profile_enable : Whether to enable alloc profiling
- * @override_cpu_access_dir : Override cpu access direction to BIDIRECTIONAL
- */
- static struct {
- struct dentry *dentry;
- bool alloc_profile_enable;
- bool override_cpu_access_dir;
- } g_cam_mem_mgr_debug;
- #if IS_REACHABLE(CONFIG_DMABUF_HEAPS)
- static void cam_mem_mgr_put_dma_heaps(void);
- static int cam_mem_mgr_get_dma_heaps(void);
- #endif
- #ifdef CONFIG_CAM_PRESIL
- static inline void cam_mem_mgr_reset_presil_params(int idx)
- {
- tbl.bufq[idx].presil_params.fd_for_umd_daemon = -1;
- tbl.bufq[idx].presil_params.refcount = 0;
- }
- #else
- static inline void cam_mem_mgr_reset_presil_params(int idx)
- {
- return;
- }
- #endif
- static unsigned long cam_mem_mgr_mini_dump_cb(void *dst, unsigned long len,
- void *priv_data)
- {
- struct cam_mem_table_mini_dump *md;
- if (!dst) {
- CAM_ERR(CAM_MEM, "Invalid params");
- return 0;
- }
- if (len < sizeof(*md)) {
- CAM_ERR(CAM_MEM, "Insufficient length %u", len);
- return 0;
- }
- md = (struct cam_mem_table_mini_dump *)dst;
- memcpy(md->bufq, tbl.bufq, CAM_MEM_BUFQ_MAX * sizeof(struct cam_mem_buf_queue));
- md->dbg_buf_idx = tbl.dbg_buf_idx;
- md->alloc_profile_enable = g_cam_mem_mgr_debug.alloc_profile_enable;
- md->force_cache_allocs = tbl.force_cache_allocs;
- md->need_shared_buffer_padding = tbl.need_shared_buffer_padding;
- return sizeof(*md);
- }
- static void cam_mem_mgr_print_tbl(void)
- {
- int i;
- uint64_t ms, hrs, min, sec;
- struct timespec64 current_ts;
- CAM_GET_TIMESTAMP(current_ts);
- CAM_CONVERT_TIMESTAMP_FORMAT(current_ts, hrs, min, sec, ms);
- CAM_INFO(CAM_MEM, "***%llu:%llu:%llu:%llu Mem mgr table dump***",
- hrs, min, sec, ms);
- for (i = 1; i < CAM_MEM_BUFQ_MAX; i++) {
- CAM_CONVERT_TIMESTAMP_FORMAT((tbl.bufq[i].timestamp), hrs, min, sec, ms);
- CAM_INFO(CAM_MEM,
- "%llu:%llu:%llu:%llu idx %d fd %d i_ino %lu size %llu active %d buf_handle %d krefCount %d urefCount %d buf_name %s",
- hrs, min, sec, ms, i, tbl.bufq[i].fd, tbl.bufq[i].i_ino,
- tbl.bufq[i].len, tbl.bufq[i].active, tbl.bufq[i].buf_handle,
- kref_read(&tbl.bufq[i].krefcount), kref_read(&tbl.bufq[i].urefcount),
- tbl.bufq[i].buf_name);
- }
- }
- /**
- * For faster lookups, maintaining same indexing as SMMU
- * for saving iova for a given buffer for a given context
- * bank
- *
- * Buffer X : [iova_1, 0x0, iova_3, ...]
- * Here iova_1 is for device_1, no iova available for device_2,
- * iova_3 for device_3 and so on
- */
- static inline bool cam_mem_mgr_get_hwva_entry_idx(
- int32_t mem_handle, int32_t *entry_idx)
- {
- int entry;
- entry = GET_SMMU_TABLE_IDX(mem_handle);
- if (unlikely((entry < 0) || (entry >= tbl.max_hdls_supported))) {
- CAM_ERR(CAM_MEM,
- "Invalid mem_hdl: 0x%x, failed to lookup", mem_handle);
- return false;
- }
- *entry_idx = entry;
- return true;
- }
- static int cam_mem_util_get_dma_dir(uint32_t flags)
- {
- int rc = -EINVAL;
- if (flags & CAM_MEM_FLAG_HW_READ_ONLY)
- rc = DMA_TO_DEVICE;
- else if (flags & CAM_MEM_FLAG_HW_WRITE_ONLY)
- rc = DMA_FROM_DEVICE;
- else if (flags & CAM_MEM_FLAG_HW_READ_WRITE)
- rc = DMA_BIDIRECTIONAL;
- else if (flags & CAM_MEM_FLAG_PROTECTED_MODE)
- rc = DMA_BIDIRECTIONAL;
- return rc;
- }
- static int cam_mem_util_map_cpu_va(struct dma_buf *dmabuf, uintptr_t *vaddr, size_t *len)
- {
- int rc = 0;
- /*
- * dma_buf_begin_cpu_access() and dma_buf_end_cpu_access()
- * need to be called in pair to avoid stability issue.
- */
- rc = dma_buf_begin_cpu_access(dmabuf, DMA_BIDIRECTIONAL);
- if (rc) {
- CAM_ERR(CAM_MEM, "dma begin access failed rc=%d", rc);
- return rc;
- }
- rc = cam_compat_util_get_dmabuf_va(dmabuf, vaddr);
- if (rc) {
- CAM_ERR(CAM_MEM, "kernel vmap failed: rc = %d", rc);
- *len = 0;
- dma_buf_end_cpu_access(dmabuf, DMA_BIDIRECTIONAL);
- }
- else {
- *len = dmabuf->size;
- CAM_DBG(CAM_MEM, "vaddr = %llu, len = %zu", *vaddr, *len);
- }
- return rc;
- }
- static int cam_mem_util_unmap_cpu_va(struct dma_buf *dmabuf,
- uint64_t vaddr)
- {
- int rc = 0;
- if (!dmabuf || !vaddr) {
- CAM_ERR(CAM_MEM, "Invalid input args %pK %llX", dmabuf, vaddr);
- return -EINVAL;
- }
- cam_compat_util_put_dmabuf_va(dmabuf, (void *)vaddr);
- /*
- * dma_buf_begin_cpu_access() and
- * dma_buf_end_cpu_access() need to be called in pair
- * to avoid stability issue.
- */
- rc = dma_buf_end_cpu_access(dmabuf, DMA_BIDIRECTIONAL);
- if (rc) {
- CAM_ERR(CAM_MEM, "Failed in end cpu access, dmabuf=%pK",
- dmabuf);
- return rc;
- }
- return rc;
- }
- static int cam_mem_mgr_create_debug_fs(void)
- {
- int rc = 0;
- struct dentry *dbgfileptr = NULL;
- if (!cam_debugfs_available() || g_cam_mem_mgr_debug.dentry)
- return 0;
- rc = cam_debugfs_create_subdir("memmgr", &dbgfileptr);
- if (rc) {
- CAM_ERR(CAM_MEM, "DebugFS could not create directory!");
- rc = -ENOENT;
- goto end;
- }
- g_cam_mem_mgr_debug.dentry = dbgfileptr;
- debugfs_create_bool("alloc_profile_enable", 0644, g_cam_mem_mgr_debug.dentry,
- &g_cam_mem_mgr_debug.alloc_profile_enable);
- debugfs_create_bool("override_cpu_access_dir", 0644, g_cam_mem_mgr_debug.dentry,
- &g_cam_mem_mgr_debug.override_cpu_access_dir);
- end:
- return rc;
- }
- int cam_mem_mgr_init(void)
- {
- int i;
- int bitmap_size;
- int rc = 0;
- if (atomic_read(&cam_mem_mgr_state))
- return 0;
- memset(tbl.bufq, 0, sizeof(tbl.bufq));
- if (cam_smmu_need_force_alloc_cached(&tbl.force_cache_allocs)) {
- CAM_ERR(CAM_MEM, "Error in getting force cache alloc flag");
- return -EINVAL;
- }
- tbl.need_shared_buffer_padding = cam_smmu_need_shared_buffer_padding();
- #if IS_REACHABLE(CONFIG_DMABUF_HEAPS)
- rc = cam_mem_mgr_get_dma_heaps();
- if (rc) {
- CAM_ERR(CAM_MEM, "Failed in getting dma heaps rc=%d", rc);
- return rc;
- }
- #endif
- bitmap_size = BITS_TO_LONGS(CAM_MEM_BUFQ_MAX) * sizeof(long);
- tbl.bitmap = kzalloc(bitmap_size, GFP_KERNEL);
- if (!tbl.bitmap) {
- rc = -ENOMEM;
- goto put_heaps;
- }
- tbl.bits = bitmap_size * BITS_PER_BYTE;
- bitmap_zero(tbl.bitmap, tbl.bits);
- /* We need to reserve slot 0 because 0 is invalid */
- set_bit(0, tbl.bitmap);
- for (i = 1; i < CAM_MEM_BUFQ_MAX; i++) {
- tbl.bufq[i].fd = -1;
- tbl.bufq[i].buf_handle = -1;
- cam_mem_mgr_reset_presil_params(i);
- }
- mutex_init(&tbl.m_lock);
- atomic_set(&cam_mem_mgr_state, CAM_MEM_MGR_INITIALIZED);
- cam_mem_mgr_create_debug_fs();
- cam_common_register_mini_dump_cb(cam_mem_mgr_mini_dump_cb,
- "cam_mem", NULL);
- rc = cam_smmu_driver_init(&tbl.csf_version, &tbl.max_hdls_supported);
- if (rc)
- goto clean_bitmap_and_mutex;
- if (!tbl.max_hdls_supported) {
- CAM_ERR(CAM_MEM, "Invalid number of supported handles");
- rc = -EINVAL;
- goto clean_bitmap_and_mutex;
- }
- tbl.max_hdls_info_size = sizeof(struct cam_mem_buf_hw_hdl_info) *
- tbl.max_hdls_supported;
- /* Index 0 is reserved as invalid slot */
- for (i = 1; i < CAM_MEM_BUFQ_MAX; i++) {
- tbl.bufq[i].hdls_info = kzalloc(tbl.max_hdls_info_size, GFP_KERNEL);
- if (!tbl.bufq[i].hdls_info) {
- CAM_ERR(CAM_MEM, "Failed to allocate hdls array queue idx: %d", i);
- rc = -ENOMEM;
- goto free_hdls_info;
- }
- }
- return 0;
- free_hdls_info:
- for (--i; i > 0; i--) {
- kfree(tbl.bufq[i].hdls_info);
- tbl.bufq[i].hdls_info = NULL;
- }
- clean_bitmap_and_mutex:
- kfree(tbl.bitmap);
- tbl.bitmap = NULL;
- mutex_destroy(&tbl.m_lock);
- atomic_set(&cam_mem_mgr_state, CAM_MEM_MGR_UNINITIALIZED);
- put_heaps:
- #if IS_REACHABLE(CONFIG_DMABUF_HEAPS)
- cam_mem_mgr_put_dma_heaps();
- #endif
- return rc;
- }
- static int32_t cam_mem_get_slot(void)
- {
- int32_t idx;
- mutex_lock(&tbl.m_lock);
- idx = find_first_zero_bit(tbl.bitmap, tbl.bits);
- if (idx >= CAM_MEM_BUFQ_MAX || idx <= 0) {
- mutex_unlock(&tbl.m_lock);
- return -ENOMEM;
- }
- set_bit(idx, tbl.bitmap);
- tbl.bufq[idx].active = true;
- tbl.bufq[idx].release_deferred = false;
- CAM_GET_TIMESTAMP((tbl.bufq[idx].timestamp));
- mutex_init(&tbl.bufq[idx].q_lock);
- mutex_unlock(&tbl.m_lock);
- return idx;
- }
- static void cam_mem_put_slot(int32_t idx)
- {
- mutex_lock(&tbl.m_lock);
- mutex_lock(&tbl.bufq[idx].q_lock);
- tbl.bufq[idx].active = false;
- tbl.bufq[idx].release_deferred = false;
- tbl.bufq[idx].is_internal = false;
- memset(&tbl.bufq[idx].timestamp, 0, sizeof(struct timespec64));
- kref_init(&tbl.bufq[idx].krefcount);
- kref_init(&tbl.bufq[idx].urefcount);
- mutex_unlock(&tbl.bufq[idx].q_lock);
- mutex_destroy(&tbl.bufq[idx].q_lock);
- clear_bit(idx, tbl.bitmap);
- mutex_unlock(&tbl.m_lock);
- }
- static bool cam_mem_mgr_is_iova_info_updated_locked(
- struct cam_mem_buf_hw_hdl_info *hw_vaddr_info_arr,
- int32_t iommu_hdl)
- {
- int entry;
- struct cam_mem_buf_hw_hdl_info *vaddr_entry;
- /* validate hdl for entry idx */
- if (!cam_mem_mgr_get_hwva_entry_idx(iommu_hdl, &entry))
- return false;
- vaddr_entry = &hw_vaddr_info_arr[entry];
- if (vaddr_entry->valid_mapping &&
- vaddr_entry->iommu_hdl == iommu_hdl)
- return true;
- return false;
- }
- static void cam_mem_mgr_update_iova_info_locked(
- struct cam_mem_buf_hw_hdl_info *hw_vaddr_info_arr,
- dma_addr_t vaddr, int32_t iommu_hdl, size_t len,
- bool valid_mapping, struct kref *ref_count)
- {
- int entry;
- struct cam_mem_buf_hw_hdl_info *vaddr_entry;
- /* validate hdl for entry idx */
- if (!cam_mem_mgr_get_hwva_entry_idx(iommu_hdl, &entry))
- return;
- vaddr_entry = &hw_vaddr_info_arr[entry];
- vaddr_entry->vaddr = vaddr;
- vaddr_entry->iommu_hdl = iommu_hdl;
- vaddr_entry->addr_updated = true;
- vaddr_entry->valid_mapping = valid_mapping;
- vaddr_entry->len = len;
- vaddr_entry->ref_count = ref_count;
- }
- /* Utility to be invoked with bufq entry lock held */
- static int cam_mem_mgr_try_retrieving_hwva_locked(
- int idx, int32_t mmu_handle, dma_addr_t *iova_ptr, size_t *len_ptr,
- struct list_head *buf_tracker)
- {
- int rc = -EINVAL, entry;
- struct cam_mem_buf_hw_hdl_info *hdl_info = NULL;
- /* Check for valid entry */
- if (cam_mem_mgr_get_hwva_entry_idx(mmu_handle, &entry)) {
- hdl_info = &tbl.bufq[idx].hdls_info[entry];
- /* Ensure we are picking a valid entry */
- if ((hdl_info->iommu_hdl == mmu_handle) && (hdl_info->addr_updated)) {
- *iova_ptr = hdl_info->vaddr;
- *len_ptr = hdl_info->len;
- if (buf_tracker)
- cam_smmu_add_buf_to_track_list(tbl.bufq[idx].fd,
- tbl.bufq[idx].i_ino, &hdl_info->ref_count, buf_tracker,
- GET_SMMU_TABLE_IDX(mmu_handle));
- rc = 0;
- }
- }
- return rc;
- }
- int cam_mem_get_io_buf(int32_t buf_handle, int32_t mmu_handle,
- dma_addr_t *iova_ptr, size_t *len_ptr, uint32_t *flags,
- struct list_head *buf_tracker)
- {
- int rc = 0, idx;
- bool retrieved_iova = false;
- struct kref *ref_count;
- *len_ptr = 0;
- if (!atomic_read(&cam_mem_mgr_state)) {
- CAM_ERR(CAM_MEM, "failed. mem_mgr not initialized");
- return -EINVAL;
- }
- idx = CAM_MEM_MGR_GET_HDL_IDX(buf_handle);
- if (idx >= CAM_MEM_BUFQ_MAX || idx <= 0)
- return -ENOENT;
- if (!tbl.bufq[idx].active) {
- CAM_ERR(CAM_MEM, "Buffer at idx=%d is already unmapped,",
- idx);
- return -EAGAIN;
- }
- mutex_lock(&tbl.bufq[idx].q_lock);
- if (buf_handle != tbl.bufq[idx].buf_handle) {
- rc = -EINVAL;
- goto err;
- }
- if (flags)
- *flags = tbl.bufq[idx].flags;
- /* Try retrieving iova if saved previously */
- rc = cam_mem_mgr_try_retrieving_hwva_locked(idx, mmu_handle, iova_ptr, len_ptr,
- buf_tracker);
- if (!rc) {
- retrieved_iova = true;
- goto end;
- }
- if (CAM_MEM_MGR_IS_SECURE_HDL(buf_handle))
- rc = cam_smmu_get_stage2_iova(mmu_handle, tbl.bufq[idx].fd, tbl.bufq[idx].dma_buf,
- iova_ptr, len_ptr, buf_tracker, &ref_count);
- else
- rc = cam_smmu_get_iova(mmu_handle, tbl.bufq[idx].fd, tbl.bufq[idx].dma_buf,
- iova_ptr, len_ptr, buf_tracker, &ref_count);
- if (rc) {
- CAM_ERR(CAM_MEM,
- "failed to find buf_hdl:0x%x, mmu_hdl: 0x%x for fd:%d i_ino:%lu",
- buf_handle, mmu_handle, tbl.bufq[idx].fd, tbl.bufq[idx].i_ino);
- goto err;
- }
- /* Save iova in bufq for future use */
- cam_mem_mgr_update_iova_info_locked(tbl.bufq[idx].hdls_info,
- *iova_ptr, mmu_handle, *len_ptr, false, ref_count);
- end:
- CAM_DBG(CAM_MEM,
- "handle:0x%x fd:%d i_ino:%lu iova_ptr:0x%lx len_ptr:%lu retrieved from bufq: %s",
- mmu_handle, tbl.bufq[idx].fd, tbl.bufq[idx].i_ino, *iova_ptr, *len_ptr,
- CAM_BOOL_TO_YESNO(retrieved_iova));
- err:
- mutex_unlock(&tbl.bufq[idx].q_lock);
- return rc;
- }
- EXPORT_SYMBOL(cam_mem_get_io_buf);
- int cam_mem_get_cpu_buf(int32_t buf_handle, uintptr_t *vaddr_ptr, size_t *len)
- {
- int idx;
- if (!atomic_read(&cam_mem_mgr_state)) {
- CAM_ERR(CAM_MEM, "failed. mem_mgr not initialized");
- return -EINVAL;
- }
- if (!buf_handle || !vaddr_ptr || !len)
- return -EINVAL;
- idx = CAM_MEM_MGR_GET_HDL_IDX(buf_handle);
- if (idx >= CAM_MEM_BUFQ_MAX || idx <= 0)
- return -EINVAL;
- if (!tbl.bufq[idx].active) {
- CAM_ERR(CAM_MEM, "Buffer at idx=%d is already unmapped,",
- idx);
- return -EPERM;
- }
- if (buf_handle != tbl.bufq[idx].buf_handle) {
- CAM_ERR(CAM_MEM, "idx: %d Invalid buf handle %d",
- idx, buf_handle);
- return -EINVAL;
- }
- if (!(tbl.bufq[idx].flags & CAM_MEM_FLAG_KMD_ACCESS)) {
- CAM_ERR(CAM_MEM, "idx: %d Invalid flag 0x%x",
- idx, tbl.bufq[idx].flags);
- return -EINVAL;
- }
- if (tbl.bufq[idx].kmdvaddr && kref_get_unless_zero(&tbl.bufq[idx].krefcount)) {
- *vaddr_ptr = tbl.bufq[idx].kmdvaddr;
- *len = tbl.bufq[idx].len;
- } else {
- CAM_ERR(CAM_MEM, "No KMD access requested, kmdvddr= %p, idx= %d, buf_handle= %d",
- tbl.bufq[idx].kmdvaddr, idx, buf_handle);
- return -EINVAL;
- }
- return 0;
- }
- EXPORT_SYMBOL(cam_mem_get_cpu_buf);
- int cam_mem_mgr_cache_ops(struct cam_mem_cache_ops_cmd *cmd)
- {
- int rc = 0, idx;
- uint32_t cache_dir;
- unsigned long dmabuf_flag = 0;
- if (!atomic_read(&cam_mem_mgr_state)) {
- CAM_ERR(CAM_MEM, "failed. mem_mgr not initialized");
- return -EINVAL;
- }
- if (!cmd)
- return -EINVAL;
- idx = CAM_MEM_MGR_GET_HDL_IDX(cmd->buf_handle);
- if (idx >= CAM_MEM_BUFQ_MAX || idx <= 0)
- return -EINVAL;
- mutex_lock(&tbl.m_lock);
- if (!test_bit(idx, tbl.bitmap)) {
- CAM_ERR(CAM_MEM, "Buffer at idx=%d is already unmapped,",
- idx);
- mutex_unlock(&tbl.m_lock);
- return -EINVAL;
- }
- mutex_lock(&tbl.bufq[idx].q_lock);
- mutex_unlock(&tbl.m_lock);
- if (cmd->buf_handle != tbl.bufq[idx].buf_handle) {
- rc = -EINVAL;
- goto end;
- }
- rc = dma_buf_get_flags(tbl.bufq[idx].dma_buf, &dmabuf_flag);
- if (rc) {
- CAM_ERR(CAM_MEM, "cache get flags failed %d", rc);
- goto end;
- }
- #if IS_REACHABLE(CONFIG_DMABUF_HEAPS)
- CAM_DBG(CAM_MEM, "Calling dmap buf APIs for cache operations");
- cache_dir = DMA_BIDIRECTIONAL;
- #else
- if (dmabuf_flag & ION_FLAG_CACHED) {
- switch (cmd->mem_cache_ops) {
- case CAM_MEM_CLEAN_CACHE:
- cache_dir = DMA_TO_DEVICE;
- break;
- case CAM_MEM_INV_CACHE:
- cache_dir = DMA_FROM_DEVICE;
- break;
- case CAM_MEM_CLEAN_INV_CACHE:
- cache_dir = DMA_BIDIRECTIONAL;
- break;
- default:
- CAM_ERR(CAM_MEM,
- "invalid cache ops :%d", cmd->mem_cache_ops);
- rc = -EINVAL;
- goto end;
- }
- } else {
- CAM_DBG(CAM_MEM, "BUF is not cached");
- goto end;
- }
- #endif
- rc = dma_buf_begin_cpu_access(tbl.bufq[idx].dma_buf,
- (cmd->mem_cache_ops == CAM_MEM_CLEAN_INV_CACHE) ?
- DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
- if (rc) {
- CAM_ERR(CAM_MEM, "dma begin access failed rc=%d", rc);
- goto end;
- }
- rc = dma_buf_end_cpu_access(tbl.bufq[idx].dma_buf,
- cache_dir);
- if (rc) {
- CAM_ERR(CAM_MEM, "dma end access failed rc=%d", rc);
- goto end;
- }
- end:
- mutex_unlock(&tbl.bufq[idx].q_lock);
- return rc;
- }
- EXPORT_SYMBOL(cam_mem_mgr_cache_ops);
- int cam_mem_mgr_cpu_access_op(struct cam_mem_cpu_access_op *cmd)
- {
- int rc = 0, idx;
- uint32_t direction;
- if (!atomic_read(&cam_mem_mgr_state)) {
- CAM_ERR(CAM_MEM, "failed. mem_mgr not initialized");
- return -EINVAL;
- }
- if (!cmd) {
- CAM_ERR(CAM_MEM, "Invalid cmd");
- return -EINVAL;
- }
- idx = CAM_MEM_MGR_GET_HDL_IDX(cmd->buf_handle);
- if (idx >= CAM_MEM_BUFQ_MAX || idx <= 0) {
- CAM_ERR(CAM_MEM, "Invalid idx=%d, buf_handle 0x%x, access=0x%x",
- idx, cmd->buf_handle, cmd->access);
- return -EINVAL;
- }
- mutex_lock(&tbl.m_lock);
- if (!test_bit(idx, tbl.bitmap)) {
- CAM_ERR(CAM_MEM, "Buffer at idx=%d is already freed/unmapped", idx);
- mutex_unlock(&tbl.m_lock);
- return -EINVAL;
- }
- mutex_lock(&tbl.bufq[idx].q_lock);
- mutex_unlock(&tbl.m_lock);
- if (cmd->buf_handle != tbl.bufq[idx].buf_handle) {
- CAM_ERR(CAM_MEM,
- "Buffer at idx=%d is different incoming handle 0x%x, actual handle 0x%x",
- idx, cmd->buf_handle, tbl.bufq[idx].buf_handle);
- rc = -EINVAL;
- goto end;
- }
- CAM_DBG(CAM_MEM, "buf_handle=0x%x, access=0x%x, access_type=0x%x, override_access=%d",
- cmd->buf_handle, cmd->access, cmd->access_type,
- g_cam_mem_mgr_debug.override_cpu_access_dir);
- if (cmd->access_type & CAM_MEM_CPU_ACCESS_READ &&
- cmd->access_type & CAM_MEM_CPU_ACCESS_WRITE) {
- direction = DMA_BIDIRECTIONAL;
- } else if (cmd->access_type & CAM_MEM_CPU_ACCESS_READ) {
- direction = DMA_FROM_DEVICE;
- } else if (cmd->access_type & CAM_MEM_CPU_ACCESS_WRITE) {
- direction = DMA_TO_DEVICE;
- } else {
- direction = DMA_BIDIRECTIONAL;
- CAM_WARN(CAM_MEM,
- "Invalid access type buf_handle=0x%x, access=0x%x, access_type=0x%x",
- cmd->buf_handle, cmd->access, cmd->access_type);
- }
- if (g_cam_mem_mgr_debug.override_cpu_access_dir)
- direction = DMA_BIDIRECTIONAL;
- if (cmd->access & CAM_MEM_BEGIN_CPU_ACCESS) {
- rc = dma_buf_begin_cpu_access(tbl.bufq[idx].dma_buf, direction);
- if (rc) {
- CAM_ERR(CAM_MEM,
- "dma begin cpu access failed rc=%d, buf_handle=0x%x, access=0x%x, access_type=0x%x",
- rc, cmd->buf_handle, cmd->access, cmd->access_type);
- goto end;
- }
- }
- if (cmd->access & CAM_MEM_END_CPU_ACCESS) {
- rc = dma_buf_end_cpu_access(tbl.bufq[idx].dma_buf, direction);
- if (rc) {
- CAM_ERR(CAM_MEM,
- "dma end cpu access failed rc=%d, buf_handle=0x%x, access=0x%x, access_type=0x%x",
- rc, cmd->buf_handle, cmd->access, cmd->access_type);
- goto end;
- }
- }
- end:
- mutex_unlock(&tbl.bufq[idx].q_lock);
- return rc;
- }
- EXPORT_SYMBOL(cam_mem_mgr_cpu_access_op);
- #if IS_REACHABLE(CONFIG_DMABUF_HEAPS)
- #define CAM_MAX_VMIDS 4
- static void cam_mem_mgr_put_dma_heaps(void)
- {
- CAM_DBG(CAM_MEM, "Releasing DMA Buf heaps usage");
- }
- static int cam_mem_mgr_get_dma_heaps(void)
- {
- int rc = 0;
- tbl.system_heap = NULL;
- tbl.system_movable_heap = NULL;
- tbl.system_uncached_heap = NULL;
- tbl.camera_heap = NULL;
- tbl.camera_uncached_heap = NULL;
- tbl.secure_display_heap = NULL;
- tbl.ubwc_p_heap = NULL;
- tbl.ubwc_p_movable_heap = NULL;
- tbl.system_heap = dma_heap_find("qcom,system");
- if (IS_ERR_OR_NULL(tbl.system_heap)) {
- rc = PTR_ERR(tbl.system_heap);
- CAM_ERR(CAM_MEM, "qcom system heap not found, rc=%d", rc);
- tbl.system_heap = NULL;
- goto put_heaps;
- }
- tbl.system_movable_heap = dma_heap_find("qcom,system-movable");
- if (IS_ERR_OR_NULL(tbl.system_movable_heap)) {
- rc = PTR_ERR(tbl.system_movable_heap);
- CAM_DBG(CAM_MEM, "qcom system heap not found, rc=%d", rc);
- tbl.system_movable_heap = NULL;
- /* not fatal error, we can fallback to system heap */
- }
- tbl.system_uncached_heap = dma_heap_find("qcom,system-uncached");
- if (IS_ERR_OR_NULL(tbl.system_uncached_heap)) {
- if (tbl.force_cache_allocs) {
- /* optional, we anyway do not use uncached */
- CAM_DBG(CAM_MEM,
- "qcom system-uncached heap not found, err=%d",
- PTR_ERR(tbl.system_uncached_heap));
- tbl.system_uncached_heap = NULL;
- } else {
- /* fatal, must need uncached heaps */
- rc = PTR_ERR(tbl.system_uncached_heap);
- CAM_ERR(CAM_MEM,
- "qcom system-uncached heap not found, rc=%d",
- rc);
- tbl.system_uncached_heap = NULL;
- goto put_heaps;
- }
- }
- tbl.ubwc_p_heap = dma_heap_find("qcom,ubwcp");
- if (IS_ERR_OR_NULL(tbl.ubwc_p_heap)) {
- CAM_DBG(CAM_MEM, "qcom ubwcp heap not found, err=%d", PTR_ERR(tbl.ubwc_p_heap));
- tbl.ubwc_p_heap = NULL;
- }
- tbl.ubwc_p_movable_heap = dma_heap_find("qcom,ubwcp-movable");
- if (IS_ERR_OR_NULL(tbl.ubwc_p_movable_heap)) {
- CAM_DBG(CAM_MEM, "qcom ubwcp movable heap not found, err=%d",
- PTR_ERR(tbl.ubwc_p_movable_heap));
- tbl.ubwc_p_movable_heap = NULL;
- }
- tbl.secure_display_heap = dma_heap_find("qcom,display");
- if (IS_ERR_OR_NULL(tbl.secure_display_heap)) {
- rc = PTR_ERR(tbl.secure_display_heap);
- CAM_ERR(CAM_MEM, "qcom,display heap not found, rc=%d",
- rc);
- tbl.secure_display_heap = NULL;
- goto put_heaps;
- }
- tbl.camera_heap = dma_heap_find("qcom,camera");
- if (IS_ERR_OR_NULL(tbl.camera_heap)) {
- /* optional heap, not a fatal error */
- CAM_DBG(CAM_MEM, "qcom camera heap not found, err=%d",
- PTR_ERR(tbl.camera_heap));
- tbl.camera_heap = NULL;
- }
- tbl.camera_uncached_heap = dma_heap_find("qcom,camera-uncached");
- if (IS_ERR_OR_NULL(tbl.camera_uncached_heap)) {
- /* optional heap, not a fatal error */
- CAM_DBG(CAM_MEM, "qcom camera heap not found, err=%d",
- PTR_ERR(tbl.camera_uncached_heap));
- tbl.camera_uncached_heap = NULL;
- }
- CAM_INFO(CAM_MEM,
- "Heaps : system=%pK %pK, system_uncached=%pK, camera=%pK, camera-uncached=%pK, secure_display=%pK, ubwc_p=%pK %pK",
- tbl.system_heap, tbl.system_movable_heap, tbl.system_uncached_heap,
- tbl.camera_heap, tbl.camera_uncached_heap,
- tbl.secure_display_heap, tbl.ubwc_p_heap, tbl.ubwc_p_movable_heap);
- return 0;
- put_heaps:
- cam_mem_mgr_put_dma_heaps();
- return rc;
- }
- int cam_mem_mgr_check_for_supported_heaps(uint64_t *heap_mask)
- {
- uint64_t heap_caps = 0;
- if (!heap_mask)
- return -EINVAL;
- if (tbl.ubwc_p_heap)
- heap_caps |= CAM_REQ_MGR_MEM_UBWC_P_HEAP_SUPPORTED;
- if ((tbl.camera_heap) || (tbl.camera_uncached_heap))
- heap_caps |= CAM_REQ_MGR_MEM_CAMERA_HEAP_SUPPORTED;
- *heap_mask = heap_caps;
- return 0;
- }
- static int cam_mem_util_get_dma_buf(size_t len,
- unsigned int cam_flags,
- enum cam_mem_mgr_allocator alloc_type,
- struct dma_buf **buf,
- unsigned long *i_ino)
- {
- int rc = 0;
- struct dma_heap *heap = NULL, *try_heap = NULL;
- struct timespec64 ts1, ts2;
- long microsec = 0;
- bool use_cached_heap = false;
- struct mem_buf_lend_kernel_arg arg;
- int vmids[CAM_MAX_VMIDS];
- int perms[CAM_MAX_VMIDS];
- int num_vmids = 0;
- if (!buf) {
- CAM_ERR(CAM_MEM, "Invalid params");
- return -EINVAL;
- }
- if (g_cam_mem_mgr_debug.alloc_profile_enable)
- CAM_GET_TIMESTAMP(ts1);
- if ((cam_flags & CAM_MEM_FLAG_CACHE) ||
- (tbl.force_cache_allocs &&
- (!(cam_flags & CAM_MEM_FLAG_PROTECTED_MODE)))) {
- CAM_DBG(CAM_MEM,
- "Using CACHED heap, cam_flags=0x%x, force_cache_allocs=%d",
- cam_flags, tbl.force_cache_allocs);
- use_cached_heap = true;
- } else if (cam_flags & CAM_MEM_FLAG_PROTECTED_MODE) {
- use_cached_heap = true;
- CAM_DBG(CAM_MEM,
- "Using CACHED heap for secure, cam_flags=0x%x, force_cache_allocs=%d",
- cam_flags, tbl.force_cache_allocs);
- } else {
- use_cached_heap = false;
- if (!tbl.system_uncached_heap && !tbl.camera_uncached_heap) {
- CAM_ERR(CAM_MEM,
- "Using UNCACHED heap not supported, cam_flags=0x%x, force_cache_allocs=%d",
- cam_flags, tbl.force_cache_allocs);
- return -EINVAL;
- }
- }
- if (cam_flags & CAM_MEM_FLAG_PROTECTED_MODE) {
- if (IS_CSF25(tbl.csf_version.arch_ver, tbl.csf_version.max_ver)) {
- heap = tbl.system_heap;
- len = cam_align_dma_buf_size(len);
- } else {
- heap = tbl.secure_display_heap;
- vmids[num_vmids] = VMID_CP_CAMERA;
- perms[num_vmids] = PERM_READ | PERM_WRITE;
- num_vmids++;
- }
- if (cam_flags & CAM_MEM_FLAG_CDSP_OUTPUT) {
- CAM_DBG(CAM_MEM, "Secure mode CDSP flags");
- vmids[num_vmids] = VMID_CP_CDSP;
- perms[num_vmids] = PERM_READ | PERM_WRITE;
- num_vmids++;
- }
- } else if (cam_flags & CAM_MEM_FLAG_EVA_NOPIXEL) {
- heap = tbl.secure_display_heap;
- vmids[num_vmids] = VMID_CP_NON_PIXEL;
- perms[num_vmids] = PERM_READ | PERM_WRITE;
- num_vmids++;
- } else if (cam_flags & CAM_MEM_FLAG_UBWC_P_HEAP) {
- if (!tbl.ubwc_p_heap) {
- CAM_ERR(CAM_MEM, "ubwc-p heap is not available, can't allocate");
- return -EINVAL;
- }
- if (tbl.ubwc_p_movable_heap && (alloc_type == CAM_MEMMGR_ALLOC_USER))
- heap = tbl.ubwc_p_movable_heap;
- else
- heap = tbl.ubwc_p_heap;
- CAM_DBG(CAM_MEM, "Allocating from ubwc-p heap %pK, size=%d, flags=0x%x",
- heap, len, cam_flags);
- } else if (use_cached_heap) {
- /*
- * The default scheme is to try allocating from the camera heap
- * if available; if not, try for the system heap. Userland can also select
- * to pick a specific heap for allocation; this will deviate from the
- * default selection scheme.
- *
- */
- if (!(cam_flags & CAM_MEM_FLAG_USE_SYS_HEAP_ONLY))
- try_heap = tbl.camera_heap;
- if (!(cam_flags & CAM_MEM_FLAG_USE_CAMERA_HEAP_ONLY)) {
- if (tbl.system_movable_heap && (alloc_type == CAM_MEMMGR_ALLOC_USER))
- heap = tbl.system_movable_heap;
- else
- heap = tbl.system_heap;
- }
- } else {
- if (!(cam_flags & CAM_MEM_FLAG_USE_SYS_HEAP_ONLY))
- try_heap = tbl.camera_uncached_heap;
- if (!(cam_flags & CAM_MEM_FLAG_USE_CAMERA_HEAP_ONLY))
- heap = tbl.system_uncached_heap;
- }
- CAM_DBG(CAM_MEM, "Using heaps : try=%pK, heap=%pK", try_heap, heap);
- *buf = NULL;
- if (!try_heap && !heap) {
- CAM_ERR(CAM_MEM,
- "No heap available for allocation, can't allocate flag: 0x%x",
- cam_flags);
- return -EINVAL;
- }
- if (try_heap) {
- *buf = dma_heap_buffer_alloc(try_heap, len, O_RDWR, 0);
- if (IS_ERR(*buf)) {
- CAM_WARN(CAM_MEM,
- "Failed in allocating from try heap, heap=%pK, len=%zu, err=%d",
- try_heap, len, PTR_ERR(*buf));
- *buf = NULL;
- }
- }
- if (*buf == NULL) {
- *buf = dma_heap_buffer_alloc(heap, len, O_RDWR, 0);
- if (IS_ERR(*buf)) {
- rc = PTR_ERR(*buf);
- CAM_ERR(CAM_MEM,
- "Failed in allocating from heap, heap=%pK, len=%zu, err=%d",
- heap, len, rc);
- *buf = NULL;
- return rc;
- }
- }
- *i_ino = file_inode((*buf)->file)->i_ino;
- if (((cam_flags & CAM_MEM_FLAG_PROTECTED_MODE) &&
- !IS_CSF25(tbl.csf_version.arch_ver, tbl.csf_version.max_ver)) ||
- (cam_flags & CAM_MEM_FLAG_EVA_NOPIXEL)) {
- if (num_vmids >= CAM_MAX_VMIDS) {
- CAM_ERR(CAM_MEM, "Insufficient array size for vmids %d", num_vmids);
- rc = -EINVAL;
- goto end;
- }
- arg.nr_acl_entries = num_vmids;
- arg.vmids = vmids;
- arg.perms = perms;
- rc = mem_buf_lend(*buf, &arg);
- if (rc) {
- CAM_ERR(CAM_MEM,
- "Failed in buf lend rc=%d, buf=%pK, vmids [0]=0x%x, [1]=0x%x, [2]=0x%x",
- rc, *buf, vmids[0], vmids[1], vmids[2]);
- goto end;
- }
- }
- CAM_DBG(CAM_MEM, "Allocate success, len=%zu, *buf=%pK, i_ino=%lu", len, *buf, *i_ino);
- if (g_cam_mem_mgr_debug.alloc_profile_enable) {
- CAM_GET_TIMESTAMP(ts2);
- CAM_GET_TIMESTAMP_DIFF_IN_MICRO(ts1, ts2, microsec);
- trace_cam_log_event("IONAllocProfile", "size and time in micro",
- len, microsec);
- }
- return rc;
- end:
- dma_buf_put(*buf);
- return rc;
- }
- #else
- bool cam_mem_mgr_ubwc_p_heap_supported(void)
- {
- return false;
- }
- static int cam_mem_util_get_dma_buf(size_t len,
- unsigned int cam_flags,
- enum cam_mem_mgr_allocator alloc_type,
- struct dma_buf **buf,
- unsigned long *i_ino)
- {
- int rc = 0;
- unsigned int heap_id;
- int32_t ion_flag = 0;
- struct timespec64 ts1, ts2;
- long microsec = 0;
- if (!buf) {
- CAM_ERR(CAM_MEM, "Invalid params");
- return -EINVAL;
- }
- if (cam_flags & CAM_MEM_FLAG_UBWC_P_HEAP) {
- CAM_ERR(CAM_MEM, "ubwcp heap not supported");
- return -EINVAL;
- }
- if (g_cam_mem_mgr_debug.alloc_profile_enable)
- CAM_GET_TIMESTAMP(ts1);
- if ((cam_flags & CAM_MEM_FLAG_PROTECTED_MODE) &&
- (cam_flags & CAM_MEM_FLAG_CDSP_OUTPUT)) {
- heap_id = ION_HEAP(ION_SECURE_DISPLAY_HEAP_ID);
- ion_flag |=
- ION_FLAG_SECURE | ION_FLAG_CP_CAMERA | ION_FLAG_CP_CDSP;
- } else if (cam_flags & CAM_MEM_FLAG_PROTECTED_MODE) {
- heap_id = ION_HEAP(ION_SECURE_DISPLAY_HEAP_ID);
- ion_flag |= ION_FLAG_SECURE | ION_FLAG_CP_CAMERA;
- } else {
- heap_id = ION_HEAP(ION_SYSTEM_HEAP_ID) |
- ION_HEAP(ION_CAMERA_HEAP_ID);
- }
- if (cam_flags & CAM_MEM_FLAG_CACHE)
- ion_flag |= ION_FLAG_CACHED;
- else
- ion_flag &= ~ION_FLAG_CACHED;
- if (tbl.force_cache_allocs && (!(ion_flag & ION_FLAG_SECURE)))
- ion_flag |= ION_FLAG_CACHED;
- *buf = ion_alloc(len, heap_id, ion_flag);
- if (IS_ERR_OR_NULL(*buf))
- return -ENOMEM;
- *i_ino = file_inode((*buf)->file)->i_ino;
- if (g_cam_mem_mgr_debug.alloc_profile_enable) {
- CAM_GET_TIMESTAMP(ts2);
- CAM_GET_TIMESTAMP_DIFF_IN_MICRO(ts1, ts2, microsec);
- trace_cam_log_event("IONAllocProfile", "size and time in micro",
- len, microsec);
- }
- return rc;
- }
- #endif
- static int cam_mem_util_buffer_alloc(size_t len, uint32_t flags,
- struct dma_buf **dmabuf,
- int *fd,
- unsigned long *i_ino)
- {
- int rc;
- rc = cam_mem_util_get_dma_buf(len, flags, CAM_MEMMGR_ALLOC_USER, dmabuf, i_ino);
- if (rc) {
- CAM_ERR(CAM_MEM,
- "Error allocating dma buf : len=%llu, flags=0x%x",
- len, flags);
- return rc;
- }
- /*
- * increment the ref count so that ref count becomes 2 here
- * when we close fd, refcount becomes 1 and when we do
- * dmap_put_buf, ref count becomes 0 and memory will be freed.
- */
- get_dma_buf(*dmabuf);
- *fd = dma_buf_fd(*dmabuf, O_CLOEXEC);
- if (*fd < 0) {
- CAM_ERR(CAM_MEM, "get fd fail, *fd=%d", *fd);
- rc = -EINVAL;
- goto put_buf;
- }
- CAM_DBG(CAM_MEM, "Alloc success : len=%zu, *dmabuf=%pK, fd=%d, i_ino=%lu",
- len, *dmabuf, *fd, *i_ino);
- return rc;
- put_buf:
- dma_buf_put(*dmabuf);
- return rc;
- }
- static int cam_mem_util_check_alloc_flags(struct cam_mem_mgr_alloc_cmd_v2 *cmd)
- {
- if (cmd->num_hdl > CAM_MEM_MMU_MAX_HANDLE) {
- CAM_ERR(CAM_MEM, "Num of mmu hdl exceeded maximum(%d)",
- CAM_MEM_MMU_MAX_HANDLE);
- return -EINVAL;
- }
- if (cmd->flags & CAM_MEM_FLAG_PROTECTED_MODE &&
- cmd->flags & CAM_MEM_FLAG_KMD_ACCESS) {
- CAM_ERR(CAM_MEM, "Kernel mapping in secure mode not allowed");
- return -EINVAL;
- }
- if ((cmd->flags & CAM_MEM_FLAG_EVA_NOPIXEL) &&
- (cmd->flags & CAM_MEM_FLAG_PROTECTED_MODE ||
- cmd->flags & CAM_MEM_FLAG_KMD_ACCESS)) {
- CAM_ERR(CAM_MEM,
- "Kernel mapping and secure mode not allowed in no pixel mode");
- return -EINVAL;
- }
- if (cmd->flags & CAM_MEM_FLAG_UBWC_P_HEAP &&
- (cmd->flags & CAM_MEM_FLAG_PROTECTED_MODE ||
- cmd->flags & CAM_MEM_FLAG_EVA_NOPIXEL ||
- cmd->flags & CAM_MEM_FLAG_KMD_ACCESS ||
- cmd->flags & CAM_MEM_FLAG_CMD_BUF_TYPE ||
- cmd->flags & CAM_MEM_FLAG_HW_SHARED_ACCESS ||
- cmd->flags & CAM_MEM_FLAG_HW_AND_CDM_OR_SHARED)) {
- CAM_ERR(CAM_MEM,
- "UBWC-P buffer not supported with this combinatation of flags 0x%x",
- cmd->flags);
- return -EINVAL;
- }
- return 0;
- }
- static int cam_mem_util_check_map_flags(struct cam_mem_mgr_map_cmd_v2 *cmd)
- {
- if (!cmd->flags) {
- CAM_ERR(CAM_MEM, "Invalid flags");
- return -EINVAL;
- }
- if (cmd->num_hdl > CAM_MEM_MMU_MAX_HANDLE) {
- CAM_ERR(CAM_MEM, "Num of mmu hdl %d exceeded maximum(%d)",
- cmd->num_hdl, CAM_MEM_MMU_MAX_HANDLE);
- return -EINVAL;
- }
- if (cmd->flags & CAM_MEM_FLAG_PROTECTED_MODE &&
- cmd->flags & CAM_MEM_FLAG_KMD_ACCESS) {
- CAM_ERR(CAM_MEM,
- "Kernel mapping in secure mode not allowed, flags=0x%x",
- cmd->flags);
- return -EINVAL;
- }
- if (cmd->flags & CAM_MEM_FLAG_HW_SHARED_ACCESS) {
- CAM_ERR(CAM_MEM,
- "Shared memory buffers are not allowed to be mapped");
- return -EINVAL;
- }
- return 0;
- }
- static int cam_mem_util_map_hw_va(uint32_t flags,
- int32_t *mmu_hdls,
- int32_t num_hdls,
- int fd,
- struct dma_buf *dmabuf,
- struct cam_mem_buf_hw_hdl_info *hw_vaddr_info_arr,
- size_t *len,
- enum cam_smmu_region_id region,
- bool is_internal)
- {
- int i;
- int rc = -1;
- int dir = cam_mem_util_get_dma_dir(flags);
- bool dis_delayed_unmap = false;
- dma_addr_t hw_vaddr;
- struct kref *ref_count;
- struct cam_mem_buf_hw_hdl_info *hdl_info = NULL;
- if (dir < 0) {
- CAM_ERR(CAM_MEM, "fail to map DMA direction, dir=%d", dir);
- return dir;
- }
- if (flags & CAM_MEM_FLAG_DISABLE_DELAYED_UNMAP)
- dis_delayed_unmap = true;
- CAM_DBG(CAM_MEM,
- "map_hw_va : fd = %d, flags = 0x%x, dir=%d, num_hdls=%d",
- fd, flags, dir, num_hdls);
- for (i = 0; i < num_hdls; i++) {
- if (cam_mem_mgr_is_iova_info_updated_locked(hw_vaddr_info_arr, mmu_hdls[i]))
- continue;
- /* If 36-bit enabled, check for ICP cmd buffers and map them within the shared region */
- if (cam_smmu_is_expanded_memory() &&
- cam_smmu_supports_shared_region(mmu_hdls[i]) &&
- ((flags & CAM_MEM_FLAG_CMD_BUF_TYPE) ||
- (flags & CAM_MEM_FLAG_HW_AND_CDM_OR_SHARED)))
- region = CAM_SMMU_REGION_SHARED;
- if (flags & CAM_MEM_FLAG_PROTECTED_MODE)
- rc = cam_smmu_map_stage2_iova(mmu_hdls[i], fd, dmabuf, dir, &hw_vaddr, len,
- &ref_count);
- else
- rc = cam_smmu_map_user_iova(mmu_hdls[i], fd, dmabuf, dis_delayed_unmap, dir,
- &hw_vaddr, len, region, is_internal, &ref_count);
- if (rc) {
- CAM_ERR(CAM_MEM,
- "Failed %s map to smmu, i=%d, fd=%d, dir=%d, mmu_hdl=%d, rc=%d",
- (flags & CAM_MEM_FLAG_PROTECTED_MODE) ? "" : "secured",
- i, fd, dir, mmu_hdls[i], rc);
- goto multi_map_fail;
- }
- /* cache hw va */
- cam_mem_mgr_update_iova_info_locked(hw_vaddr_info_arr,
- hw_vaddr, mmu_hdls[i], *len, true, ref_count);
- }
- return rc;
- multi_map_fail:
- for (i = 0; i < tbl.max_hdls_supported; i++) {
- if (!hw_vaddr_info_arr[i].valid_mapping)
- continue;
- hdl_info = &hw_vaddr_info_arr[i];
- if (flags & CAM_MEM_FLAG_PROTECTED_MODE)
- cam_smmu_unmap_stage2_iova(hdl_info->iommu_hdl, fd, dmabuf,
- false);
- else
- cam_smmu_unmap_user_iova(hdl_info->iommu_hdl, fd, dmabuf,
- CAM_SMMU_REGION_IO, false);
- }
- /* reset any updated entries */
- memset(hw_vaddr_info_arr, 0x0, tbl.max_hdls_info_size);
- return rc;
- }
- int cam_mem_mgr_alloc_and_map(struct cam_mem_mgr_alloc_cmd_v2 *cmd)
- {
- int rc, idx;
- struct dma_buf *dmabuf = NULL;
- int fd = -1;
- size_t len;
- uintptr_t kvaddr = 0;
- size_t klen;
- unsigned long i_ino = 0;
- if (!atomic_read(&cam_mem_mgr_state)) {
- CAM_ERR(CAM_MEM, "failed. mem_mgr not initialized");
- return -EINVAL;
- }
- if (!cmd) {
- CAM_ERR(CAM_MEM, " Invalid argument");
- return -EINVAL;
- }
- if (cmd->num_hdl > tbl.max_hdls_supported) {
- CAM_ERR(CAM_MEM, "Num of mmu hdl %d exceeded maximum(%d)",
- cmd->num_hdl, tbl.max_hdls_supported);
- return -EINVAL;
- }
- len = cmd->len;
- if (tbl.need_shared_buffer_padding &&
- (cmd->flags & CAM_MEM_FLAG_HW_SHARED_ACCESS)) {
- len += CAM_MEM_SHARED_BUFFER_PAD_4K;
- CAM_DBG(CAM_MEM, "Pad 4k size, actual %llu, allocating %zu",
- cmd->len, len);
- }
- rc = cam_mem_util_check_alloc_flags(cmd);
- if (rc) {
- CAM_ERR(CAM_MEM, "Invalid flags: flags = 0x%X, rc=%d",
- cmd->flags, rc);
- return rc;
- }
- rc = cam_mem_util_buffer_alloc(len, cmd->flags, &dmabuf, &fd, &i_ino);
- if (rc) {
- CAM_ERR(CAM_MEM,
- "Ion Alloc failed, len=%llu, align=%llu, flags=0x%x, num_hdl=%d",
- len, cmd->align, cmd->flags, cmd->num_hdl);
- cam_mem_mgr_print_tbl();
- return rc;
- }
- if (!dmabuf) {
- CAM_ERR(CAM_MEM,
- "Ion Alloc return NULL dmabuf! fd=%d, i_ino=%lu, len=%d", fd, i_ino, len);
- cam_mem_mgr_print_tbl();
- return rc;
- }
- idx = cam_mem_get_slot();
- if (idx < 0) {
- CAM_ERR(CAM_MEM, "Failed in getting mem slot, idx=%d", idx);
- rc = -ENOMEM;
- cam_mem_mgr_print_tbl();
- goto slot_fail;
- }
- if (cam_dma_buf_set_name(dmabuf, cmd->buf_name))
- CAM_ERR(CAM_MEM, "set dma buffer name(%s) failed", cmd->buf_name);
- if ((cmd->flags & CAM_MEM_FLAG_HW_READ_WRITE) ||
- (cmd->flags & CAM_MEM_FLAG_HW_SHARED_ACCESS) ||
- (cmd->flags & CAM_MEM_FLAG_PROTECTED_MODE)) {
- enum cam_smmu_region_id region;
- if (cmd->flags & CAM_MEM_FLAG_HW_READ_WRITE)
- region = CAM_SMMU_REGION_IO;
- if (cmd->flags & CAM_MEM_FLAG_HW_SHARED_ACCESS)
- region = CAM_SMMU_REGION_SHARED;
- if (cmd->flags & CAM_MEM_FLAG_PROTECTED_MODE)
- region = CAM_SMMU_REGION_IO;
- rc = cam_mem_util_map_hw_va(cmd->flags,
- cmd->mmu_hdls,
- cmd->num_hdl,
- fd,
- dmabuf,
- tbl.bufq[idx].hdls_info,
- &len,
- region,
- true);
- if (rc) {
- CAM_ERR(CAM_MEM,
- "Failed in map_hw_va len=%llu, flags=0x%x, fd=%d, region=%d, num_hdl=%d, rc=%d",
- len, cmd->flags,
- fd, region, cmd->num_hdl, rc);
- if (rc == -EALREADY) {
- if ((size_t)dmabuf->size != len)
- rc = -EBADR;
- cam_mem_mgr_print_tbl();
- }
- goto map_hw_fail;
- }
- }
- mutex_lock(&tbl.bufq[idx].q_lock);
- tbl.bufq[idx].fd = fd;
- tbl.bufq[idx].i_ino = i_ino;
- tbl.bufq[idx].dma_buf = NULL;
- tbl.bufq[idx].flags = cmd->flags;
- tbl.bufq[idx].buf_handle = GET_MEM_HANDLE(idx, fd);
- tbl.bufq[idx].is_internal = true;
- if (cmd->flags & CAM_MEM_FLAG_PROTECTED_MODE)
- CAM_MEM_MGR_SET_SECURE_HDL(tbl.bufq[idx].buf_handle, true);
- if (cmd->flags & CAM_MEM_FLAG_KMD_ACCESS) {
- rc = cam_mem_util_map_cpu_va(dmabuf, &kvaddr, &klen);
- if (rc) {
- CAM_ERR(CAM_MEM, "dmabuf: %pK mapping failed: %d",
- dmabuf, rc);
- goto map_kernel_fail;
- }
- }
- if (cmd->flags & CAM_MEM_FLAG_KMD_DEBUG_FLAG)
- tbl.dbg_buf_idx = idx;
- tbl.bufq[idx].kmdvaddr = kvaddr;
- tbl.bufq[idx].dma_buf = dmabuf;
- tbl.bufq[idx].len = len;
- tbl.bufq[idx].num_hdls = cmd->num_hdl;
- cam_mem_mgr_reset_presil_params(idx);
- tbl.bufq[idx].is_imported = false;
- if (cmd->flags & CAM_MEM_FLAG_KMD_ACCESS)
- kref_init(&tbl.bufq[idx].krefcount);
- kref_init(&tbl.bufq[idx].urefcount);
- tbl.bufq[idx].smmu_mapping_client = CAM_SMMU_MAPPING_USER;
- strscpy(tbl.bufq[idx].buf_name, cmd->buf_name, sizeof(tbl.bufq[idx].buf_name));
- mutex_unlock(&tbl.bufq[idx].q_lock);
- cmd->out.buf_handle = tbl.bufq[idx].buf_handle;
- cmd->out.fd = tbl.bufq[idx].fd;
- cmd->out.vaddr = 0;
- CAM_DBG(CAM_MEM,
- "fd=%d, flags=0x%x, num_hdl=%d, idx=%d, buf handle=%x, len=%zu, i_ino=%lu, name:%s",
- cmd->out.fd, cmd->flags, cmd->num_hdl, idx, cmd->out.buf_handle,
- tbl.bufq[idx].len, tbl.bufq[idx].i_ino, cmd->buf_name);
- return rc;
- map_kernel_fail:
- mutex_unlock(&tbl.bufq[idx].q_lock);
- map_hw_fail:
- cam_mem_put_slot(idx);
- slot_fail:
- dma_buf_put(dmabuf);
- return rc;
- }
- static bool cam_mem_util_is_map_internal(int32_t fd, unsigned i_ino)
- {
- uint32_t i;
- bool is_internal = false;
- mutex_lock(&tbl.m_lock);
- for_each_set_bit(i, tbl.bitmap, tbl.bits) {
- if ((tbl.bufq[i].fd == fd) && (tbl.bufq[i].i_ino == i_ino)) {
- is_internal = tbl.bufq[i].is_internal;
- break;
- }
- }
- mutex_unlock(&tbl.m_lock);
- return is_internal;
- }
- int cam_mem_mgr_map(struct cam_mem_mgr_map_cmd_v2 *cmd)
- {
- int32_t idx;
- int rc;
- struct dma_buf *dmabuf;
- size_t len = 0;
- bool is_internal = false;
- unsigned long i_ino;
- if (!atomic_read(&cam_mem_mgr_state)) {
- CAM_ERR(CAM_MEM, "failed. mem_mgr not initialized");
- return -EINVAL;
- }
- if (!cmd || (cmd->fd < 0)) {
- CAM_ERR(CAM_MEM, "Invalid argument");
- return -EINVAL;
- }
- if (cmd->num_hdl > tbl.max_hdls_supported) {
- CAM_ERR(CAM_MEM, "Num of mmu hdl %d exceeded maximum(%d)",
- cmd->num_hdl, tbl.max_hdls_supported);
- return -EINVAL;
- }
- rc = cam_mem_util_check_map_flags(cmd);
- if (rc) {
- CAM_ERR(CAM_MEM, "Invalid flags: flags = %X", cmd->flags);
- return rc;
- }
- dmabuf = dma_buf_get(cmd->fd);
- if (IS_ERR_OR_NULL((void *)(dmabuf))) {
- CAM_ERR(CAM_MEM, "Failed to import dma_buf fd");
- return -EINVAL;
- }
- i_ino = file_inode(dmabuf->file)->i_ino;
- is_internal = cam_mem_util_is_map_internal(cmd->fd, i_ino);
- idx = cam_mem_get_slot();
- if (idx < 0) {
- CAM_ERR(CAM_MEM, "Failed in getting mem slot, idx=%d, fd=%d",
- idx, cmd->fd);
- rc = -ENOMEM;
- cam_mem_mgr_print_tbl();
- goto slot_fail;
- }
- if (cam_dma_buf_set_name(dmabuf, cmd->buf_name))
- CAM_DBG(CAM_MEM, "Dma buffer (%s) busy", cmd->buf_name);
- if ((cmd->flags & CAM_MEM_FLAG_HW_READ_WRITE) ||
- (cmd->flags & CAM_MEM_FLAG_PROTECTED_MODE)) {
- rc = cam_mem_util_map_hw_va(cmd->flags,
- cmd->mmu_hdls,
- cmd->num_hdl,
- cmd->fd,
- dmabuf,
- tbl.bufq[idx].hdls_info,
- &len,
- CAM_SMMU_REGION_IO,
- is_internal);
- if (rc) {
- CAM_ERR(CAM_MEM,
- "Failed in map_hw_va, flags=0x%x, fd=%d, len=%llu, region=%d, num_hdl=%d, rc=%d",
- cmd->flags, cmd->fd, len,
- CAM_SMMU_REGION_IO, cmd->num_hdl, rc);
- if (rc == -EALREADY) {
- if ((size_t)dmabuf->size != len) {
- rc = -EBADR;
- cam_mem_mgr_print_tbl();
- }
- }
- goto map_fail;
- }
- }
- mutex_lock(&tbl.bufq[idx].q_lock);
- tbl.bufq[idx].fd = cmd->fd;
- tbl.bufq[idx].i_ino = i_ino;
- tbl.bufq[idx].dma_buf = NULL;
- tbl.bufq[idx].flags = cmd->flags;
- tbl.bufq[idx].buf_handle = GET_MEM_HANDLE(idx, cmd->fd);
- if (cmd->flags & CAM_MEM_FLAG_PROTECTED_MODE)
- CAM_MEM_MGR_SET_SECURE_HDL(tbl.bufq[idx].buf_handle, true);
- tbl.bufq[idx].kmdvaddr = 0;
- tbl.bufq[idx].dma_buf = dmabuf;
- tbl.bufq[idx].len = len;
- tbl.bufq[idx].num_hdls = cmd->num_hdl;
- tbl.bufq[idx].is_imported = true;
- tbl.bufq[idx].is_internal = is_internal;
- if (cmd->flags & CAM_MEM_FLAG_KMD_ACCESS)
- kref_init(&tbl.bufq[idx].krefcount);
- kref_init(&tbl.bufq[idx].urefcount);
- tbl.bufq[idx].smmu_mapping_client = CAM_SMMU_MAPPING_USER;
- strscpy(tbl.bufq[idx].buf_name, cmd->buf_name, sizeof(tbl.bufq[idx].buf_name));
- mutex_unlock(&tbl.bufq[idx].q_lock);
- cmd->out.buf_handle = tbl.bufq[idx].buf_handle;
- cmd->out.vaddr = 0;
- cmd->out.size = (uint32_t)len;
- CAM_DBG(CAM_MEM,
- "fd=%d, flags=0x%x, num_hdl=%d, idx=%d, buf handle=%x, len=%zu, i_ino=%lu, name:%s",
- cmd->fd, cmd->flags, cmd->num_hdl, idx, cmd->out.buf_handle,
- tbl.bufq[idx].len, tbl.bufq[idx].i_ino, cmd->buf_name);
- return rc;
- map_fail:
- cam_mem_put_slot(idx);
- slot_fail:
- dma_buf_put(dmabuf);
- return rc;
- }
- static int cam_mem_util_unmap_hw_va(int32_t idx,
- enum cam_smmu_region_id region,
- enum cam_smmu_mapping_client client, bool force_unmap)
- {
- int i, fd, num_hdls;
- uint32_t flags;
- struct cam_mem_buf_hw_hdl_info *hdl_info = NULL;
- struct dma_buf *dma_buf;
- unsigned long i_ino;
- int rc = 0;
- if (idx >= CAM_MEM_BUFQ_MAX || idx <= 0) {
- CAM_ERR(CAM_MEM, "Incorrect index");
- return -EINVAL;
- }
- flags = tbl.bufq[idx].flags;
- num_hdls = tbl.bufq[idx].num_hdls;
- fd = tbl.bufq[idx].fd;
- dma_buf = tbl.bufq[idx].dma_buf;
- i_ino = tbl.bufq[idx].i_ino;
- if (unlikely(!num_hdls)) {
- CAM_DBG(CAM_MEM, "No valid handles to unmap");
- return 0;
- }
- CAM_DBG(CAM_MEM,
- "unmap_hw_va : idx=%d, fd=%x, i_ino=%lu flags=0x%x, num_hdls=%d, client=%d",
- idx, fd, i_ino, flags, tbl.bufq[idx].num_hdls, client);
- for (i = 0; i < tbl.max_hdls_supported; i++) {
- if (!tbl.bufq[idx].hdls_info[i].valid_mapping)
- continue;
- hdl_info = &tbl.bufq[idx].hdls_info[i];
- if (flags & CAM_MEM_FLAG_PROTECTED_MODE)
- rc = cam_smmu_unmap_stage2_iova(hdl_info->iommu_hdl, fd, dma_buf,
- force_unmap);
- else if (client == CAM_SMMU_MAPPING_USER)
- rc = cam_smmu_unmap_user_iova(hdl_info->iommu_hdl, fd, dma_buf, region,
- force_unmap);
- else if (client == CAM_SMMU_MAPPING_KERNEL)
- rc = cam_smmu_unmap_kernel_iova(hdl_info->iommu_hdl,
- tbl.bufq[idx].dma_buf, region);
- else {
- CAM_ERR(CAM_MEM, "invalid caller for unmapping : %d", client);
- rc = -EINVAL;
- goto end;
- }
- if (rc < 0) {
- CAM_ERR(CAM_MEM,
- "Failed in %s unmap, i=%d, fd=%d, i_ino=%lu, mmu_hdl=%d, rc=%d",
- ((flags & CAM_MEM_FLAG_PROTECTED_MODE) ? "secure" : "non-secure"),
- i, fd, i_ino, hdl_info->iommu_hdl, rc);
- goto end;
- }
- CAM_DBG(CAM_MEM,
- "i: %d unmap_hw_va : idx=%d, fd=%x, i_ino=%lu flags=0x%x, num_hdls=%d, client=%d hdl: %d",
- i, idx, fd, i_ino, flags, tbl.bufq[idx].num_hdls,
- client, hdl_info->iommu_hdl);
- /* exit loop if all handles for this buffer have been unmapped */
- if (!(--num_hdls))
- break;
- }
- end:
- return rc;
- }
- static void cam_mem_mgr_unmap_active_buf(int idx)
- {
- enum cam_smmu_region_id region = CAM_SMMU_REGION_SHARED;
- if (tbl.bufq[idx].flags & CAM_MEM_FLAG_HW_SHARED_ACCESS)
- region = CAM_SMMU_REGION_SHARED;
- else if (tbl.bufq[idx].flags & CAM_MEM_FLAG_HW_READ_WRITE)
- region = CAM_SMMU_REGION_IO;
- cam_mem_util_unmap_hw_va(idx, region, CAM_SMMU_MAPPING_USER, true);
- if (tbl.bufq[idx].flags & CAM_MEM_FLAG_KMD_ACCESS)
- cam_mem_util_unmap_cpu_va(tbl.bufq[idx].dma_buf,
- tbl.bufq[idx].kmdvaddr);
- }
- static int cam_mem_mgr_cleanup_table(void)
- {
- int i;
- mutex_lock(&tbl.m_lock);
- for (i = 1; i < CAM_MEM_BUFQ_MAX; i++) {
- if (!tbl.bufq[i].active) {
- CAM_DBG(CAM_MEM,
- "Buffer inactive at idx=%d, continuing", i);
- continue;
- } else {
- CAM_DBG(CAM_MEM,
- "Active buffer at idx=%d, possible leak needs unmapping",
- i);
- cam_mem_mgr_unmap_active_buf(i);
- }
- mutex_lock(&tbl.bufq[i].q_lock);
- if (tbl.bufq[i].dma_buf) {
- dma_buf_put(tbl.bufq[i].dma_buf);
- tbl.bufq[i].dma_buf = NULL;
- }
- tbl.bufq[i].fd = -1;
- tbl.bufq[i].i_ino = 0;
- tbl.bufq[i].flags = 0;
- tbl.bufq[i].buf_handle = -1;
- tbl.bufq[i].len = 0;
- tbl.bufq[i].num_hdls = 0;
- tbl.bufq[i].dma_buf = NULL;
- tbl.bufq[i].active = false;
- tbl.bufq[i].release_deferred = false;
- tbl.bufq[i].is_internal = false;
- memset(tbl.bufq[i].hdls_info, 0x0, tbl.max_hdls_info_size);
- cam_mem_mgr_reset_presil_params(i);
- kref_init(&tbl.bufq[i].krefcount);
- kref_init(&tbl.bufq[i].urefcount);
- mutex_unlock(&tbl.bufq[i].q_lock);
- mutex_destroy(&tbl.bufq[i].q_lock);
- }
- bitmap_zero(tbl.bitmap, tbl.bits);
- /* We need to reserve slot 0 because 0 is invalid */
- set_bit(0, tbl.bitmap);
- mutex_unlock(&tbl.m_lock);
- return 0;
- }
- void cam_mem_mgr_deinit(void)
- {
- int i;
- if (!atomic_read(&cam_mem_mgr_state))
- return;
- atomic_set(&cam_mem_mgr_state, CAM_MEM_MGR_UNINITIALIZED);
- cam_mem_mgr_cleanup_table();
- cam_smmu_driver_deinit();
- mutex_lock(&tbl.m_lock);
- bitmap_zero(tbl.bitmap, tbl.bits);
- kfree(tbl.bitmap);
- tbl.bitmap = NULL;
- tbl.dbg_buf_idx = -1;
- /* index 0 is reserved */
- for (i = 1; i < CAM_MEM_BUFQ_MAX; i++) {
- kfree(tbl.bufq[i].hdls_info);
- tbl.bufq[i].hdls_info = NULL;
- }
- mutex_unlock(&tbl.m_lock);
- mutex_destroy(&tbl.m_lock);
- }
- static void cam_mem_util_unmap_dummy(struct kref *kref)
- {
- CAM_DBG(CAM_MEM, "Cam mem util unmap dummy");
- }
- static void cam_mem_util_unmap(int32_t idx)
- {
- int rc = 0;
- enum cam_smmu_region_id region = CAM_SMMU_REGION_SHARED;
- enum cam_smmu_mapping_client client;
- if (idx >= CAM_MEM_BUFQ_MAX || idx <= 0) {
- CAM_ERR(CAM_MEM, "Incorrect index");
- return;
- }
- client = tbl.bufq[idx].smmu_mapping_client;
- CAM_DBG(CAM_MEM, "Flags = %X idx %d", tbl.bufq[idx].flags, idx);
- mutex_lock(&tbl.m_lock);
- if (!tbl.bufq[idx].active) {
- CAM_WARN(CAM_MEM, "Buffer at idx=%d is already unmapped", idx);
- mutex_unlock(&tbl.m_lock);
- return;
- }
- /* Deactivate the buffer queue to prevent multiple unmap */
- mutex_lock(&tbl.bufq[idx].q_lock);
- tbl.bufq[idx].active = false;
- tbl.bufq[idx].release_deferred = false;
- mutex_unlock(&tbl.bufq[idx].q_lock);
- mutex_unlock(&tbl.m_lock);
- if (tbl.bufq[idx].flags & CAM_MEM_FLAG_KMD_ACCESS) {
- if (tbl.bufq[idx].dma_buf && tbl.bufq[idx].kmdvaddr) {
- rc = cam_mem_util_unmap_cpu_va(tbl.bufq[idx].dma_buf,
- tbl.bufq[idx].kmdvaddr);
- if (rc)
- CAM_ERR(CAM_MEM,
- "Failed, dmabuf=%pK, kmdvaddr=%pK",
- tbl.bufq[idx].dma_buf,
- (void *) tbl.bufq[idx].kmdvaddr);
- }
- }
- /* SHARED flag gets precedence, all other flags after it */
- if (tbl.bufq[idx].flags & CAM_MEM_FLAG_HW_SHARED_ACCESS) {
- region = CAM_SMMU_REGION_SHARED;
- } else {
- if (tbl.bufq[idx].flags & CAM_MEM_FLAG_HW_READ_WRITE)
- region = CAM_SMMU_REGION_IO;
- }
- if ((tbl.bufq[idx].flags & CAM_MEM_FLAG_HW_READ_WRITE) ||
- (tbl.bufq[idx].flags & CAM_MEM_FLAG_HW_SHARED_ACCESS) ||
- (tbl.bufq[idx].flags & CAM_MEM_FLAG_PROTECTED_MODE)) {
- rc = cam_mem_util_unmap_hw_va(idx, region, client, false);
- if (rc)
- CAM_ERR(CAM_MEM, "Failed, dmabuf=%pK",
- tbl.bufq[idx].dma_buf);
- }
- mutex_lock(&tbl.m_lock);
- mutex_lock(&tbl.bufq[idx].q_lock);
- tbl.bufq[idx].flags = 0;
- tbl.bufq[idx].buf_handle = -1;
- CAM_DBG(CAM_MEM,
- "Ion buf at idx = %d freeing fd = %d, imported %d, dma_buf %pK, i_ino %lu",
- idx, tbl.bufq[idx].fd, tbl.bufq[idx].is_imported, tbl.bufq[idx].dma_buf,
- tbl.bufq[idx].i_ino);
- if (tbl.bufq[idx].dma_buf)
- dma_buf_put(tbl.bufq[idx].dma_buf);
- tbl.bufq[idx].fd = -1;
- tbl.bufq[idx].i_ino = 0;
- tbl.bufq[idx].dma_buf = NULL;
- tbl.bufq[idx].is_imported = false;
- tbl.bufq[idx].is_internal = false;
- tbl.bufq[idx].len = 0;
- tbl.bufq[idx].num_hdls = 0;
- memset(tbl.bufq[idx].hdls_info, 0x0, tbl.max_hdls_info_size);
- cam_mem_mgr_reset_presil_params(idx);
- memset(&tbl.bufq[idx].timestamp, 0, sizeof(struct timespec64));
- memset(&tbl.bufq[idx].krefcount, 0, sizeof(struct kref));
- memset(&tbl.bufq[idx].urefcount, 0, sizeof(struct kref));
- mutex_unlock(&tbl.bufq[idx].q_lock);
- mutex_destroy(&tbl.bufq[idx].q_lock);
- clear_bit(idx, tbl.bitmap);
- mutex_unlock(&tbl.m_lock);
- }
- static void cam_mem_util_unmap_wrapper(struct kref *kref)
- {
- int32_t idx;
- struct cam_mem_buf_queue *bufq = container_of(kref, typeof(*bufq), krefcount);
- idx = CAM_MEM_MGR_GET_HDL_IDX(bufq->buf_handle);
- if (idx >= CAM_MEM_BUFQ_MAX || idx <= 0) {
- CAM_ERR(CAM_MEM, "idx: %d not valid", idx);
- return;
- }
- cam_mem_util_unmap(idx);
- }
- void cam_mem_put_cpu_buf(int32_t buf_handle)
- {
- int idx;
- uint64_t ms, hrs, min, sec;
- struct timespec64 current_ts;
- uint32_t krefcount = 0, urefcount = 0;
- bool unmap = false;
- if (!buf_handle) {
- CAM_ERR(CAM_MEM, "Invalid buf_handle");
- return;
- }
- idx = CAM_MEM_MGR_GET_HDL_IDX(buf_handle);
- if (idx >= CAM_MEM_BUFQ_MAX || idx <= 0) {
- CAM_ERR(CAM_MEM, "idx: %d not valid", idx);
- return;
- }
- if (!tbl.bufq[idx].active) {
- CAM_ERR(CAM_MEM, "idx: %d not active", idx);
- return;
- }
- if (buf_handle != tbl.bufq[idx].buf_handle) {
- CAM_ERR(CAM_MEM, "idx: %d Invalid buf handle %d",
- idx, buf_handle);
- return;
- }
- kref_put(&tbl.bufq[idx].krefcount, cam_mem_util_unmap_dummy);
- krefcount = kref_read(&tbl.bufq[idx].krefcount);
- urefcount = kref_read(&tbl.bufq[idx].urefcount);
- if ((krefcount == 1) && (urefcount == 0))
- unmap = true;
- if (unmap) {
- cam_mem_util_unmap(idx);
- CAM_GET_TIMESTAMP(current_ts);
- CAM_CONVERT_TIMESTAMP_FORMAT(current_ts, hrs, min, sec, ms);
- CAM_DBG(CAM_MEM,
- "%llu:%llu:%llu:%llu Called unmap from here, buf_handle: %u, idx: %d",
- hrs, min, sec, ms, buf_handle, idx);
- } else if (tbl.bufq[idx].release_deferred) {
- CAM_CONVERT_TIMESTAMP_FORMAT((tbl.bufq[idx].timestamp), hrs, min, sec, ms);
- CAM_ERR(CAM_MEM,
- "%llu:%llu:%llu:%llu idx %d fd %d i_ino %lu size %llu active %d buf_handle %d krefCount %d urefCount %d buf_name %s",
- hrs, min, sec, ms, idx, tbl.bufq[idx].fd, tbl.bufq[idx].i_ino,
- tbl.bufq[idx].len, tbl.bufq[idx].active, tbl.bufq[idx].buf_handle,
- krefcount, urefcount, tbl.bufq[idx].buf_name);
- CAM_GET_TIMESTAMP(current_ts);
- CAM_CONVERT_TIMESTAMP_FORMAT(current_ts, hrs, min, sec, ms);
- CAM_ERR(CAM_MEM,
- "%llu:%llu:%llu:%llu Not unmapping even after defer, buf_handle: %u, idx: %d",
- hrs, min, sec, ms, buf_handle, idx);
- } else if (krefcount == 0) {
- CAM_ERR(CAM_MEM,
- "Unbalanced release Called buf_handle: %u, idx: %d",
- tbl.bufq[idx].buf_handle, idx);
- }
- }
- EXPORT_SYMBOL(cam_mem_put_cpu_buf);
- int cam_mem_mgr_release(struct cam_mem_mgr_release_cmd *cmd)
- {
- int idx;
- int rc = 0;
- uint64_t ms, hrs, min, sec;
- struct timespec64 current_ts;
- uint32_t krefcount = 0, urefcount = 0;
- bool unmap = false;
- if (!atomic_read(&cam_mem_mgr_state)) {
- CAM_ERR(CAM_MEM, "failed. mem_mgr not initialized");
- return -EINVAL;
- }
- if (!cmd) {
- CAM_ERR(CAM_MEM, "Invalid argument");
- return -EINVAL;
- }
- idx = CAM_MEM_MGR_GET_HDL_IDX(cmd->buf_handle);
- if (idx >= CAM_MEM_BUFQ_MAX || idx <= 0) {
- CAM_ERR(CAM_MEM, "Incorrect index %d extracted from mem handle",
- idx);
- return -EINVAL;
- }
- if (!tbl.bufq[idx].active) {
- CAM_ERR(CAM_MEM, "Released buffer state should be active");
- return -EINVAL;
- }
- if (tbl.bufq[idx].buf_handle != cmd->buf_handle) {
- CAM_ERR(CAM_MEM,
- "Released buf handle %d not matching within table %d, idx=%d",
- cmd->buf_handle, tbl.bufq[idx].buf_handle, idx);
- return -EINVAL;
- }
- CAM_DBG(CAM_MEM, "Releasing hdl = %x, idx = %d", cmd->buf_handle, idx);
- kref_put(&tbl.bufq[idx].urefcount, cam_mem_util_unmap_dummy);
- urefcount = kref_read(&tbl.bufq[idx].urefcount);
- if (tbl.bufq[idx].flags & CAM_MEM_FLAG_KMD_ACCESS) {
- krefcount = kref_read(&tbl.bufq[idx].krefcount);
- if ((krefcount == 1) && (urefcount == 0))
- unmap = true;
- } else {
- if (urefcount == 0)
- unmap = true;
- }
- if (unmap) {
- cam_mem_util_unmap(idx);
- CAM_DBG(CAM_MEM,
- "Called unmap from here, buf_handle: %u, idx: %d", cmd->buf_handle, idx);
- } else if (tbl.bufq[idx].flags & CAM_MEM_FLAG_KMD_ACCESS) {
- rc = -EINVAL;
- CAM_GET_TIMESTAMP(current_ts);
- CAM_CONVERT_TIMESTAMP_FORMAT(current_ts, hrs, min, sec, ms);
- CAM_CONVERT_TIMESTAMP_FORMAT((tbl.bufq[idx].timestamp), hrs, min, sec, ms);
- CAM_ERR(CAM_MEM,
- "%llu:%llu:%llu:%llu idx %d fd %d i_ino %lu size %llu active %d buf_handle %d krefCount %d urefCount %d buf_name %s",
- hrs, min, sec, ms, idx, tbl.bufq[idx].fd, tbl.bufq[idx].i_ino,
- tbl.bufq[idx].len, tbl.bufq[idx].active, tbl.bufq[idx].buf_handle,
- krefcount, urefcount, tbl.bufq[idx].buf_name);
- if (tbl.bufq[idx].release_deferred)
- CAM_ERR(CAM_MEM, "Unbalanced release Called buf_handle: %u, idx: %d",
- tbl.bufq[idx].buf_handle, idx);
- tbl.bufq[idx].release_deferred = true;
- }
- return rc;
- }
- int cam_mem_mgr_request_mem(struct cam_mem_mgr_request_desc *inp,
- struct cam_mem_mgr_memory_desc *out)
- {
- struct dma_buf *buf = NULL;
- int ion_fd = -1, rc = 0;
- uintptr_t kvaddr;
- dma_addr_t iova = 0;
- size_t request_len = 0;
- uint32_t mem_handle;
- int32_t idx;
- int32_t smmu_hdl = 0;
- unsigned long i_ino = 0;
- enum cam_smmu_region_id region = CAM_SMMU_REGION_SHARED;
- if (!atomic_read(&cam_mem_mgr_state)) {
- CAM_ERR(CAM_MEM, "failed. mem_mgr not initialized");
- return -EINVAL;
- }
- if (!inp || !out) {
- CAM_ERR(CAM_MEM, "Invalid params");
- return -EINVAL;
- }
- if (!(inp->flags & CAM_MEM_FLAG_HW_READ_WRITE ||
- inp->flags & CAM_MEM_FLAG_HW_SHARED_ACCESS ||
- inp->flags & CAM_MEM_FLAG_CACHE)) {
- CAM_ERR(CAM_MEM, "Invalid flags for request mem");
- return -EINVAL;
- }
- rc = cam_mem_util_get_dma_buf(inp->size, inp->flags, CAM_MEMMGR_ALLOC_KERNEL, &buf, &i_ino);
- if (rc) {
- CAM_ERR(CAM_MEM, "ION alloc failed for shared buffer");
- goto ion_fail;
- } else if (!buf) {
- CAM_ERR(CAM_MEM, "ION alloc returned NULL buffer");
- goto ion_fail;
- } else {
- CAM_DBG(CAM_MEM, "Got dma_buf = %pK", buf);
- }
- /*
- * we are mapping kva always here,
- * update flags so that we do unmap properly
- */
- inp->flags |= CAM_MEM_FLAG_KMD_ACCESS;
- rc = cam_mem_util_map_cpu_va(buf, &kvaddr, &request_len);
- if (rc) {
- CAM_ERR(CAM_MEM, "Failed to get kernel vaddr");
- goto map_fail;
- }
- if (!inp->smmu_hdl) {
- CAM_ERR(CAM_MEM, "Invalid SMMU handle");
- rc = -EINVAL;
- goto smmu_fail;
- }
- /* SHARED flag gets precedence, all other flags after it */
- if (inp->flags & CAM_MEM_FLAG_HW_SHARED_ACCESS) {
- region = CAM_SMMU_REGION_SHARED;
- } else {
- if (inp->flags & CAM_MEM_FLAG_HW_READ_WRITE)
- region = CAM_SMMU_REGION_IO;
- }
- rc = cam_smmu_map_kernel_iova(inp->smmu_hdl,
- buf,
- CAM_SMMU_MAP_RW,
- &iova,
- &request_len,
- region);
- if (rc < 0) {
- CAM_ERR(CAM_MEM, "SMMU mapping failed");
- goto smmu_fail;
- }
- smmu_hdl = inp->smmu_hdl;
- idx = cam_mem_get_slot();
- if (idx < 0) {
- CAM_ERR(CAM_MEM, "Failed in getting mem slot, idx=%d", idx);
- rc = -ENOMEM;
- cam_mem_mgr_print_tbl();
- goto slot_fail;
- }
- mutex_lock(&tbl.bufq[idx].q_lock);
- mem_handle = GET_MEM_HANDLE(idx, ion_fd);
- tbl.bufq[idx].dma_buf = buf;
- tbl.bufq[idx].fd = -1;
- tbl.bufq[idx].i_ino = i_ino;
- tbl.bufq[idx].flags = inp->flags;
- tbl.bufq[idx].buf_handle = mem_handle;
- tbl.bufq[idx].kmdvaddr = kvaddr;
- cam_mem_mgr_update_iova_info_locked(tbl.bufq[idx].hdls_info,
- iova, inp->smmu_hdl, inp->size, true, NULL);
- tbl.bufq[idx].len = inp->size;
- tbl.bufq[idx].num_hdls = 1;
- tbl.bufq[idx].is_imported = false;
- kref_init(&tbl.bufq[idx].krefcount);
- tbl.bufq[idx].smmu_mapping_client = CAM_SMMU_MAPPING_KERNEL;
- mutex_unlock(&tbl.bufq[idx].q_lock);
- out->kva = kvaddr;
- out->iova = (uint32_t)iova;
- out->smmu_hdl = smmu_hdl;
- out->mem_handle = mem_handle;
- out->len = inp->size;
- out->region = region;
- CAM_DBG(CAM_MEM, "idx=%d, dmabuf=%pK, i_ino=%lu, flags=0x%x, mem_handle=0x%x",
- idx, buf, i_ino, inp->flags, mem_handle);
- return rc;
- slot_fail:
- cam_smmu_unmap_kernel_iova(inp->smmu_hdl,
- buf, region);
- smmu_fail:
- cam_mem_util_unmap_cpu_va(buf, kvaddr);
- map_fail:
- dma_buf_put(buf);
- ion_fail:
- return rc;
- }
- EXPORT_SYMBOL(cam_mem_mgr_request_mem);
- int cam_mem_mgr_release_mem(struct cam_mem_mgr_memory_desc *inp)
- {
- int32_t idx;
- int rc = 0;
- if (!atomic_read(&cam_mem_mgr_state)) {
- CAM_ERR(CAM_MEM, "failed. mem_mgr not initialized");
- return -EINVAL;
- }
- if (!inp) {
- CAM_ERR(CAM_MEM, "Invalid argument");
- return -EINVAL;
- }
- idx = CAM_MEM_MGR_GET_HDL_IDX(inp->mem_handle);
- if (idx >= CAM_MEM_BUFQ_MAX || idx <= 0) {
- CAM_ERR(CAM_MEM, "Incorrect index extracted from mem handle");
- return -EINVAL;
- }
- if (!tbl.bufq[idx].active) {
- CAM_ERR(CAM_MEM, "Released buffer state should be active");
- return -EINVAL;
- }
- if (tbl.bufq[idx].buf_handle != inp->mem_handle) {
- CAM_ERR(CAM_MEM,
- "Released buf handle not matching within table");
- return -EINVAL;
- }
- CAM_DBG(CAM_MEM, "Releasing hdl = %X", inp->mem_handle);
- if (kref_put(&tbl.bufq[idx].krefcount, cam_mem_util_unmap_wrapper))
- CAM_DBG(CAM_MEM,
- "Called unmap from here, buf_handle: %u, idx: %d",
- tbl.bufq[idx].buf_handle, idx);
- else {
- CAM_ERR(CAM_MEM,
- "Unbalanced release Called buf_handle: %u, idx: %d",
- tbl.bufq[idx].buf_handle, idx);
- rc = -EINVAL;
- }
- return rc;
- }
- EXPORT_SYMBOL(cam_mem_mgr_release_mem);
- int cam_mem_mgr_reserve_memory_region(struct cam_mem_mgr_request_desc *inp,
- enum cam_smmu_region_id region,
- struct cam_mem_mgr_memory_desc *out)
- {
- struct dma_buf *buf = NULL;
- int rc = 0, ion_fd = -1;
- dma_addr_t iova = 0;
- size_t request_len = 0;
- uint32_t mem_handle;
- int32_t idx;
- int32_t smmu_hdl = 0;
- uintptr_t kvaddr = 0;
- unsigned long i_ino = 0;
- if (!atomic_read(&cam_mem_mgr_state)) {
- CAM_ERR(CAM_MEM, "failed. mem_mgr not initialized");
- return -EINVAL;
- }
- if (!inp || !out) {
- CAM_ERR(CAM_MEM, "Invalid param(s)");
- return -EINVAL;
- }
- if (!inp->smmu_hdl) {
- CAM_ERR(CAM_MEM, "Invalid SMMU handle");
- return -EINVAL;
- }
- if ((region != CAM_SMMU_REGION_SECHEAP) &&
- (region != CAM_SMMU_REGION_FWUNCACHED)) {
- CAM_ERR(CAM_MEM, "Only secondary heap supported");
- return -EINVAL;
- }
- rc = cam_mem_util_get_dma_buf(inp->size, 0, CAM_MEMMGR_ALLOC_KERNEL, &buf, &i_ino);
- if (rc) {
- CAM_ERR(CAM_MEM, "ION alloc failed for sec heap buffer");
- goto ion_fail;
- } else if (!buf) {
- CAM_ERR(CAM_MEM, "ION alloc returned NULL buffer");
- goto ion_fail;
- } else {
- CAM_DBG(CAM_MEM, "Got dma_buf = %pK", buf);
- }
- if (inp->flags & CAM_MEM_FLAG_KMD_ACCESS) {
- rc = cam_mem_util_map_cpu_va(buf, &kvaddr, &request_len);
- if (rc) {
- CAM_ERR(CAM_MEM, "Failed to get kernel vaddr");
- goto kmap_fail;
- }
- }
- rc = cam_smmu_reserve_buf_region(region,
- inp->smmu_hdl, buf, &iova, &request_len);
- if (rc) {
- CAM_ERR(CAM_MEM, "Reserving secondary heap failed");
- goto smmu_fail;
- }
- smmu_hdl = inp->smmu_hdl;
- idx = cam_mem_get_slot();
- if (idx < 0) {
- CAM_ERR(CAM_MEM, "Failed in getting mem slot, idx=%d", idx);
- rc = -ENOMEM;
- cam_mem_mgr_print_tbl();
- goto slot_fail;
- }
- mutex_lock(&tbl.bufq[idx].q_lock);
- mem_handle = GET_MEM_HANDLE(idx, ion_fd);
- tbl.bufq[idx].fd = -1;
- tbl.bufq[idx].i_ino = i_ino;
- tbl.bufq[idx].dma_buf = buf;
- tbl.bufq[idx].flags = inp->flags;
- tbl.bufq[idx].buf_handle = mem_handle;
- tbl.bufq[idx].kmdvaddr = kvaddr;
- cam_mem_mgr_update_iova_info_locked(tbl.bufq[idx].hdls_info,
- iova, inp->smmu_hdl, request_len, true, NULL);
- tbl.bufq[idx].len = request_len;
- tbl.bufq[idx].num_hdls = 1;
- tbl.bufq[idx].is_imported = false;
- kref_init(&tbl.bufq[idx].krefcount);
- tbl.bufq[idx].smmu_mapping_client = CAM_SMMU_MAPPING_KERNEL;
- mutex_unlock(&tbl.bufq[idx].q_lock);
- out->kva = kvaddr;
- out->iova = (uint32_t)iova;
- out->smmu_hdl = smmu_hdl;
- out->mem_handle = mem_handle;
- out->len = request_len;
- out->region = region;
- return rc;
- slot_fail:
- cam_smmu_release_buf_region(region, smmu_hdl);
- smmu_fail:
- if (region == CAM_SMMU_REGION_FWUNCACHED)
- cam_mem_util_unmap_cpu_va(buf, kvaddr);
- kmap_fail:
- dma_buf_put(buf);
- ion_fail:
- return rc;
- }
- EXPORT_SYMBOL(cam_mem_mgr_reserve_memory_region);
- static void *cam_mem_mgr_user_dump_buf(
- void *dump_struct, uint8_t *addr_ptr)
- {
- struct cam_mem_buf_queue *buf = NULL;
- uint64_t *addr;
- int i = 0;
- buf = (struct cam_mem_buf_queue *)dump_struct;
- addr = (uint64_t *)addr_ptr;
- *addr++ = buf->timestamp.tv_sec;
- *addr++ = buf->timestamp.tv_nsec / NSEC_PER_USEC;
- *addr++ = buf->fd;
- *addr++ = buf->i_ino;
- *addr++ = buf->buf_handle;
- *addr++ = buf->len;
- *addr++ = buf->align;
- *addr++ = buf->flags;
- *addr++ = buf->kmdvaddr;
- *addr++ = buf->is_imported;
- *addr++ = buf->is_internal;
- *addr++ = buf->num_hdls;
- for (i = 0; i < tbl.max_hdls_supported; i++) {
- if (!buf->hdls_info[i].addr_updated)
- continue;
- *addr++ = buf->hdls_info[i].iommu_hdl;
- *addr++ = buf->hdls_info[i].vaddr;
- }
- return addr;
- }
- int cam_mem_mgr_dump_user(struct cam_dump_req_cmd *dump_req)
- {
- int rc = 0;
- int i;
- struct cam_common_hw_dump_args dump_args;
- size_t buf_len;
- size_t remain_len;
- uint32_t min_len;
- uintptr_t cpu_addr;
- rc = cam_mem_get_cpu_buf(dump_req->buf_handle,
- &cpu_addr, &buf_len);
- if (rc) {
- CAM_ERR(CAM_MEM, "Invalid handle %u rc %d",
- dump_req->buf_handle, rc);
- return rc;
- }
- if (buf_len <= dump_req->offset) {
- CAM_WARN(CAM_MEM, "Dump buffer overshoot len %zu offset %zu",
- buf_len, dump_req->offset);
- cam_mem_put_cpu_buf(dump_req->buf_handle);
- return -ENOSPC;
- }
- remain_len = buf_len - dump_req->offset;
- min_len =
- (CAM_MEM_BUFQ_MAX *
- (CAM_MEM_MGR_DUMP_BUF_NUM_WORDS * sizeof(uint64_t) +
- sizeof(struct cam_common_hw_dump_header)));
- if (remain_len < min_len) {
- CAM_WARN(CAM_MEM, "Dump buffer exhaust remain %zu min %u",
- remain_len, min_len);
- cam_mem_put_cpu_buf(dump_req->buf_handle);
- return -ENOSPC;
- }
- dump_args.req_id = dump_req->issue_req_id;
- dump_args.cpu_addr = cpu_addr;
- dump_args.buf_len = buf_len;
- dump_args.offset = dump_req->offset;
- dump_args.ctxt_to_hw_map = NULL;
- mutex_lock(&tbl.m_lock);
- for (i = 1; i < CAM_MEM_BUFQ_MAX; i++) {
- if (tbl.bufq[i].active) {
- mutex_lock(&tbl.bufq[i].q_lock);
- rc = cam_common_user_dump_helper(&dump_args,
- cam_mem_mgr_user_dump_buf,
- &tbl.bufq[i],
- sizeof(uint64_t), "MEM_MGR_BUF.%d:", i);
- if (rc) {
- CAM_ERR(CAM_CRM,
- "Dump state info failed, rc: %d",
- rc);
- return rc;
- }
- mutex_unlock(&tbl.bufq[i].q_lock);
- }
- }
- mutex_unlock(&tbl.m_lock);
- dump_req->offset = dump_args.offset;
- cam_mem_put_cpu_buf(dump_req->buf_handle);
- return rc;
- }
- int cam_mem_mgr_free_memory_region(struct cam_mem_mgr_memory_desc *inp)
- {
- int32_t rc = 0, idx, entry_idx;
- if (!atomic_read(&cam_mem_mgr_state)) {
- CAM_ERR(CAM_MEM, "failed. mem_mgr not initialized");
- return -EINVAL;
- }
- if (!inp) {
- CAM_ERR(CAM_MEM, "Invalid argument");
- return -EINVAL;
- }
- if ((inp->region != CAM_SMMU_REGION_SECHEAP) &&
- (inp->region != CAM_SMMU_REGION_FWUNCACHED)) {
- CAM_ERR(CAM_MEM, "Only secondary heap supported");
- return -EINVAL;
- }
- idx = CAM_MEM_MGR_GET_HDL_IDX(inp->mem_handle);
- if (idx >= CAM_MEM_BUFQ_MAX || idx <= 0) {
- CAM_ERR(CAM_MEM, "Incorrect index extracted from mem handle");
- return -EINVAL;
- }
- if (!tbl.bufq[idx].active) {
- CAM_ERR(CAM_MEM, "Released buffer state should be active");
- return -EINVAL;
- }
- if (tbl.bufq[idx].buf_handle != inp->mem_handle) {
- CAM_ERR(CAM_MEM,
- "Released buf handle not matching within table");
- return -EINVAL;
- }
- if (tbl.bufq[idx].num_hdls != 1) {
- CAM_ERR(CAM_MEM,
- "Sec heap region should have only one smmu hdl");
- return -ENODEV;
- }
- if (!cam_mem_mgr_get_hwva_entry_idx(inp->smmu_hdl, &entry_idx)) {
- CAM_ERR(CAM_MEM,
- "Passed SMMU handle not a valid handle");
- return -ENODEV;
- }
- if (inp->smmu_hdl != tbl.bufq[idx].hdls_info[entry_idx].iommu_hdl) {
- CAM_ERR(CAM_MEM,
- "Passed SMMU handle doesn't match with internal hdl");
- return -ENODEV;
- }
- rc = cam_smmu_release_buf_region(inp->region, inp->smmu_hdl);
- if (rc) {
- CAM_ERR(CAM_MEM,
- "Sec heap region release failed");
- return -ENODEV;
- }
- CAM_DBG(CAM_MEM, "Releasing hdl = %X", inp->mem_handle);
- if (kref_put(&tbl.bufq[idx].krefcount, cam_mem_util_unmap_wrapper))
- CAM_DBG(CAM_MEM,
- "Called unmap from here, buf_handle: %u, idx: %d",
- inp->mem_handle, idx);
- else {
- CAM_ERR(CAM_MEM,
- "Unbalanced release Called buf_handle: %u, idx: %d",
- inp->mem_handle, idx);
- rc = -EINVAL;
- }
- return rc;
- }
- EXPORT_SYMBOL(cam_mem_mgr_free_memory_region);
- #ifdef CONFIG_CAM_PRESIL
- struct dma_buf *cam_mem_mgr_get_dma_buf(int fd)
- {
- struct dma_buf *dmabuf = NULL;
- dmabuf = dma_buf_get(fd);
- if (IS_ERR_OR_NULL((void *)(dmabuf))) {
- CAM_ERR(CAM_MEM, "Failed to import dma_buf for fd");
- return NULL;
- }
- CAM_INFO(CAM_PRESIL, "Received DMA Buf* %pK", dmabuf);
- return dmabuf;
- }
- int cam_mem_mgr_put_dmabuf_from_fd(uint64_t input_dmabuf)
- {
- struct dma_buf *dmabuf = (struct dma_buf *)(uint64_t)input_dmabuf;
- int idx = 0;
- CAM_INFO(CAM_PRESIL, "Received dma_buf :%pK", dmabuf);
- if (!dmabuf) {
- CAM_ERR(CAM_PRESIL, "NULL to import dma_buf fd");
- return -EINVAL;
- }
- for (idx = 0; idx < CAM_MEM_BUFQ_MAX; idx++) {
- if ((tbl.bufq[idx].dma_buf != NULL) && (tbl.bufq[idx].dma_buf == dmabuf)) {
- if (tbl.bufq[idx].presil_params.refcount)
- tbl.bufq[idx].presil_params.refcount--;
- else
- CAM_ERR(CAM_PRESIL, "Unbalanced dmabuf put: %pK", dmabuf);
- if (!tbl.bufq[idx].presil_params.refcount) {
- dma_buf_put(dmabuf);
- cam_mem_mgr_reset_presil_params(idx);
- CAM_DBG(CAM_PRESIL, "Done dma_buf_put for %pK", dmabuf);
- }
- }
- }
- return 0;
- }
- int cam_mem_mgr_get_fd_from_dmabuf(uint64_t input_dmabuf)
- {
- int fd_for_dmabuf = -1;
- struct dma_buf *dmabuf = (struct dma_buf *)(uint64_t)input_dmabuf;
- int idx = 0;
- CAM_DBG(CAM_PRESIL, "Received dma_buf :%pK", dmabuf);
- if (!dmabuf) {
- CAM_ERR(CAM_PRESIL, "NULL to import dma_buf fd");
- return -EINVAL;
- }
- for (idx = 0; idx < CAM_MEM_BUFQ_MAX; idx++) {
- if ((tbl.bufq[idx].dma_buf != NULL) && (tbl.bufq[idx].dma_buf == dmabuf)) {
- CAM_DBG(CAM_PRESIL,
- "Found entry for request from Presil UMD Daemon at %d, dmabuf %pK fd_for_umd_daemon %d refcount: %d",
- idx, tbl.bufq[idx].dma_buf,
- tbl.bufq[idx].presil_params.fd_for_umd_daemon,
- tbl.bufq[idx].presil_params.refcount);
- if (tbl.bufq[idx].presil_params.fd_for_umd_daemon < 0) {
- fd_for_dmabuf = dma_buf_fd(dmabuf, O_CLOEXEC);
- if (fd_for_dmabuf < 0) {
- CAM_ERR(CAM_PRESIL, "get fd fail, fd_for_dmabuf=%d",
- fd_for_dmabuf);
- return -EINVAL;
- }
- tbl.bufq[idx].presil_params.fd_for_umd_daemon = fd_for_dmabuf;
- CAM_INFO(CAM_PRESIL,
- "Received generated idx %d fd_for_dmabuf Buf* %lld", idx,
- fd_for_dmabuf);
- } else {
- fd_for_dmabuf = tbl.bufq[idx].presil_params.fd_for_umd_daemon;
- CAM_INFO(CAM_PRESIL,
- "Received existing at idx %d fd_for_dmabuf Buf* %lld", idx,
- fd_for_dmabuf);
- }
- tbl.bufq[idx].presil_params.refcount++;
- } else {
- CAM_DBG(CAM_MEM,
- "Not found dmabuf at idx=%d, dma_buf %pK handle 0x%0x active %d ",
- idx, tbl.bufq[idx].dma_buf, tbl.bufq[idx].buf_handle,
- tbl.bufq[idx].active);
- }
- }
- return (int)fd_for_dmabuf;
- }
- int cam_mem_mgr_send_buffer_to_presil(int32_t iommu_hdl, int32_t buf_handle)
- {
- int rc = 0;
- /* Sending Presil IO Buf to PC side ( as iova start address indicates) */
- uint64_t io_buf_addr;
- size_t io_buf_size;
- int i, j, fd = -1, idx = 0;
- uint8_t *iova_ptr = NULL;
- uint64_t dmabuf = 0;
- bool is_mapped_in_cb = false;
- CAM_DBG(CAM_PRESIL, "buf handle 0x%0x", buf_handle);
- idx = CAM_MEM_MGR_GET_HDL_IDX(buf_handle);
- for (i = 0; i < tbl.bufq[idx].num_hdl; i++) {
- if (tbl.bufq[idx].hdls[i] == iommu_hdl)
- is_mapped_in_cb = true;
- }
- if (!is_mapped_in_cb) {
- for (j = 0; j < CAM_MEM_BUFQ_MAX; j++) {
- if (tbl.bufq[j].i_ino == tbl.bufq[idx].i_ino) {
- for (i = 0; i < tbl.bufq[j].num_hdl; i++) {
- if (tbl.bufq[j].hdls[i] == iommu_hdl)
- is_mapped_in_cb = true;
- }
- }
- }
- if (!is_mapped_in_cb) {
- CAM_DBG(CAM_PRESIL,
- "Still Could not find idx=%d, FD %d buf_handle 0x%0x",
- idx, GET_FD_FROM_HANDLE(buf_handle), buf_handle);
- /*
- * Okay to return 0, since this function also gets called for buffers that
- * are shared only between umd/kmd, these may not be mapped with smmu
- */
- return 0;
- }
- }
- if ((tbl.bufq[idx].buf_handle != 0) && (tbl.bufq[idx].active) &&
- (tbl.bufq[idx].buf_handle == buf_handle)) {
- CAM_DBG(CAM_PRESIL,
- "Found dmabuf in bufq idx %d, FD %d handle 0x%0x dmabuf %pK",
- idx, tbl.bufq[idx].fd, tbl.bufq[idx].buf_handle, tbl.bufq[idx].dma_buf);
- dmabuf = (uint64_t)tbl.bufq[idx].dma_buf;
- fd = tbl.bufq[idx].fd;
- } else {
- CAM_ERR(CAM_PRESIL,
- "Could not find dmabuf Invalid Mem idx=%d, FD %d handle 0x%0x active %d",
- idx, tbl.bufq[idx].fd, tbl.bufq[idx].buf_handle, tbl.bufq[idx].active);
- return -EINVAL;
- }
- rc = cam_mem_get_io_buf(buf_handle, iommu_hdl, &io_buf_addr, &io_buf_size,
- NULL, NULL);
- if (rc || NULL == (void *)io_buf_addr) {
- CAM_DBG(CAM_PRESIL, "Invalid ioaddr : 0x%x, fd = %d, dmabuf = %pK",
- io_buf_addr, fd, dmabuf);
- return -EINVAL;
- }
- iova_ptr = (uint8_t *)io_buf_addr;
- CAM_INFO(CAM_PRESIL, "Sending buffer with ioaddr : 0x%x, fd = %d, dmabuf = %pK",
- io_buf_addr, fd, dmabuf);
- rc = cam_presil_send_buffer(dmabuf, 0, 0, (uint32_t)io_buf_size, (uint64_t)iova_ptr);
- return rc;
- }
- int cam_mem_mgr_send_all_buffers_to_presil(int32_t iommu_hdl)
- {
- int idx = 0;
- int rc = 0;
- int32_t fd_already_sent[128];
- int fd_already_sent_count = 0;
- int fd_already_index = 0;
- int fd_already_sent_found = 0;
- memset(&fd_already_sent, 0x0, sizeof(fd_already_sent));
- for (idx = 0; idx < CAM_MEM_BUFQ_MAX; idx++) {
- if ((tbl.bufq[idx].buf_handle != 0) && (tbl.bufq[idx].active)) {
- CAM_DBG(CAM_PRESIL, "Sending %d, FD %d handle 0x%0x", idx, tbl.bufq[idx].fd,
- tbl.bufq[idx].buf_handle);
- fd_already_sent_found = 0;
- for (fd_already_index = 0; fd_already_index < fd_already_sent_count;
- fd_already_index++) {
- if (fd_already_sent[fd_already_index] == tbl.bufq[idx].fd) {
- fd_already_sent_found = 1;
- CAM_DBG(CAM_PRESIL,
- "fd_already_sent %d, FD %d handle 0x%0x flags=0x%0x",
- idx, tbl.bufq[idx].fd, tbl.bufq[idx].buf_handle,
- tbl.bufq[idx].flags);
- }
- }
- if (fd_already_sent_found)
- continue;
- CAM_DBG(CAM_PRESIL, "Sending %d, FD %d handle 0x%0x flags=0x%0x", idx,
- tbl.bufq[idx].fd, tbl.bufq[idx].buf_handle, tbl.bufq[idx].flags);
- rc = cam_mem_mgr_send_buffer_to_presil(iommu_hdl, tbl.bufq[idx].buf_handle);
- fd_already_sent[fd_already_sent_count++] = tbl.bufq[idx].fd;
- } else {
- CAM_DBG(CAM_PRESIL, "Invalid Mem idx=%d, FD %d handle 0x%0x active %d",
- idx, tbl.bufq[idx].fd, tbl.bufq[idx].buf_handle,
- tbl.bufq[idx].active);
- }
- }
- return rc;
- }
- EXPORT_SYMBOL(cam_mem_mgr_send_all_buffers_to_presil);
- int cam_mem_mgr_retrieve_buffer_from_presil(int32_t buf_handle, uint32_t buf_size,
- uint32_t offset, int32_t iommu_hdl)
- {
- int rc = 0;
- /* Receive output buffer from Presil IO Buf to PC side (as iova start address indicates) */
- uint64_t io_buf_addr;
- size_t io_buf_size;
- uint64_t dmabuf = 0;
- int fd = 0;
- uint8_t *iova_ptr = NULL;
- int idx = 0;
- CAM_DBG(CAM_PRESIL, "buf handle 0x%0x ", buf_handle);
- rc = cam_mem_get_io_buf(buf_handle, iommu_hdl, &io_buf_addr, &io_buf_size,
- NULL, NULL);
- if (rc) {
- CAM_ERR(CAM_PRESIL, "Unable to get IOVA for buffer buf_hdl: 0x%0x iommu_hdl: 0x%0x",
- buf_handle, iommu_hdl);
- return -EINVAL;
- }
- iova_ptr = (uint8_t *)io_buf_addr;
- iova_ptr += offset; // correct target address to start writing buffer to.
- if (!buf_size) {
- buf_size = io_buf_size;
- CAM_DBG(CAM_PRESIL, "Updated buf_size from Zero to 0x%0x", buf_size);
- }
- fd = GET_FD_FROM_HANDLE(buf_handle);
- idx = CAM_MEM_MGR_GET_HDL_IDX(buf_handle);
- if ((tbl.bufq[idx].buf_handle != 0) && (tbl.bufq[idx].active) &&
- (tbl.bufq[idx].buf_handle == buf_handle)) {
- CAM_DBG(CAM_PRESIL, "Found dmabuf in bufq idx %d, FD %d handle 0x%0x dmabuf %pK",
- idx, tbl.bufq[idx].fd, tbl.bufq[idx].buf_handle, tbl.bufq[idx].dma_buf);
- dmabuf = (uint64_t)tbl.bufq[idx].dma_buf;
- } else {
- CAM_ERR(CAM_PRESIL,
- "Could not find dmabuf Invalid Mem idx=%d, FD %d handle 0x%0x active %d ",
- idx, tbl.bufq[idx].fd, tbl.bufq[idx].buf_handle, tbl.bufq[idx].active);
- }
- CAM_DBG(CAM_PRESIL,
- "Retrieving buffer with ioaddr : 0x%x, offset = %d, size = %d, fd = %d, dmabuf = %pK",
- io_buf_addr, offset, buf_size, fd, dmabuf);
- rc = cam_presil_retrieve_buffer(dmabuf, 0, 0, (uint32_t)buf_size, (uint64_t)io_buf_addr);
- CAM_INFO(CAM_PRESIL,
- "Retrieved buffer with ioaddr : 0x%x, offset = %d, size = %d, fd = %d, dmabuf = %pK",
- io_buf_addr, 0, buf_size, fd, dmabuf);
- return rc;
- }
- #else /* ifdef CONFIG_CAM_PRESIL */
- struct dma_buf * cam_mem_mgr_get_dma_buf(int fd)
- {
- return NULL;
- }
- int cam_mem_mgr_send_all_buffers_to_presil(int32_t iommu_hdl)
- {
- return 0;
- }
- int cam_mem_mgr_send_buffer_to_presil(int32_t iommu_hdl, int32_t buf_handle)
- {
- return 0;
- }
- int cam_mem_mgr_retrieve_buffer_from_presil(int32_t buf_handle,
- uint32_t buf_size,
- uint32_t offset,
- int32_t iommu_hdl)
- {
- return 0;
- }
- #endif /* ifdef CONFIG_CAM_PRESIL */
|