ubwcp_main.c 87 KB

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