msm_vidc_driver.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/iommu.h>
  6. #include <linux/workqueue.h>
  7. #include <media/v4l2_vidc_extensions.h>
  8. #include "msm_media_info.h"
  9. #include "msm_vidc_driver.h"
  10. #include "msm_vidc_platform.h"
  11. #include "msm_vidc_internal.h"
  12. #include "msm_vidc_memory.h"
  13. #include "msm_vidc_debug.h"
  14. #include "msm_vidc_power.h"
  15. #include "msm_vidc.h"
  16. #include "msm_vdec.h"
  17. #include "msm_venc.h"
  18. #include "venus_hfi.h"
  19. #include "venus_hfi_response.h"
  20. #include "hfi_packet.h"
  21. #define COUNT_BITS(a, out) { \
  22. while ((a) >= 1) { \
  23. (out) += (a) & (1); \
  24. (a) >>= (1); \
  25. } \
  26. }
  27. struct msm_vidc_buf_details {
  28. enum msm_vidc_buffer_type type;
  29. char *name;
  30. };
  31. void print_vidc_buffer(u32 tag, const char *tag_str, const char *str, struct msm_vidc_inst *inst,
  32. struct msm_vidc_buffer *vbuf)
  33. {
  34. if (!(tag & msm_vidc_debug) || !inst || !vbuf || !tag_str || !str)
  35. return;
  36. if (vbuf->type == MSM_VIDC_BUF_INPUT || vbuf->type == MSM_VIDC_BUF_OUTPUT) {
  37. dprintk_inst(tag, tag_str, inst,
  38. "%s: %s: idx %2d fd %3d off %d daddr %#llx size %d filled %d flags %#x ts %lld attr %#x\n",
  39. str, vbuf->type == MSM_VIDC_BUF_INPUT ? "INPUT" : "OUTPUT",
  40. vbuf->index, vbuf->fd, vbuf->data_offset,
  41. vbuf->device_addr, vbuf->buffer_size, vbuf->data_size,
  42. vbuf->flags, vbuf->timestamp, vbuf->attr);
  43. } else if (vbuf->type == MSM_VIDC_BUF_INPUT_META ||
  44. vbuf->type == MSM_VIDC_BUF_OUTPUT_META) {
  45. dprintk_inst(tag, tag_str, inst,
  46. "%s: %s: idx %2d fd %3d off %d daddr %#llx size %d filled %d flags %#x ts %lld attr %#x\n",
  47. str, vbuf->type == MSM_VIDC_BUF_INPUT_META ? "INPUT_META" : "OUTPUT_META",
  48. vbuf->index, vbuf->fd, vbuf->data_offset,
  49. vbuf->device_addr, vbuf->buffer_size, vbuf->data_size,
  50. vbuf->flags, vbuf->timestamp, vbuf->attr);
  51. }
  52. }
  53. void print_vb2_buffer(const char *str, struct msm_vidc_inst *inst,
  54. struct vb2_buffer *vb2)
  55. {
  56. if (!inst || !vb2)
  57. return;
  58. i_vpr_e(inst,
  59. "%s: %s: idx %2d fd %d off %d size %d filled %d\n",
  60. str, vb2->type == INPUT_MPLANE ? "INPUT" : "OUTPUT",
  61. vb2->index, vb2->planes[0].m.fd,
  62. vb2->planes[0].data_offset, vb2->planes[0].length,
  63. vb2->planes[0].bytesused);
  64. }
  65. enum msm_vidc_buffer_type v4l2_type_to_driver(u32 type, const char *func)
  66. {
  67. enum msm_vidc_buffer_type buffer_type = 0;
  68. switch (type) {
  69. case INPUT_MPLANE:
  70. buffer_type = MSM_VIDC_BUF_INPUT;
  71. break;
  72. case OUTPUT_MPLANE:
  73. buffer_type = MSM_VIDC_BUF_OUTPUT;
  74. break;
  75. case INPUT_META_PLANE:
  76. buffer_type = MSM_VIDC_BUF_INPUT_META;
  77. break;
  78. case OUTPUT_META_PLANE:
  79. buffer_type = MSM_VIDC_BUF_OUTPUT_META;
  80. break;
  81. default:
  82. d_vpr_e("%s: invalid v4l2 buffer type %#x\n", func, type);
  83. break;
  84. }
  85. return buffer_type;
  86. }
  87. u32 v4l2_type_from_driver(enum msm_vidc_buffer_type buffer_type,
  88. const char *func)
  89. {
  90. u32 type = 0;
  91. switch (buffer_type) {
  92. case MSM_VIDC_BUF_INPUT:
  93. type = INPUT_MPLANE;
  94. break;
  95. case MSM_VIDC_BUF_OUTPUT:
  96. type = OUTPUT_MPLANE;
  97. break;
  98. case MSM_VIDC_BUF_INPUT_META:
  99. type = INPUT_META_PLANE;
  100. break;
  101. case MSM_VIDC_BUF_OUTPUT_META:
  102. type = OUTPUT_META_PLANE;
  103. break;
  104. default:
  105. d_vpr_e("%s: invalid driver buffer type %d\n",
  106. func, buffer_type);
  107. break;
  108. }
  109. return type;
  110. }
  111. enum msm_vidc_codec_type v4l2_codec_to_driver(u32 v4l2_codec, const char *func)
  112. {
  113. enum msm_vidc_codec_type codec = 0;
  114. switch (v4l2_codec) {
  115. case V4L2_PIX_FMT_H264:
  116. codec = MSM_VIDC_H264;
  117. break;
  118. case V4L2_PIX_FMT_HEVC:
  119. codec = MSM_VIDC_HEVC;
  120. break;
  121. case V4L2_PIX_FMT_VP9:
  122. codec = MSM_VIDC_VP9;
  123. break;
  124. default:
  125. d_vpr_e("%s: invalid v4l2 codec %#x\n", func, v4l2_codec);
  126. break;
  127. }
  128. return codec;
  129. }
  130. u32 v4l2_codec_from_driver(enum msm_vidc_codec_type codec, const char *func)
  131. {
  132. u32 v4l2_codec = 0;
  133. switch (codec) {
  134. case MSM_VIDC_H264:
  135. v4l2_codec = V4L2_PIX_FMT_H264;
  136. break;
  137. case MSM_VIDC_HEVC:
  138. v4l2_codec = V4L2_PIX_FMT_HEVC;
  139. break;
  140. case MSM_VIDC_VP9:
  141. v4l2_codec = V4L2_PIX_FMT_VP9;
  142. break;
  143. default:
  144. d_vpr_e("%s: invalid driver codec %#x\n", func, codec);
  145. break;
  146. }
  147. return v4l2_codec;
  148. }
  149. enum msm_vidc_colorformat_type v4l2_colorformat_to_driver(u32 v4l2_colorformat,
  150. const char *func)
  151. {
  152. enum msm_vidc_colorformat_type colorformat = 0;
  153. switch (v4l2_colorformat) {
  154. case V4L2_PIX_FMT_NV12:
  155. colorformat = MSM_VIDC_FMT_NV12;
  156. break;
  157. case V4L2_PIX_FMT_NV21:
  158. colorformat = MSM_VIDC_FMT_NV21;
  159. break;
  160. case V4L2_PIX_FMT_VIDC_NV12C:
  161. colorformat = MSM_VIDC_FMT_NV12C;
  162. break;
  163. case V4L2_PIX_FMT_VIDC_TP10C:
  164. colorformat = MSM_VIDC_FMT_TP10C;
  165. break;
  166. case V4L2_PIX_FMT_VIDC_ARGB32C:
  167. colorformat = MSM_VIDC_FMT_RGBA8888C;
  168. break;
  169. case V4L2_PIX_FMT_VIDC_P010:
  170. colorformat = MSM_VIDC_FMT_P010;
  171. break;
  172. default:
  173. d_vpr_e("%s: invalid v4l2 color format %#x\n",
  174. func, v4l2_colorformat);
  175. break;
  176. }
  177. return colorformat;
  178. }
  179. u32 v4l2_colorformat_from_driver(enum msm_vidc_colorformat_type colorformat,
  180. const char *func)
  181. {
  182. u32 v4l2_colorformat = 0;
  183. switch (colorformat) {
  184. case MSM_VIDC_FMT_NV12:
  185. v4l2_colorformat = V4L2_PIX_FMT_NV12;
  186. break;
  187. case MSM_VIDC_FMT_NV21:
  188. v4l2_colorformat = V4L2_PIX_FMT_NV21;
  189. break;
  190. case MSM_VIDC_FMT_NV12C:
  191. v4l2_colorformat = V4L2_PIX_FMT_VIDC_NV12C;
  192. break;
  193. case MSM_VIDC_FMT_TP10C:
  194. v4l2_colorformat = V4L2_PIX_FMT_VIDC_TP10C;
  195. break;
  196. case MSM_VIDC_FMT_RGBA8888C:
  197. v4l2_colorformat = V4L2_PIX_FMT_VIDC_ARGB32C;
  198. break;
  199. case MSM_VIDC_FMT_P010:
  200. v4l2_colorformat = V4L2_PIX_FMT_VIDC_P010;
  201. break;
  202. default:
  203. d_vpr_e("%s: invalid driver color format %#x\n",
  204. func, colorformat);
  205. break;
  206. }
  207. return v4l2_colorformat;
  208. }
  209. u32 v4l2_color_primaries_to_driver(struct msm_vidc_inst *inst,
  210. u32 v4l2_primaries)
  211. {
  212. u32 vidc_color_primaries = MSM_VIDC_PRIMARIES_RESERVED;
  213. switch(v4l2_primaries) {
  214. case V4L2_COLORSPACE_DEFAULT:
  215. vidc_color_primaries = MSM_VIDC_PRIMARIES_RESERVED;
  216. break;
  217. case V4L2_COLORSPACE_REC709:
  218. vidc_color_primaries = MSM_VIDC_PRIMARIES_BT709;
  219. break;
  220. case V4L2_COLORSPACE_470_SYSTEM_M:
  221. vidc_color_primaries = MSM_VIDC_PRIMARIES_BT470_SYSTEM_M;
  222. break;
  223. case V4L2_COLORSPACE_470_SYSTEM_BG:
  224. vidc_color_primaries = MSM_VIDC_PRIMARIES_BT470_SYSTEM_BG;
  225. break;
  226. case V4L2_COLORSPACE_SMPTE170M:
  227. vidc_color_primaries = MSM_VIDC_PRIMARIES_BT601_525;
  228. break;
  229. case V4L2_COLORSPACE_SMPTE240M:
  230. vidc_color_primaries = MSM_VIDC_PRIMARIES_SMPTE_ST240M;
  231. break;
  232. case V4L2_COLORSPACE_VIDC_GENERIC_FILM:
  233. vidc_color_primaries = MSM_VIDC_PRIMARIES_GENERIC_FILM;
  234. break;
  235. case V4L2_COLORSPACE_BT2020:
  236. vidc_color_primaries = MSM_VIDC_PRIMARIES_BT2020;
  237. break;
  238. case V4L2_COLORSPACE_DCI_P3:
  239. vidc_color_primaries = MSM_VIDC_PRIMARIES_SMPTE_RP431_2;
  240. break;
  241. case V4L2_COLORSPACE_VIDC_EG431:
  242. vidc_color_primaries = MSM_VIDC_PRIMARIES_SMPTE_EG431_1;
  243. break;
  244. case V4L2_COLORSPACE_VIDC_EBU_TECH:
  245. vidc_color_primaries = MSM_VIDC_PRIMARIES_SMPTE_EBU_TECH;
  246. break;
  247. default:
  248. i_vpr_e(inst, "%s: invalid color primaries %d\n",
  249. __func__, v4l2_primaries);
  250. break;
  251. }
  252. return vidc_color_primaries;
  253. }
  254. u32 v4l2_color_primaries_from_driver(struct msm_vidc_inst *inst,
  255. u32 vidc_color_primaries)
  256. {
  257. u32 v4l2_primaries = V4L2_COLORSPACE_DEFAULT;
  258. switch(vidc_color_primaries) {
  259. case MSM_VIDC_PRIMARIES_UNSPECIFIED:
  260. v4l2_primaries = V4L2_COLORSPACE_DEFAULT;
  261. break;
  262. case MSM_VIDC_PRIMARIES_BT709:
  263. v4l2_primaries = V4L2_COLORSPACE_REC709;
  264. break;
  265. case MSM_VIDC_PRIMARIES_BT470_SYSTEM_M:
  266. v4l2_primaries = V4L2_COLORSPACE_470_SYSTEM_M;
  267. break;
  268. case MSM_VIDC_PRIMARIES_BT470_SYSTEM_BG:
  269. v4l2_primaries = V4L2_COLORSPACE_470_SYSTEM_BG;
  270. break;
  271. case MSM_VIDC_PRIMARIES_BT601_525:
  272. v4l2_primaries = V4L2_COLORSPACE_SMPTE170M;
  273. break;
  274. case MSM_VIDC_PRIMARIES_SMPTE_ST240M:
  275. v4l2_primaries = V4L2_COLORSPACE_SMPTE240M;
  276. break;
  277. case MSM_VIDC_PRIMARIES_GENERIC_FILM:
  278. v4l2_primaries = V4L2_COLORSPACE_VIDC_GENERIC_FILM;
  279. break;
  280. case MSM_VIDC_PRIMARIES_BT2020:
  281. v4l2_primaries = V4L2_COLORSPACE_BT2020;
  282. break;
  283. case MSM_VIDC_PRIMARIES_SMPTE_RP431_2:
  284. v4l2_primaries = V4L2_COLORSPACE_DCI_P3;
  285. break;
  286. case MSM_VIDC_PRIMARIES_SMPTE_EG431_1:
  287. v4l2_primaries = V4L2_COLORSPACE_VIDC_EG431;
  288. break;
  289. case MSM_VIDC_PRIMARIES_SMPTE_EBU_TECH:
  290. v4l2_primaries = V4L2_COLORSPACE_VIDC_EBU_TECH;
  291. break;
  292. default:
  293. i_vpr_e(inst, "%s: invalid hfi color primaries %d\n",
  294. __func__, vidc_color_primaries);
  295. v4l2_primaries = V4L2_COLORSPACE_DEFAULT;
  296. break;
  297. }
  298. return v4l2_primaries;
  299. }
  300. u32 v4l2_transfer_char_to_driver(struct msm_vidc_inst *inst,
  301. u32 v4l2_transfer_char)
  302. {
  303. u32 vidc_transfer_char = MSM_VIDC_TRANSFER_RESERVED;
  304. switch(v4l2_transfer_char) {
  305. case V4L2_XFER_FUNC_DEFAULT:
  306. vidc_transfer_char = MSM_VIDC_TRANSFER_RESERVED;
  307. break;
  308. case V4L2_XFER_FUNC_709:
  309. vidc_transfer_char = MSM_VIDC_TRANSFER_BT709;
  310. break;
  311. case V4L2_XFER_FUNC_VIDC_BT470_SYSTEM_M:
  312. vidc_transfer_char = MSM_VIDC_TRANSFER_BT470_SYSTEM_M;
  313. break;
  314. case V4L2_XFER_FUNC_VIDC_BT470_SYSTEM_BG:
  315. vidc_transfer_char = MSM_VIDC_TRANSFER_BT470_SYSTEM_BG;
  316. break;
  317. case V4L2_XFER_FUNC_VIDC_BT601_525_OR_625:
  318. vidc_transfer_char = MSM_VIDC_TRANSFER_BT601_525_OR_625;
  319. break;
  320. case V4L2_XFER_FUNC_SMPTE240M:
  321. vidc_transfer_char = MSM_VIDC_TRANSFER_SMPTE_ST240M;
  322. break;
  323. case V4L2_XFER_FUNC_VIDC_LINEAR:
  324. vidc_transfer_char = MSM_VIDC_TRANSFER_LINEAR;
  325. break;
  326. case V4L2_XFER_FUNC_VIDC_XVYCC:
  327. vidc_transfer_char = MSM_VIDC_TRANSFER_XVYCC;
  328. break;
  329. case V4L2_XFER_FUNC_VIDC_BT1361:
  330. vidc_transfer_char = MSM_VIDC_TRANSFER_BT1361_0;
  331. break;
  332. case V4L2_XFER_FUNC_SRGB:
  333. vidc_transfer_char = MSM_VIDC_TRANSFER_SRGB_SYCC;
  334. break;
  335. case V4L2_XFER_FUNC_VIDC_BT2020:
  336. vidc_transfer_char = MSM_VIDC_TRANSFER_BT2020_14;
  337. break;
  338. case V4L2_XFER_FUNC_SMPTE2084:
  339. vidc_transfer_char = MSM_VIDC_TRANSFER_SMPTE_ST2084_PQ;
  340. break;
  341. case V4L2_XFER_FUNC_VIDC_ST428:
  342. vidc_transfer_char = MSM_VIDC_TRANSFER_SMPTE_ST428_1;
  343. break;
  344. case V4L2_XFER_FUNC_VIDC_HLG:
  345. vidc_transfer_char = MSM_VIDC_TRANSFER_BT2100_2_HLG;
  346. break;
  347. default:
  348. i_vpr_e(inst, "%s: invalid transfer char %d\n",
  349. __func__, v4l2_transfer_char);
  350. break;
  351. }
  352. return vidc_transfer_char;
  353. }
  354. u32 v4l2_transfer_char_from_driver(struct msm_vidc_inst *inst,
  355. u32 vidc_transfer_char)
  356. {
  357. u32 v4l2_transfer_char = V4L2_XFER_FUNC_DEFAULT;
  358. switch(vidc_transfer_char) {
  359. case MSM_VIDC_TRANSFER_UNSPECIFIED:
  360. v4l2_transfer_char = V4L2_XFER_FUNC_DEFAULT;
  361. break;
  362. case MSM_VIDC_TRANSFER_BT709:
  363. v4l2_transfer_char = V4L2_XFER_FUNC_709;
  364. break;
  365. case MSM_VIDC_TRANSFER_BT470_SYSTEM_M:
  366. v4l2_transfer_char = V4L2_XFER_FUNC_VIDC_BT470_SYSTEM_M;
  367. break;
  368. case MSM_VIDC_TRANSFER_BT470_SYSTEM_BG:
  369. v4l2_transfer_char = V4L2_XFER_FUNC_VIDC_BT470_SYSTEM_BG;
  370. break;
  371. case MSM_VIDC_TRANSFER_BT601_525_OR_625:
  372. v4l2_transfer_char = V4L2_XFER_FUNC_VIDC_BT601_525_OR_625;
  373. break;
  374. case MSM_VIDC_TRANSFER_SMPTE_ST240M:
  375. v4l2_transfer_char = V4L2_XFER_FUNC_SMPTE240M;
  376. break;
  377. case MSM_VIDC_TRANSFER_LINEAR:
  378. v4l2_transfer_char = V4L2_XFER_FUNC_VIDC_LINEAR;
  379. break;
  380. case MSM_VIDC_TRANSFER_XVYCC:
  381. v4l2_transfer_char = V4L2_XFER_FUNC_VIDC_XVYCC;
  382. break;
  383. case MSM_VIDC_TRANSFER_BT1361_0:
  384. v4l2_transfer_char = V4L2_XFER_FUNC_VIDC_BT1361;
  385. break;
  386. case MSM_VIDC_TRANSFER_SRGB_SYCC:
  387. v4l2_transfer_char = V4L2_XFER_FUNC_SRGB;
  388. break;
  389. case MSM_VIDC_TRANSFER_BT2020_14:
  390. case MSM_VIDC_TRANSFER_BT2020_15:
  391. v4l2_transfer_char = V4L2_XFER_FUNC_VIDC_BT2020;
  392. break;
  393. case MSM_VIDC_TRANSFER_SMPTE_ST2084_PQ:
  394. v4l2_transfer_char = V4L2_XFER_FUNC_SMPTE2084;
  395. break;
  396. case MSM_VIDC_TRANSFER_SMPTE_ST428_1:
  397. v4l2_transfer_char = V4L2_XFER_FUNC_VIDC_ST428;
  398. break;
  399. case MSM_VIDC_TRANSFER_BT2100_2_HLG:
  400. v4l2_transfer_char = V4L2_XFER_FUNC_VIDC_HLG;
  401. break;
  402. default:
  403. i_vpr_e(inst, "%s: invalid transfer char %d\n",
  404. __func__, vidc_transfer_char);
  405. break;
  406. }
  407. return v4l2_transfer_char;
  408. }
  409. u32 v4l2_matrix_coeff_to_driver(struct msm_vidc_inst *inst,
  410. u32 v4l2_matrix_coeff)
  411. {
  412. u32 vidc_matrix_coeff = MSM_VIDC_MATRIX_COEFF_RESERVED;
  413. switch(v4l2_matrix_coeff) {
  414. case V4L2_YCBCR_VIDC_SRGB_OR_SMPTE_ST428:
  415. vidc_matrix_coeff = MSM_VIDC_MATRIX_COEFF_SRGB_SMPTE_ST428_1;
  416. break;
  417. case V4L2_YCBCR_ENC_709:
  418. case V4L2_YCBCR_ENC_XV709:
  419. vidc_matrix_coeff = MSM_VIDC_MATRIX_COEFF_BT709;
  420. break;
  421. case V4L2_YCBCR_VIDC_FCC47_73_682:
  422. vidc_matrix_coeff = MSM_VIDC_MATRIX_COEFF_FCC_TITLE_47;
  423. break;
  424. case V4L2_YCBCR_ENC_XV601:
  425. vidc_matrix_coeff = MSM_VIDC_MATRIX_COEFF_BT470_SYS_BG_OR_BT601_625;
  426. break;
  427. case V4L2_YCBCR_ENC_601:
  428. vidc_matrix_coeff = MSM_VIDC_MATRIX_COEFF_BT601_525_BT1358_525_OR_625;
  429. break;
  430. case V4L2_YCBCR_ENC_SMPTE240M:
  431. vidc_matrix_coeff = MSM_VIDC_MATRIX_COEFF_SMPTE_ST240;
  432. break;
  433. case V4L2_YCBCR_ENC_BT2020:
  434. vidc_matrix_coeff = MSM_VIDC_MATRIX_COEFF_BT2020_NON_CONSTANT;
  435. break;
  436. case V4L2_YCBCR_ENC_BT2020_CONST_LUM:
  437. vidc_matrix_coeff = MSM_VIDC_MATRIX_COEFF_BT2020_CONSTANT;
  438. break;
  439. default:
  440. i_vpr_e(inst, "%s: invalid matrix coeff %d\n",
  441. __func__, v4l2_matrix_coeff);
  442. break;
  443. }
  444. return vidc_matrix_coeff;
  445. }
  446. u32 v4l2_matrix_coeff_from_driver(struct msm_vidc_inst *inst,
  447. u32 vidc_matrix_coeff)
  448. {
  449. u32 v4l2_matrix_coeff = V4L2_YCBCR_ENC_DEFAULT;
  450. switch(vidc_matrix_coeff) {
  451. case MSM_VIDC_MATRIX_COEFF_SRGB_SMPTE_ST428_1:
  452. v4l2_matrix_coeff = V4L2_YCBCR_VIDC_SRGB_OR_SMPTE_ST428;
  453. break;
  454. case MSM_VIDC_MATRIX_COEFF_BT709:
  455. v4l2_matrix_coeff = V4L2_YCBCR_ENC_709;
  456. break;
  457. case MSM_VIDC_MATRIX_COEFF_FCC_TITLE_47:
  458. v4l2_matrix_coeff = V4L2_YCBCR_VIDC_FCC47_73_682;
  459. break;
  460. case MSM_VIDC_MATRIX_COEFF_BT470_SYS_BG_OR_BT601_625:
  461. v4l2_matrix_coeff = V4L2_YCBCR_ENC_XV601;
  462. break;
  463. case MSM_VIDC_MATRIX_COEFF_BT601_525_BT1358_525_OR_625:
  464. v4l2_matrix_coeff = V4L2_YCBCR_ENC_601;
  465. break;
  466. case MSM_VIDC_MATRIX_COEFF_SMPTE_ST240:
  467. v4l2_matrix_coeff = V4L2_YCBCR_ENC_SMPTE240M;
  468. break;
  469. case MSM_VIDC_MATRIX_COEFF_BT2020_NON_CONSTANT:
  470. v4l2_matrix_coeff = V4L2_YCBCR_ENC_BT2020;
  471. break;
  472. case MSM_VIDC_MATRIX_COEFF_BT2020_CONSTANT:
  473. v4l2_matrix_coeff = V4L2_YCBCR_ENC_BT2020_CONST_LUM;
  474. break;
  475. default:
  476. i_vpr_e(inst, "%s: invalid matrix coeff %d\n",
  477. __func__, vidc_matrix_coeff);
  478. break;
  479. }
  480. return v4l2_matrix_coeff;
  481. }
  482. int v4l2_type_to_driver_port(struct msm_vidc_inst *inst, u32 type,
  483. const char *func)
  484. {
  485. int port;
  486. if (type == INPUT_MPLANE) {
  487. port = INPUT_PORT;
  488. } else if (type == INPUT_META_PLANE) {
  489. port = INPUT_META_PORT;
  490. } else if (type == OUTPUT_MPLANE) {
  491. port = OUTPUT_PORT;
  492. } else if (type == OUTPUT_META_PLANE) {
  493. port = OUTPUT_META_PORT;
  494. } else {
  495. i_vpr_e(inst, "%s: port not found for v4l2 type %d\n",
  496. func, type);
  497. port = -EINVAL;
  498. }
  499. return port;
  500. }
  501. u32 msm_vidc_get_buffer_region(struct msm_vidc_inst *inst,
  502. enum msm_vidc_buffer_type buffer_type, const char *func)
  503. {
  504. u32 region = MSM_VIDC_NON_SECURE;
  505. if (!is_secure_session(inst) &&
  506. buffer_type != MSM_VIDC_BUF_ARP) {
  507. return region;
  508. }
  509. switch (buffer_type) {
  510. case MSM_VIDC_BUF_INPUT:
  511. if (is_encode_session(inst))
  512. region = MSM_VIDC_SECURE_PIXEL;
  513. else
  514. region = MSM_VIDC_SECURE_BITSTREAM;
  515. break;
  516. case MSM_VIDC_BUF_OUTPUT:
  517. if (is_encode_session(inst))
  518. region = MSM_VIDC_SECURE_BITSTREAM;
  519. else
  520. region = MSM_VIDC_SECURE_PIXEL;
  521. break;
  522. case MSM_VIDC_BUF_INPUT_META:
  523. case MSM_VIDC_BUF_OUTPUT_META:
  524. region = MSM_VIDC_NON_SECURE;
  525. break;
  526. case MSM_VIDC_BUF_BIN:
  527. region = MSM_VIDC_SECURE_BITSTREAM;
  528. break;
  529. case MSM_VIDC_BUF_COMV:
  530. case MSM_VIDC_BUF_NON_COMV:
  531. case MSM_VIDC_BUF_LINE:
  532. region = MSM_VIDC_SECURE_NONPIXEL;
  533. break;
  534. case MSM_VIDC_BUF_DPB:
  535. region = MSM_VIDC_SECURE_PIXEL;
  536. break;
  537. case MSM_VIDC_BUF_PERSIST:
  538. // TODO: Need to revisit for ARP
  539. case MSM_VIDC_BUF_ARP:
  540. region = MSM_VIDC_SECURE_NONPIXEL;
  541. break;
  542. default:
  543. i_vpr_e(inst, "%s: invalid driver buffer type %d\n",
  544. func, buffer_type);
  545. }
  546. return region;
  547. }
  548. struct msm_vidc_buffers *msm_vidc_get_buffers(
  549. struct msm_vidc_inst *inst, enum msm_vidc_buffer_type buffer_type,
  550. const char *func)
  551. {
  552. switch (buffer_type) {
  553. case MSM_VIDC_BUF_INPUT:
  554. return &inst->buffers.input;
  555. case MSM_VIDC_BUF_INPUT_META:
  556. return &inst->buffers.input_meta;
  557. case MSM_VIDC_BUF_OUTPUT:
  558. return &inst->buffers.output;
  559. case MSM_VIDC_BUF_OUTPUT_META:
  560. return &inst->buffers.output_meta;
  561. case MSM_VIDC_BUF_BIN:
  562. return &inst->buffers.bin;
  563. case MSM_VIDC_BUF_ARP:
  564. return &inst->buffers.arp;
  565. case MSM_VIDC_BUF_COMV:
  566. return &inst->buffers.comv;
  567. case MSM_VIDC_BUF_NON_COMV:
  568. return &inst->buffers.non_comv;
  569. case MSM_VIDC_BUF_LINE:
  570. return &inst->buffers.line;
  571. case MSM_VIDC_BUF_DPB:
  572. return &inst->buffers.dpb;
  573. case MSM_VIDC_BUF_PERSIST:
  574. return &inst->buffers.persist;
  575. case MSM_VIDC_BUF_VPSS:
  576. return &inst->buffers.vpss;
  577. default:
  578. i_vpr_e(inst, "%s: invalid driver buffer type %d\n",
  579. func, buffer_type);
  580. return NULL;
  581. }
  582. }
  583. struct msm_vidc_mappings *msm_vidc_get_mappings(
  584. struct msm_vidc_inst *inst, enum msm_vidc_buffer_type buffer_type,
  585. const char *func)
  586. {
  587. switch (buffer_type) {
  588. case MSM_VIDC_BUF_INPUT:
  589. return &inst->mappings.input;
  590. case MSM_VIDC_BUF_INPUT_META:
  591. return &inst->mappings.input_meta;
  592. case MSM_VIDC_BUF_OUTPUT:
  593. return &inst->mappings.output;
  594. case MSM_VIDC_BUF_OUTPUT_META:
  595. return &inst->mappings.output_meta;
  596. case MSM_VIDC_BUF_BIN:
  597. return &inst->mappings.bin;
  598. case MSM_VIDC_BUF_ARP:
  599. return &inst->mappings.arp;
  600. case MSM_VIDC_BUF_COMV:
  601. return &inst->mappings.comv;
  602. case MSM_VIDC_BUF_NON_COMV:
  603. return &inst->mappings.non_comv;
  604. case MSM_VIDC_BUF_LINE:
  605. return &inst->mappings.line;
  606. case MSM_VIDC_BUF_DPB:
  607. return &inst->mappings.dpb;
  608. case MSM_VIDC_BUF_PERSIST:
  609. return &inst->mappings.persist;
  610. case MSM_VIDC_BUF_VPSS:
  611. return &inst->mappings.vpss;
  612. default:
  613. i_vpr_e(inst, "%s: invalid driver buffer type %d\n",
  614. func, buffer_type);
  615. return NULL;
  616. }
  617. }
  618. struct msm_vidc_allocations *msm_vidc_get_allocations(
  619. struct msm_vidc_inst *inst, enum msm_vidc_buffer_type buffer_type,
  620. const char *func)
  621. {
  622. switch (buffer_type) {
  623. case MSM_VIDC_BUF_BIN:
  624. return &inst->allocations.bin;
  625. case MSM_VIDC_BUF_ARP:
  626. return &inst->allocations.arp;
  627. case MSM_VIDC_BUF_COMV:
  628. return &inst->allocations.comv;
  629. case MSM_VIDC_BUF_NON_COMV:
  630. return &inst->allocations.non_comv;
  631. case MSM_VIDC_BUF_LINE:
  632. return &inst->allocations.line;
  633. case MSM_VIDC_BUF_DPB:
  634. return &inst->allocations.dpb;
  635. case MSM_VIDC_BUF_PERSIST:
  636. return &inst->allocations.persist;
  637. case MSM_VIDC_BUF_VPSS:
  638. return &inst->allocations.vpss;
  639. default:
  640. i_vpr_e(inst, "%s: invalid driver buffer type %d\n",
  641. func, buffer_type);
  642. return NULL;
  643. }
  644. }
  645. const char *core_state_name(enum msm_vidc_core_state state)
  646. {
  647. const char* name = "UNKNOWN";
  648. switch (state) {
  649. case MSM_VIDC_CORE_INIT:
  650. name = "CORE_INIT";
  651. break;
  652. case MSM_VIDC_CORE_DEINIT:
  653. name = "CORE_DEINIT";
  654. break;
  655. default:
  656. name = "UNKNOWN";
  657. break;
  658. }
  659. return name;
  660. }
  661. int msm_vidc_change_core_state(struct msm_vidc_core *core,
  662. enum msm_vidc_core_state request_state, const char *func)
  663. {
  664. if (!core) {
  665. d_vpr_e("%s: invalid params\n", __func__);
  666. return -EINVAL;
  667. }
  668. d_vpr_h("%s: core state changed from %s to %s\n",
  669. func, core_state_name(core->state),
  670. core_state_name(request_state));
  671. core->state = request_state;
  672. return 0;
  673. }
  674. const char *state_name(enum msm_vidc_inst_state state)
  675. {
  676. const char *name = "UNKNOWN";
  677. switch (state) {
  678. case MSM_VIDC_OPEN:
  679. name = "OPEN";
  680. break;
  681. case MSM_VIDC_START_INPUT:
  682. name = "START_INPUT";
  683. break;
  684. case MSM_VIDC_START_OUTPUT:
  685. name = "START_OUTPUT";
  686. break;
  687. case MSM_VIDC_START:
  688. name = "START";
  689. break;
  690. case MSM_VIDC_DRC:
  691. name = "DRC";
  692. break;
  693. case MSM_VIDC_DRC_LAST_FLAG:
  694. name = "DRC_LAST_FLAG";
  695. break;
  696. case MSM_VIDC_DRAIN:
  697. name = "DRAIN";
  698. break;
  699. case MSM_VIDC_DRAIN_LAST_FLAG:
  700. name = "DRAIN_LAST_FLAG";
  701. break;
  702. case MSM_VIDC_DRC_DRAIN:
  703. name = "DRC_DRAIN";
  704. break;
  705. case MSM_VIDC_DRC_DRAIN_LAST_FLAG:
  706. name = "DRC_DRAIN_LAST_FLAG";
  707. break;
  708. case MSM_VIDC_DRAIN_START_INPUT:
  709. name = "DRAIN_START_INPUT";
  710. break;
  711. case MSM_VIDC_ERROR:
  712. name = "ERROR";
  713. break;
  714. default:
  715. name = "UNKNOWN";
  716. break;
  717. }
  718. return name;
  719. }
  720. int msm_vidc_change_inst_state(struct msm_vidc_inst *inst,
  721. enum msm_vidc_inst_state request_state, const char *func)
  722. {
  723. if (!inst) {
  724. d_vpr_e("%s: invalid params\n", __func__);
  725. return -EINVAL;
  726. }
  727. if (!request_state) {
  728. i_vpr_e(inst, "%s: invalid request state\n", func);
  729. return -EINVAL;
  730. }
  731. if (inst->state == MSM_VIDC_ERROR) {
  732. i_vpr_h(inst,
  733. "%s: inst is in bad state, can not change state to %s\n",
  734. func, state_name(request_state));
  735. return 0;
  736. }
  737. i_vpr_h(inst, "%s: state changed from %s to %s\n",
  738. func, state_name(inst->state), state_name(request_state));
  739. inst->state = request_state;
  740. return 0;
  741. }
  742. bool msm_vidc_allow_s_fmt(struct msm_vidc_inst *inst, u32 type)
  743. {
  744. bool allow = false;
  745. if (!inst) {
  746. d_vpr_e("%s: invalid params\n", __func__);
  747. return false;
  748. }
  749. if (inst->state == MSM_VIDC_OPEN) {
  750. allow = true;
  751. goto exit;
  752. }
  753. if (inst->state == MSM_VIDC_START_INPUT) {
  754. if (type == OUTPUT_MPLANE || type == OUTPUT_META_PLANE) {
  755. allow = true;
  756. goto exit;
  757. }
  758. }
  759. if (inst->state == MSM_VIDC_START_OUTPUT) {
  760. if (type == INPUT_MPLANE || type == INPUT_META_PLANE) {
  761. allow = true;
  762. goto exit;
  763. }
  764. }
  765. exit:
  766. if (!allow)
  767. i_vpr_e(inst, "%s: type %d not allowed in state %s\n",
  768. __func__, type, state_name(inst->state));
  769. return allow;
  770. }
  771. bool msm_vidc_allow_s_ctrl(struct msm_vidc_inst *inst, u32 id)
  772. {
  773. bool allow = false;
  774. if (!inst) {
  775. d_vpr_e("%s: invalid params\n", __func__);
  776. return false;
  777. }
  778. if (inst->state == MSM_VIDC_OPEN) {
  779. allow = true;
  780. goto exit;
  781. }
  782. if (is_decode_session(inst)) {
  783. if (!inst->vb2q[INPUT_PORT].streaming) {
  784. allow = true;
  785. goto exit;
  786. }
  787. if (inst->vb2q[INPUT_PORT].streaming) {
  788. switch (id) {
  789. case V4L2_CID_MPEG_VIDC_CODEC_CONFIG:
  790. allow = true;
  791. break;
  792. default:
  793. allow = false;
  794. break;
  795. }
  796. }
  797. } else if (is_encode_session(inst)) {
  798. if (inst->state == MSM_VIDC_START || inst->state == MSM_VIDC_START_OUTPUT) {
  799. switch (id) {
  800. case V4L2_CID_MPEG_VIDEO_BITRATE:
  801. case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
  802. case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
  803. case V4L2_CID_HFLIP:
  804. case V4L2_CID_VFLIP:
  805. case V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP:
  806. case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_LAYER:
  807. case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_BR:
  808. case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_BR:
  809. case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_BR:
  810. case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_BR:
  811. case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_BR:
  812. case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_BR:
  813. case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L6_BR:
  814. case V4L2_CID_MPEG_VIDC_BASELAYER_PRIORITY:
  815. case V4L2_CID_MPEG_VIDC_USELTRFRAME:
  816. case V4L2_CID_MPEG_VIDC_MARKLTRFRAME:
  817. case V4L2_CID_MPEG_VIDC_VIDEO_BLUR_TYPES:
  818. case V4L2_CID_MPEG_VIDC_VIDEO_BLUR_RESOLUTION:
  819. case V4L2_CID_MPEG_VIDEO_CONSTANT_QUALITY:
  820. allow = true;
  821. break;
  822. default:
  823. allow = false;
  824. break;
  825. }
  826. }
  827. }
  828. exit:
  829. if (!allow)
  830. i_vpr_e(inst, "%s: id %d not allowed in state %s\n",
  831. __func__, id, state_name(inst->state));
  832. return allow;
  833. }
  834. bool msm_vidc_allow_reqbufs(struct msm_vidc_inst *inst, u32 type)
  835. {
  836. bool allow = false;
  837. if (!inst) {
  838. d_vpr_e("%s: invalid params\n", __func__);
  839. return false;
  840. }
  841. if (inst->state == MSM_VIDC_OPEN) {
  842. allow = true;
  843. goto exit;
  844. }
  845. if (inst->state == MSM_VIDC_START_INPUT) {
  846. if (type == OUTPUT_MPLANE || type == OUTPUT_META_PLANE) {
  847. allow = true;
  848. goto exit;
  849. }
  850. }
  851. if (inst->state == MSM_VIDC_START_OUTPUT) {
  852. if (type == INPUT_MPLANE || type == INPUT_META_PLANE) {
  853. allow = true;
  854. goto exit;
  855. }
  856. }
  857. exit:
  858. if (!allow)
  859. i_vpr_e(inst, "%s: type %d not allowed in state %s\n",
  860. __func__, type, state_name(inst->state));
  861. return allow;
  862. }
  863. enum msm_vidc_allow msm_vidc_allow_stop(struct msm_vidc_inst *inst)
  864. {
  865. enum msm_vidc_allow allow = MSM_VIDC_DISALLOW;
  866. if (!inst) {
  867. d_vpr_e("%s: invalid params\n", __func__);
  868. return allow;
  869. }
  870. if (inst->state == MSM_VIDC_START ||
  871. inst->state == MSM_VIDC_DRC ||
  872. inst->state == MSM_VIDC_DRC_LAST_FLAG ||
  873. inst->state == MSM_VIDC_DRC_DRAIN) {
  874. allow = MSM_VIDC_ALLOW;
  875. } else if (inst->state == MSM_VIDC_START_INPUT) {
  876. allow = MSM_VIDC_IGNORE;
  877. i_vpr_e(inst, "%s: stop ignored in state %s\n",
  878. __func__, state_name(inst->state));
  879. } else {
  880. i_vpr_e(inst, "%s: stop not allowed in state %s\n",
  881. __func__, state_name(inst->state));
  882. }
  883. return allow;
  884. }
  885. bool msm_vidc_allow_start(struct msm_vidc_inst *inst)
  886. {
  887. if (!inst) {
  888. d_vpr_e("%s: invalid params\n", __func__);
  889. return false;
  890. }
  891. if (inst->state == MSM_VIDC_DRAIN_LAST_FLAG ||
  892. inst->state == MSM_VIDC_DRC_LAST_FLAG ||
  893. inst->state == MSM_VIDC_DRC_DRAIN_LAST_FLAG)
  894. return true;
  895. i_vpr_e(inst, "%s: not allowed in state %s\n",
  896. __func__, state_name(inst->state));
  897. return false;
  898. }
  899. bool msm_vidc_allow_streamon(struct msm_vidc_inst *inst, u32 type)
  900. {
  901. if (!inst) {
  902. d_vpr_e("%s: invalid params\n", __func__);
  903. return false;
  904. }
  905. if (type == INPUT_MPLANE || type == INPUT_META_PLANE) {
  906. if (inst->state == MSM_VIDC_OPEN ||
  907. inst->state == MSM_VIDC_START_OUTPUT)
  908. return true;
  909. } else if (type == OUTPUT_MPLANE || type == OUTPUT_META_PLANE) {
  910. if (inst->state == MSM_VIDC_OPEN ||
  911. inst->state == MSM_VIDC_START_INPUT ||
  912. inst->state == MSM_VIDC_DRAIN_START_INPUT)
  913. return true;
  914. }
  915. i_vpr_e(inst, "%s: type %d not allowed in state %s\n",
  916. __func__, type, state_name(inst->state));
  917. return false;
  918. }
  919. bool msm_vidc_allow_streamoff(struct msm_vidc_inst *inst, u32 type)
  920. {
  921. bool allow = true;
  922. if (!inst) {
  923. d_vpr_e("%s: invalid params\n", __func__);
  924. return false;
  925. }
  926. if (type == INPUT_MPLANE) {
  927. if (inst->state == MSM_VIDC_OPEN ||
  928. inst->state == MSM_VIDC_START_OUTPUT)
  929. allow = false;
  930. } else if (type == INPUT_META_PLANE) {
  931. if (inst->state == MSM_VIDC_START_INPUT)
  932. allow = false;
  933. } else if (type == OUTPUT_MPLANE) {
  934. if (inst->state == MSM_VIDC_OPEN ||
  935. inst->state == MSM_VIDC_START_INPUT)
  936. allow = false;
  937. } else if (type == OUTPUT_META_PLANE) {
  938. if (inst->state == MSM_VIDC_START_OUTPUT)
  939. allow = false;
  940. }
  941. if (!allow)
  942. i_vpr_e(inst, "%s: type %d not allowed in state %s\n",
  943. __func__, type, state_name(inst->state));
  944. return allow;
  945. }
  946. enum msm_vidc_allow msm_vidc_allow_qbuf(struct msm_vidc_inst *inst, u32 type)
  947. {
  948. if (!inst) {
  949. d_vpr_e("%s: invalid params\n", __func__);
  950. return MSM_VIDC_DISALLOW;
  951. }
  952. if (inst->state == MSM_VIDC_ERROR) {
  953. i_vpr_e(inst, "%s: inst in error state\n", __func__);
  954. return MSM_VIDC_DISALLOW;
  955. }
  956. if (type == INPUT_META_PLANE || type == OUTPUT_META_PLANE)
  957. return MSM_VIDC_DEFER;
  958. if (type == INPUT_MPLANE) {
  959. if (inst->state == MSM_VIDC_OPEN ||
  960. inst->state == MSM_VIDC_START_OUTPUT)
  961. return MSM_VIDC_DEFER;
  962. else
  963. return MSM_VIDC_ALLOW;
  964. } else if (type == OUTPUT_MPLANE) {
  965. if (inst->state == MSM_VIDC_OPEN ||
  966. inst->state == MSM_VIDC_START_INPUT ||
  967. inst->state == MSM_VIDC_DRAIN_START_INPUT)
  968. return MSM_VIDC_DEFER;
  969. else
  970. return MSM_VIDC_ALLOW;
  971. } else {
  972. i_vpr_e(inst, "%s: unknown buffer type %d\n", __func__, type);
  973. return MSM_VIDC_DISALLOW;
  974. }
  975. return MSM_VIDC_DISALLOW;
  976. }
  977. enum msm_vidc_allow msm_vidc_allow_input_psc(struct msm_vidc_inst *inst)
  978. {
  979. enum msm_vidc_allow allow = MSM_VIDC_DISALLOW;
  980. if (!inst) {
  981. d_vpr_e("%s: invalid params\n", __func__);
  982. return MSM_VIDC_DISALLOW;
  983. }
  984. if (inst->state == MSM_VIDC_START ||
  985. inst->state == MSM_VIDC_START_INPUT ||
  986. inst->state == MSM_VIDC_DRAIN) {
  987. allow = MSM_VIDC_ALLOW;
  988. } else if (inst->state == MSM_VIDC_DRC ||
  989. inst->state == MSM_VIDC_DRC_LAST_FLAG ||
  990. inst->state == MSM_VIDC_DRC_DRAIN ||
  991. inst->state == MSM_VIDC_DRC_DRAIN_LAST_FLAG ||
  992. inst->state == MSM_VIDC_DRAIN_START_INPUT) {
  993. i_vpr_h(inst, "%s: defer input psc, inst state %s\n",
  994. __func__, state_name(inst->state));
  995. allow = MSM_VIDC_DEFER;
  996. } else {
  997. i_vpr_e(inst, "%s: input psc in wrong state %s\n",
  998. __func__, state_name(inst->state));
  999. allow = MSM_VIDC_DISALLOW;
  1000. }
  1001. return allow;
  1002. }
  1003. bool msm_vidc_allow_last_flag(struct msm_vidc_inst *inst)
  1004. {
  1005. if (!inst) {
  1006. d_vpr_e("%s: invalid params\n", __func__);
  1007. return false;
  1008. }
  1009. if (inst->state == MSM_VIDC_DRC ||
  1010. inst->state == MSM_VIDC_DRAIN ||
  1011. inst->state == MSM_VIDC_DRC_DRAIN)
  1012. return true;
  1013. i_vpr_e(inst, "%s: not allowed in state %s\n",
  1014. __func__, state_name(inst->state));
  1015. return false;
  1016. }
  1017. static int msm_vidc_process_pending_ipsc(struct msm_vidc_inst *inst,
  1018. enum msm_vidc_inst_state *new_state)
  1019. {
  1020. struct response_work *resp_work, *dummy = NULL;
  1021. int rc = 0;
  1022. if (!inst || !new_state) {
  1023. d_vpr_e("%s: invalid params\n", __func__);
  1024. return -EINVAL;
  1025. }
  1026. if (list_empty(&inst->response_works))
  1027. return 0;
  1028. i_vpr_h(inst, "%s: state %s, ipsc pending\n", __func__, state_name(inst->state));
  1029. list_for_each_entry_safe(resp_work, dummy, &inst->response_works, list) {
  1030. if (resp_work->type == RESP_WORK_INPUT_PSC) {
  1031. rc = handle_session_response_work(inst, resp_work);
  1032. if (rc) {
  1033. i_vpr_e(inst, "%s: handle ipsc failed\n", __func__);
  1034. *new_state = MSM_VIDC_ERROR;
  1035. } else {
  1036. if (inst->state == MSM_VIDC_DRC_DRAIN_LAST_FLAG ||
  1037. inst->state == MSM_VIDC_DRAIN_START_INPUT) {
  1038. *new_state = MSM_VIDC_DRC_DRAIN;
  1039. } else if (inst->state == MSM_VIDC_DRC_LAST_FLAG) {
  1040. *new_state = MSM_VIDC_DRC;
  1041. }
  1042. }
  1043. list_del(&resp_work->list);
  1044. kfree(resp_work->data);
  1045. kfree(resp_work);
  1046. /* list contains max only one ipsc at anytime */
  1047. break;
  1048. }
  1049. }
  1050. return rc;
  1051. }
  1052. int msm_vidc_state_change_streamon(struct msm_vidc_inst *inst, u32 type)
  1053. {
  1054. int rc = 0;
  1055. enum msm_vidc_inst_state new_state = MSM_VIDC_ERROR;
  1056. if (!inst || !inst->core) {
  1057. d_vpr_e("%s: invalid params\n", __func__);
  1058. return -EINVAL;
  1059. }
  1060. if (type == INPUT_META_PLANE || type == OUTPUT_META_PLANE)
  1061. return 0;
  1062. if (type == INPUT_MPLANE) {
  1063. if (inst->state == MSM_VIDC_OPEN)
  1064. new_state = MSM_VIDC_START_INPUT;
  1065. else if (inst->state == MSM_VIDC_START_OUTPUT)
  1066. new_state = MSM_VIDC_START;
  1067. } else if (type == OUTPUT_MPLANE) {
  1068. if (inst->state == MSM_VIDC_OPEN) {
  1069. new_state = MSM_VIDC_START_OUTPUT;
  1070. } else if (inst->state == MSM_VIDC_START_INPUT) {
  1071. new_state = MSM_VIDC_START;
  1072. } else if (inst->state == MSM_VIDC_DRAIN_START_INPUT) {
  1073. i_vpr_h(inst, "%s: streamon(output) in %s state\n",
  1074. __func__, state_name(inst->state));
  1075. new_state = MSM_VIDC_DRAIN;
  1076. rc = msm_vidc_process_pending_ipsc(inst, &new_state);
  1077. if (rc) {
  1078. i_vpr_e(inst, "%s: process pending ipsc failed\n", __func__);
  1079. goto state_change;
  1080. }
  1081. }
  1082. }
  1083. state_change:
  1084. msm_vidc_change_inst_state(inst, new_state, __func__);
  1085. return rc;
  1086. }
  1087. int msm_vidc_state_change_streamoff(struct msm_vidc_inst *inst, u32 type)
  1088. {
  1089. int rc = 0;
  1090. enum msm_vidc_inst_state new_state = MSM_VIDC_ERROR;
  1091. struct response_work *resp_work, *dummy;
  1092. if (!inst || !inst->core) {
  1093. d_vpr_e("%s: invalid params\n", __func__);
  1094. return -EINVAL;
  1095. }
  1096. if (type == INPUT_META_PLANE || type == OUTPUT_META_PLANE)
  1097. return 0;
  1098. if (type == INPUT_MPLANE) {
  1099. if (inst->state == MSM_VIDC_START_INPUT) {
  1100. new_state = MSM_VIDC_OPEN;
  1101. } else if (inst->state == MSM_VIDC_START) {
  1102. new_state = MSM_VIDC_START_OUTPUT;
  1103. } else if (inst->state == MSM_VIDC_DRC ||
  1104. inst->state == MSM_VIDC_DRC_LAST_FLAG ||
  1105. inst->state == MSM_VIDC_DRAIN ||
  1106. inst->state == MSM_VIDC_DRAIN_LAST_FLAG ||
  1107. inst->state == MSM_VIDC_DRC_DRAIN ||
  1108. inst->state == MSM_VIDC_DRC_DRAIN_LAST_FLAG ||
  1109. inst->state == MSM_VIDC_DRAIN_START_INPUT) {
  1110. new_state = MSM_VIDC_START_OUTPUT;
  1111. /* discard pending port settings change if any */
  1112. list_for_each_entry_safe(resp_work, dummy,
  1113. &inst->response_works, list) {
  1114. if (resp_work->type == RESP_WORK_INPUT_PSC) {
  1115. i_vpr_h(inst,
  1116. "%s: discard pending input psc\n", __func__);
  1117. list_del(&resp_work->list);
  1118. kfree(resp_work->data);
  1119. kfree(resp_work);
  1120. }
  1121. }
  1122. }
  1123. } else if (type == OUTPUT_MPLANE) {
  1124. if (inst->state == MSM_VIDC_START_OUTPUT) {
  1125. new_state = MSM_VIDC_OPEN;
  1126. } else if (inst->state == MSM_VIDC_START ||
  1127. inst->state == MSM_VIDC_DRAIN ||
  1128. inst->state == MSM_VIDC_DRAIN_LAST_FLAG ||
  1129. inst->state == MSM_VIDC_DRC ||
  1130. inst->state == MSM_VIDC_DRC_LAST_FLAG ||
  1131. inst->state == MSM_VIDC_DRC_DRAIN) {
  1132. new_state = MSM_VIDC_START_INPUT;
  1133. } else if (inst->state == MSM_VIDC_DRC_DRAIN_LAST_FLAG) {
  1134. new_state = MSM_VIDC_DRAIN_START_INPUT;
  1135. }
  1136. }
  1137. rc = msm_vidc_change_inst_state(inst, new_state, __func__);
  1138. if (rc)
  1139. goto exit;
  1140. exit:
  1141. return rc;
  1142. }
  1143. int msm_vidc_state_change_stop(struct msm_vidc_inst *inst)
  1144. {
  1145. int rc = 0;
  1146. enum msm_vidc_inst_state new_state = MSM_VIDC_ERROR;
  1147. if (!inst || !inst->core) {
  1148. d_vpr_e("%s: invalid params\n", __func__);
  1149. return -EINVAL;
  1150. }
  1151. if (inst->state == MSM_VIDC_START) {
  1152. new_state = MSM_VIDC_DRAIN;
  1153. } else if (inst->state == MSM_VIDC_DRC) {
  1154. new_state = MSM_VIDC_DRC_DRAIN;
  1155. } else if (inst->state == MSM_VIDC_DRC_DRAIN ||
  1156. inst->state == MSM_VIDC_DRC_LAST_FLAG) {
  1157. new_state = MSM_VIDC_DRC_DRAIN_LAST_FLAG;
  1158. } else {
  1159. i_vpr_e(inst, "%s: wrong state %s\n",
  1160. __func__, state_name(inst->state));
  1161. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  1162. return -EINVAL;
  1163. }
  1164. rc = msm_vidc_change_inst_state(inst, new_state, __func__);
  1165. if (rc)
  1166. return rc;
  1167. return rc;
  1168. }
  1169. int msm_vidc_state_change_start(struct msm_vidc_inst *inst)
  1170. {
  1171. int rc = 0;
  1172. enum msm_vidc_inst_state new_state = MSM_VIDC_ERROR;
  1173. if (!inst || !inst->core) {
  1174. d_vpr_e("%s: invalid params\n", __func__);
  1175. return -EINVAL;
  1176. }
  1177. if (inst->state == MSM_VIDC_DRAIN_LAST_FLAG ||
  1178. inst->state == MSM_VIDC_DRC_LAST_FLAG) {
  1179. new_state = MSM_VIDC_START;
  1180. rc = msm_vidc_process_pending_ipsc(inst, &new_state);
  1181. if (rc) {
  1182. i_vpr_e(inst, "%s: process pending ipsc failed\n", __func__);
  1183. goto state_change;
  1184. }
  1185. } else if (inst->state == MSM_VIDC_DRC_DRAIN_LAST_FLAG) {
  1186. new_state = MSM_VIDC_DRAIN;
  1187. rc = msm_vidc_process_pending_ipsc(inst, &new_state);
  1188. if (rc) {
  1189. i_vpr_e(inst, "%s: process pending ipsc failed\n", __func__);
  1190. goto state_change;
  1191. }
  1192. } else {
  1193. i_vpr_e(inst, "%s: wrong state %s\n", __func__, state_name(inst->state));
  1194. new_state = MSM_VIDC_ERROR;
  1195. rc = -EINVAL;
  1196. goto state_change;
  1197. }
  1198. state_change:
  1199. msm_vidc_change_inst_state(inst, new_state, __func__);
  1200. return rc;
  1201. }
  1202. int msm_vidc_state_change_input_psc(struct msm_vidc_inst *inst)
  1203. {
  1204. int rc = 0;
  1205. enum msm_vidc_inst_state new_state = MSM_VIDC_ERROR;
  1206. if (!inst || !inst->core) {
  1207. d_vpr_e("%s: invalid params\n", __func__);
  1208. return -EINVAL;
  1209. }
  1210. /* don't change state as output port is not started yet */
  1211. if (inst->state == MSM_VIDC_START_INPUT)
  1212. return 0;
  1213. if (inst->state == MSM_VIDC_START) {
  1214. new_state = MSM_VIDC_DRC;
  1215. } else if (inst->state == MSM_VIDC_DRAIN) {
  1216. new_state = MSM_VIDC_DRC_DRAIN;
  1217. } else {
  1218. i_vpr_e(inst, "%s: wrong state %s\n",
  1219. __func__, state_name(inst->state));
  1220. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  1221. return -EINVAL;
  1222. }
  1223. rc = msm_vidc_change_inst_state(inst, new_state, __func__);
  1224. if (rc)
  1225. return rc;
  1226. return rc;
  1227. }
  1228. int msm_vidc_state_change_last_flag(struct msm_vidc_inst *inst)
  1229. {
  1230. int rc = 0;
  1231. enum msm_vidc_inst_state new_state = MSM_VIDC_ERROR;
  1232. if (!inst || !inst->core) {
  1233. d_vpr_e("%s: invalid params\n", __func__);
  1234. return -EINVAL;
  1235. }
  1236. if (inst->state == MSM_VIDC_DRC) {
  1237. new_state = MSM_VIDC_DRC_LAST_FLAG;
  1238. } else if (inst->state == MSM_VIDC_DRAIN) {
  1239. new_state = MSM_VIDC_DRAIN_LAST_FLAG;
  1240. } else if (inst->state == MSM_VIDC_DRC_DRAIN) {
  1241. new_state = MSM_VIDC_DRC_DRAIN_LAST_FLAG;
  1242. } else {
  1243. i_vpr_e(inst, "%s: wrong state %s\n",
  1244. __func__, state_name(inst->state));
  1245. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  1246. return -EINVAL;
  1247. }
  1248. rc = msm_vidc_change_inst_state(inst, new_state, __func__);
  1249. if (rc)
  1250. return rc;
  1251. return rc;
  1252. }
  1253. int msm_vidc_get_control(struct msm_vidc_inst *inst, struct v4l2_ctrl *ctrl)
  1254. {
  1255. int rc = 0;
  1256. if (!inst || !ctrl) {
  1257. d_vpr_e("%s: invalid params\n", __func__);
  1258. return -EINVAL;
  1259. }
  1260. switch (ctrl->id) {
  1261. case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
  1262. ctrl->val = inst->buffers.output.min_count +
  1263. inst->buffers.output.extra_count;
  1264. i_vpr_h(inst, "g_min: output buffers %d\n", ctrl->val);
  1265. break;
  1266. case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT:
  1267. ctrl->val = inst->buffers.input.min_count +
  1268. inst->buffers.input.extra_count;
  1269. i_vpr_h(inst, "g_min: input buffers %d\n", ctrl->val);
  1270. break;
  1271. default:
  1272. break;
  1273. }
  1274. return rc;
  1275. }
  1276. int msm_vidc_get_mbs_per_frame(struct msm_vidc_inst *inst)
  1277. {
  1278. int height, width;
  1279. struct v4l2_format *out_f;
  1280. struct v4l2_format *inp_f;
  1281. out_f = &inst->fmts[OUTPUT_PORT];
  1282. inp_f = &inst->fmts[INPUT_PORT];
  1283. height = max(out_f->fmt.pix_mp.height,
  1284. inp_f->fmt.pix_mp.height);
  1285. width = max(out_f->fmt.pix_mp.width,
  1286. inp_f->fmt.pix_mp.width);
  1287. return NUM_MBS_PER_FRAME(height, width);
  1288. }
  1289. int msm_vidc_get_fps(struct msm_vidc_inst *inst)
  1290. {
  1291. int fps;
  1292. u32 frame_rate, operating_rate;
  1293. if (!inst || !inst->capabilities) {
  1294. d_vpr_e("%s: invalid params\n", __func__);
  1295. return -EINVAL;
  1296. }
  1297. frame_rate = inst->capabilities->cap[FRAME_RATE].value;
  1298. operating_rate = inst->capabilities->cap[OPERATING_RATE].value;
  1299. if (operating_rate > frame_rate)
  1300. fps = (operating_rate >> 16) ?
  1301. (operating_rate >> 16) : 1;
  1302. else
  1303. fps = frame_rate >> 16;
  1304. return fps;
  1305. }
  1306. int msm_vidc_num_buffers(struct msm_vidc_inst *inst,
  1307. enum msm_vidc_buffer_type type, enum msm_vidc_buffer_attributes attr)
  1308. {
  1309. int count = 0;
  1310. struct msm_vidc_buffer *vbuf;
  1311. struct msm_vidc_buffers *buffers;
  1312. if (!inst) {
  1313. d_vpr_e("%s: invalid params\n", __func__);
  1314. return count;
  1315. }
  1316. if (type == MSM_VIDC_BUF_OUTPUT) {
  1317. buffers = &inst->buffers.output;
  1318. } else if (type == MSM_VIDC_BUF_INPUT) {
  1319. buffers = &inst->buffers.input;
  1320. } else {
  1321. i_vpr_e(inst, "%s: invalid buffer type %#x\n",
  1322. __func__, type);
  1323. return count;
  1324. }
  1325. list_for_each_entry(vbuf, &buffers->list, list) {
  1326. if (vbuf->type != type)
  1327. continue;
  1328. if (!(vbuf->attr & attr))
  1329. continue;
  1330. count++;
  1331. }
  1332. return count;
  1333. }
  1334. static int vb2_buffer_to_driver(struct vb2_buffer *vb2,
  1335. struct msm_vidc_buffer *buf)
  1336. {
  1337. int rc = 0;
  1338. if (!vb2 || !buf) {
  1339. d_vpr_e("%s: invalid params\n", __func__);
  1340. return -EINVAL;
  1341. }
  1342. buf->valid = true;
  1343. buf->type = v4l2_type_to_driver(vb2->type, __func__);
  1344. if (!buf->type)
  1345. return -EINVAL;
  1346. buf->index = vb2->index;
  1347. buf->fd = vb2->planes[0].m.fd;
  1348. buf->data_offset = vb2->planes[0].data_offset;
  1349. buf->data_size = vb2->planes[0].bytesused;
  1350. buf->buffer_size = vb2->planes[0].length;
  1351. buf->timestamp = vb2->timestamp;
  1352. return rc;
  1353. }
  1354. int msm_vidc_unmap_driver_buf(struct msm_vidc_inst *inst,
  1355. struct msm_vidc_buffer *buf)
  1356. {
  1357. int rc = 0;
  1358. struct msm_vidc_mappings *mappings;
  1359. struct msm_vidc_map *map = NULL;
  1360. bool found = false;
  1361. if (!inst || !buf) {
  1362. d_vpr_e("%s: invalid params\n", __func__);
  1363. return -EINVAL;
  1364. }
  1365. mappings = msm_vidc_get_mappings(inst, buf->type, __func__);
  1366. if (!mappings)
  1367. return -EINVAL;
  1368. /* sanity check to see if it was not removed */
  1369. list_for_each_entry(map, &mappings->list, list) {
  1370. if (map->dmabuf == buf->dmabuf) {
  1371. found = true;
  1372. break;
  1373. }
  1374. }
  1375. if (!found) {
  1376. print_vidc_buffer(VIDC_ERR, "err ", "no buf in mappings", inst, buf);
  1377. return -EINVAL;
  1378. }
  1379. rc = msm_vidc_memory_unmap(inst->core, map);
  1380. if (rc) {
  1381. print_vidc_buffer(VIDC_ERR, "err ", "unmap failed", inst, buf);
  1382. return -EINVAL;
  1383. }
  1384. /* finally delete if refcount is zero */
  1385. if (!map->refcount) {
  1386. list_del(&map->list);
  1387. kfree(map);
  1388. }
  1389. return 0;
  1390. }
  1391. int msm_vidc_put_driver_buf(struct msm_vidc_inst *inst,
  1392. struct msm_vidc_buffer *buf)
  1393. {
  1394. int rc = 0;
  1395. if (!inst || !buf) {
  1396. d_vpr_e("%s: invalid params\n", __func__);
  1397. return -EINVAL;
  1398. }
  1399. rc = msm_vidc_unmap_driver_buf(inst, buf);
  1400. if (rc)
  1401. return rc;
  1402. msm_vidc_memory_put_dmabuf(buf->dmabuf);
  1403. /* delete the buffer from buffers->list */
  1404. list_del(&buf->list);
  1405. kfree(buf);
  1406. return 0;
  1407. }
  1408. int msm_vidc_map_driver_buf(struct msm_vidc_inst *inst,
  1409. struct msm_vidc_buffer *buf)
  1410. {
  1411. int rc = 0;
  1412. struct msm_vidc_mappings *mappings;
  1413. struct msm_vidc_map *map = NULL;
  1414. bool found = false;
  1415. if (!inst || !buf) {
  1416. d_vpr_e("%s: invalid params\n", __func__);
  1417. return -EINVAL;
  1418. }
  1419. mappings = msm_vidc_get_mappings(inst, buf->type, __func__);
  1420. if (!mappings)
  1421. return -EINVAL;
  1422. /* check if it is an existing one */
  1423. list_for_each_entry(map, &mappings->list, list) {
  1424. if (map->dmabuf == buf->dmabuf) {
  1425. found = true;
  1426. break;
  1427. }
  1428. }
  1429. if (found) {
  1430. /* skip mapping for RO buffer */
  1431. if (!(buf->attr & MSM_VIDC_ATTR_READ_ONLY)) {
  1432. rc = msm_vidc_memory_map(inst->core, map);
  1433. if (rc)
  1434. return -ENOMEM;
  1435. buf->device_addr = map->device_addr;
  1436. }
  1437. return 0;
  1438. }
  1439. map = kzalloc(sizeof(struct msm_vidc_map), GFP_KERNEL);
  1440. if (!map) {
  1441. i_vpr_e(inst, "%s: alloc failed\n", __func__);
  1442. return -ENOMEM;
  1443. }
  1444. INIT_LIST_HEAD(&map->list);
  1445. map->type = buf->type;
  1446. map->dmabuf = buf->dmabuf;
  1447. map->region = msm_vidc_get_buffer_region(inst, buf->type, __func__);
  1448. rc = msm_vidc_memory_map(inst->core, map);
  1449. if (rc) {
  1450. kfree(map);
  1451. return -ENOMEM;
  1452. }
  1453. buf->device_addr = map->device_addr;
  1454. list_add_tail(&map->list, &mappings->list);
  1455. return 0;
  1456. }
  1457. struct msm_vidc_buffer *msm_vidc_get_driver_buf(struct msm_vidc_inst *inst,
  1458. struct vb2_buffer *vb2)
  1459. {
  1460. int rc = 0;
  1461. struct msm_vidc_buffer *buf = NULL;
  1462. struct msm_vidc_buffers *buffers;
  1463. struct dma_buf *dmabuf;
  1464. enum msm_vidc_buffer_type buf_type;
  1465. bool found = false;
  1466. if (!inst || !vb2) {
  1467. d_vpr_e("%s: invalid params\n", __func__);
  1468. return NULL;
  1469. }
  1470. buf_type = v4l2_type_to_driver(vb2->type, __func__);
  1471. if (!buf_type)
  1472. return NULL;
  1473. buffers = msm_vidc_get_buffers(inst, buf_type, __func__);
  1474. if (!buffers)
  1475. return NULL;
  1476. dmabuf = msm_vidc_memory_get_dmabuf(vb2->planes[0].m.fd);
  1477. if (!dmabuf)
  1478. return NULL;
  1479. /* check if it is an existing buffer */
  1480. list_for_each_entry(buf, &buffers->list, list) {
  1481. if (buf->dmabuf == dmabuf &&
  1482. buf->data_offset == vb2->planes[0].data_offset) {
  1483. found = true;
  1484. break;
  1485. }
  1486. }
  1487. if (found) {
  1488. /* only YUV buffers are allowed to repeat */
  1489. if ((is_decode_session(inst) && vb2->type != OUTPUT_MPLANE) ||
  1490. (is_encode_session(inst) && vb2->type != INPUT_MPLANE)) {
  1491. print_vidc_buffer(VIDC_ERR, "err ",
  1492. "existing buffer", inst, buf);
  1493. goto error;
  1494. }
  1495. /* for decoder, YUV with RO flag are allowed to repeat */
  1496. if (is_decode_session(inst) &&
  1497. !(buf->attr & MSM_VIDC_ATTR_READ_ONLY)) {
  1498. print_vidc_buffer(VIDC_ERR, "err ",
  1499. "existing buffer without RO flag", inst, buf);
  1500. goto error;
  1501. }
  1502. /* for encoder, treat the repeated buffer as new buffer */
  1503. if (is_encode_session(inst) && vb2->type == INPUT_MPLANE)
  1504. found = false;
  1505. }
  1506. if (!found) {
  1507. buf = kzalloc(sizeof(struct msm_vidc_buffer), GFP_KERNEL);
  1508. if (!buf) {
  1509. i_vpr_e(inst, "%s: alloc failed\n", __func__);
  1510. goto error;
  1511. }
  1512. buf->dmabuf = dmabuf;
  1513. INIT_LIST_HEAD(&buf->list);
  1514. list_add_tail(&buf->list, &buffers->list);
  1515. } else {
  1516. /* reset all attributes except read only */
  1517. buf->attr &= MSM_VIDC_ATTR_READ_ONLY;
  1518. }
  1519. rc = vb2_buffer_to_driver(vb2, buf);
  1520. if (rc)
  1521. goto error;
  1522. rc = msm_vidc_map_driver_buf(inst, buf);
  1523. if (rc)
  1524. goto error;
  1525. return buf;
  1526. error:
  1527. msm_vidc_memory_put_dmabuf(dmabuf);
  1528. if (!found)
  1529. kfree(buf);
  1530. return NULL;
  1531. }
  1532. struct msm_vidc_buffer *get_meta_buffer(struct msm_vidc_inst *inst,
  1533. struct msm_vidc_buffer *buf)
  1534. {
  1535. struct msm_vidc_buffer *mbuf;
  1536. struct msm_vidc_buffers *buffers;
  1537. bool found = false;
  1538. if (!inst || !buf) {
  1539. d_vpr_e("%s: invalid params\n", __func__);
  1540. return NULL;
  1541. }
  1542. if (buf->type == MSM_VIDC_BUF_INPUT) {
  1543. buffers = &inst->buffers.input_meta;
  1544. } else if (buf->type == MSM_VIDC_BUF_OUTPUT) {
  1545. buffers = &inst->buffers.output_meta;
  1546. } else {
  1547. i_vpr_e(inst, "%s: invalid buffer type %d\n",
  1548. __func__, buf->type);
  1549. return NULL;
  1550. }
  1551. list_for_each_entry(mbuf, &buffers->list, list) {
  1552. if (!mbuf->valid)
  1553. continue;
  1554. if (mbuf->index == buf->index) {
  1555. found = true;
  1556. break;
  1557. }
  1558. }
  1559. if (!found)
  1560. return NULL;
  1561. return mbuf;
  1562. }
  1563. bool msm_vidc_is_super_buffer(struct msm_vidc_inst *inst)
  1564. {
  1565. struct msm_vidc_inst_capability *capability = NULL;
  1566. if (!inst || !inst->capabilities) {
  1567. i_vpr_e(inst, "%s: Invalid params\n", __func__);
  1568. return false;
  1569. }
  1570. capability = inst->capabilities;
  1571. return !!capability->cap[SUPER_FRAME].value;
  1572. }
  1573. int msm_vidc_queue_buffer(struct msm_vidc_inst *inst, struct vb2_buffer *vb2)
  1574. {
  1575. int rc = 0;
  1576. struct msm_vidc_buffer *buf;
  1577. struct msm_vidc_buffer *meta;
  1578. enum msm_vidc_allow allow;
  1579. int port;
  1580. if (!inst || !vb2) {
  1581. d_vpr_e("%s: invalid params\n", __func__);
  1582. return -EINVAL;
  1583. }
  1584. buf = msm_vidc_get_driver_buf(inst, vb2);
  1585. if (!buf)
  1586. return -EINVAL;
  1587. /* skip queuing if streamon not completed */
  1588. port = v4l2_type_to_driver_port(inst, vb2->type, __func__);
  1589. if (port < 0)
  1590. return -EINVAL;
  1591. allow = msm_vidc_allow_qbuf(inst, vb2->type);
  1592. if (allow == MSM_VIDC_DISALLOW) {
  1593. i_vpr_e(inst, "%s: qbuf not allowed\n", __func__);
  1594. return -EINVAL;
  1595. }
  1596. if (!inst->vb2q[port].streaming || allow == MSM_VIDC_DEFER) {
  1597. buf->attr |= MSM_VIDC_ATTR_DEFERRED;
  1598. print_vidc_buffer(VIDC_HIGH, "high", "qbuf deferred", inst, buf);
  1599. return 0;
  1600. }
  1601. if (is_decode_session(inst) &&
  1602. inst->capabilities->cap[CODEC_CONFIG].value) {
  1603. buf->flags |= MSM_VIDC_BUF_FLAG_CODECCONFIG;
  1604. inst->capabilities->cap[CODEC_CONFIG].value = 0;
  1605. }
  1606. if (buf->type == MSM_VIDC_BUF_INPUT) {
  1607. inst->power.buffer_counter++;
  1608. msm_vidc_scale_power(inst, true);
  1609. }
  1610. print_vidc_buffer(VIDC_HIGH, "high", "qbuf", inst, buf);
  1611. meta = get_meta_buffer(inst, buf);
  1612. if (meta)
  1613. print_vidc_buffer(VIDC_HIGH, "high", "qbuf", inst, meta);
  1614. if (!meta && is_meta_enabled(inst, buf->type)) {
  1615. print_vidc_buffer(VIDC_ERR, "err ", "missing meta for", inst, buf);
  1616. return -EINVAL;
  1617. }
  1618. if (msm_vidc_is_super_buffer(inst) && is_input_buffer(buf->type))
  1619. rc = venus_hfi_queue_super_buffer(inst, buf, meta);
  1620. else
  1621. rc = venus_hfi_queue_buffer(inst, buf, meta);
  1622. if (rc)
  1623. return rc;
  1624. buf->attr &= ~MSM_VIDC_ATTR_DEFERRED;
  1625. buf->attr |= MSM_VIDC_ATTR_QUEUED;
  1626. if (meta) {
  1627. meta->attr &= ~MSM_VIDC_ATTR_DEFERRED;
  1628. meta->attr |= MSM_VIDC_ATTR_QUEUED;
  1629. }
  1630. if (buf->type == MSM_VIDC_BUF_INPUT)
  1631. msm_vidc_debugfs_update(inst, MSM_VIDC_DEBUGFS_EVENT_ETB);
  1632. else if (buf->type == MSM_VIDC_BUF_OUTPUT)
  1633. msm_vidc_debugfs_update(inst, MSM_VIDC_DEBUGFS_EVENT_FTB);
  1634. return rc;
  1635. }
  1636. int msm_vidc_destroy_internal_buffer(struct msm_vidc_inst *inst,
  1637. struct msm_vidc_buffer *buffer)
  1638. {
  1639. struct msm_vidc_buffers *buffers;
  1640. struct msm_vidc_allocations *allocations;
  1641. struct msm_vidc_mappings *mappings;
  1642. struct msm_vidc_alloc *alloc, *alloc_dummy;
  1643. struct msm_vidc_map *map, *map_dummy;
  1644. struct msm_vidc_buffer *buf, *dummy;
  1645. if (!inst || !inst->core) {
  1646. d_vpr_e("%s: invalid params\n", __func__);
  1647. return -EINVAL;
  1648. }
  1649. if (!is_internal_buffer(buffer->type)) {
  1650. i_vpr_e(inst, "%s: buffer type %#x is not internal\n",
  1651. __func__, buffer->type);
  1652. return 0;
  1653. }
  1654. i_vpr_h(inst,
  1655. "%s: destroy buffer_type %#x, size %d device_addr %#x\n",
  1656. __func__, buffer->type, buffer->buffer_size,
  1657. buffer->device_addr);
  1658. buffers = msm_vidc_get_buffers(inst, buffer->type, __func__);
  1659. if (!buffers)
  1660. return -EINVAL;
  1661. allocations = msm_vidc_get_allocations(inst, buffer->type, __func__);
  1662. if (!allocations)
  1663. return -EINVAL;
  1664. mappings = msm_vidc_get_mappings(inst, buffer->type, __func__);
  1665. if (!mappings)
  1666. return -EINVAL;
  1667. list_for_each_entry_safe(map, map_dummy, &mappings->list, list) {
  1668. if (map->dmabuf == buffer->dmabuf) {
  1669. msm_vidc_memory_unmap(inst->core, map);
  1670. list_del(&map->list);
  1671. kfree(map);
  1672. break;
  1673. }
  1674. }
  1675. list_for_each_entry_safe(alloc, alloc_dummy, &allocations->list, list) {
  1676. if (alloc->dmabuf == buffer->dmabuf) {
  1677. msm_vidc_memory_free(inst->core, alloc);
  1678. list_del(&alloc->list);
  1679. kfree(alloc);
  1680. break;
  1681. }
  1682. }
  1683. list_for_each_entry_safe(buf, dummy, &buffers->list, list) {
  1684. if (buf->dmabuf == buffer->dmabuf) {
  1685. list_del(&buf->list);
  1686. kfree(buf);
  1687. break;
  1688. }
  1689. }
  1690. buffers->size = 0;
  1691. buffers->min_count = buffers->extra_count = buffers->actual_count = 0;
  1692. return 0;
  1693. }
  1694. int msm_vidc_get_internal_buffers(struct msm_vidc_inst *inst,
  1695. enum msm_vidc_buffer_type buffer_type)
  1696. {
  1697. u32 buf_size;
  1698. u32 buf_count;
  1699. struct msm_vidc_core *core;
  1700. struct msm_vidc_buffers *buffers;
  1701. if (!inst || !inst->core) {
  1702. d_vpr_e("%s: invalid params\n", __func__);
  1703. return -EINVAL;
  1704. }
  1705. core = inst->core;
  1706. buf_size = call_session_op(core, buffer_size,
  1707. inst, buffer_type);
  1708. buf_count = call_session_op(core, min_count,
  1709. inst, buffer_type);
  1710. buffers = msm_vidc_get_buffers(inst, buffer_type, __func__);
  1711. if (!buffers)
  1712. return -EINVAL;
  1713. if (buf_size <= buffers->size &&
  1714. buf_count <= buffers->min_count) {
  1715. buffers->reuse = true;
  1716. } else {
  1717. buffers->reuse = false;
  1718. buffers->size = buf_size;
  1719. buffers->min_count = buf_count;
  1720. }
  1721. return 0;
  1722. }
  1723. int msm_vidc_create_internal_buffer(struct msm_vidc_inst *inst,
  1724. enum msm_vidc_buffer_type buffer_type, u32 index)
  1725. {
  1726. int rc = 0;
  1727. struct msm_vidc_buffers *buffers;
  1728. struct msm_vidc_allocations *allocations;
  1729. struct msm_vidc_mappings *mappings;
  1730. struct msm_vidc_buffer *buffer;
  1731. struct msm_vidc_alloc *alloc;
  1732. struct msm_vidc_map *map;
  1733. if (!inst || !inst->core) {
  1734. d_vpr_e("%s: invalid params\n", __func__);
  1735. return -EINVAL;
  1736. }
  1737. if (!is_internal_buffer(buffer_type)) {
  1738. i_vpr_e(inst, "%s: buffer type %#x is not internal\n",
  1739. __func__, buffer_type);
  1740. return 0;
  1741. }
  1742. buffers = msm_vidc_get_buffers(inst, buffer_type, __func__);
  1743. if (!buffers)
  1744. return -EINVAL;
  1745. allocations = msm_vidc_get_allocations(inst, buffer_type, __func__);
  1746. if (!allocations)
  1747. return -EINVAL;
  1748. mappings = msm_vidc_get_mappings(inst, buffer_type, __func__);
  1749. if (!mappings)
  1750. return -EINVAL;
  1751. if (!buffers->size) {
  1752. i_vpr_e(inst, "%s: invalid buffer %#x\n",
  1753. __func__, buffer_type);
  1754. return -EINVAL;
  1755. }
  1756. buffer = kzalloc(sizeof(struct msm_vidc_buffer), GFP_KERNEL);
  1757. if (!buffer) {
  1758. i_vpr_e(inst, "%s: buf alloc failed\n", __func__);
  1759. return -ENOMEM;
  1760. }
  1761. INIT_LIST_HEAD(&buffer->list);
  1762. buffer->valid = true;
  1763. buffer->type = buffer_type;
  1764. buffer->index = index;
  1765. buffer->buffer_size = buffers->size;
  1766. list_add_tail(&buffer->list, &buffers->list);
  1767. alloc = kzalloc(sizeof(struct msm_vidc_alloc), GFP_KERNEL);
  1768. if (!alloc) {
  1769. i_vpr_e(inst, "%s: alloc failed\n", __func__);
  1770. return -ENOMEM;
  1771. }
  1772. INIT_LIST_HEAD(&alloc->list);
  1773. alloc->type = buffer_type;
  1774. alloc->region = msm_vidc_get_buffer_region(inst,
  1775. buffer_type, __func__);
  1776. alloc->size = buffer->buffer_size;
  1777. alloc->secure = (alloc->region > MSM_VIDC_NON_SECURE) ? 1 : 0;
  1778. rc = msm_vidc_memory_alloc(inst->core, alloc);
  1779. if (rc)
  1780. return -ENOMEM;
  1781. list_add_tail(&alloc->list, &allocations->list);
  1782. map = kzalloc(sizeof(struct msm_vidc_map), GFP_KERNEL);
  1783. if (!map) {
  1784. i_vpr_e(inst, "%s: map alloc failed\n", __func__);
  1785. return -ENOMEM;
  1786. }
  1787. INIT_LIST_HEAD(&map->list);
  1788. map->type = alloc->type;
  1789. map->region = alloc->region;
  1790. map->dmabuf = alloc->dmabuf;
  1791. rc = msm_vidc_memory_map(inst->core, map);
  1792. if (rc)
  1793. return -ENOMEM;
  1794. list_add_tail(&map->list, &mappings->list);
  1795. buffer->dmabuf = alloc->dmabuf;
  1796. buffer->device_addr = map->device_addr;
  1797. i_vpr_h(inst,
  1798. "%s: created buffer_type %#x, size %d device_addr %#x\n",
  1799. __func__, buffer_type, buffers->size,
  1800. buffer->device_addr);
  1801. return 0;
  1802. }
  1803. int msm_vidc_create_internal_buffers(struct msm_vidc_inst *inst,
  1804. enum msm_vidc_buffer_type buffer_type)
  1805. {
  1806. int rc = 0;
  1807. struct msm_vidc_buffers *buffers;
  1808. int i;
  1809. if (!inst || !inst->core) {
  1810. d_vpr_e("%s: invalid params\n", __func__);
  1811. return -EINVAL;
  1812. }
  1813. buffers = msm_vidc_get_buffers(inst, buffer_type, __func__);
  1814. if (!buffers)
  1815. return -EINVAL;
  1816. if (buffers->reuse) {
  1817. i_vpr_l(inst, "%s: reuse enabled for buffer type %#x\n",
  1818. __func__, buffer_type);
  1819. return 0;
  1820. }
  1821. for (i = 0; i < buffers->min_count; i++) {
  1822. rc = msm_vidc_create_internal_buffer(inst, buffer_type, i);
  1823. if (rc)
  1824. return rc;
  1825. }
  1826. return rc;
  1827. }
  1828. int msm_vidc_queue_internal_buffers(struct msm_vidc_inst *inst,
  1829. enum msm_vidc_buffer_type buffer_type)
  1830. {
  1831. int rc = 0;
  1832. struct msm_vidc_buffers *buffers;
  1833. struct msm_vidc_buffer *buffer, *dummy;
  1834. if (!inst || !inst->core) {
  1835. d_vpr_e("%s: invalid params\n", __func__);
  1836. return -EINVAL;
  1837. }
  1838. if (!is_internal_buffer(buffer_type)) {
  1839. i_vpr_e(inst, "%s: buffer type %#x is not internal\n",
  1840. __func__, buffer_type);
  1841. return 0;
  1842. }
  1843. buffers = msm_vidc_get_buffers(inst, buffer_type, __func__);
  1844. if (!buffers)
  1845. return -EINVAL;
  1846. if (buffers->reuse) {
  1847. i_vpr_l(inst, "%s: reuse enabled for buffer type %#x\n",
  1848. __func__, buffer_type);
  1849. return 0;
  1850. }
  1851. list_for_each_entry_safe(buffer, dummy, &buffers->list, list) {
  1852. /* do not queue pending release buffers */
  1853. if (buffer->flags & MSM_VIDC_ATTR_PENDING_RELEASE)
  1854. continue;
  1855. /* do not queue already queued buffers */
  1856. if (buffer->attr & MSM_VIDC_ATTR_QUEUED)
  1857. continue;
  1858. rc = venus_hfi_queue_buffer(inst, buffer, NULL);
  1859. if (rc)
  1860. return rc;
  1861. /* mark queued */
  1862. buffer->attr |= MSM_VIDC_ATTR_QUEUED;
  1863. i_vpr_h(inst, "%s: queued buffer_type %#x, size %d\n",
  1864. __func__, buffer_type, buffers->size);
  1865. }
  1866. return 0;
  1867. }
  1868. int msm_vidc_alloc_and_queue_session_internal_buffers(struct msm_vidc_inst *inst,
  1869. enum msm_vidc_buffer_type buffer_type)
  1870. {
  1871. int rc = 0;
  1872. if (!inst || !inst->core) {
  1873. d_vpr_e("%s: invalid params\n", __func__);
  1874. return -EINVAL;
  1875. }
  1876. if (buffer_type != MSM_VIDC_BUF_ARP &&
  1877. buffer_type != MSM_VIDC_BUF_PERSIST) {
  1878. i_vpr_e(inst, "%s: invalid buffer type: %d\n",
  1879. __func__, buffer_type);
  1880. rc = -EINVAL;
  1881. goto exit;
  1882. }
  1883. rc = msm_vidc_get_internal_buffers(inst, buffer_type);
  1884. if (rc)
  1885. goto exit;
  1886. rc = msm_vidc_create_internal_buffers(inst, buffer_type);
  1887. if (rc)
  1888. goto exit;
  1889. rc = msm_vidc_queue_internal_buffers(inst, buffer_type);
  1890. if (rc)
  1891. goto exit;
  1892. exit:
  1893. return rc;
  1894. }
  1895. int msm_vidc_release_internal_buffers(struct msm_vidc_inst *inst,
  1896. enum msm_vidc_buffer_type buffer_type)
  1897. {
  1898. int rc = 0;
  1899. struct msm_vidc_buffers *buffers;
  1900. struct msm_vidc_buffer *buffer, *dummy;
  1901. if (!inst || !inst->core) {
  1902. d_vpr_e("%s: invalid params\n", __func__);
  1903. return -EINVAL;
  1904. }
  1905. if (!is_internal_buffer(buffer_type)) {
  1906. i_vpr_e(inst, "%s: buffer type %#x is not internal\n",
  1907. __func__, buffer_type);
  1908. return 0;
  1909. }
  1910. buffers = msm_vidc_get_buffers(inst, buffer_type, __func__);
  1911. if (!buffers)
  1912. return -EINVAL;
  1913. if (buffers->reuse) {
  1914. i_vpr_l(inst, "%s: reuse enabled for buffer type %#x\n",
  1915. __func__, buffer_type);
  1916. return 0;
  1917. }
  1918. list_for_each_entry_safe(buffer, dummy, &buffers->list, list) {
  1919. /* do not release already pending release buffers */
  1920. if (buffer->attr & MSM_VIDC_ATTR_PENDING_RELEASE)
  1921. continue;
  1922. /* release only queued buffers */
  1923. if (!(buffer->attr & MSM_VIDC_ATTR_QUEUED))
  1924. continue;
  1925. rc = venus_hfi_release_buffer(inst, buffer);
  1926. if (rc)
  1927. return rc;
  1928. /* mark pending release */
  1929. buffer->attr |= MSM_VIDC_ATTR_PENDING_RELEASE;
  1930. i_vpr_h(inst, "%s: released buffer_type %#x, size %d\n",
  1931. __func__, buffer_type, buffers->size);
  1932. }
  1933. return 0;
  1934. }
  1935. int msm_vidc_vb2_buffer_done(struct msm_vidc_inst *inst,
  1936. struct msm_vidc_buffer *buf)
  1937. {
  1938. int type, port;
  1939. struct vb2_queue *q;
  1940. struct vb2_buffer *vb2;
  1941. struct vb2_v4l2_buffer *vbuf;
  1942. bool found;
  1943. if (!inst || !buf) {
  1944. d_vpr_e("%s: invalid params\n", __func__);
  1945. return -EINVAL;
  1946. }
  1947. type = v4l2_type_from_driver(buf->type, __func__);
  1948. if (!type)
  1949. return -EINVAL;
  1950. port = v4l2_type_to_driver_port(inst, type, __func__);
  1951. if (port < 0)
  1952. return -EINVAL;
  1953. q = &inst->vb2q[port];
  1954. if (!q->streaming) {
  1955. i_vpr_e(inst, "%s: port %d is not streaming\n",
  1956. __func__, port);
  1957. return -EINVAL;
  1958. }
  1959. found = false;
  1960. list_for_each_entry(vb2, &q->queued_list, queued_entry) {
  1961. if (vb2->state != VB2_BUF_STATE_ACTIVE)
  1962. continue;
  1963. if (vb2->index == buf->index) {
  1964. found = true;
  1965. break;
  1966. }
  1967. }
  1968. if (!found) {
  1969. print_vidc_buffer(VIDC_ERR, "err ", "vb2 not found for", inst, buf);
  1970. return -EINVAL;
  1971. }
  1972. vbuf = to_vb2_v4l2_buffer(vb2);
  1973. vbuf->flags = buf->flags;
  1974. vb2->timestamp = buf->timestamp;
  1975. vb2->planes[0].bytesused = buf->data_size;
  1976. vb2_buffer_done(vb2, VB2_BUF_STATE_DONE);
  1977. return 0;
  1978. }
  1979. int msm_vidc_event_queue_init(struct msm_vidc_inst *inst)
  1980. {
  1981. int rc = 0;
  1982. int index;
  1983. struct msm_vidc_core *core;
  1984. if (!inst || !inst->core) {
  1985. d_vpr_e("%s: invalid params\n", __func__);
  1986. return -EINVAL;
  1987. }
  1988. core = inst->core;
  1989. if (is_decode_session(inst))
  1990. index = 0;
  1991. else if (is_encode_session(inst))
  1992. index = 1;
  1993. else
  1994. return -EINVAL;
  1995. v4l2_fh_init(&inst->event_handler, &core->vdev[index].vdev);
  1996. v4l2_fh_add(&inst->event_handler);
  1997. return rc;
  1998. }
  1999. int msm_vidc_event_queue_deinit(struct msm_vidc_inst *inst)
  2000. {
  2001. int rc = 0;
  2002. if (!inst) {
  2003. d_vpr_e("%s: invalid params\n", __func__);
  2004. return -EINVAL;
  2005. }
  2006. v4l2_fh_del(&inst->event_handler);
  2007. v4l2_fh_exit(&inst->event_handler);
  2008. return rc;
  2009. }
  2010. static int vb2q_init(struct msm_vidc_inst *inst,
  2011. struct vb2_queue *q, enum v4l2_buf_type type)
  2012. {
  2013. int rc = 0;
  2014. struct msm_vidc_core *core;
  2015. if (!inst || !q || !inst->core) {
  2016. d_vpr_e("%s: invalid params\n", __func__);
  2017. return -EINVAL;
  2018. }
  2019. core = inst->core;
  2020. q->type = type;
  2021. q->io_modes = VB2_DMABUF;
  2022. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  2023. q->ops = core->vb2_ops;
  2024. q->mem_ops = core->vb2_mem_ops;
  2025. q->drv_priv = inst;
  2026. q->allow_zero_bytesused = 1;
  2027. q->copy_timestamp = 1;
  2028. rc = vb2_queue_init(q);
  2029. if (rc)
  2030. i_vpr_e(inst, "%s: vb2_queue_init failed for type %d\n",
  2031. __func__, type);
  2032. return rc;
  2033. }
  2034. int msm_vidc_vb2_queue_init(struct msm_vidc_inst *inst)
  2035. {
  2036. int rc = 0;
  2037. if (!inst) {
  2038. i_vpr_e(inst, "%s: invalid params\n", __func__);
  2039. return -EINVAL;
  2040. }
  2041. if (inst->vb2q_init) {
  2042. i_vpr_h(inst, "%s: vb2q already inited\n", __func__);
  2043. return 0;
  2044. }
  2045. rc = vb2q_init(inst, &inst->vb2q[INPUT_PORT], INPUT_MPLANE);
  2046. if (rc)
  2047. goto exit;
  2048. rc = vb2q_init(inst, &inst->vb2q[OUTPUT_PORT], OUTPUT_MPLANE);
  2049. if (rc)
  2050. goto fail_out_vb2q_init;
  2051. rc = vb2q_init(inst, &inst->vb2q[INPUT_META_PORT], INPUT_META_PLANE);
  2052. if (rc)
  2053. goto fail_in_meta_vb2q_init;
  2054. rc = vb2q_init(inst, &inst->vb2q[OUTPUT_META_PORT], OUTPUT_META_PLANE);
  2055. if (rc)
  2056. goto fail_out_meta_vb2q_init;
  2057. inst->vb2q_init = true;
  2058. return 0;
  2059. fail_out_meta_vb2q_init:
  2060. vb2_queue_release(&inst->vb2q[INPUT_META_PORT]);
  2061. fail_in_meta_vb2q_init:
  2062. vb2_queue_release(&inst->vb2q[OUTPUT_PORT]);
  2063. fail_out_vb2q_init:
  2064. vb2_queue_release(&inst->vb2q[INPUT_PORT]);
  2065. exit:
  2066. return rc;
  2067. }
  2068. int msm_vidc_vb2_queue_deinit(struct msm_vidc_inst *inst)
  2069. {
  2070. int rc = 0;
  2071. if (!inst) {
  2072. d_vpr_e("%s: invalid params\n", __func__);
  2073. return -EINVAL;
  2074. }
  2075. if (!inst->vb2q_init) {
  2076. i_vpr_h(inst, "%s: vb2q already deinited\n", __func__);
  2077. return 0;
  2078. }
  2079. vb2_queue_release(&inst->vb2q[OUTPUT_META_PORT]);
  2080. vb2_queue_release(&inst->vb2q[INPUT_META_PORT]);
  2081. vb2_queue_release(&inst->vb2q[OUTPUT_PORT]);
  2082. vb2_queue_release(&inst->vb2q[INPUT_PORT]);
  2083. inst->vb2q_init = false;
  2084. return rc;
  2085. }
  2086. int msm_vidc_add_session(struct msm_vidc_inst *inst)
  2087. {
  2088. int rc = 0;
  2089. struct msm_vidc_inst *i;
  2090. struct msm_vidc_core *core;
  2091. u32 count = 0;
  2092. if (!inst || !inst->core) {
  2093. d_vpr_e("%s: invalid params\n", __func__);
  2094. return -EINVAL;
  2095. }
  2096. core = inst->core;
  2097. core_lock(core, __func__);
  2098. list_for_each_entry(i, &core->instances, list)
  2099. count++;
  2100. if (count < 0xffffff /*TODO: MAX_SUPPORTED_INSTANCES*/) {
  2101. list_add_tail(&inst->list, &core->instances);
  2102. } else {
  2103. i_vpr_e(inst, "%s: total sessions %d exceeded max limit %d\n",
  2104. __func__, count, MAX_SUPPORTED_INSTANCES);
  2105. rc = -EINVAL;
  2106. }
  2107. core_unlock(core, __func__);
  2108. return rc;
  2109. }
  2110. int msm_vidc_remove_session(struct msm_vidc_inst *inst)
  2111. {
  2112. struct msm_vidc_inst *i, *temp;
  2113. struct msm_vidc_core *core;
  2114. u32 count = 0;
  2115. if (!inst || !inst->core) {
  2116. d_vpr_e("%s: invalid params\n", __func__);
  2117. return -EINVAL;
  2118. }
  2119. core = inst->core;
  2120. core_lock(core, __func__);
  2121. list_for_each_entry_safe(i, temp, &core->instances, list) {
  2122. if (i->session_id == inst->session_id) {
  2123. list_del_init(&i->list);
  2124. i_vpr_h(inst, "%s: removed session %#x\n",
  2125. __func__, i->session_id);
  2126. }
  2127. }
  2128. list_for_each_entry(i, &core->instances, list)
  2129. count++;
  2130. i_vpr_h(inst, "%s: remaining sessions %d\n", __func__, count);
  2131. core_unlock(core, __func__);
  2132. return 0;
  2133. }
  2134. int msm_vidc_session_open(struct msm_vidc_inst *inst)
  2135. {
  2136. int rc = 0;
  2137. if (!inst) {
  2138. d_vpr_e("%s: invalid params\n", __func__);
  2139. return -EINVAL;
  2140. }
  2141. inst->packet_size = 4096;
  2142. inst->packet = kzalloc(inst->packet_size, GFP_KERNEL);
  2143. if (!inst->packet) {
  2144. i_vpr_e(inst, "%s(): inst packet allocation failed\n", __func__);
  2145. return -ENOMEM;
  2146. }
  2147. rc = venus_hfi_session_open(inst);
  2148. if (rc)
  2149. goto error;
  2150. return 0;
  2151. error:
  2152. i_vpr_e(inst, "%s(): session open failed\n", __func__);
  2153. kfree(inst->packet);
  2154. inst->packet = NULL;
  2155. return rc;
  2156. }
  2157. int msm_vidc_session_set_codec(struct msm_vidc_inst *inst)
  2158. {
  2159. int rc = 0;
  2160. if (!inst) {
  2161. d_vpr_e("%s: invalid params\n", __func__);
  2162. return -EINVAL;
  2163. }
  2164. rc = venus_hfi_session_set_codec(inst);
  2165. if (rc)
  2166. return rc;
  2167. return 0;
  2168. }
  2169. int msm_vidc_session_set_default_header(struct msm_vidc_inst *inst)
  2170. {
  2171. int rc = 0;
  2172. u32 default_header = false;
  2173. if (!inst) {
  2174. d_vpr_e("%s: invalid params\n", __func__);
  2175. return -EINVAL;
  2176. }
  2177. default_header = inst->capabilities->cap[DEFAULT_HEADER].value;
  2178. i_vpr_h(inst, "%s: default header: %d", __func__, default_header);
  2179. rc = venus_hfi_session_property(inst,
  2180. HFI_PROP_DEC_DEFAULT_HEADER,
  2181. HFI_HOST_FLAGS_NONE,
  2182. get_hfi_port(inst, INPUT_PORT),
  2183. HFI_PAYLOAD_U32,
  2184. &default_header,
  2185. sizeof(u32));
  2186. if (rc)
  2187. i_vpr_e(inst, "%s: set property failed\n", __func__);
  2188. return rc;
  2189. }
  2190. int msm_vidc_session_streamon(struct msm_vidc_inst *inst,
  2191. enum msm_vidc_port_type port)
  2192. {
  2193. int rc = 0;
  2194. if (!inst || !inst->core) {
  2195. d_vpr_e("%s: invalid params\n", __func__);
  2196. return -EINVAL;
  2197. }
  2198. msm_vidc_scale_power(inst, true);
  2199. rc = venus_hfi_start(inst, port);
  2200. if (rc)
  2201. return rc;
  2202. return rc;
  2203. }
  2204. int msm_vidc_session_streamoff(struct msm_vidc_inst *inst,
  2205. enum msm_vidc_port_type port)
  2206. {
  2207. int rc = 0;
  2208. int count = 0;
  2209. struct msm_vidc_core *core;
  2210. enum signal_session_response signal_type;
  2211. enum msm_vidc_buffer_type buffer_type;
  2212. if (!inst || !inst->core) {
  2213. d_vpr_e("%s: invalid params\n", __func__);
  2214. return -EINVAL;
  2215. }
  2216. if (port == INPUT_PORT) {
  2217. signal_type = SIGNAL_CMD_STOP_INPUT;
  2218. buffer_type = MSM_VIDC_BUF_INPUT;
  2219. } else if (port == OUTPUT_PORT) {
  2220. signal_type = SIGNAL_CMD_STOP_OUTPUT;
  2221. buffer_type = MSM_VIDC_BUF_OUTPUT;
  2222. } else {
  2223. i_vpr_e(inst, "%s: invalid port: %d\n", __func__, port);
  2224. return -EINVAL;
  2225. }
  2226. rc = venus_hfi_stop(inst, port);
  2227. if (rc)
  2228. goto error;
  2229. core = inst->core;
  2230. i_vpr_h(inst, "%s: wait on port: %d for time: %d ms\n",
  2231. __func__, port, core->capabilities[HW_RESPONSE_TIMEOUT].value);
  2232. mutex_unlock(&inst->lock);
  2233. rc = wait_for_completion_timeout(
  2234. &inst->completions[signal_type],
  2235. msecs_to_jiffies(
  2236. core->capabilities[HW_RESPONSE_TIMEOUT].value));
  2237. if (!rc) {
  2238. i_vpr_e(inst, "%s: session stop timed out for port: %d\n",
  2239. __func__, port);
  2240. rc = -ETIMEDOUT;
  2241. msm_vidc_core_timeout(inst->core);
  2242. } else {
  2243. rc = 0;
  2244. }
  2245. mutex_lock(&inst->lock);
  2246. if(rc)
  2247. goto error;
  2248. /* no more queued buffers after streamoff */
  2249. count = msm_vidc_num_buffers(inst, buffer_type, MSM_VIDC_ATTR_QUEUED);
  2250. if (!count) {
  2251. i_vpr_h(inst, "%s: stop successful on port: %d\n",
  2252. __func__, port);
  2253. } else {
  2254. i_vpr_e(inst,
  2255. "%s: %d buffers pending with firmware on port: %d\n",
  2256. __func__, count, port);
  2257. rc = -EINVAL;
  2258. goto error;
  2259. }
  2260. return 0;
  2261. error:
  2262. msm_vidc_kill_session(inst);
  2263. msm_vidc_flush_buffers(inst, buffer_type);
  2264. return rc;
  2265. }
  2266. int msm_vidc_session_close(struct msm_vidc_inst *inst)
  2267. {
  2268. int rc = 0;
  2269. struct msm_vidc_core *core;
  2270. if (!inst || !inst->core) {
  2271. d_vpr_e("%s: invalid params\n", __func__);
  2272. return -EINVAL;
  2273. }
  2274. rc = venus_hfi_session_close(inst);
  2275. if (rc)
  2276. return rc;
  2277. core = inst->core;
  2278. i_vpr_h(inst, "%s: wait on close for time: %d ms\n",
  2279. __func__, core->capabilities[HW_RESPONSE_TIMEOUT].value);
  2280. mutex_unlock(&inst->lock);
  2281. rc = wait_for_completion_timeout(
  2282. &inst->completions[SIGNAL_CMD_CLOSE],
  2283. msecs_to_jiffies(
  2284. core->capabilities[HW_RESPONSE_TIMEOUT].value));
  2285. if (!rc) {
  2286. i_vpr_e(inst, "%s: session close timed out\n", __func__);
  2287. rc = -ETIMEDOUT;
  2288. msm_vidc_core_timeout(inst->core);
  2289. } else {
  2290. rc = 0;
  2291. i_vpr_h(inst, "%s: close successful\n", __func__);
  2292. }
  2293. mutex_lock(&inst->lock);
  2294. msm_vidc_remove_session(inst);
  2295. i_vpr_h(inst, "%s: free session packet data\n", __func__);
  2296. kfree(inst->packet);
  2297. inst->packet = NULL;
  2298. return rc;
  2299. }
  2300. int msm_vidc_kill_session(struct msm_vidc_inst *inst)
  2301. {
  2302. if (!inst) {
  2303. d_vpr_e("%s: invalid params\n", __func__);
  2304. return -EINVAL;
  2305. }
  2306. if (!inst->session_id) {
  2307. i_vpr_e(inst, "%s: already killed\n", __func__);
  2308. return 0;
  2309. }
  2310. i_vpr_e(inst, "%s: killing session\n", __func__);
  2311. msm_vidc_session_close(inst);
  2312. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  2313. return 0;
  2314. }
  2315. int msm_vidc_get_inst_capability(struct msm_vidc_inst *inst)
  2316. {
  2317. int rc = 0;
  2318. int i;
  2319. struct msm_vidc_core *core;
  2320. if (!inst || !inst->core || !inst->capabilities) {
  2321. d_vpr_e("%s: invalid params\n", __func__);
  2322. return -EINVAL;
  2323. }
  2324. core = inst->core;
  2325. for (i = 0; i < core->codecs_count; i++) {
  2326. if (core->inst_caps[i].domain == inst->domain &&
  2327. core->inst_caps[i].codec == inst->codec) {
  2328. i_vpr_h(inst,
  2329. "%s: copied capabilities with %#x codec, %#x domain\n",
  2330. __func__, inst->codec, inst->domain);
  2331. memcpy(inst->capabilities, &core->inst_caps[i],
  2332. sizeof(struct msm_vidc_inst_capability));
  2333. }
  2334. }
  2335. return rc;
  2336. }
  2337. int msm_vidc_deinit_core_caps(struct msm_vidc_core *core)
  2338. {
  2339. int rc = 0;
  2340. if (!core) {
  2341. d_vpr_e("%s: invalid params\n", __func__);
  2342. return -EINVAL;
  2343. }
  2344. kfree(core->capabilities);
  2345. core->capabilities = NULL;
  2346. d_vpr_h("%s: Core capabilities freed\n", __func__);
  2347. return rc;
  2348. }
  2349. int msm_vidc_init_core_caps(struct msm_vidc_core *core)
  2350. {
  2351. int rc = 0;
  2352. int i, num_platform_caps;
  2353. struct msm_platform_core_capability *platform_data;
  2354. if (!core || !core->platform) {
  2355. d_vpr_e("%s: invalid params\n", __func__);
  2356. rc = -EINVAL;
  2357. goto exit;
  2358. }
  2359. platform_data = core->platform->data.core_data;
  2360. if (!platform_data) {
  2361. d_vpr_e("%s: platform core data is NULL\n",
  2362. __func__);
  2363. rc = -EINVAL;
  2364. goto exit;
  2365. }
  2366. core->capabilities = kcalloc(1,
  2367. (sizeof(struct msm_vidc_core_capability) *
  2368. (CORE_CAP_MAX + 1)), GFP_KERNEL);
  2369. if (!core->capabilities) {
  2370. d_vpr_e("%s: failed to allocate core capabilities\n",
  2371. __func__);
  2372. rc = -ENOMEM;
  2373. goto exit;
  2374. }
  2375. num_platform_caps = core->platform->data.core_data_size;
  2376. /* loop over platform caps */
  2377. for (i = 0; i < num_platform_caps && i < CORE_CAP_MAX; i++) {
  2378. core->capabilities[platform_data[i].type].type = platform_data[i].type;
  2379. core->capabilities[platform_data[i].type].value = platform_data[i].value;
  2380. }
  2381. exit:
  2382. return rc;
  2383. }
  2384. static void update_inst_capability(struct msm_platform_inst_capability *in,
  2385. struct msm_vidc_inst_capability *capability)
  2386. {
  2387. if (!in || !capability) {
  2388. d_vpr_e("%s: invalid params %pK %pK\n",
  2389. __func__, in, capability);
  2390. return;
  2391. }
  2392. if (in->cap < INST_CAP_MAX) {
  2393. capability->cap[in->cap].cap = in->cap;
  2394. capability->cap[in->cap].min = in->min;
  2395. capability->cap[in->cap].max = in->max;
  2396. capability->cap[in->cap].step_or_mask = in->step_or_mask;
  2397. capability->cap[in->cap].value = in->value;
  2398. capability->cap[in->cap].flags = in->flags;
  2399. capability->cap[in->cap].v4l2_id = in->v4l2_id;
  2400. capability->cap[in->cap].hfi_id = in->hfi_id;
  2401. memcpy(capability->cap[in->cap].parents, in->parents,
  2402. sizeof(capability->cap[in->cap].parents));
  2403. memcpy(capability->cap[in->cap].children, in->children,
  2404. sizeof(capability->cap[in->cap].children));
  2405. capability->cap[in->cap].adjust = in->adjust;
  2406. capability->cap[in->cap].set = in->set;
  2407. } else {
  2408. d_vpr_e("%s: invalid cap %d\n",
  2409. __func__, in->cap);
  2410. }
  2411. }
  2412. int msm_vidc_deinit_instance_caps(struct msm_vidc_core *core)
  2413. {
  2414. int rc = 0;
  2415. if (!core) {
  2416. d_vpr_e("%s: invalid params\n", __func__);
  2417. return -EINVAL;
  2418. }
  2419. kfree(core->inst_caps);
  2420. core->inst_caps = NULL;
  2421. d_vpr_h("%s: core->inst_caps freed\n", __func__);
  2422. return rc;
  2423. }
  2424. int msm_vidc_init_instance_caps(struct msm_vidc_core *core)
  2425. {
  2426. int rc = 0;
  2427. u8 enc_valid_codecs, dec_valid_codecs;
  2428. u8 count_bits, enc_codec_count;
  2429. u8 codecs_count = 0;
  2430. int i, j, check_bit, num_platform_caps;
  2431. struct msm_platform_inst_capability *platform_data = NULL;
  2432. if (!core || !core->platform || !core->capabilities) {
  2433. d_vpr_e("%s: invalid params\n", __func__);
  2434. rc = -EINVAL;
  2435. goto error;
  2436. }
  2437. platform_data = core->platform->data.instance_data;
  2438. if (!platform_data) {
  2439. d_vpr_e("%s: platform instance data is NULL\n",
  2440. __func__);
  2441. rc = -EINVAL;
  2442. goto error;
  2443. }
  2444. enc_valid_codecs = core->capabilities[ENC_CODECS].value;
  2445. count_bits = enc_valid_codecs;
  2446. COUNT_BITS(count_bits, codecs_count);
  2447. enc_codec_count = codecs_count;
  2448. dec_valid_codecs = core->capabilities[DEC_CODECS].value;
  2449. count_bits = dec_valid_codecs;
  2450. COUNT_BITS(count_bits, codecs_count);
  2451. core->codecs_count = codecs_count;
  2452. core->inst_caps = kcalloc(codecs_count,
  2453. sizeof(struct msm_vidc_inst_capability),
  2454. GFP_KERNEL);
  2455. if (!core->inst_caps) {
  2456. d_vpr_e("%s: failed to allocate core capabilities\n",
  2457. __func__);
  2458. rc = -ENOMEM;
  2459. goto error;
  2460. }
  2461. check_bit = 0;
  2462. /* determine codecs for enc domain */
  2463. for (i = 0; i < enc_codec_count; i++) {
  2464. while (check_bit < (sizeof(enc_valid_codecs) * 8)) {
  2465. if (enc_valid_codecs & BIT(check_bit)) {
  2466. core->inst_caps[i].domain = MSM_VIDC_ENCODER;
  2467. core->inst_caps[i].codec = enc_valid_codecs &
  2468. BIT(check_bit);
  2469. check_bit++;
  2470. break;
  2471. }
  2472. check_bit++;
  2473. }
  2474. }
  2475. /* reset checkbit to check from 0th bit of decoder codecs set bits*/
  2476. check_bit = 0;
  2477. /* determine codecs for dec domain */
  2478. for (; i < codecs_count; i++) {
  2479. while (check_bit < (sizeof(dec_valid_codecs) * 8)) {
  2480. if (dec_valid_codecs & BIT(check_bit)) {
  2481. core->inst_caps[i].domain = MSM_VIDC_DECODER;
  2482. core->inst_caps[i].codec = dec_valid_codecs &
  2483. BIT(check_bit);
  2484. check_bit++;
  2485. break;
  2486. }
  2487. check_bit++;
  2488. }
  2489. }
  2490. num_platform_caps = core->platform->data.instance_data_size;
  2491. d_vpr_h("%s: num caps %d\n", __func__, num_platform_caps);
  2492. /* loop over each platform capability */
  2493. for (i = 0; i < num_platform_caps; i++) {
  2494. /* select matching core codec and update it */
  2495. for (j = 0; j < codecs_count; j++) {
  2496. if ((platform_data[i].domain &
  2497. core->inst_caps[j].domain) &&
  2498. (platform_data[i].codec &
  2499. core->inst_caps[j].codec)) {
  2500. /* update core capability */
  2501. update_inst_capability(&platform_data[i],
  2502. &core->inst_caps[j]);
  2503. }
  2504. }
  2505. }
  2506. error:
  2507. return rc;
  2508. }
  2509. int msm_vidc_core_deinit(struct msm_vidc_core *core, bool force)
  2510. {
  2511. int rc = 0;
  2512. struct msm_vidc_inst *inst, *dummy;
  2513. if (!core) {
  2514. d_vpr_e("%s: invalid params\n", __func__);
  2515. return -EINVAL;
  2516. }
  2517. core_lock(core, __func__);
  2518. d_vpr_h("%s(): force %u\n", __func__, force);
  2519. if (core->state == MSM_VIDC_CORE_DEINIT)
  2520. goto unlock;
  2521. if (!force)
  2522. if (!list_empty(&core->instances))
  2523. goto unlock;
  2524. venus_hfi_core_deinit(core);
  2525. /* unlink all sessions from core, if any */
  2526. list_for_each_entry_safe(inst, dummy, &core->instances, list) {
  2527. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  2528. list_del(&inst->list);
  2529. }
  2530. msm_vidc_change_core_state(core, MSM_VIDC_CORE_DEINIT, __func__);
  2531. unlock:
  2532. core_unlock(core, __func__);
  2533. return rc;
  2534. }
  2535. int msm_vidc_core_init(struct msm_vidc_core *core)
  2536. {
  2537. int rc = 0;
  2538. if (!core || !core->platform) {
  2539. d_vpr_e("%s: invalid params\n", __func__);
  2540. return -EINVAL;
  2541. }
  2542. core_lock(core, __func__);
  2543. if (core->state == MSM_VIDC_CORE_INIT) {
  2544. rc = 0;
  2545. goto unlock;
  2546. }
  2547. msm_vidc_change_core_state(core, MSM_VIDC_CORE_INIT, __func__);
  2548. init_completion(&core->init_done);
  2549. core->smmu_fault_handled = false;
  2550. core->ssr.trigger = false;
  2551. rc = venus_hfi_core_init(core);
  2552. if (rc) {
  2553. d_vpr_e("%s: core init failed\n", __func__);
  2554. goto unlock;
  2555. }
  2556. d_vpr_h("%s(): waiting for sys_init_done, %d ms\n", __func__,
  2557. core->capabilities[HW_RESPONSE_TIMEOUT].value);
  2558. core_unlock(core, __func__);
  2559. rc = wait_for_completion_timeout(&core->init_done, msecs_to_jiffies(
  2560. core->capabilities[HW_RESPONSE_TIMEOUT].value));
  2561. core_lock(core, __func__);
  2562. if (!rc) {
  2563. d_vpr_e("%s: core init timed out\n", __func__);
  2564. rc = -ETIMEDOUT;
  2565. } else {
  2566. d_vpr_h("%s: system init wait completed\n", __func__);
  2567. rc = 0;
  2568. }
  2569. unlock:
  2570. core_unlock(core, __func__);
  2571. if (rc)
  2572. msm_vidc_core_deinit(core, true);
  2573. return rc;
  2574. }
  2575. int msm_vidc_core_timeout(struct msm_vidc_core *core)
  2576. {
  2577. return msm_vidc_core_deinit(core, true);
  2578. }
  2579. int msm_vidc_print_inst_info(struct msm_vidc_inst *inst)
  2580. {
  2581. struct msm_vidc_buffers *buffers;
  2582. struct msm_vidc_buffer *buf;
  2583. enum msm_vidc_port_type port;
  2584. bool is_secure, is_decode;
  2585. u32 bit_depth, bit_rate, frame_rate, width, height;
  2586. struct dma_buf *dbuf;
  2587. int i = 0;
  2588. struct msm_vidc_buf_details buffer_details[] = {
  2589. {MSM_VIDC_BUF_INPUT, "INPUT" },
  2590. {MSM_VIDC_BUF_OUTPUT, "OUTPUT" },
  2591. {MSM_VIDC_BUF_INPUT_META, "IN_META" },
  2592. {MSM_VIDC_BUF_OUTPUT_META, "OUT_META" },
  2593. {MSM_VIDC_BUF_BIN, "BIN" },
  2594. {MSM_VIDC_BUF_ARP, "ARP" },
  2595. {MSM_VIDC_BUF_COMV, "COMV" },
  2596. {MSM_VIDC_BUF_NON_COMV, "NON_COMV" },
  2597. {MSM_VIDC_BUF_LINE, "LINE" },
  2598. {MSM_VIDC_BUF_PERSIST, "PERSIST" },
  2599. {MSM_VIDC_BUF_VPSS, "VPSS" },
  2600. };
  2601. if (!inst || !inst->capabilities) {
  2602. i_vpr_e(inst, "%s: invalid params\n", __func__);
  2603. return -EINVAL;
  2604. }
  2605. is_secure = is_secure_session(inst);
  2606. is_decode = inst->domain == MSM_VIDC_DECODER;
  2607. port = is_decode ? INPUT_PORT : OUTPUT_PORT;
  2608. width = inst->fmts[port].fmt.pix_mp.width;
  2609. height = inst->fmts[port].fmt.pix_mp.height;
  2610. bit_depth = inst->capabilities->cap[BIT_DEPTH].value & 0xFFFF;
  2611. bit_rate = inst->capabilities->cap[BIT_RATE].value;
  2612. frame_rate = inst->capabilities->cap[FRAME_RATE].value >> 16;
  2613. i_vpr_e(inst, "%s %s session, HxW: %d x %d, fps: %d, bitrate: %d, bit-depth: %d\n",
  2614. is_secure ? "Secure" : "Non-Secure",
  2615. is_decode ? "Decode" : "Encode",
  2616. height, width,
  2617. frame_rate, bit_rate, bit_depth);
  2618. /* Print buffer details */
  2619. for (i = 0; i < ARRAY_SIZE(buffer_details); i++) {
  2620. buffers = msm_vidc_get_buffers(inst, buffer_details[i].type, __func__);
  2621. if (!buffers)
  2622. continue;
  2623. i_vpr_e(inst, "count: type: %8s, min: %2d, extra: %2d, actual: %2d\n",
  2624. buffer_details[i].name, buffers->min_count,
  2625. buffers->extra_count, buffers->actual_count);
  2626. list_for_each_entry(buf, &buffers->list, list) {
  2627. if (!buf->valid || !buf->dmabuf)
  2628. continue;
  2629. dbuf = (struct dma_buf *)buf->dmabuf;
  2630. i_vpr_e(inst,
  2631. "buf: type: %8s, index: %2d, fd: %4d, size: %9u, off: %8u, filled: %9u, iova: %8x, inode: %9ld, flags: %8x, ts: %16lld, attr: %8x\n",
  2632. buffer_details[i].name, buf->index, buf->fd, buf->buffer_size,
  2633. buf->data_offset, buf->data_size, buf->device_addr,
  2634. file_inode(dbuf->file)->i_ino,
  2635. buf->flags, buf->timestamp, buf->attr);
  2636. }
  2637. }
  2638. return 0;
  2639. }
  2640. void msm_vidc_smmu_fault_work_handler(struct work_struct *work)
  2641. {
  2642. struct msm_vidc_core *core;
  2643. struct msm_vidc_inst *inst = NULL;
  2644. struct msm_vidc_inst *instances[MAX_SUPPORTED_INSTANCES];
  2645. s32 num_instances = 0;
  2646. core = container_of(work, struct msm_vidc_core, smmu_fault_work);
  2647. if (!core) {
  2648. d_vpr_e("%s: invalid params\n", __func__);
  2649. return;
  2650. }
  2651. core_lock(core, __func__);
  2652. list_for_each_entry(inst, &core->instances, list)
  2653. instances[num_instances++] = inst;
  2654. core_unlock(core, __func__);
  2655. while (num_instances--) {
  2656. inst = instances[num_instances];
  2657. inst = get_inst_ref(core, inst);
  2658. if (!inst)
  2659. continue;
  2660. inst_lock(inst, __func__);
  2661. msm_vidc_print_inst_info(inst);
  2662. inst_unlock(inst, __func__);
  2663. put_inst(inst);
  2664. }
  2665. }
  2666. int msm_vidc_smmu_fault_handler(struct iommu_domain *domain,
  2667. struct device *dev, unsigned long iova, int flags, void *data)
  2668. {
  2669. struct msm_vidc_core *core = data;
  2670. if (!domain || !core || !core->capabilities) {
  2671. d_vpr_e("%s: invalid params %pK %pK\n",
  2672. __func__, domain, core);
  2673. return -EINVAL;
  2674. }
  2675. if (core->smmu_fault_handled) {
  2676. if (core->capabilities[NON_FATAL_FAULTS].value) {
  2677. dprintk_ratelimit(VIDC_ERR, "err ",
  2678. "%s: non-fatal pagefault address: %lx\n",
  2679. __func__, iova);
  2680. return 0;
  2681. }
  2682. }
  2683. d_vpr_e("%s: faulting address: %lx\n", __func__, iova);
  2684. core->smmu_fault_handled = true;
  2685. /**
  2686. * Fault handler shouldn't be blocked for longtime. So offload work
  2687. * to device_workq to print buffer and memory consumption details.
  2688. */
  2689. queue_work(core->device_workq, &core->smmu_fault_work);
  2690. /*
  2691. * Return -ENOSYS to elicit the default behaviour of smmu driver.
  2692. * If we return -ENOSYS, then smmu driver assumes page fault handler
  2693. * is not installed and prints a list of useful debug information like
  2694. * FAR, SID etc. This information is not printed if we return 0.
  2695. */
  2696. return -ENOSYS;
  2697. }
  2698. int msm_vidc_trigger_ssr(struct msm_vidc_core *core,
  2699. enum msm_vidc_ssr_trigger_type type)
  2700. {
  2701. return 0;
  2702. }
  2703. void msm_vidc_ssr_handler(struct work_struct *work)
  2704. {
  2705. }
  2706. void msm_vidc_pm_work_handler(struct work_struct *work)
  2707. {
  2708. }
  2709. void msm_vidc_fw_unload_handler(struct work_struct *work)
  2710. {
  2711. struct msm_vidc_core *core = NULL;
  2712. int rc = 0;
  2713. core = container_of(work, struct msm_vidc_core, fw_unload_work.work);
  2714. if (!core) {
  2715. d_vpr_e("%s: invalid work or core handle\n", __func__);
  2716. return;
  2717. }
  2718. d_vpr_h("%s: deinitializing video core\n",__func__);
  2719. rc = msm_vidc_core_deinit(core, false);
  2720. if (rc)
  2721. d_vpr_e("%s: Failed to deinit core\n", __func__);
  2722. }
  2723. void msm_vidc_batch_handler(struct work_struct *work)
  2724. {
  2725. }
  2726. int msm_vidc_flush_buffers(struct msm_vidc_inst* inst,
  2727. enum msm_vidc_buffer_type type)
  2728. {
  2729. int rc = 0;
  2730. struct msm_vidc_buffers *buffers;
  2731. struct msm_vidc_buffer *buf, *dummy;
  2732. enum msm_vidc_buffer_type buffer_type[2];
  2733. int i;
  2734. if (!inst) {
  2735. d_vpr_e("%s: invalid params\n", __func__);
  2736. return -EINVAL;
  2737. }
  2738. if (type == MSM_VIDC_BUF_INPUT) {
  2739. buffer_type[0] = MSM_VIDC_BUF_INPUT_META;
  2740. buffer_type[1] = MSM_VIDC_BUF_INPUT;
  2741. } else if (type == MSM_VIDC_BUF_OUTPUT) {
  2742. buffer_type[0] = MSM_VIDC_BUF_OUTPUT_META;
  2743. buffer_type[1] = MSM_VIDC_BUF_OUTPUT;
  2744. } else {
  2745. i_vpr_h(inst, "%s: invalid buffer type %d\n",
  2746. __func__, type);
  2747. return -EINVAL;
  2748. }
  2749. for (i = 0; i < ARRAY_SIZE(buffer_type); i++) {
  2750. buffers = msm_vidc_get_buffers(inst, buffer_type[i], __func__);
  2751. if (!buffers)
  2752. return -EINVAL;
  2753. list_for_each_entry_safe(buf, dummy, &buffers->list, list) {
  2754. if (buf->attr & MSM_VIDC_ATTR_QUEUED ||
  2755. buf->attr & MSM_VIDC_ATTR_DEFERRED) {
  2756. print_vidc_buffer(VIDC_ERR, "err ", "flushing buffer", inst, buf);
  2757. msm_vidc_vb2_buffer_done(inst, buf);
  2758. msm_vidc_put_driver_buf(inst, buf);
  2759. }
  2760. }
  2761. }
  2762. return rc;
  2763. }
  2764. void msm_vidc_destroy_buffers(struct msm_vidc_inst *inst)
  2765. {
  2766. struct msm_vidc_buffers *buffers;
  2767. struct msm_vidc_buffer *buf, *dummy;
  2768. enum msm_vidc_buffer_type buf_types[] = {
  2769. MSM_VIDC_BUF_INPUT,
  2770. MSM_VIDC_BUF_OUTPUT,
  2771. MSM_VIDC_BUF_INPUT_META,
  2772. MSM_VIDC_BUF_OUTPUT_META,
  2773. MSM_VIDC_BUF_BIN,
  2774. MSM_VIDC_BUF_ARP,
  2775. MSM_VIDC_BUF_COMV,
  2776. MSM_VIDC_BUF_NON_COMV,
  2777. MSM_VIDC_BUF_LINE,
  2778. MSM_VIDC_BUF_DPB,
  2779. MSM_VIDC_BUF_PERSIST,
  2780. MSM_VIDC_BUF_VPSS,
  2781. };
  2782. int i;
  2783. if (!inst) {
  2784. d_vpr_e("%s: invalid params\n", __func__);
  2785. return;
  2786. }
  2787. for (i = 0; i < ARRAY_SIZE(buf_types); i++) {
  2788. buffers = msm_vidc_get_buffers(inst, buf_types[i], __func__);
  2789. if (!buffers)
  2790. continue;
  2791. list_for_each_entry_safe(buf, dummy, &buffers->list, list) {
  2792. i_vpr_h(inst,
  2793. "destroying buffer: type %d idx %d fd %d addr %#x size %d\n",
  2794. buf->type, buf->index, buf->fd, buf->device_addr, buf->buffer_size);
  2795. if (is_internal_buffer(buf->type))
  2796. msm_vidc_destroy_internal_buffer(inst, buf);
  2797. else
  2798. msm_vidc_put_driver_buf(inst, buf);
  2799. }
  2800. }
  2801. }
  2802. static void msm_vidc_close_helper(struct kref *kref)
  2803. {
  2804. struct msm_vidc_inst *inst = container_of(kref,
  2805. struct msm_vidc_inst, kref);
  2806. i_vpr_h(inst, "%s()\n", __func__);
  2807. msm_vidc_event_queue_deinit(inst);
  2808. msm_vidc_vb2_queue_deinit(inst);
  2809. msm_vidc_debugfs_deinit_inst(inst);
  2810. if (is_decode_session(inst))
  2811. msm_vdec_inst_deinit(inst);
  2812. else if (is_encode_session(inst))
  2813. msm_venc_inst_deinit(inst);
  2814. kfree(inst->capabilities);
  2815. if (inst->response_workq)
  2816. destroy_workqueue(inst->response_workq);
  2817. kfree(inst);
  2818. }
  2819. struct msm_vidc_inst *get_inst_ref(struct msm_vidc_core *core,
  2820. struct msm_vidc_inst *instance)
  2821. {
  2822. struct msm_vidc_inst *inst = NULL;
  2823. bool matches = false;
  2824. if (!core) {
  2825. d_vpr_e("%s: invalid params\n", __func__);
  2826. return NULL;
  2827. }
  2828. mutex_lock(&core->lock);
  2829. list_for_each_entry(inst, &core->instances, list) {
  2830. if (inst == instance) {
  2831. matches = true;
  2832. break;
  2833. }
  2834. }
  2835. inst = (matches && kref_get_unless_zero(&inst->kref)) ? inst : NULL;
  2836. mutex_unlock(&core->lock);
  2837. return inst;
  2838. }
  2839. struct msm_vidc_inst *get_inst(struct msm_vidc_core *core,
  2840. u32 session_id)
  2841. {
  2842. struct msm_vidc_inst *inst = NULL;
  2843. bool matches = false;
  2844. if (!core) {
  2845. d_vpr_e("%s: invalid params\n", __func__);
  2846. return NULL;
  2847. }
  2848. mutex_lock(&core->lock);
  2849. list_for_each_entry(inst, &core->instances, list) {
  2850. if (inst->session_id == session_id) {
  2851. matches = true;
  2852. break;
  2853. }
  2854. }
  2855. inst = (matches && kref_get_unless_zero(&inst->kref)) ? inst : NULL;
  2856. mutex_unlock(&core->lock);
  2857. return inst;
  2858. }
  2859. void put_inst(struct msm_vidc_inst *inst)
  2860. {
  2861. if (!inst) {
  2862. d_vpr_e("%s: invalid params\n", __func__);
  2863. return;
  2864. }
  2865. kref_put(&inst->kref, msm_vidc_close_helper);
  2866. }
  2867. bool core_lock_check(struct msm_vidc_core *core, const char* func)
  2868. {
  2869. return mutex_is_locked(&core->lock);
  2870. }
  2871. void core_lock(struct msm_vidc_core *core, const char *function)
  2872. {
  2873. mutex_lock(&core->lock);
  2874. }
  2875. void core_unlock(struct msm_vidc_core *core, const char *function)
  2876. {
  2877. mutex_unlock(&core->lock);
  2878. }
  2879. bool inst_lock_check(struct msm_vidc_inst *inst, const char* func)
  2880. {
  2881. return mutex_is_locked(&inst->lock);
  2882. }
  2883. void inst_lock(struct msm_vidc_inst *inst, const char *function)
  2884. {
  2885. mutex_lock(&inst->lock);
  2886. }
  2887. void inst_unlock(struct msm_vidc_inst *inst, const char *function)
  2888. {
  2889. mutex_unlock(&inst->lock);
  2890. }
  2891. int msm_vidc_update_meta_port_settings(struct msm_vidc_inst *inst)
  2892. {
  2893. struct msm_vidc_core *core;
  2894. struct v4l2_format *fmt;
  2895. if (!inst || !inst->core) {
  2896. d_vpr_e("%s: invalid params\n", __func__);
  2897. return -EINVAL;
  2898. }
  2899. core = inst->core;
  2900. fmt = &inst->fmts[INPUT_META_PORT];
  2901. if (is_input_meta_enabled(inst)) {
  2902. fmt->fmt.meta.buffersize = call_session_op(core,
  2903. buffer_size, inst, MSM_VIDC_BUF_INPUT_META);
  2904. inst->buffers.input_meta.min_count =
  2905. inst->buffers.input.min_count;
  2906. inst->buffers.input_meta.extra_count =
  2907. inst->buffers.input.extra_count;
  2908. inst->buffers.input_meta.actual_count =
  2909. inst->buffers.input.actual_count;
  2910. inst->buffers.input_meta.size = fmt->fmt.meta.buffersize;
  2911. } else {
  2912. fmt->fmt.meta.buffersize = 0;
  2913. inst->buffers.input_meta.min_count = 0;
  2914. inst->buffers.input_meta.extra_count = 0;
  2915. inst->buffers.input_meta.actual_count = 0;
  2916. inst->buffers.input_meta.size = 0;
  2917. }
  2918. fmt = &inst->fmts[OUTPUT_META_PORT];
  2919. if (is_output_meta_enabled(inst)) {
  2920. fmt->fmt.meta.buffersize = call_session_op(core,
  2921. buffer_size, inst, MSM_VIDC_BUF_OUTPUT_META);
  2922. inst->buffers.output_meta.min_count =
  2923. inst->buffers.output.min_count;
  2924. inst->buffers.output_meta.extra_count =
  2925. inst->buffers.output.extra_count;
  2926. inst->buffers.output_meta.actual_count =
  2927. inst->buffers.output.actual_count;
  2928. inst->buffers.output_meta.size = fmt->fmt.meta.buffersize;
  2929. } else {
  2930. fmt->fmt.meta.buffersize = 0;
  2931. inst->buffers.output_meta.min_count = 0;
  2932. inst->buffers.output_meta.extra_count = 0;
  2933. inst->buffers.output_meta.actual_count = 0;
  2934. inst->buffers.output_meta.size = 0;
  2935. }
  2936. return 0;
  2937. }
  2938. void msm_vidc_schedule_core_deinit(struct msm_vidc_core *core)
  2939. {
  2940. if (!core)
  2941. return;
  2942. if (!core->capabilities[FW_UNLOAD].value)
  2943. return;
  2944. cancel_delayed_work(&core->fw_unload_work);
  2945. schedule_delayed_work(&core->fw_unload_work,
  2946. msecs_to_jiffies(core->capabilities[FW_UNLOAD_DELAY].value));
  2947. d_vpr_h("firmware unload delayed by %u ms\n",
  2948. core->capabilities[FW_UNLOAD_DELAY].value);
  2949. return;
  2950. }
  2951. static const char *get_codec_str(enum msm_vidc_codec_type type)
  2952. {
  2953. switch (type) {
  2954. case MSM_VIDC_H264: return "h264";
  2955. case MSM_VIDC_HEVC: return "h265";
  2956. case MSM_VIDC_VP9: return " vp9";
  2957. }
  2958. return "....";
  2959. }
  2960. static const char *get_domain_str(enum msm_vidc_domain_type type)
  2961. {
  2962. switch (type) {
  2963. case MSM_VIDC_ENCODER: return "e";
  2964. case MSM_VIDC_DECODER: return "d";
  2965. }
  2966. return ".";
  2967. }
  2968. int msm_vidc_update_debug_str(struct msm_vidc_inst *inst)
  2969. {
  2970. u32 sid;
  2971. const char *codec;
  2972. const char *domain;
  2973. if (!inst) {
  2974. d_vpr_e("%s: Invalid params\n", __func__);
  2975. return -EINVAL;
  2976. }
  2977. sid = inst->session_id;
  2978. codec = get_codec_str(inst->codec);
  2979. domain = get_domain_str(inst->domain);
  2980. snprintf(inst->debug_str, sizeof(inst->debug_str), "%08x: %s%s", sid, codec, domain);
  2981. d_vpr_h("%s: sid: %08x, codec: %s, domain: %s, final: %s\n",
  2982. __func__, sid, codec, domain, inst->debug_str);
  2983. return 0;
  2984. }