ubwcp_main.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "%s: %s(): " fmt, KBUILD_MODNAME, __func__
  6. #include <linux/module.h>
  7. #include <linux/kernel.h>
  8. #include <linux/dma-buf.h>
  9. #include <linux/slab.h>
  10. #include <linux/cdev.h>
  11. #include <linux/hashtable.h>
  12. #include <linux/scatterlist.h>
  13. #include <linux/types.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/of.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/of_address.h>
  18. #include <linux/genalloc.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/regulator/consumer.h>
  21. #include <linux/numa.h>
  22. #include <linux/memory_hotplug.h>
  23. #include <asm/page.h>
  24. #include <linux/delay.h>
  25. #include <linux/ubwcp_dma_heap.h>
  26. #include <linux/debugfs.h>
  27. #include <linux/clk.h>
  28. #include <linux/iommu.h>
  29. #include <linux/set_memory.h>
  30. #include <linux/range.h>
  31. #include <linux/qcom_scm.h>
  32. MODULE_IMPORT_NS(DMA_BUF);
  33. #include "include/kernel/ubwcp.h"
  34. #include "ubwcp_hw.h"
  35. #include "include/uapi/ubwcp_ioctl.h"
  36. #define CREATE_TRACE_POINTS
  37. #include "ubwcp_trace.h"
  38. #define UBWCP_NUM_DEVICES 1
  39. #define UBWCP_DEVICE_NAME "ubwcp"
  40. #define UBWCP_BUFFER_DESC_OFFSET 64
  41. #define UBWCP_BUFFER_DESC_COUNT 256
  42. #define CACHE_ADDR(x) ((x) >> 6)
  43. #define PAGE_ADDR(x) ((x) >> 12)
  44. #define UBWCP_ALIGN(_x, _y) ((((_x) + (_y) - 1)/(_y))*(_y))
  45. #define DBG_BUF_ATTR(fmt, args...) do { if (unlikely(ubwcp_debug_trace_enable)) \
  46. pr_err(fmt "\n", ##args); \
  47. } while (0)
  48. #define DBG(fmt, args...) do { if (unlikely(ubwcp_debug_trace_enable)) \
  49. pr_err(fmt "\n", ##args); \
  50. } while (0)
  51. #define ERR(fmt, args...) pr_err_ratelimited("%d: ~~~ERROR~~~: " fmt "\n", __LINE__, ##args)
  52. #define META_DATA_PITCH_ALIGN 64
  53. #define META_DATA_HEIGHT_ALIGN 16
  54. #define META_DATA_SIZE_ALIGN 4096
  55. #define PIXEL_DATA_SIZE_ALIGN 4096
  56. #define UBWCP_SYNC_GRANULE 0x4000000L /* 64 MB */
  57. /* Max values for attributes */
  58. #define MAX_ATTR_WIDTH (10*1024)
  59. #define MAX_ATTR_HEIGHT (10*1024)
  60. #define MAX_ATTR_STRIDE (64*1024)
  61. #define MAX_ATTR_PLANAR_PAD 4096
  62. #define MAX_ATTR_SCANLN_HT_DELTA (32*1024)
  63. enum ula_remove_mem_status {
  64. ULA_REMOVE_MEM_SUCCESS = 0,
  65. ULA_REMOVE_MEM_ABORTED = 1
  66. };
  67. struct ubwcp_desc {
  68. int idx;
  69. void *ptr;
  70. };
  71. struct tile_dimension {
  72. u16 width;
  73. u16 height;
  74. };
  75. struct ubwcp_plane_info {
  76. u16 pixel_bytes;
  77. u16 per_pixel;
  78. struct tile_dimension tilesize_p; /* pixels */
  79. struct tile_dimension macrotilesize_p; /* pixels */
  80. };
  81. struct ubwcp_image_format_info {
  82. u16 planes;
  83. struct ubwcp_plane_info p_info[2];
  84. };
  85. enum ubwcp_std_image_format {
  86. RGBA = 0,
  87. NV12 = 1,
  88. NV124R = 2,
  89. P010 = 3,
  90. TP10 = 4,
  91. P016 = 5,
  92. INFO_FORMAT_LIST_SIZE,
  93. };
  94. enum ubwcp_state {
  95. UBWCP_STATE_READY = 0,
  96. UBWCP_STATE_INVALID = -1,
  97. UBWCP_STATE_FAULT = -2,
  98. };
  99. struct ubwcp_prefetch_tgt_ctrl {
  100. atomic_t cpu_count;
  101. bool enable;
  102. int result;
  103. };
  104. struct ubwcp_driver {
  105. /* cdev related */
  106. dev_t devt;
  107. struct class *dev_class; //sysfs dev class
  108. struct device *dev_sys; //sysfs dev
  109. struct cdev cdev; //char dev
  110. /* debugfs */
  111. struct dentry *debugfs_root;
  112. bool read_err_irq_en;
  113. bool write_err_irq_en;
  114. bool decode_err_irq_en;
  115. bool encode_err_irq_en;
  116. /* ubwcp devices */
  117. struct device *dev; //ubwcp device
  118. struct device *dev_desc_cb; //smmu dev for descriptors
  119. struct device *dev_buf_cb; //smmu dev for ubwcp buffers
  120. void __iomem *base; //ubwcp base address
  121. struct regulator *vdd;
  122. struct clk **clocks;
  123. int num_clocks;
  124. /* interrupts */
  125. int irq_range_ck_rd;
  126. int irq_range_ck_wr;
  127. int irq_encode;
  128. int irq_decode;
  129. /* ula address pool */
  130. u64 ula_pool_base;
  131. u64 ula_pool_size;
  132. struct gen_pool *ula_pool;
  133. configure_mmap mmap_config_fptr;
  134. /* HW version */
  135. u32 hw_ver_major;
  136. u32 hw_ver_minor;
  137. /* keep track of all potential buffers.
  138. * hash table index'ed using dma_buf ptr.
  139. * 2**13 = 8192 hash values
  140. */
  141. DECLARE_HASHTABLE(buf_table, 13);
  142. /* buffer descriptor */
  143. void *buffer_desc_base; /* CPU address */
  144. dma_addr_t buffer_desc_dma_handle; /* dma address */
  145. size_t buffer_desc_size;
  146. struct ubwcp_desc desc_list[UBWCP_BUFFER_DESC_COUNT];
  147. struct ubwcp_image_format_info format_info[INFO_FORMAT_LIST_SIZE];
  148. /* driver state */
  149. enum ubwcp_state state;
  150. atomic_t num_non_lin_buffers;
  151. bool mem_online;
  152. struct mutex desc_lock; /* allocate/free descriptors */
  153. spinlock_t buf_table_lock; /* add/remove dma_buf into list of managed bufffers */
  154. struct mutex mem_hotplug_lock; /* memory hotplug lock */
  155. struct mutex ula_lock; /* allocate/free ula */
  156. struct mutex ubwcp_flush_lock; /* ubwcp flush */
  157. struct mutex hw_range_ck_lock; /* range ck */
  158. struct list_head err_handler_list; /* error handler list */
  159. spinlock_t err_handler_list_lock; /* err_handler_list lock */
  160. struct dev_pagemap pgmap;
  161. /* power state tracking */
  162. int power_on;
  163. struct mutex power_ctrl_lock;
  164. struct ubwcp_prefetch_tgt_ctrl ctrl;
  165. };
  166. struct ubwcp_buf {
  167. struct hlist_node hnode;
  168. struct ubwcp_driver *ubwcp;
  169. struct ubwcp_buffer_attrs buf_attr;
  170. bool perm;
  171. struct ubwcp_desc *desc;
  172. bool buf_attr_set;
  173. enum dma_data_direction dma_dir;
  174. int lock_count;
  175. /* dma_buf info */
  176. struct dma_buf *dma_buf;
  177. struct dma_buf_attachment *attachment;
  178. struct sg_table *sgt;
  179. /* ula info */
  180. phys_addr_t ula_pa;
  181. size_t ula_size;
  182. /* meta metadata */
  183. struct ubwcp_hw_meta_metadata mmdata;
  184. struct mutex lock;
  185. };
  186. static struct ubwcp_driver *me;
  187. static u32 ubwcp_debug_trace_enable;
  188. static void prefetch_tgt_per_cpu(void *info)
  189. {
  190. int ret = 0;
  191. struct ubwcp_prefetch_tgt_ctrl *ctrl;
  192. ctrl = (struct ubwcp_prefetch_tgt_ctrl *) info;
  193. ret = qcom_scm_prefetch_tgt_ctrl(ctrl->enable);
  194. if (ret) {
  195. ctrl->result = ret;
  196. ERR("scm call failed, ret: %d enable: %d", ret, ctrl->enable);
  197. }
  198. atomic_dec(&ctrl->cpu_count);
  199. }
  200. /* Enable/disable generation of prefetch target opcode. smc call must be done from each core
  201. * to update the core specific register. Not thread-safe.
  202. */
  203. static int prefetch_tgt(struct ubwcp_driver *ubwcp, bool enable)
  204. {
  205. int cpu;
  206. trace_ubwcp_prefetch_tgt_start(enable);
  207. DBG("enable: %d", enable);
  208. ubwcp->ctrl.enable = enable;
  209. ubwcp->ctrl.result = 0;
  210. atomic_set(&ubwcp->ctrl.cpu_count, 0);
  211. cpus_read_lock();
  212. for_each_cpu(cpu, cpu_online_mask) {
  213. atomic_inc(&ubwcp->ctrl.cpu_count);
  214. smp_call_function_single(cpu, prefetch_tgt_per_cpu, (void *) &ubwcp->ctrl, false);
  215. }
  216. cpus_read_unlock();
  217. while (atomic_read(&ubwcp->ctrl.cpu_count))
  218. ;
  219. DBG("done");
  220. trace_ubwcp_prefetch_tgt_end(enable);
  221. return ubwcp->ctrl.result;
  222. }
  223. static struct ubwcp_driver *ubwcp_get_driver(void)
  224. {
  225. if (!me)
  226. WARN(1, "ubwcp: driver ptr requested but driver not initialized");
  227. return me;
  228. }
  229. static void image_format_init(struct ubwcp_driver *ubwcp)
  230. { /* planes, bytes/p, Tp , MTp */
  231. ubwcp->format_info[RGBA] = (struct ubwcp_image_format_info)
  232. {1, {{4, 1, {16, 4}, {64, 16}}}};
  233. ubwcp->format_info[NV12] = (struct ubwcp_image_format_info)
  234. {2, {{1, 1, {32, 8}, {128, 32}},
  235. {2, 1, {16, 8}, { 64, 32}}}};
  236. ubwcp->format_info[NV124R] = (struct ubwcp_image_format_info)
  237. {2, {{1, 1, {64, 4}, {256, 16}},
  238. {2, 1, {32, 4}, {128, 16}}}};
  239. ubwcp->format_info[P010] = (struct ubwcp_image_format_info)
  240. {2, {{2, 1, {32, 4}, {128, 16}},
  241. {4, 1, {16, 4}, { 64, 16}}}};
  242. ubwcp->format_info[TP10] = (struct ubwcp_image_format_info)
  243. {2, {{4, 3, {48, 4}, {192, 16}},
  244. {8, 3, {24, 4}, { 96, 16}}}};
  245. ubwcp->format_info[P016] = (struct ubwcp_image_format_info)
  246. {2, {{2, 1, {32, 4}, {128, 16}},
  247. {4, 1, {16, 4}, { 64, 16}}}};
  248. }
  249. static void ubwcp_buf_desc_list_init(struct ubwcp_driver *ubwcp)
  250. {
  251. int idx;
  252. struct ubwcp_desc *desc_list = ubwcp->desc_list;
  253. for (idx = 0; idx < UBWCP_BUFFER_DESC_COUNT; idx++) {
  254. desc_list[idx].idx = -1;
  255. desc_list[idx].ptr = NULL;
  256. }
  257. }
  258. static int ubwcp_init_clocks(struct ubwcp_driver *ubwcp, struct device *dev)
  259. {
  260. const char *cname;
  261. struct property *prop;
  262. int i;
  263. ubwcp->num_clocks =
  264. of_property_count_strings(dev->of_node, "clock-names");
  265. if (ubwcp->num_clocks < 1) {
  266. ubwcp->num_clocks = 0;
  267. return 0;
  268. }
  269. ubwcp->clocks = devm_kzalloc(dev,
  270. sizeof(*ubwcp->clocks) * ubwcp->num_clocks, GFP_KERNEL);
  271. if (!ubwcp->clocks)
  272. return -ENOMEM;
  273. i = 0;
  274. of_property_for_each_string(dev->of_node, "clock-names",
  275. prop, cname) {
  276. struct clk *c = devm_clk_get(dev, cname);
  277. if (IS_ERR(c)) {
  278. ERR("Couldn't get clock: %s\n", cname);
  279. return PTR_ERR(c);
  280. }
  281. ubwcp->clocks[i] = c;
  282. ++i;
  283. }
  284. return 0;
  285. }
  286. static int ubwcp_enable_clocks(struct ubwcp_driver *ubwcp)
  287. {
  288. int i, ret = 0;
  289. for (i = 0; i < ubwcp->num_clocks; ++i) {
  290. ret = clk_prepare_enable(ubwcp->clocks[i]);
  291. if (ret) {
  292. ERR("Couldn't enable clock #%d\n", i);
  293. while (i--)
  294. clk_disable_unprepare(ubwcp->clocks[i]);
  295. break;
  296. }
  297. }
  298. return ret;
  299. }
  300. static void ubwcp_disable_clocks(struct ubwcp_driver *ubwcp)
  301. {
  302. int i;
  303. for (i = ubwcp->num_clocks; i; --i)
  304. clk_disable_unprepare(ubwcp->clocks[i - 1]);
  305. }
  306. /* UBWCP Power control
  307. * Due to hw bug, ubwcp block cannot handle prefetch target opcode. Thus we disable the opcode
  308. * when ubwcp is powered on and enable it back when ubwcp is powered off.
  309. */
  310. static int ubwcp_power(struct ubwcp_driver *ubwcp, bool enable)
  311. {
  312. int ret = 0;
  313. mutex_lock(&ubwcp->power_ctrl_lock);
  314. if (enable) {
  315. ubwcp->power_on++;
  316. if (ubwcp->power_on != 1)
  317. goto done;
  318. } else {
  319. ubwcp->power_on--;
  320. if (ubwcp->power_on != 0)
  321. goto done;
  322. }
  323. if (enable) {
  324. ret = prefetch_tgt(ubwcp, 0);
  325. if (ret)
  326. goto done;
  327. ret = regulator_enable(ubwcp->vdd);
  328. if (ret) {
  329. ERR("regulator call (enable: %d) failed: %d", enable, ret);
  330. goto done;
  331. }
  332. ret = ubwcp_enable_clocks(ubwcp);
  333. if (ret) {
  334. ERR("enable clocks failed: %d", ret);
  335. regulator_disable(ubwcp->vdd);
  336. goto done;
  337. }
  338. } else {
  339. ret = regulator_disable(ubwcp->vdd);
  340. if (ret) {
  341. ERR("regulator call (enable: %d) failed: %d", enable, ret);
  342. goto done;
  343. }
  344. ubwcp_disable_clocks(ubwcp);
  345. ret = prefetch_tgt(ubwcp, 1);
  346. }
  347. done:
  348. mutex_unlock(&ubwcp->power_ctrl_lock);
  349. return ret;
  350. }
  351. /* get ubwcp_buf corresponding to the given dma_buf */
  352. static struct ubwcp_buf *dma_buf_to_ubwcp_buf(struct dma_buf *dmabuf)
  353. {
  354. struct ubwcp_buf *buf = NULL;
  355. struct ubwcp_buf *ret_buf = NULL;
  356. struct ubwcp_driver *ubwcp = ubwcp_get_driver();
  357. unsigned long flags;
  358. if (!dmabuf || !ubwcp)
  359. return NULL;
  360. spin_lock_irqsave(&ubwcp->buf_table_lock, flags);
  361. /* look up ubwcp_buf corresponding to this dma_buf */
  362. hash_for_each_possible(ubwcp->buf_table, buf, hnode, (u64)dmabuf) {
  363. if (buf->dma_buf == dmabuf) {
  364. ret_buf = buf;
  365. break;
  366. }
  367. }
  368. spin_unlock_irqrestore(&ubwcp->buf_table_lock, flags);
  369. return ret_buf;
  370. }
  371. /* return ubwcp hardware version */
  372. int ubwcp_get_hw_version(struct ubwcp_ioctl_hw_version *ver)
  373. {
  374. struct ubwcp_driver *ubwcp;
  375. if (!ver) {
  376. ERR("invalid version ptr");
  377. return -EINVAL;
  378. }
  379. ubwcp = ubwcp_get_driver();
  380. if (!ubwcp)
  381. return -1;
  382. if (ubwcp->state == UBWCP_STATE_INVALID)
  383. return -EPERM;
  384. ver->major = ubwcp->hw_ver_major;
  385. ver->minor = ubwcp->hw_ver_minor;
  386. return 0;
  387. }
  388. EXPORT_SYMBOL(ubwcp_get_hw_version);
  389. static int ula_add_mem(struct ubwcp_driver *ubwcp)
  390. {
  391. int ret = 0;
  392. int nid;
  393. void *ptr;
  394. nid = memory_add_physaddr_to_nid(ubwcp->ula_pool_base);
  395. DBG("calling memremap_pages()...");
  396. ubwcp->pgmap.type = MEMORY_DEVICE_GENERIC;
  397. ubwcp->pgmap.nr_range = 1;
  398. ubwcp->pgmap.range.start = ubwcp->ula_pool_base;
  399. ubwcp->pgmap.range.end = ubwcp->ula_pool_base + ubwcp->ula_pool_size - 1;
  400. trace_ubwcp_memremap_pages_start(ubwcp->ula_pool_size);
  401. ptr = memremap_pages(&ubwcp->pgmap, nid);
  402. trace_ubwcp_memremap_pages_end(ubwcp->ula_pool_size);
  403. if (IS_ERR(ptr)) {
  404. ret = IS_ERR(ptr);
  405. ERR("memremap_pages() failed st:0x%lx sz:0x%lx err: %d",
  406. ubwcp->ula_pool_base,
  407. ubwcp->ula_pool_size,
  408. ret);
  409. } else {
  410. DBG("memremap_pages() ula_pool_base:0x%llx, size:0x%zx, kernel addr:0x%p",
  411. ubwcp->ula_pool_base,
  412. ubwcp->ula_pool_size,
  413. page_to_virt(pfn_to_page(PFN_DOWN(ubwcp->ula_pool_base))));
  414. }
  415. return ret;
  416. }
  417. static int ula_map_uncached(u64 base, u64 size)
  418. {
  419. int ret;
  420. trace_ubwcp_set_direct_map_range_uncached_start(size);
  421. ret = set_direct_map_range_uncached((unsigned long)phys_to_virt(base), size >> PAGE_SHIFT);
  422. trace_ubwcp_set_direct_map_range_uncached_end(size);
  423. if (ret)
  424. ERR("set_direct_map_range_uncached failed st:0x%lx num pages:%lu err: %d",
  425. base, size >> PAGE_SHIFT, ret);
  426. return ret;
  427. }
  428. static void ula_unmap(struct ubwcp_driver *ubwcp)
  429. {
  430. DBG("Calling memunmap_pages() for ULA PA pool");
  431. trace_ubwcp_memunmap_pages_start(ubwcp->ula_pool_size);
  432. memunmap_pages(&ubwcp->pgmap);
  433. trace_ubwcp_memunmap_pages_end(ubwcp->ula_pool_size);
  434. }
  435. static void ula_sync_for_cpu(struct device *dev, u64 addr, unsigned long size)
  436. {
  437. trace_ubwcp_dma_sync_single_for_cpu_start(size, DMA_BIDIRECTIONAL);
  438. dma_sync_single_for_cpu(dev, addr, size, DMA_BIDIRECTIONAL);
  439. trace_ubwcp_dma_sync_single_for_cpu_end(size, DMA_BIDIRECTIONAL);
  440. }
  441. /** Remove ula memory in chunks
  442. * Abort if new buffer addition is detected
  443. * If remove succeeds or aborted, return success
  444. * status value indicates if mem was removed or aborted (not removed)
  445. * Otherwise return failure
  446. */
  447. static int ula_remove_mem(struct ubwcp_driver *ubwcp, enum ula_remove_mem_status *status)
  448. {
  449. int ret = 0;
  450. unsigned long sync_remain = ubwcp->ula_pool_size;
  451. unsigned long sync_offset = 0;
  452. unsigned long sync_size = 0;
  453. ret = ula_map_uncached(ubwcp->ula_pool_base, ubwcp->ula_pool_size);
  454. if (ret)
  455. return ret;
  456. trace_ubwcp_offline_sync_start(ubwcp->ula_pool_size);
  457. while (sync_remain > 0) {
  458. if (atomic_read(&ubwcp->num_non_lin_buffers) > 0) {
  459. trace_ubwcp_offline_sync_end(ubwcp->ula_pool_size);
  460. ula_unmap(ubwcp);
  461. if (ula_add_mem(ubwcp)) {
  462. ERR("remove mem: failed to add back during abort");
  463. return -1;
  464. }
  465. *status = ULA_REMOVE_MEM_ABORTED;
  466. return 0;
  467. }
  468. if (UBWCP_SYNC_GRANULE > sync_remain) {
  469. sync_size = sync_remain;
  470. sync_remain = 0;
  471. } else {
  472. sync_size = UBWCP_SYNC_GRANULE;
  473. sync_remain -= UBWCP_SYNC_GRANULE;
  474. }
  475. ula_sync_for_cpu(ubwcp->dev, ubwcp->ula_pool_base + sync_offset, sync_size);
  476. sync_offset += sync_size;
  477. }
  478. trace_ubwcp_offline_sync_end(ubwcp->ula_pool_size);
  479. ula_unmap(ubwcp);
  480. *status = ULA_REMOVE_MEM_SUCCESS;
  481. return 0;
  482. }
  483. static int inc_num_non_lin_buffers(struct ubwcp_driver *ubwcp)
  484. {
  485. atomic_inc(&ubwcp->num_non_lin_buffers);
  486. mutex_lock(&ubwcp->mem_hotplug_lock);
  487. if (!ubwcp->mem_online) {
  488. if (atomic_read(&ubwcp->num_non_lin_buffers) == 0) {
  489. ERR("Bad state: num_non_lin_buffers should not be 0");
  490. goto err;
  491. }
  492. if (ubwcp_power(ubwcp, true))
  493. goto err;
  494. if (ula_add_mem(ubwcp))
  495. goto err_add_memory;
  496. ubwcp->mem_online = true;
  497. }
  498. mutex_unlock(&ubwcp->mem_hotplug_lock);
  499. return 0;
  500. err_add_memory:
  501. ubwcp_power(ubwcp, false);
  502. err:
  503. atomic_dec(&ubwcp->num_non_lin_buffers);
  504. mutex_unlock(&ubwcp->mem_hotplug_lock);
  505. ubwcp->state = UBWCP_STATE_FAULT;
  506. ERR("state set to fault");
  507. return -1;
  508. }
  509. static int dec_num_non_lin_buffers(struct ubwcp_driver *ubwcp)
  510. {
  511. int ret;
  512. enum ula_remove_mem_status remove_status;
  513. atomic_dec(&ubwcp->num_non_lin_buffers);
  514. mutex_lock(&ubwcp->mem_hotplug_lock);
  515. if (atomic_read(&ubwcp->num_non_lin_buffers) == 0) {
  516. DBG("last buffer: ~~~~~~~~~~~");
  517. if (!ubwcp->mem_online) {
  518. ERR("Bad state: mem_online should not be false");
  519. goto err;
  520. }
  521. ret = ula_remove_mem(ubwcp, &remove_status);
  522. if (ret)
  523. goto err;
  524. if (remove_status == ULA_REMOVE_MEM_SUCCESS) {
  525. ubwcp->mem_online = false;
  526. if (ubwcp_power(ubwcp, false))
  527. goto err;
  528. } else if (remove_status == ULA_REMOVE_MEM_ABORTED) {
  529. DBG("ula memory offline aborted");
  530. } else {
  531. ERR("unexpected ula remove status: %d", remove_status);
  532. goto err;
  533. }
  534. }
  535. mutex_unlock(&ubwcp->mem_hotplug_lock);
  536. return 0;
  537. err:
  538. atomic_inc(&ubwcp->num_non_lin_buffers);
  539. mutex_unlock(&ubwcp->mem_hotplug_lock);
  540. ubwcp->state = UBWCP_STATE_FAULT;
  541. ERR("state set to fault");
  542. return -1;
  543. }
  544. /**
  545. *
  546. * Initialize ubwcp buffer for the given dma_buf. This
  547. * initializes ubwcp internal data structures and possibly hw to
  548. * use ubwcp for this buffer.
  549. *
  550. * @param dmabuf : ptr to the buffer to be configured for ubwcp
  551. *
  552. * @return int : 0 on success, otherwise error code
  553. */
  554. static int ubwcp_init_buffer(struct dma_buf *dmabuf)
  555. {
  556. struct ubwcp_buf *buf;
  557. struct ubwcp_driver *ubwcp = ubwcp_get_driver();
  558. unsigned long flags;
  559. trace_ubwcp_init_buffer_start(dmabuf);
  560. if (!ubwcp) {
  561. trace_ubwcp_init_buffer_end(dmabuf);
  562. return -1;
  563. }
  564. if (ubwcp->state != UBWCP_STATE_READY) {
  565. ERR("driver in invalid state: %d", ubwcp->state);
  566. trace_ubwcp_init_buffer_end(dmabuf);
  567. return -EPERM;
  568. }
  569. if (!dmabuf) {
  570. ERR("NULL dmabuf input ptr");
  571. trace_ubwcp_init_buffer_end(dmabuf);
  572. return -EINVAL;
  573. }
  574. if (dma_buf_to_ubwcp_buf(dmabuf)) {
  575. ERR("dma_buf already initialized for ubwcp");
  576. trace_ubwcp_init_buffer_end(dmabuf);
  577. return -EEXIST;
  578. }
  579. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  580. if (!buf) {
  581. ERR("failed to alloc for new ubwcp_buf");
  582. trace_ubwcp_init_buffer_end(dmabuf);
  583. return -ENOMEM;
  584. }
  585. mutex_init(&buf->lock);
  586. buf->dma_buf = dmabuf;
  587. buf->ubwcp = ubwcp;
  588. buf->buf_attr.image_format = UBWCP_LINEAR;
  589. spin_lock_irqsave(&ubwcp->buf_table_lock, flags);
  590. hash_add(ubwcp->buf_table, &buf->hnode, (u64)buf->dma_buf);
  591. spin_unlock_irqrestore(&ubwcp->buf_table_lock, flags);
  592. trace_ubwcp_init_buffer_end(dmabuf);
  593. return 0;
  594. }
  595. static void dump_attributes(struct ubwcp_buffer_attrs *attr)
  596. {
  597. DBG_BUF_ATTR("");
  598. DBG_BUF_ATTR("image_format: %d", attr->image_format);
  599. DBG_BUF_ATTR("major_ubwc_ver: %d", attr->major_ubwc_ver);
  600. DBG_BUF_ATTR("minor_ubwc_ver: %d", attr->minor_ubwc_ver);
  601. DBG_BUF_ATTR("compression_type: %d", attr->compression_type);
  602. DBG_BUF_ATTR("lossy_params: %llu", attr->lossy_params);
  603. DBG_BUF_ATTR("width: %d", attr->width);
  604. DBG_BUF_ATTR("height: %d", attr->height);
  605. DBG_BUF_ATTR("stride: %d", attr->stride);
  606. DBG_BUF_ATTR("scanlines: %d", attr->scanlines);
  607. DBG_BUF_ATTR("planar_padding: %d", attr->planar_padding);
  608. DBG_BUF_ATTR("subsample: %d", attr->subsample);
  609. DBG_BUF_ATTR("sub_system_target: %d", attr->sub_system_target);
  610. DBG_BUF_ATTR("y_offset: %d", attr->y_offset);
  611. DBG_BUF_ATTR("batch_size: %d", attr->batch_size);
  612. DBG_BUF_ATTR("");
  613. }
  614. static int to_std_format(u16 ioctl_image_format, enum ubwcp_std_image_format *format)
  615. {
  616. switch (ioctl_image_format) {
  617. case UBWCP_RGBA8888:
  618. *format = RGBA;
  619. return 0;
  620. case UBWCP_NV12:
  621. case UBWCP_NV12_Y:
  622. case UBWCP_NV12_UV:
  623. *format = NV12;
  624. return 0;
  625. case UBWCP_NV124R:
  626. case UBWCP_NV124R_Y:
  627. case UBWCP_NV124R_UV:
  628. *format = NV124R;
  629. return 0;
  630. case UBWCP_TP10:
  631. case UBWCP_TP10_Y:
  632. case UBWCP_TP10_UV:
  633. *format = TP10;
  634. return 0;
  635. case UBWCP_P010:
  636. case UBWCP_P010_Y:
  637. case UBWCP_P010_UV:
  638. *format = P010;
  639. return 0;
  640. case UBWCP_P016:
  641. case UBWCP_P016_Y:
  642. case UBWCP_P016_UV:
  643. *format = P016;
  644. return 0;
  645. default:
  646. ERR("Failed to convert ioctl image format to std format: %d", ioctl_image_format);
  647. return -1;
  648. }
  649. }
  650. static int std_to_hw_img_fmt(enum ubwcp_std_image_format format, u16 *hw_fmt)
  651. {
  652. switch (format) {
  653. case RGBA:
  654. *hw_fmt = HW_BUFFER_FORMAT_RGBA;
  655. return 0;
  656. case NV12:
  657. *hw_fmt = HW_BUFFER_FORMAT_NV12;
  658. return 0;
  659. case NV124R:
  660. *hw_fmt = HW_BUFFER_FORMAT_NV124R;
  661. return 0;
  662. case P010:
  663. *hw_fmt = HW_BUFFER_FORMAT_P010;
  664. return 0;
  665. case TP10:
  666. *hw_fmt = HW_BUFFER_FORMAT_TP10;
  667. return 0;
  668. case P016:
  669. *hw_fmt = HW_BUFFER_FORMAT_P016;
  670. return 0;
  671. default:
  672. ERR("Failed to convert std image format to hw format: %d", format);
  673. return -1;
  674. }
  675. }
  676. static int get_stride_alignment(enum ubwcp_std_image_format format, u16 *align)
  677. {
  678. switch (format) {
  679. case TP10:
  680. *align = 64;
  681. return 0;
  682. case NV12:
  683. *align = 128;
  684. return 0;
  685. case RGBA:
  686. case NV124R:
  687. case P010:
  688. case P016:
  689. *align = 256;
  690. return 0;
  691. default:
  692. return -1;
  693. }
  694. }
  695. /* returns stride of compressed image */
  696. static u32 get_compressed_stride(struct ubwcp_driver *ubwcp,
  697. enum ubwcp_std_image_format format, u32 width)
  698. {
  699. struct ubwcp_plane_info p_info;
  700. u16 macro_tile_width_p;
  701. u16 pixel_bytes;
  702. u16 per_pixel;
  703. p_info = ubwcp->format_info[format].p_info[0];
  704. macro_tile_width_p = p_info.macrotilesize_p.width;
  705. pixel_bytes = p_info.pixel_bytes;
  706. per_pixel = p_info.per_pixel;
  707. return UBWCP_ALIGN(width, macro_tile_width_p)*pixel_bytes/per_pixel;
  708. }
  709. static void
  710. ubwcp_pixel_to_bytes(struct ubwcp_driver *ubwcp,
  711. enum ubwcp_std_image_format format,
  712. u32 width_p, u32 height_p,
  713. u32 *width_b, u32 *height_b)
  714. {
  715. u16 pixel_bytes;
  716. u16 per_pixel;
  717. struct ubwcp_image_format_info f_info;
  718. struct ubwcp_plane_info p_info;
  719. f_info = ubwcp->format_info[format];
  720. p_info = f_info.p_info[0];
  721. pixel_bytes = p_info.pixel_bytes;
  722. per_pixel = p_info.per_pixel;
  723. *width_b = (width_p*pixel_bytes)/per_pixel;
  724. *height_b = (height_p*pixel_bytes)/per_pixel;
  725. }
  726. /* check if linear stride conforms to hw limitations
  727. * always returns false for linear image
  728. */
  729. static bool stride_is_valid(struct ubwcp_driver *ubwcp,
  730. enum ubwcp_std_image_format format, u32 width, u32 lin_stride)
  731. {
  732. u32 compressed_stride;
  733. u32 width_b;
  734. u32 height_b;
  735. ubwcp_pixel_to_bytes(ubwcp, format, width, 0, &width_b, &height_b);
  736. if ((lin_stride < width_b) || (lin_stride > MAX_ATTR_STRIDE)) {
  737. ERR("Invalid stride: %u width: %u width_b: %u", lin_stride, width, width_b);
  738. return false;
  739. }
  740. if (format == TP10) {
  741. if(!IS_ALIGNED(lin_stride, 64)) {
  742. ERR("stride must be aligned to 64: %d", lin_stride);
  743. return false;
  744. }
  745. } else {
  746. compressed_stride = get_compressed_stride(ubwcp, format, width);
  747. if (lin_stride != compressed_stride) {
  748. ERR("linear stride: %d must be same as compressed stride: %d",
  749. lin_stride, compressed_stride);
  750. return false;
  751. }
  752. }
  753. return true;
  754. }
  755. static bool ioctl_format_is_valid(u16 ioctl_image_format)
  756. {
  757. switch (ioctl_image_format) {
  758. case UBWCP_LINEAR:
  759. case UBWCP_RGBA8888:
  760. case UBWCP_NV12:
  761. case UBWCP_NV12_Y:
  762. case UBWCP_NV12_UV:
  763. case UBWCP_NV124R:
  764. case UBWCP_NV124R_Y:
  765. case UBWCP_NV124R_UV:
  766. case UBWCP_TP10:
  767. case UBWCP_TP10_Y:
  768. case UBWCP_TP10_UV:
  769. case UBWCP_P010:
  770. case UBWCP_P010_Y:
  771. case UBWCP_P010_UV:
  772. case UBWCP_P016:
  773. case UBWCP_P016_Y:
  774. case UBWCP_P016_UV:
  775. return true;
  776. default:
  777. return false;
  778. }
  779. }
  780. /* validate buffer attributes */
  781. static bool ubwcp_buf_attrs_valid(struct ubwcp_driver *ubwcp, struct ubwcp_buffer_attrs *attr)
  782. {
  783. enum ubwcp_std_image_format format;
  784. if (attr->unused1 || attr->unused2 || attr->unused3 || attr->unused4 || attr->unused5 ||
  785. attr->unused6 || attr->unused7 || attr->unused8 || attr->unused9) {
  786. ERR("buf attr unused values must be set to 0");
  787. goto err;
  788. }
  789. if (!ioctl_format_is_valid(attr->image_format)) {
  790. ERR("invalid image format: %d", attr->image_format);
  791. goto err;
  792. }
  793. /* rest of the fields are ignored for linear format */
  794. if (attr->image_format == UBWCP_LINEAR) {
  795. goto valid;
  796. }
  797. if (to_std_format(attr->image_format, &format))
  798. goto err;
  799. if (attr->major_ubwc_ver || attr->minor_ubwc_ver) {
  800. ERR("major/minor ubwc ver must be 0. major: %d minor: %d",
  801. attr->major_ubwc_ver, attr->minor_ubwc_ver);
  802. goto err;
  803. }
  804. if (attr->compression_type != UBWCP_COMPRESSION_LOSSLESS) {
  805. ERR("compression_type is not valid: %d",
  806. attr->compression_type);
  807. goto err;
  808. }
  809. if (attr->lossy_params != 0) {
  810. ERR("lossy_params is not valid: %d", attr->lossy_params);
  811. goto err;
  812. }
  813. if (attr->width > MAX_ATTR_WIDTH) {
  814. ERR("width is invalid (above upper limit): %d", attr->width);
  815. goto err;
  816. }
  817. if (attr->height > MAX_ATTR_HEIGHT) {
  818. ERR("height is invalid (above upper limit): %d", attr->height);
  819. goto err;
  820. }
  821. if(!stride_is_valid(ubwcp, format, attr->width, attr->stride)) {
  822. ERR("stride is invalid: %d", attr->stride);
  823. goto err;
  824. }
  825. if ((attr->scanlines < attr->height) ||
  826. (attr->scanlines > attr->height + MAX_ATTR_SCANLN_HT_DELTA)) {
  827. ERR("scanlines is not valid - height: %d scanlines: %d",
  828. attr->height, attr->scanlines);
  829. goto err;
  830. }
  831. if (attr->planar_padding > MAX_ATTR_PLANAR_PAD) {
  832. ERR("planar_padding is not valid: %d", attr->planar_padding);
  833. goto err;
  834. }
  835. if (attr->subsample != UBWCP_SUBSAMPLE_4_2_0) {
  836. ERR("subsample is not valid: %d", attr->subsample);
  837. goto err;
  838. }
  839. if (attr->sub_system_target & ~UBWCP_SUBSYSTEM_TARGET_CPU) {
  840. ERR("sub_system_target other that CPU is not supported: %d",
  841. attr->sub_system_target);
  842. goto err;
  843. }
  844. if (!(attr->sub_system_target & UBWCP_SUBSYSTEM_TARGET_CPU)) {
  845. ERR("sub_system_target is not set to CPU: %d",
  846. attr->sub_system_target);
  847. goto err;
  848. }
  849. if (attr->y_offset != 0) {
  850. ERR("y_offset is not valid: %d", attr->y_offset);
  851. goto err;
  852. }
  853. if (attr->batch_size != 1) {
  854. ERR("batch_size is not valid: %d", attr->batch_size);
  855. goto err;
  856. }
  857. valid:
  858. dump_attributes(attr);
  859. return true;
  860. err:
  861. dump_attributes(attr);
  862. return false;
  863. }
  864. /* calculate and return metadata buffer size for a given plane
  865. * and buffer attributes
  866. */
  867. static int metadata_buf_sz(struct ubwcp_driver *ubwcp,
  868. enum ubwcp_std_image_format format,
  869. u32 width, u32 height, u8 plane, size_t *size)
  870. {
  871. u64 pitch;
  872. u64 lines;
  873. u64 tile_width;
  874. u32 tile_height;
  875. struct ubwcp_image_format_info f_info;
  876. struct ubwcp_plane_info p_info;
  877. f_info = ubwcp->format_info[format];
  878. DBG_BUF_ATTR("");
  879. DBG_BUF_ATTR("");
  880. DBG_BUF_ATTR("Calculating metadata buffer size: format = %d, plane = %d", format, plane);
  881. if (plane >= f_info.planes) {
  882. ERR("Missing plane info: format: %d, plane: %d", format, plane);
  883. return -1;
  884. }
  885. p_info = f_info.p_info[plane];
  886. /* UV plane */
  887. if (plane == 1) {
  888. width = width/2;
  889. height = height/2;
  890. }
  891. tile_width = p_info.tilesize_p.width;
  892. tile_height = p_info.tilesize_p.height;
  893. /* pitch: # of tiles in a row
  894. * lines: # of tile rows
  895. */
  896. pitch = UBWCP_ALIGN((width + tile_width - 1)/tile_width, META_DATA_PITCH_ALIGN);
  897. lines = UBWCP_ALIGN((height + tile_height - 1)/tile_height, META_DATA_HEIGHT_ALIGN);
  898. DBG_BUF_ATTR("image params : %d x %d (pixels)", width, height);
  899. DBG_BUF_ATTR("tile params : %d x %d (pixels)", tile_width, tile_height);
  900. DBG_BUF_ATTR("pitch : %d (%d)", pitch, width/tile_width);
  901. DBG_BUF_ATTR("lines : %d (%d)", lines, height);
  902. DBG_BUF_ATTR("size (p*l*bytes) : %d", pitch*lines*1);
  903. /* x1 below is only to clarify that we are multiplying by 1 bytes/tile */
  904. *size = UBWCP_ALIGN(pitch*lines*1, META_DATA_SIZE_ALIGN);
  905. DBG_BUF_ATTR("size (aligned 4K): %zu (0x%zx)", *size, *size);
  906. return 0;
  907. }
  908. /* calculate and return size of pixel data buffer for a given plane
  909. * and buffer attributes
  910. */
  911. static int pixeldata_buf_sz(struct ubwcp_driver *ubwcp,
  912. u16 format, u32 width,
  913. u32 height, u8 plane, size_t *size)
  914. {
  915. u64 pitch;
  916. u64 lines;
  917. u16 pixel_bytes;
  918. u16 per_pixel;
  919. u64 macro_tile_width_p;
  920. u64 macro_tile_height_p;
  921. struct ubwcp_image_format_info f_info;
  922. struct ubwcp_plane_info p_info;
  923. f_info = ubwcp->format_info[format];
  924. DBG_BUF_ATTR("");
  925. DBG_BUF_ATTR("");
  926. DBG_BUF_ATTR("Calculating Pixeldata buffer size: format = %d, plane = %d", format, plane);
  927. if (plane >= f_info.planes) {
  928. ERR("Missing plane info: format: %d, plane: %d", format, plane);
  929. return -1;
  930. }
  931. p_info = f_info.p_info[plane];
  932. pixel_bytes = p_info.pixel_bytes;
  933. per_pixel = p_info.per_pixel;
  934. /* UV plane */
  935. if (plane == 1) {
  936. width = width/2;
  937. height = height/2;
  938. }
  939. macro_tile_width_p = p_info.macrotilesize_p.width;
  940. macro_tile_height_p = p_info.macrotilesize_p.height;
  941. /* align pixel width and height macro tile width and height */
  942. pitch = UBWCP_ALIGN(width, macro_tile_width_p);
  943. lines = UBWCP_ALIGN(height, macro_tile_height_p);
  944. DBG_BUF_ATTR("image params : %d x %d (pixels)", width, height);
  945. DBG_BUF_ATTR("macro tile params: %d x %d (pixels)", macro_tile_width_p,
  946. macro_tile_height_p);
  947. DBG_BUF_ATTR("bytes_per_pixel : %d/%d", pixel_bytes, per_pixel);
  948. DBG_BUF_ATTR("pitch : %d", pitch);
  949. DBG_BUF_ATTR("lines : %d", lines);
  950. DBG_BUF_ATTR("size (p*l*bytes) : %d", (pitch*lines*pixel_bytes)/per_pixel);
  951. *size = UBWCP_ALIGN((pitch*lines*pixel_bytes)/per_pixel, PIXEL_DATA_SIZE_ALIGN);
  952. DBG_BUF_ATTR("size (aligned 4K): %zu (0x%zx)", *size, *size);
  953. return 0;
  954. }
  955. static int get_tile_height(struct ubwcp_driver *ubwcp, enum ubwcp_std_image_format format,
  956. u8 plane)
  957. {
  958. struct ubwcp_image_format_info f_info;
  959. struct ubwcp_plane_info p_info;
  960. f_info = ubwcp->format_info[format];
  961. p_info = f_info.p_info[plane];
  962. return p_info.tilesize_p.height;
  963. }
  964. /*
  965. * plane: must be 0 or 1 (1st plane == 0, 2nd plane == 1)
  966. */
  967. static size_t ubwcp_ula_size(struct ubwcp_driver *ubwcp, u16 format,
  968. u32 stride_b, u32 scanlines, u8 plane,
  969. bool add_tile_pad)
  970. {
  971. size_t size;
  972. DBG_BUF_ATTR("%s(format = %d, plane = %d)", __func__, format, plane);
  973. /* UV plane */
  974. if (plane == 1)
  975. scanlines = scanlines/2;
  976. if (add_tile_pad) {
  977. int tile_height = get_tile_height(ubwcp, format, plane);
  978. /* Align plane size to plane tile height */
  979. scanlines = ((scanlines + tile_height - 1) / tile_height) * tile_height;
  980. }
  981. size = stride_b*scanlines;
  982. DBG_BUF_ATTR("Size of plane-%u: (%u * %u) = %zu (0x%zx)",
  983. plane, stride_b, scanlines, size, size);
  984. return size;
  985. }
  986. static int missing_plane_from_format(u16 ioctl_image_format)
  987. {
  988. int missing_plane;
  989. switch (ioctl_image_format) {
  990. case UBWCP_NV12_Y:
  991. missing_plane = 2;
  992. break;
  993. case UBWCP_NV12_UV:
  994. missing_plane = 1;
  995. break;
  996. case UBWCP_NV124R_Y:
  997. missing_plane = 2;
  998. break;
  999. case UBWCP_NV124R_UV:
  1000. missing_plane = 1;
  1001. break;
  1002. case UBWCP_TP10_Y:
  1003. missing_plane = 2;
  1004. break;
  1005. case UBWCP_TP10_UV:
  1006. missing_plane = 1;
  1007. break;
  1008. case UBWCP_P010_Y:
  1009. missing_plane = 2;
  1010. break;
  1011. case UBWCP_P010_UV:
  1012. missing_plane = 1;
  1013. break;
  1014. case UBWCP_P016_Y:
  1015. missing_plane = 2;
  1016. break;
  1017. case UBWCP_P016_UV:
  1018. missing_plane = 1;
  1019. break;
  1020. default:
  1021. missing_plane = 0;
  1022. }
  1023. return missing_plane;
  1024. }
  1025. static int planes_in_format(enum ubwcp_std_image_format format)
  1026. {
  1027. if (format == RGBA)
  1028. return 1;
  1029. else
  1030. return 2;
  1031. }
  1032. static int ubwcp_validate_uv_align(struct ubwcp_driver *ubwcp,
  1033. struct ubwcp_buffer_attrs *attr,
  1034. size_t ula_y_plane_size,
  1035. size_t uv_start_offset)
  1036. {
  1037. int ret = 0;
  1038. size_t ula_y_plane_size_align;
  1039. size_t y_tile_align_bytes;
  1040. int y_tile_height;
  1041. int planes;
  1042. enum ubwcp_std_image_format format;
  1043. ret = to_std_format(attr->image_format, &format);
  1044. if (ret)
  1045. goto err;
  1046. /* Only validate UV align if there is both a Y and UV plane */
  1047. planes = planes_in_format(format);
  1048. if (planes != 2)
  1049. return 0;
  1050. /* Check it is cache line size aligned */
  1051. if ((uv_start_offset % 64) != 0) {
  1052. ret = -EINVAL;
  1053. ERR("uv_start_offset %zu not cache line aligned",
  1054. uv_start_offset);
  1055. goto err;
  1056. }
  1057. /*
  1058. * Check that UV plane does not overlap with any of the Y plane’s tiles
  1059. */
  1060. y_tile_height = get_tile_height(ubwcp, format, 0);
  1061. y_tile_align_bytes = y_tile_height * attr->stride;
  1062. ula_y_plane_size_align = ((ula_y_plane_size + y_tile_align_bytes - 1) /
  1063. y_tile_align_bytes) * y_tile_align_bytes;
  1064. if (uv_start_offset < ula_y_plane_size_align) {
  1065. ret = -EINVAL;
  1066. ERR("uv offset %zu less than y plane align %zu for y plane size %zu",
  1067. uv_start_offset, ula_y_plane_size_align,
  1068. ula_y_plane_size);
  1069. goto err;
  1070. }
  1071. return 0;
  1072. err:
  1073. return ret;
  1074. }
  1075. /* calculate ULA buffer parms */
  1076. static int ubwcp_calc_ula_params(struct ubwcp_driver *ubwcp,
  1077. struct ubwcp_buffer_attrs *attr,
  1078. size_t *ula_size,
  1079. size_t *ula_y_plane_size,
  1080. size_t *uv_start_offset)
  1081. {
  1082. size_t size;
  1083. enum ubwcp_std_image_format format;
  1084. int planes;
  1085. int missing_plane;
  1086. u32 stride;
  1087. u32 scanlines;
  1088. u32 planar_padding;
  1089. int ret;
  1090. ret = to_std_format(attr->image_format, &format);
  1091. if (ret)
  1092. return ret;
  1093. stride = attr->stride;
  1094. scanlines = attr->scanlines;
  1095. planar_padding = attr->planar_padding;
  1096. /* Number of "expected" planes in "the standard defined" image format */
  1097. planes = planes_in_format(format);
  1098. missing_plane = missing_plane_from_format(attr->image_format);
  1099. DBG_BUF_ATTR("ula params -->");
  1100. DBG_BUF_ATTR("ioctl_image_format : %d, std_format: %d", attr->image_format, format);
  1101. DBG_BUF_ATTR("planes_in_format : %d", planes);
  1102. DBG_BUF_ATTR("missing_plane : %d", missing_plane);
  1103. DBG_BUF_ATTR("Planar Padding : %d", planar_padding);
  1104. if (planes == 1) {
  1105. /* uv_start beyond ULA range */
  1106. size = ubwcp_ula_size(ubwcp, format, stride, scanlines, 0, true);
  1107. *uv_start_offset = size;
  1108. *ula_y_plane_size = size;
  1109. } else {
  1110. if (!missing_plane) {
  1111. /* size for both planes and padding */
  1112. /* Don't pad out Y plane as client would not expect this padding */
  1113. size = ubwcp_ula_size(ubwcp, format, stride, scanlines, 0, false);
  1114. *ula_y_plane_size = size;
  1115. size += planar_padding;
  1116. *uv_start_offset = size;
  1117. size += ubwcp_ula_size(ubwcp, format, stride, scanlines, 1, true);
  1118. } else {
  1119. if (missing_plane == 2) {
  1120. /* Y-only image, set uv_start beyond ULA range */
  1121. size = ubwcp_ula_size(ubwcp, format, stride, scanlines, 0, true);
  1122. *uv_start_offset = size;
  1123. *ula_y_plane_size = size;
  1124. } else {
  1125. /* first plane data is not there */
  1126. size = ubwcp_ula_size(ubwcp, format, stride, scanlines, 1, true);
  1127. *uv_start_offset = 0; /* uv data is at the beginning */
  1128. *ula_y_plane_size = 0;
  1129. }
  1130. }
  1131. }
  1132. *ula_size = UBWCP_ALIGN(size, 4096);
  1133. DBG_BUF_ATTR("ULA_Size: %zu (0x%x) (before 4K align: %zu)", *ula_size, *ula_size, size);
  1134. return 0;
  1135. }
  1136. /* calculate UBWCP buffer parms */
  1137. static int ubwcp_calc_ubwcp_buf_params(struct ubwcp_driver *ubwcp,
  1138. struct ubwcp_buffer_attrs *attr,
  1139. size_t *md_p0, size_t *pd_p0,
  1140. size_t *md_p1, size_t *pd_p1,
  1141. size_t *stride_tp10_b)
  1142. {
  1143. int planes;
  1144. int missing_plane;
  1145. enum ubwcp_std_image_format format;
  1146. size_t stride_tp10_p;
  1147. int ret;
  1148. ret = to_std_format(attr->image_format, &format);
  1149. if (ret)
  1150. return ret;
  1151. missing_plane = missing_plane_from_format(attr->image_format);
  1152. planes = planes_in_format(format);
  1153. DBG_BUF_ATTR("ubwcp params -->");
  1154. DBG_BUF_ATTR("ioctl_image_format : %d, std_format: %d", attr->image_format, format);
  1155. DBG_BUF_ATTR("planes_in_format : %d", planes);
  1156. DBG_BUF_ATTR("missing_plane : %d", missing_plane);
  1157. *md_p0 = 0;
  1158. *pd_p0 = 0;
  1159. *md_p1 = 0;
  1160. *pd_p1 = 0;
  1161. *stride_tp10_b = 0;
  1162. if (missing_plane != 1) {
  1163. if (metadata_buf_sz(ubwcp, format, attr->width, attr->height, 0, md_p0))
  1164. return -1;
  1165. if (pixeldata_buf_sz(ubwcp, format, attr->width, attr->height, 0, pd_p0))
  1166. return -1;
  1167. }
  1168. if ((planes == 2) && (missing_plane != 2)){
  1169. if (metadata_buf_sz(ubwcp, format, attr->width, attr->height, 1, md_p1))
  1170. return -1;
  1171. if (pixeldata_buf_sz(ubwcp, format, attr->width, attr->height, 1, pd_p1))
  1172. return -1;
  1173. }
  1174. if (format == TP10) {
  1175. stride_tp10_p = UBWCP_ALIGN(attr->width, 192);
  1176. *stride_tp10_b = (stride_tp10_p/3) + stride_tp10_p;
  1177. }
  1178. return 0;
  1179. }
  1180. /* reserve ULA address space of the given size */
  1181. static phys_addr_t ubwcp_ula_alloc(struct ubwcp_driver *ubwcp, size_t size)
  1182. {
  1183. phys_addr_t pa;
  1184. mutex_lock(&ubwcp->ula_lock);
  1185. pa = gen_pool_alloc(ubwcp->ula_pool, size);
  1186. mutex_unlock(&ubwcp->ula_lock);
  1187. return pa;
  1188. }
  1189. /* free ULA address space of the given address and size */
  1190. static void ubwcp_ula_free(struct ubwcp_driver *ubwcp, phys_addr_t pa, size_t size)
  1191. {
  1192. mutex_lock(&ubwcp->ula_lock);
  1193. if (!gen_pool_has_addr(ubwcp->ula_pool, pa, size)) {
  1194. ERR("Attempt to free mem not from gen_pool: pa: %p, size: %zx", pa, size);
  1195. goto err;
  1196. }
  1197. DBG("addr: %p, size: %zx", pa, size);
  1198. gen_pool_free(ubwcp->ula_pool, pa, size);
  1199. mutex_unlock(&ubwcp->ula_lock);
  1200. return;
  1201. err:
  1202. mutex_unlock(&ubwcp->ula_lock);
  1203. }
  1204. /* free up or expand current_pa and return the new pa */
  1205. static phys_addr_t ubwcp_ula_realloc(struct ubwcp_driver *ubwcp,
  1206. phys_addr_t pa,
  1207. size_t size,
  1208. size_t new_size)
  1209. {
  1210. if (size == new_size)
  1211. return pa;
  1212. if (pa)
  1213. ubwcp_ula_free(ubwcp, pa, size);
  1214. return ubwcp_ula_alloc(ubwcp, new_size);
  1215. }
  1216. /* unmap dma buf */
  1217. static void ubwcp_dma_unmap(struct ubwcp_buf *buf)
  1218. {
  1219. if (buf->dma_buf && buf->attachment) {
  1220. DBG("Calling dma_buf_unmap_attachment()");
  1221. dma_buf_unmap_attachment(buf->attachment, buf->sgt, DMA_BIDIRECTIONAL);
  1222. buf->sgt = NULL;
  1223. dma_buf_detach(buf->dma_buf, buf->attachment);
  1224. buf->attachment = NULL;
  1225. }
  1226. }
  1227. static bool verify_dma_buf_size(struct ubwcp_buf *buf, size_t min_size)
  1228. {
  1229. size_t dma_len;
  1230. dma_len = sg_dma_len(buf->sgt->sgl);
  1231. if (dma_len < min_size) {
  1232. ERR("dma len: %zu is less than min ubwcp buffer size: %zu", dma_len, min_size);
  1233. return false;
  1234. } else
  1235. return true;
  1236. }
  1237. /* dma map ubwcp buffer */
  1238. static int ubwcp_dma_map(struct ubwcp_buf *buf,
  1239. struct device *dev,
  1240. dma_addr_t *iova)
  1241. {
  1242. int ret = 0;
  1243. struct dma_buf *dma_buf = buf->dma_buf;
  1244. struct dma_buf_attachment *attachment;
  1245. struct sg_table *sgt;
  1246. /* Map buffer to SMMU and get IOVA */
  1247. attachment = dma_buf_attach(dma_buf, dev);
  1248. if (IS_ERR(attachment)) {
  1249. ret = PTR_ERR(attachment);
  1250. ERR("dma_buf_attach() failed: %d", ret);
  1251. goto err;
  1252. }
  1253. dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
  1254. dma_set_seg_boundary(dev, (unsigned long)DMA_BIT_MASK(64));
  1255. sgt = dma_buf_map_attachment(attachment, DMA_BIDIRECTIONAL);
  1256. if (IS_ERR_OR_NULL(sgt)) {
  1257. ret = PTR_ERR(sgt);
  1258. ERR("dma_buf_map_attachment() failed: %d", ret);
  1259. goto err_detach;
  1260. }
  1261. if (sgt->nents != 1) {
  1262. ERR("nents = %d", sgt->nents);
  1263. goto err_unmap;
  1264. }
  1265. *iova = sg_dma_address(sgt->sgl);
  1266. buf->attachment = attachment;
  1267. buf->sgt = sgt;
  1268. return ret;
  1269. err_unmap:
  1270. dma_buf_unmap_attachment(attachment, sgt, DMA_BIDIRECTIONAL);
  1271. err_detach:
  1272. dma_buf_detach(dma_buf, attachment);
  1273. err:
  1274. if (!ret)
  1275. ret = -1;
  1276. return ret;
  1277. }
  1278. static void reset_buf_attrs(struct ubwcp_buf *buf)
  1279. {
  1280. struct ubwcp_hw_meta_metadata *mmdata;
  1281. struct ubwcp_driver *ubwcp;
  1282. ubwcp = buf->ubwcp;
  1283. mmdata = &buf->mmdata;
  1284. ubwcp_dma_unmap(buf);
  1285. /* reset ula params */
  1286. if (buf->ula_size) {
  1287. ubwcp_ula_free(ubwcp, buf->ula_pa, buf->ula_size);
  1288. buf->ula_size = 0;
  1289. buf->ula_pa = 0;
  1290. }
  1291. /* reset ubwcp params */
  1292. memset(mmdata, 0, sizeof(*mmdata));
  1293. buf->buf_attr_set = false;
  1294. buf->buf_attr.image_format = UBWCP_LINEAR;
  1295. }
  1296. static void print_mmdata_desc(struct ubwcp_hw_meta_metadata *mmdata)
  1297. {
  1298. DBG_BUF_ATTR("");
  1299. DBG_BUF_ATTR("--------MM_DATA DESC ---------");
  1300. DBG_BUF_ATTR("uv_start_addr : 0x%08llx (cache addr) (actual: 0x%llx)",
  1301. mmdata->uv_start_addr, mmdata->uv_start_addr << 6);
  1302. DBG_BUF_ATTR("format : 0x%08x", mmdata->format);
  1303. DBG_BUF_ATTR("stride : 0x%08x (cache addr) (actual: 0x%x)",
  1304. mmdata->stride, mmdata->stride << 6);
  1305. DBG_BUF_ATTR("stride_ubwcp : 0x%08x (cache addr) (actual: 0x%zx)",
  1306. mmdata->stride_ubwcp, mmdata->stride_ubwcp << 6);
  1307. DBG_BUF_ATTR("metadata_base_y : 0x%08x (page addr) (actual: 0x%llx)",
  1308. mmdata->metadata_base_y, mmdata->metadata_base_y << 12);
  1309. DBG_BUF_ATTR("metadata_base_uv: 0x%08x (page addr) (actual: 0x%zx)",
  1310. mmdata->metadata_base_uv, mmdata->metadata_base_uv << 12);
  1311. DBG_BUF_ATTR("buffer_y_offset : 0x%08x (page addr) (actual: 0x%zx)",
  1312. mmdata->buffer_y_offset, mmdata->buffer_y_offset << 12);
  1313. DBG_BUF_ATTR("buffer_uv_offset: 0x%08x (page addr) (actual: 0x%zx)",
  1314. mmdata->buffer_uv_offset, mmdata->buffer_uv_offset << 12);
  1315. DBG_BUF_ATTR("width_height : 0x%08x (width: 0x%x height: 0x%x)",
  1316. mmdata->width_height, mmdata->width_height >> 16, mmdata->width_height & 0xFFFF);
  1317. DBG_BUF_ATTR("");
  1318. }
  1319. /* set buffer attributes:
  1320. * Failure:
  1321. * This call may fail for multiple reasons and it will leave the buffer in an undefined state.
  1322. * In some situations it may leave the buffer in linear mapped state, and in other situations it
  1323. * may leave the buffer in previously set attributes state.
  1324. */
  1325. int ubwcp_set_buf_attrs(struct dma_buf *dmabuf, struct ubwcp_buffer_attrs *attr)
  1326. {
  1327. int ret = 0;
  1328. size_t ula_size = 0;
  1329. size_t uv_start_offset = 0;
  1330. size_t ula_y_plane_size = 0;
  1331. phys_addr_t ula_pa = 0x0;
  1332. struct ubwcp_buf *buf;
  1333. struct ubwcp_driver *ubwcp;
  1334. size_t metadata_p0;
  1335. size_t pixeldata_p0;
  1336. size_t metadata_p1;
  1337. size_t pixeldata_p1;
  1338. size_t iova_min_size;
  1339. size_t stride_tp10_b;
  1340. dma_addr_t iova_base;
  1341. struct ubwcp_hw_meta_metadata *mmdata;
  1342. u64 uv_start;
  1343. u32 stride_b;
  1344. u32 width_b;
  1345. u32 height_b;
  1346. enum ubwcp_std_image_format std_image_format;
  1347. bool is_non_lin_buf;
  1348. u16 hw_img_format;
  1349. trace_ubwcp_set_buf_attrs_start(dmabuf);
  1350. if (!dmabuf) {
  1351. ERR("NULL dmabuf input ptr");
  1352. ret = -EINVAL;
  1353. goto err_validation;
  1354. }
  1355. if (!attr) {
  1356. ERR("NULL attr ptr");
  1357. ret = -EINVAL;
  1358. goto err_validation;
  1359. }
  1360. buf = dma_buf_to_ubwcp_buf(dmabuf);
  1361. if (!buf) {
  1362. ERR("No corresponding ubwcp_buf for the passed in dma_buf");
  1363. ret = -EINVAL;
  1364. goto err_validation;
  1365. }
  1366. ubwcp = buf->ubwcp;
  1367. if (ubwcp->state != UBWCP_STATE_READY) {
  1368. ret = EPERM;
  1369. goto err_validation;
  1370. }
  1371. if (!ubwcp_buf_attrs_valid(ubwcp, attr)) {
  1372. ERR("Invalid buf attrs");
  1373. ret = -EINVAL;
  1374. goto err_validation;
  1375. }
  1376. mutex_lock(&buf->lock);
  1377. if (buf->lock_count) {
  1378. ERR("Cannot set attr when buffer is locked");
  1379. ret = -EBUSY;
  1380. goto unlock;
  1381. }
  1382. mmdata = &buf->mmdata;
  1383. is_non_lin_buf = (buf->buf_attr.image_format != UBWCP_LINEAR);
  1384. /* note: this also checks if buf is mmap'ed */
  1385. ret = ubwcp->mmap_config_fptr(buf->dma_buf, true, 0, 0);
  1386. if (ret) {
  1387. ERR("dma_buf_mmap_config(0,0) failed: %d", ret);
  1388. goto unlock;
  1389. }
  1390. if (attr->image_format == UBWCP_LINEAR) {
  1391. DBG_BUF_ATTR("Linear format requested");
  1392. if (buf->buf_attr_set)
  1393. reset_buf_attrs(buf);
  1394. if (is_non_lin_buf) {
  1395. /*
  1396. * Changing buffer from ubwc to linear so decrement
  1397. * number of ubwc buffers
  1398. */
  1399. ret = dec_num_non_lin_buffers(ubwcp);
  1400. }
  1401. mutex_unlock(&buf->lock);
  1402. trace_ubwcp_set_buf_attrs_end(dmabuf);
  1403. return ret;
  1404. }
  1405. if (to_std_format(attr->image_format, &std_image_format)) {
  1406. ERR("Unable to map ioctl image format to std image format");
  1407. goto unlock;
  1408. }
  1409. if (std_to_hw_img_fmt(std_image_format, &hw_img_format)) {
  1410. ERR("Unable to map std image format to hw image format");
  1411. goto unlock;
  1412. }
  1413. /* Calculate uncompressed-buffer size. */
  1414. ret = ubwcp_calc_ula_params(ubwcp, attr, &ula_size, &ula_y_plane_size, &uv_start_offset);
  1415. if (ret) {
  1416. ERR("ubwcp_calc_ula_params() failed: %d", ret);
  1417. goto unlock;
  1418. }
  1419. ret = ubwcp_validate_uv_align(ubwcp, attr, ula_y_plane_size, uv_start_offset);
  1420. if (ret) {
  1421. ERR("ubwcp_validate_uv_align() failed: %d", ret);
  1422. goto unlock;
  1423. }
  1424. ret = ubwcp_calc_ubwcp_buf_params(ubwcp, attr, &metadata_p0, &pixeldata_p0, &metadata_p1,
  1425. &pixeldata_p1, &stride_tp10_b);
  1426. if (ret) {
  1427. ERR("ubwcp_calc_buf_params() failed: %d", ret);
  1428. goto unlock;
  1429. }
  1430. iova_min_size = metadata_p0 + pixeldata_p0 + metadata_p1 + pixeldata_p1;
  1431. DBG_BUF_ATTR("");
  1432. DBG_BUF_ATTR("");
  1433. DBG_BUF_ATTR("------Summary ULA Calculated Params ------");
  1434. DBG_BUF_ATTR("ULA Size : %8zu (0x%8zx)", ula_size, ula_size);
  1435. DBG_BUF_ATTR("UV Start Offset : %8zu (0x%8zx)", uv_start_offset, uv_start_offset);
  1436. DBG_BUF_ATTR("------Summary UBCP Calculated Params ------");
  1437. DBG_BUF_ATTR("metadata_p0 : %8d (0x%8zx)", metadata_p0, metadata_p0);
  1438. DBG_BUF_ATTR("pixeldata_p0 : %8d (0x%8zx)", pixeldata_p0, pixeldata_p0);
  1439. DBG_BUF_ATTR("metadata_p1 : %8d (0x%8zx)", metadata_p1, metadata_p1);
  1440. DBG_BUF_ATTR("pixeldata_p1 : %8d (0x%8zx)", pixeldata_p1, pixeldata_p1);
  1441. DBG_BUF_ATTR("stride_tp10 : %8d (0x%8zx)", stride_tp10_b, stride_tp10_b);
  1442. DBG_BUF_ATTR("iova_min_size : %8d (0x%8zx)", iova_min_size, iova_min_size);
  1443. DBG_BUF_ATTR("");
  1444. /* assign ULA PA with uncompressed-size range */
  1445. ula_pa = ubwcp_ula_realloc(ubwcp, buf->ula_pa, buf->ula_size, ula_size);
  1446. if (!ula_pa) {
  1447. ERR("ubwcp_ula_alloc/realloc() failed. running out of ULA PA space?");
  1448. goto err;
  1449. }
  1450. buf->ula_size = ula_size;
  1451. buf->ula_pa = ula_pa;
  1452. DBG_BUF_ATTR("Allocated ULA_PA: 0x%p of size: 0x%zx", ula_pa, ula_size);
  1453. DBG_BUF_ATTR("");
  1454. /* dma map only the first time attribute is set */
  1455. if (!buf->buf_attr_set) {
  1456. /* linear -> ubwcp. map ubwcp buffer */
  1457. ret = ubwcp_dma_map(buf, ubwcp->dev_buf_cb, &iova_base);
  1458. if (ret) {
  1459. ERR("ubwcp_dma_map() failed: %d", ret);
  1460. goto err;
  1461. }
  1462. DBG_BUF_ATTR("dma_buf IOVA range: 0x%llx + min_size (0x%zx): 0x%llx",
  1463. iova_base, iova_min_size, iova_base + iova_min_size);
  1464. }
  1465. if(!verify_dma_buf_size(buf, iova_min_size))
  1466. goto err;
  1467. uv_start = ula_pa + uv_start_offset;
  1468. if (!IS_ALIGNED(uv_start, 64)) {
  1469. ERR("ERROR: uv_start is NOT aligned to cache line");
  1470. goto err;
  1471. }
  1472. /* Convert height and width to bytes for writing to mmdata */
  1473. if (std_image_format != TP10) {
  1474. ubwcp_pixel_to_bytes(ubwcp, std_image_format, attr->width,
  1475. attr->height, &width_b, &height_b);
  1476. } else {
  1477. /* for tp10 image compression, we need to program p010 width/height */
  1478. ubwcp_pixel_to_bytes(ubwcp, P010, attr->width,
  1479. attr->height, &width_b, &height_b);
  1480. }
  1481. stride_b = attr->stride;
  1482. /* create the mmdata descriptor */
  1483. memset(mmdata, 0, sizeof(*mmdata));
  1484. mmdata->uv_start_addr = CACHE_ADDR(uv_start);
  1485. mmdata->format = hw_img_format;
  1486. if (std_image_format != TP10) {
  1487. mmdata->stride = CACHE_ADDR(stride_b); /* uncompressed stride */
  1488. } else {
  1489. mmdata->stride = CACHE_ADDR(stride_tp10_b); /* compressed stride */
  1490. mmdata->stride_ubwcp = CACHE_ADDR(stride_b); /* uncompressed stride */
  1491. }
  1492. mmdata->metadata_base_y = PAGE_ADDR(iova_base);
  1493. mmdata->metadata_base_uv = PAGE_ADDR(iova_base + metadata_p0 + pixeldata_p0);
  1494. mmdata->buffer_y_offset = PAGE_ADDR(metadata_p0);
  1495. mmdata->buffer_uv_offset = PAGE_ADDR(metadata_p1);
  1496. /* NOTE: For version 1.1, both width & height needs to be in bytes.
  1497. * For other versions, width in bytes & height in pixels.
  1498. */
  1499. if ((ubwcp->hw_ver_major == 1) && (ubwcp->hw_ver_minor == 1))
  1500. mmdata->width_height = width_b << 16 | height_b;
  1501. else
  1502. mmdata->width_height = width_b << 16 | attr->height;
  1503. print_mmdata_desc(mmdata);
  1504. if (!is_non_lin_buf) {
  1505. /*
  1506. * Changing buffer from linear to ubwc so increment
  1507. * number of ubwc buffers
  1508. */
  1509. ret = inc_num_non_lin_buffers(ubwcp);
  1510. }
  1511. if (ret) {
  1512. ERR("inc_num_non_lin_buffers failed: %d", ret);
  1513. goto err;
  1514. }
  1515. /* inform ULA-PA to dma-heap */
  1516. DBG_BUF_ATTR("Calling mmap_config(): ULA_PA: 0x%p size: 0x%zx", ula_pa, ula_size);
  1517. ret = ubwcp->mmap_config_fptr(buf->dma_buf, false, buf->ula_pa, buf->ula_size);
  1518. if (ret) {
  1519. ERR("dma_buf_mmap_config() failed: %d", ret);
  1520. if (!is_non_lin_buf)
  1521. dec_num_non_lin_buffers(ubwcp);
  1522. goto err;
  1523. }
  1524. buf->buf_attr = *attr;
  1525. buf->buf_attr_set = true;
  1526. mutex_unlock(&buf->lock);
  1527. trace_ubwcp_set_buf_attrs_end(dmabuf);
  1528. return 0;
  1529. err:
  1530. reset_buf_attrs(buf);
  1531. if (is_non_lin_buf) {
  1532. /*
  1533. * Changing buffer from ubwc to linear so decrement
  1534. * number of ubwc buffers
  1535. */
  1536. dec_num_non_lin_buffers(ubwcp);
  1537. }
  1538. unlock:
  1539. mutex_unlock(&buf->lock);
  1540. err_validation:
  1541. if (!ret)
  1542. ret = -1;
  1543. trace_ubwcp_set_buf_attrs_end(dmabuf);
  1544. return ret;
  1545. }
  1546. EXPORT_SYMBOL(ubwcp_set_buf_attrs);
  1547. /* Free up the buffer descriptor */
  1548. static void ubwcp_buf_desc_free(struct ubwcp_driver *ubwcp, struct ubwcp_desc *desc)
  1549. {
  1550. int idx = desc->idx;
  1551. struct ubwcp_desc *desc_list = ubwcp->desc_list;
  1552. mutex_lock(&ubwcp->desc_lock);
  1553. desc_list[idx].idx = -1;
  1554. desc_list[idx].ptr = NULL;
  1555. DBG("freed descriptor_id: %d", idx);
  1556. mutex_unlock(&ubwcp->desc_lock);
  1557. }
  1558. /* Allocate next available buffer descriptor. */
  1559. static struct ubwcp_desc *ubwcp_buf_desc_allocate(struct ubwcp_driver *ubwcp)
  1560. {
  1561. int idx;
  1562. struct ubwcp_desc *desc_list = ubwcp->desc_list;
  1563. mutex_lock(&ubwcp->desc_lock);
  1564. for (idx = 0; idx < UBWCP_BUFFER_DESC_COUNT; idx++) {
  1565. if (desc_list[idx].idx == -1) {
  1566. desc_list[idx].idx = idx;
  1567. desc_list[idx].ptr = ubwcp->buffer_desc_base +
  1568. idx*UBWCP_BUFFER_DESC_OFFSET;
  1569. DBG("allocated descriptor_id: %d", idx);
  1570. mutex_unlock(&ubwcp->desc_lock);
  1571. return &desc_list[idx];
  1572. }
  1573. }
  1574. mutex_unlock(&ubwcp->desc_lock);
  1575. return NULL;
  1576. }
  1577. static int ubwcp_flush(struct ubwcp_driver *ubwcp)
  1578. {
  1579. int ret = 0;
  1580. mutex_lock(&ubwcp->ubwcp_flush_lock);
  1581. trace_ubwcp_hw_flush_start(0);
  1582. ret = ubwcp_hw_flush(ubwcp->base);
  1583. trace_ubwcp_hw_flush_end(0);
  1584. if (ret)
  1585. ERR("ubwcp_hw_flush() failed, ret = %d", ret);
  1586. mutex_unlock(&ubwcp->ubwcp_flush_lock);
  1587. return ret;
  1588. }
  1589. static int range_check_disable(struct ubwcp_driver *ubwcp, int idx)
  1590. {
  1591. int ret;
  1592. mutex_lock(&ubwcp->ubwcp_flush_lock);
  1593. mutex_lock(&ubwcp->hw_range_ck_lock);
  1594. trace_ubwcp_hw_flush_start(0);
  1595. ret = ubwcp_hw_disable_range_check_with_flush(ubwcp->base, idx);
  1596. trace_ubwcp_hw_flush_end(0);
  1597. if (ret)
  1598. ERR("disable_range_check_with_flush() failed: %d", ret);
  1599. mutex_unlock(&ubwcp->hw_range_ck_lock);
  1600. mutex_unlock(&ubwcp->ubwcp_flush_lock);
  1601. return ret;
  1602. }
  1603. static void range_check_enable(struct ubwcp_driver *ubwcp, int idx)
  1604. {
  1605. mutex_lock(&ubwcp->hw_range_ck_lock);
  1606. ubwcp_hw_enable_range_check(ubwcp->base, idx);
  1607. mutex_unlock(&ubwcp->hw_range_ck_lock);
  1608. }
  1609. /**
  1610. * Lock buffer for CPU access. This prepares ubwcp hw to allow
  1611. * CPU access to the compressed buffer. It will perform
  1612. * necessary address translation configuration and cache maintenance ops
  1613. * so that CPU can safely access ubwcp buffer, if this call is
  1614. * successful.
  1615. * Allocate descriptor if not already,
  1616. * perform CMO and then enable range check
  1617. *
  1618. * @param dmabuf : ptr to the dma buf
  1619. * @param direction : direction of access
  1620. *
  1621. * @return int : 0 on success, otherwise error code
  1622. */
  1623. static int ubwcp_lock(struct dma_buf *dmabuf, enum dma_data_direction dir)
  1624. {
  1625. int ret = 0;
  1626. struct ubwcp_buf *buf;
  1627. struct ubwcp_driver *ubwcp;
  1628. trace_ubwcp_lock_start(dmabuf);
  1629. if (!dmabuf) {
  1630. ERR("NULL dmabuf input ptr");
  1631. trace_ubwcp_lock_end(dmabuf);
  1632. return -EINVAL;
  1633. }
  1634. if (!valid_dma_direction(dir)) {
  1635. ERR("invalid direction: %d", dir);
  1636. trace_ubwcp_lock_end(dmabuf);
  1637. return -EINVAL;
  1638. }
  1639. buf = dma_buf_to_ubwcp_buf(dmabuf);
  1640. if (!buf) {
  1641. ERR("ubwcp_buf ptr not found");
  1642. trace_ubwcp_lock_end(dmabuf);
  1643. return -1;
  1644. }
  1645. ubwcp = buf->ubwcp;
  1646. if (ubwcp->state != UBWCP_STATE_READY) {
  1647. ERR("driver in invalid state: %d", ubwcp->state);
  1648. trace_ubwcp_lock_end(dmabuf);
  1649. return -EPERM;
  1650. }
  1651. mutex_lock(&buf->lock);
  1652. if (!buf->buf_attr_set) {
  1653. ERR("lock() called on buffer, but attr not set");
  1654. goto err;
  1655. }
  1656. if (buf->buf_attr.image_format == UBWCP_LINEAR) {
  1657. ERR("lock() called on linear buffer");
  1658. goto err;
  1659. }
  1660. if (!buf->lock_count) {
  1661. DBG("first lock on buffer");
  1662. /* buf->desc could already be allocated because of perm range xlation */
  1663. if (!buf->desc) {
  1664. /* allocate a buffer descriptor */
  1665. buf->desc = ubwcp_buf_desc_allocate(buf->ubwcp);
  1666. if (!buf->desc) {
  1667. ERR("ubwcp_allocate_buf_desc() failed");
  1668. goto err;
  1669. }
  1670. memcpy(buf->desc->ptr, &buf->mmdata, sizeof(buf->mmdata));
  1671. /* Flushing of updated mmdata:
  1672. * mmdata is iocoherent and ubwcp will get it from CPU cache -
  1673. * *as long as* it has not cached that itself during previous
  1674. * access to the same descriptor.
  1675. *
  1676. * During unlock of previous use of this descriptor,
  1677. * we do hw flush, which will get rid of this mmdata from
  1678. * ubwcp cache.
  1679. *
  1680. * In addition, we also do a hw flush after enable_range_ck().
  1681. * That will also get rid of any speculative fetch of mmdata
  1682. * by the ubwcp hw. At this time, the assumption is that ubwcp
  1683. * will cache mmdata only for active descriptor. But if ubwcp
  1684. * is speculatively fetching mmdata for all descriptors
  1685. * (irrespetive of enabled or not), the flush during lock
  1686. * will be necessary to make sure ubwcp sees updated mmdata
  1687. * that we just updated
  1688. */
  1689. /* program ULA range for this buffer */
  1690. DBG("setting range check: descriptor_id: %d, addr: %p, size: %zx",
  1691. buf->desc->idx, buf->ula_pa, buf->ula_size);
  1692. ubwcp_hw_set_range_check(ubwcp->base, buf->desc->idx, buf->ula_pa,
  1693. buf->ula_size);
  1694. }
  1695. /* enable range check */
  1696. DBG("enabling range check, descriptor_id: %d", buf->desc->idx);
  1697. range_check_enable(ubwcp, buf->desc->idx);
  1698. /* Flush/invalidate UBWCP caches */
  1699. /* Why: cpu could have done a speculative fetch before
  1700. * enable_range_ck() and ubwcp in process of returning "default" data
  1701. * we don't want that stashing of default data pending.
  1702. * we force completion of that and then we also cpu invalidate which
  1703. * will get rid of that line.
  1704. */
  1705. ret = ubwcp_flush(ubwcp);
  1706. if (ret) {
  1707. ubwcp->state = UBWCP_STATE_FAULT;
  1708. ERR("state set to fault");
  1709. goto err_flush_failed;
  1710. }
  1711. /* Flush/invalidate ULA PA from CPU caches
  1712. * Always invalidate cache, even when writing.
  1713. * Upgrade direction to force invalidate.
  1714. */
  1715. if (dir == DMA_TO_DEVICE)
  1716. dir = DMA_BIDIRECTIONAL;
  1717. trace_ubwcp_dma_sync_single_for_cpu_start(buf->ula_size, dir);
  1718. dma_sync_single_for_cpu(ubwcp->dev, buf->ula_pa, buf->ula_size, dir);
  1719. trace_ubwcp_dma_sync_single_for_cpu_end(buf->ula_size, dir);
  1720. buf->dma_dir = dir;
  1721. } else {
  1722. DBG("buf already locked");
  1723. /* For write locks, always upgrade direction to bi_directional.
  1724. * A previous read lock will now become write lock.
  1725. * This will ensure a flush when the last unlock comes in.
  1726. */
  1727. if ((dir == DMA_TO_DEVICE) || (dir == DMA_BIDIRECTIONAL))
  1728. buf->dma_dir = DMA_BIDIRECTIONAL;
  1729. }
  1730. buf->lock_count++;
  1731. DBG("new lock_count: %d", buf->lock_count);
  1732. mutex_unlock(&buf->lock);
  1733. trace_ubwcp_lock_end(dmabuf);
  1734. return ret;
  1735. err_flush_failed:
  1736. range_check_disable(ubwcp, buf->desc->idx);
  1737. ubwcp_buf_desc_free(ubwcp, buf->desc);
  1738. buf->desc = NULL;
  1739. err:
  1740. mutex_unlock(&buf->lock);
  1741. if (!ret)
  1742. ret = -1;
  1743. trace_ubwcp_lock_end(dmabuf);
  1744. return ret;
  1745. }
  1746. /* This can be called as a result of external unlock() call or
  1747. * internally if free() is called without unlock().
  1748. */
  1749. static int unlock_internal(struct ubwcp_buf *buf, enum dma_data_direction dir, bool free_buffer)
  1750. {
  1751. int ret = 0;
  1752. struct ubwcp_driver *ubwcp;
  1753. DBG("current lock_count: %d", buf->lock_count);
  1754. if (free_buffer) {
  1755. buf->lock_count = 0;
  1756. DBG("Forced lock_count: %d", buf->lock_count);
  1757. } else {
  1758. /* for write unlocks, remember the direction so we flush on last unlock */
  1759. if ((dir == DMA_TO_DEVICE) || (dir == DMA_BIDIRECTIONAL))
  1760. buf->dma_dir = DMA_BIDIRECTIONAL;
  1761. buf->lock_count--;
  1762. DBG("new lock_count: %d", buf->lock_count);
  1763. if (buf->lock_count) {
  1764. DBG("more than 1 lock on buffer. waiting until last unlock");
  1765. return 0;
  1766. }
  1767. }
  1768. ubwcp = buf->ubwcp;
  1769. /* Only apply CMOs if there were potential CPU writes */
  1770. if (buf->dma_dir == DMA_TO_DEVICE || buf->dma_dir == DMA_BIDIRECTIONAL) {
  1771. /* Flush/invalidate ULA PA from CPU caches */
  1772. trace_ubwcp_dma_sync_single_for_device_start(buf->ula_size, buf->dma_dir);
  1773. dma_sync_single_for_device(ubwcp->dev, buf->ula_pa, buf->ula_size, buf->dma_dir);
  1774. trace_ubwcp_dma_sync_single_for_device_end(buf->ula_size, buf->dma_dir);
  1775. }
  1776. /* disable range check */
  1777. DBG("disabling range check");
  1778. ret = range_check_disable(ubwcp, buf->desc->idx);
  1779. if (ret) {
  1780. ubwcp->state = UBWCP_STATE_FAULT;
  1781. ERR("state set to fault");
  1782. }
  1783. /* release descriptor if perm range xlation is not set */
  1784. if (!buf->perm) {
  1785. ubwcp_buf_desc_free(buf->ubwcp, buf->desc);
  1786. buf->desc = NULL;
  1787. }
  1788. return ret;
  1789. }
  1790. /**
  1791. * Unlock buffer from CPU access. This prepares ubwcp hw to
  1792. * safely allow for device access to the compressed buffer including any
  1793. * necessary cache maintenance ops. It may also free up certain ubwcp
  1794. * resources that could result in error when accessed by CPU in
  1795. * unlocked state.
  1796. *
  1797. * @param dmabuf : ptr to the dma buf
  1798. * @param direction : direction of access
  1799. *
  1800. * @return int : 0 on success, otherwise error code
  1801. */
  1802. static int ubwcp_unlock(struct dma_buf *dmabuf, enum dma_data_direction dir)
  1803. {
  1804. struct ubwcp_buf *buf;
  1805. int ret;
  1806. trace_ubwcp_unlock_start(dmabuf);
  1807. if (!dmabuf) {
  1808. ERR("NULL dmabuf input ptr");
  1809. trace_ubwcp_unlock_end(dmabuf);
  1810. return -EINVAL;
  1811. }
  1812. if (!valid_dma_direction(dir)) {
  1813. ERR("invalid direction: %d", dir);
  1814. trace_ubwcp_unlock_end(dmabuf);
  1815. return -EINVAL;
  1816. }
  1817. buf = dma_buf_to_ubwcp_buf(dmabuf);
  1818. if (!buf) {
  1819. ERR("ubwcp_buf not found");
  1820. trace_ubwcp_unlock_end(dmabuf);
  1821. return -1;
  1822. }
  1823. if (buf->ubwcp->state != UBWCP_STATE_READY) {
  1824. ERR("driver in invalid state: %d", buf->ubwcp->state);
  1825. trace_ubwcp_unlock_end(dmabuf);
  1826. return -EPERM;
  1827. }
  1828. mutex_lock(&buf->lock);
  1829. if (!buf->lock_count) {
  1830. ERR("unlock() called on buffer which not in locked state");
  1831. trace_ubwcp_unlock_end(dmabuf);
  1832. mutex_unlock(&buf->lock);
  1833. return -1;
  1834. }
  1835. ret = unlock_internal(buf, dir, false);
  1836. mutex_unlock(&buf->lock);
  1837. trace_ubwcp_unlock_end(dmabuf);
  1838. return ret;
  1839. }
  1840. /* Return buffer attributes for the given buffer */
  1841. int ubwcp_get_buf_attrs(struct dma_buf *dmabuf, struct ubwcp_buffer_attrs *attr)
  1842. {
  1843. int ret = 0;
  1844. struct ubwcp_buf *buf;
  1845. if (!dmabuf) {
  1846. ERR("NULL dmabuf input ptr");
  1847. return -EINVAL;
  1848. }
  1849. if (!attr) {
  1850. ERR("NULL attr ptr");
  1851. return -EINVAL;
  1852. }
  1853. buf = dma_buf_to_ubwcp_buf(dmabuf);
  1854. if (!buf) {
  1855. ERR("ubwcp_buf ptr not found");
  1856. return -1;
  1857. }
  1858. if (buf->ubwcp->state != UBWCP_STATE_READY) {
  1859. ERR("driver in invalid state: %d", buf->ubwcp->state);
  1860. return -EPERM;
  1861. }
  1862. mutex_lock(&buf->lock);
  1863. if (!buf->buf_attr_set) {
  1864. ERR("buffer attributes not set");
  1865. mutex_unlock(&buf->lock);
  1866. return -1;
  1867. }
  1868. *attr = buf->buf_attr;
  1869. mutex_unlock(&buf->lock);
  1870. return ret;
  1871. }
  1872. EXPORT_SYMBOL(ubwcp_get_buf_attrs);
  1873. /* Set permanent range translation.
  1874. * enable: Descriptor will be reserved for this buffer until disabled,
  1875. * making lock/unlock quicker.
  1876. * disable: Descriptor will not be reserved for this buffer. Instead,
  1877. * descriptor will be allocated and released for each lock/unlock.
  1878. * If currently allocated but not being used, descriptor will be
  1879. * released.
  1880. */
  1881. int ubwcp_set_perm_range_translation(struct dma_buf *dmabuf, bool enable)
  1882. {
  1883. int ret = 0;
  1884. struct ubwcp_buf *buf;
  1885. if (!dmabuf) {
  1886. ERR("NULL dmabuf input ptr");
  1887. return -EINVAL;
  1888. }
  1889. buf = dma_buf_to_ubwcp_buf(dmabuf);
  1890. if (!buf) {
  1891. ERR("ubwcp_buf not found");
  1892. return -1;
  1893. }
  1894. if (buf->ubwcp->state != UBWCP_STATE_READY) {
  1895. ERR("driver in invalid state: %d", buf->ubwcp->state);
  1896. return -EPERM;
  1897. }
  1898. /* not implemented */
  1899. if (1) {
  1900. ERR("API not implemented yet");
  1901. return -1;
  1902. }
  1903. /* TBD: make sure we acquire buf lock while setting this so there is
  1904. * no race condition with attr_set/lock/unlock
  1905. */
  1906. buf->perm = enable;
  1907. /* if "disable" and we have allocated a desc and it is not being
  1908. * used currently, release it
  1909. */
  1910. if (!enable && buf->desc && !buf->lock_count) {
  1911. ubwcp_buf_desc_free(buf->ubwcp, buf->desc);
  1912. buf->desc = NULL;
  1913. /* Flush/invalidate UBWCP caches */
  1914. //TBD: need to do anything?
  1915. }
  1916. return ret;
  1917. }
  1918. EXPORT_SYMBOL(ubwcp_set_perm_range_translation);
  1919. /**
  1920. * Free up ubwcp resources for this buffer.
  1921. *
  1922. * @param dmabuf : ptr to the dma buf
  1923. *
  1924. * @return int : 0 on success, otherwise error code
  1925. */
  1926. static int ubwcp_free_buffer(struct dma_buf *dmabuf)
  1927. {
  1928. int ret = 0;
  1929. struct ubwcp_buf *buf;
  1930. struct ubwcp_driver *ubwcp;
  1931. unsigned long flags;
  1932. bool is_non_lin_buf;
  1933. trace_ubwcp_free_buffer_start(dmabuf);
  1934. if (!dmabuf) {
  1935. ERR("NULL dmabuf input ptr");
  1936. trace_ubwcp_free_buffer_end(dmabuf);
  1937. return -EINVAL;
  1938. }
  1939. buf = dma_buf_to_ubwcp_buf(dmabuf);
  1940. if (!buf) {
  1941. ERR("ubwcp_buf ptr not found");
  1942. trace_ubwcp_free_buffer_end(dmabuf);
  1943. return -1;
  1944. }
  1945. ubwcp = buf->ubwcp;
  1946. if (ubwcp->state != UBWCP_STATE_READY) {
  1947. ERR("driver in invalid state: %d", ubwcp->state);
  1948. trace_ubwcp_free_buffer_end(dmabuf);
  1949. return -EPERM;
  1950. }
  1951. mutex_lock(&buf->lock);
  1952. is_non_lin_buf = (buf->buf_attr.image_format != UBWCP_LINEAR);
  1953. if (buf->lock_count) {
  1954. DBG("free before unlock (lock_count: %d). unlock()'ing first", buf->lock_count);
  1955. ret = unlock_internal(buf, buf->dma_dir, true);
  1956. if (ret)
  1957. ERR("unlock_internal(): failed : %d, but continuing free()", ret);
  1958. }
  1959. /* if we are still holding a desc, release it. this can happen only if perm == true */
  1960. if (buf->desc) {
  1961. if (!buf->perm) {
  1962. ubwcp->state = UBWCP_STATE_FAULT;
  1963. ERR("state set to fault");
  1964. }
  1965. ubwcp_buf_desc_free(buf->ubwcp, buf->desc);
  1966. buf->desc = NULL;
  1967. }
  1968. if (buf->buf_attr_set)
  1969. reset_buf_attrs(buf);
  1970. spin_lock_irqsave(&ubwcp->buf_table_lock, flags);
  1971. hash_del(&buf->hnode);
  1972. spin_unlock_irqrestore(&ubwcp->buf_table_lock, flags);
  1973. mutex_unlock(&buf->lock);
  1974. kfree(buf);
  1975. if (is_non_lin_buf)
  1976. dec_num_non_lin_buffers(ubwcp);
  1977. trace_ubwcp_free_buffer_end(dmabuf);
  1978. return ret;
  1979. }
  1980. /* file open: TBD: increment ref count? */
  1981. static int ubwcp_open(struct inode *i, struct file *f)
  1982. {
  1983. return 0;
  1984. }
  1985. /* file open: TBD: decrement ref count? */
  1986. static int ubwcp_close(struct inode *i, struct file *f)
  1987. {
  1988. return 0;
  1989. }
  1990. static int ioctl_set_buf_attr(struct ubwcp_driver *ubwcp, unsigned long ioctl_param)
  1991. {
  1992. int ret;
  1993. struct dma_buf *dmabuf;
  1994. struct ubwcp_ioctl_buffer_attrs buf_attr_ioctl;
  1995. if (copy_from_user(&buf_attr_ioctl, (const void __user *) ioctl_param,
  1996. sizeof(buf_attr_ioctl))) {
  1997. ERR("copy_from_user() failed");
  1998. return -EFAULT;
  1999. }
  2000. DBG("IOCTL: SET_BUF_ATTR: fd = %d", buf_attr_ioctl.fd);
  2001. dmabuf = dma_buf_get(buf_attr_ioctl.fd);
  2002. if (IS_ERR(dmabuf)) {
  2003. ERR("dmabuf ptr not found for dma_buf_fd = %d", buf_attr_ioctl.fd);
  2004. return PTR_ERR(dmabuf);
  2005. }
  2006. ret = ubwcp_set_buf_attrs(dmabuf, &buf_attr_ioctl.attr);
  2007. dma_buf_put(dmabuf);
  2008. return ret;
  2009. }
  2010. static int ioctl_get_hw_ver(struct ubwcp_driver *ubwcp, unsigned long ioctl_param)
  2011. {
  2012. struct ubwcp_ioctl_hw_version hw_ver;
  2013. DBG("IOCTL: GET_HW_VER");
  2014. if (ubwcp_get_hw_version(&hw_ver))
  2015. return -EINVAL;
  2016. if (copy_to_user((void __user *)ioctl_param, &hw_ver, sizeof(hw_ver))) {
  2017. ERR("copy_to_user() failed");
  2018. return -EFAULT;
  2019. }
  2020. return 0;
  2021. }
  2022. static int ioctl_get_stride_align(struct ubwcp_driver *ubwcp, unsigned long ioctl_param)
  2023. {
  2024. struct ubwcp_ioctl_stride_align stride_align_ioctl;
  2025. enum ubwcp_std_image_format format;
  2026. DBG("IOCTL: GET_STRIDE_ALIGN");
  2027. if (copy_from_user(&stride_align_ioctl, (const void __user *) ioctl_param,
  2028. sizeof(stride_align_ioctl))) {
  2029. ERR("copy_from_user() failed");
  2030. return -EFAULT;
  2031. }
  2032. if (stride_align_ioctl.unused != 0) {
  2033. ERR("unused values must be set to 0");
  2034. return -EINVAL;
  2035. }
  2036. if (!ioctl_format_is_valid(stride_align_ioctl.image_format)) {
  2037. ERR("invalid image format: %d", stride_align_ioctl.image_format);
  2038. return -EINVAL;
  2039. }
  2040. if (stride_align_ioctl.image_format == UBWCP_LINEAR) {
  2041. ERR("not supported for LINEAR format");
  2042. return -EINVAL;
  2043. }
  2044. if (to_std_format(stride_align_ioctl.image_format, &format)) {
  2045. ERR("Unable to map ioctl image format to std image format");
  2046. return -EINVAL;
  2047. }
  2048. if (get_stride_alignment(format, &stride_align_ioctl.stride_align)) {
  2049. ERR("failed for format: %d", format);
  2050. return -EFAULT;
  2051. }
  2052. if (copy_to_user((void __user *)ioctl_param, &stride_align_ioctl,
  2053. sizeof(stride_align_ioctl))) {
  2054. ERR("copy_to_user() failed");
  2055. return -EFAULT;
  2056. }
  2057. return 0;
  2058. }
  2059. static int ioctl_validate_stride(struct ubwcp_driver *ubwcp, unsigned long ioctl_param)
  2060. {
  2061. struct ubwcp_ioctl_validate_stride validate_stride_ioctl;
  2062. enum ubwcp_std_image_format format;
  2063. DBG("IOCTL: VALIDATE_STRIDE");
  2064. if (copy_from_user(&validate_stride_ioctl, (const void __user *) ioctl_param,
  2065. sizeof(validate_stride_ioctl))) {
  2066. ERR("copy_from_user() failed");
  2067. return -EFAULT;
  2068. }
  2069. if (validate_stride_ioctl.unused1 || validate_stride_ioctl.unused2) {
  2070. ERR("unused values must be set to 0");
  2071. return -EINVAL;
  2072. }
  2073. if (!ioctl_format_is_valid(validate_stride_ioctl.image_format)) {
  2074. ERR("not supported for LINEAR format");
  2075. return -EINVAL;
  2076. }
  2077. if (validate_stride_ioctl.image_format == UBWCP_LINEAR) {
  2078. ERR("not supported for LINEAR format");
  2079. return -EINVAL;
  2080. }
  2081. if (to_std_format(validate_stride_ioctl.image_format, &format)) {
  2082. ERR("Unable to map ioctl image format to std image format");
  2083. return -EINVAL;
  2084. }
  2085. validate_stride_ioctl.valid = stride_is_valid(ubwcp, format, validate_stride_ioctl.width,
  2086. validate_stride_ioctl.stride);
  2087. if (copy_to_user((void __user *)ioctl_param, &validate_stride_ioctl,
  2088. sizeof(validate_stride_ioctl))) {
  2089. ERR("copy_to_user() failed");
  2090. return -EFAULT;
  2091. }
  2092. return 0;
  2093. }
  2094. /* handle IOCTLs */
  2095. static long ubwcp_ioctl(struct file *file, unsigned int ioctl_num, unsigned long ioctl_param)
  2096. {
  2097. struct ubwcp_driver *ubwcp;
  2098. ubwcp = ubwcp_get_driver();
  2099. if (!ubwcp)
  2100. return -EINVAL;
  2101. if (ubwcp->state != UBWCP_STATE_READY) {
  2102. ERR("driver in invalid state: %d", ubwcp->state);
  2103. return -EPERM;
  2104. }
  2105. switch (ioctl_num) {
  2106. case UBWCP_IOCTL_SET_BUF_ATTR:
  2107. return ioctl_set_buf_attr(ubwcp, ioctl_param);
  2108. case UBWCP_IOCTL_GET_HW_VER:
  2109. return ioctl_get_hw_ver(ubwcp, ioctl_param);
  2110. case UBWCP_IOCTL_GET_STRIDE_ALIGN:
  2111. return ioctl_get_stride_align(ubwcp, ioctl_param);
  2112. case UBWCP_IOCTL_VALIDATE_STRIDE:
  2113. return ioctl_validate_stride(ubwcp, ioctl_param);
  2114. default:
  2115. ERR("Invalid ioctl_num = %d", ioctl_num);
  2116. return -EINVAL;
  2117. }
  2118. return 0;
  2119. }
  2120. static const struct file_operations ubwcp_fops = {
  2121. .owner = THIS_MODULE,
  2122. .open = ubwcp_open,
  2123. .release = ubwcp_close,
  2124. .unlocked_ioctl = ubwcp_ioctl,
  2125. };
  2126. static int read_err_r_op(void *data, u64 *value)
  2127. {
  2128. struct ubwcp_driver *ubwcp = data;
  2129. *value = ubwcp->read_err_irq_en;
  2130. return 0;
  2131. }
  2132. static int read_err_w_op(void *data, u64 value)
  2133. {
  2134. struct ubwcp_driver *ubwcp = data;
  2135. if (ubwcp->state != UBWCP_STATE_READY)
  2136. return -EPERM;
  2137. if (ubwcp_power(ubwcp, true))
  2138. goto err;
  2139. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_READ_ERROR, value);
  2140. ubwcp->read_err_irq_en = value;
  2141. if (ubwcp_power(ubwcp, false))
  2142. goto err;
  2143. return 0;
  2144. err:
  2145. ubwcp->state = UBWCP_STATE_FAULT;
  2146. ERR("state set to fault");
  2147. return -1;
  2148. }
  2149. static int write_err_r_op(void *data, u64 *value)
  2150. {
  2151. struct ubwcp_driver *ubwcp = data;
  2152. if (ubwcp->state != UBWCP_STATE_READY)
  2153. return -EPERM;
  2154. *value = ubwcp->write_err_irq_en;
  2155. return 0;
  2156. }
  2157. static int write_err_w_op(void *data, u64 value)
  2158. {
  2159. struct ubwcp_driver *ubwcp = data;
  2160. if (ubwcp->state != UBWCP_STATE_READY)
  2161. return -EPERM;
  2162. if (ubwcp_power(ubwcp, true))
  2163. goto err;
  2164. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_WRITE_ERROR, value);
  2165. ubwcp->write_err_irq_en = value;
  2166. if (ubwcp_power(ubwcp, false))
  2167. goto err;
  2168. return 0;
  2169. err:
  2170. ubwcp->state = UBWCP_STATE_FAULT;
  2171. ERR("state set to fault");
  2172. return -1;
  2173. }
  2174. static int decode_err_r_op(void *data, u64 *value)
  2175. {
  2176. struct ubwcp_driver *ubwcp = data;
  2177. if (ubwcp->state != UBWCP_STATE_READY)
  2178. return -EPERM;
  2179. *value = ubwcp->decode_err_irq_en;
  2180. return 0;
  2181. }
  2182. static int decode_err_w_op(void *data, u64 value)
  2183. {
  2184. struct ubwcp_driver *ubwcp = data;
  2185. if (ubwcp->state != UBWCP_STATE_READY)
  2186. return -EPERM;
  2187. if (ubwcp_power(ubwcp, true))
  2188. goto err;
  2189. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_DECODE_ERROR, value);
  2190. ubwcp->decode_err_irq_en = value;
  2191. if (ubwcp_power(ubwcp, false))
  2192. goto err;
  2193. return 0;
  2194. err:
  2195. ubwcp->state = UBWCP_STATE_FAULT;
  2196. ERR("state set to fault");
  2197. return -1;
  2198. }
  2199. static int encode_err_r_op(void *data, u64 *value)
  2200. {
  2201. struct ubwcp_driver *ubwcp = data;
  2202. if (ubwcp->state != UBWCP_STATE_READY)
  2203. return -EPERM;
  2204. *value = ubwcp->encode_err_irq_en;
  2205. return 0;
  2206. }
  2207. static int encode_err_w_op(void *data, u64 value)
  2208. {
  2209. struct ubwcp_driver *ubwcp = data;
  2210. if (ubwcp->state != UBWCP_STATE_READY)
  2211. return -EPERM;
  2212. if (ubwcp_power(ubwcp, true))
  2213. goto err;
  2214. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_ENCODE_ERROR, value);
  2215. ubwcp->encode_err_irq_en = value;
  2216. if (ubwcp_power(ubwcp, false))
  2217. goto err;
  2218. return 0;
  2219. err:
  2220. ubwcp->state = UBWCP_STATE_FAULT;
  2221. ERR("state set to fault");
  2222. return -1;
  2223. }
  2224. static int reg_rw_trace_w_op(void *data, u64 value)
  2225. {
  2226. struct ubwcp_driver *ubwcp = data;
  2227. if (ubwcp->state != UBWCP_STATE_READY)
  2228. return -EPERM;
  2229. ubwcp_hw_trace_set(value);
  2230. return 0;
  2231. }
  2232. static int reg_rw_trace_r_op(void *data, u64 *value)
  2233. {
  2234. struct ubwcp_driver *ubwcp = data;
  2235. bool trace_status;
  2236. if (ubwcp->state != UBWCP_STATE_READY)
  2237. return -EPERM;
  2238. ubwcp_hw_trace_get(&trace_status);
  2239. *value = trace_status;
  2240. return 0;
  2241. }
  2242. DEFINE_DEBUGFS_ATTRIBUTE(read_err_fops, read_err_r_op, read_err_w_op, "%d\n");
  2243. DEFINE_DEBUGFS_ATTRIBUTE(decode_err_fops, decode_err_r_op, decode_err_w_op, "%d\n");
  2244. DEFINE_DEBUGFS_ATTRIBUTE(write_err_fops, write_err_r_op, write_err_w_op, "%d\n");
  2245. DEFINE_DEBUGFS_ATTRIBUTE(encode_err_fops, encode_err_r_op, encode_err_w_op, "%d\n");
  2246. DEFINE_DEBUGFS_ATTRIBUTE(reg_rw_trace_fops, reg_rw_trace_r_op, reg_rw_trace_w_op, "%d\n");
  2247. static void ubwcp_debugfs_init(struct ubwcp_driver *ubwcp)
  2248. {
  2249. struct dentry *debugfs_root;
  2250. struct dentry *dfile;
  2251. debugfs_root = debugfs_create_dir("ubwcp", NULL);
  2252. if (IS_ERR_OR_NULL(debugfs_root)) {
  2253. ERR("Failed to create debugfs for ubwcp\n");
  2254. return;
  2255. }
  2256. debugfs_create_u32("debug_trace_enable", 0644, debugfs_root, &ubwcp_debug_trace_enable);
  2257. dfile = debugfs_create_file("reg_rw_trace_en", 0644, debugfs_root, ubwcp, &reg_rw_trace_fops);
  2258. if (IS_ERR_OR_NULL(dfile)) {
  2259. ERR("failed to create reg_rw_trace_en debugfs file");
  2260. goto err;
  2261. }
  2262. dfile = debugfs_create_file("read_err_irq_en", 0644, debugfs_root, ubwcp, &read_err_fops);
  2263. if (IS_ERR_OR_NULL(dfile)) {
  2264. ERR("failed to create read_err_irq debugfs file");
  2265. goto err;
  2266. }
  2267. dfile = debugfs_create_file("write_err_irq_en", 0644, debugfs_root, ubwcp, &write_err_fops);
  2268. if (IS_ERR_OR_NULL(dfile)) {
  2269. ERR("failed to create write_err_irq debugfs file");
  2270. goto err;
  2271. }
  2272. dfile = debugfs_create_file("decode_err_irq_en", 0644, debugfs_root, ubwcp,
  2273. &decode_err_fops);
  2274. if (IS_ERR_OR_NULL(dfile)) {
  2275. ERR("failed to create decode_err_irq debugfs file");
  2276. goto err;
  2277. }
  2278. dfile = debugfs_create_file("encode_err_irq_en", 0644, debugfs_root, ubwcp,
  2279. &encode_err_fops);
  2280. if (IS_ERR_OR_NULL(dfile)) {
  2281. ERR("failed to create encode_err_irq debugfs file");
  2282. goto err;
  2283. }
  2284. ubwcp->debugfs_root = debugfs_root;
  2285. return;
  2286. err:
  2287. debugfs_remove_recursive(ubwcp->debugfs_root);
  2288. ubwcp->debugfs_root = NULL;
  2289. }
  2290. static void ubwcp_debugfs_deinit(struct ubwcp_driver *ubwcp)
  2291. {
  2292. debugfs_remove_recursive(ubwcp->debugfs_root);
  2293. }
  2294. /* ubwcp char device initialization */
  2295. static int ubwcp_cdev_init(struct ubwcp_driver *ubwcp)
  2296. {
  2297. int ret;
  2298. dev_t devt;
  2299. struct class *dev_class;
  2300. struct device *dev_sys;
  2301. /* allocate major device number (/proc/devices -> major_num ubwcp) */
  2302. ret = alloc_chrdev_region(&devt, 0, UBWCP_NUM_DEVICES, UBWCP_DEVICE_NAME);
  2303. if (ret) {
  2304. ERR("alloc_chrdev_region() failed: %d", ret);
  2305. return ret;
  2306. }
  2307. /* create device class (/sys/class/ubwcp_class) */
  2308. dev_class = class_create(THIS_MODULE, "ubwcp_class");
  2309. if (IS_ERR(dev_class)) {
  2310. ret = PTR_ERR(dev_class);
  2311. ERR("class_create() failed, ret: %d", ret);
  2312. goto err;
  2313. }
  2314. /* Create device and register with sysfs
  2315. * (/sys/class/ubwcp_class/ubwcp/... -> dev/power/subsystem/uevent)
  2316. */
  2317. dev_sys = device_create(dev_class, NULL, devt, NULL,
  2318. UBWCP_DEVICE_NAME);
  2319. if (IS_ERR(dev_sys)) {
  2320. ret = PTR_ERR(dev_sys);
  2321. ERR("device_create() failed, ret: %d", ret);
  2322. goto err_device_create;
  2323. }
  2324. /* register file operations and get cdev */
  2325. cdev_init(&ubwcp->cdev, &ubwcp_fops);
  2326. /* associate cdev and device major/minor with file system
  2327. * can do file ops on /dev/ubwcp after this
  2328. */
  2329. ret = cdev_add(&ubwcp->cdev, devt, 1);
  2330. if (ret) {
  2331. ERR("cdev_add() failed, ret: %d", ret);
  2332. goto err_cdev_add;
  2333. }
  2334. ubwcp->devt = devt;
  2335. ubwcp->dev_class = dev_class;
  2336. ubwcp->dev_sys = dev_sys;
  2337. return 0;
  2338. err_cdev_add:
  2339. device_destroy(dev_class, devt);
  2340. err_device_create:
  2341. class_destroy(dev_class);
  2342. err:
  2343. unregister_chrdev_region(devt, UBWCP_NUM_DEVICES);
  2344. return ret;
  2345. }
  2346. static void ubwcp_cdev_deinit(struct ubwcp_driver *ubwcp)
  2347. {
  2348. device_destroy(ubwcp->dev_class, ubwcp->devt);
  2349. class_destroy(ubwcp->dev_class);
  2350. cdev_del(&ubwcp->cdev);
  2351. unregister_chrdev_region(ubwcp->devt, UBWCP_NUM_DEVICES);
  2352. }
  2353. struct handler_node {
  2354. struct list_head list;
  2355. u32 client_id;
  2356. ubwcp_error_handler_t handler;
  2357. void *data;
  2358. };
  2359. int ubwcp_register_error_handler(u32 client_id, ubwcp_error_handler_t handler,
  2360. void *data)
  2361. {
  2362. struct handler_node *node;
  2363. unsigned long flags;
  2364. struct ubwcp_driver *ubwcp = ubwcp_get_driver();
  2365. if (!ubwcp)
  2366. return -EINVAL;
  2367. if (client_id != -1)
  2368. return -EINVAL;
  2369. if (!handler)
  2370. return -EINVAL;
  2371. if (ubwcp->state != UBWCP_STATE_READY)
  2372. return -EPERM;
  2373. node = kzalloc(sizeof(*node), GFP_KERNEL);
  2374. if (!node)
  2375. return -ENOMEM;
  2376. node->client_id = client_id;
  2377. node->handler = handler;
  2378. node->data = data;
  2379. spin_lock_irqsave(&ubwcp->err_handler_list_lock, flags);
  2380. list_add_tail(&node->list, &ubwcp->err_handler_list);
  2381. spin_unlock_irqrestore(&ubwcp->err_handler_list_lock, flags);
  2382. return 0;
  2383. }
  2384. EXPORT_SYMBOL(ubwcp_register_error_handler);
  2385. static void ubwcp_notify_error_handlers(struct ubwcp_err_info *err)
  2386. {
  2387. struct handler_node *node;
  2388. unsigned long flags;
  2389. struct ubwcp_driver *ubwcp = ubwcp_get_driver();
  2390. if (!ubwcp)
  2391. return;
  2392. spin_lock_irqsave(&ubwcp->err_handler_list_lock, flags);
  2393. list_for_each_entry(node, &ubwcp->err_handler_list, list)
  2394. node->handler(err, node->data);
  2395. spin_unlock_irqrestore(&ubwcp->err_handler_list_lock, flags);
  2396. }
  2397. int ubwcp_unregister_error_handler(u32 client_id)
  2398. {
  2399. int ret = -EINVAL;
  2400. struct handler_node *node;
  2401. unsigned long flags;
  2402. struct ubwcp_driver *ubwcp = ubwcp_get_driver();
  2403. if (!ubwcp)
  2404. return -EINVAL;
  2405. if (ubwcp->state != UBWCP_STATE_INVALID)
  2406. return -EPERM;
  2407. spin_lock_irqsave(&ubwcp->err_handler_list_lock, flags);
  2408. list_for_each_entry(node, &ubwcp->err_handler_list, list)
  2409. if (node->client_id == client_id) {
  2410. list_del(&node->list);
  2411. kfree(node);
  2412. ret = 0;
  2413. break;
  2414. }
  2415. spin_unlock_irqrestore(&ubwcp->err_handler_list_lock, flags);
  2416. return ret;
  2417. }
  2418. EXPORT_SYMBOL(ubwcp_unregister_error_handler);
  2419. /* get ubwcp_buf corresponding to the ULA PA*/
  2420. static struct dma_buf *get_dma_buf_from_ulapa(phys_addr_t addr)
  2421. {
  2422. struct ubwcp_buf *buf = NULL;
  2423. struct dma_buf *ret_buf = NULL;
  2424. struct ubwcp_driver *ubwcp = ubwcp_get_driver();
  2425. unsigned long flags;
  2426. u32 i;
  2427. if (!ubwcp)
  2428. return NULL;
  2429. spin_lock_irqsave(&ubwcp->buf_table_lock, flags);
  2430. hash_for_each(ubwcp->buf_table, i, buf, hnode) {
  2431. if (buf->ula_pa <= addr && addr < buf->ula_pa + buf->ula_size) {
  2432. ret_buf = buf->dma_buf;
  2433. break;
  2434. }
  2435. }
  2436. spin_unlock_irqrestore(&ubwcp->buf_table_lock, flags);
  2437. return ret_buf;
  2438. }
  2439. /* get ubwcp_buf corresponding to the IOVA*/
  2440. static struct dma_buf *get_dma_buf_from_iova(unsigned long addr)
  2441. {
  2442. struct ubwcp_buf *buf = NULL;
  2443. struct dma_buf *ret_buf = NULL;
  2444. struct ubwcp_driver *ubwcp = ubwcp_get_driver();
  2445. unsigned long flags;
  2446. u32 i;
  2447. if (!ubwcp)
  2448. return NULL;
  2449. spin_lock_irqsave(&ubwcp->buf_table_lock, flags);
  2450. hash_for_each(ubwcp->buf_table, i, buf, hnode) {
  2451. unsigned long iova_base;
  2452. unsigned int iova_size;
  2453. if (!buf->sgt)
  2454. continue;
  2455. iova_base = sg_dma_address(buf->sgt->sgl);
  2456. iova_size = sg_dma_len(buf->sgt->sgl);
  2457. if (iova_base <= addr && addr < iova_base + iova_size) {
  2458. ret_buf = buf->dma_buf;
  2459. break;
  2460. }
  2461. }
  2462. spin_unlock_irqrestore(&ubwcp->buf_table_lock, flags);
  2463. return ret_buf;
  2464. }
  2465. int ubwcp_iommu_fault_handler(struct iommu_domain *domain, struct device *dev,
  2466. unsigned long iova, int flags, void *data)
  2467. {
  2468. int ret = 0;
  2469. struct ubwcp_err_info err;
  2470. struct ubwcp_driver *ubwcp = ubwcp_get_driver();
  2471. struct device *cb_dev = (struct device *)data;
  2472. if (!ubwcp) {
  2473. ret = -EINVAL;
  2474. goto err;
  2475. }
  2476. err.err_code = UBWCP_SMMU_FAULT;
  2477. if (cb_dev == ubwcp->dev_desc_cb)
  2478. err.smmu_err.iommu_dev_id = UBWCP_DESC_CB_ID;
  2479. else if (cb_dev == ubwcp->dev_buf_cb)
  2480. err.smmu_err.iommu_dev_id = UBWCP_BUF_CB_ID;
  2481. else
  2482. err.smmu_err.iommu_dev_id = UBWCP_UNKNOWN_CB_ID;
  2483. err.smmu_err.dmabuf = get_dma_buf_from_iova(iova);
  2484. err.smmu_err.iova = iova;
  2485. err.smmu_err.iommu_fault_flags = flags;
  2486. ERR("ubwcp_err: err code: %d (smmu), iommu_dev_id: %d, iova: 0x%llx, flags: 0x%x",
  2487. err.err_code, err.smmu_err.iommu_dev_id, err.smmu_err.iova,
  2488. err.smmu_err.iommu_fault_flags);
  2489. ubwcp_notify_error_handlers(&err);
  2490. err:
  2491. return ret;
  2492. }
  2493. static irqreturn_t ubwcp_irq_handler(int irq, void *ptr)
  2494. {
  2495. struct ubwcp_driver *ubwcp;
  2496. void __iomem *base;
  2497. phys_addr_t addr;
  2498. struct ubwcp_err_info err;
  2499. ubwcp = (struct ubwcp_driver *) ptr;
  2500. base = ubwcp->base;
  2501. if (irq == ubwcp->irq_range_ck_rd) {
  2502. addr = ubwcp_hw_interrupt_src_address(base, 0) << 6;
  2503. err.err_code = UBWCP_RANGE_TRANSLATION_ERROR;
  2504. err.translation_err.dmabuf = get_dma_buf_from_ulapa(addr);
  2505. err.translation_err.ula_pa = addr;
  2506. err.translation_err.read = true;
  2507. ERR("err_code: %d (range read), dmabuf: 0x%llx, read: %d, addr: 0x%llx",
  2508. err.err_code, err.translation_err.dmabuf, err.translation_err.read, addr);
  2509. ubwcp_notify_error_handlers(&err);
  2510. ubwcp_hw_interrupt_clear(ubwcp->base, 0);
  2511. } else if (irq == ubwcp->irq_range_ck_wr) {
  2512. addr = ubwcp_hw_interrupt_src_address(base, 1) << 6;
  2513. err.err_code = UBWCP_RANGE_TRANSLATION_ERROR;
  2514. err.translation_err.dmabuf = get_dma_buf_from_ulapa(addr);
  2515. err.translation_err.ula_pa = addr;
  2516. err.translation_err.read = false;
  2517. ERR("err_code: %d (range write), dmabuf: 0x%llx, read: %d, addr: 0x%llx",
  2518. err.err_code, err.translation_err.dmabuf, err.translation_err.read, addr);
  2519. ubwcp_notify_error_handlers(&err);
  2520. ubwcp_hw_interrupt_clear(ubwcp->base, 1);
  2521. } else if (irq == ubwcp->irq_encode) {
  2522. addr = ubwcp_hw_interrupt_src_address(base, 3) << 6;
  2523. err.err_code = UBWCP_ENCODE_ERROR;
  2524. err.enc_err.dmabuf = get_dma_buf_from_ulapa(addr);
  2525. err.enc_err.ula_pa = addr;
  2526. ERR("err_code: %d (encode), dmabuf: 0x%llx, addr: 0x%llx",
  2527. err.err_code, err.enc_err.dmabuf, addr);
  2528. ubwcp_notify_error_handlers(&err);
  2529. ubwcp_hw_interrupt_clear(ubwcp->base, 3);
  2530. } else if (irq == ubwcp->irq_decode) {
  2531. addr = ubwcp_hw_interrupt_src_address(base, 2) << 6;
  2532. err.err_code = UBWCP_DECODE_ERROR;
  2533. err.dec_err.dmabuf = get_dma_buf_from_ulapa(addr);
  2534. err.dec_err.ula_pa = addr;
  2535. ERR("err_code: %d (decode), dmabuf: 0x%llx, addr: 0x%llx",
  2536. err.err_code, err.enc_err.dmabuf, addr);
  2537. ubwcp_notify_error_handlers(&err);
  2538. ubwcp_hw_interrupt_clear(ubwcp->base, 2);
  2539. } else {
  2540. ERR("unknown irq: %d", irq);
  2541. return IRQ_NONE;
  2542. }
  2543. return IRQ_HANDLED;
  2544. }
  2545. static int ubwcp_interrupt_register(struct platform_device *pdev, struct ubwcp_driver *ubwcp)
  2546. {
  2547. int ret = 0;
  2548. struct device *dev = &pdev->dev;
  2549. ubwcp->irq_range_ck_rd = platform_get_irq(pdev, 0);
  2550. if (ubwcp->irq_range_ck_rd < 0)
  2551. return ubwcp->irq_range_ck_rd;
  2552. ubwcp->irq_range_ck_wr = platform_get_irq(pdev, 1);
  2553. if (ubwcp->irq_range_ck_wr < 0)
  2554. return ubwcp->irq_range_ck_wr;
  2555. ubwcp->irq_encode = platform_get_irq(pdev, 2);
  2556. if (ubwcp->irq_encode < 0)
  2557. return ubwcp->irq_encode;
  2558. ubwcp->irq_decode = platform_get_irq(pdev, 3);
  2559. if (ubwcp->irq_decode < 0)
  2560. return ubwcp->irq_decode;
  2561. DBG("got irqs: %d %d %d %d", ubwcp->irq_range_ck_rd,
  2562. ubwcp->irq_range_ck_wr,
  2563. ubwcp->irq_encode,
  2564. ubwcp->irq_decode);
  2565. ret = devm_request_irq(dev, ubwcp->irq_range_ck_rd, ubwcp_irq_handler, 0, "ubwcp", ubwcp);
  2566. if (ret) {
  2567. ERR("request_irq() failed. irq: %d ret: %d",
  2568. ubwcp->irq_range_ck_rd, ret);
  2569. return ret;
  2570. }
  2571. ret = devm_request_irq(dev, ubwcp->irq_range_ck_wr, ubwcp_irq_handler, 0, "ubwcp", ubwcp);
  2572. if (ret) {
  2573. ERR("request_irq() failed. irq: %d ret: %d",
  2574. ubwcp->irq_range_ck_wr, ret);
  2575. return ret;
  2576. }
  2577. ret = devm_request_irq(dev, ubwcp->irq_encode, ubwcp_irq_handler, 0, "ubwcp", ubwcp);
  2578. if (ret) {
  2579. ERR("request_irq() failed. irq: %d ret: %d",
  2580. ubwcp->irq_encode, ret);
  2581. return ret;
  2582. }
  2583. ret = devm_request_irq(dev, ubwcp->irq_decode, ubwcp_irq_handler, 0, "ubwcp", ubwcp);
  2584. if (ret) {
  2585. ERR("request_irq() failed. irq: %d ret: %d",
  2586. ubwcp->irq_decode, ret);
  2587. return ret;
  2588. }
  2589. return ret;
  2590. }
  2591. /* ubwcp device probe */
  2592. static int qcom_ubwcp_probe(struct platform_device *pdev)
  2593. {
  2594. int ret = 0;
  2595. struct ubwcp_driver *ubwcp;
  2596. struct device *ubwcp_dev = &pdev->dev;
  2597. ubwcp = devm_kzalloc(ubwcp_dev, sizeof(*ubwcp), GFP_KERNEL);
  2598. if (!ubwcp) {
  2599. ERR("devm_kzalloc() failed");
  2600. return -ENOMEM;
  2601. }
  2602. ubwcp->dev = &pdev->dev;
  2603. ret = dma_set_mask_and_coherent(ubwcp->dev, DMA_BIT_MASK(64));
  2604. ubwcp->base = devm_platform_ioremap_resource(pdev, 0);
  2605. if (IS_ERR(ubwcp->base)) {
  2606. ERR("devm ioremap() failed: %d", PTR_ERR(ubwcp->base));
  2607. return PTR_ERR(ubwcp->base);
  2608. }
  2609. DBG("ubwcp->base: %p", ubwcp->base);
  2610. ret = of_property_read_u64_index(ubwcp_dev->of_node, "ula_range", 0, &ubwcp->ula_pool_base);
  2611. if (ret) {
  2612. ERR("failed reading ula_range (base): %d", ret);
  2613. return ret;
  2614. }
  2615. DBG("ubwcp: ula_range: base = 0x%lx", ubwcp->ula_pool_base);
  2616. ret = of_property_read_u64_index(ubwcp_dev->of_node, "ula_range", 1, &ubwcp->ula_pool_size);
  2617. if (ret) {
  2618. ERR("failed reading ula_range (size): %d", ret);
  2619. return ret;
  2620. }
  2621. DBG("ubwcp: ula_range: size = 0x%lx", ubwcp->ula_pool_size);
  2622. INIT_LIST_HEAD(&ubwcp->err_handler_list);
  2623. /* driver initial state */
  2624. ubwcp->state = UBWCP_STATE_INVALID;
  2625. atomic_set(&ubwcp->num_non_lin_buffers, 0);
  2626. ubwcp->mem_online = false;
  2627. mutex_init(&ubwcp->desc_lock);
  2628. spin_lock_init(&ubwcp->buf_table_lock);
  2629. mutex_init(&ubwcp->mem_hotplug_lock);
  2630. mutex_init(&ubwcp->ula_lock);
  2631. mutex_init(&ubwcp->ubwcp_flush_lock);
  2632. mutex_init(&ubwcp->hw_range_ck_lock);
  2633. mutex_init(&ubwcp->power_ctrl_lock);
  2634. spin_lock_init(&ubwcp->err_handler_list_lock);
  2635. /* Regulator */
  2636. ubwcp->vdd = devm_regulator_get(ubwcp_dev, "vdd");
  2637. if (IS_ERR_OR_NULL(ubwcp->vdd)) {
  2638. ret = PTR_ERR(ubwcp->vdd);
  2639. ERR("devm_regulator_get() failed: %d", ret);
  2640. return ret;
  2641. }
  2642. ret = ubwcp_init_clocks(ubwcp, ubwcp_dev);
  2643. if (ret) {
  2644. ERR("failed to initialize ubwcp clocks err: %d", ret);
  2645. return ret;
  2646. }
  2647. if (ubwcp_power(ubwcp, true))
  2648. return -1;
  2649. if (ubwcp_cdev_init(ubwcp))
  2650. return -1;
  2651. /* disable all interrupts (reset value has some interrupts enabled by default) */
  2652. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_READ_ERROR, false);
  2653. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_WRITE_ERROR, false);
  2654. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_ENCODE_ERROR, false);
  2655. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_DECODE_ERROR, false);
  2656. if (ubwcp_interrupt_register(pdev, ubwcp))
  2657. return -1;
  2658. ubwcp_debugfs_init(ubwcp);
  2659. /* create ULA pool */
  2660. ubwcp->ula_pool = gen_pool_create(PAGE_SHIFT, -1);
  2661. if (!ubwcp->ula_pool) {
  2662. ERR("failed gen_pool_create()");
  2663. ret = -1;
  2664. goto err_pool_create;
  2665. }
  2666. ret = gen_pool_add(ubwcp->ula_pool, ubwcp->ula_pool_base, ubwcp->ula_pool_size, -1);
  2667. if (ret) {
  2668. ERR("failed gen_pool_add(): %d", ret);
  2669. ret = -1;
  2670. goto err_pool_add;
  2671. }
  2672. /* register the default config mmap function. */
  2673. ubwcp->mmap_config_fptr = msm_ubwcp_dma_buf_configure_mmap;
  2674. hash_init(ubwcp->buf_table);
  2675. ubwcp_buf_desc_list_init(ubwcp);
  2676. image_format_init(ubwcp);
  2677. /* one time hw init */
  2678. ubwcp_hw_one_time_init(ubwcp->base);
  2679. ubwcp_hw_version(ubwcp->base, &ubwcp->hw_ver_major, &ubwcp->hw_ver_minor);
  2680. pr_err("ubwcp: hw version: major %d, minor %d\n", ubwcp->hw_ver_major, ubwcp->hw_ver_minor);
  2681. if (ubwcp->hw_ver_major == 0) {
  2682. ERR("Failed to read HW version");
  2683. ret = -1;
  2684. goto err_pool_add;
  2685. }
  2686. /* set pdev->dev->driver_data = ubwcp */
  2687. platform_set_drvdata(pdev, ubwcp);
  2688. /* enable interrupts */
  2689. if (ubwcp->read_err_irq_en)
  2690. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_READ_ERROR, true);
  2691. if (ubwcp->write_err_irq_en)
  2692. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_WRITE_ERROR, true);
  2693. if (ubwcp->decode_err_irq_en)
  2694. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_DECODE_ERROR, true);
  2695. if (ubwcp->encode_err_irq_en)
  2696. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_ENCODE_ERROR, true);
  2697. /* Turn OFF until buffers are allocated */
  2698. if (ubwcp_power(ubwcp, false)) {
  2699. ret = -1;
  2700. goto err_power_off;
  2701. }
  2702. ret = msm_ubwcp_set_ops(ubwcp_init_buffer, ubwcp_free_buffer, ubwcp_lock, ubwcp_unlock);
  2703. if (ret) {
  2704. ERR("msm_ubwcp_set_ops() failed: %d", ret);
  2705. goto err_power_off;
  2706. } else {
  2707. DBG("msm_ubwcp_set_ops(): success"); }
  2708. me = ubwcp;
  2709. return ret;
  2710. err_power_off:
  2711. if (!ubwcp_power(ubwcp, true)) {
  2712. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_READ_ERROR, false);
  2713. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_WRITE_ERROR, false);
  2714. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_ENCODE_ERROR, false);
  2715. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_DECODE_ERROR, false);
  2716. ubwcp_power(ubwcp, false);
  2717. }
  2718. err_pool_add:
  2719. gen_pool_destroy(ubwcp->ula_pool);
  2720. err_pool_create:
  2721. ubwcp_debugfs_deinit(ubwcp);
  2722. ubwcp_cdev_deinit(ubwcp);
  2723. return ret;
  2724. }
  2725. /* buffer context bank device probe */
  2726. static int ubwcp_probe_cb_buf(struct platform_device *pdev)
  2727. {
  2728. struct ubwcp_driver *ubwcp;
  2729. struct iommu_domain *domain = NULL;
  2730. ubwcp = dev_get_drvdata(pdev->dev.parent);
  2731. if (!ubwcp) {
  2732. ERR("failed to get ubwcp ptr");
  2733. return -EINVAL;
  2734. }
  2735. ubwcp->dev_buf_cb = &pdev->dev;
  2736. domain = iommu_get_domain_for_dev(ubwcp->dev_buf_cb);
  2737. if (domain)
  2738. iommu_set_fault_handler(domain, ubwcp_iommu_fault_handler, ubwcp->dev_buf_cb);
  2739. if (ubwcp->dev_desc_cb)
  2740. ubwcp->state = UBWCP_STATE_READY;
  2741. return 0;
  2742. }
  2743. /* descriptor context bank device probe */
  2744. static int ubwcp_probe_cb_desc(struct platform_device *pdev)
  2745. {
  2746. int ret = 0;
  2747. struct ubwcp_driver *ubwcp;
  2748. struct iommu_domain *domain = NULL;
  2749. ubwcp = dev_get_drvdata(pdev->dev.parent);
  2750. if (!ubwcp) {
  2751. ERR("failed to get ubwcp ptr");
  2752. return -EINVAL;
  2753. }
  2754. ubwcp->buffer_desc_size = UBWCP_BUFFER_DESC_OFFSET *
  2755. UBWCP_BUFFER_DESC_COUNT;
  2756. ubwcp->dev_desc_cb = &pdev->dev;
  2757. dma_set_max_seg_size(ubwcp->dev_desc_cb, DMA_BIT_MASK(32));
  2758. dma_set_seg_boundary(ubwcp->dev_desc_cb, (unsigned long)DMA_BIT_MASK(64));
  2759. /* Allocate buffer descriptors. UBWCP is iocoherent device.
  2760. * Thus we don't need to flush after updates to buffer descriptors.
  2761. */
  2762. ubwcp->buffer_desc_base = dma_alloc_coherent(ubwcp->dev_desc_cb,
  2763. ubwcp->buffer_desc_size,
  2764. &ubwcp->buffer_desc_dma_handle,
  2765. GFP_KERNEL);
  2766. if (!ubwcp->buffer_desc_base) {
  2767. ERR("failed to allocate desc buffer");
  2768. return -ENOMEM;
  2769. }
  2770. DBG("desc_base = %p size = %zu", ubwcp->buffer_desc_base,
  2771. ubwcp->buffer_desc_size);
  2772. ret = ubwcp_power(ubwcp, true);
  2773. if (ret) {
  2774. ERR("failed to power on");
  2775. goto err;
  2776. }
  2777. ubwcp_hw_set_buf_desc(ubwcp->base, (u64) ubwcp->buffer_desc_dma_handle,
  2778. UBWCP_BUFFER_DESC_OFFSET);
  2779. ret = ubwcp_power(ubwcp, false);
  2780. if (ret) {
  2781. ERR("failed to power off");
  2782. goto err;
  2783. }
  2784. domain = iommu_get_domain_for_dev(ubwcp->dev_desc_cb);
  2785. if (domain)
  2786. iommu_set_fault_handler(domain, ubwcp_iommu_fault_handler, ubwcp->dev_desc_cb);
  2787. if (ubwcp->dev_buf_cb)
  2788. ubwcp->state = UBWCP_STATE_READY;
  2789. return ret;
  2790. err:
  2791. dma_free_coherent(ubwcp->dev_desc_cb,
  2792. ubwcp->buffer_desc_size,
  2793. ubwcp->buffer_desc_base,
  2794. ubwcp->buffer_desc_dma_handle);
  2795. ubwcp->buffer_desc_base = NULL;
  2796. ubwcp->buffer_desc_dma_handle = 0;
  2797. ubwcp->dev_desc_cb = NULL;
  2798. return -1;
  2799. }
  2800. /* buffer context bank device remove */
  2801. static int ubwcp_remove_cb_buf(struct platform_device *pdev)
  2802. {
  2803. struct ubwcp_driver *ubwcp;
  2804. ubwcp = dev_get_drvdata(pdev->dev.parent);
  2805. if (!ubwcp) {
  2806. ERR("failed to get ubwcp ptr");
  2807. return -EINVAL;
  2808. }
  2809. ubwcp->state = UBWCP_STATE_INVALID;
  2810. ubwcp->dev_buf_cb = NULL;
  2811. return 0;
  2812. }
  2813. /* descriptor context bank device remove */
  2814. static int ubwcp_remove_cb_desc(struct platform_device *pdev)
  2815. {
  2816. struct ubwcp_driver *ubwcp;
  2817. ubwcp = dev_get_drvdata(pdev->dev.parent);
  2818. if (!ubwcp) {
  2819. ERR("failed to get ubwcp ptr");
  2820. return -EINVAL;
  2821. }
  2822. if (!ubwcp->dev_desc_cb) {
  2823. ERR("ubwcp->dev_desc_cb == NULL");
  2824. return -1;
  2825. }
  2826. if (!ubwcp_power(ubwcp, true)) {
  2827. ubwcp_hw_set_buf_desc(ubwcp->base, 0x0, 0x0);
  2828. ubwcp_power(ubwcp, false);
  2829. }
  2830. ubwcp->state = UBWCP_STATE_INVALID;
  2831. dma_free_coherent(ubwcp->dev_desc_cb,
  2832. ubwcp->buffer_desc_size,
  2833. ubwcp->buffer_desc_base,
  2834. ubwcp->buffer_desc_dma_handle);
  2835. ubwcp->buffer_desc_base = NULL;
  2836. ubwcp->buffer_desc_dma_handle = 0;
  2837. return 0;
  2838. }
  2839. /* ubwcp device remove */
  2840. static int qcom_ubwcp_remove(struct platform_device *pdev)
  2841. {
  2842. size_t avail;
  2843. size_t psize;
  2844. struct ubwcp_driver *ubwcp;
  2845. /* get pdev->dev->driver_data = ubwcp */
  2846. ubwcp = platform_get_drvdata(pdev);
  2847. if (!ubwcp) {
  2848. ERR("ubwcp == NULL");
  2849. return -1;
  2850. }
  2851. if (!ubwcp_power(ubwcp, true)) {
  2852. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_READ_ERROR, false);
  2853. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_WRITE_ERROR, false);
  2854. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_ENCODE_ERROR, false);
  2855. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_DECODE_ERROR, false);
  2856. ubwcp_power(ubwcp, false);
  2857. }
  2858. ubwcp->state = UBWCP_STATE_INVALID;
  2859. /* before destroying, make sure pool is empty. otherwise pool_destroy() panics. */
  2860. avail = gen_pool_avail(ubwcp->ula_pool);
  2861. psize = gen_pool_size(ubwcp->ula_pool);
  2862. if (psize != avail) {
  2863. ERR("gen_pool is not empty! avail: %zx size: %zx", avail, psize);
  2864. ERR("skipping pool destroy....cause it will PANIC. Fix this!!!!");
  2865. } else {
  2866. gen_pool_destroy(ubwcp->ula_pool);
  2867. }
  2868. ubwcp_debugfs_deinit(ubwcp);
  2869. ubwcp_cdev_deinit(ubwcp);
  2870. return 0;
  2871. }
  2872. /* top level ubwcp device probe function */
  2873. static int ubwcp_probe(struct platform_device *pdev)
  2874. {
  2875. const char *compatible = "";
  2876. trace_ubwcp_probe(pdev);
  2877. if (of_device_is_compatible(pdev->dev.of_node, "qcom,ubwcp"))
  2878. return qcom_ubwcp_probe(pdev);
  2879. else if (of_device_is_compatible(pdev->dev.of_node, "qcom,ubwcp-context-bank-desc"))
  2880. return ubwcp_probe_cb_desc(pdev);
  2881. else if (of_device_is_compatible(pdev->dev.of_node, "qcom,ubwcp-context-bank-buf"))
  2882. return ubwcp_probe_cb_buf(pdev);
  2883. of_property_read_string(pdev->dev.of_node, "compatible", &compatible);
  2884. ERR("unknown device: %s", compatible);
  2885. return -EINVAL;
  2886. }
  2887. /* top level ubwcp device remove function */
  2888. static int ubwcp_remove(struct platform_device *pdev)
  2889. {
  2890. const char *compatible = "";
  2891. trace_ubwcp_remove(pdev);
  2892. /* TBD: what if buffers are still allocated? locked? etc.
  2893. * also should turn off power?
  2894. */
  2895. if (of_device_is_compatible(pdev->dev.of_node, "qcom,ubwcp"))
  2896. return qcom_ubwcp_remove(pdev);
  2897. else if (of_device_is_compatible(pdev->dev.of_node, "qcom,ubwcp-context-bank-desc"))
  2898. return ubwcp_remove_cb_desc(pdev);
  2899. else if (of_device_is_compatible(pdev->dev.of_node, "qcom,ubwcp-context-bank-buf"))
  2900. return ubwcp_remove_cb_buf(pdev);
  2901. of_property_read_string(pdev->dev.of_node, "compatible", &compatible);
  2902. ERR("unknown device: %s", compatible);
  2903. return -EINVAL;
  2904. }
  2905. static const struct of_device_id ubwcp_dt_match[] = {
  2906. {.compatible = "qcom,ubwcp"},
  2907. {.compatible = "qcom,ubwcp-context-bank-desc"},
  2908. {.compatible = "qcom,ubwcp-context-bank-buf"},
  2909. {}
  2910. };
  2911. struct platform_driver ubwcp_platform_driver = {
  2912. .probe = ubwcp_probe,
  2913. .remove = ubwcp_remove,
  2914. .driver = {
  2915. .name = "qcom,ubwcp",
  2916. .of_match_table = ubwcp_dt_match,
  2917. },
  2918. };
  2919. int ubwcp_init(void)
  2920. {
  2921. int ret = 0;
  2922. DBG("+++++++++++");
  2923. ret = platform_driver_register(&ubwcp_platform_driver);
  2924. if (ret)
  2925. ERR("platform_driver_register() failed: %d", ret);
  2926. return ret;
  2927. }
  2928. void ubwcp_exit(void)
  2929. {
  2930. platform_driver_unregister(&ubwcp_platform_driver);
  2931. DBG("-----------");
  2932. }
  2933. module_init(ubwcp_init);
  2934. module_exit(ubwcp_exit);
  2935. MODULE_LICENSE("GPL");