ubwcp_main.c 76 KB

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