ubwcp_main.c 89 KB

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