ubwcp_main.c 90 KB

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