2
0

950-0834-media-platform-Add-Raspberry-Pi-HEVC-decoder-driver.patch 127 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733
  1. From 038efbab4ef95fd22ee6b25027cb6cf25248ea3d Mon Sep 17 00:00:00 2001
  2. From: John Cox <[email protected]>
  3. Date: Thu, 6 Feb 2025 18:02:37 +0000
  4. Subject: [PATCH] media: platform: Add Raspberry Pi HEVC decoder driver
  5. The BCM2711 and BCM2712 SoCs used on Rapsberry Pi 4 and Raspberry
  6. Pi 5 boards include an HEVC decoder block. Add a driver for it.
  7. Signed-off-by: John Cox <[email protected]>
  8. Signed-off-by: Dave Stevenson <[email protected]>
  9. ---
  10. MAINTAINERS | 10 +
  11. drivers/media/platform/raspberrypi/Kconfig | 1 +
  12. drivers/media/platform/raspberrypi/Makefile | 1 +
  13. .../platform/raspberrypi/hevc_dec/Kconfig | 17 +
  14. .../platform/raspberrypi/hevc_dec/Makefile | 5 +
  15. .../platform/raspberrypi/hevc_dec/hevc_d.c | 450 +++
  16. .../platform/raspberrypi/hevc_dec/hevc_d.h | 189 ++
  17. .../raspberrypi/hevc_dec/hevc_d_h265.c | 2542 +++++++++++++++++
  18. .../raspberrypi/hevc_dec/hevc_d_h265.h | 23 +
  19. .../platform/raspberrypi/hevc_dec/hevc_d_hw.c | 376 +++
  20. .../platform/raspberrypi/hevc_dec/hevc_d_hw.h | 303 ++
  21. .../raspberrypi/hevc_dec/hevc_d_video.c | 688 +++++
  22. .../raspberrypi/hevc_dec/hevc_d_video.h | 38 +
  23. 13 files changed, 4643 insertions(+)
  24. create mode 100644 drivers/media/platform/raspberrypi/hevc_dec/Kconfig
  25. create mode 100644 drivers/media/platform/raspberrypi/hevc_dec/Makefile
  26. create mode 100644 drivers/media/platform/raspberrypi/hevc_dec/hevc_d.c
  27. create mode 100644 drivers/media/platform/raspberrypi/hevc_dec/hevc_d.h
  28. create mode 100644 drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c
  29. create mode 100644 drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.h
  30. create mode 100644 drivers/media/platform/raspberrypi/hevc_dec/hevc_d_hw.c
  31. create mode 100644 drivers/media/platform/raspberrypi/hevc_dec/hevc_d_hw.h
  32. create mode 100644 drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c
  33. create mode 100644 drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.h
  34. --- a/MAINTAINERS
  35. +++ b/MAINTAINERS
  36. @@ -19367,6 +19367,16 @@ S: Maintained
  37. F: Documentation/devicetree/bindings/spi/raspberrypi,rp2040-gpio-bridge.yaml
  38. F: drivers/spi/spi-rp2040-gpio-bridge.c
  39. +RASPBERRY PI HEVC DECODER
  40. +M: John Cox <[email protected]>
  41. +M: Dom Cobley <[email protected]>
  42. +M: Dave Stevenson <[email protected]>
  43. +M: Raspberry Pi Internal Kernel List <[email protected]>
  44. +L: [email protected]
  45. +S: Maintained
  46. +F: Documentation/devicetree/bindings/media/raspberrypi,rpi_hevc_dec.yaml
  47. +F: drivers/media/platform/raspberrypi/hevc_dec
  48. +
  49. RASPBERRY PI PISP BACK END
  50. M: Jacopo Mondi <[email protected]>
  51. L: Raspberry Pi Kernel Maintenance <[email protected]>
  52. --- a/drivers/media/platform/raspberrypi/Kconfig
  53. +++ b/drivers/media/platform/raspberrypi/Kconfig
  54. @@ -2,5 +2,6 @@
  55. comment "Raspberry Pi media platform drivers"
  56. +source "drivers/media/platform/raspberrypi/hevc_dec/Kconfig"
  57. source "drivers/media/platform/raspberrypi/pisp_be/Kconfig"
  58. source "drivers/media/platform/raspberrypi/rp1_cfe/Kconfig"
  59. --- a/drivers/media/platform/raspberrypi/Makefile
  60. +++ b/drivers/media/platform/raspberrypi/Makefile
  61. @@ -1,4 +1,5 @@
  62. # SPDX-License-Identifier: GPL-2.0
  63. +obj-y += hevc_dec/
  64. obj-y += pisp_be/
  65. obj-y += rp1_cfe/
  66. --- /dev/null
  67. +++ b/drivers/media/platform/raspberrypi/hevc_dec/Kconfig
  68. @@ -0,0 +1,17 @@
  69. +# SPDX-License-Identifier: GPL-2.0
  70. +
  71. +config VIDEO_RPI_HEVC_DEC
  72. + tristate "Rasperry Pi HEVC decoder"
  73. + depends on VIDEO_DEV && VIDEO_DEV
  74. + depends on OF
  75. + select MEDIA_CONTROLLER
  76. + select MEDIA_CONTROLLER_REQUEST_API
  77. + select VIDEOBUF2_DMA_CONTIG
  78. + select V4L2_MEM2MEM_DEV
  79. + help
  80. + Support for the Raspberry Pi HEVC / H265 H/W decoder as a stateless
  81. + V4L2 decoder device.
  82. +
  83. + To compile this driver as a module, choose M here: the module
  84. + will be called rpi-hevc-dec.
  85. +
  86. --- /dev/null
  87. +++ b/drivers/media/platform/raspberrypi/hevc_dec/Makefile
  88. @@ -0,0 +1,5 @@
  89. +# SPDX-License-Identifier: GPL-2.0
  90. +obj-$(CONFIG_VIDEO_RPI_HEVC_DEC) += rpi-hevc-dec.o
  91. +
  92. +rpi-hevc-dec-y = hevc_d.o hevc_d_video.o hevc_d_hw.o\
  93. + hevc_d_h265.o
  94. --- /dev/null
  95. +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d.c
  96. @@ -0,0 +1,450 @@
  97. +// SPDX-License-Identifier: GPL-2.0
  98. +/*
  99. + * Raspberry Pi HEVC driver
  100. + *
  101. + * Copyright (C) 2024 Raspberry Pi Ltd
  102. + *
  103. + * Based on the Cedrus VPU driver, that is:
  104. + *
  105. + * Copyright (C) 2016 Florent Revest <[email protected]>
  106. + * Copyright (C) 2018 Paul Kocialkowski <[email protected]>
  107. + * Copyright (C) 2018 Bootlin
  108. + */
  109. +
  110. +#include <linux/platform_device.h>
  111. +#include <linux/module.h>
  112. +#include <linux/of.h>
  113. +
  114. +#include <media/v4l2-device.h>
  115. +#include <media/v4l2-ioctl.h>
  116. +#include <media/v4l2-ctrls.h>
  117. +#include <media/v4l2-mem2mem.h>
  118. +
  119. +#include "hevc_d.h"
  120. +#include "hevc_d_h265.h"
  121. +#include "hevc_d_video.h"
  122. +#include "hevc_d_hw.h"
  123. +
  124. +static const struct hevc_d_control hevc_d_ctrls[] = {
  125. + {
  126. + .cfg = {
  127. + .id = V4L2_CID_STATELESS_HEVC_SPS,
  128. + .ops = &hevc_d_hevc_sps_ctrl_ops,
  129. + },
  130. + .required = false,
  131. + }, {
  132. + .cfg = {
  133. + .id = V4L2_CID_STATELESS_HEVC_PPS,
  134. + .ops = &hevc_d_hevc_pps_ctrl_ops,
  135. + },
  136. + .required = false,
  137. + }, {
  138. + .cfg = {
  139. + .id = V4L2_CID_STATELESS_HEVC_SCALING_MATRIX,
  140. + },
  141. + .required = false,
  142. + }, {
  143. + .cfg = {
  144. + .id = V4L2_CID_STATELESS_HEVC_DECODE_PARAMS,
  145. + },
  146. + .required = true,
  147. + }, {
  148. + .cfg = {
  149. + .name = "Slice param array",
  150. + .id = V4L2_CID_STATELESS_HEVC_SLICE_PARAMS,
  151. + .type = V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS,
  152. + .flags = V4L2_CTRL_FLAG_DYNAMIC_ARRAY,
  153. + .dims = { 0x1000 },
  154. + },
  155. + .required = true,
  156. + }, {
  157. + .cfg = {
  158. + .id = V4L2_CID_STATELESS_HEVC_DECODE_MODE,
  159. + .min = V4L2_STATELESS_HEVC_DECODE_MODE_FRAME_BASED,
  160. + .max = V4L2_STATELESS_HEVC_DECODE_MODE_FRAME_BASED,
  161. + .def = V4L2_STATELESS_HEVC_DECODE_MODE_FRAME_BASED,
  162. + },
  163. + .required = false,
  164. + }, {
  165. + .cfg = {
  166. + .id = V4L2_CID_STATELESS_HEVC_START_CODE,
  167. + .min = V4L2_STATELESS_HEVC_START_CODE_NONE,
  168. + .max = V4L2_STATELESS_HEVC_START_CODE_ANNEX_B,
  169. + .def = V4L2_STATELESS_HEVC_START_CODE_NONE,
  170. + },
  171. + .required = false,
  172. + },
  173. +};
  174. +
  175. +#define HEVC_D_CTRLS_COUNT ARRAY_SIZE(hevc_d_ctrls)
  176. +
  177. +struct v4l2_ctrl *hevc_d_find_ctrl(struct hevc_d_ctx *ctx, u32 id)
  178. +{
  179. + unsigned int i;
  180. +
  181. + for (i = 0; i < HEVC_D_CTRLS_COUNT; i++)
  182. + if (ctx->ctrls[i]->id == id)
  183. + return ctx->ctrls[i];
  184. +
  185. + return NULL;
  186. +}
  187. +
  188. +void *hevc_d_find_control_data(struct hevc_d_ctx *ctx, u32 id)
  189. +{
  190. + struct v4l2_ctrl *const ctrl = hevc_d_find_ctrl(ctx, id);
  191. +
  192. + return !ctrl ? NULL : ctrl->p_cur.p;
  193. +}
  194. +
  195. +static int hevc_d_init_ctrls(struct hevc_d_dev *dev, struct hevc_d_ctx *ctx)
  196. +{
  197. + struct v4l2_ctrl_handler *hdl = &ctx->hdl;
  198. + struct v4l2_ctrl *ctrl;
  199. + unsigned int i;
  200. +
  201. + v4l2_ctrl_handler_init(hdl, HEVC_D_CTRLS_COUNT);
  202. + if (hdl->error) {
  203. + v4l2_err(&dev->v4l2_dev,
  204. + "Failed to initialize control handler\n");
  205. + return hdl->error;
  206. + }
  207. +
  208. + ctx->ctrls = kzalloc(HEVC_D_CTRLS_COUNT * sizeof(ctrl), GFP_KERNEL);
  209. + if (!ctx->ctrls)
  210. + return -ENOMEM;
  211. +
  212. + for (i = 0; i < HEVC_D_CTRLS_COUNT; i++) {
  213. + ctrl = v4l2_ctrl_new_custom(hdl, &hevc_d_ctrls[i].cfg,
  214. + ctx);
  215. + if (hdl->error) {
  216. + v4l2_err(&dev->v4l2_dev,
  217. + "Failed to create new custom control id=%#x\n",
  218. + hevc_d_ctrls[i].cfg.id);
  219. +
  220. + v4l2_ctrl_handler_free(hdl);
  221. + kfree(ctx->ctrls);
  222. + return hdl->error;
  223. + }
  224. +
  225. + ctx->ctrls[i] = ctrl;
  226. + }
  227. +
  228. + ctx->fh.ctrl_handler = hdl;
  229. + v4l2_ctrl_handler_setup(hdl);
  230. +
  231. + return 0;
  232. +}
  233. +
  234. +static int hevc_d_request_validate(struct media_request *req)
  235. +{
  236. + struct media_request_object *obj;
  237. + struct v4l2_ctrl_handler *parent_hdl, *hdl;
  238. + struct hevc_d_ctx *ctx = NULL;
  239. + struct v4l2_ctrl *ctrl_test;
  240. + unsigned int count;
  241. + unsigned int i;
  242. +
  243. + list_for_each_entry(obj, &req->objects, list) {
  244. + struct vb2_buffer *vb;
  245. +
  246. + if (vb2_request_object_is_buffer(obj)) {
  247. + vb = container_of(obj, struct vb2_buffer, req_obj);
  248. + ctx = vb2_get_drv_priv(vb->vb2_queue);
  249. +
  250. + break;
  251. + }
  252. + }
  253. +
  254. + if (!ctx)
  255. + return -ENOENT;
  256. +
  257. + count = vb2_request_buffer_cnt(req);
  258. + if (!count) {
  259. + v4l2_info(&ctx->dev->v4l2_dev,
  260. + "No buffer was provided with the request\n");
  261. + return -ENOENT;
  262. + } else if (count > 1) {
  263. + v4l2_info(&ctx->dev->v4l2_dev,
  264. + "More than one buffer was provided with the request\n");
  265. + return -EINVAL;
  266. + }
  267. +
  268. + parent_hdl = &ctx->hdl;
  269. +
  270. + hdl = v4l2_ctrl_request_hdl_find(req, parent_hdl);
  271. + if (!hdl) {
  272. + v4l2_info(&ctx->dev->v4l2_dev, "Missing codec control(s)\n");
  273. + return -ENOENT;
  274. + }
  275. +
  276. + for (i = 0; i < HEVC_D_CTRLS_COUNT; i++) {
  277. + if (!hevc_d_ctrls[i].required)
  278. + continue;
  279. +
  280. + ctrl_test =
  281. + v4l2_ctrl_request_hdl_ctrl_find(hdl,
  282. + hevc_d_ctrls[i].cfg.id);
  283. + if (!ctrl_test) {
  284. + v4l2_info(&ctx->dev->v4l2_dev,
  285. + "Missing required codec control %d: id=%#x\n",
  286. + i, hevc_d_ctrls[i].cfg.id);
  287. + v4l2_ctrl_request_hdl_put(hdl);
  288. + return -ENOENT;
  289. + }
  290. + }
  291. +
  292. + v4l2_ctrl_request_hdl_put(hdl);
  293. +
  294. + return vb2_request_validate(req);
  295. +}
  296. +
  297. +static int hevc_d_open(struct file *file)
  298. +{
  299. + struct hevc_d_dev *dev = video_drvdata(file);
  300. + struct hevc_d_ctx *ctx = NULL;
  301. + int ret;
  302. +
  303. + if (mutex_lock_interruptible(&dev->dev_mutex))
  304. + return -ERESTARTSYS;
  305. +
  306. + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  307. + if (!ctx) {
  308. + mutex_unlock(&dev->dev_mutex);
  309. + ret = -ENOMEM;
  310. + goto err_unlock;
  311. + }
  312. +
  313. + mutex_init(&ctx->ctx_mutex);
  314. +
  315. + v4l2_fh_init(&ctx->fh, video_devdata(file));
  316. + file->private_data = &ctx->fh;
  317. + ctx->dev = dev;
  318. +
  319. + ret = hevc_d_init_ctrls(dev, ctx);
  320. + if (ret)
  321. + goto err_free;
  322. +
  323. + ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
  324. + &hevc_d_queue_init);
  325. + if (IS_ERR(ctx->fh.m2m_ctx)) {
  326. + ret = PTR_ERR(ctx->fh.m2m_ctx);
  327. + goto err_ctrls;
  328. + }
  329. +
  330. + /* The only bit of format info that we can guess now is H265 src
  331. + * Everything else we need more info for
  332. + */
  333. + hevc_d_prepare_src_format(&ctx->src_fmt);
  334. +
  335. + v4l2_fh_add(&ctx->fh);
  336. +
  337. + mutex_unlock(&dev->dev_mutex);
  338. +
  339. + return 0;
  340. +
  341. +err_ctrls:
  342. + v4l2_ctrl_handler_free(&ctx->hdl);
  343. + kfree(ctx->ctrls);
  344. +err_free:
  345. + mutex_destroy(&ctx->ctx_mutex);
  346. + kfree(ctx);
  347. +err_unlock:
  348. + mutex_unlock(&dev->dev_mutex);
  349. +
  350. + return ret;
  351. +}
  352. +
  353. +static int hevc_d_release(struct file *file)
  354. +{
  355. + struct hevc_d_dev *dev = video_drvdata(file);
  356. + struct hevc_d_ctx *ctx = container_of(file->private_data,
  357. + struct hevc_d_ctx, fh);
  358. +
  359. + mutex_lock(&dev->dev_mutex);
  360. +
  361. + v4l2_fh_del(&ctx->fh);
  362. + v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  363. +
  364. + v4l2_ctrl_handler_free(&ctx->hdl);
  365. + kfree(ctx->ctrls);
  366. +
  367. + v4l2_fh_exit(&ctx->fh);
  368. + mutex_destroy(&ctx->ctx_mutex);
  369. +
  370. + kfree(ctx);
  371. +
  372. + mutex_unlock(&dev->dev_mutex);
  373. +
  374. + return 0;
  375. +}
  376. +
  377. +static void hevc_d_media_req_queue(struct media_request *req)
  378. +{
  379. + media_request_mark_manual_completion(req);
  380. + v4l2_m2m_request_queue(req);
  381. +}
  382. +
  383. +static const struct v4l2_file_operations hevc_d_fops = {
  384. + .owner = THIS_MODULE,
  385. + .open = hevc_d_open,
  386. + .release = hevc_d_release,
  387. + .poll = v4l2_m2m_fop_poll,
  388. + .unlocked_ioctl = video_ioctl2,
  389. + .mmap = v4l2_m2m_fop_mmap,
  390. +};
  391. +
  392. +static const struct video_device hevc_d_video_device = {
  393. + .name = HEVC_D_NAME,
  394. + .vfl_dir = VFL_DIR_M2M,
  395. + .fops = &hevc_d_fops,
  396. + .ioctl_ops = &hevc_d_ioctl_ops,
  397. + .minor = -1,
  398. + .release = video_device_release_empty,
  399. + .device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING,
  400. +};
  401. +
  402. +static const struct v4l2_m2m_ops hevc_d_m2m_ops = {
  403. + .device_run = hevc_d_device_run,
  404. +};
  405. +
  406. +static const struct media_device_ops hevc_d_m2m_media_ops = {
  407. + .req_validate = hevc_d_request_validate,
  408. + .req_queue = hevc_d_media_req_queue,
  409. +};
  410. +
  411. +static int hevc_d_probe(struct platform_device *pdev)
  412. +{
  413. + struct hevc_d_dev *dev;
  414. + struct video_device *vfd;
  415. + int ret;
  416. +
  417. + dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  418. + if (!dev)
  419. + return -ENOMEM;
  420. +
  421. + dev->vfd = hevc_d_video_device;
  422. + dev->dev = &pdev->dev;
  423. + dev->pdev = pdev;
  424. +
  425. + ret = 0;
  426. + ret = hevc_d_hw_probe(dev);
  427. + if (ret) {
  428. + dev_err(&pdev->dev, "Failed to probe hardware - %d\n", ret);
  429. + return ret;
  430. + }
  431. +
  432. + mutex_init(&dev->dev_mutex);
  433. +
  434. + ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  435. + if (ret) {
  436. + dev_err(&pdev->dev, "Failed to register V4L2 device\n");
  437. + return ret;
  438. + }
  439. +
  440. + vfd = &dev->vfd;
  441. + vfd->lock = &dev->dev_mutex;
  442. + vfd->v4l2_dev = &dev->v4l2_dev;
  443. +
  444. + snprintf(vfd->name, sizeof(vfd->name), "%s", hevc_d_video_device.name);
  445. + video_set_drvdata(vfd, dev);
  446. +
  447. + ret = dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(36));
  448. + if (ret) {
  449. + v4l2_err(&dev->v4l2_dev,
  450. + "Failed dma_set_mask_and_coherent\n");
  451. + goto err_v4l2;
  452. + }
  453. +
  454. + dev->m2m_dev = v4l2_m2m_init(&hevc_d_m2m_ops);
  455. + if (IS_ERR(dev->m2m_dev)) {
  456. + v4l2_err(&dev->v4l2_dev,
  457. + "Failed to initialize V4L2 M2M device\n");
  458. + ret = PTR_ERR(dev->m2m_dev);
  459. +
  460. + goto err_v4l2;
  461. + }
  462. +
  463. + dev->mdev.dev = &pdev->dev;
  464. + strscpy(dev->mdev.model, HEVC_D_NAME, sizeof(dev->mdev.model));
  465. + strscpy(dev->mdev.bus_info, "platform:" HEVC_D_NAME,
  466. + sizeof(dev->mdev.bus_info));
  467. +
  468. + media_device_init(&dev->mdev);
  469. + dev->mdev.ops = &hevc_d_m2m_media_ops;
  470. + dev->v4l2_dev.mdev = &dev->mdev;
  471. +
  472. + ret = video_register_device(vfd, VFL_TYPE_VIDEO, -1);
  473. + if (ret) {
  474. + v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  475. + goto err_m2m;
  476. + }
  477. +
  478. + v4l2_info(&dev->v4l2_dev,
  479. + "Device registered as /dev/video%d\n", vfd->num);
  480. +
  481. + ret = v4l2_m2m_register_media_controller(dev->m2m_dev, vfd,
  482. + MEDIA_ENT_F_PROC_VIDEO_DECODER);
  483. + if (ret) {
  484. + v4l2_err(&dev->v4l2_dev,
  485. + "Failed to initialize V4L2 M2M media controller\n");
  486. + goto err_video;
  487. + }
  488. +
  489. + ret = media_device_register(&dev->mdev);
  490. + if (ret) {
  491. + v4l2_err(&dev->v4l2_dev, "Failed to register media device\n");
  492. + goto err_m2m_mc;
  493. + }
  494. +
  495. + platform_set_drvdata(pdev, dev);
  496. +
  497. + return 0;
  498. +
  499. +err_m2m_mc:
  500. + v4l2_m2m_unregister_media_controller(dev->m2m_dev);
  501. +err_video:
  502. + video_unregister_device(&dev->vfd);
  503. +err_m2m:
  504. + v4l2_m2m_release(dev->m2m_dev);
  505. +err_v4l2:
  506. + v4l2_device_unregister(&dev->v4l2_dev);
  507. +
  508. + return ret;
  509. +}
  510. +
  511. +static void hevc_d_remove(struct platform_device *pdev)
  512. +{
  513. + struct hevc_d_dev *dev = platform_get_drvdata(pdev);
  514. +
  515. + if (media_devnode_is_registered(dev->mdev.devnode)) {
  516. + media_device_unregister(&dev->mdev);
  517. + v4l2_m2m_unregister_media_controller(dev->m2m_dev);
  518. + media_device_cleanup(&dev->mdev);
  519. + }
  520. +
  521. + v4l2_m2m_release(dev->m2m_dev);
  522. + video_unregister_device(&dev->vfd);
  523. + v4l2_device_unregister(&dev->v4l2_dev);
  524. +
  525. + hevc_d_hw_remove(dev);
  526. +}
  527. +
  528. +static const struct of_device_id hevc_d_dt_match[] = {
  529. + { .compatible = "raspberrypi,hevc-dec", },
  530. + { /* sentinel */ }
  531. +};
  532. +MODULE_DEVICE_TABLE(of, hevc_d_dt_match);
  533. +
  534. +static struct platform_driver hevc_d_driver = {
  535. + .probe = hevc_d_probe,
  536. + .remove = hevc_d_remove,
  537. + .driver = {
  538. + .name = HEVC_D_NAME,
  539. + .of_match_table = of_match_ptr(hevc_d_dt_match),
  540. + },
  541. +};
  542. +module_platform_driver(hevc_d_driver);
  543. +
  544. +MODULE_LICENSE("GPL");
  545. +MODULE_AUTHOR("John Cox <[email protected]>");
  546. +MODULE_DESCRIPTION("Raspberry Pi HEVC V4L2 driver");
  547. --- /dev/null
  548. +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d.h
  549. @@ -0,0 +1,189 @@
  550. +/* SPDX-License-Identifier: GPL-2.0 */
  551. +/*
  552. + * Raspberry Pi HEVC driver
  553. + *
  554. + * Copyright (C) 2024 Raspberry Pi Ltd
  555. + *
  556. + * Based on the Cedrus VPU driver, that is:
  557. + *
  558. + * Copyright (C) 2016 Florent Revest <[email protected]>
  559. + * Copyright (C) 2018 Paul Kocialkowski <[email protected]>
  560. + * Copyright (C) 2018 Bootlin
  561. + */
  562. +
  563. +#ifndef _HEVC_D_H_
  564. +#define _HEVC_D_H_
  565. +
  566. +#include <linux/clk.h>
  567. +#include <linux/platform_device.h>
  568. +#include <media/v4l2-ctrls.h>
  569. +#include <media/v4l2-device.h>
  570. +#include <media/v4l2-mem2mem.h>
  571. +#include <media/videobuf2-v4l2.h>
  572. +#include <media/videobuf2-dma-contig.h>
  573. +
  574. +#define HEVC_D_DEC_ENV_COUNT 6
  575. +#define HEVC_D_P1BUF_COUNT 3
  576. +#define HEVC_D_P2BUF_COUNT 3
  577. +
  578. +#define HEVC_D_NAME "rpi-hevc-dec"
  579. +
  580. +#define HEVC_D_CAPABILITY_UNTILED BIT(0)
  581. +#define HEVC_D_CAPABILITY_H265_DEC BIT(1)
  582. +
  583. +#define HEVC_D_QUIRK_NO_DMA_OFFSET BIT(0)
  584. +
  585. +enum hevc_d_irq_status {
  586. + HEVC_D_IRQ_NONE,
  587. + HEVC_D_IRQ_ERROR,
  588. + HEVC_D_IRQ_OK,
  589. +};
  590. +
  591. +struct hevc_d_control {
  592. + struct v4l2_ctrl_config cfg;
  593. + unsigned char required:1;
  594. +};
  595. +
  596. +struct hevc_d_h265_run {
  597. + u32 slice_ents;
  598. + const struct v4l2_ctrl_hevc_sps *sps;
  599. + const struct v4l2_ctrl_hevc_pps *pps;
  600. + const struct v4l2_ctrl_hevc_decode_params *dec;
  601. + const struct v4l2_ctrl_hevc_slice_params *slice_params;
  602. + const struct v4l2_ctrl_hevc_scaling_matrix *scaling_matrix;
  603. +};
  604. +
  605. +struct hevc_d_run {
  606. + struct vb2_v4l2_buffer *src;
  607. + struct vb2_v4l2_buffer *dst;
  608. +
  609. + struct hevc_d_h265_run h265;
  610. +};
  611. +
  612. +struct hevc_d_buffer {
  613. + struct v4l2_m2m_buffer m2m_buf;
  614. +};
  615. +
  616. +struct hevc_d_dec_state;
  617. +struct hevc_d_dec_env;
  618. +
  619. +struct hevc_d_gptr {
  620. + size_t size;
  621. + __u8 *ptr;
  622. + dma_addr_t addr;
  623. + unsigned long attrs;
  624. +};
  625. +
  626. +struct hevc_d_dev;
  627. +typedef void (*hevc_d_irq_callback)(struct hevc_d_dev *dev, void *ctx);
  628. +
  629. +struct hevc_d_q_aux;
  630. +#define HEVC_D_AUX_ENT_COUNT VB2_MAX_FRAME
  631. +
  632. +struct hevc_d_ctx {
  633. + struct v4l2_fh fh;
  634. + struct hevc_d_dev *dev;
  635. +
  636. + struct v4l2_pix_format_mplane src_fmt;
  637. + struct v4l2_pix_format_mplane dst_fmt;
  638. + int dst_fmt_set;
  639. +
  640. + int src_stream_on;
  641. + int dst_stream_on;
  642. +
  643. + /*
  644. + * fatal_err is set if an error has occurred s.t. decode cannot
  645. + * continue (such as running out of CMA)
  646. + */
  647. + int fatal_err;
  648. +
  649. + /* Lock for queue operations */
  650. + struct mutex ctx_mutex;
  651. +
  652. + struct v4l2_ctrl_handler hdl;
  653. + struct v4l2_ctrl **ctrls;
  654. +
  655. + /*
  656. + * state contains stuff that is only needed in phase0
  657. + * it could be held in dec_env but that would be wasteful
  658. + */
  659. + struct hevc_d_dec_state *state;
  660. + struct hevc_d_dec_env *dec0;
  661. +
  662. + /* Spinlock protecting dec_free */
  663. + spinlock_t dec_lock;
  664. + struct hevc_d_dec_env *dec_free;
  665. +
  666. + struct hevc_d_dec_env *dec_pool;
  667. +
  668. + unsigned int p1idx;
  669. + atomic_t p1out;
  670. +
  671. + unsigned int p2idx;
  672. + struct hevc_d_gptr pu_bufs[HEVC_D_P2BUF_COUNT];
  673. + struct hevc_d_gptr coeff_bufs[HEVC_D_P2BUF_COUNT];
  674. +
  675. + /* Spinlock protecting aux_free */
  676. + spinlock_t aux_lock;
  677. + struct hevc_d_q_aux *aux_free;
  678. +
  679. + struct hevc_d_q_aux *aux_ents[HEVC_D_AUX_ENT_COUNT];
  680. +
  681. + unsigned int colmv_stride;
  682. + unsigned int colmv_picsize;
  683. +};
  684. +
  685. +struct hevc_d_variant {
  686. + unsigned int capabilities;
  687. + unsigned int quirks;
  688. + unsigned int mod_rate;
  689. +};
  690. +
  691. +struct hevc_d_hw_irq_ent;
  692. +
  693. +#define HEVC_D_ICTL_ENABLE_UNLIMITED (-1)
  694. +
  695. +struct hevc_d_hw_irq_ctrl {
  696. + /* Spinlock protecting claim and tail */
  697. + spinlock_t lock;
  698. + struct hevc_d_hw_irq_ent *claim;
  699. + struct hevc_d_hw_irq_ent *tail;
  700. +
  701. + /* Ent for pending irq - also prevents sched */
  702. + struct hevc_d_hw_irq_ent *irq;
  703. + /* Non-zero => do not start a new job - outer layer sched pending */
  704. + int no_sched;
  705. + /* Enable count. -1 always OK, 0 do not sched, +ve shed & count down */
  706. + int enable;
  707. + /* Thread CB requested */
  708. + bool thread_reqed;
  709. +};
  710. +
  711. +struct hevc_d_dev {
  712. + struct v4l2_device v4l2_dev;
  713. + struct video_device vfd;
  714. + struct media_device mdev;
  715. + struct media_pad pad[2];
  716. + struct platform_device *pdev;
  717. + struct device *dev;
  718. + struct v4l2_m2m_dev *m2m_dev;
  719. +
  720. + /* Device file mutex */
  721. + struct mutex dev_mutex;
  722. +
  723. + void __iomem *base_irq;
  724. + void __iomem *base_h265;
  725. +
  726. + struct clk *clock;
  727. + unsigned long max_clock_rate;
  728. +
  729. + int cache_align;
  730. +
  731. + struct hevc_d_hw_irq_ctrl ic_active1;
  732. + struct hevc_d_hw_irq_ctrl ic_active2;
  733. +};
  734. +
  735. +struct v4l2_ctrl *hevc_d_find_ctrl(struct hevc_d_ctx *ctx, u32 id);
  736. +void *hevc_d_find_control_data(struct hevc_d_ctx *ctx, u32 id);
  737. +
  738. +#endif
  739. --- /dev/null
  740. +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c
  741. @@ -0,0 +1,2542 @@
  742. +// SPDX-License-Identifier: GPL-2.0-or-later
  743. +/*
  744. + * Raspberry Pi HEVC driver
  745. + *
  746. + * Copyright (C) 2020 Raspberry Pi Ltd
  747. + *
  748. + * Based on the Cedrus VPU driver, that is:
  749. + *
  750. + * Copyright (C) 2016 Florent Revest <[email protected]>
  751. + * Copyright (C) 2018 Paul Kocialkowski <[email protected]>
  752. + * Copyright (C) 2018 Bootlin
  753. + */
  754. +
  755. +#include <linux/delay.h>
  756. +#include <linux/types.h>
  757. +
  758. +#include <media/videobuf2-dma-contig.h>
  759. +
  760. +#include "hevc_d.h"
  761. +#include "hevc_d_h265.h"
  762. +#include "hevc_d_hw.h"
  763. +#include "hevc_d_video.h"
  764. +
  765. +enum hevc_slice_type {
  766. + HEVC_SLICE_B = 0,
  767. + HEVC_SLICE_P = 1,
  768. + HEVC_SLICE_I = 2,
  769. +};
  770. +
  771. +enum hevc_layer { L0 = 0, L1 = 1 };
  772. +
  773. +static int gptr_alloc(struct hevc_d_dev *const dev, struct hevc_d_gptr *gptr,
  774. + size_t size, unsigned long attrs)
  775. +{
  776. + gptr->size = size;
  777. + gptr->attrs = attrs;
  778. + gptr->addr = 0;
  779. + gptr->ptr = dma_alloc_attrs(dev->dev, gptr->size, &gptr->addr,
  780. + GFP_KERNEL, gptr->attrs);
  781. + return !gptr->ptr ? -ENOMEM : 0;
  782. +}
  783. +
  784. +static void gptr_free(struct hevc_d_dev *const dev,
  785. + struct hevc_d_gptr *const gptr)
  786. +{
  787. + if (gptr->ptr)
  788. + dma_free_attrs(dev->dev, gptr->size, gptr->ptr, gptr->addr,
  789. + gptr->attrs);
  790. + gptr->size = 0;
  791. + gptr->ptr = NULL;
  792. + gptr->addr = 0;
  793. + gptr->attrs = 0;
  794. +}
  795. +
  796. +/* Realloc but do not copy
  797. + *
  798. + * Frees then allocs.
  799. + * If the alloc fails then it attempts to re-allocote the old size
  800. + * On error then check gptr->ptr to determine if anything is currently
  801. + * allocated.
  802. + */
  803. +static int gptr_realloc_new(struct hevc_d_dev * const dev,
  804. + struct hevc_d_gptr * const gptr, size_t size)
  805. +{
  806. + const size_t old_size = gptr->size;
  807. +
  808. + if (size == gptr->size)
  809. + return 0;
  810. +
  811. + if (gptr->ptr)
  812. + dma_free_attrs(dev->dev, gptr->size, gptr->ptr,
  813. + gptr->addr, gptr->attrs);
  814. +
  815. + gptr->addr = 0;
  816. + gptr->size = size;
  817. + gptr->ptr = dma_alloc_attrs(dev->dev, gptr->size,
  818. + &gptr->addr, GFP_KERNEL, gptr->attrs);
  819. +
  820. + if (!gptr->ptr) {
  821. + gptr->addr = 0;
  822. + gptr->size = old_size;
  823. + gptr->ptr = dma_alloc_attrs(dev->dev, gptr->size,
  824. + &gptr->addr, GFP_KERNEL, gptr->attrs);
  825. + if (!gptr->ptr) {
  826. + gptr->size = 0;
  827. + gptr->addr = 0;
  828. + gptr->attrs = 0;
  829. + }
  830. + return -ENOMEM;
  831. + }
  832. +
  833. + return 0;
  834. +}
  835. +
  836. +static size_t next_size(const size_t x)
  837. +{
  838. + return hevc_d_round_up_size(x + 1);
  839. +}
  840. +
  841. +#define NUM_SCALING_FACTORS 4064 /* Not a typo = 0xbe0 + 0x400 */
  842. +
  843. +#define AXI_BASE64 0
  844. +
  845. +#define PROB_BACKUP ((20 << 12) + (20 << 6) + (0 << 0))
  846. +#define PROB_RELOAD ((20 << 12) + (20 << 0) + (0 << 6))
  847. +
  848. +#define HEVC_MAX_REFS V4L2_HEVC_DPB_ENTRIES_NUM_MAX
  849. +
  850. +struct rpi_cmd {
  851. + u32 addr;
  852. + u32 data;
  853. +} __packed;
  854. +
  855. +struct hevc_d_q_aux {
  856. + unsigned int refcount;
  857. + unsigned int q_index;
  858. + struct hevc_d_q_aux *next;
  859. + struct hevc_d_gptr col;
  860. +};
  861. +
  862. +enum hevc_d_decode_state {
  863. + HEVC_D_DECODE_SLICE_START,
  864. + HEVC_D_DECODE_ERROR_DONE,
  865. + HEVC_D_DECODE_PHASE1,
  866. + HEVC_D_DECODE_END,
  867. +};
  868. +
  869. +struct hevc_d_dec_env {
  870. + struct hevc_d_ctx *ctx;
  871. + struct hevc_d_dec_env *next;
  872. +
  873. + enum hevc_d_decode_state state;
  874. + unsigned int decode_order;
  875. + int p1_status; /* P1 status - what to realloc */
  876. +
  877. + struct rpi_cmd *cmd_fifo;
  878. + unsigned int cmd_len, cmd_max;
  879. + unsigned int num_slice_msgs;
  880. + unsigned int pic_width_in_ctbs_y;
  881. + unsigned int pic_height_in_ctbs_y;
  882. + unsigned int dpbno_col;
  883. + u32 reg_slicestart;
  884. + int collocated_from_l0_flag;
  885. + /*
  886. + * Last CTB/Tile X,Y processed by (wpp_)entry_point
  887. + * Could be in _state as P0 only but needs updating where _state
  888. + * is const
  889. + */
  890. + unsigned int entry_ctb_x;
  891. + unsigned int entry_ctb_y;
  892. + unsigned int entry_tile_x;
  893. + unsigned int entry_tile_y;
  894. + unsigned int entry_qp;
  895. + u32 entry_slice;
  896. +
  897. + u32 rpi_config2;
  898. + u32 rpi_framesize;
  899. + u32 rpi_currpoc;
  900. +
  901. + struct vb2_v4l2_buffer *frame_buf;
  902. + struct vb2_v4l2_buffer *src_buf;
  903. + dma_addr_t frame_luma_addr;
  904. + unsigned int luma_stride;
  905. + dma_addr_t frame_chroma_addr;
  906. + unsigned int chroma_stride;
  907. + dma_addr_t ref_addrs[16][2];
  908. + struct hevc_d_q_aux *frame_aux;
  909. + struct hevc_d_q_aux *col_aux;
  910. +
  911. + dma_addr_t cmd_addr;
  912. + size_t cmd_size;
  913. +
  914. + dma_addr_t pu_base_vc;
  915. + dma_addr_t coeff_base_vc;
  916. + u32 pu_stride;
  917. + u32 coeff_stride;
  918. +
  919. +#define SLICE_MSGS_MAX (2 * HEVC_MAX_REFS * 8 + 3)
  920. + u16 slice_msgs[SLICE_MSGS_MAX];
  921. + u8 scaling_factors[NUM_SCALING_FACTORS];
  922. +
  923. + struct media_request *req_pin;
  924. + struct hevc_d_hw_irq_ent irq_ent;
  925. +};
  926. +
  927. +struct hevc_d_dec_state {
  928. + struct v4l2_ctrl_hevc_sps sps;
  929. + struct v4l2_ctrl_hevc_pps pps;
  930. +
  931. + /* Helper vars & tables derived from sps/pps */
  932. + unsigned int log2_ctb_size; /* log2 width of a CTB */
  933. + unsigned int ctb_width; /* Width in CTBs */
  934. + unsigned int ctb_height; /* Height in CTBs */
  935. + unsigned int ctb_size; /* Pic area in CTBs */
  936. + unsigned int tile_width; /* Width in tiles */
  937. + unsigned int tile_height; /* Height in tiles */
  938. +
  939. + int *col_bd;
  940. + int *row_bd;
  941. + int *ctb_addr_rs_to_ts;
  942. + int *ctb_addr_ts_to_rs;
  943. +
  944. + /* Aux starage for DPB */
  945. + struct hevc_d_q_aux *ref_aux[HEVC_MAX_REFS];
  946. + struct hevc_d_q_aux *frame_aux;
  947. +
  948. + /* Slice vars */
  949. + unsigned int slice_idx;
  950. + bool slice_temporal_mvp; /* Slice flag but constant for frame */
  951. + bool use_aux;
  952. + bool mk_aux;
  953. +
  954. + /* Temp vars per run - don't actually need to persist */
  955. + dma_addr_t src_addr;
  956. + const struct v4l2_ctrl_hevc_slice_params *sh;
  957. + const struct v4l2_ctrl_hevc_decode_params *dec;
  958. + unsigned int nb_refs[2];
  959. + unsigned int slice_qp;
  960. + unsigned int max_num_merge_cand; // 0 if I-slice
  961. + bool dependent_slice_segment_flag;
  962. +
  963. + unsigned int start_ts; /* slice_segment_addr -> ts */
  964. + unsigned int start_ctb_x; /* CTB X,Y of start_ts */
  965. + unsigned int start_ctb_y;
  966. + unsigned int prev_ctb_x; /* CTB X,Y of start_ts - 1 */
  967. + unsigned int prev_ctb_y;
  968. +};
  969. +
  970. +static inline int clip_int(const int x, const int lo, const int hi)
  971. +{
  972. + return x < lo ? lo : x > hi ? hi : x;
  973. +}
  974. +
  975. +/* Phase 1 command and bit FIFOs */
  976. +static int cmds_check_space(struct hevc_d_dec_env *const de, unsigned int n)
  977. +{
  978. + struct rpi_cmd *a;
  979. + unsigned int newmax;
  980. +
  981. + if (n > 0x100000) {
  982. + v4l2_err(&de->ctx->dev->v4l2_dev,
  983. + "%s: n %u implausible\n", __func__, n);
  984. + return -ENOMEM;
  985. + }
  986. +
  987. + if (de->cmd_len + n <= de->cmd_max)
  988. + return 0;
  989. +
  990. + newmax = roundup_pow_of_two(de->cmd_len + n);
  991. +
  992. + a = krealloc(de->cmd_fifo, newmax * sizeof(struct rpi_cmd),
  993. + GFP_KERNEL);
  994. + if (!a) {
  995. + v4l2_err(&de->ctx->dev->v4l2_dev,
  996. + "Failed cmd buffer realloc from %u to %u\n",
  997. + de->cmd_max, newmax);
  998. + return -ENOMEM;
  999. + }
  1000. + v4l2_info(&de->ctx->dev->v4l2_dev,
  1001. + "cmd buffer realloc from %u to %u\n", de->cmd_max, newmax);
  1002. +
  1003. + de->cmd_fifo = a;
  1004. + de->cmd_max = newmax;
  1005. + return 0;
  1006. +}
  1007. +
  1008. +// ???? u16 addr - put in u32
  1009. +static void p1_apb_write(struct hevc_d_dec_env *const de, const u16 addr,
  1010. + const u32 data)
  1011. +{
  1012. + if (de->cmd_len >= de->cmd_max) {
  1013. + v4l2_err(&de->ctx->dev->v4l2_dev,
  1014. + "%s: Overflow @ %d\n", __func__, de->cmd_len);
  1015. + return;
  1016. + }
  1017. +
  1018. + de->cmd_fifo[de->cmd_len].addr = addr;
  1019. + de->cmd_fifo[de->cmd_len].data = data;
  1020. +
  1021. + de->cmd_len++;
  1022. +}
  1023. +
  1024. +static int ctb_to_tile(unsigned int ctb, unsigned int *bd, int num)
  1025. +{
  1026. + int i;
  1027. +
  1028. + for (i = 1; ctb >= bd[i]; i++)
  1029. + ; /* bd[] has num+1 elements; bd[0]=0; */
  1030. +
  1031. + return i - 1;
  1032. +}
  1033. +
  1034. +static unsigned int ctb_to_tile_x(const struct hevc_d_dec_state *const s,
  1035. + const unsigned int ctb_x)
  1036. +{
  1037. + return ctb_to_tile(ctb_x, s->col_bd, s->tile_width);
  1038. +}
  1039. +
  1040. +static unsigned int ctb_to_tile_y(const struct hevc_d_dec_state *const s,
  1041. + const unsigned int ctb_y)
  1042. +{
  1043. + return ctb_to_tile(ctb_y, s->row_bd, s->tile_height);
  1044. +}
  1045. +
  1046. +static void aux_q_free(struct hevc_d_ctx *const ctx,
  1047. + struct hevc_d_q_aux *const aq)
  1048. +{
  1049. + struct hevc_d_dev *const dev = ctx->dev;
  1050. +
  1051. + gptr_free(dev, &aq->col);
  1052. + kfree(aq);
  1053. +}
  1054. +
  1055. +static struct hevc_d_q_aux *aux_q_alloc(struct hevc_d_ctx *const ctx,
  1056. + const unsigned int q_index)
  1057. +{
  1058. + struct hevc_d_dev *const dev = ctx->dev;
  1059. + struct hevc_d_q_aux *const aq = kzalloc(sizeof(*aq), GFP_KERNEL);
  1060. +
  1061. + if (!aq)
  1062. + return NULL;
  1063. +
  1064. + if (gptr_alloc(dev, &aq->col, ctx->colmv_picsize,
  1065. + DMA_ATTR_FORCE_CONTIGUOUS | DMA_ATTR_NO_KERNEL_MAPPING))
  1066. + goto fail;
  1067. +
  1068. + /*
  1069. + * Spinlock not required as called in P0 only and
  1070. + * aux checks done by _new
  1071. + */
  1072. + aq->refcount = 1;
  1073. + aq->q_index = q_index;
  1074. + ctx->aux_ents[q_index] = aq;
  1075. + return aq;
  1076. +
  1077. +fail:
  1078. + kfree(aq);
  1079. + return NULL;
  1080. +}
  1081. +
  1082. +static struct hevc_d_q_aux *aux_q_new(struct hevc_d_ctx *const ctx,
  1083. + const unsigned int q_index)
  1084. +{
  1085. + struct hevc_d_q_aux *aq;
  1086. + unsigned long lockflags;
  1087. +
  1088. + spin_lock_irqsave(&ctx->aux_lock, lockflags);
  1089. + /*
  1090. + * If we already have this allocated to a slot then use that
  1091. + * and assume that it will all work itself out in the pipeline
  1092. + */
  1093. + aq = ctx->aux_ents[q_index];
  1094. + if (aq) {
  1095. + ++aq->refcount;
  1096. + } else {
  1097. + aq = ctx->aux_free;
  1098. + if (aq) {
  1099. + ctx->aux_free = aq->next;
  1100. + aq->next = NULL;
  1101. + aq->refcount = 1;
  1102. + aq->q_index = q_index;
  1103. + ctx->aux_ents[q_index] = aq;
  1104. + }
  1105. + }
  1106. + spin_unlock_irqrestore(&ctx->aux_lock, lockflags);
  1107. +
  1108. + if (!aq)
  1109. + aq = aux_q_alloc(ctx, q_index);
  1110. +
  1111. + return aq;
  1112. +}
  1113. +
  1114. +static struct hevc_d_q_aux *aux_q_ref_idx(struct hevc_d_ctx *const ctx,
  1115. + const int q_index)
  1116. +{
  1117. + unsigned long lockflags;
  1118. + struct hevc_d_q_aux *aq;
  1119. +
  1120. + spin_lock_irqsave(&ctx->aux_lock, lockflags);
  1121. + aq = ctx->aux_ents[q_index];
  1122. + if (aq)
  1123. + ++aq->refcount;
  1124. + spin_unlock_irqrestore(&ctx->aux_lock, lockflags);
  1125. +
  1126. + return aq;
  1127. +}
  1128. +
  1129. +static struct hevc_d_q_aux *aux_q_ref(struct hevc_d_ctx *const ctx,
  1130. + struct hevc_d_q_aux *const aq)
  1131. +{
  1132. + unsigned long lockflags;
  1133. +
  1134. + if (aq) {
  1135. + spin_lock_irqsave(&ctx->aux_lock, lockflags);
  1136. + ++aq->refcount;
  1137. + spin_unlock_irqrestore(&ctx->aux_lock, lockflags);
  1138. + }
  1139. + return aq;
  1140. +}
  1141. +
  1142. +static void aux_q_release(struct hevc_d_ctx *const ctx,
  1143. + struct hevc_d_q_aux **const paq)
  1144. +{
  1145. + struct hevc_d_q_aux *const aq = *paq;
  1146. + unsigned long lockflags;
  1147. +
  1148. + if (!aq)
  1149. + return;
  1150. +
  1151. + *paq = NULL;
  1152. +
  1153. + spin_lock_irqsave(&ctx->aux_lock, lockflags);
  1154. + if (--aq->refcount == 0) {
  1155. + aq->next = ctx->aux_free;
  1156. + ctx->aux_free = aq;
  1157. + ctx->aux_ents[aq->q_index] = NULL;
  1158. + aq->q_index = ~0U;
  1159. + }
  1160. + spin_unlock_irqrestore(&ctx->aux_lock, lockflags);
  1161. +}
  1162. +
  1163. +static void aux_q_init(struct hevc_d_ctx *const ctx)
  1164. +{
  1165. + spin_lock_init(&ctx->aux_lock);
  1166. + ctx->aux_free = NULL;
  1167. +}
  1168. +
  1169. +static void aux_q_uninit(struct hevc_d_ctx *const ctx)
  1170. +{
  1171. + struct hevc_d_q_aux *aq;
  1172. +
  1173. + ctx->colmv_picsize = 0;
  1174. + ctx->colmv_stride = 0;
  1175. + while ((aq = ctx->aux_free) != NULL) {
  1176. + ctx->aux_free = aq->next;
  1177. + aux_q_free(ctx, aq);
  1178. + }
  1179. +}
  1180. +
  1181. +/*
  1182. + * Initialisation process for context variables (CABAC init)
  1183. + * see H.265 9.3.2.2
  1184. + *
  1185. + * N.B. If comparing with FFmpeg note that this h/w uses slightly different
  1186. + * offsets to FFmpegs array
  1187. + */
  1188. +
  1189. +/* Actual number of values */
  1190. +#define RPI_PROB_VALS 154U
  1191. +/* Rounded up as we copy words */
  1192. +#define RPI_PROB_ARRAY_SIZE ((154 + 3) & ~3)
  1193. +
  1194. +/* Initialiser values - see tables H.265 9-4 through 9-42 */
  1195. +static const u8 prob_init[3][156] = {
  1196. + {
  1197. + 153, 200, 139, 141, 157, 154, 154, 154, 154, 154, 184, 154, 154,
  1198. + 154, 184, 63, 154, 154, 154, 154, 154, 154, 154, 154, 154, 154,
  1199. + 154, 154, 154, 153, 138, 138, 111, 141, 94, 138, 182, 154, 154,
  1200. + 154, 140, 92, 137, 138, 140, 152, 138, 139, 153, 74, 149, 92,
  1201. + 139, 107, 122, 152, 140, 179, 166, 182, 140, 227, 122, 197, 110,
  1202. + 110, 124, 125, 140, 153, 125, 127, 140, 109, 111, 143, 127, 111,
  1203. + 79, 108, 123, 63, 110, 110, 124, 125, 140, 153, 125, 127, 140,
  1204. + 109, 111, 143, 127, 111, 79, 108, 123, 63, 91, 171, 134, 141,
  1205. + 138, 153, 136, 167, 152, 152, 139, 139, 111, 111, 125, 110, 110,
  1206. + 94, 124, 108, 124, 107, 125, 141, 179, 153, 125, 107, 125, 141,
  1207. + 179, 153, 125, 107, 125, 141, 179, 153, 125, 140, 139, 182, 182,
  1208. + 152, 136, 152, 136, 153, 136, 139, 111, 136, 139, 111, 0, 0,
  1209. + },
  1210. + {
  1211. + 153, 185, 107, 139, 126, 197, 185, 201, 154, 149, 154, 139, 154,
  1212. + 154, 154, 152, 110, 122, 95, 79, 63, 31, 31, 153, 153, 168,
  1213. + 140, 198, 79, 124, 138, 94, 153, 111, 149, 107, 167, 154, 154,
  1214. + 154, 154, 196, 196, 167, 154, 152, 167, 182, 182, 134, 149, 136,
  1215. + 153, 121, 136, 137, 169, 194, 166, 167, 154, 167, 137, 182, 125,
  1216. + 110, 94, 110, 95, 79, 125, 111, 110, 78, 110, 111, 111, 95,
  1217. + 94, 108, 123, 108, 125, 110, 94, 110, 95, 79, 125, 111, 110,
  1218. + 78, 110, 111, 111, 95, 94, 108, 123, 108, 121, 140, 61, 154,
  1219. + 107, 167, 91, 122, 107, 167, 139, 139, 155, 154, 139, 153, 139,
  1220. + 123, 123, 63, 153, 166, 183, 140, 136, 153, 154, 166, 183, 140,
  1221. + 136, 153, 154, 166, 183, 140, 136, 153, 154, 170, 153, 123, 123,
  1222. + 107, 121, 107, 121, 167, 151, 183, 140, 151, 183, 140, 0, 0,
  1223. + },
  1224. + {
  1225. + 153, 160, 107, 139, 126, 197, 185, 201, 154, 134, 154, 139, 154,
  1226. + 154, 183, 152, 154, 137, 95, 79, 63, 31, 31, 153, 153, 168,
  1227. + 169, 198, 79, 224, 167, 122, 153, 111, 149, 92, 167, 154, 154,
  1228. + 154, 154, 196, 167, 167, 154, 152, 167, 182, 182, 134, 149, 136,
  1229. + 153, 121, 136, 122, 169, 208, 166, 167, 154, 152, 167, 182, 125,
  1230. + 110, 124, 110, 95, 94, 125, 111, 111, 79, 125, 126, 111, 111,
  1231. + 79, 108, 123, 93, 125, 110, 124, 110, 95, 94, 125, 111, 111,
  1232. + 79, 125, 126, 111, 111, 79, 108, 123, 93, 121, 140, 61, 154,
  1233. + 107, 167, 91, 107, 107, 167, 139, 139, 170, 154, 139, 153, 139,
  1234. + 123, 123, 63, 124, 166, 183, 140, 136, 153, 154, 166, 183, 140,
  1235. + 136, 153, 154, 166, 183, 140, 136, 153, 154, 170, 153, 138, 138,
  1236. + 122, 121, 122, 121, 167, 151, 183, 140, 151, 183, 140, 0, 0,
  1237. + },
  1238. +};
  1239. +
  1240. +#define CMDS_WRITE_PROB ((RPI_PROB_ARRAY_SIZE / 4) + 1)
  1241. +
  1242. +static void write_prob(struct hevc_d_dec_env *const de,
  1243. + const struct hevc_d_dec_state *const s)
  1244. +{
  1245. + const unsigned int init_type =
  1246. + ((s->sh->flags & V4L2_HEVC_SLICE_PARAMS_FLAG_CABAC_INIT) != 0 &&
  1247. + s->sh->slice_type != HEVC_SLICE_I) ?
  1248. + s->sh->slice_type + 1 :
  1249. + 2 - s->sh->slice_type;
  1250. + const int q = clip_int(s->slice_qp, 0, 51);
  1251. + const u8 *p = prob_init[init_type];
  1252. + u8 dst[RPI_PROB_ARRAY_SIZE];
  1253. + unsigned int i;
  1254. +
  1255. + for (i = 0; i < RPI_PROB_VALS; i++) {
  1256. + int init_value = p[i];
  1257. + int m = (init_value >> 4) * 5 - 45;
  1258. + int n = ((init_value & 15) << 3) - 16;
  1259. + int pre = 2 * (((m * q) >> 4) + n) - 127;
  1260. +
  1261. + pre ^= pre >> 31;
  1262. + if (pre > 124)
  1263. + pre = 124 + (pre & 1);
  1264. + dst[i] = pre;
  1265. + }
  1266. + for (i = RPI_PROB_VALS; i != RPI_PROB_ARRAY_SIZE; ++i)
  1267. + dst[i] = 0;
  1268. +
  1269. + for (i = 0; i < RPI_PROB_ARRAY_SIZE; i += 4)
  1270. + p1_apb_write(de, 0x1000 + i,
  1271. + dst[i] + (dst[i + 1] << 8) + (dst[i + 2] << 16) +
  1272. + (dst[i + 3] << 24));
  1273. +
  1274. + /*
  1275. + * Having written the prob array back it up
  1276. + * This is not always needed but is a small overhead that simplifies
  1277. + * (and speeds up) some multi-tile & WPP scenarios
  1278. + * There are no scenarios where having written a prob we ever want
  1279. + * a previous (non-initial) state back
  1280. + */
  1281. + p1_apb_write(de, RPI_TRANSFER, PROB_BACKUP);
  1282. +}
  1283. +
  1284. +#define CMDS_WRITE_SCALING_FACTORS NUM_SCALING_FACTORS
  1285. +static void write_scaling_factors(struct hevc_d_dec_env *const de)
  1286. +{
  1287. + const u8 *p = (u8 *)de->scaling_factors;
  1288. + int i;
  1289. +
  1290. + for (i = 0; i < NUM_SCALING_FACTORS; i += 4, p += 4)
  1291. + p1_apb_write(de, 0x2000 + i,
  1292. + p[0] + (p[1] << 8) + (p[2] << 16) + (p[3] << 24));
  1293. +}
  1294. +
  1295. +static inline __u32 dma_to_axi_addr(dma_addr_t a)
  1296. +{
  1297. + return (__u32)(a >> 6);
  1298. +}
  1299. +
  1300. +#define CMDS_WRITE_BITSTREAM 4
  1301. +static int write_bitstream(struct hevc_d_dec_env *const de,
  1302. + const struct hevc_d_dec_state *const s)
  1303. +{
  1304. + // FIXME!!!!
  1305. + // Note that FFmpeg V4L2 does not remove emulation prevention bytes,
  1306. + // so this is matched in the configuration here.
  1307. + // Whether that is the correct behaviour or not is not clear in the
  1308. + // spec.
  1309. + const int rpi_use_emu = 1;
  1310. + unsigned int offset = s->sh->data_byte_offset;
  1311. + const unsigned int len = (s->sh->bit_size + 7) / 8 - offset;
  1312. + dma_addr_t addr = s->src_addr + offset;
  1313. +
  1314. + offset = addr & 63;
  1315. +
  1316. + p1_apb_write(de, RPI_BFBASE, dma_to_axi_addr(addr));
  1317. + p1_apb_write(de, RPI_BFNUM, len);
  1318. + p1_apb_write(de, RPI_BFCONTROL, offset + (1 << 7)); // Stop
  1319. + p1_apb_write(de, RPI_BFCONTROL, offset + (rpi_use_emu << 6));
  1320. + return 0;
  1321. +}
  1322. +
  1323. +/*
  1324. + * The slice constant part of the slice register - width and height need to
  1325. + * be ORed in later as they are per-tile / WPP-row
  1326. + */
  1327. +static u32 slice_reg_const(const struct hevc_d_dec_state *const s)
  1328. +{
  1329. + u32 x = (s->max_num_merge_cand << 0) |
  1330. + (s->nb_refs[L0] << 4) |
  1331. + (s->nb_refs[L1] << 8) |
  1332. + (s->sh->slice_type << 12);
  1333. +
  1334. + if (s->sh->flags & V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_SAO_LUMA)
  1335. + x |= BIT(14);
  1336. + if (s->sh->flags & V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_SAO_CHROMA)
  1337. + x |= BIT(15);
  1338. + if (s->sh->slice_type == HEVC_SLICE_B &&
  1339. + (s->sh->flags & V4L2_HEVC_SLICE_PARAMS_FLAG_MVD_L1_ZERO))
  1340. + x |= BIT(16);
  1341. +
  1342. + return x;
  1343. +}
  1344. +
  1345. +#define CMDS_NEW_SLICE_SEGMENT (4 + CMDS_WRITE_SCALING_FACTORS)
  1346. +
  1347. +static void new_slice_segment(struct hevc_d_dec_env *const de,
  1348. + const struct hevc_d_dec_state *const s)
  1349. +{
  1350. + const struct v4l2_ctrl_hevc_sps *const sps = &s->sps;
  1351. + const struct v4l2_ctrl_hevc_pps *const pps = &s->pps;
  1352. +
  1353. + p1_apb_write(de,
  1354. + RPI_SPS0,
  1355. + ((sps->log2_min_luma_coding_block_size_minus3 + 3) << 0) |
  1356. + (s->log2_ctb_size << 4) |
  1357. + ((sps->log2_min_luma_transform_block_size_minus2 + 2)
  1358. + << 8) |
  1359. + ((sps->log2_min_luma_transform_block_size_minus2 + 2 +
  1360. + sps->log2_diff_max_min_luma_transform_block_size)
  1361. + << 12) |
  1362. + ((sps->bit_depth_luma_minus8 + 8) << 16) |
  1363. + ((sps->bit_depth_chroma_minus8 + 8) << 20) |
  1364. + (sps->max_transform_hierarchy_depth_intra << 24) |
  1365. + (sps->max_transform_hierarchy_depth_inter << 28));
  1366. +
  1367. + p1_apb_write(de,
  1368. + RPI_SPS1,
  1369. + ((sps->pcm_sample_bit_depth_luma_minus1 + 1) << 0) |
  1370. + ((sps->pcm_sample_bit_depth_chroma_minus1 + 1) << 4) |
  1371. + ((sps->log2_min_pcm_luma_coding_block_size_minus3 + 3)
  1372. + << 8) |
  1373. + ((sps->log2_min_pcm_luma_coding_block_size_minus3 + 3 +
  1374. + sps->log2_diff_max_min_pcm_luma_coding_block_size)
  1375. + << 12) |
  1376. + (((sps->flags & V4L2_HEVC_SPS_FLAG_SEPARATE_COLOUR_PLANE) ?
  1377. + 0 : sps->chroma_format_idc) << 16) |
  1378. + ((!!(sps->flags & V4L2_HEVC_SPS_FLAG_AMP_ENABLED)) << 18) |
  1379. + ((!!(sps->flags & V4L2_HEVC_SPS_FLAG_PCM_ENABLED)) << 19) |
  1380. + ((!!(sps->flags & V4L2_HEVC_SPS_FLAG_SCALING_LIST_ENABLED))
  1381. + << 20) |
  1382. + ((!!(sps->flags &
  1383. + V4L2_HEVC_SPS_FLAG_STRONG_INTRA_SMOOTHING_ENABLED))
  1384. + << 21));
  1385. +
  1386. + p1_apb_write(de,
  1387. + RPI_PPS,
  1388. + ((s->log2_ctb_size - pps->diff_cu_qp_delta_depth) << 0) |
  1389. + ((!!(pps->flags & V4L2_HEVC_PPS_FLAG_CU_QP_DELTA_ENABLED))
  1390. + << 4) |
  1391. + ((!!(pps->flags &
  1392. + V4L2_HEVC_PPS_FLAG_TRANSQUANT_BYPASS_ENABLED))
  1393. + << 5) |
  1394. + ((!!(pps->flags & V4L2_HEVC_PPS_FLAG_TRANSFORM_SKIP_ENABLED))
  1395. + << 6) |
  1396. + ((!!(pps->flags &
  1397. + V4L2_HEVC_PPS_FLAG_SIGN_DATA_HIDING_ENABLED))
  1398. + << 7) |
  1399. + (((pps->pps_cb_qp_offset + s->sh->slice_cb_qp_offset) & 255)
  1400. + << 8) |
  1401. + (((pps->pps_cr_qp_offset + s->sh->slice_cr_qp_offset) & 255)
  1402. + << 16) |
  1403. + ((!!(pps->flags &
  1404. + V4L2_HEVC_PPS_FLAG_CONSTRAINED_INTRA_PRED))
  1405. + << 24));
  1406. +
  1407. + if (!s->start_ts &&
  1408. + (sps->flags & V4L2_HEVC_SPS_FLAG_SCALING_LIST_ENABLED) != 0)
  1409. + write_scaling_factors(de);
  1410. +
  1411. + if (!s->dependent_slice_segment_flag) {
  1412. + int ctb_col = s->sh->slice_segment_addr %
  1413. + de->pic_width_in_ctbs_y;
  1414. + int ctb_row = s->sh->slice_segment_addr /
  1415. + de->pic_width_in_ctbs_y;
  1416. +
  1417. + de->reg_slicestart = (ctb_col << 0) + (ctb_row << 16);
  1418. + }
  1419. +
  1420. + p1_apb_write(de, RPI_SLICESTART, de->reg_slicestart);
  1421. +}
  1422. +
  1423. +/* Slice messages */
  1424. +
  1425. +static void msg_slice(struct hevc_d_dec_env *const de, const u16 msg)
  1426. +{
  1427. + de->slice_msgs[de->num_slice_msgs++] = msg;
  1428. +}
  1429. +
  1430. +#define CMDS_PROGRAM_SLICECMDS (1 + SLICE_MSGS_MAX)
  1431. +static void program_slicecmds(struct hevc_d_dec_env *const de,
  1432. + const int sliceid)
  1433. +{
  1434. + int i;
  1435. +
  1436. + p1_apb_write(de, RPI_SLICECMDS, de->num_slice_msgs + (sliceid << 8));
  1437. +
  1438. + for (i = 0; i < de->num_slice_msgs; i++)
  1439. + p1_apb_write(de, 0x4000 + 4 * i, de->slice_msgs[i] & 0xffff);
  1440. +}
  1441. +
  1442. +/* NoBackwardPredictionFlag 8.3.5 - Simply checks POCs */
  1443. +static int has_backward(const struct v4l2_hevc_dpb_entry *const dpb,
  1444. + const __u8 *const idx, const unsigned int n,
  1445. + const s32 cur_poc)
  1446. +{
  1447. + unsigned int i;
  1448. +
  1449. + for (i = 0; i < n; ++i) {
  1450. + if (cur_poc < dpb[idx[i]].pic_order_cnt_val)
  1451. + return 0;
  1452. + }
  1453. + return 1;
  1454. +}
  1455. +
  1456. +static void pre_slice_decode(struct hevc_d_dec_env *const de,
  1457. + const struct hevc_d_dec_state *const s)
  1458. +{
  1459. + const struct v4l2_ctrl_hevc_slice_params *const sh = s->sh;
  1460. + const struct v4l2_ctrl_hevc_decode_params *const dec = s->dec;
  1461. + int weighted_pred_flag, idx;
  1462. + u16 cmd_slice;
  1463. + unsigned int collocated_from_l0_flag;
  1464. +
  1465. + de->num_slice_msgs = 0;
  1466. +
  1467. + cmd_slice = 0;
  1468. + if (sh->slice_type == HEVC_SLICE_I)
  1469. + cmd_slice = 1;
  1470. + if (sh->slice_type == HEVC_SLICE_P)
  1471. + cmd_slice = 2;
  1472. + if (sh->slice_type == HEVC_SLICE_B)
  1473. + cmd_slice = 3;
  1474. +
  1475. + cmd_slice |= (s->nb_refs[L0] << 2) | (s->nb_refs[L1] << 6) |
  1476. + (s->max_num_merge_cand << 11);
  1477. +
  1478. + collocated_from_l0_flag =
  1479. + !s->slice_temporal_mvp ||
  1480. + sh->slice_type != HEVC_SLICE_B ||
  1481. + (sh->flags & V4L2_HEVC_SLICE_PARAMS_FLAG_COLLOCATED_FROM_L0);
  1482. + cmd_slice |= collocated_from_l0_flag << 14;
  1483. +
  1484. + if (sh->slice_type == HEVC_SLICE_P || sh->slice_type == HEVC_SLICE_B) {
  1485. + /* Flag to say all reference pictures are from the past */
  1486. + const int no_backward_pred_flag =
  1487. + has_backward(dec->dpb, sh->ref_idx_l0, s->nb_refs[L0],
  1488. + sh->slice_pic_order_cnt) &&
  1489. + has_backward(dec->dpb, sh->ref_idx_l1, s->nb_refs[L1],
  1490. + sh->slice_pic_order_cnt);
  1491. + cmd_slice |= no_backward_pred_flag << 10;
  1492. + msg_slice(de, cmd_slice);
  1493. +
  1494. + if (s->slice_temporal_mvp) {
  1495. + const __u8 *const rpl = collocated_from_l0_flag ?
  1496. + sh->ref_idx_l0 : sh->ref_idx_l1;
  1497. + de->dpbno_col = rpl[sh->collocated_ref_idx];
  1498. + }
  1499. +
  1500. + /* Write reference picture descriptions */
  1501. + weighted_pred_flag =
  1502. + sh->slice_type == HEVC_SLICE_P ?
  1503. + !!(s->pps.flags & V4L2_HEVC_PPS_FLAG_WEIGHTED_PRED) :
  1504. + !!(s->pps.flags & V4L2_HEVC_PPS_FLAG_WEIGHTED_BIPRED);
  1505. +
  1506. + for (idx = 0; idx < s->nb_refs[L0]; ++idx) {
  1507. + unsigned int dpb_no = sh->ref_idx_l0[idx];
  1508. +
  1509. + msg_slice(de,
  1510. + dpb_no |
  1511. + ((dec->dpb[dpb_no].flags &
  1512. + V4L2_HEVC_DPB_ENTRY_LONG_TERM_REFERENCE) ?
  1513. + (1 << 4) : 0) |
  1514. + (weighted_pred_flag ? (3 << 5) : 0));
  1515. + msg_slice(de, dec->dpb[dpb_no].pic_order_cnt_val & 0xffff);
  1516. +
  1517. + if (weighted_pred_flag) {
  1518. + const struct v4l2_hevc_pred_weight_table
  1519. + *const w = &sh->pred_weight_table;
  1520. + const int luma_weight_denom =
  1521. + (1 << w->luma_log2_weight_denom);
  1522. + const unsigned int chroma_log2_weight_denom =
  1523. + (w->luma_log2_weight_denom +
  1524. + w->delta_chroma_log2_weight_denom);
  1525. + const int chroma_weight_denom =
  1526. + (1 << chroma_log2_weight_denom);
  1527. +
  1528. + msg_slice(de,
  1529. + w->luma_log2_weight_denom |
  1530. + (((w->delta_luma_weight_l0[idx] +
  1531. + luma_weight_denom) & 0x1ff)
  1532. + << 3));
  1533. + msg_slice(de, w->luma_offset_l0[idx] & 0xff);
  1534. + msg_slice(de,
  1535. + chroma_log2_weight_denom |
  1536. + (((w->delta_chroma_weight_l0[idx][0] +
  1537. + chroma_weight_denom) & 0x1ff)
  1538. + << 3));
  1539. + msg_slice(de,
  1540. + w->chroma_offset_l0[idx][0] & 0xff);
  1541. + msg_slice(de,
  1542. + chroma_log2_weight_denom |
  1543. + (((w->delta_chroma_weight_l0[idx][1] +
  1544. + chroma_weight_denom) & 0x1ff)
  1545. + << 3));
  1546. + msg_slice(de,
  1547. + w->chroma_offset_l0[idx][1] & 0xff);
  1548. + }
  1549. + }
  1550. +
  1551. + for (idx = 0; idx < s->nb_refs[L1]; ++idx) {
  1552. + unsigned int dpb_no = sh->ref_idx_l1[idx];
  1553. +
  1554. + msg_slice(de,
  1555. + dpb_no |
  1556. + ((dec->dpb[dpb_no].flags &
  1557. + V4L2_HEVC_DPB_ENTRY_LONG_TERM_REFERENCE) ?
  1558. + (1 << 4) : 0) |
  1559. + (weighted_pred_flag ? (3 << 5) : 0));
  1560. + msg_slice(de, dec->dpb[dpb_no].pic_order_cnt_val & 0xffff);
  1561. + if (weighted_pred_flag) {
  1562. + const struct v4l2_hevc_pred_weight_table
  1563. + *const w = &sh->pred_weight_table;
  1564. + const int luma_weight_denom =
  1565. + (1 << w->luma_log2_weight_denom);
  1566. + const unsigned int chroma_log2_weight_denom =
  1567. + (w->luma_log2_weight_denom +
  1568. + w->delta_chroma_log2_weight_denom);
  1569. + const int chroma_weight_denom =
  1570. + (1 << chroma_log2_weight_denom);
  1571. +
  1572. + msg_slice(de,
  1573. + w->luma_log2_weight_denom |
  1574. + (((w->delta_luma_weight_l1[idx] +
  1575. + luma_weight_denom) & 0x1ff) << 3));
  1576. + msg_slice(de, w->luma_offset_l1[idx] & 0xff);
  1577. + msg_slice(de,
  1578. + chroma_log2_weight_denom |
  1579. + (((w->delta_chroma_weight_l1[idx][0] +
  1580. + chroma_weight_denom) & 0x1ff)
  1581. + << 3));
  1582. + msg_slice(de,
  1583. + w->chroma_offset_l1[idx][0] & 0xff);
  1584. + msg_slice(de,
  1585. + chroma_log2_weight_denom |
  1586. + (((w->delta_chroma_weight_l1[idx][1] +
  1587. + chroma_weight_denom) & 0x1ff)
  1588. + << 3));
  1589. + msg_slice(de,
  1590. + w->chroma_offset_l1[idx][1] & 0xff);
  1591. + }
  1592. + }
  1593. + } else {
  1594. + msg_slice(de, cmd_slice);
  1595. + }
  1596. +
  1597. + msg_slice(de,
  1598. + (sh->slice_beta_offset_div2 & 15) |
  1599. + ((sh->slice_tc_offset_div2 & 15) << 4) |
  1600. + ((sh->flags &
  1601. + V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_DEBLOCKING_FILTER_DISABLED) ?
  1602. + 1 << 8 : 0) |
  1603. + ((sh->flags &
  1604. + V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_LOOP_FILTER_ACROSS_SLICES_ENABLED) ?
  1605. + 1 << 9 : 0) |
  1606. + ((s->pps.flags &
  1607. + V4L2_HEVC_PPS_FLAG_LOOP_FILTER_ACROSS_TILES_ENABLED) ?
  1608. + 1 << 10 : 0));
  1609. +
  1610. + msg_slice(de, ((sh->slice_cr_qp_offset & 31) << 5) +
  1611. + (sh->slice_cb_qp_offset & 31)); /* CMD_QPOFF */
  1612. +}
  1613. +
  1614. +#define CMDS_WRITE_SLICE 1
  1615. +static void write_slice(struct hevc_d_dec_env *const de,
  1616. + const struct hevc_d_dec_state *const s,
  1617. + const u32 slice_const,
  1618. + const unsigned int ctb_col,
  1619. + const unsigned int ctb_row)
  1620. +{
  1621. + const unsigned int cs = (1 << s->log2_ctb_size);
  1622. + const unsigned int w_last = s->sps.pic_width_in_luma_samples & (cs - 1);
  1623. + const unsigned int h_last = s->sps.pic_height_in_luma_samples & (cs - 1);
  1624. +
  1625. + p1_apb_write(de, RPI_SLICE,
  1626. + slice_const |
  1627. + ((ctb_col + 1 < s->ctb_width || !w_last ?
  1628. + cs : w_last) << 17) |
  1629. + ((ctb_row + 1 < s->ctb_height || !h_last ?
  1630. + cs : h_last) << 24));
  1631. +}
  1632. +
  1633. +#define PAUSE_MODE_WPP 1
  1634. +#define PAUSE_MODE_TILE 0xffff
  1635. +
  1636. +/*
  1637. + * N.B. This can be called to fill in data from the previous slice so must not
  1638. + * use any state data that may change from slice to slice (e.g. qp)
  1639. + */
  1640. +#define CMDS_NEW_ENTRY_POINT (6 + CMDS_WRITE_SLICE)
  1641. +
  1642. +static void new_entry_point(struct hevc_d_dec_env *const de,
  1643. + const struct hevc_d_dec_state *const s,
  1644. + const bool do_bte,
  1645. + const bool reset_qp_y,
  1646. + const u32 pause_mode,
  1647. + const unsigned int tile_x,
  1648. + const unsigned int tile_y,
  1649. + const unsigned int ctb_col,
  1650. + const unsigned int ctb_row,
  1651. + const unsigned int slice_qp,
  1652. + const u32 slice_const)
  1653. +{
  1654. + const unsigned int endx = s->col_bd[tile_x + 1] - 1;
  1655. + const unsigned int endy = (pause_mode == PAUSE_MODE_WPP) ?
  1656. + ctb_row : s->row_bd[tile_y + 1] - 1;
  1657. +
  1658. + p1_apb_write(de, RPI_TILESTART,
  1659. + s->col_bd[tile_x] | (s->row_bd[tile_y] << 16));
  1660. + p1_apb_write(de, RPI_TILEEND, endx | (endy << 16));
  1661. +
  1662. + if (do_bte)
  1663. + p1_apb_write(de, RPI_BEGINTILEEND, endx | (endy << 16));
  1664. +
  1665. + write_slice(de, s, slice_const, endx, endy);
  1666. +
  1667. + if (reset_qp_y) {
  1668. + unsigned int sps_qp_bd_offset =
  1669. + 6 * s->sps.bit_depth_luma_minus8;
  1670. +
  1671. + p1_apb_write(de, RPI_QP, sps_qp_bd_offset + slice_qp);
  1672. + }
  1673. +
  1674. + p1_apb_write(de, RPI_MODE,
  1675. + pause_mode |
  1676. + ((endx == s->ctb_width - 1) << 17) |
  1677. + ((endy == s->ctb_height - 1) << 18));
  1678. +
  1679. + p1_apb_write(de, RPI_CONTROL, (ctb_col << 0) | (ctb_row << 16));
  1680. +
  1681. + de->entry_tile_x = tile_x;
  1682. + de->entry_tile_y = tile_y;
  1683. + de->entry_ctb_x = ctb_col;
  1684. + de->entry_ctb_y = ctb_row;
  1685. + de->entry_qp = slice_qp;
  1686. + de->entry_slice = slice_const;
  1687. +}
  1688. +
  1689. +/* Wavefront mode */
  1690. +
  1691. +#define CMDS_WPP_PAUSE 4
  1692. +static void wpp_pause(struct hevc_d_dec_env *const de, int ctb_row)
  1693. +{
  1694. + p1_apb_write(de, RPI_STATUS, (ctb_row << 18) | 0x25);
  1695. + p1_apb_write(de, RPI_TRANSFER, PROB_BACKUP);
  1696. + p1_apb_write(de, RPI_MODE,
  1697. + ctb_row == de->pic_height_in_ctbs_y - 1 ?
  1698. + 0x70000 : 0x30000);
  1699. + p1_apb_write(de, RPI_CONTROL, (ctb_row << 16) + 2);
  1700. +}
  1701. +
  1702. +#define CMDS_WPP_ENTRY_FILL_1 (CMDS_WPP_PAUSE + 2 + CMDS_NEW_ENTRY_POINT)
  1703. +static int wpp_entry_fill(struct hevc_d_dec_env *const de,
  1704. + const struct hevc_d_dec_state *const s,
  1705. + const unsigned int last_y)
  1706. +{
  1707. + int rv;
  1708. + const unsigned int last_x = s->ctb_width - 1;
  1709. +
  1710. + rv = cmds_check_space(de, CMDS_WPP_ENTRY_FILL_1 *
  1711. + (last_y - de->entry_ctb_y));
  1712. + if (rv)
  1713. + return rv;
  1714. +
  1715. + while (de->entry_ctb_y < last_y) {
  1716. + /* wpp_entry_x/y set by wpp_entry_point */
  1717. + if (s->ctb_width > 2)
  1718. + wpp_pause(de, de->entry_ctb_y);
  1719. + p1_apb_write(de, RPI_STATUS,
  1720. + (de->entry_ctb_y << 18) | (last_x << 5) | 2);
  1721. +
  1722. + /* if width == 1 then the saved state is the init one */
  1723. + if (s->ctb_width == 2)
  1724. + p1_apb_write(de, RPI_TRANSFER, PROB_BACKUP);
  1725. + else
  1726. + p1_apb_write(de, RPI_TRANSFER, PROB_RELOAD);
  1727. +
  1728. + new_entry_point(de, s, false, true, PAUSE_MODE_WPP,
  1729. + 0, 0, 0, de->entry_ctb_y + 1,
  1730. + de->entry_qp, de->entry_slice);
  1731. + }
  1732. + return 0;
  1733. +}
  1734. +
  1735. +static int wpp_end_previous_slice(struct hevc_d_dec_env *const de,
  1736. + const struct hevc_d_dec_state *const s)
  1737. +{
  1738. + int rv;
  1739. +
  1740. + rv = wpp_entry_fill(de, s, s->prev_ctb_y);
  1741. + if (rv)
  1742. + return rv;
  1743. +
  1744. + rv = cmds_check_space(de, CMDS_WPP_PAUSE + 2);
  1745. + if (rv)
  1746. + return rv;
  1747. +
  1748. + if (de->entry_ctb_x < 2 &&
  1749. + (de->entry_ctb_y < s->start_ctb_y || s->start_ctb_x > 2) &&
  1750. + s->ctb_width > 2)
  1751. + wpp_pause(de, s->prev_ctb_y);
  1752. + p1_apb_write(de, RPI_STATUS,
  1753. + 1 | (s->prev_ctb_x << 5) | (s->prev_ctb_y << 18));
  1754. + if (s->start_ctb_x == 2 ||
  1755. + (s->ctb_width == 2 && de->entry_ctb_y < s->start_ctb_y))
  1756. + p1_apb_write(de, RPI_TRANSFER, PROB_BACKUP);
  1757. + return 0;
  1758. +}
  1759. +
  1760. +/*
  1761. + * Only main profile supported so WPP => !Tiles which makes some of the
  1762. + * next chunk code simpler
  1763. + */
  1764. +static int wpp_decode_slice(struct hevc_d_dec_env *const de,
  1765. + const struct hevc_d_dec_state *const s,
  1766. + bool last_slice)
  1767. +{
  1768. + bool reset_qp_y = true;
  1769. + const bool indep = !s->dependent_slice_segment_flag;
  1770. + int rv;
  1771. +
  1772. + if (s->start_ts) {
  1773. + rv = wpp_end_previous_slice(de, s);
  1774. + if (rv)
  1775. + return rv;
  1776. + }
  1777. + pre_slice_decode(de, s);
  1778. +
  1779. + rv = cmds_check_space(de,
  1780. + CMDS_WRITE_BITSTREAM +
  1781. + CMDS_WRITE_PROB +
  1782. + CMDS_PROGRAM_SLICECMDS +
  1783. + CMDS_NEW_SLICE_SEGMENT +
  1784. + CMDS_NEW_ENTRY_POINT);
  1785. + if (rv)
  1786. + return rv;
  1787. +
  1788. + rv = write_bitstream(de, s);
  1789. + if (rv)
  1790. + return rv;
  1791. +
  1792. + if (!s->start_ts || indep || s->ctb_width == 1)
  1793. + write_prob(de, s);
  1794. + else if (!s->start_ctb_x)
  1795. + p1_apb_write(de, RPI_TRANSFER, PROB_RELOAD);
  1796. + else
  1797. + reset_qp_y = false;
  1798. +
  1799. + program_slicecmds(de, s->slice_idx);
  1800. + new_slice_segment(de, s);
  1801. + new_entry_point(de, s, indep, reset_qp_y, PAUSE_MODE_WPP,
  1802. + 0, 0, s->start_ctb_x, s->start_ctb_y,
  1803. + s->slice_qp, slice_reg_const(s));
  1804. +
  1805. + if (last_slice) {
  1806. + rv = wpp_entry_fill(de, s, s->ctb_height - 1);
  1807. + if (rv)
  1808. + return rv;
  1809. +
  1810. + rv = cmds_check_space(de, CMDS_WPP_PAUSE + 1);
  1811. + if (rv)
  1812. + return rv;
  1813. +
  1814. + if (de->entry_ctb_x < 2 && s->ctb_width > 2)
  1815. + wpp_pause(de, s->ctb_height - 1);
  1816. +
  1817. + p1_apb_write(de, RPI_STATUS,
  1818. + 1 | ((s->ctb_width - 1) << 5) |
  1819. + ((s->ctb_height - 1) << 18));
  1820. + }
  1821. + return 0;
  1822. +}
  1823. +
  1824. +/* Tiles mode */
  1825. +
  1826. +/* Guarantees 1 cmd entry free on exit */
  1827. +static int tile_entry_fill(struct hevc_d_dec_env *const de,
  1828. + const struct hevc_d_dec_state *const s,
  1829. + const unsigned int last_tile_x,
  1830. + const unsigned int last_tile_y)
  1831. +{
  1832. + while (de->entry_tile_y < last_tile_y ||
  1833. + (de->entry_tile_y == last_tile_y &&
  1834. + de->entry_tile_x < last_tile_x)) {
  1835. + int rv;
  1836. + unsigned int t_x = de->entry_tile_x;
  1837. + unsigned int t_y = de->entry_tile_y;
  1838. + const unsigned int last_x = s->col_bd[t_x + 1] - 1;
  1839. + const unsigned int last_y = s->row_bd[t_y + 1] - 1;
  1840. +
  1841. + /* One more than needed here */
  1842. + rv = cmds_check_space(de, CMDS_NEW_ENTRY_POINT + 3);
  1843. + if (rv)
  1844. + return rv;
  1845. +
  1846. + p1_apb_write(de, RPI_STATUS,
  1847. + 2 | (last_x << 5) | (last_y << 18));
  1848. + p1_apb_write(de, RPI_TRANSFER, PROB_RELOAD);
  1849. +
  1850. + // Inc tile
  1851. + if (++t_x >= s->tile_width) {
  1852. + t_x = 0;
  1853. + ++t_y;
  1854. + }
  1855. +
  1856. + new_entry_point(de, s, false, true, PAUSE_MODE_TILE,
  1857. + t_x, t_y, s->col_bd[t_x], s->row_bd[t_y],
  1858. + de->entry_qp, de->entry_slice);
  1859. + }
  1860. + return 0;
  1861. +}
  1862. +
  1863. +/* Write STATUS register with expected end CTU address of previous slice */
  1864. +static int end_previous_slice(struct hevc_d_dec_env *const de,
  1865. + const struct hevc_d_dec_state *const s)
  1866. +{
  1867. + int rv;
  1868. +
  1869. + rv = tile_entry_fill(de, s,
  1870. + ctb_to_tile_x(s, s->prev_ctb_x),
  1871. + ctb_to_tile_y(s, s->prev_ctb_y));
  1872. + if (rv)
  1873. + return rv;
  1874. +
  1875. + p1_apb_write(de, RPI_STATUS,
  1876. + 1 | (s->prev_ctb_x << 5) | (s->prev_ctb_y << 18));
  1877. + return 0;
  1878. +}
  1879. +
  1880. +static int decode_slice(struct hevc_d_dec_env *const de,
  1881. + const struct hevc_d_dec_state *const s,
  1882. + bool last_slice)
  1883. +{
  1884. + bool reset_qp_y;
  1885. + unsigned int tile_x = ctb_to_tile_x(s, s->start_ctb_x);
  1886. + unsigned int tile_y = ctb_to_tile_y(s, s->start_ctb_y);
  1887. + int rv;
  1888. +
  1889. + if (s->start_ts) {
  1890. + rv = end_previous_slice(de, s);
  1891. + if (rv)
  1892. + return rv;
  1893. + }
  1894. +
  1895. + rv = cmds_check_space(de,
  1896. + CMDS_WRITE_BITSTREAM +
  1897. + CMDS_WRITE_PROB +
  1898. + CMDS_PROGRAM_SLICECMDS +
  1899. + CMDS_NEW_SLICE_SEGMENT +
  1900. + CMDS_NEW_ENTRY_POINT);
  1901. + if (rv)
  1902. + return rv;
  1903. +
  1904. + pre_slice_decode(de, s);
  1905. + rv = write_bitstream(de, s);
  1906. + if (rv)
  1907. + return rv;
  1908. +
  1909. + reset_qp_y = !s->start_ts ||
  1910. + !s->dependent_slice_segment_flag ||
  1911. + tile_x != ctb_to_tile_x(s, s->prev_ctb_x) ||
  1912. + tile_y != ctb_to_tile_y(s, s->prev_ctb_y);
  1913. + if (reset_qp_y)
  1914. + write_prob(de, s);
  1915. +
  1916. + program_slicecmds(de, s->slice_idx);
  1917. + new_slice_segment(de, s);
  1918. + new_entry_point(de, s, !s->dependent_slice_segment_flag, reset_qp_y,
  1919. + PAUSE_MODE_TILE,
  1920. + tile_x, tile_y, s->start_ctb_x, s->start_ctb_y,
  1921. + s->slice_qp, slice_reg_const(s));
  1922. +
  1923. + /*
  1924. + * If this is the last slice then fill in the other tile entries
  1925. + * now, otherwise this will be done at the start of the next slice
  1926. + * when it will be known where this slice finishes
  1927. + */
  1928. + if (last_slice) {
  1929. + rv = tile_entry_fill(de, s,
  1930. + s->tile_width - 1,
  1931. + s->tile_height - 1);
  1932. + if (rv)
  1933. + return rv;
  1934. + p1_apb_write(de, RPI_STATUS,
  1935. + 1 | ((s->ctb_width - 1) << 5) |
  1936. + ((s->ctb_height - 1) << 18));
  1937. + }
  1938. + return 0;
  1939. +}
  1940. +
  1941. +/* Scaling factors */
  1942. +
  1943. +static void expand_scaling_list(const unsigned int size_id,
  1944. + u8 *const dst0,
  1945. + const u8 *const src0, uint8_t dc)
  1946. +{
  1947. + u8 *d;
  1948. + unsigned int x, y;
  1949. +
  1950. + switch (size_id) {
  1951. + case 0:
  1952. + memcpy(dst0, src0, 16);
  1953. + break;
  1954. + case 1:
  1955. + memcpy(dst0, src0, 64);
  1956. + break;
  1957. + case 2:
  1958. + d = dst0;
  1959. +
  1960. + for (y = 0; y != 16; y++) {
  1961. + const u8 *s = src0 + (y >> 1) * 8;
  1962. +
  1963. + for (x = 0; x != 8; ++x) {
  1964. + *d++ = *s;
  1965. + *d++ = *s++;
  1966. + }
  1967. + }
  1968. + dst0[0] = dc;
  1969. + break;
  1970. + default:
  1971. + d = dst0;
  1972. +
  1973. + for (y = 0; y != 32; y++) {
  1974. + const u8 *s = src0 + (y >> 2) * 8;
  1975. +
  1976. + for (x = 0; x != 8; ++x) {
  1977. + *d++ = *s;
  1978. + *d++ = *s;
  1979. + *d++ = *s;
  1980. + *d++ = *s++;
  1981. + }
  1982. + }
  1983. + dst0[0] = dc;
  1984. + break;
  1985. + }
  1986. +}
  1987. +
  1988. +static void populate_scaling_factors(const struct hevc_d_run *const run,
  1989. + struct hevc_d_dec_env *const de,
  1990. + const struct hevc_d_dec_state *const s)
  1991. +{
  1992. + const struct v4l2_ctrl_hevc_scaling_matrix *const sl =
  1993. + run->h265.scaling_matrix;
  1994. + /* Array of constants for scaling factors */
  1995. + static const u32 scaling_factor_offsets[4][6] = {
  1996. + /*
  1997. + * MID0 MID1 MID2 MID3 MID4 MID5
  1998. + */
  1999. + /* SID0 (4x4) */
  2000. + { 0x0000, 0x0010, 0x0020, 0x0030, 0x0040, 0x0050 },
  2001. + /* SID1 (8x8) */
  2002. + { 0x0060, 0x00A0, 0x00E0, 0x0120, 0x0160, 0x01A0 },
  2003. + /* SID2 (16x16) */
  2004. + { 0x01E0, 0x02E0, 0x03E0, 0x04E0, 0x05E0, 0x06E0 },
  2005. + /* SID3 (32x32) */
  2006. + { 0x07E0, 0x0BE0, 0x0000, 0x0000, 0x0000, 0x0000 }
  2007. + };
  2008. + unsigned int mid;
  2009. +
  2010. + for (mid = 0; mid < 6; mid++)
  2011. + expand_scaling_list(0, de->scaling_factors +
  2012. + scaling_factor_offsets[0][mid],
  2013. + sl->scaling_list_4x4[mid], 0);
  2014. + for (mid = 0; mid < 6; mid++)
  2015. + expand_scaling_list(1, de->scaling_factors +
  2016. + scaling_factor_offsets[1][mid],
  2017. + sl->scaling_list_8x8[mid], 0);
  2018. + for (mid = 0; mid < 6; mid++)
  2019. + expand_scaling_list(2, de->scaling_factors +
  2020. + scaling_factor_offsets[2][mid],
  2021. + sl->scaling_list_16x16[mid],
  2022. + sl->scaling_list_dc_coef_16x16[mid]);
  2023. + for (mid = 0; mid < 2; mid++)
  2024. + expand_scaling_list(3, de->scaling_factors +
  2025. + scaling_factor_offsets[3][mid],
  2026. + sl->scaling_list_32x32[mid],
  2027. + sl->scaling_list_dc_coef_32x32[mid]);
  2028. +}
  2029. +
  2030. +static void free_ps_info(struct hevc_d_dec_state *const s)
  2031. +{
  2032. + kfree(s->ctb_addr_rs_to_ts);
  2033. + s->ctb_addr_rs_to_ts = NULL;
  2034. + kfree(s->ctb_addr_ts_to_rs);
  2035. + s->ctb_addr_ts_to_rs = NULL;
  2036. +
  2037. + kfree(s->col_bd);
  2038. + s->col_bd = NULL;
  2039. + kfree(s->row_bd);
  2040. + s->row_bd = NULL;
  2041. +}
  2042. +
  2043. +static unsigned int tile_width(const struct hevc_d_dec_state *const s,
  2044. + const unsigned int t_x)
  2045. +{
  2046. + return s->col_bd[t_x + 1] - s->col_bd[t_x];
  2047. +}
  2048. +
  2049. +static unsigned int tile_height(const struct hevc_d_dec_state *const s,
  2050. + const unsigned int t_y)
  2051. +{
  2052. + return s->row_bd[t_y + 1] - s->row_bd[t_y];
  2053. +}
  2054. +
  2055. +static void fill_rs_to_ts(struct hevc_d_dec_state *const s)
  2056. +{
  2057. + unsigned int ts = 0;
  2058. + unsigned int t_y;
  2059. + unsigned int tr_rs = 0;
  2060. +
  2061. + for (t_y = 0; t_y != s->tile_height; ++t_y) {
  2062. + const unsigned int t_h = tile_height(s, t_y);
  2063. + unsigned int t_x;
  2064. + unsigned int tc_rs = tr_rs;
  2065. +
  2066. + for (t_x = 0; t_x != s->tile_width; ++t_x) {
  2067. + const unsigned int t_w = tile_width(s, t_x);
  2068. + unsigned int y;
  2069. + unsigned int rs = tc_rs;
  2070. +
  2071. + for (y = 0; y != t_h; ++y) {
  2072. + unsigned int x;
  2073. +
  2074. + for (x = 0; x != t_w; ++x) {
  2075. + s->ctb_addr_rs_to_ts[rs + x] = ts;
  2076. + s->ctb_addr_ts_to_rs[ts] = rs + x;
  2077. + ++ts;
  2078. + }
  2079. + rs += s->ctb_width;
  2080. + }
  2081. + tc_rs += t_w;
  2082. + }
  2083. + tr_rs += t_h * s->ctb_width;
  2084. + }
  2085. +}
  2086. +
  2087. +static int updated_ps(struct hevc_d_dec_state *const s)
  2088. +{
  2089. + unsigned int i;
  2090. +
  2091. + free_ps_info(s);
  2092. +
  2093. + /* Inferred parameters */
  2094. + s->log2_ctb_size = s->sps.log2_min_luma_coding_block_size_minus3 + 3 +
  2095. + s->sps.log2_diff_max_min_luma_coding_block_size;
  2096. +
  2097. + s->ctb_width = (s->sps.pic_width_in_luma_samples +
  2098. + (1 << s->log2_ctb_size) - 1) >>
  2099. + s->log2_ctb_size;
  2100. + s->ctb_height = (s->sps.pic_height_in_luma_samples +
  2101. + (1 << s->log2_ctb_size) - 1) >>
  2102. + s->log2_ctb_size;
  2103. + s->ctb_size = s->ctb_width * s->ctb_height;
  2104. +
  2105. + s->ctb_addr_rs_to_ts = kmalloc_array(s->ctb_size,
  2106. + sizeof(*s->ctb_addr_rs_to_ts),
  2107. + GFP_KERNEL);
  2108. + if (!s->ctb_addr_rs_to_ts)
  2109. + goto fail;
  2110. + s->ctb_addr_ts_to_rs = kmalloc_array(s->ctb_size,
  2111. + sizeof(*s->ctb_addr_ts_to_rs),
  2112. + GFP_KERNEL);
  2113. + if (!s->ctb_addr_ts_to_rs)
  2114. + goto fail;
  2115. +
  2116. + if (!(s->pps.flags & V4L2_HEVC_PPS_FLAG_TILES_ENABLED)) {
  2117. + s->tile_width = 1;
  2118. + s->tile_height = 1;
  2119. + } else {
  2120. + s->tile_width = s->pps.num_tile_columns_minus1 + 1;
  2121. + s->tile_height = s->pps.num_tile_rows_minus1 + 1;
  2122. + }
  2123. +
  2124. + s->col_bd = kmalloc_array((s->tile_width + 1), sizeof(*s->col_bd),
  2125. + GFP_KERNEL);
  2126. + if (!s->col_bd)
  2127. + goto fail;
  2128. + s->row_bd = kmalloc_array((s->tile_height + 1), sizeof(*s->row_bd),
  2129. + GFP_KERNEL);
  2130. + if (!s->row_bd)
  2131. + goto fail;
  2132. +
  2133. + s->col_bd[0] = 0;
  2134. + for (i = 1; i < s->tile_width; i++)
  2135. + s->col_bd[i] = s->col_bd[i - 1] +
  2136. + s->pps.column_width_minus1[i - 1] + 1;
  2137. + s->col_bd[s->tile_width] = s->ctb_width;
  2138. +
  2139. + s->row_bd[0] = 0;
  2140. + for (i = 1; i < s->tile_height; i++)
  2141. + s->row_bd[i] = s->row_bd[i - 1] +
  2142. + s->pps.row_height_minus1[i - 1] + 1;
  2143. + s->row_bd[s->tile_height] = s->ctb_height;
  2144. +
  2145. + fill_rs_to_ts(s);
  2146. + return 0;
  2147. +
  2148. +fail:
  2149. + free_ps_info(s);
  2150. + /* Set invalid to force reload */
  2151. + s->sps.pic_width_in_luma_samples = 0;
  2152. + return -ENOMEM;
  2153. +}
  2154. +
  2155. +static int write_cmd_buffer(struct hevc_d_dev *const dev,
  2156. + struct hevc_d_dec_env *const de,
  2157. + const struct hevc_d_dec_state *const s)
  2158. +{
  2159. + const size_t cmd_size = ALIGN(de->cmd_len * sizeof(de->cmd_fifo[0]),
  2160. + dev->cache_align);
  2161. +
  2162. + de->cmd_addr = dma_map_single(dev->dev, de->cmd_fifo,
  2163. + cmd_size, DMA_TO_DEVICE);
  2164. + if (dma_mapping_error(dev->dev, de->cmd_addr)) {
  2165. + v4l2_err(&dev->v4l2_dev,
  2166. + "Map cmd buffer (%zu): FAILED\n", cmd_size);
  2167. + return -ENOMEM;
  2168. + }
  2169. + de->cmd_size = cmd_size;
  2170. + return 0;
  2171. +}
  2172. +
  2173. +static void setup_colmv(struct hevc_d_ctx *const ctx, struct hevc_d_run *run,
  2174. + struct hevc_d_dec_state *const s)
  2175. +{
  2176. + ctx->colmv_stride = ALIGN(s->sps.pic_width_in_luma_samples, 64);
  2177. + ctx->colmv_picsize = ctx->colmv_stride *
  2178. + (ALIGN(s->sps.pic_height_in_luma_samples, 64) >> 4);
  2179. +}
  2180. +
  2181. +static struct hevc_d_dec_env *dec_env_new(struct hevc_d_ctx *const ctx)
  2182. +{
  2183. + struct hevc_d_dec_env *de;
  2184. + unsigned long lock_flags;
  2185. +
  2186. + spin_lock_irqsave(&ctx->dec_lock, lock_flags);
  2187. +
  2188. + de = ctx->dec_free;
  2189. + if (de) {
  2190. + ctx->dec_free = de->next;
  2191. + de->next = NULL;
  2192. + de->state = HEVC_D_DECODE_SLICE_START;
  2193. + }
  2194. +
  2195. + spin_unlock_irqrestore(&ctx->dec_lock, lock_flags);
  2196. + return de;
  2197. +}
  2198. +
  2199. +/* Can be called from irq context */
  2200. +static void dec_env_delete(struct hevc_d_dec_env *const de)
  2201. +{
  2202. + struct hevc_d_ctx * const ctx = de->ctx;
  2203. + unsigned long lock_flags;
  2204. +
  2205. + if (de->cmd_size) {
  2206. + dma_unmap_single(ctx->dev->dev, de->cmd_addr, de->cmd_size,
  2207. + DMA_TO_DEVICE);
  2208. + de->cmd_size = 0;
  2209. + }
  2210. +
  2211. + aux_q_release(ctx, &de->frame_aux);
  2212. + aux_q_release(ctx, &de->col_aux);
  2213. +
  2214. + spin_lock_irqsave(&ctx->dec_lock, lock_flags);
  2215. +
  2216. + de->state = HEVC_D_DECODE_END;
  2217. + de->next = ctx->dec_free;
  2218. + ctx->dec_free = de;
  2219. +
  2220. + spin_unlock_irqrestore(&ctx->dec_lock, lock_flags);
  2221. +}
  2222. +
  2223. +static void dec_env_uninit(struct hevc_d_ctx *const ctx)
  2224. +{
  2225. + unsigned int i;
  2226. +
  2227. + if (ctx->dec_pool) {
  2228. + for (i = 0; i != HEVC_D_DEC_ENV_COUNT; ++i) {
  2229. + struct hevc_d_dec_env *const de = ctx->dec_pool + i;
  2230. +
  2231. + kfree(de->cmd_fifo);
  2232. + }
  2233. +
  2234. + kfree(ctx->dec_pool);
  2235. + }
  2236. +
  2237. + ctx->dec_pool = NULL;
  2238. + ctx->dec_free = NULL;
  2239. +}
  2240. +
  2241. +static int dec_env_init(struct hevc_d_ctx *const ctx)
  2242. +{
  2243. + unsigned int i;
  2244. +
  2245. + ctx->dec_pool = kzalloc(sizeof(*ctx->dec_pool) * HEVC_D_DEC_ENV_COUNT,
  2246. + GFP_KERNEL);
  2247. + if (!ctx->dec_pool)
  2248. + return -1;
  2249. +
  2250. + spin_lock_init(&ctx->dec_lock);
  2251. +
  2252. + ctx->dec_free = ctx->dec_pool;
  2253. + for (i = 0; i != HEVC_D_DEC_ENV_COUNT - 1; ++i)
  2254. + ctx->dec_pool[i].next = ctx->dec_pool + i + 1;
  2255. +
  2256. + for (i = 0; i != HEVC_D_DEC_ENV_COUNT; ++i) {
  2257. + struct hevc_d_dec_env *const de = ctx->dec_pool + i;
  2258. +
  2259. + de->ctx = ctx;
  2260. + de->decode_order = i;
  2261. + de->cmd_max = 8096;
  2262. + de->cmd_fifo = kmalloc_array(de->cmd_max,
  2263. + sizeof(struct rpi_cmd),
  2264. + GFP_KERNEL);
  2265. + if (!de->cmd_fifo)
  2266. + goto fail;
  2267. + }
  2268. +
  2269. + return 0;
  2270. +
  2271. +fail:
  2272. + dec_env_uninit(ctx);
  2273. + return -1;
  2274. +}
  2275. +
  2276. +/*
  2277. + * Assume that we get exactly the same DPB for every slice it makes no real
  2278. + * sense otherwise.
  2279. + */
  2280. +#if V4L2_HEVC_DPB_ENTRIES_NUM_MAX > 16
  2281. +#error HEVC_DPB_ENTRIES > h/w slots
  2282. +#endif
  2283. +
  2284. +static u32 mk_config2(const struct hevc_d_dec_state *const s)
  2285. +{
  2286. + const struct v4l2_ctrl_hevc_sps *const sps = &s->sps;
  2287. + const struct v4l2_ctrl_hevc_pps *const pps = &s->pps;
  2288. + u32 c;
  2289. +
  2290. + c = (sps->bit_depth_luma_minus8 + 8) << 0; /* BitDepthY */
  2291. + c |= (sps->bit_depth_chroma_minus8 + 8) << 4; /* BitDepthC */
  2292. + if (sps->bit_depth_luma_minus8) /* BitDepthY */
  2293. + c |= BIT(8);
  2294. + if (sps->bit_depth_chroma_minus8) /* BitDepthC */
  2295. + c |= BIT(9);
  2296. + c |= s->log2_ctb_size << 10;
  2297. + if (pps->flags & V4L2_HEVC_PPS_FLAG_CONSTRAINED_INTRA_PRED)
  2298. + c |= BIT(13);
  2299. + if (sps->flags & V4L2_HEVC_SPS_FLAG_STRONG_INTRA_SMOOTHING_ENABLED)
  2300. + c |= BIT(14);
  2301. + if (s->mk_aux)
  2302. + c |= BIT(15); /* Write motion vectors to external memory */
  2303. + c |= (pps->log2_parallel_merge_level_minus2 + 2) << 16;
  2304. + if (s->slice_temporal_mvp)
  2305. + c |= BIT(19);
  2306. + if (sps->flags & V4L2_HEVC_SPS_FLAG_PCM_LOOP_FILTER_DISABLED)
  2307. + c |= BIT(20);
  2308. + c |= (pps->pps_cb_qp_offset & 31) << 21;
  2309. + c |= (pps->pps_cr_qp_offset & 31) << 26;
  2310. + return c;
  2311. +}
  2312. +
  2313. +static inline bool is_ref_unit_type(const unsigned int nal_unit_type)
  2314. +{
  2315. + /* From Table 7-1
  2316. + * True for 1, 3, 5, 7, 9, 11, 13, 15
  2317. + */
  2318. + return (nal_unit_type & ~0xe) != 0;
  2319. +}
  2320. +
  2321. +void hevc_d_h265_setup(struct hevc_d_ctx *ctx, struct hevc_d_run *run)
  2322. +{
  2323. + struct hevc_d_dev *const dev = ctx->dev;
  2324. + const struct v4l2_ctrl_hevc_decode_params *const dec =
  2325. + run->h265.dec;
  2326. + /* sh0 used where slice header contents should be constant over all
  2327. + * slices, or first slice of frame
  2328. + */
  2329. + const struct v4l2_ctrl_hevc_slice_params *const sh0 =
  2330. + run->h265.slice_params;
  2331. + struct hevc_d_q_aux *dpb_q_aux[V4L2_HEVC_DPB_ENTRIES_NUM_MAX];
  2332. + struct hevc_d_dec_state *const s = ctx->state;
  2333. + struct vb2_queue *vq;
  2334. + struct hevc_d_dec_env *de = ctx->dec0;
  2335. + unsigned int prev_rs;
  2336. + unsigned int i;
  2337. + int rv;
  2338. + bool slice_temporal_mvp;
  2339. + unsigned int ctb_size_y;
  2340. + bool sps_changed = false;
  2341. +
  2342. + s->sh = NULL; /* Avoid use until in the slice loop */
  2343. +
  2344. + slice_temporal_mvp = (sh0->flags &
  2345. + V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_TEMPORAL_MVP_ENABLED);
  2346. +
  2347. + if (de) {
  2348. + v4l2_warn(&dev->v4l2_dev, "Decode env set unexpectedly");
  2349. + goto fail;
  2350. + }
  2351. +
  2352. + /* Frame start */
  2353. +
  2354. + if (!is_sps_set(run->h265.sps)) {
  2355. + v4l2_warn(&dev->v4l2_dev, "SPS never set\n");
  2356. + goto fail;
  2357. + }
  2358. + /* Can't check for PPS easily as all 0's looks valid */
  2359. +
  2360. + if (memcmp(&s->sps, run->h265.sps, sizeof(s->sps)) != 0) {
  2361. + /* SPS changed */
  2362. + memcpy(&s->sps, run->h265.sps, sizeof(s->sps));
  2363. + sps_changed = true;
  2364. + }
  2365. + if (sps_changed ||
  2366. + memcmp(&s->pps, run->h265.pps, sizeof(s->pps)) != 0) {
  2367. + /* SPS changed */
  2368. + memcpy(&s->pps, run->h265.pps, sizeof(s->pps));
  2369. +
  2370. + /* Recalc stuff as required */
  2371. + rv = updated_ps(s);
  2372. + if (rv)
  2373. + goto fail;
  2374. + }
  2375. +
  2376. + de = dec_env_new(ctx);
  2377. + if (!de) {
  2378. + v4l2_err(&dev->v4l2_dev, "Failed to find free decode env\n");
  2379. + goto fail;
  2380. + }
  2381. + ctx->dec0 = de;
  2382. +
  2383. + ctb_size_y =
  2384. + 1U << (s->sps.log2_min_luma_coding_block_size_minus3 +
  2385. + 3 + s->sps.log2_diff_max_min_luma_coding_block_size);
  2386. +
  2387. + de->pic_width_in_ctbs_y =
  2388. + (s->sps.pic_width_in_luma_samples + ctb_size_y - 1) /
  2389. + ctb_size_y; /* 7-15 */
  2390. + de->pic_height_in_ctbs_y =
  2391. + (s->sps.pic_height_in_luma_samples + ctb_size_y - 1) /
  2392. + ctb_size_y; /* 7-17 */
  2393. + de->cmd_len = 0;
  2394. + de->dpbno_col = ~0U;
  2395. +
  2396. + de->luma_stride = ctx->dst_fmt.height * 128;
  2397. + de->frame_luma_addr =
  2398. + vb2_dma_contig_plane_dma_addr(&run->dst->vb2_buf, 0);
  2399. + de->chroma_stride = de->luma_stride / 2;
  2400. + de->frame_chroma_addr =
  2401. + vb2_dma_contig_plane_dma_addr(&run->dst->vb2_buf, 1);
  2402. + de->frame_aux = NULL;
  2403. +
  2404. + if (s->sps.bit_depth_luma_minus8 !=
  2405. + s->sps.bit_depth_chroma_minus8) {
  2406. + v4l2_warn(&dev->v4l2_dev,
  2407. + "Chroma depth (%d) != Luma depth (%d)\n",
  2408. + s->sps.bit_depth_chroma_minus8 + 8,
  2409. + s->sps.bit_depth_luma_minus8 + 8);
  2410. + goto fail;
  2411. + }
  2412. + if (s->sps.bit_depth_luma_minus8 == 0) {
  2413. + if (ctx->dst_fmt.pixelformat != V4L2_PIX_FMT_NV12MT_COL128) {
  2414. + v4l2_err(&dev->v4l2_dev,
  2415. + "Pixel format %#x != NV12MT_COL128 for 8-bit output",
  2416. + ctx->dst_fmt.pixelformat);
  2417. + goto fail;
  2418. + }
  2419. + } else if (s->sps.bit_depth_luma_minus8 == 2) {
  2420. + if (ctx->dst_fmt.pixelformat !=
  2421. + V4L2_PIX_FMT_NV12MT_10_COL128) {
  2422. + v4l2_err(&dev->v4l2_dev,
  2423. + "Pixel format %#x != NV12MT_10_COL128 for 10-bit output",
  2424. + ctx->dst_fmt.pixelformat);
  2425. + goto fail;
  2426. + }
  2427. + } else {
  2428. + v4l2_warn(&dev->v4l2_dev, "Luma depth (%d) unsupported\n",
  2429. + s->sps.bit_depth_luma_minus8 + 8);
  2430. + goto fail;
  2431. + }
  2432. + if (run->dst->vb2_buf.num_planes != 2) {
  2433. + v4l2_warn(&dev->v4l2_dev, "Capture planes (%d) != 2\n",
  2434. + run->dst->vb2_buf.num_planes);
  2435. + goto fail;
  2436. + }
  2437. + if (run->dst->planes[0].length < ctx->dst_fmt.plane_fmt[0].sizeimage ||
  2438. + run->dst->planes[1].length < ctx->dst_fmt.plane_fmt[1].sizeimage) {
  2439. + v4l2_warn(&dev->v4l2_dev,
  2440. + "Capture planes length (%d/%d) < sizeimage (%d/%d)\n",
  2441. + run->dst->planes[0].length,
  2442. + run->dst->planes[1].length,
  2443. + ctx->dst_fmt.plane_fmt[0].sizeimage,
  2444. + ctx->dst_fmt.plane_fmt[1].sizeimage);
  2445. + goto fail;
  2446. + }
  2447. +
  2448. + /*
  2449. + * Fill in ref planes with our address s.t. if we mess up refs
  2450. + * somehow then we still have a valid address entry
  2451. + */
  2452. + for (i = 0; i != 16; ++i) {
  2453. + de->ref_addrs[i][0] = de->frame_luma_addr;
  2454. + de->ref_addrs[i][1] = de->frame_chroma_addr;
  2455. + }
  2456. +
  2457. + /*
  2458. + * Stash initial temporal_mvp flag
  2459. + * This must be the same for all pic slices (7.4.7.1)
  2460. + */
  2461. + s->slice_temporal_mvp = slice_temporal_mvp;
  2462. +
  2463. + /*
  2464. + * Need Aux ents for all (ref) DPB ents if temporal MV could
  2465. + * be enabled for any pic
  2466. + */
  2467. + s->use_aux = ((s->sps.flags &
  2468. + V4L2_HEVC_SPS_FLAG_SPS_TEMPORAL_MVP_ENABLED) != 0);
  2469. + s->mk_aux = s->use_aux &&
  2470. + (s->sps.sps_max_sub_layers_minus1 >= sh0->nuh_temporal_id_plus1 ||
  2471. + is_ref_unit_type(sh0->nal_unit_type));
  2472. +
  2473. + /* Phase 2 reg pre-calc */
  2474. + de->rpi_config2 = mk_config2(s);
  2475. + de->rpi_framesize = (s->sps.pic_height_in_luma_samples << 16) |
  2476. + s->sps.pic_width_in_luma_samples;
  2477. + de->rpi_currpoc = sh0->slice_pic_order_cnt;
  2478. +
  2479. + if (s->sps.flags &
  2480. + V4L2_HEVC_SPS_FLAG_SPS_TEMPORAL_MVP_ENABLED) {
  2481. + setup_colmv(ctx, run, s);
  2482. + }
  2483. +
  2484. + s->slice_idx = 0;
  2485. +
  2486. + if (sh0->slice_segment_addr != 0) {
  2487. + v4l2_warn(&dev->v4l2_dev,
  2488. + "New frame but segment_addr=%d\n",
  2489. + sh0->slice_segment_addr);
  2490. + goto fail;
  2491. + }
  2492. +
  2493. + /* Either map src buffer or use directly */
  2494. + s->src_addr = 0;
  2495. +
  2496. + s->src_addr = vb2_dma_contig_plane_dma_addr(&run->src->vb2_buf, 0);
  2497. + if (!s->src_addr) {
  2498. + v4l2_err(&dev->v4l2_dev, "Failed to map src buffer\n");
  2499. + goto fail;
  2500. + }
  2501. +
  2502. + /* Pre calc parameters */
  2503. + s->dec = dec;
  2504. + for (i = 0; i != run->h265.slice_ents; ++i) {
  2505. + const struct v4l2_ctrl_hevc_slice_params *const sh = sh0 + i;
  2506. + const bool last_slice = i + 1 == run->h265.slice_ents;
  2507. +
  2508. + s->sh = sh;
  2509. +
  2510. + if (run->src->planes[0].bytesused < (sh->bit_size + 7) / 8) {
  2511. + v4l2_warn(&dev->v4l2_dev,
  2512. + "Bit size %d > bytesused %d\n",
  2513. + sh->bit_size, run->src->planes[0].bytesused);
  2514. + goto fail;
  2515. + }
  2516. + if (sh->data_byte_offset >= sh->bit_size / 8) {
  2517. + v4l2_warn(&dev->v4l2_dev,
  2518. + "Bit size %u < Byte offset %u * 8\n",
  2519. + sh->bit_size, sh->data_byte_offset);
  2520. + goto fail;
  2521. + }
  2522. +
  2523. + s->slice_qp = 26 + s->pps.init_qp_minus26 + sh->slice_qp_delta;
  2524. + s->max_num_merge_cand = sh->slice_type == HEVC_SLICE_I ?
  2525. + 0 :
  2526. + (5 - sh->five_minus_max_num_merge_cand);
  2527. + s->dependent_slice_segment_flag =
  2528. + ((sh->flags &
  2529. + V4L2_HEVC_SLICE_PARAMS_FLAG_DEPENDENT_SLICE_SEGMENT) != 0);
  2530. +
  2531. + s->nb_refs[0] = (sh->slice_type == HEVC_SLICE_I) ?
  2532. + 0 :
  2533. + sh->num_ref_idx_l0_active_minus1 + 1;
  2534. + s->nb_refs[1] = (sh->slice_type != HEVC_SLICE_B) ?
  2535. + 0 :
  2536. + sh->num_ref_idx_l1_active_minus1 + 1;
  2537. +
  2538. + if (s->sps.flags & V4L2_HEVC_SPS_FLAG_SCALING_LIST_ENABLED)
  2539. + populate_scaling_factors(run, de, s);
  2540. +
  2541. + /* Calc all the random coord info to avoid repeated conversion in/out */
  2542. + s->start_ts = s->ctb_addr_rs_to_ts[sh->slice_segment_addr];
  2543. + s->start_ctb_x = sh->slice_segment_addr % de->pic_width_in_ctbs_y;
  2544. + s->start_ctb_y = sh->slice_segment_addr / de->pic_width_in_ctbs_y;
  2545. + /* Last CTB of previous slice */
  2546. + prev_rs = !s->start_ts ? 0 : s->ctb_addr_ts_to_rs[s->start_ts - 1];
  2547. + s->prev_ctb_x = prev_rs % de->pic_width_in_ctbs_y;
  2548. + s->prev_ctb_y = prev_rs / de->pic_width_in_ctbs_y;
  2549. +
  2550. + if ((s->pps.flags & V4L2_HEVC_PPS_FLAG_ENTROPY_CODING_SYNC_ENABLED))
  2551. + rv = wpp_decode_slice(de, s, last_slice);
  2552. + else
  2553. + rv = decode_slice(de, s, last_slice);
  2554. + if (rv)
  2555. + goto fail;
  2556. +
  2557. + ++s->slice_idx;
  2558. + }
  2559. +
  2560. + /* Frame end */
  2561. + memset(dpb_q_aux, 0,
  2562. + sizeof(*dpb_q_aux) * V4L2_HEVC_DPB_ENTRIES_NUM_MAX);
  2563. +
  2564. + /*
  2565. + * Locate ref frames
  2566. + * At least in the current implementation this is constant across all
  2567. + * slices. If this changes we will need idx mapping code.
  2568. + * Uses sh so here rather than trigger
  2569. + */
  2570. +
  2571. + vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
  2572. + V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
  2573. +
  2574. + if (!vq) {
  2575. + v4l2_err(&dev->v4l2_dev, "VQ gone!\n");
  2576. + goto fail;
  2577. + }
  2578. +
  2579. + if (write_cmd_buffer(dev, de, s))
  2580. + goto fail;
  2581. +
  2582. + for (i = 0; i < dec->num_active_dpb_entries; ++i) {
  2583. + struct vb2_buffer *buf = vb2_find_buffer(vq, dec->dpb[i].timestamp);
  2584. +
  2585. + if (!buf) {
  2586. + v4l2_warn(&dev->v4l2_dev,
  2587. + "Missing DPB ent %d, timestamp=%lld\n",
  2588. + i, (long long)dec->dpb[i].timestamp);
  2589. + continue;
  2590. + }
  2591. +
  2592. + if (s->use_aux) {
  2593. + int buffer_index = buf->index;
  2594. +
  2595. + dpb_q_aux[i] = aux_q_ref_idx(ctx, buffer_index);
  2596. + if (!dpb_q_aux[i])
  2597. + v4l2_warn(&dev->v4l2_dev,
  2598. + "Missing DPB AUX ent %d, timestamp=%lld, index=%d\n",
  2599. + i, (long long)dec->dpb[i].timestamp,
  2600. + buffer_index);
  2601. + }
  2602. +
  2603. + de->ref_addrs[i][0] =
  2604. + vb2_dma_contig_plane_dma_addr(buf, 0);
  2605. + de->ref_addrs[i][1] =
  2606. + vb2_dma_contig_plane_dma_addr(buf, 1);
  2607. + }
  2608. +
  2609. + /* Move DPB from temp */
  2610. + for (i = 0; i != V4L2_HEVC_DPB_ENTRIES_NUM_MAX; ++i) {
  2611. + aux_q_release(ctx, &s->ref_aux[i]);
  2612. + s->ref_aux[i] = dpb_q_aux[i];
  2613. + }
  2614. +
  2615. + /* Unref the old frame aux too - it is either in the DPB or not now */
  2616. + aux_q_release(ctx, &s->frame_aux);
  2617. +
  2618. + if (s->mk_aux) {
  2619. + s->frame_aux = aux_q_new(ctx, run->dst->vb2_buf.index);
  2620. +
  2621. + if (!s->frame_aux) {
  2622. + v4l2_err(&dev->v4l2_dev,
  2623. + "Failed to obtain aux storage for frame\n");
  2624. + goto fail;
  2625. + }
  2626. +
  2627. + de->frame_aux = aux_q_ref(ctx, s->frame_aux);
  2628. + }
  2629. +
  2630. + if (de->dpbno_col != ~0U) {
  2631. + if (de->dpbno_col >= dec->num_active_dpb_entries) {
  2632. + v4l2_err(&dev->v4l2_dev,
  2633. + "Col ref index %d >= %d\n",
  2634. + de->dpbno_col,
  2635. + dec->num_active_dpb_entries);
  2636. + } else {
  2637. + /* Standard requires that the col pic is constant for
  2638. + * the duration of the pic (text of collocated_ref_idx
  2639. + * in H265-2 2018 7.4.7.1)
  2640. + */
  2641. +
  2642. + /* Spot the collocated ref in passing */
  2643. + de->col_aux = aux_q_ref(ctx,
  2644. + dpb_q_aux[de->dpbno_col]);
  2645. +
  2646. + if (!de->col_aux) {
  2647. + v4l2_warn(&dev->v4l2_dev,
  2648. + "Missing DPB ent for col\n");
  2649. + /* Need to abort if this fails as P2 may
  2650. + * explode on bad data
  2651. + */
  2652. + goto fail;
  2653. + }
  2654. + }
  2655. + }
  2656. +
  2657. + de->state = HEVC_D_DECODE_PHASE1;
  2658. + return;
  2659. +
  2660. +fail:
  2661. + if (de)
  2662. + // Actual error reporting happens in Trigger
  2663. + de->state = HEVC_D_DECODE_ERROR_DONE;
  2664. +}
  2665. +
  2666. +/* Handle PU and COEFF stream overflow
  2667. + *
  2668. + * Returns:
  2669. + * -1 Phase 1 decode error
  2670. + * 0 OK
  2671. + * >0 Out of space (bitmask)
  2672. + */
  2673. +
  2674. +#define STATUS_COEFF_EXHAUSTED 8
  2675. +#define STATUS_PU_EXHAUSTED 16
  2676. +
  2677. +static int check_status(const struct hevc_d_dev *const dev)
  2678. +{
  2679. + const u32 cfstatus = apb_read(dev, RPI_CFSTATUS);
  2680. + const u32 cfnum = apb_read(dev, RPI_CFNUM);
  2681. + u32 status = apb_read(dev, RPI_STATUS);
  2682. +
  2683. + /*
  2684. + * Handle PU and COEFF stream overflow
  2685. + * This is the definition of successful completion of phase 1.
  2686. + * It assures that status register is zero and all blocks in each tile
  2687. + * have completed
  2688. + */
  2689. + if (cfstatus == cfnum)
  2690. + return 0;
  2691. +
  2692. + status &= (STATUS_PU_EXHAUSTED | STATUS_COEFF_EXHAUSTED);
  2693. + if (status)
  2694. + return status;
  2695. +
  2696. + return -1;
  2697. +}
  2698. +
  2699. +static void phase2_cb(struct hevc_d_dev *const dev, void *v)
  2700. +{
  2701. + struct hevc_d_dec_env *const de = v;
  2702. +
  2703. + /* Done with buffers - allow new P1 */
  2704. + hevc_d_hw_irq_active1_enable_claim(dev, 1);
  2705. +
  2706. + v4l2_m2m_buf_done(de->frame_buf, VB2_BUF_STATE_DONE);
  2707. + de->frame_buf = NULL;
  2708. +
  2709. + media_request_manual_complete(de->req_pin);
  2710. + de->req_pin = NULL;
  2711. +
  2712. + dec_env_delete(de);
  2713. +}
  2714. +
  2715. +static void phase2_claimed(struct hevc_d_dev *const dev, void *v)
  2716. +{
  2717. + struct hevc_d_dec_env *const de = v;
  2718. + unsigned int i;
  2719. +
  2720. + apb_write_vc_addr(dev, RPI_PURBASE, de->pu_base_vc);
  2721. + apb_write_vc_len(dev, RPI_PURSTRIDE, de->pu_stride);
  2722. + apb_write_vc_addr(dev, RPI_COEFFRBASE, de->coeff_base_vc);
  2723. + apb_write_vc_len(dev, RPI_COEFFRSTRIDE, de->coeff_stride);
  2724. +
  2725. + apb_write_vc_addr(dev, RPI_OUTYBASE, de->frame_luma_addr);
  2726. + apb_write_vc_addr(dev, RPI_OUTCBASE, de->frame_chroma_addr);
  2727. + apb_write_vc_len(dev, RPI_OUTYSTRIDE, de->luma_stride);
  2728. + apb_write_vc_len(dev, RPI_OUTCSTRIDE, de->chroma_stride);
  2729. +
  2730. + for (i = 0; i < 16; i++) {
  2731. + // Strides are in fact unused but fill in anyway
  2732. + apb_write_vc_addr(dev, 0x9000 + 16 * i, de->ref_addrs[i][0]);
  2733. + apb_write_vc_len(dev, 0x9004 + 16 * i, de->luma_stride);
  2734. + apb_write_vc_addr(dev, 0x9008 + 16 * i, de->ref_addrs[i][1]);
  2735. + apb_write_vc_len(dev, 0x900C + 16 * i, de->chroma_stride);
  2736. + }
  2737. +
  2738. + apb_write(dev, RPI_CONFIG2, de->rpi_config2);
  2739. + apb_write(dev, RPI_FRAMESIZE, de->rpi_framesize);
  2740. + apb_write(dev, RPI_CURRPOC, de->rpi_currpoc);
  2741. +
  2742. + /* collocated reads/writes */
  2743. + apb_write_vc_len(dev, RPI_COLSTRIDE,
  2744. + de->ctx->colmv_stride);
  2745. + apb_write_vc_len(dev, RPI_MVSTRIDE,
  2746. + de->ctx->colmv_stride);
  2747. + apb_write_vc_addr(dev, RPI_MVBASE,
  2748. + !de->frame_aux ? 0 : de->frame_aux->col.addr);
  2749. + apb_write_vc_addr(dev, RPI_COLBASE,
  2750. + !de->col_aux ? 0 : de->col_aux->col.addr);
  2751. +
  2752. + hevc_d_hw_irq_active2_irq(dev, &de->irq_ent, phase2_cb, de);
  2753. +
  2754. + apb_write_final(dev, RPI_NUMROWS, de->pic_height_in_ctbs_y);
  2755. +}
  2756. +
  2757. +static void phase1_claimed(struct hevc_d_dev *const dev, void *v);
  2758. +
  2759. +/* release any and all objects associated with de and reenable phase 1 if
  2760. + * required
  2761. + */// 1 if required
  2762. +static void phase1_err_fin(struct hevc_d_dev *const dev,
  2763. + struct hevc_d_ctx *const ctx,
  2764. + struct hevc_d_dec_env *const de)
  2765. +{
  2766. + /* Return all detached buffers */
  2767. + if (de->src_buf)
  2768. + v4l2_m2m_buf_done(de->src_buf, VB2_BUF_STATE_ERROR);
  2769. + de->src_buf = NULL;
  2770. + if (de->frame_buf)
  2771. + v4l2_m2m_buf_done(de->frame_buf, VB2_BUF_STATE_ERROR);
  2772. + de->frame_buf = NULL;
  2773. +
  2774. + if (de->req_pin)
  2775. + media_request_manual_complete(de->req_pin);
  2776. + de->req_pin = NULL;
  2777. +
  2778. + dec_env_delete(de);
  2779. +
  2780. + /* Reenable phase 0 if we were blocking */
  2781. + if (atomic_add_return(-1, &ctx->p1out) >= HEVC_D_P1BUF_COUNT - 1)
  2782. + v4l2_m2m_job_finish(dev->m2m_dev, ctx->fh.m2m_ctx);
  2783. +
  2784. + /* Done with P1-P2 buffers - allow new P1 */
  2785. + hevc_d_hw_irq_active1_enable_claim(dev, 1);
  2786. +}
  2787. +
  2788. +static void phase1_thread(struct hevc_d_dev *const dev, void *v)
  2789. +{
  2790. + struct hevc_d_dec_env *const de = v;
  2791. + struct hevc_d_ctx *const ctx = de->ctx;
  2792. +
  2793. + struct hevc_d_gptr *const pu_gptr = ctx->pu_bufs + ctx->p2idx;
  2794. + struct hevc_d_gptr *const coeff_gptr = ctx->coeff_bufs + ctx->p2idx;
  2795. +
  2796. + if (de->p1_status & STATUS_PU_EXHAUSTED) {
  2797. + if (gptr_realloc_new(dev, pu_gptr, next_size(pu_gptr->size))) {
  2798. + v4l2_err(&dev->v4l2_dev,
  2799. + "%s: PU realloc (%zx) failed\n",
  2800. + __func__, pu_gptr->size);
  2801. + goto fail;
  2802. + }
  2803. + v4l2_info(&dev->v4l2_dev, "%s: PU realloc (%zx) OK\n",
  2804. + __func__, pu_gptr->size);
  2805. + }
  2806. +
  2807. + if (de->p1_status & STATUS_COEFF_EXHAUSTED) {
  2808. + if (gptr_realloc_new(dev, coeff_gptr,
  2809. + next_size(coeff_gptr->size))) {
  2810. + v4l2_err(&dev->v4l2_dev,
  2811. + "%s: Coeff realloc (%zx) failed\n",
  2812. + __func__, coeff_gptr->size);
  2813. + goto fail;
  2814. + }
  2815. + v4l2_info(&dev->v4l2_dev, "%s: Coeff realloc (%zx) OK\n",
  2816. + __func__, coeff_gptr->size);
  2817. + }
  2818. +
  2819. + phase1_claimed(dev, de);
  2820. + return;
  2821. +
  2822. +fail:
  2823. + if (!pu_gptr->addr || !coeff_gptr->addr) {
  2824. + v4l2_err(&dev->v4l2_dev,
  2825. + "%s: Fatal: failed to reclaim old alloc\n",
  2826. + __func__);
  2827. + ctx->fatal_err = 1;
  2828. + }
  2829. + phase1_err_fin(dev, ctx, de);
  2830. +}
  2831. +
  2832. +/* Always called in irq context (this is good) */
  2833. +static void phase1_cb(struct hevc_d_dev *const dev, void *v)
  2834. +{
  2835. + struct hevc_d_dec_env *const de = v;
  2836. + struct hevc_d_ctx *const ctx = de->ctx;
  2837. +
  2838. + de->p1_status = check_status(dev);
  2839. +
  2840. + if (de->p1_status != 0) {
  2841. + v4l2_info(&dev->v4l2_dev, "%s: Post wait: %#x\n",
  2842. + __func__, de->p1_status);
  2843. +
  2844. + if (de->p1_status < 0)
  2845. + goto fail;
  2846. +
  2847. + /* Need to realloc - push onto a thread rather than IRQ */
  2848. + hevc_d_hw_irq_active1_thread(dev, &de->irq_ent,
  2849. + phase1_thread, de);
  2850. + return;
  2851. + }
  2852. +
  2853. + v4l2_m2m_buf_done(de->src_buf, VB2_BUF_STATE_DONE);
  2854. + de->src_buf = NULL;
  2855. +
  2856. + /* All phase1 error paths done - it is safe to inc p2idx */
  2857. + ctx->p2idx =
  2858. + (ctx->p2idx + 1 >= HEVC_D_P2BUF_COUNT) ? 0 : ctx->p2idx + 1;
  2859. +
  2860. + /* Renable the next setup if we were blocking */
  2861. + if (atomic_add_return(-1, &ctx->p1out) >= HEVC_D_P1BUF_COUNT - 1)
  2862. + v4l2_m2m_job_finish(dev->m2m_dev, ctx->fh.m2m_ctx);
  2863. +
  2864. + hevc_d_hw_irq_active2_claim(dev, &de->irq_ent, phase2_claimed, de);
  2865. +
  2866. + return;
  2867. +
  2868. +fail:
  2869. + phase1_err_fin(dev, ctx, de);
  2870. +}
  2871. +
  2872. +static void phase1_claimed(struct hevc_d_dev *const dev, void *v)
  2873. +{
  2874. + struct hevc_d_dec_env *const de = v;
  2875. + struct hevc_d_ctx *const ctx = de->ctx;
  2876. +
  2877. + const struct hevc_d_gptr * const pu_gptr = ctx->pu_bufs + ctx->p2idx;
  2878. + const struct hevc_d_gptr * const coeff_gptr = ctx->coeff_bufs +
  2879. + ctx->p2idx;
  2880. +
  2881. + if (ctx->fatal_err)
  2882. + goto fail;
  2883. +
  2884. + de->pu_base_vc = pu_gptr->addr;
  2885. + de->pu_stride =
  2886. + ALIGN_DOWN(pu_gptr->size / de->pic_height_in_ctbs_y, 64);
  2887. +
  2888. + de->coeff_base_vc = coeff_gptr->addr;
  2889. + de->coeff_stride =
  2890. + ALIGN_DOWN(coeff_gptr->size / de->pic_height_in_ctbs_y, 64);
  2891. +
  2892. + /* phase1_claimed blocked until cb_phase1 completed so p2idx inc
  2893. + * in cb_phase1 after error detection
  2894. + */
  2895. +
  2896. + apb_write_vc_addr(dev, RPI_PUWBASE, de->pu_base_vc);
  2897. + apb_write_vc_len(dev, RPI_PUWSTRIDE, de->pu_stride);
  2898. + apb_write_vc_addr(dev, RPI_COEFFWBASE, de->coeff_base_vc);
  2899. + apb_write_vc_len(dev, RPI_COEFFWSTRIDE, de->coeff_stride);
  2900. +
  2901. + /* Trigger command FIFO */
  2902. + apb_write(dev, RPI_CFNUM, de->cmd_len);
  2903. +
  2904. + /* Claim irq */
  2905. + hevc_d_hw_irq_active1_irq(dev, &de->irq_ent, phase1_cb, de);
  2906. +
  2907. + /* Start the h/w */
  2908. + apb_write_vc_addr_final(dev, RPI_CFBASE, de->cmd_addr);
  2909. +
  2910. + return;
  2911. +
  2912. +fail:
  2913. + phase1_err_fin(dev, ctx, de);
  2914. +}
  2915. +
  2916. +static void dec_state_delete(struct hevc_d_ctx *const ctx)
  2917. +{
  2918. + unsigned int i;
  2919. + struct hevc_d_dec_state *const s = ctx->state;
  2920. +
  2921. + if (!s)
  2922. + return;
  2923. + ctx->state = NULL;
  2924. +
  2925. + free_ps_info(s);
  2926. +
  2927. + for (i = 0; i != HEVC_MAX_REFS; ++i)
  2928. + aux_q_release(ctx, &s->ref_aux[i]);
  2929. + aux_q_release(ctx, &s->frame_aux);
  2930. +
  2931. + kfree(s);
  2932. +}
  2933. +
  2934. +struct irq_sync {
  2935. + atomic_t done;
  2936. + wait_queue_head_t wq;
  2937. + struct hevc_d_hw_irq_ent irq_ent;
  2938. +};
  2939. +
  2940. +static void phase2_sync_claimed(struct hevc_d_dev *const dev, void *v)
  2941. +{
  2942. + struct irq_sync *const sync = v;
  2943. +
  2944. + atomic_set(&sync->done, 1);
  2945. + wake_up(&sync->wq);
  2946. +}
  2947. +
  2948. +static void phase1_sync_claimed(struct hevc_d_dev *const dev, void *v)
  2949. +{
  2950. + struct irq_sync *const sync = v;
  2951. +
  2952. + hevc_d_hw_irq_active1_enable_claim(dev, 1);
  2953. + hevc_d_hw_irq_active2_claim(dev, &sync->irq_ent, phase2_sync_claimed, sync);
  2954. +}
  2955. +
  2956. +/* Sync with IRQ operations
  2957. + *
  2958. + * Claims phase1 and phase2 in turn and waits for the phase2 claim so any
  2959. + * pending IRQ ops will have completed by the time this returns
  2960. + *
  2961. + * phase1 has counted enables so must reenable once claimed
  2962. + * phase2 has unlimited enables
  2963. + */
  2964. +static void irq_sync(struct hevc_d_dev *const dev)
  2965. +{
  2966. + struct irq_sync sync;
  2967. +
  2968. + atomic_set(&sync.done, 0);
  2969. + init_waitqueue_head(&sync.wq);
  2970. +
  2971. + hevc_d_hw_irq_active1_claim(dev, &sync.irq_ent, phase1_sync_claimed, &sync);
  2972. + wait_event(sync.wq, atomic_read(&sync.done));
  2973. +}
  2974. +
  2975. +static void h265_ctx_uninit(struct hevc_d_dev *const dev, struct hevc_d_ctx *ctx)
  2976. +{
  2977. + unsigned int i;
  2978. +
  2979. + dec_env_uninit(ctx);
  2980. + dec_state_delete(ctx);
  2981. +
  2982. + /*
  2983. + * dec_env & state must be killed before this to release the buffer to
  2984. + * the free pool
  2985. + */
  2986. + aux_q_uninit(ctx);
  2987. +
  2988. + for (i = 0; i != ARRAY_SIZE(ctx->pu_bufs); ++i)
  2989. + gptr_free(dev, ctx->pu_bufs + i);
  2990. + for (i = 0; i != ARRAY_SIZE(ctx->coeff_bufs); ++i)
  2991. + gptr_free(dev, ctx->coeff_bufs + i);
  2992. +}
  2993. +
  2994. +void hevc_d_h265_stop(struct hevc_d_ctx *ctx)
  2995. +{
  2996. + struct hevc_d_dev *const dev = ctx->dev;
  2997. +
  2998. + irq_sync(dev);
  2999. + h265_ctx_uninit(dev, ctx);
  3000. +}
  3001. +
  3002. +int hevc_d_h265_start(struct hevc_d_ctx *ctx)
  3003. +{
  3004. + struct hevc_d_dev *const dev = ctx->dev;
  3005. + unsigned int i;
  3006. +
  3007. + unsigned int w = ctx->dst_fmt.width;
  3008. + unsigned int h = ctx->dst_fmt.height;
  3009. + unsigned int wxh;
  3010. + size_t pu_alloc;
  3011. + size_t coeff_alloc;
  3012. +
  3013. + /* Generate a sanitised WxH for memory alloc. Assume HD if unset */
  3014. + if (w == 0)
  3015. + w = 1920;
  3016. + if (w > 4096)
  3017. + w = 4096;
  3018. + if (h == 0)
  3019. + h = 1088;
  3020. + if (h > 4096)
  3021. + h = 4096;
  3022. + wxh = w * h;
  3023. +
  3024. + ctx->fatal_err = 0;
  3025. + ctx->dec0 = NULL;
  3026. + ctx->state = kzalloc(sizeof(*ctx->state), GFP_KERNEL);
  3027. + if (!ctx->state) {
  3028. + v4l2_err(&dev->v4l2_dev, "Failed to allocate decode state\n");
  3029. + goto fail;
  3030. + }
  3031. +
  3032. + if (dec_env_init(ctx) != 0) {
  3033. + v4l2_err(&dev->v4l2_dev, "Failed to allocate decode envs\n");
  3034. + goto fail;
  3035. + }
  3036. +
  3037. + coeff_alloc = hevc_d_round_up_size(wxh);
  3038. + pu_alloc = hevc_d_round_up_size(wxh / 4);
  3039. + for (i = 0; i != ARRAY_SIZE(ctx->pu_bufs); ++i) {
  3040. + /* Don't actually need a kernel mapping here */
  3041. + if (gptr_alloc(dev, ctx->pu_bufs + i, pu_alloc,
  3042. + DMA_ATTR_NO_KERNEL_MAPPING)) {
  3043. + v4l2_err(&dev->v4l2_dev,
  3044. + "Failed to alloc %#zx PU%d buffer\n",
  3045. + pu_alloc, i);
  3046. + goto fail;
  3047. + }
  3048. + if (gptr_alloc(dev, ctx->coeff_bufs + i, coeff_alloc,
  3049. + DMA_ATTR_NO_KERNEL_MAPPING)) {
  3050. + v4l2_err(&dev->v4l2_dev,
  3051. + "Failed to alloc %#zx Coeff%d buffer\n",
  3052. + pu_alloc, i);
  3053. + goto fail;
  3054. + }
  3055. + }
  3056. + aux_q_init(ctx);
  3057. +
  3058. + return 0;
  3059. +
  3060. +fail:
  3061. + h265_ctx_uninit(dev, ctx);
  3062. + return -ENOMEM;
  3063. +}
  3064. +
  3065. +void hevc_d_h265_trigger(struct hevc_d_ctx *ctx)
  3066. +{
  3067. + struct hevc_d_dev *const dev = ctx->dev;
  3068. + struct hevc_d_dec_env *const de = ctx->dec0;
  3069. + struct vb2_v4l2_buffer *src_buf;
  3070. + struct media_request *req;
  3071. +
  3072. + src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  3073. + req = src_buf->vb2_buf.req_obj.req;
  3074. +
  3075. + switch (!de ? HEVC_D_DECODE_ERROR_DONE : de->state) {
  3076. + default:
  3077. + v4l2_err(&dev->v4l2_dev, "%s: Unexpected state: %d\n", __func__,
  3078. + de->state);
  3079. + fallthrough;
  3080. + case HEVC_D_DECODE_ERROR_DONE:
  3081. + ctx->dec0 = NULL;
  3082. + dec_env_delete(de);
  3083. + v4l2_m2m_buf_done_and_job_finish(dev->m2m_dev, ctx->fh.m2m_ctx,
  3084. + VB2_BUF_STATE_ERROR);
  3085. + media_request_manual_complete(req);
  3086. + break;
  3087. +
  3088. + case HEVC_D_DECODE_PHASE1:
  3089. + ctx->dec0 = NULL;
  3090. +
  3091. + ctx->p1idx = (ctx->p1idx + 1 >= HEVC_D_P1BUF_COUNT) ?
  3092. + 0 : ctx->p1idx + 1;
  3093. +
  3094. + /* We know we have src & dst so no need to test */
  3095. + de->src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  3096. + de->frame_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  3097. + de->req_pin = req;
  3098. +
  3099. + /* We could get rid of the src buffer here if we've already
  3100. + * copied it, but we don't copy the last buffer unless it
  3101. + * didn't return a contig dma addr, and that shouldn't happen
  3102. + */
  3103. +
  3104. + /* Enable the next setup if our Q isn't too big */
  3105. + if (atomic_add_return(1, &ctx->p1out) < HEVC_D_P1BUF_COUNT)
  3106. + v4l2_m2m_job_finish(dev->m2m_dev, ctx->fh.m2m_ctx);
  3107. +
  3108. + hevc_d_hw_irq_active1_claim(dev, &de->irq_ent, phase1_claimed,
  3109. + de);
  3110. + break;
  3111. + }
  3112. +}
  3113. +
  3114. +static int try_ctrl_sps(struct v4l2_ctrl *ctrl)
  3115. +{
  3116. + const struct v4l2_ctrl_hevc_sps *const sps = ctrl->p_new.p_hevc_sps;
  3117. + struct hevc_d_ctx *const ctx = ctrl->priv;
  3118. + struct hevc_d_dev *const dev = ctx->dev;
  3119. +
  3120. + if (sps->chroma_format_idc != 1) {
  3121. + v4l2_warn(&dev->v4l2_dev,
  3122. + "Chroma format (%d) unsupported\n",
  3123. + sps->chroma_format_idc);
  3124. + return -EINVAL;
  3125. + }
  3126. +
  3127. + if (sps->bit_depth_luma_minus8 != 0 &&
  3128. + sps->bit_depth_luma_minus8 != 2) {
  3129. + v4l2_warn(&dev->v4l2_dev,
  3130. + "Luma depth (%d) unsupported\n",
  3131. + sps->bit_depth_luma_minus8 + 8);
  3132. + return -EINVAL;
  3133. + }
  3134. +
  3135. + if (sps->bit_depth_luma_minus8 != sps->bit_depth_chroma_minus8) {
  3136. + v4l2_warn(&dev->v4l2_dev,
  3137. + "Chroma depth (%d) != Luma depth (%d)\n",
  3138. + sps->bit_depth_chroma_minus8 + 8,
  3139. + sps->bit_depth_luma_minus8 + 8);
  3140. + return -EINVAL;
  3141. + }
  3142. +
  3143. + if (!sps->pic_width_in_luma_samples ||
  3144. + !sps->pic_height_in_luma_samples ||
  3145. + sps->pic_width_in_luma_samples > 4096 ||
  3146. + sps->pic_height_in_luma_samples > 4096) {
  3147. + v4l2_warn(&dev->v4l2_dev,
  3148. + "Bad sps width (%u) x height (%u)\n",
  3149. + sps->pic_width_in_luma_samples,
  3150. + sps->pic_height_in_luma_samples);
  3151. + return -EINVAL;
  3152. + }
  3153. +
  3154. + if (!ctx->dst_fmt_set)
  3155. + return 0;
  3156. +
  3157. + if ((sps->bit_depth_luma_minus8 == 0 &&
  3158. + ctx->dst_fmt.pixelformat != V4L2_PIX_FMT_NV12MT_COL128) ||
  3159. + (sps->bit_depth_luma_minus8 == 2 &&
  3160. + ctx->dst_fmt.pixelformat != V4L2_PIX_FMT_NV12MT_10_COL128)) {
  3161. + v4l2_warn(&dev->v4l2_dev,
  3162. + "SPS luma depth %d does not match capture format\n",
  3163. + sps->bit_depth_luma_minus8 + 8);
  3164. + return -EINVAL;
  3165. + }
  3166. +
  3167. + if (sps->pic_width_in_luma_samples > ctx->dst_fmt.width ||
  3168. + sps->pic_height_in_luma_samples > ctx->dst_fmt.height) {
  3169. + v4l2_warn(&dev->v4l2_dev,
  3170. + "SPS size (%dx%d) > capture size (%d,%d)\n",
  3171. + sps->pic_width_in_luma_samples,
  3172. + sps->pic_height_in_luma_samples,
  3173. + ctx->dst_fmt.width,
  3174. + ctx->dst_fmt.height);
  3175. + return -EINVAL;
  3176. + }
  3177. +
  3178. + return 0;
  3179. +}
  3180. +
  3181. +const struct v4l2_ctrl_ops hevc_d_hevc_sps_ctrl_ops = {
  3182. + .try_ctrl = try_ctrl_sps,
  3183. +};
  3184. +
  3185. +static int try_ctrl_pps(struct v4l2_ctrl *ctrl)
  3186. +{
  3187. + const struct v4l2_ctrl_hevc_pps *const pps = ctrl->p_new.p_hevc_pps;
  3188. + struct hevc_d_ctx *const ctx = ctrl->priv;
  3189. + struct hevc_d_dev *const dev = ctx->dev;
  3190. +
  3191. + if ((pps->flags &
  3192. + V4L2_HEVC_PPS_FLAG_ENTROPY_CODING_SYNC_ENABLED) &&
  3193. + (pps->flags &
  3194. + V4L2_HEVC_PPS_FLAG_TILES_ENABLED) &&
  3195. + (pps->num_tile_columns_minus1 || pps->num_tile_rows_minus1)) {
  3196. + v4l2_warn(&dev->v4l2_dev,
  3197. + "WPP + Tiles not supported\n");
  3198. + return -EINVAL;
  3199. + }
  3200. +
  3201. + return 0;
  3202. +}
  3203. +
  3204. +const struct v4l2_ctrl_ops hevc_d_hevc_pps_ctrl_ops = {
  3205. + .try_ctrl = try_ctrl_pps,
  3206. +};
  3207. +
  3208. +void hevc_d_device_run(void *priv)
  3209. +{
  3210. + struct hevc_d_ctx *const ctx = priv;
  3211. + struct hevc_d_dev *const dev = ctx->dev;
  3212. + struct hevc_d_run run = {};
  3213. + struct media_request *src_req;
  3214. +
  3215. + run.src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  3216. + run.dst = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  3217. +
  3218. + if (!run.src || !run.dst) {
  3219. + v4l2_err(&dev->v4l2_dev, "%s: Missing buffer: src=%p, dst=%p\n",
  3220. + __func__, run.src, run.dst);
  3221. + goto fail;
  3222. + }
  3223. +
  3224. + /* Apply request(s) controls */
  3225. + src_req = run.src->vb2_buf.req_obj.req;
  3226. + if (!src_req) {
  3227. + v4l2_err(&dev->v4l2_dev, "%s: Missing request\n", __func__);
  3228. + goto fail;
  3229. + }
  3230. +
  3231. + v4l2_ctrl_request_setup(src_req, &ctx->hdl);
  3232. +
  3233. + switch (ctx->src_fmt.pixelformat) {
  3234. + case V4L2_PIX_FMT_HEVC_SLICE:
  3235. + {
  3236. + const struct v4l2_ctrl *ctrl;
  3237. +
  3238. + run.h265.sps =
  3239. + hevc_d_find_control_data(ctx,
  3240. + V4L2_CID_STATELESS_HEVC_SPS);
  3241. + run.h265.pps =
  3242. + hevc_d_find_control_data(ctx,
  3243. + V4L2_CID_STATELESS_HEVC_PPS);
  3244. + run.h265.dec =
  3245. + hevc_d_find_control_data(ctx,
  3246. + V4L2_CID_STATELESS_HEVC_DECODE_PARAMS);
  3247. +
  3248. + ctrl = hevc_d_find_ctrl(ctx,
  3249. + V4L2_CID_STATELESS_HEVC_SLICE_PARAMS);
  3250. + if (!ctrl || !ctrl->elems) {
  3251. + v4l2_err(&dev->v4l2_dev, "%s: Missing slice params\n",
  3252. + __func__);
  3253. + goto fail;
  3254. + }
  3255. + run.h265.slice_ents = ctrl->elems;
  3256. + run.h265.slice_params = ctrl->p_cur.p;
  3257. +
  3258. + run.h265.scaling_matrix =
  3259. + hevc_d_find_control_data(ctx,
  3260. + V4L2_CID_STATELESS_HEVC_SCALING_MATRIX);
  3261. + break;
  3262. + }
  3263. +
  3264. + default:
  3265. + break;
  3266. + }
  3267. +
  3268. + v4l2_m2m_buf_copy_metadata(run.src, run.dst, true);
  3269. +
  3270. + hevc_d_h265_setup(ctx, &run);
  3271. +
  3272. + /* Complete request(s) controls */
  3273. + v4l2_ctrl_request_complete(src_req, &ctx->hdl);
  3274. +
  3275. + hevc_d_h265_trigger(ctx);
  3276. + return;
  3277. +
  3278. +fail:
  3279. + /* We really shouldn't get here but tidy up what we can */
  3280. + v4l2_m2m_buf_done_and_job_finish(dev->m2m_dev, ctx->fh.m2m_ctx,
  3281. + VB2_BUF_STATE_ERROR);
  3282. + media_request_manual_complete(src_req);
  3283. +}
  3284. --- /dev/null
  3285. +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.h
  3286. @@ -0,0 +1,23 @@
  3287. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  3288. +/*
  3289. + * Raspberry Pi HEVC driver
  3290. + *
  3291. + * Copyright (C) 2024 Raspberry Pi Ltd
  3292. + *
  3293. + */
  3294. +
  3295. +#ifndef _HEVC_D_H265_H_
  3296. +#define _HEVC_D_H265_H_
  3297. +#include "hevc_d.h"
  3298. +
  3299. +extern const struct v4l2_ctrl_ops hevc_d_hevc_sps_ctrl_ops;
  3300. +extern const struct v4l2_ctrl_ops hevc_d_hevc_pps_ctrl_ops;
  3301. +
  3302. +void hevc_d_h265_setup(struct hevc_d_ctx *ctx, struct hevc_d_run *run);
  3303. +int hevc_d_h265_start(struct hevc_d_ctx *ctx);
  3304. +void hevc_d_h265_stop(struct hevc_d_ctx *ctx);
  3305. +void hevc_d_h265_trigger(struct hevc_d_ctx *ctx);
  3306. +
  3307. +void hevc_d_device_run(void *priv);
  3308. +
  3309. +#endif
  3310. --- /dev/null
  3311. +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_hw.c
  3312. @@ -0,0 +1,376 @@
  3313. +// SPDX-License-Identifier: GPL-2.0
  3314. +/*
  3315. + * Raspberry Pi HEVC driver
  3316. + *
  3317. + * Copyright (C) 2024 Raspberry Pi Ltd
  3318. + *
  3319. + * Based on the Cedrus VPU driver, that is:
  3320. + *
  3321. + * Copyright (C) 2016 Florent Revest <[email protected]>
  3322. + * Copyright (C) 2018 Paul Kocialkowski <[email protected]>
  3323. + * Copyright (C) 2018 Bootlin
  3324. + */
  3325. +#include <linux/clk.h>
  3326. +#include <linux/component.h>
  3327. +#include <linux/dma-mapping.h>
  3328. +#include <linux/interrupt.h>
  3329. +#include <linux/io.h>
  3330. +#include <linux/of_reserved_mem.h>
  3331. +#include <linux/of_device.h>
  3332. +#include <linux/of_platform.h>
  3333. +#include <linux/platform_device.h>
  3334. +#include <linux/regmap.h>
  3335. +#include <linux/reset.h>
  3336. +
  3337. +#include <media/videobuf2-core.h>
  3338. +#include <media/v4l2-mem2mem.h>
  3339. +
  3340. +#include <soc/bcm2835/raspberrypi-firmware.h>
  3341. +
  3342. +#include "hevc_d.h"
  3343. +#include "hevc_d_hw.h"
  3344. +
  3345. +static void pre_irq(struct hevc_d_dev *dev, struct hevc_d_hw_irq_ent *ient,
  3346. + hevc_d_irq_callback cb, void *v,
  3347. + struct hevc_d_hw_irq_ctrl *ictl)
  3348. +{
  3349. + unsigned long flags;
  3350. +
  3351. + if (ictl->irq) {
  3352. + v4l2_err(&dev->v4l2_dev, "Attempt to claim IRQ when already claimed\n");
  3353. + return;
  3354. + }
  3355. +
  3356. + ient->cb = cb;
  3357. + ient->v = v;
  3358. +
  3359. + spin_lock_irqsave(&ictl->lock, flags);
  3360. + ictl->irq = ient;
  3361. + ictl->no_sched++;
  3362. + spin_unlock_irqrestore(&ictl->lock, flags);
  3363. +}
  3364. +
  3365. +/* Should be called from inside ictl->lock */
  3366. +static inline bool sched_enabled(const struct hevc_d_hw_irq_ctrl * const ictl)
  3367. +{
  3368. + return ictl->no_sched <= 0 && ictl->enable;
  3369. +}
  3370. +
  3371. +/* Should be called from inside ictl->lock & after checking sched_enabled() */
  3372. +static inline void set_claimed(struct hevc_d_hw_irq_ctrl * const ictl)
  3373. +{
  3374. + if (ictl->enable > 0)
  3375. + --ictl->enable;
  3376. + ictl->no_sched = 1;
  3377. +}
  3378. +
  3379. +/* Should be called from inside ictl->lock */
  3380. +static struct hevc_d_hw_irq_ent *get_sched(struct hevc_d_hw_irq_ctrl * const ictl)
  3381. +{
  3382. + struct hevc_d_hw_irq_ent *ient;
  3383. +
  3384. + if (!sched_enabled(ictl))
  3385. + return NULL;
  3386. +
  3387. + ient = ictl->claim;
  3388. + if (!ient)
  3389. + return NULL;
  3390. + ictl->claim = ient->next;
  3391. +
  3392. + set_claimed(ictl);
  3393. + return ient;
  3394. +}
  3395. +
  3396. +/* Run a callback & check to see if there is anything else to run */
  3397. +static void sched_cb(struct hevc_d_dev * const dev,
  3398. + struct hevc_d_hw_irq_ctrl * const ictl,
  3399. + struct hevc_d_hw_irq_ent *ient)
  3400. +{
  3401. + while (ient) {
  3402. + unsigned long flags;
  3403. +
  3404. + ient->cb(dev, ient->v);
  3405. +
  3406. + spin_lock_irqsave(&ictl->lock, flags);
  3407. +
  3408. + /*
  3409. + * Always dec no_sched after cb exec - must have been set
  3410. + * on entry to cb
  3411. + */
  3412. + --ictl->no_sched;
  3413. + ient = get_sched(ictl);
  3414. +
  3415. + spin_unlock_irqrestore(&ictl->lock, flags);
  3416. + }
  3417. +}
  3418. +
  3419. +/* Should only ever be called from its own IRQ cb so no lock required */
  3420. +static void pre_thread(struct hevc_d_dev *dev,
  3421. + struct hevc_d_hw_irq_ent *ient,
  3422. + hevc_d_irq_callback cb, void *v,
  3423. + struct hevc_d_hw_irq_ctrl *ictl)
  3424. +{
  3425. + ient->cb = cb;
  3426. + ient->v = v;
  3427. + ictl->irq = ient;
  3428. + ictl->thread_reqed = true;
  3429. + ictl->no_sched++; /* This is unwound in do_thread */
  3430. +}
  3431. +
  3432. +/* Called in irq context */
  3433. +static void do_irq(struct hevc_d_dev * const dev,
  3434. + struct hevc_d_hw_irq_ctrl * const ictl)
  3435. +{
  3436. + struct hevc_d_hw_irq_ent *ient;
  3437. + unsigned long flags;
  3438. +
  3439. + spin_lock_irqsave(&ictl->lock, flags);
  3440. + ient = ictl->irq;
  3441. + ictl->irq = NULL;
  3442. + spin_unlock_irqrestore(&ictl->lock, flags);
  3443. +
  3444. + sched_cb(dev, ictl, ient);
  3445. +}
  3446. +
  3447. +static void do_claim(struct hevc_d_dev * const dev,
  3448. + struct hevc_d_hw_irq_ent *ient,
  3449. + const hevc_d_irq_callback cb, void * const v,
  3450. + struct hevc_d_hw_irq_ctrl * const ictl)
  3451. +{
  3452. + unsigned long flags;
  3453. +
  3454. + ient->next = NULL;
  3455. + ient->cb = cb;
  3456. + ient->v = v;
  3457. +
  3458. + spin_lock_irqsave(&ictl->lock, flags);
  3459. +
  3460. + if (ictl->claim) {
  3461. + /* If we have a Q then add to end */
  3462. + ictl->tail->next = ient;
  3463. + ictl->tail = ient;
  3464. + ient = NULL;
  3465. + } else if (!sched_enabled(ictl)) {
  3466. + /* Empty Q but other activity in progress so Q */
  3467. + ictl->claim = ient;
  3468. + ictl->tail = ient;
  3469. + ient = NULL;
  3470. + } else {
  3471. + /*
  3472. + * Nothing else going on - schedule immediately and
  3473. + * prevent anything else scheduling claims
  3474. + */
  3475. + set_claimed(ictl);
  3476. + }
  3477. +
  3478. + spin_unlock_irqrestore(&ictl->lock, flags);
  3479. +
  3480. + sched_cb(dev, ictl, ient);
  3481. +}
  3482. +
  3483. +/* Enable n claims.
  3484. + * n < 0 set to unlimited (default on init)
  3485. + * n = 0 if previously unlimited then disable otherwise nop
  3486. + * n > 0 if previously unlimited then set to n enables
  3487. + * otherwise add n enables
  3488. + * The enable count is automatically decremented every time a claim is run
  3489. + */
  3490. +static void do_enable_claim(struct hevc_d_dev * const dev,
  3491. + int n,
  3492. + struct hevc_d_hw_irq_ctrl * const ictl)
  3493. +{
  3494. + unsigned long flags;
  3495. + struct hevc_d_hw_irq_ent *ient;
  3496. +
  3497. + spin_lock_irqsave(&ictl->lock, flags);
  3498. + ictl->enable = n < 0 ? -1 : ictl->enable <= 0 ? n : ictl->enable + n;
  3499. + ient = get_sched(ictl);
  3500. + spin_unlock_irqrestore(&ictl->lock, flags);
  3501. +
  3502. + sched_cb(dev, ictl, ient);
  3503. +}
  3504. +
  3505. +static void ictl_init(struct hevc_d_hw_irq_ctrl * const ictl, int enables)
  3506. +{
  3507. + spin_lock_init(&ictl->lock);
  3508. + ictl->claim = NULL;
  3509. + ictl->tail = NULL;
  3510. + ictl->irq = NULL;
  3511. + ictl->no_sched = 0;
  3512. + ictl->enable = enables;
  3513. + ictl->thread_reqed = false;
  3514. +}
  3515. +
  3516. +static void ictl_uninit(struct hevc_d_hw_irq_ctrl * const ictl)
  3517. +{
  3518. + /* Nothing to do */
  3519. +}
  3520. +
  3521. +static irqreturn_t hevc_d_irq_irq(int irq, void *data)
  3522. +{
  3523. + struct hevc_d_dev * const dev = data;
  3524. + __u32 ictrl;
  3525. +
  3526. + ictrl = irq_read(dev, ARG_IC_ICTRL);
  3527. + if (!(ictrl & ARG_IC_ICTRL_ALL_IRQ_MASK)) {
  3528. + v4l2_warn(&dev->v4l2_dev, "IRQ but no IRQ bits set\n");
  3529. + return IRQ_NONE;
  3530. + }
  3531. +
  3532. + /* Cancel any/all irqs */
  3533. + irq_write(dev, ARG_IC_ICTRL, ictrl & ~ARG_IC_ICTRL_SET_ZERO_MASK);
  3534. +
  3535. + /*
  3536. + * Service Active2 before Active1 so Phase 1 can transition to Phase 2
  3537. + * without delay
  3538. + */
  3539. + if (ictrl & ARG_IC_ICTRL_ACTIVE2_INT_SET)
  3540. + do_irq(dev, &dev->ic_active2);
  3541. + if (ictrl & ARG_IC_ICTRL_ACTIVE1_INT_SET)
  3542. + do_irq(dev, &dev->ic_active1);
  3543. +
  3544. + return dev->ic_active1.thread_reqed || dev->ic_active2.thread_reqed ?
  3545. + IRQ_WAKE_THREAD : IRQ_HANDLED;
  3546. +}
  3547. +
  3548. +static void do_thread(struct hevc_d_dev * const dev,
  3549. + struct hevc_d_hw_irq_ctrl *const ictl)
  3550. +{
  3551. + unsigned long flags;
  3552. + struct hevc_d_hw_irq_ent *ient = NULL;
  3553. +
  3554. + spin_lock_irqsave(&ictl->lock, flags);
  3555. +
  3556. + if (ictl->thread_reqed) {
  3557. + ient = ictl->irq;
  3558. + ictl->thread_reqed = false;
  3559. + ictl->irq = NULL;
  3560. + }
  3561. +
  3562. + spin_unlock_irqrestore(&ictl->lock, flags);
  3563. +
  3564. + sched_cb(dev, ictl, ient);
  3565. +}
  3566. +
  3567. +static irqreturn_t hevc_d_irq_thread(int irq, void *data)
  3568. +{
  3569. + struct hevc_d_dev * const dev = data;
  3570. +
  3571. + do_thread(dev, &dev->ic_active1);
  3572. + do_thread(dev, &dev->ic_active2);
  3573. +
  3574. + return IRQ_HANDLED;
  3575. +}
  3576. +
  3577. +/*
  3578. + * May only be called from Active1 CB
  3579. + * IRQs should not be expected until execution continues in the cb
  3580. + */
  3581. +void hevc_d_hw_irq_active1_thread(struct hevc_d_dev *dev,
  3582. + struct hevc_d_hw_irq_ent *ient,
  3583. + hevc_d_irq_callback thread_cb, void *ctx)
  3584. +{
  3585. + pre_thread(dev, ient, thread_cb, ctx, &dev->ic_active1);
  3586. +}
  3587. +
  3588. +void hevc_d_hw_irq_active1_enable_claim(struct hevc_d_dev *dev,
  3589. + int n)
  3590. +{
  3591. + do_enable_claim(dev, n, &dev->ic_active1);
  3592. +}
  3593. +
  3594. +void hevc_d_hw_irq_active1_claim(struct hevc_d_dev *dev,
  3595. + struct hevc_d_hw_irq_ent *ient,
  3596. + hevc_d_irq_callback ready_cb, void *ctx)
  3597. +{
  3598. + do_claim(dev, ient, ready_cb, ctx, &dev->ic_active1);
  3599. +}
  3600. +
  3601. +void hevc_d_hw_irq_active1_irq(struct hevc_d_dev *dev,
  3602. + struct hevc_d_hw_irq_ent *ient,
  3603. + hevc_d_irq_callback irq_cb, void *ctx)
  3604. +{
  3605. + pre_irq(dev, ient, irq_cb, ctx, &dev->ic_active1);
  3606. +}
  3607. +
  3608. +void hevc_d_hw_irq_active2_claim(struct hevc_d_dev *dev,
  3609. + struct hevc_d_hw_irq_ent *ient,
  3610. + hevc_d_irq_callback ready_cb, void *ctx)
  3611. +{
  3612. + do_claim(dev, ient, ready_cb, ctx, &dev->ic_active2);
  3613. +}
  3614. +
  3615. +void hevc_d_hw_irq_active2_irq(struct hevc_d_dev *dev,
  3616. + struct hevc_d_hw_irq_ent *ient,
  3617. + hevc_d_irq_callback irq_cb, void *ctx)
  3618. +{
  3619. + pre_irq(dev, ient, irq_cb, ctx, &dev->ic_active2);
  3620. +}
  3621. +
  3622. +int hevc_d_hw_probe(struct hevc_d_dev *dev)
  3623. +{
  3624. + struct rpi_firmware *firmware;
  3625. + struct device_node *node;
  3626. + __u32 irq_stat;
  3627. + int irq_dec;
  3628. + int ret = 0;
  3629. +
  3630. + ictl_init(&dev->ic_active1, HEVC_D_P2BUF_COUNT);
  3631. + ictl_init(&dev->ic_active2, HEVC_D_ICTL_ENABLE_UNLIMITED);
  3632. +
  3633. + dev->base_irq = devm_platform_ioremap_resource_byname(dev->pdev, "intc");
  3634. + if (IS_ERR(dev->base_irq))
  3635. + return PTR_ERR(dev->base_irq);
  3636. +
  3637. + dev->base_h265 = devm_platform_ioremap_resource_byname(dev->pdev, "hevc");
  3638. + if (IS_ERR(dev->base_h265))
  3639. + return PTR_ERR(dev->base_h265);
  3640. +
  3641. + dev->clock = devm_clk_get(&dev->pdev->dev, NULL);
  3642. + if (IS_ERR(dev->clock))
  3643. + return PTR_ERR(dev->clock);
  3644. +
  3645. + node = rpi_firmware_find_node();
  3646. + if (!node)
  3647. + return -EINVAL;
  3648. +
  3649. + firmware = rpi_firmware_get(node);
  3650. + of_node_put(node);
  3651. + if (!firmware)
  3652. + return -EPROBE_DEFER;
  3653. +
  3654. + dev->max_clock_rate = rpi_firmware_clk_get_max_rate(firmware,
  3655. + RPI_FIRMWARE_HEVC_CLK_ID);
  3656. + rpi_firmware_put(firmware);
  3657. +
  3658. + dev->cache_align = dma_get_cache_alignment();
  3659. +
  3660. + /* Disable IRQs & reset anything pending */
  3661. + irq_write(dev, 0,
  3662. + ARG_IC_ICTRL_ACTIVE1_EN_SET | ARG_IC_ICTRL_ACTIVE2_EN_SET);
  3663. + irq_stat = irq_read(dev, 0);
  3664. + irq_write(dev, 0, irq_stat);
  3665. +
  3666. + irq_dec = platform_get_irq(dev->pdev, 0);
  3667. + if (irq_dec <= 0)
  3668. + return irq_dec;
  3669. + ret = devm_request_threaded_irq(dev->dev, irq_dec,
  3670. + hevc_d_irq_irq,
  3671. + hevc_d_irq_thread,
  3672. + 0, dev_name(dev->dev), dev);
  3673. + if (ret)
  3674. + dev_err(dev->dev, "Failed to request IRQ - %d\n", ret);
  3675. +
  3676. + return ret;
  3677. +}
  3678. +
  3679. +void hevc_d_hw_remove(struct hevc_d_dev *dev)
  3680. +{
  3681. + /*
  3682. + * IRQ auto freed on unload so no need to do it here
  3683. + * ioremap auto freed on unload
  3684. + */
  3685. + ictl_uninit(&dev->ic_active1);
  3686. + ictl_uninit(&dev->ic_active2);
  3687. +}
  3688. +
  3689. --- /dev/null
  3690. +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_hw.h
  3691. @@ -0,0 +1,303 @@
  3692. +/* SPDX-License-Identifier: GPL-2.0 */
  3693. +/*
  3694. + * Raspberry Pi HEVC driver
  3695. + *
  3696. + * Copyright (C) 2024 Raspberry Pi Ltd
  3697. + *
  3698. + * Based on the Cedrus VPU driver, that is:
  3699. + *
  3700. + * Copyright (C) 2016 Florent Revest <[email protected]>
  3701. + * Copyright (C) 2018 Paul Kocialkowski <[email protected]>
  3702. + * Copyright (C) 2018 Bootlin
  3703. + */
  3704. +
  3705. +#ifndef _HEVC_D_HW_H_
  3706. +#define _HEVC_D_HW_H_
  3707. +
  3708. +struct hevc_d_hw_irq_ent {
  3709. + struct hevc_d_hw_irq_ent *next;
  3710. + hevc_d_irq_callback cb;
  3711. + void *v;
  3712. +};
  3713. +
  3714. +/* Phase 1 Register offsets */
  3715. +
  3716. +#define RPI_SPS0 0
  3717. +#define RPI_SPS1 4
  3718. +#define RPI_PPS 8
  3719. +#define RPI_SLICE 12
  3720. +#define RPI_TILESTART 16
  3721. +#define RPI_TILEEND 20
  3722. +#define RPI_SLICESTART 24
  3723. +#define RPI_MODE 28
  3724. +#define RPI_LEFT0 32
  3725. +#define RPI_LEFT1 36
  3726. +#define RPI_LEFT2 40
  3727. +#define RPI_LEFT3 44
  3728. +#define RPI_QP 48
  3729. +#define RPI_CONTROL 52
  3730. +#define RPI_STATUS 56
  3731. +#define RPI_VERSION 60
  3732. +#define RPI_BFBASE 64
  3733. +#define RPI_BFNUM 68
  3734. +#define RPI_BFCONTROL 72
  3735. +#define RPI_BFSTATUS 76
  3736. +#define RPI_PUWBASE 80
  3737. +#define RPI_PUWSTRIDE 84
  3738. +#define RPI_COEFFWBASE 88
  3739. +#define RPI_COEFFWSTRIDE 92
  3740. +#define RPI_SLICECMDS 96
  3741. +#define RPI_BEGINTILEEND 100
  3742. +#define RPI_TRANSFER 104
  3743. +#define RPI_CFBASE 108
  3744. +#define RPI_CFNUM 112
  3745. +#define RPI_CFSTATUS 116
  3746. +
  3747. +/* Phase 2 Register offsets */
  3748. +
  3749. +#define RPI_PURBASE 0x8000
  3750. +#define RPI_PURSTRIDE 0x8004
  3751. +#define RPI_COEFFRBASE 0x8008
  3752. +#define RPI_COEFFRSTRIDE 0x800C
  3753. +#define RPI_NUMROWS 0x8010
  3754. +#define RPI_CONFIG2 0x8014
  3755. +#define RPI_OUTYBASE 0x8018
  3756. +#define RPI_OUTYSTRIDE 0x801C
  3757. +#define RPI_OUTCBASE 0x8020
  3758. +#define RPI_OUTCSTRIDE 0x8024
  3759. +#define RPI_STATUS2 0x8028
  3760. +#define RPI_FRAMESIZE 0x802C
  3761. +#define RPI_MVBASE 0x8030
  3762. +#define RPI_MVSTRIDE 0x8034
  3763. +#define RPI_COLBASE 0x8038
  3764. +#define RPI_COLSTRIDE 0x803C
  3765. +#define RPI_CURRPOC 0x8040
  3766. +
  3767. +/*
  3768. + * Write a general register value
  3769. + * Order is unimportant
  3770. + */
  3771. +static inline void apb_write(const struct hevc_d_dev * const dev,
  3772. + const unsigned int offset, const u32 val)
  3773. +{
  3774. + writel_relaxed(val, dev->base_h265 + offset);
  3775. +}
  3776. +
  3777. +/* Write the final register value that actually starts the phase */
  3778. +static inline void apb_write_final(const struct hevc_d_dev * const dev,
  3779. + const unsigned int offset, const u32 val)
  3780. +{
  3781. + writel(val, dev->base_h265 + offset);
  3782. +}
  3783. +
  3784. +static inline u32 apb_read(const struct hevc_d_dev * const dev,
  3785. + const unsigned int offset)
  3786. +{
  3787. + return readl(dev->base_h265 + offset);
  3788. +}
  3789. +
  3790. +static inline void irq_write(const struct hevc_d_dev * const dev,
  3791. + const unsigned int offset, const u32 val)
  3792. +{
  3793. + writel(val, dev->base_irq + offset);
  3794. +}
  3795. +
  3796. +static inline u32 irq_read(const struct hevc_d_dev * const dev,
  3797. + const unsigned int offset)
  3798. +{
  3799. + return readl(dev->base_irq + offset);
  3800. +}
  3801. +
  3802. +static inline void apb_write_vc_addr(const struct hevc_d_dev * const dev,
  3803. + const unsigned int offset,
  3804. + const dma_addr_t a)
  3805. +{
  3806. + apb_write(dev, offset, (u32)(a >> 6));
  3807. +}
  3808. +
  3809. +static inline void apb_write_vc_addr_final(const struct hevc_d_dev * const dev,
  3810. + const unsigned int offset,
  3811. + const dma_addr_t a)
  3812. +{
  3813. + apb_write_final(dev, offset, (u32)(a >> 6));
  3814. +}
  3815. +
  3816. +static inline void apb_write_vc_len(const struct hevc_d_dev * const dev,
  3817. + const unsigned int offset,
  3818. + const unsigned int x)
  3819. +{
  3820. + apb_write(dev, offset, (x + 63) >> 6);
  3821. +}
  3822. +
  3823. +/* *ARG_IC_ICTRL - Interrupt control for ARGON Core*
  3824. + * Offset (byte space) = 40'h2b10000
  3825. + * Physical Address (byte space) = 40'h7eb10000
  3826. + * Verilog Macro Address = `ARG_IC_REG_START + `ARGON_INTCTRL_ICTRL
  3827. + * Reset Value = 32'b100x100x_100xxxxx_xxxxxxx0_x100x100
  3828. + * Access = RW (32-bit only)
  3829. + * Interrupt control logic for ARGON Core.
  3830. + */
  3831. +#define ARG_IC_ICTRL 0
  3832. +
  3833. +/* acc=LWC ACTIVE1_INT FIELD ACCESS: LWC
  3834. + *
  3835. + * Interrupt 1
  3836. + * This is set and held when an hevc_active1 interrupt edge is detected
  3837. + * The polarity of the edge is set by the ACTIVE1_EDGE field
  3838. + * Write a 1 to this bit to clear down the latched interrupt
  3839. + * The latched interrupt is only enabled out onto the interrupt line if
  3840. + * ACTIVE1_EN is set
  3841. + * Reset value is *0* decimal.
  3842. + */
  3843. +#define ARG_IC_ICTRL_ACTIVE1_INT_SET BIT(0)
  3844. +
  3845. +/* ACTIVE1_EDGE Sets the polarity of the interrupt edge detection logic
  3846. + * This logic detects edges of the hevc_active1 line from the argon core
  3847. + * 0 = negedge, 1 = posedge
  3848. + * Reset value is *0* decimal.
  3849. + */
  3850. +#define ARG_IC_ICTRL_ACTIVE1_EDGE_SET BIT(1)
  3851. +
  3852. +/* ACTIVE1_EN Enables ACTIVE1_INT out onto the argon interrupt line.
  3853. + * If this isn't set, the interrupt logic will work but no interrupt will be
  3854. + * set to the interrupt controller
  3855. + * Reset value is *1* decimal.
  3856. + *
  3857. + * [JC] The above appears to be a lie - if unset then b0 is never set
  3858. + */
  3859. +#define ARG_IC_ICTRL_ACTIVE1_EN_SET BIT(2)
  3860. +
  3861. +/* acc=RO ACTIVE1_STATUS FIELD ACCESS: RO
  3862. + *
  3863. + * The current status of the hevc_active1 signal
  3864. + */
  3865. +#define ARG_IC_ICTRL_ACTIVE1_STATUS_SET BIT(3)
  3866. +
  3867. +/* acc=LWC ACTIVE2_INT FIELD ACCESS: LWC
  3868. + *
  3869. + * Interrupt 2
  3870. + * This is set and held when an hevc_active2 interrupt edge is detected
  3871. + * The polarity of the edge is set by the ACTIVE2_EDGE field
  3872. + * Write a 1 to this bit to clear down the latched interrupt
  3873. + * The latched interrupt is only enabled out onto the interrupt line if
  3874. + * ACTIVE2_EN is set
  3875. + * Reset value is *0* decimal.
  3876. + */
  3877. +#define ARG_IC_ICTRL_ACTIVE2_INT_SET BIT(4)
  3878. +
  3879. +/* ACTIVE2_EDGE Sets the polarity of the interrupt edge detection logic
  3880. + * This logic detects edges of the hevc_active2 line from the argon core
  3881. + * 0 = negedge, 1 = posedge
  3882. + * Reset value is *0* decimal.
  3883. + */
  3884. +#define ARG_IC_ICTRL_ACTIVE2_EDGE_SET BIT(5)
  3885. +
  3886. +/* ACTIVE2_EN Enables ACTIVE2_INT out onto the argon interrupt line.
  3887. + * If this isn't set, the interrupt logic will work but no interrupt will be
  3888. + * set to the interrupt controller
  3889. + * Reset value is *1* decimal.
  3890. + */
  3891. +#define ARG_IC_ICTRL_ACTIVE2_EN_SET BIT(6)
  3892. +
  3893. +/* acc=RO ACTIVE2_STATUS FIELD ACCESS: RO
  3894. + *
  3895. + * The current status of the hevc_active2 signal
  3896. + */
  3897. +#define ARG_IC_ICTRL_ACTIVE2_STATUS_SET BIT(7)
  3898. +
  3899. +/* TEST_INT Forces the argon int high for test purposes.
  3900. + * Reset value is *0* decimal.
  3901. + */
  3902. +#define ARG_IC_ICTRL_TEST_INT BIT(8)
  3903. +#define ARG_IC_ICTRL_SPARE BIT(9)
  3904. +
  3905. +/* acc=RO VP9_INTERRUPT_STATUS FIELD ACCESS: RO
  3906. + *
  3907. + * The current status of the vp9_interrupt signal
  3908. + */
  3909. +#define ARG_IC_ICTRL_VP9_INTERRUPT_STATUS BIT(10)
  3910. +
  3911. +/* AIO_INT_ENABLE 1 = Or the AIO int in with the Argon int so the VPU can see
  3912. + * it
  3913. + * 0 = the AIO int is masked. (It should still be connected to the GIC though).
  3914. + */
  3915. +#define ARG_IC_ICTRL_AIO_INT_ENABLE BIT(20)
  3916. +#define ARG_IC_ICTRL_H264_ACTIVE_INT BIT(21)
  3917. +#define ARG_IC_ICTRL_H264_ACTIVE_EDGE BIT(22)
  3918. +#define ARG_IC_ICTRL_H264_ACTIVE_EN BIT(23)
  3919. +#define ARG_IC_ICTRL_H264_ACTIVE_STATUS BIT(24)
  3920. +#define ARG_IC_ICTRL_H264_INTERRUPT_INT BIT(25)
  3921. +#define ARG_IC_ICTRL_H264_INTERRUPT_EDGE BIT(26)
  3922. +#define ARG_IC_ICTRL_H264_INTERRUPT_EN BIT(27)
  3923. +
  3924. +/* acc=RO H264_INTERRUPT_STATUS FIELD ACCESS: RO
  3925. + *
  3926. + * The current status of the h264_interrupt signal
  3927. + */
  3928. +#define ARG_IC_ICTRL_H264_INTERRUPT_STATUS BIT(28)
  3929. +
  3930. +/* acc=LWC VP9_INTERRUPT_INT FIELD ACCESS: LWC
  3931. + *
  3932. + * Interrupt 1
  3933. + * This is set and held when an vp9_interrupt interrupt edge is detected
  3934. + * The polarity of the edge is set by the VP9_INTERRUPT_EDGE field
  3935. + * Write a 1 to this bit to clear down the latched interrupt
  3936. + * The latched interrupt is only enabled out onto the interrupt line if
  3937. + * VP9_INTERRUPT_EN is set
  3938. + * Reset value is *0* decimal.
  3939. + */
  3940. +#define ARG_IC_ICTRL_VP9_INTERRUPT_INT BIT(29)
  3941. +
  3942. +/* VP9_INTERRUPT_EDGE Sets the polarity of the interrupt edge detection logic
  3943. + * This logic detects edges of the vp9_interrupt line from the argon h264 core
  3944. + * 0 = negedge, 1 = posedge
  3945. + * Reset value is *0* decimal.
  3946. + */
  3947. +#define ARG_IC_ICTRL_VP9_INTERRUPT_EDGE BIT(30)
  3948. +
  3949. +/* VP9_INTERRUPT_EN Enables VP9_INTERRUPT_INT out onto the argon interrupt line.
  3950. + * If this isn't set, the interrupt logic will work but no interrupt will be
  3951. + * set to the interrupt controller
  3952. + * Reset value is *1* decimal.
  3953. + */
  3954. +#define ARG_IC_ICTRL_VP9_INTERRUPT_EN BIT(31)
  3955. +
  3956. +/* Bits 19:12, 11 reserved - read ?, write 0 */
  3957. +#define ARG_IC_ICTRL_SET_ZERO_MASK ((0xff << 12) | BIT(11))
  3958. +
  3959. +/* All IRQ bits */
  3960. +#define ARG_IC_ICTRL_ALL_IRQ_MASK (\
  3961. + ARG_IC_ICTRL_VP9_INTERRUPT_INT |\
  3962. + ARG_IC_ICTRL_H264_INTERRUPT_INT |\
  3963. + ARG_IC_ICTRL_ACTIVE1_INT_SET |\
  3964. + ARG_IC_ICTRL_ACTIVE2_INT_SET)
  3965. +
  3966. +/* Regulate claim Q */
  3967. +void hevc_d_hw_irq_active1_enable_claim(struct hevc_d_dev *dev,
  3968. + int n);
  3969. +/* Auto release once all CBs called */
  3970. +void hevc_d_hw_irq_active1_claim(struct hevc_d_dev *dev,
  3971. + struct hevc_d_hw_irq_ent *ient,
  3972. + hevc_d_irq_callback ready_cb, void *ctx);
  3973. +/* May only be called in claim cb */
  3974. +void hevc_d_hw_irq_active1_irq(struct hevc_d_dev *dev,
  3975. + struct hevc_d_hw_irq_ent *ient,
  3976. + hevc_d_irq_callback irq_cb, void *ctx);
  3977. +/* May only be called in irq cb */
  3978. +void hevc_d_hw_irq_active1_thread(struct hevc_d_dev *dev,
  3979. + struct hevc_d_hw_irq_ent *ient,
  3980. + hevc_d_irq_callback thread_cb, void *ctx);
  3981. +
  3982. +/* Auto release once all CBs called */
  3983. +void hevc_d_hw_irq_active2_claim(struct hevc_d_dev *dev,
  3984. + struct hevc_d_hw_irq_ent *ient,
  3985. + hevc_d_irq_callback ready_cb, void *ctx);
  3986. +/* May only be called in claim cb */
  3987. +void hevc_d_hw_irq_active2_irq(struct hevc_d_dev *dev,
  3988. + struct hevc_d_hw_irq_ent *ient,
  3989. + hevc_d_irq_callback irq_cb, void *ctx);
  3990. +
  3991. +int hevc_d_hw_probe(struct hevc_d_dev *dev);
  3992. +void hevc_d_hw_remove(struct hevc_d_dev *dev);
  3993. +
  3994. +#endif
  3995. --- /dev/null
  3996. +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c
  3997. @@ -0,0 +1,688 @@
  3998. +// SPDX-License-Identifier: GPL-2.0
  3999. +/*
  4000. + * Raspberry Pi HEVC driver
  4001. + *
  4002. + * Copyright (C) 2024 Raspberry Pi Ltd
  4003. + *
  4004. + * Based on the Cedrus VPU driver, that is:
  4005. + *
  4006. + * Copyright (C) 2016 Florent Revest <[email protected]>
  4007. + * Copyright (C) 2018 Paul Kocialkowski <[email protected]>
  4008. + * Copyright (C) 2018 Bootlin
  4009. + */
  4010. +
  4011. +#include <media/videobuf2-dma-contig.h>
  4012. +#include <media/v4l2-device.h>
  4013. +#include <media/v4l2-ioctl.h>
  4014. +#include <media/v4l2-event.h>
  4015. +#include <media/v4l2-mem2mem.h>
  4016. +
  4017. +#include "hevc_d.h"
  4018. +#include "hevc_d_h265.h"
  4019. +#include "hevc_d_hw.h"
  4020. +#include "hevc_d_video.h"
  4021. +
  4022. +#define HEVC_D_DECODE_SRC BIT(0)
  4023. +#define HEVC_D_DECODE_DST BIT(1)
  4024. +
  4025. +#define HEVC_D_MIN_WIDTH 16U
  4026. +#define HEVC_D_MIN_HEIGHT 16U
  4027. +#define HEVC_D_DEFAULT_WIDTH 1920U
  4028. +#define HEVC_D_DEFAULT_HEIGHT 1088U
  4029. +#define HEVC_D_MAX_WIDTH 4096U
  4030. +#define HEVC_D_MAX_HEIGHT 4096U
  4031. +
  4032. +static inline struct hevc_d_ctx *hevc_d_file2ctx(struct file *file)
  4033. +{
  4034. + return container_of(file->private_data, struct hevc_d_ctx, fh);
  4035. +}
  4036. +
  4037. +/* constrain x to y,y*2 */
  4038. +static inline unsigned int constrain2x(unsigned int x, unsigned int y)
  4039. +{
  4040. + return (x < y) ?
  4041. + y :
  4042. + (x > y * 2) ? y : x;
  4043. +}
  4044. +
  4045. +size_t hevc_d_round_up_size(const size_t x)
  4046. +{
  4047. + /* Admit no size < 256 */
  4048. + const unsigned int n = x < 256 ? 8 : ilog2(x);
  4049. +
  4050. + return x >= (3 << n) ? 4 << n : (3 << n);
  4051. +}
  4052. +
  4053. +size_t hevc_d_bit_buf_size(unsigned int w, unsigned int h, unsigned int bits_minus8)
  4054. +{
  4055. + const size_t wxh = w * h;
  4056. + size_t bits_alloc;
  4057. +
  4058. + /* Annex A gives a min compression of 2 @ lvl 3.1
  4059. + * (wxh <= 983040) and min 4 thereafter but avoid
  4060. + * the odity of 983041 having a lower limit than
  4061. + * 983040.
  4062. + * Multiply by 3/2 for 4:2:0
  4063. + */
  4064. + bits_alloc = wxh < 983040 ? wxh * 3 / 4 :
  4065. + wxh < 983040 * 2 ? 983040 * 3 / 4 :
  4066. + wxh * 3 / 8;
  4067. + /* Allow for bit depth */
  4068. + bits_alloc += (bits_alloc * bits_minus8) / 8;
  4069. + return hevc_d_round_up_size(bits_alloc);
  4070. +}
  4071. +
  4072. +void hevc_d_prepare_src_format(struct v4l2_pix_format_mplane *pix_fmt)
  4073. +{
  4074. + size_t size;
  4075. + u32 w;
  4076. + u32 h;
  4077. +
  4078. + w = pix_fmt->width;
  4079. + h = pix_fmt->height;
  4080. + if (!w || !h) {
  4081. + w = HEVC_D_DEFAULT_WIDTH;
  4082. + h = HEVC_D_DEFAULT_HEIGHT;
  4083. + }
  4084. + if (w > HEVC_D_MAX_WIDTH)
  4085. + w = HEVC_D_MAX_WIDTH;
  4086. + if (h > HEVC_D_MAX_HEIGHT)
  4087. + h = HEVC_D_MAX_HEIGHT;
  4088. +
  4089. + if (!pix_fmt->plane_fmt[0].sizeimage ||
  4090. + pix_fmt->plane_fmt[0].sizeimage > SZ_32M) {
  4091. + /* Unspecified or way too big - pick max for size */
  4092. + size = hevc_d_bit_buf_size(w, h, 2);
  4093. + }
  4094. + /* Set a minimum */
  4095. + size = max_t(u32, SZ_4K, pix_fmt->plane_fmt[0].sizeimage);
  4096. +
  4097. + pix_fmt->pixelformat = V4L2_PIX_FMT_HEVC_SLICE;
  4098. + pix_fmt->width = w;
  4099. + pix_fmt->height = h;
  4100. + pix_fmt->num_planes = 1;
  4101. + pix_fmt->field = V4L2_FIELD_NONE;
  4102. + /* Zero bytes per line for encoded source. */
  4103. + pix_fmt->plane_fmt[0].bytesperline = 0;
  4104. + pix_fmt->plane_fmt[0].sizeimage = size;
  4105. +}
  4106. +
  4107. +/* Take any pix_format and make it valid */
  4108. +static void hevc_d_prepare_dst_format(struct v4l2_pix_format_mplane *pix_fmt)
  4109. +{
  4110. + unsigned int width = pix_fmt->width;
  4111. + unsigned int height = pix_fmt->height;
  4112. + unsigned int sizeimage = pix_fmt->plane_fmt[0].sizeimage;
  4113. + unsigned int bytesperline = pix_fmt->plane_fmt[0].bytesperline;
  4114. +
  4115. + if (!width)
  4116. + width = HEVC_D_DEFAULT_WIDTH;
  4117. + if (width > HEVC_D_MAX_WIDTH)
  4118. + width = HEVC_D_MAX_WIDTH;
  4119. + if (!height)
  4120. + height = HEVC_D_DEFAULT_HEIGHT;
  4121. + if (height > HEVC_D_MAX_HEIGHT)
  4122. + height = HEVC_D_MAX_HEIGHT;
  4123. +
  4124. + /* For column formats set bytesperline to column height (stride2) */
  4125. + switch (pix_fmt->pixelformat) {
  4126. + default:
  4127. + pix_fmt->pixelformat = V4L2_PIX_FMT_NV12MT_COL128;
  4128. + fallthrough;
  4129. + case V4L2_PIX_FMT_NV12MT_COL128:
  4130. + /* Width rounds up to columns */
  4131. + width = ALIGN(width, 128);
  4132. + height = ALIGN(height, 8);
  4133. +
  4134. + /* column height is sizeimage / bytesperline */
  4135. + bytesperline = width;
  4136. + sizeimage = bytesperline * height;
  4137. + break;
  4138. +
  4139. + case V4L2_PIX_FMT_NV12MT_10_COL128:
  4140. + /* width in pixels (3 pels = 4 bytes) rounded to 128 byte
  4141. + * columns
  4142. + */
  4143. + width = ALIGN(((width + 2) / 3), 32) * 3;
  4144. + height = ALIGN(height, 8);
  4145. +
  4146. + /* column height is sizeimage / bytesperline */
  4147. + bytesperline = width * 4 / 3;
  4148. + sizeimage = bytesperline * height;
  4149. + break;
  4150. + }
  4151. +
  4152. + pix_fmt->width = width;
  4153. + pix_fmt->height = height;
  4154. +
  4155. + pix_fmt->field = V4L2_FIELD_NONE;
  4156. + pix_fmt->plane_fmt[0].bytesperline = bytesperline;
  4157. + pix_fmt->plane_fmt[0].sizeimage = sizeimage;
  4158. + pix_fmt->plane_fmt[1].bytesperline = bytesperline;
  4159. + pix_fmt->plane_fmt[1].sizeimage = sizeimage / 2;
  4160. + pix_fmt->num_planes = 2;
  4161. +}
  4162. +
  4163. +static int hevc_d_querycap(struct file *file, void *priv,
  4164. + struct v4l2_capability *cap)
  4165. +{
  4166. + strscpy(cap->driver, HEVC_D_NAME, sizeof(cap->driver));
  4167. + strscpy(cap->card, HEVC_D_NAME, sizeof(cap->card));
  4168. + snprintf(cap->bus_info, sizeof(cap->bus_info),
  4169. + "platform:%s", HEVC_D_NAME);
  4170. +
  4171. + return 0;
  4172. +}
  4173. +
  4174. +static int hevc_d_enum_fmt_vid_out(struct file *file, void *priv,
  4175. + struct v4l2_fmtdesc *f)
  4176. +{
  4177. + /*
  4178. + * Input formats
  4179. + * H.265 Slice only
  4180. + */
  4181. + if (f->index == 0) {
  4182. + f->pixelformat = V4L2_PIX_FMT_HEVC_SLICE;
  4183. + return 0;
  4184. + }
  4185. +
  4186. + return -EINVAL;
  4187. +}
  4188. +
  4189. +static int hevc_d_hevc_validate_sps(const struct v4l2_ctrl_hevc_sps * const sps)
  4190. +{
  4191. + const unsigned int ctb_log2_size_y =
  4192. + sps->log2_min_luma_coding_block_size_minus3 + 3 +
  4193. + sps->log2_diff_max_min_luma_coding_block_size;
  4194. + const unsigned int min_tb_log2_size_y =
  4195. + sps->log2_min_luma_transform_block_size_minus2 + 2;
  4196. + const unsigned int max_tb_log2_size_y = min_tb_log2_size_y +
  4197. + sps->log2_diff_max_min_luma_transform_block_size;
  4198. +
  4199. + /* Local limitations */
  4200. + if (sps->pic_width_in_luma_samples < 32 ||
  4201. + sps->pic_width_in_luma_samples > 4096)
  4202. + return 0;
  4203. + if (sps->pic_height_in_luma_samples < 32 ||
  4204. + sps->pic_height_in_luma_samples > 4096)
  4205. + return 0;
  4206. + if (!(sps->bit_depth_luma_minus8 == 0 ||
  4207. + sps->bit_depth_luma_minus8 == 2))
  4208. + return 0;
  4209. + if (sps->bit_depth_luma_minus8 != sps->bit_depth_chroma_minus8)
  4210. + return 0;
  4211. + if (sps->chroma_format_idc != 1)
  4212. + return 0;
  4213. +
  4214. + /* Limits from H.265 7.4.3.2.1 */
  4215. + if (sps->log2_max_pic_order_cnt_lsb_minus4 > 12)
  4216. + return 0;
  4217. + if (sps->sps_max_dec_pic_buffering_minus1 > 15)
  4218. + return 0;
  4219. + if (sps->sps_max_num_reorder_pics >
  4220. + sps->sps_max_dec_pic_buffering_minus1)
  4221. + return 0;
  4222. + if (ctb_log2_size_y > 6)
  4223. + return 0;
  4224. + if (max_tb_log2_size_y > 5)
  4225. + return 0;
  4226. + if (max_tb_log2_size_y > ctb_log2_size_y)
  4227. + return 0;
  4228. + if (sps->max_transform_hierarchy_depth_inter >
  4229. + (ctb_log2_size_y - min_tb_log2_size_y))
  4230. + return 0;
  4231. + if (sps->max_transform_hierarchy_depth_intra >
  4232. + (ctb_log2_size_y - min_tb_log2_size_y))
  4233. + return 0;
  4234. + /* Check pcm stuff */
  4235. + if (sps->num_short_term_ref_pic_sets > 64)
  4236. + return 0;
  4237. + if (sps->num_long_term_ref_pics_sps > 32)
  4238. + return 0;
  4239. + return 1;
  4240. +}
  4241. +
  4242. +static u32 pixelformat_from_sps(const struct v4l2_ctrl_hevc_sps * const sps,
  4243. + const int index)
  4244. +{
  4245. + u32 pf = 0;
  4246. +
  4247. + if (!is_sps_set(sps) || !hevc_d_hevc_validate_sps(sps)) {
  4248. + /* Treat this as an error? For now return both */
  4249. + if (index == 0)
  4250. + pf = V4L2_PIX_FMT_NV12MT_COL128;
  4251. + else if (index == 1)
  4252. + pf = V4L2_PIX_FMT_NV12MT_10_COL128;
  4253. + } else if (index == 0) {
  4254. + if (sps->bit_depth_luma_minus8 == 0)
  4255. + pf = V4L2_PIX_FMT_NV12MT_COL128;
  4256. + else if (sps->bit_depth_luma_minus8 == 2)
  4257. + pf = V4L2_PIX_FMT_NV12MT_10_COL128;
  4258. + }
  4259. +
  4260. + return pf;
  4261. +}
  4262. +
  4263. +static void copy_color(struct v4l2_pix_format_mplane *d,
  4264. + const struct v4l2_pix_format_mplane *s)
  4265. +{
  4266. + d->colorspace = s->colorspace;
  4267. + d->xfer_func = s->xfer_func;
  4268. + d->ycbcr_enc = s->ycbcr_enc;
  4269. + d->quantization = s->quantization;
  4270. +}
  4271. +
  4272. +static struct v4l2_pix_format_mplane
  4273. +hevc_d_hevc_default_dst_fmt(struct hevc_d_ctx * const ctx)
  4274. +{
  4275. + const struct v4l2_ctrl_hevc_sps * const sps =
  4276. + hevc_d_find_control_data(ctx, V4L2_CID_STATELESS_HEVC_SPS);
  4277. + struct v4l2_pix_format_mplane pix_fmt;
  4278. +
  4279. + memset(&pix_fmt, 0, sizeof(pix_fmt));
  4280. + if (is_sps_set(sps)) {
  4281. + pix_fmt.width = sps->pic_width_in_luma_samples;
  4282. + pix_fmt.height = sps->pic_height_in_luma_samples;
  4283. + pix_fmt.pixelformat = pixelformat_from_sps(sps, 0);
  4284. + }
  4285. +
  4286. + hevc_d_prepare_dst_format(&pix_fmt);
  4287. + copy_color(&pix_fmt, &ctx->src_fmt);
  4288. +
  4289. + return pix_fmt;
  4290. +}
  4291. +
  4292. +static u32 hevc_d_hevc_get_dst_pixelformat(struct hevc_d_ctx * const ctx,
  4293. + const int index)
  4294. +{
  4295. + const struct v4l2_ctrl_hevc_sps * const sps =
  4296. + hevc_d_find_control_data(ctx, V4L2_CID_STATELESS_HEVC_SPS);
  4297. +
  4298. + return pixelformat_from_sps(sps, index);
  4299. +}
  4300. +
  4301. +static int hevc_d_enum_fmt_vid_cap(struct file *file, void *priv,
  4302. + struct v4l2_fmtdesc *f)
  4303. +{
  4304. + struct hevc_d_ctx * const ctx = hevc_d_file2ctx(file);
  4305. +
  4306. + const u32 pf = hevc_d_hevc_get_dst_pixelformat(ctx, f->index);
  4307. +
  4308. + if (pf == 0)
  4309. + return -EINVAL;
  4310. +
  4311. + f->pixelformat = pf;
  4312. + return 0;
  4313. +}
  4314. +
  4315. +/*
  4316. + * get dst format - sets it to default if otherwise unset
  4317. + * returns a pointer to the struct as a convienience
  4318. + */
  4319. +static struct v4l2_pix_format_mplane *get_dst_fmt(struct hevc_d_ctx *const ctx)
  4320. +{
  4321. + if (!ctx->dst_fmt_set)
  4322. + ctx->dst_fmt = hevc_d_hevc_default_dst_fmt(ctx);
  4323. + return &ctx->dst_fmt;
  4324. +}
  4325. +
  4326. +static int hevc_d_g_fmt_vid_cap(struct file *file, void *priv,
  4327. + struct v4l2_format *f)
  4328. +{
  4329. + struct hevc_d_ctx *ctx = hevc_d_file2ctx(file);
  4330. +
  4331. + f->fmt.pix_mp = *get_dst_fmt(ctx);
  4332. + return 0;
  4333. +}
  4334. +
  4335. +static int hevc_d_g_fmt_vid_out(struct file *file, void *priv,
  4336. + struct v4l2_format *f)
  4337. +{
  4338. + struct hevc_d_ctx *ctx = hevc_d_file2ctx(file);
  4339. +
  4340. + f->fmt.pix_mp = ctx->src_fmt;
  4341. + return 0;
  4342. +}
  4343. +
  4344. +static int hevc_d_try_fmt_vid_cap(struct file *file, void *priv,
  4345. + struct v4l2_format *f)
  4346. +{
  4347. + struct hevc_d_ctx *ctx = hevc_d_file2ctx(file);
  4348. + const struct v4l2_ctrl_hevc_sps * const sps =
  4349. + hevc_d_find_control_data(ctx, V4L2_CID_STATELESS_HEVC_SPS);
  4350. + u32 pixelformat;
  4351. + int i;
  4352. +
  4353. + for (i = 0; (pixelformat = pixelformat_from_sps(sps, i)) != 0; i++) {
  4354. + if (f->fmt.pix_mp.pixelformat == pixelformat)
  4355. + break;
  4356. + }
  4357. +
  4358. + /*
  4359. + * We don't have any way of finding out colourspace so believe
  4360. + * anything we are told - take anything set in src as a default
  4361. + */
  4362. + if (f->fmt.pix_mp.colorspace == V4L2_COLORSPACE_DEFAULT)
  4363. + copy_color(&f->fmt.pix_mp, &ctx->src_fmt);
  4364. +
  4365. + f->fmt.pix_mp.pixelformat = pixelformat;
  4366. + hevc_d_prepare_dst_format(&f->fmt.pix_mp);
  4367. + return 0;
  4368. +}
  4369. +
  4370. +static int hevc_d_try_fmt_vid_out(struct file *file, void *priv,
  4371. + struct v4l2_format *f)
  4372. +{
  4373. + hevc_d_prepare_src_format(&f->fmt.pix_mp);
  4374. + return 0;
  4375. +}
  4376. +
  4377. +static int hevc_d_s_fmt_vid_cap(struct file *file, void *priv,
  4378. + struct v4l2_format *f)
  4379. +{
  4380. + struct hevc_d_ctx *ctx = hevc_d_file2ctx(file);
  4381. + struct vb2_queue *vq;
  4382. + int ret;
  4383. +
  4384. + vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  4385. + if (vb2_is_busy(vq))
  4386. + return -EBUSY;
  4387. +
  4388. + ret = hevc_d_try_fmt_vid_cap(file, priv, f);
  4389. + if (ret)
  4390. + return ret;
  4391. +
  4392. + ctx->dst_fmt = f->fmt.pix_mp;
  4393. + ctx->dst_fmt_set = 1;
  4394. +
  4395. + return 0;
  4396. +}
  4397. +
  4398. +static int hevc_d_s_fmt_vid_out(struct file *file, void *priv,
  4399. + struct v4l2_format *f)
  4400. +{
  4401. + struct hevc_d_ctx *ctx = hevc_d_file2ctx(file);
  4402. + struct vb2_queue *vq;
  4403. + int ret;
  4404. +
  4405. + vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  4406. + if (vb2_is_busy(vq))
  4407. + return -EBUSY;
  4408. +
  4409. + ret = hevc_d_try_fmt_vid_out(file, priv, f);
  4410. + if (ret)
  4411. + return ret;
  4412. +
  4413. + ctx->src_fmt = f->fmt.pix_mp;
  4414. + ctx->dst_fmt_set = 0; /* Setting src invalidates dst */
  4415. +
  4416. + /* Propagate colorspace information to capture. */
  4417. + copy_color(&ctx->dst_fmt, &f->fmt.pix_mp);
  4418. + return 0;
  4419. +}
  4420. +
  4421. +const struct v4l2_ioctl_ops hevc_d_ioctl_ops = {
  4422. + .vidioc_querycap = hevc_d_querycap,
  4423. +
  4424. + .vidioc_enum_fmt_vid_cap = hevc_d_enum_fmt_vid_cap,
  4425. + .vidioc_g_fmt_vid_cap_mplane = hevc_d_g_fmt_vid_cap,
  4426. + .vidioc_try_fmt_vid_cap_mplane = hevc_d_try_fmt_vid_cap,
  4427. + .vidioc_s_fmt_vid_cap_mplane = hevc_d_s_fmt_vid_cap,
  4428. +
  4429. + .vidioc_enum_fmt_vid_out = hevc_d_enum_fmt_vid_out,
  4430. + .vidioc_g_fmt_vid_out_mplane = hevc_d_g_fmt_vid_out,
  4431. + .vidioc_try_fmt_vid_out_mplane = hevc_d_try_fmt_vid_out,
  4432. + .vidioc_s_fmt_vid_out_mplane = hevc_d_s_fmt_vid_out,
  4433. +
  4434. + .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  4435. + .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  4436. + .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  4437. + .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  4438. + .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
  4439. + .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
  4440. + .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  4441. +
  4442. + .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  4443. + .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  4444. +
  4445. + .vidioc_try_decoder_cmd = v4l2_m2m_ioctl_stateless_try_decoder_cmd,
  4446. + .vidioc_decoder_cmd = v4l2_m2m_ioctl_stateless_decoder_cmd,
  4447. +
  4448. + .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  4449. + .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  4450. +};
  4451. +
  4452. +static int hevc_d_queue_setup(struct vb2_queue *vq, unsigned int *nbufs,
  4453. + unsigned int *nplanes, unsigned int sizes[],
  4454. + struct device *alloc_devs[])
  4455. +{
  4456. + struct hevc_d_ctx *ctx = vb2_get_drv_priv(vq);
  4457. + struct v4l2_pix_format_mplane *pix_fmt;
  4458. + int expected_nplanes;
  4459. +
  4460. + if (V4L2_TYPE_IS_OUTPUT(vq->type)) {
  4461. + pix_fmt = &ctx->src_fmt;
  4462. + expected_nplanes = 1;
  4463. + } else {
  4464. + pix_fmt = get_dst_fmt(ctx);
  4465. + expected_nplanes = 2;
  4466. + }
  4467. +
  4468. + if (*nplanes) {
  4469. + if (*nplanes != expected_nplanes ||
  4470. + sizes[0] < pix_fmt->plane_fmt[0].sizeimage ||
  4471. + sizes[1] < pix_fmt->plane_fmt[1].sizeimage)
  4472. + return -EINVAL;
  4473. + } else {
  4474. + sizes[0] = pix_fmt->plane_fmt[0].sizeimage;
  4475. + if (V4L2_TYPE_IS_OUTPUT(vq->type)) {
  4476. + *nplanes = 1;
  4477. + } else {
  4478. + sizes[1] = pix_fmt->plane_fmt[1].sizeimage;
  4479. + *nplanes = 2;
  4480. + }
  4481. + }
  4482. +
  4483. + return 0;
  4484. +}
  4485. +
  4486. +static void hevc_d_queue_cleanup(struct vb2_queue *vq, u32 state)
  4487. +{
  4488. + struct hevc_d_ctx *ctx = vb2_get_drv_priv(vq);
  4489. + struct vb2_v4l2_buffer *vbuf;
  4490. +
  4491. + for (;;) {
  4492. + if (V4L2_TYPE_IS_OUTPUT(vq->type))
  4493. + vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  4494. + else
  4495. + vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  4496. +
  4497. + if (!vbuf)
  4498. + return;
  4499. +
  4500. + v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
  4501. + &ctx->hdl);
  4502. + v4l2_m2m_buf_done(vbuf, state);
  4503. + }
  4504. +}
  4505. +
  4506. +static int hevc_d_buf_out_validate(struct vb2_buffer *vb)
  4507. +{
  4508. + struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  4509. +
  4510. + vbuf->field = V4L2_FIELD_NONE;
  4511. + return 0;
  4512. +}
  4513. +
  4514. +static int hevc_d_buf_prepare(struct vb2_buffer *vb)
  4515. +{
  4516. + struct vb2_queue *vq = vb->vb2_queue;
  4517. + struct hevc_d_ctx *ctx = vb2_get_drv_priv(vq);
  4518. + struct v4l2_pix_format_mplane *pix_fmt;
  4519. +
  4520. + if (V4L2_TYPE_IS_OUTPUT(vq->type))
  4521. + pix_fmt = &ctx->src_fmt;
  4522. + else
  4523. + pix_fmt = &ctx->dst_fmt;
  4524. +
  4525. + if (vb2_plane_size(vb, 0) < pix_fmt->plane_fmt[0].sizeimage ||
  4526. + vb2_plane_size(vb, 1) < pix_fmt->plane_fmt[1].sizeimage)
  4527. + return -EINVAL;
  4528. +
  4529. + vb2_set_plane_payload(vb, 0, pix_fmt->plane_fmt[0].sizeimage);
  4530. + vb2_set_plane_payload(vb, 1, pix_fmt->plane_fmt[1].sizeimage);
  4531. +
  4532. + return 0;
  4533. +}
  4534. +
  4535. +/* Only stops the clock if streaom off on both output & capture */
  4536. +static void stop_clock(struct hevc_d_dev *dev, struct hevc_d_ctx *ctx)
  4537. +{
  4538. + if (ctx->src_stream_on ||
  4539. + ctx->dst_stream_on)
  4540. + return;
  4541. +
  4542. + clk_set_min_rate(dev->clock, 0);
  4543. + clk_disable_unprepare(dev->clock);
  4544. +}
  4545. +
  4546. +/* Always starts the clock if it isn't already on this ctx */
  4547. +static int start_clock(struct hevc_d_dev *dev, struct hevc_d_ctx *ctx)
  4548. +{
  4549. + int rv;
  4550. +
  4551. + rv = clk_set_min_rate(dev->clock, dev->max_clock_rate);
  4552. + if (rv) {
  4553. + dev_err(dev->dev, "Failed to set clock rate\n");
  4554. + return rv;
  4555. + }
  4556. +
  4557. + rv = clk_prepare_enable(dev->clock);
  4558. + if (rv) {
  4559. + dev_err(dev->dev, "Failed to enable clock\n");
  4560. + return rv;
  4561. + }
  4562. +
  4563. + return 0;
  4564. +}
  4565. +
  4566. +static int hevc_d_start_streaming(struct vb2_queue *vq, unsigned int count)
  4567. +{
  4568. + struct hevc_d_ctx *ctx = vb2_get_drv_priv(vq);
  4569. + struct hevc_d_dev *dev = ctx->dev;
  4570. + int ret = 0;
  4571. +
  4572. + if (!V4L2_TYPE_IS_OUTPUT(vq->type)) {
  4573. + ctx->dst_stream_on = 1;
  4574. + goto ok;
  4575. + }
  4576. +
  4577. + if (ctx->src_fmt.pixelformat != V4L2_PIX_FMT_HEVC_SLICE) {
  4578. + ret = -EINVAL;
  4579. + goto fail_cleanup;
  4580. + }
  4581. +
  4582. + if (ctx->src_stream_on)
  4583. + goto ok;
  4584. +
  4585. + ret = start_clock(dev, ctx);
  4586. + if (ret)
  4587. + goto fail_cleanup;
  4588. +
  4589. + ret = hevc_d_h265_start(ctx);
  4590. + if (ret)
  4591. + goto fail_stop_clock;
  4592. +
  4593. + ctx->src_stream_on = 1;
  4594. +ok:
  4595. + return 0;
  4596. +
  4597. +fail_stop_clock:
  4598. + stop_clock(dev, ctx);
  4599. +fail_cleanup:
  4600. + v4l2_err(&dev->v4l2_dev, "%s: qtype=%d: FAIL\n", __func__, vq->type);
  4601. + hevc_d_queue_cleanup(vq, VB2_BUF_STATE_QUEUED);
  4602. + return ret;
  4603. +}
  4604. +
  4605. +static void hevc_d_stop_streaming(struct vb2_queue *vq)
  4606. +{
  4607. + struct hevc_d_ctx *ctx = vb2_get_drv_priv(vq);
  4608. + struct hevc_d_dev *dev = ctx->dev;
  4609. +
  4610. + if (V4L2_TYPE_IS_OUTPUT(vq->type)) {
  4611. + ctx->src_stream_on = 0;
  4612. + hevc_d_h265_stop(ctx);
  4613. + } else {
  4614. + ctx->dst_stream_on = 0;
  4615. + }
  4616. +
  4617. + hevc_d_queue_cleanup(vq, VB2_BUF_STATE_ERROR);
  4618. +
  4619. + vb2_wait_for_all_buffers(vq);
  4620. +
  4621. + stop_clock(dev, ctx);
  4622. +}
  4623. +
  4624. +static void hevc_d_buf_queue(struct vb2_buffer *vb)
  4625. +{
  4626. + struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  4627. + struct hevc_d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  4628. +
  4629. + v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  4630. +}
  4631. +
  4632. +static void hevc_d_buf_request_complete(struct vb2_buffer *vb)
  4633. +{
  4634. + struct hevc_d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  4635. +
  4636. + v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->hdl);
  4637. +}
  4638. +
  4639. +static const struct vb2_ops hevc_d_qops = {
  4640. + .queue_setup = hevc_d_queue_setup,
  4641. + .buf_prepare = hevc_d_buf_prepare,
  4642. + .buf_queue = hevc_d_buf_queue,
  4643. + .buf_out_validate = hevc_d_buf_out_validate,
  4644. + .buf_request_complete = hevc_d_buf_request_complete,
  4645. + .start_streaming = hevc_d_start_streaming,
  4646. + .stop_streaming = hevc_d_stop_streaming,
  4647. + .wait_prepare = vb2_ops_wait_prepare,
  4648. + .wait_finish = vb2_ops_wait_finish,
  4649. +};
  4650. +
  4651. +int hevc_d_queue_init(void *priv, struct vb2_queue *src_vq,
  4652. + struct vb2_queue *dst_vq)
  4653. +{
  4654. + struct hevc_d_ctx *ctx = priv;
  4655. + int ret;
  4656. +
  4657. + src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  4658. + src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  4659. + src_vq->drv_priv = ctx;
  4660. + src_vq->buf_struct_size = sizeof(struct hevc_d_buffer);
  4661. + src_vq->ops = &hevc_d_qops;
  4662. + src_vq->mem_ops = &vb2_dma_contig_memops;
  4663. + src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  4664. + src_vq->lock = &ctx->ctx_mutex;
  4665. + src_vq->dev = ctx->dev->dev;
  4666. + src_vq->supports_requests = true;
  4667. + src_vq->requires_requests = true;
  4668. +
  4669. + ret = vb2_queue_init(src_vq);
  4670. + if (ret)
  4671. + return ret;
  4672. +
  4673. + dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  4674. + dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  4675. + dst_vq->drv_priv = ctx;
  4676. + dst_vq->buf_struct_size = sizeof(struct hevc_d_buffer);
  4677. + dst_vq->min_queued_buffers = 1;
  4678. + dst_vq->ops = &hevc_d_qops;
  4679. + dst_vq->mem_ops = &vb2_dma_contig_memops;
  4680. + dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  4681. + dst_vq->lock = &ctx->ctx_mutex;
  4682. + dst_vq->dev = ctx->dev->dev;
  4683. +
  4684. + return vb2_queue_init(dst_vq);
  4685. +}
  4686. --- /dev/null
  4687. +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.h
  4688. @@ -0,0 +1,38 @@
  4689. +/* SPDX-License-Identifier: GPL-2.0 */
  4690. +/*
  4691. + * Raspberry Pi HEVC driver
  4692. + *
  4693. + * Copyright (C) 2024 Raspberry Pi Ltd
  4694. + *
  4695. + * Based on the Cedrus VPU driver, that is:
  4696. + *
  4697. + * Copyright (C) 2016 Florent Revest <[email protected]>
  4698. + * Copyright (C) 2018 Paul Kocialkowski <[email protected]>
  4699. + * Copyright (C) 2018 Bootlin
  4700. + */
  4701. +
  4702. +#ifndef _HEVC_D_VIDEO_H_
  4703. +#define _HEVC_D_VIDEO_H_
  4704. +
  4705. +struct hevc_d_format {
  4706. + u32 pixelformat;
  4707. + u32 directions;
  4708. + unsigned int capabilities;
  4709. +};
  4710. +
  4711. +static inline int is_sps_set(const struct v4l2_ctrl_hevc_sps * const sps)
  4712. +{
  4713. + return sps && sps->pic_width_in_luma_samples;
  4714. +}
  4715. +
  4716. +extern const struct v4l2_ioctl_ops hevc_d_ioctl_ops;
  4717. +
  4718. +int hevc_d_queue_init(void *priv, struct vb2_queue *src_vq,
  4719. + struct vb2_queue *dst_vq);
  4720. +
  4721. +size_t hevc_d_bit_buf_size(unsigned int w, unsigned int h, unsigned int bits_minus8);
  4722. +size_t hevc_d_round_up_size(const size_t x);
  4723. +
  4724. +void hevc_d_prepare_src_format(struct v4l2_pix_format_mplane *pix_fmt);
  4725. +
  4726. +#endif