ubwcp_main.c 87 KB

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