ubwcp_main.c 82 KB

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