ubwcp_main.c 86 KB

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