ubwcp_main.c 81 KB

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