cam_isp_context.c 189 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/debugfs.h>
  6. #include <linux/videodev2.h>
  7. #include <linux/slab.h>
  8. #include <linux/uaccess.h>
  9. #include <linux/ratelimit.h>
  10. #include "cam_mem_mgr.h"
  11. #include "cam_sync_api.h"
  12. #include "cam_req_mgr_dev.h"
  13. #include "cam_trace.h"
  14. #include "cam_debug_util.h"
  15. #include "cam_packet_util.h"
  16. #include "cam_context_utils.h"
  17. #include "cam_cdm_util.h"
  18. #include "cam_isp_context.h"
  19. #include "cam_common_util.h"
  20. #include "cam_req_mgr_debug.h"
  21. #include "cam_cpas_api.h"
  22. static const char isp_dev_name[] = "cam-isp";
  23. static struct cam_isp_ctx_debug isp_ctx_debug;
  24. #define INC_HEAD(head, max_entries, ret) \
  25. div_u64_rem(atomic64_add_return(1, head),\
  26. max_entries, (ret))
  27. static int cam_isp_context_dump_requests(void *data,
  28. struct cam_smmu_pf_info *pf_info);
  29. static int __cam_isp_ctx_start_dev_in_ready(struct cam_context *ctx,
  30. struct cam_start_stop_dev_cmd *cmd);
  31. static const char *__cam_isp_evt_val_to_type(
  32. uint32_t evt_id)
  33. {
  34. switch (evt_id) {
  35. case CAM_ISP_CTX_EVENT_SUBMIT:
  36. return "SUBMIT";
  37. case CAM_ISP_CTX_EVENT_APPLY:
  38. return "APPLY";
  39. case CAM_ISP_CTX_EVENT_EPOCH:
  40. return "EPOCH";
  41. case CAM_ISP_CTX_EVENT_RUP:
  42. return "RUP";
  43. case CAM_ISP_CTX_EVENT_BUFDONE:
  44. return "BUFDONE";
  45. default:
  46. return "CAM_ISP_EVENT_INVALID";
  47. }
  48. }
  49. static void __cam_isp_ctx_update_event_record(
  50. struct cam_isp_context *ctx_isp,
  51. enum cam_isp_ctx_event event,
  52. struct cam_ctx_request *req)
  53. {
  54. int iterator = 0;
  55. ktime_t cur_time;
  56. struct cam_isp_ctx_req *req_isp;
  57. if (!ctx_isp) {
  58. CAM_ERR(CAM_ISP, "Invalid Args");
  59. return;
  60. }
  61. switch (event) {
  62. case CAM_ISP_CTX_EVENT_EPOCH:
  63. case CAM_ISP_CTX_EVENT_RUP:
  64. case CAM_ISP_CTX_EVENT_BUFDONE:
  65. break;
  66. case CAM_ISP_CTX_EVENT_SUBMIT:
  67. case CAM_ISP_CTX_EVENT_APPLY:
  68. if (!req) {
  69. CAM_ERR(CAM_ISP, "Invalid arg for event %d", event);
  70. return;
  71. }
  72. break;
  73. default:
  74. break;
  75. }
  76. INC_HEAD(&ctx_isp->event_record_head[event],
  77. CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES, &iterator);
  78. cur_time = ktime_get();
  79. if (req) {
  80. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  81. ctx_isp->event_record[event][iterator].req_id =
  82. req->request_id;
  83. req_isp->event_timestamp[event] = cur_time;
  84. } else {
  85. ctx_isp->event_record[event][iterator].req_id = 0;
  86. }
  87. ctx_isp->event_record[event][iterator].timestamp = cur_time;
  88. }
  89. static int cam_isp_ctx_sync_signal_on_buf_done_ready(
  90. struct cam_context *ctx,
  91. struct cam_ctx_request *req,
  92. uint32_t status, uint32_t event_cause)
  93. {
  94. struct cam_isp_ctx_req *req_isp;
  95. int k = 0, rc = 0;
  96. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  97. if (!req_isp->buf_done_mask)
  98. return rc;
  99. for (k = 0; k < req_isp->num_fence_map_out; k++) {
  100. if (req_isp->buf_done_mask & (1 << k)) {
  101. rc = cam_sync_signal(req_isp->fence_map_out[k].sync_id,
  102. status, event_cause);
  103. if (rc) {
  104. CAM_ERR(CAM_ISP,
  105. "ctx[%d] : Sync signal for Req %llu, sync_id %d status=%d failed with rc = %d",
  106. ctx->ctx_id, req->request_id,
  107. req_isp->fence_map_out[k].sync_id,
  108. status, rc);
  109. return rc;
  110. } else {
  111. CAM_DBG(CAM_ISP,
  112. "ctx[%d] : Sync signal success for Req %llu, sync_id %d status=%d",
  113. ctx->ctx_id, req->request_id,
  114. req_isp->fence_map_out[k].sync_id, status);
  115. req_isp->fence_map_out[k].sync_id = -1;
  116. }
  117. }
  118. }
  119. req_isp->buf_done_mask = 0;
  120. return rc;
  121. }
  122. static int cam_isp_ctx_handle_sync_signal(
  123. struct cam_context *ctx,
  124. struct cam_ctx_request *req, int32_t sync_index,
  125. uint32_t status, uint32_t event_cause)
  126. {
  127. struct cam_isp_ctx_req *req_isp;
  128. int32_t buf_done_ready = 0;
  129. int rc = 0;
  130. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  131. buf_done_ready = atomic_read(&req_isp->buf_done_ready);
  132. if (buf_done_ready != INIT_BUF_DONE) {
  133. rc = cam_isp_ctx_sync_signal_on_buf_done_ready(
  134. ctx, req, status, event_cause);
  135. if (rc) {
  136. CAM_DBG(CAM_ISP,
  137. "Sync failed with rc = %d", rc);
  138. return rc;
  139. }
  140. rc = cam_sync_signal(req_isp->fence_map_out[sync_index].sync_id,
  141. status,
  142. event_cause);
  143. if (rc) {
  144. CAM_DBG(CAM_ISP,
  145. "ctx[%d] :Sync failed Req %llu, sync_id %d status %d with rc = %d",
  146. ctx->ctx_id, req->request_id,
  147. req_isp->fence_map_out[sync_index].sync_id,
  148. status, rc);
  149. return rc;
  150. }
  151. CAM_DBG(CAM_ISP,
  152. "ctx[%d] : Sync signal success for Req %llu, sync_id %d status %d",
  153. ctx->ctx_id, req->request_id,
  154. req_isp->fence_map_out[sync_index].sync_id,
  155. status);
  156. req_isp->fence_map_out[sync_index].sync_id = -1;
  157. } else {
  158. req_isp->buf_done_mask |= 1 << sync_index;
  159. }
  160. return rc;
  161. }
  162. static int __cam_isp_ctx_dump_event_record(
  163. struct cam_isp_context *ctx_isp,
  164. uintptr_t cpu_addr,
  165. size_t buf_len,
  166. size_t *offset)
  167. {
  168. int i, j;
  169. int index;
  170. size_t remain_len;
  171. uint8_t *dst;
  172. uint32_t oldest_entry, num_entries;
  173. uint32_t min_len;
  174. uint64_t *addr, *start;
  175. uint64_t state_head;
  176. struct timespec64 ts;
  177. struct cam_isp_context_dump_header *hdr;
  178. struct cam_isp_context_event_record *record;
  179. if (!cpu_addr || !buf_len || !offset || !ctx_isp) {
  180. CAM_ERR(CAM_ISP, "Invalid args %pK %zu %pK %pK",
  181. cpu_addr, buf_len, offset, ctx_isp);
  182. return -EINVAL;
  183. }
  184. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++) {
  185. state_head = atomic64_read(&ctx_isp->event_record_head[i]);
  186. if (state_head == -1) {
  187. return 0;
  188. } else if (state_head < CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES) {
  189. num_entries = state_head + 1;
  190. oldest_entry = 0;
  191. } else {
  192. num_entries = CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES;
  193. div_u64_rem(state_head + 1,
  194. CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES,
  195. &oldest_entry);
  196. }
  197. index = oldest_entry;
  198. if (buf_len <= *offset) {
  199. CAM_WARN(CAM_ISP,
  200. "Dump buffer overshoot len %zu offset %zu",
  201. buf_len, *offset);
  202. return -ENOSPC;
  203. }
  204. min_len = sizeof(struct cam_isp_context_dump_header) +
  205. ((num_entries * CAM_ISP_CTX_DUMP_EVENT_NUM_WORDS) *
  206. sizeof(uint64_t));
  207. remain_len = buf_len - *offset;
  208. if (remain_len < min_len) {
  209. CAM_WARN(CAM_ISP,
  210. "Dump buffer exhaust remain %zu min %u",
  211. remain_len, min_len);
  212. return -ENOSPC;
  213. }
  214. dst = (uint8_t *)cpu_addr + *offset;
  215. hdr = (struct cam_isp_context_dump_header *)dst;
  216. scnprintf(hdr->tag,
  217. CAM_ISP_CONTEXT_DUMP_TAG_MAX_LEN, "ISP_EVT_%s:",
  218. __cam_isp_evt_val_to_type(i));
  219. hdr->word_size = sizeof(uint64_t);
  220. addr = (uint64_t *)(dst +
  221. sizeof(struct cam_isp_context_dump_header));
  222. start = addr;
  223. for (j = 0; j < num_entries; j++) {
  224. record = &ctx_isp->event_record[i][index];
  225. ts = ktime_to_timespec64(record->timestamp);
  226. *addr++ = record->req_id;
  227. *addr++ = ts.tv_sec;
  228. *addr++ = ts.tv_nsec/NSEC_PER_USEC;
  229. index = (index + 1) %
  230. CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES;
  231. }
  232. hdr->size = hdr->word_size * (addr - start);
  233. *offset += hdr->size +
  234. sizeof(struct cam_isp_context_dump_header);
  235. }
  236. return 0;
  237. }
  238. static void __cam_isp_ctx_update_state_monitor_array(
  239. struct cam_isp_context *ctx_isp,
  240. enum cam_isp_state_change_trigger trigger_type,
  241. uint64_t req_id)
  242. {
  243. int iterator;
  244. INC_HEAD(&ctx_isp->state_monitor_head,
  245. CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES, &iterator);
  246. ctx_isp->cam_isp_ctx_state_monitor[iterator].curr_state =
  247. ctx_isp->substate_activated;
  248. ctx_isp->cam_isp_ctx_state_monitor[iterator].frame_id =
  249. ctx_isp->frame_id;
  250. ctx_isp->cam_isp_ctx_state_monitor[iterator].trigger =
  251. trigger_type;
  252. ctx_isp->cam_isp_ctx_state_monitor[iterator].req_id =
  253. req_id;
  254. ctx_isp->cam_isp_ctx_state_monitor[iterator].evt_time_stamp =
  255. jiffies_to_msecs(jiffies) - ctx_isp->init_timestamp;
  256. }
  257. static const char *__cam_isp_ctx_substate_val_to_type(
  258. enum cam_isp_ctx_activated_substate type)
  259. {
  260. switch (type) {
  261. case CAM_ISP_CTX_ACTIVATED_SOF:
  262. return "SOF";
  263. case CAM_ISP_CTX_ACTIVATED_APPLIED:
  264. return "APPLIED";
  265. case CAM_ISP_CTX_ACTIVATED_EPOCH:
  266. return "EPOCH";
  267. case CAM_ISP_CTX_ACTIVATED_BUBBLE:
  268. return "BUBBLE";
  269. case CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED:
  270. return "BUBBLE_APPLIED";
  271. case CAM_ISP_CTX_ACTIVATED_HW_ERROR:
  272. return "HW_ERROR";
  273. case CAM_ISP_CTX_ACTIVATED_HALT:
  274. return "HALT";
  275. default:
  276. return "INVALID";
  277. }
  278. }
  279. static const char *__cam_isp_hw_evt_val_to_type(
  280. uint32_t evt_id)
  281. {
  282. switch (evt_id) {
  283. case CAM_ISP_STATE_CHANGE_TRIGGER_ERROR:
  284. return "ERROR";
  285. case CAM_ISP_STATE_CHANGE_TRIGGER_APPLIED:
  286. return "APPLIED";
  287. case CAM_ISP_STATE_CHANGE_TRIGGER_SOF:
  288. return "SOF";
  289. case CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE:
  290. return "REG_UPDATE";
  291. case CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH:
  292. return "EPOCH";
  293. case CAM_ISP_STATE_CHANGE_TRIGGER_EOF:
  294. return "EOF";
  295. case CAM_ISP_STATE_CHANGE_TRIGGER_DONE:
  296. return "DONE";
  297. case CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH:
  298. return "FLUSH";
  299. default:
  300. return "CAM_ISP_EVENT_INVALID";
  301. }
  302. }
  303. static void __cam_isp_ctx_dump_state_monitor_array(
  304. struct cam_isp_context *ctx_isp)
  305. {
  306. int i = 0;
  307. int64_t state_head = 0;
  308. uint32_t index, num_entries, oldest_entry;
  309. state_head = atomic64_read(&ctx_isp->state_monitor_head);
  310. if (state_head == -1) {
  311. return;
  312. } else if (state_head < CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES) {
  313. num_entries = state_head;
  314. oldest_entry = 0;
  315. } else {
  316. num_entries = CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES;
  317. div_u64_rem(state_head + 1,
  318. CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES, &oldest_entry);
  319. }
  320. CAM_ERR(CAM_ISP,
  321. "Dumping state information for preceding requests");
  322. index = oldest_entry;
  323. for (i = 0; i < num_entries; i++) {
  324. CAM_ERR(CAM_ISP,
  325. "Index[%d] time[%d] : Substate[%s] Frame[%lld] ReqId[%llu] evt_type[%s]",
  326. index,
  327. ctx_isp->cam_isp_ctx_state_monitor[index].evt_time_stamp,
  328. __cam_isp_ctx_substate_val_to_type(
  329. ctx_isp->cam_isp_ctx_state_monitor[index].curr_state),
  330. ctx_isp->cam_isp_ctx_state_monitor[index].frame_id,
  331. ctx_isp->cam_isp_ctx_state_monitor[index].req_id,
  332. __cam_isp_hw_evt_val_to_type(
  333. ctx_isp->cam_isp_ctx_state_monitor[index].trigger));
  334. index = (index + 1) % CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES;
  335. }
  336. }
  337. static int cam_isp_context_info_dump(void *context,
  338. enum cam_context_dump_id id)
  339. {
  340. struct cam_context *ctx = (struct cam_context *)context;
  341. switch (id) {
  342. case CAM_CTX_DUMP_ACQ_INFO: {
  343. cam_context_dump_hw_acq_info(ctx);
  344. break;
  345. }
  346. default:
  347. CAM_DBG(CAM_ISP, "DUMP id not valid %u", id);
  348. break;
  349. }
  350. return 0;
  351. }
  352. static int cam_isp_ctx_dump_req(
  353. struct cam_isp_ctx_req *req_isp,
  354. uintptr_t cpu_addr,
  355. size_t buf_len,
  356. size_t *offset,
  357. bool dump_to_buff)
  358. {
  359. int i, rc = 0;
  360. size_t len = 0;
  361. uint32_t *buf_addr;
  362. uint32_t *buf_start, *buf_end;
  363. size_t remain_len = 0;
  364. struct cam_cdm_cmd_buf_dump_info dump_info;
  365. for (i = 0; i < req_isp->num_cfg; i++) {
  366. rc = cam_packet_util_get_cmd_mem_addr(
  367. req_isp->cfg[i].handle, &buf_addr, &len);
  368. if (rc) {
  369. CAM_ERR_RATE_LIMIT(CAM_ISP,
  370. "Failed to get_cmd_mem_addr, rc=%d",
  371. rc);
  372. } else {
  373. if (req_isp->cfg[i].offset >= ((uint32_t)len)) {
  374. CAM_ERR(CAM_ISP,
  375. "Invalid offset exp %u actual %u",
  376. req_isp->cfg[i].offset, (uint32_t)len);
  377. return -EINVAL;
  378. }
  379. remain_len = len - req_isp->cfg[i].offset;
  380. if (req_isp->cfg[i].len >
  381. ((uint32_t)remain_len)) {
  382. CAM_ERR(CAM_ISP,
  383. "Invalid len exp %u remain_len %u",
  384. req_isp->cfg[i].len,
  385. (uint32_t)remain_len);
  386. return -EINVAL;
  387. }
  388. buf_start = (uint32_t *)((uint8_t *) buf_addr +
  389. req_isp->cfg[i].offset);
  390. buf_end = (uint32_t *)((uint8_t *) buf_start +
  391. req_isp->cfg[i].len - 1);
  392. if (dump_to_buff) {
  393. if (!cpu_addr || !offset || !buf_len) {
  394. CAM_ERR(CAM_ISP, "Invalid args");
  395. break;
  396. }
  397. dump_info.src_start = buf_start;
  398. dump_info.src_end = buf_end;
  399. dump_info.dst_start = cpu_addr;
  400. dump_info.dst_offset = *offset;
  401. dump_info.dst_max_size = buf_len;
  402. rc = cam_cdm_util_dump_cmd_bufs_v2(
  403. &dump_info);
  404. *offset = dump_info.dst_offset;
  405. if (rc)
  406. return rc;
  407. } else
  408. cam_cdm_util_dump_cmd_buf(buf_start, buf_end);
  409. }
  410. }
  411. return rc;
  412. }
  413. static int __cam_isp_ctx_enqueue_request_in_order(
  414. struct cam_context *ctx, struct cam_ctx_request *req)
  415. {
  416. struct cam_ctx_request *req_current;
  417. struct cam_ctx_request *req_prev;
  418. struct list_head temp_list;
  419. struct cam_isp_context *ctx_isp;
  420. INIT_LIST_HEAD(&temp_list);
  421. spin_lock_bh(&ctx->lock);
  422. if (list_empty(&ctx->pending_req_list)) {
  423. list_add_tail(&req->list, &ctx->pending_req_list);
  424. } else {
  425. list_for_each_entry_safe_reverse(
  426. req_current, req_prev, &ctx->pending_req_list, list) {
  427. if (req->request_id < req_current->request_id) {
  428. list_del_init(&req_current->list);
  429. list_add(&req_current->list, &temp_list);
  430. continue;
  431. } else if (req->request_id == req_current->request_id) {
  432. CAM_WARN(CAM_ISP,
  433. "Received duplicated request %lld",
  434. req->request_id);
  435. }
  436. break;
  437. }
  438. list_add_tail(&req->list, &ctx->pending_req_list);
  439. if (!list_empty(&temp_list)) {
  440. list_for_each_entry_safe(
  441. req_current, req_prev, &temp_list, list) {
  442. list_del_init(&req_current->list);
  443. list_add_tail(&req_current->list,
  444. &ctx->pending_req_list);
  445. }
  446. }
  447. }
  448. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  449. __cam_isp_ctx_update_event_record(ctx_isp,
  450. CAM_ISP_CTX_EVENT_SUBMIT, req);
  451. spin_unlock_bh(&ctx->lock);
  452. return 0;
  453. }
  454. static int __cam_isp_ctx_enqueue_init_request(
  455. struct cam_context *ctx, struct cam_ctx_request *req)
  456. {
  457. int rc = 0;
  458. struct cam_ctx_request *req_old;
  459. struct cam_isp_ctx_req *req_isp_old;
  460. struct cam_isp_ctx_req *req_isp_new;
  461. struct cam_isp_prepare_hw_update_data *req_update_old;
  462. struct cam_isp_prepare_hw_update_data *req_update_new;
  463. struct cam_isp_prepare_hw_update_data *hw_update_data;
  464. spin_lock_bh(&ctx->lock);
  465. if (list_empty(&ctx->pending_req_list)) {
  466. list_add_tail(&req->list, &ctx->pending_req_list);
  467. CAM_DBG(CAM_ISP, "INIT packet added req id= %d",
  468. req->request_id);
  469. goto end;
  470. }
  471. req_old = list_first_entry(&ctx->pending_req_list,
  472. struct cam_ctx_request, list);
  473. req_isp_old = (struct cam_isp_ctx_req *) req_old->req_priv;
  474. req_isp_new = (struct cam_isp_ctx_req *) req->req_priv;
  475. if (req_isp_old->hw_update_data.packet_opcode_type ==
  476. CAM_ISP_PACKET_INIT_DEV) {
  477. if ((req_isp_old->num_cfg + req_isp_new->num_cfg) >=
  478. ctx->max_hw_update_entries) {
  479. CAM_WARN(CAM_ISP,
  480. "Can not merge INIT pkt num_cfgs = %d",
  481. (req_isp_old->num_cfg +
  482. req_isp_new->num_cfg));
  483. rc = -ENOMEM;
  484. }
  485. if (req_isp_old->num_fence_map_out != 0 ||
  486. req_isp_old->num_fence_map_in != 0) {
  487. CAM_WARN(CAM_ISP, "Invalid INIT pkt sequence");
  488. rc = -EINVAL;
  489. }
  490. if (!rc) {
  491. memcpy(req_isp_old->fence_map_out,
  492. req_isp_new->fence_map_out,
  493. sizeof(req_isp_new->fence_map_out[0])*
  494. req_isp_new->num_fence_map_out);
  495. req_isp_old->num_fence_map_out =
  496. req_isp_new->num_fence_map_out;
  497. memcpy(req_isp_old->fence_map_in,
  498. req_isp_new->fence_map_in,
  499. sizeof(req_isp_new->fence_map_in[0])*
  500. req_isp_new->num_fence_map_in);
  501. req_isp_old->num_fence_map_in =
  502. req_isp_new->num_fence_map_in;
  503. memcpy(&req_isp_old->cfg[req_isp_old->num_cfg],
  504. req_isp_new->cfg,
  505. sizeof(req_isp_new->cfg[0]) *
  506. req_isp_new->num_cfg);
  507. req_isp_old->num_cfg += req_isp_new->num_cfg;
  508. memcpy(&req_old->pf_data, &req->pf_data,
  509. sizeof(struct cam_hw_mgr_dump_pf_data));
  510. if (req_isp_new->hw_update_data.num_reg_dump_buf) {
  511. req_update_new = &req_isp_new->hw_update_data;
  512. req_update_old = &req_isp_old->hw_update_data;
  513. memcpy(&req_update_old->reg_dump_buf_desc,
  514. &req_update_new->reg_dump_buf_desc,
  515. sizeof(struct cam_cmd_buf_desc) *
  516. req_update_new->num_reg_dump_buf);
  517. req_update_old->num_reg_dump_buf =
  518. req_update_new->num_reg_dump_buf;
  519. }
  520. /* Update frame header params for EPCR */
  521. hw_update_data = &req_isp_new->hw_update_data;
  522. req_isp_old->hw_update_data.frame_header_res_id =
  523. req_isp_new->hw_update_data.frame_header_res_id;
  524. req_isp_old->hw_update_data.frame_header_cpu_addr =
  525. hw_update_data->frame_header_cpu_addr;
  526. req_old->request_id = req->request_id;
  527. list_add_tail(&req->list, &ctx->free_req_list);
  528. }
  529. } else {
  530. CAM_WARN(CAM_ISP,
  531. "Received Update pkt before INIT pkt. req_id= %lld",
  532. req->request_id);
  533. rc = -EINVAL;
  534. }
  535. end:
  536. spin_unlock_bh(&ctx->lock);
  537. return rc;
  538. }
  539. static const char *__cam_isp_resource_handle_id_to_type(
  540. uint32_t resource_handle)
  541. {
  542. switch (resource_handle) {
  543. case CAM_ISP_IFE_OUT_RES_FULL:
  544. return "FULL";
  545. case CAM_ISP_IFE_OUT_RES_DS4:
  546. return "DS4";
  547. case CAM_ISP_IFE_OUT_RES_DS16:
  548. return "DS16";
  549. case CAM_ISP_IFE_OUT_RES_RAW_DUMP:
  550. return "RAW_DUMP";
  551. case CAM_ISP_IFE_OUT_RES_FD:
  552. return "FD";
  553. case CAM_ISP_IFE_OUT_RES_PDAF:
  554. return "PDAF";
  555. case CAM_ISP_IFE_OUT_RES_RDI_0:
  556. return "RDI_0";
  557. case CAM_ISP_IFE_OUT_RES_RDI_1:
  558. return "RDI_1";
  559. case CAM_ISP_IFE_OUT_RES_RDI_2:
  560. return "RDI_2";
  561. case CAM_ISP_IFE_OUT_RES_RDI_3:
  562. return "RDI_3";
  563. case CAM_ISP_IFE_OUT_RES_STATS_HDR_BE:
  564. return "STATS_HDR_BE";
  565. case CAM_ISP_IFE_OUT_RES_STATS_HDR_BHIST:
  566. return "STATS_HDR_BHIST";
  567. case CAM_ISP_IFE_OUT_RES_STATS_TL_BG:
  568. return "STATS_TL_BG";
  569. case CAM_ISP_IFE_OUT_RES_STATS_BF:
  570. return "STATS_BF";
  571. case CAM_ISP_IFE_OUT_RES_STATS_AWB_BG:
  572. return "STATS_AWB_BG";
  573. case CAM_ISP_IFE_OUT_RES_STATS_BHIST:
  574. return "STATS_BHIST";
  575. case CAM_ISP_IFE_OUT_RES_STATS_RS:
  576. return "STATS_RS";
  577. case CAM_ISP_IFE_OUT_RES_STATS_CS:
  578. return "STATS_CS";
  579. case CAM_ISP_IFE_OUT_RES_STATS_IHIST:
  580. return "STATS_IHIST";
  581. case CAM_ISP_IFE_OUT_RES_FULL_DISP:
  582. return "FULL_DISP";
  583. case CAM_ISP_IFE_OUT_RES_DS4_DISP:
  584. return "DS4_DISP";
  585. case CAM_ISP_IFE_OUT_RES_DS16_DISP:
  586. return "DS16_DISP";
  587. case CAM_ISP_IFE_OUT_RES_2PD:
  588. return "2PD";
  589. case CAM_ISP_IFE_OUT_RES_RDI_RD:
  590. return "RDI_RD";
  591. case CAM_ISP_IFE_OUT_RES_LCR:
  592. return "LCR";
  593. case CAM_ISP_IFE_OUT_RES_AWB_BFW:
  594. return "AWB_BFW";
  595. case CAM_ISP_IFE_OUT_RES_PREPROCESS_2PD:
  596. return "PREPROCESS_2PD";
  597. case CAM_ISP_IFE_OUT_RES_STATS_AEC_BE:
  598. return "STATS_AEC_BE";
  599. case CAM_ISP_IFE_OUT_RES_LTM_STATS:
  600. return "LTM_STATS";
  601. case CAM_ISP_IFE_OUT_RES_STATS_GTM_BHIST:
  602. return "STATS_GTM_BHIST";
  603. case CAM_ISP_IFE_LITE_OUT_RES_STATS_BG:
  604. return "STATS_BG";
  605. case CAM_ISP_IFE_LITE_OUT_RES_PREPROCESS_RAW:
  606. return "PREPROCESS_RAW";
  607. case CAM_ISP_IFE_OUT_RES_SPARSE_PD:
  608. return "SPARSE_PD";
  609. default:
  610. return "CAM_ISP_Invalid_Resource_Type";
  611. }
  612. }
  613. static const char *__cam_isp_tfe_resource_handle_id_to_type(
  614. uint32_t resource_handle)
  615. {
  616. switch (resource_handle) {
  617. case CAM_ISP_TFE_OUT_RES_FULL:
  618. return "FULL";
  619. case CAM_ISP_TFE_OUT_RES_RAW_DUMP:
  620. return "RAW_DUMP";
  621. case CAM_ISP_TFE_OUT_RES_PDAF:
  622. return "PDAF";
  623. case CAM_ISP_TFE_OUT_RES_RDI_0:
  624. return "RDI_0";
  625. case CAM_ISP_TFE_OUT_RES_RDI_1:
  626. return "RDI_1";
  627. case CAM_ISP_TFE_OUT_RES_RDI_2:
  628. return "RDI_2";
  629. case CAM_ISP_TFE_OUT_RES_STATS_HDR_BE:
  630. return "STATS_HDR_BE";
  631. case CAM_ISP_TFE_OUT_RES_STATS_HDR_BHIST:
  632. return "STATS_HDR_BHIST";
  633. case CAM_ISP_TFE_OUT_RES_STATS_TL_BG:
  634. return "STATS_TL_BG";
  635. case CAM_ISP_TFE_OUT_RES_STATS_BF:
  636. return "STATS_BF";
  637. case CAM_ISP_TFE_OUT_RES_STATS_AWB_BG:
  638. return "STATS_AWB_BG";
  639. case CAM_ISP_TFE_OUT_RES_STATS_RS:
  640. return "STATS_RS";
  641. case CAM_ISP_TFE_OUT_RES_DS4:
  642. return "DS_4";
  643. case CAM_ISP_TFE_OUT_RES_DS16:
  644. return "DS_16";
  645. case CAM_ISP_TFE_OUT_RES_AI:
  646. return "AI";
  647. default:
  648. return "CAM_ISP_Invalid_Resource_Type";
  649. }
  650. }
  651. static uint64_t __cam_isp_ctx_get_event_ts(uint32_t evt_id, void *evt_data)
  652. {
  653. uint64_t ts = 0;
  654. if (!evt_data)
  655. return 0;
  656. switch (evt_id) {
  657. case CAM_ISP_HW_EVENT_ERROR:
  658. ts = ((struct cam_isp_hw_error_event_data *)evt_data)->
  659. timestamp;
  660. break;
  661. case CAM_ISP_HW_EVENT_SOF:
  662. ts = ((struct cam_isp_hw_sof_event_data *)evt_data)->
  663. timestamp;
  664. break;
  665. case CAM_ISP_HW_EVENT_REG_UPDATE:
  666. ts = ((struct cam_isp_hw_reg_update_event_data *)evt_data)->
  667. timestamp;
  668. break;
  669. case CAM_ISP_HW_EVENT_EPOCH:
  670. ts = ((struct cam_isp_hw_epoch_event_data *)evt_data)->
  671. timestamp;
  672. break;
  673. case CAM_ISP_HW_EVENT_EOF:
  674. ts = ((struct cam_isp_hw_eof_event_data *)evt_data)->
  675. timestamp;
  676. break;
  677. case CAM_ISP_HW_EVENT_DONE:
  678. break;
  679. default:
  680. CAM_DBG(CAM_ISP, "Invalid Event Type %d", evt_id);
  681. }
  682. return ts;
  683. }
  684. static void __cam_isp_ctx_send_sof_boot_timestamp(
  685. struct cam_isp_context *ctx_isp, uint64_t request_id,
  686. uint32_t sof_event_status)
  687. {
  688. struct cam_req_mgr_message req_msg;
  689. req_msg.session_hdl = ctx_isp->base->session_hdl;
  690. req_msg.u.frame_msg.frame_id = ctx_isp->frame_id;
  691. req_msg.u.frame_msg.request_id = request_id;
  692. req_msg.u.frame_msg.timestamp = ctx_isp->boot_timestamp;
  693. req_msg.u.frame_msg.link_hdl = ctx_isp->base->link_hdl;
  694. req_msg.u.frame_msg.sof_status = sof_event_status;
  695. req_msg.u.frame_msg.frame_id_meta = ctx_isp->frame_id_meta;
  696. CAM_DBG(CAM_ISP,
  697. "request id:%lld frame number:%lld boot time stamp:0x%llx status:%u",
  698. request_id, ctx_isp->frame_id,
  699. ctx_isp->boot_timestamp, sof_event_status);
  700. if (cam_req_mgr_notify_message(&req_msg,
  701. V4L_EVENT_CAM_REQ_MGR_SOF_BOOT_TS,
  702. V4L_EVENT_CAM_REQ_MGR_EVENT))
  703. CAM_ERR(CAM_ISP,
  704. "Error in notifying the boot time for req id:%lld",
  705. request_id);
  706. }
  707. static void __cam_isp_ctx_send_sof_timestamp_frame_header(
  708. struct cam_isp_context *ctx_isp, uint32_t *frame_header_cpu_addr,
  709. uint64_t request_id, uint32_t sof_event_status)
  710. {
  711. uint32_t *time32 = NULL;
  712. uint64_t timestamp = 0;
  713. struct cam_req_mgr_message req_msg;
  714. time32 = frame_header_cpu_addr;
  715. timestamp = (uint64_t) time32[1];
  716. timestamp = timestamp << 24;
  717. timestamp |= (uint64_t)(time32[0] >> 8);
  718. timestamp = mul_u64_u32_div(timestamp,
  719. CAM_IFE_QTIMER_MUL_FACTOR,
  720. CAM_IFE_QTIMER_DIV_FACTOR);
  721. ctx_isp->sof_timestamp_val = timestamp;
  722. req_msg.session_hdl = ctx_isp->base->session_hdl;
  723. req_msg.u.frame_msg.frame_id = ctx_isp->frame_id;
  724. req_msg.u.frame_msg.request_id = request_id;
  725. req_msg.u.frame_msg.timestamp = ctx_isp->sof_timestamp_val;
  726. req_msg.u.frame_msg.link_hdl = ctx_isp->base->link_hdl;
  727. req_msg.u.frame_msg.sof_status = sof_event_status;
  728. CAM_DBG(CAM_ISP,
  729. "request id:%lld frame number:%lld SOF time stamp:0x%llx status:%u",
  730. request_id, ctx_isp->frame_id,
  731. ctx_isp->sof_timestamp_val, sof_event_status);
  732. if (cam_req_mgr_notify_message(&req_msg,
  733. V4L_EVENT_CAM_REQ_MGR_SOF, V4L_EVENT_CAM_REQ_MGR_EVENT))
  734. CAM_ERR(CAM_ISP,
  735. "Error in notifying the sof time for req id:%lld",
  736. request_id);
  737. }
  738. static void __cam_isp_ctx_send_sof_timestamp(
  739. struct cam_isp_context *ctx_isp, uint64_t request_id,
  740. uint32_t sof_event_status)
  741. {
  742. struct cam_req_mgr_message req_msg;
  743. if ((ctx_isp->use_frame_header_ts) || (request_id == 0))
  744. goto end;
  745. req_msg.session_hdl = ctx_isp->base->session_hdl;
  746. req_msg.u.frame_msg.frame_id = ctx_isp->frame_id;
  747. req_msg.u.frame_msg.request_id = request_id;
  748. req_msg.u.frame_msg.timestamp = ctx_isp->sof_timestamp_val;
  749. req_msg.u.frame_msg.link_hdl = ctx_isp->base->link_hdl;
  750. req_msg.u.frame_msg.sof_status = sof_event_status;
  751. req_msg.u.frame_msg.frame_id_meta = ctx_isp->frame_id_meta;
  752. CAM_DBG(CAM_ISP,
  753. "link hdl 0x%x request id:%lld frame number:%lld SOF time stamp:%lld status:%u",
  754. ctx_isp->base->link_hdl, request_id, ctx_isp->frame_id,
  755. ctx_isp->sof_timestamp_val, sof_event_status);
  756. if (cam_req_mgr_notify_message(&req_msg,
  757. V4L_EVENT_CAM_REQ_MGR_SOF, V4L_EVENT_CAM_REQ_MGR_EVENT))
  758. CAM_ERR(CAM_ISP,
  759. "Error in notifying the sof time for req id:%lld",
  760. request_id);
  761. end:
  762. __cam_isp_ctx_send_sof_boot_timestamp(ctx_isp,
  763. request_id, sof_event_status);
  764. }
  765. static void __cam_isp_ctx_handle_buf_done_fail_log(
  766. uint64_t request_id, struct cam_isp_ctx_req *req_isp,
  767. uint32_t isp_device_type)
  768. {
  769. int i;
  770. const char *handle_type;
  771. if (req_isp->num_fence_map_out >= CAM_ISP_CTX_RES_MAX) {
  772. CAM_ERR(CAM_ISP,
  773. "Num Resources exceed mMAX %d >= %d ",
  774. req_isp->num_fence_map_out, CAM_ISP_CTX_RES_MAX);
  775. return;
  776. }
  777. CAM_WARN_RATE_LIMIT(CAM_ISP,
  778. "Prev Req[%lld] : num_out=%d, num_acked=%d, bubble : report=%d, detected=%d",
  779. request_id, req_isp->num_fence_map_out, req_isp->num_acked,
  780. req_isp->bubble_report, req_isp->bubble_detected);
  781. CAM_WARN_RATE_LIMIT(CAM_ISP,
  782. "Resource Handles that fail to generate buf_done in prev frame");
  783. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  784. if (req_isp->fence_map_out[i].sync_id != -1) {
  785. if (isp_device_type == CAM_IFE_DEVICE_TYPE) {
  786. handle_type =
  787. __cam_isp_resource_handle_id_to_type(
  788. req_isp->fence_map_out[i].resource_handle);
  789. trace_cam_log_event("Buf_done Congestion",
  790. __cam_isp_resource_handle_id_to_type(
  791. req_isp->fence_map_out[i].resource_handle),
  792. request_id, req_isp->fence_map_out[i].sync_id);
  793. } else {
  794. handle_type =
  795. __cam_isp_tfe_resource_handle_id_to_type(
  796. req_isp->fence_map_out[i].resource_handle);
  797. trace_cam_log_event("Buf_done Congestion",
  798. __cam_isp_tfe_resource_handle_id_to_type(
  799. req_isp->fence_map_out[i].resource_handle),
  800. request_id, req_isp->fence_map_out[i].sync_id);
  801. }
  802. CAM_WARN_RATE_LIMIT(CAM_ISP,
  803. "Resource_Handle: [%s][0x%x] Sync_ID: [0x%x]",
  804. handle_type,
  805. req_isp->fence_map_out[i].resource_handle,
  806. req_isp->fence_map_out[i].sync_id);
  807. }
  808. }
  809. }
  810. static int __cam_isp_ctx_handle_buf_done_for_req_list(
  811. struct cam_isp_context *ctx_isp,
  812. struct cam_ctx_request *req)
  813. {
  814. int rc = 0, i;
  815. uint64_t buf_done_req_id;
  816. struct cam_isp_ctx_req *req_isp;
  817. struct cam_context *ctx = ctx_isp->base;
  818. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  819. ctx_isp->active_req_cnt--;
  820. buf_done_req_id = req->request_id;
  821. if (req_isp->bubble_detected && req_isp->bubble_report) {
  822. req_isp->num_acked = 0;
  823. req_isp->num_deferred_acks = 0;
  824. req_isp->bubble_detected = false;
  825. list_del_init(&req->list);
  826. atomic_set(&ctx_isp->process_bubble, 0);
  827. req_isp->cdm_reset_before_apply = false;
  828. ctx_isp->bubble_frame_cnt = 0;
  829. atomic_set(&req_isp->buf_done_ready, 0);
  830. if (buf_done_req_id <= ctx->last_flush_req) {
  831. for (i = 0; i < req_isp->num_fence_map_out; i++)
  832. rc = cam_sync_signal(
  833. req_isp->fence_map_out[i].sync_id,
  834. CAM_SYNC_STATE_SIGNALED_ERROR,
  835. CAM_SYNC_ISP_EVENT_BUBBLE);
  836. list_add_tail(&req->list, &ctx->free_req_list);
  837. CAM_DBG(CAM_REQ,
  838. "Move active request %lld to free list(cnt = %d) [flushed], ctx %u",
  839. buf_done_req_id, ctx_isp->active_req_cnt,
  840. ctx->ctx_id);
  841. ctx_isp->last_bufdone_err_apply_req_id = 0;
  842. } else {
  843. list_add(&req->list, &ctx->pending_req_list);
  844. CAM_DBG(CAM_REQ,
  845. "Move active request %lld to pending list(cnt = %d) [bubble recovery], ctx %u",
  846. req->request_id, ctx_isp->active_req_cnt,
  847. ctx->ctx_id);
  848. }
  849. } else {
  850. if (!ctx_isp->use_frame_header_ts) {
  851. if (ctx_isp->reported_req_id < buf_done_req_id) {
  852. ctx_isp->reported_req_id = buf_done_req_id;
  853. __cam_isp_ctx_send_sof_timestamp(ctx_isp,
  854. buf_done_req_id,
  855. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  856. }
  857. }
  858. rc = cam_isp_ctx_sync_signal_on_buf_done_ready(
  859. ctx, req, CAM_SYNC_STATE_SIGNALED_SUCCESS,
  860. CAM_SYNC_COMMON_EVENT_SUCCESS);
  861. list_del_init(&req->list);
  862. list_add_tail(&req->list, &ctx->free_req_list);
  863. req_isp->reapply = false;
  864. req_isp->cdm_reset_before_apply = false;
  865. CAM_DBG(CAM_REQ,
  866. "Move active request %lld to free list(cnt = %d) [all fences done], ctx %u",
  867. buf_done_req_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  868. ctx_isp->req_info.last_bufdone_req_id = req->request_id;
  869. ctx_isp->last_bufdone_err_apply_req_id = 0;
  870. }
  871. cam_cpas_notify_event("IFE BufDone", buf_done_req_id);
  872. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  873. CAM_ISP_STATE_CHANGE_TRIGGER_DONE, buf_done_req_id);
  874. __cam_isp_ctx_update_event_record(ctx_isp,
  875. CAM_ISP_CTX_EVENT_BUFDONE, req);
  876. return rc;
  877. }
  878. static int __cam_isp_ctx_handle_buf_done_for_request(
  879. struct cam_isp_context *ctx_isp,
  880. struct cam_ctx_request *req,
  881. struct cam_isp_hw_done_event_data *done,
  882. uint32_t bubble_state,
  883. struct cam_isp_hw_done_event_data *done_next_req)
  884. {
  885. int rc = 0;
  886. int i, j;
  887. struct cam_isp_ctx_req *req_isp;
  888. struct cam_context *ctx = ctx_isp->base;
  889. const char *handle_type;
  890. trace_cam_buf_done("ISP", ctx, req);
  891. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  892. CAM_DBG(CAM_ISP, "Enter with bubble_state %d, req_bubble_detected %d",
  893. bubble_state, req_isp->bubble_detected);
  894. done_next_req->num_handles = 0;
  895. done_next_req->timestamp = done->timestamp;
  896. for (i = 0; i < done->num_handles; i++) {
  897. for (j = 0; j < req_isp->num_fence_map_out; j++) {
  898. if (done->resource_handle[i] ==
  899. req_isp->fence_map_out[j].resource_handle)
  900. break;
  901. }
  902. if (j == req_isp->num_fence_map_out) {
  903. /*
  904. * If not found in current request, it could be
  905. * belonging to next request, this can happen if
  906. * IRQ delay happens. It is only valid when the
  907. * platform doesn't have last consumed address.
  908. */
  909. CAM_WARN(CAM_ISP,
  910. "BUF_DONE for res %s not found in Req %lld ",
  911. __cam_isp_resource_handle_id_to_type(
  912. done->resource_handle[i]),
  913. req->request_id);
  914. done_next_req->resource_handle
  915. [done_next_req->num_handles++] =
  916. done->resource_handle[i];
  917. continue;
  918. }
  919. if (req_isp->fence_map_out[j].sync_id == -1) {
  920. if (ctx_isp->isp_device_type == CAM_IFE_DEVICE_TYPE)
  921. handle_type =
  922. __cam_isp_resource_handle_id_to_type(
  923. req_isp->fence_map_out[i].resource_handle);
  924. else
  925. handle_type =
  926. __cam_isp_tfe_resource_handle_id_to_type(
  927. req_isp->fence_map_out[i].resource_handle);
  928. CAM_WARN(CAM_ISP,
  929. "Duplicate BUF_DONE for req %lld : i=%d, j=%d, res=%s",
  930. req->request_id, i, j, handle_type);
  931. trace_cam_log_event("Duplicate BufDone",
  932. handle_type, req->request_id, ctx->ctx_id);
  933. done_next_req->resource_handle
  934. [done_next_req->num_handles++] =
  935. done->resource_handle[i];
  936. continue;
  937. }
  938. if (!req_isp->bubble_detected) {
  939. if (req_isp->is_sync_mode) {
  940. CAM_DBG(CAM_ISP,
  941. "Hold sync signal: req %lld res 0x%x fd 0x%x, ctx %u",
  942. req->request_id,
  943. req_isp->fence_map_out[j].resource_handle,
  944. req_isp->fence_map_out[j].sync_id,
  945. ctx->ctx_id);
  946. rc = cam_isp_ctx_handle_sync_signal(ctx, req, j,
  947. CAM_SYNC_STATE_SIGNALED_SUCCESS,
  948. CAM_SYNC_COMMON_EVENT_SUCCESS);
  949. } else {
  950. CAM_DBG(CAM_ISP,
  951. "Sync with success: req %lld res 0x%x fd 0x%x, ctx %u",
  952. req->request_id,
  953. req_isp->fence_map_out[j].resource_handle,
  954. req_isp->fence_map_out[j].sync_id,
  955. ctx->ctx_id);
  956. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  957. CAM_SYNC_STATE_SIGNALED_SUCCESS,
  958. CAM_SYNC_COMMON_EVENT_SUCCESS);
  959. if (rc)
  960. CAM_DBG(CAM_ISP, "Sync failed with rc = %d", rc);
  961. }
  962. } else if (!req_isp->bubble_report) {
  963. CAM_DBG(CAM_ISP,
  964. "Sync with failure: req %lld res 0x%x fd 0x%x, ctx %u",
  965. req->request_id,
  966. req_isp->fence_map_out[j].resource_handle,
  967. req_isp->fence_map_out[j].sync_id,
  968. ctx->ctx_id);
  969. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  970. CAM_SYNC_STATE_SIGNALED_ERROR,
  971. CAM_SYNC_ISP_EVENT_BUBBLE);
  972. if (rc)
  973. CAM_ERR(CAM_ISP, "Sync failed with rc = %d",
  974. rc);
  975. } else {
  976. /*
  977. * Ignore the buffer done if bubble detect is on
  978. * Increment the ack number here, and queue the
  979. * request back to pending list whenever all the
  980. * buffers are done.
  981. */
  982. req_isp->num_acked++;
  983. CAM_DBG(CAM_ISP,
  984. "buf done with bubble state %d recovery %d",
  985. bubble_state, req_isp->bubble_report);
  986. continue;
  987. }
  988. CAM_DBG(CAM_ISP, "req %lld, reset sync id 0x%x ctx %u",
  989. req->request_id,
  990. req_isp->fence_map_out[j].sync_id, ctx->ctx_id);
  991. if (!rc) {
  992. req_isp->num_acked++;
  993. req_isp->fence_map_out[j].sync_id = -1;
  994. }
  995. if ((ctx_isp->use_frame_header_ts) &&
  996. (req_isp->hw_update_data.frame_header_res_id ==
  997. req_isp->fence_map_out[j].resource_handle))
  998. __cam_isp_ctx_send_sof_timestamp_frame_header(
  999. ctx_isp,
  1000. req_isp->hw_update_data.frame_header_cpu_addr,
  1001. req->request_id, CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1002. }
  1003. if (req_isp->num_acked > req_isp->num_fence_map_out) {
  1004. /* Should not happen */
  1005. CAM_ERR(CAM_ISP,
  1006. "WARNING: req_id %lld num_acked %d > map_out %d, ctx %u",
  1007. req->request_id, req_isp->num_acked,
  1008. req_isp->num_fence_map_out, ctx->ctx_id);
  1009. WARN_ON(req_isp->num_acked > req_isp->num_fence_map_out);
  1010. }
  1011. if (req_isp->num_acked != req_isp->num_fence_map_out)
  1012. return rc;
  1013. rc = __cam_isp_ctx_handle_buf_done_for_req_list(ctx_isp, req);
  1014. return rc;
  1015. }
  1016. static int __cam_isp_handle_deferred_buf_done(
  1017. struct cam_isp_context *ctx_isp,
  1018. struct cam_ctx_request *req,
  1019. bool bubble_handling,
  1020. uint32_t status, uint32_t event_cause)
  1021. {
  1022. int i, j;
  1023. int rc = 0;
  1024. struct cam_isp_ctx_req *req_isp =
  1025. (struct cam_isp_ctx_req *) req->req_priv;
  1026. struct cam_context *ctx = ctx_isp->base;
  1027. CAM_DBG(CAM_ISP,
  1028. "ctx[%d] : Req %llu : Handling %d deferred buf_dones num_acked=%d, bubble_handling=%d",
  1029. ctx->ctx_id, req->request_id, req_isp->num_deferred_acks,
  1030. req_isp->num_acked, bubble_handling);
  1031. for (i = 0; i < req_isp->num_deferred_acks; i++) {
  1032. j = req_isp->deferred_fence_map_index[i];
  1033. CAM_DBG(CAM_ISP,
  1034. "ctx[%d] : Sync with status=%d, event_cause=%d: req %lld res 0x%x sync_id 0x%x",
  1035. ctx->ctx_id, status, event_cause,
  1036. req->request_id,
  1037. req_isp->fence_map_out[j].resource_handle,
  1038. req_isp->fence_map_out[j].sync_id);
  1039. if (req_isp->fence_map_out[j].sync_id == -1) {
  1040. CAM_WARN(CAM_ISP,
  1041. "ctx[%d Deferred buf_done already signalled, req_id=%llu, j=%d, res=0x%x",
  1042. ctx->ctx_id, req->request_id, j,
  1043. req_isp->fence_map_out[j].resource_handle);
  1044. continue;
  1045. }
  1046. if (!bubble_handling) {
  1047. CAM_WARN(CAM_ISP,
  1048. "ctx[%d] : Req %llu, status=%d res=0x%x should never happen",
  1049. ctx->ctx_id, req->request_id, status,
  1050. req_isp->fence_map_out[j].resource_handle);
  1051. if (req_isp->is_sync_mode) {
  1052. rc = cam_isp_ctx_handle_sync_signal(
  1053. ctx, req, j, status, event_cause);
  1054. } else {
  1055. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  1056. status, event_cause);
  1057. if (rc) {
  1058. CAM_ERR(CAM_ISP,
  1059. "ctx[%d] : Sync signal for Req %llu, sync_id %d status=%d failed with rc = %d",
  1060. ctx->ctx_id, req->request_id,
  1061. req_isp->fence_map_out[j].sync_id,
  1062. status, rc);
  1063. } else {
  1064. req_isp->num_acked++;
  1065. }
  1066. }
  1067. } else {
  1068. req_isp->num_acked++;
  1069. }
  1070. }
  1071. req_isp->num_deferred_acks = 0;
  1072. return rc;
  1073. }
  1074. static int __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  1075. struct cam_isp_context *ctx_isp,
  1076. struct cam_ctx_request *req,
  1077. struct cam_isp_hw_done_event_data *done,
  1078. uint32_t bubble_state,
  1079. bool verify_consumed_addr,
  1080. bool defer_buf_done)
  1081. {
  1082. int rc = 0;
  1083. int i, j;
  1084. struct cam_isp_ctx_req *req_isp;
  1085. struct cam_context *ctx = ctx_isp->base;
  1086. const char *handle_type;
  1087. trace_cam_buf_done("ISP", ctx, req);
  1088. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1089. CAM_DBG(CAM_ISP, "Enter with bubble_state %d, req_bubble_detected %d",
  1090. bubble_state, req_isp->bubble_detected);
  1091. for (i = 0; i < done->num_handles; i++) {
  1092. for (j = 0; j < req_isp->num_fence_map_out; j++) {
  1093. if (verify_consumed_addr &&
  1094. (done->last_consumed_addr[i] !=
  1095. req_isp->fence_map_out[j].image_buf_addr[0]))
  1096. continue;
  1097. if (done->resource_handle[i] ==
  1098. req_isp->fence_map_out[j].resource_handle)
  1099. break;
  1100. }
  1101. if (j == req_isp->num_fence_map_out) {
  1102. /*
  1103. * If not found in current request, it could be
  1104. * belonging to next request, this can happen if
  1105. * IRQ delay happens. It is only valid when the
  1106. * platform doesn't have last consumed address.
  1107. */
  1108. CAM_DBG(CAM_ISP,
  1109. "BUF_DONE for res %s not found in Req %lld ",
  1110. __cam_isp_resource_handle_id_to_type(
  1111. done->resource_handle[i]),
  1112. req->request_id);
  1113. continue;
  1114. }
  1115. if (req_isp->fence_map_out[j].sync_id == -1) {
  1116. if (ctx_isp->isp_device_type == CAM_IFE_DEVICE_TYPE)
  1117. handle_type =
  1118. __cam_isp_resource_handle_id_to_type(
  1119. req_isp->fence_map_out[i].resource_handle);
  1120. else
  1121. handle_type =
  1122. __cam_isp_tfe_resource_handle_id_to_type(
  1123. req_isp->fence_map_out[i].resource_handle);
  1124. CAM_WARN(CAM_ISP,
  1125. "Duplicate BUF_DONE for req %lld : i=%d, j=%d, res=%s",
  1126. req->request_id, i, j, handle_type);
  1127. trace_cam_log_event("Duplicate BufDone",
  1128. handle_type, req->request_id, ctx->ctx_id);
  1129. continue;
  1130. }
  1131. if (defer_buf_done) {
  1132. uint32_t deferred_indx = req_isp->num_deferred_acks;
  1133. /*
  1134. * If we are handling this BUF_DONE event for a request
  1135. * that is still in wait_list, do not signal now,
  1136. * instead mark it as done and handle it later -
  1137. * if this request is going into BUBBLE state later
  1138. * it will automatically be re-applied. If this is not
  1139. * going into BUBBLE, signal fences later.
  1140. * Note - we will come here only if the last consumed
  1141. * address matches with this ports buffer.
  1142. */
  1143. req_isp->deferred_fence_map_index[deferred_indx] = j;
  1144. req_isp->num_deferred_acks++;
  1145. CAM_DBG(CAM_ISP,
  1146. "ctx[%d] : Deferred buf done for %llu with bubble state %d recovery %d",
  1147. ctx->ctx_id, req->request_id, bubble_state,
  1148. req_isp->bubble_report);
  1149. CAM_DBG(CAM_ISP,
  1150. "ctx[%d] : Deferred info : num_acks=%d, fence_map_index=%d, resource_handle=0x%x, sync_id=%d",
  1151. ctx->ctx_id, req_isp->num_deferred_acks, j,
  1152. req_isp->fence_map_out[j].resource_handle,
  1153. req_isp->fence_map_out[j].sync_id);
  1154. continue;
  1155. } else if (!req_isp->bubble_detected) {
  1156. if (req_isp->is_sync_mode) {
  1157. CAM_DBG(CAM_ISP,
  1158. "Hold sync signal: req %lld res 0x%x fd 0x%x, ctx %u",
  1159. req->request_id,
  1160. req_isp->fence_map_out[j].resource_handle,
  1161. req_isp->fence_map_out[j].sync_id,
  1162. ctx->ctx_id);
  1163. rc = cam_isp_ctx_handle_sync_signal(ctx, req, j,
  1164. CAM_SYNC_STATE_SIGNALED_SUCCESS,
  1165. CAM_SYNC_COMMON_EVENT_SUCCESS);
  1166. } else {
  1167. CAM_DBG(CAM_ISP,
  1168. "Sync with success: req %lld res 0x%x fd 0x%x, ctx %u",
  1169. req->request_id,
  1170. req_isp->fence_map_out[j].resource_handle,
  1171. req_isp->fence_map_out[j].sync_id,
  1172. ctx->ctx_id);
  1173. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  1174. CAM_SYNC_STATE_SIGNALED_SUCCESS,
  1175. CAM_SYNC_COMMON_EVENT_SUCCESS);
  1176. }
  1177. if (rc) {
  1178. CAM_DBG(CAM_ISP, "Sync failed with rc = %d",
  1179. rc);
  1180. } else if (req_isp->num_deferred_acks) {
  1181. /* Process deferred buf_done acks */
  1182. __cam_isp_handle_deferred_buf_done(ctx_isp,
  1183. req, false,
  1184. CAM_SYNC_STATE_SIGNALED_SUCCESS,
  1185. CAM_SYNC_COMMON_EVENT_SUCCESS);
  1186. }
  1187. } else if (!req_isp->bubble_report) {
  1188. CAM_DBG(CAM_ISP,
  1189. "Sync with failure: req %lld res 0x%x fd 0x%x, ctx %u",
  1190. req->request_id,
  1191. req_isp->fence_map_out[j].resource_handle,
  1192. req_isp->fence_map_out[j].sync_id,
  1193. ctx->ctx_id);
  1194. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  1195. CAM_SYNC_STATE_SIGNALED_ERROR,
  1196. CAM_SYNC_ISP_EVENT_BUBBLE);
  1197. if (rc) {
  1198. CAM_ERR(CAM_ISP, "Sync failed with rc = %d",
  1199. rc);
  1200. } else if (req_isp->num_deferred_acks) {
  1201. /* Process deferred buf_done acks */
  1202. __cam_isp_handle_deferred_buf_done(ctx_isp, req,
  1203. false,
  1204. CAM_SYNC_STATE_SIGNALED_ERROR,
  1205. CAM_SYNC_ISP_EVENT_BUBBLE);
  1206. }
  1207. } else {
  1208. /*
  1209. * Ignore the buffer done if bubble detect is on
  1210. * Increment the ack number here, and queue the
  1211. * request back to pending list whenever all the
  1212. * buffers are done.
  1213. */
  1214. req_isp->num_acked++;
  1215. CAM_DBG(CAM_ISP,
  1216. "buf done with bubble state %d recovery %d",
  1217. bubble_state, req_isp->bubble_report);
  1218. /* Process deferred buf_done acks */
  1219. if (req_isp->num_deferred_acks)
  1220. __cam_isp_handle_deferred_buf_done(ctx_isp, req,
  1221. true,
  1222. CAM_SYNC_STATE_SIGNALED_ERROR,
  1223. CAM_SYNC_ISP_EVENT_BUBBLE);
  1224. continue;
  1225. }
  1226. CAM_DBG(CAM_ISP, "req %lld, reset sync id 0x%x ctx %u",
  1227. req->request_id,
  1228. req_isp->fence_map_out[j].sync_id, ctx->ctx_id);
  1229. if (!rc) {
  1230. req_isp->num_acked++;
  1231. }
  1232. if ((ctx_isp->use_frame_header_ts) &&
  1233. (req_isp->hw_update_data.frame_header_res_id ==
  1234. req_isp->fence_map_out[j].resource_handle))
  1235. __cam_isp_ctx_send_sof_timestamp_frame_header(
  1236. ctx_isp,
  1237. req_isp->hw_update_data.frame_header_cpu_addr,
  1238. req->request_id, CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1239. }
  1240. if (req_isp->num_acked > req_isp->num_fence_map_out) {
  1241. /* Should not happen */
  1242. CAM_ERR(CAM_ISP,
  1243. "WARNING: req_id %lld num_acked %d > map_out %d, ctx %u",
  1244. req->request_id, req_isp->num_acked,
  1245. req_isp->num_fence_map_out, ctx->ctx_id);
  1246. }
  1247. if (req_isp->num_acked != req_isp->num_fence_map_out)
  1248. return rc;
  1249. rc = __cam_isp_ctx_handle_buf_done_for_req_list(ctx_isp, req);
  1250. return rc;
  1251. }
  1252. static int __cam_isp_ctx_handle_buf_done(
  1253. struct cam_isp_context *ctx_isp,
  1254. struct cam_isp_hw_done_event_data *done,
  1255. uint32_t bubble_state)
  1256. {
  1257. int rc = 0;
  1258. struct cam_ctx_request *req;
  1259. struct cam_context *ctx = ctx_isp->base;
  1260. struct cam_isp_hw_done_event_data done_next_req;
  1261. if (list_empty(&ctx->active_req_list)) {
  1262. CAM_WARN(CAM_ISP, "Buf done with no active request");
  1263. return 0;
  1264. }
  1265. req = list_first_entry(&ctx->active_req_list,
  1266. struct cam_ctx_request, list);
  1267. rc = __cam_isp_ctx_handle_buf_done_for_request(ctx_isp, req, done,
  1268. bubble_state, &done_next_req);
  1269. if (done_next_req.num_handles) {
  1270. struct cam_isp_hw_done_event_data unhandled_res;
  1271. struct cam_ctx_request *next_req = list_last_entry(
  1272. &ctx->active_req_list, struct cam_ctx_request, list);
  1273. if (next_req->request_id != req->request_id) {
  1274. /*
  1275. * Few resource handles are already signalled in the
  1276. * current request, lets check if there is another
  1277. * request waiting for these resources. This can
  1278. * happen if handling some of next request's buf done
  1279. * events are happening first before handling current
  1280. * request's remaining buf dones due to IRQ scheduling.
  1281. * Lets check only one more request as we will have
  1282. * maximum of 2 requests in active_list at any time.
  1283. */
  1284. CAM_WARN(CAM_ISP,
  1285. "Unhandled buf done resources for req %lld, trying next request %lld in active_list",
  1286. req->request_id, next_req->request_id);
  1287. __cam_isp_ctx_handle_buf_done_for_request(ctx_isp,
  1288. next_req, &done_next_req,
  1289. bubble_state, &unhandled_res);
  1290. if (unhandled_res.num_handles == 0)
  1291. CAM_INFO(CAM_ISP,
  1292. "BUF Done event handed for next request %lld",
  1293. next_req->request_id);
  1294. else
  1295. CAM_ERR(CAM_ISP,
  1296. "BUF Done not handled for next request %lld",
  1297. next_req->request_id);
  1298. } else {
  1299. CAM_WARN(CAM_ISP,
  1300. "Req %lld only active request, spurious buf_done rxd",
  1301. req->request_id);
  1302. }
  1303. }
  1304. return rc;
  1305. }
  1306. static void __cam_isp_ctx_buf_done_match_req(
  1307. struct cam_ctx_request *req,
  1308. struct cam_isp_hw_done_event_data *done,
  1309. bool *irq_delay_detected)
  1310. {
  1311. int i, j;
  1312. uint32_t match_count = 0;
  1313. struct cam_isp_ctx_req *req_isp;
  1314. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1315. for (i = 0; i < done->num_handles; i++) {
  1316. for (j = 0; j < req_isp->num_fence_map_out; j++) {
  1317. if ((done->resource_handle[i] ==
  1318. req_isp->fence_map_out[j].resource_handle) &&
  1319. (done->last_consumed_addr[i] ==
  1320. req_isp->fence_map_out[j].image_buf_addr[0])) {
  1321. match_count++;
  1322. break;
  1323. }
  1324. }
  1325. }
  1326. if (match_count > 0)
  1327. *irq_delay_detected = true;
  1328. else
  1329. *irq_delay_detected = false;
  1330. CAM_DBG(CAM_ISP,
  1331. "buf done num handles %d match count %d for next req:%lld",
  1332. done->num_handles, match_count, req->request_id);
  1333. CAM_DBG(CAM_ISP,
  1334. "irq_delay_detected %d", *irq_delay_detected);
  1335. }
  1336. static int __cam_isp_ctx_handle_buf_done_verify_addr(
  1337. struct cam_isp_context *ctx_isp,
  1338. struct cam_isp_hw_done_event_data *done,
  1339. uint32_t bubble_state)
  1340. {
  1341. int rc = 0;
  1342. bool irq_delay_detected = false;
  1343. struct cam_ctx_request *req;
  1344. struct cam_ctx_request *next_req = NULL;
  1345. struct cam_context *ctx = ctx_isp->base;
  1346. bool req_in_wait_list = false;
  1347. if (list_empty(&ctx->active_req_list)) {
  1348. if (!list_empty(&ctx->wait_req_list)) {
  1349. struct cam_isp_ctx_req *req_isp;
  1350. req = list_first_entry(&ctx->wait_req_list,
  1351. struct cam_ctx_request, list);
  1352. req_in_wait_list = true;
  1353. if (ctx_isp->last_applied_req_id !=
  1354. ctx_isp->last_bufdone_err_apply_req_id) {
  1355. CAM_WARN(CAM_ISP,
  1356. "Buf done with no active request but with req in wait list, req %llu last apply id:%lld last err id:%lld",
  1357. req->request_id,
  1358. ctx_isp->last_applied_req_id,
  1359. ctx_isp->last_bufdone_err_apply_req_id);
  1360. ctx_isp->last_bufdone_err_apply_req_id =
  1361. ctx_isp->last_applied_req_id;
  1362. }
  1363. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1364. /*
  1365. * Verify consumed address for this request to make sure
  1366. * we are handling the buf_done for the correct
  1367. * buffer. Also defer actual buf_done handling, i.e
  1368. * do not signal the fence as this request may go into
  1369. * Bubble state eventully.
  1370. */
  1371. rc =
  1372. __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  1373. ctx_isp, req, done, bubble_state, true, true);
  1374. }
  1375. if (!req_in_wait_list && (ctx_isp->last_applied_req_id !=
  1376. ctx_isp->last_bufdone_err_apply_req_id)) {
  1377. CAM_WARN(CAM_ISP,
  1378. "Buf done with no active request bubble_state=%d last_applied_req_id:%lld ",
  1379. bubble_state, ctx_isp->last_applied_req_id);
  1380. ctx_isp->last_bufdone_err_apply_req_id =
  1381. ctx_isp->last_applied_req_id;
  1382. }
  1383. return 0;
  1384. }
  1385. req = list_first_entry(&ctx->active_req_list,
  1386. struct cam_ctx_request, list);
  1387. if (ctx_isp->active_req_cnt > 1) {
  1388. next_req = list_last_entry(
  1389. &ctx->active_req_list,
  1390. struct cam_ctx_request, list);
  1391. if (next_req->request_id != req->request_id)
  1392. __cam_isp_ctx_buf_done_match_req(next_req, done,
  1393. &irq_delay_detected);
  1394. else
  1395. CAM_WARN(CAM_ISP,
  1396. "Req %lld only active request, spurious buf_done rxd",
  1397. req->request_id);
  1398. }
  1399. /*
  1400. * If irq delay isn't detected, then we need to verify
  1401. * the consumed address for current req, otherwise, we
  1402. * can't verify the consumed address.
  1403. */
  1404. rc = __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  1405. ctx_isp, req, done, bubble_state,
  1406. !irq_delay_detected, false);
  1407. /*
  1408. * Verify the consumed address for next req all the time,
  1409. * since the reported buf done event may belong to current
  1410. * req, then we can't signal this event for next req.
  1411. */
  1412. if (!rc && irq_delay_detected)
  1413. rc = __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  1414. ctx_isp, next_req, done,
  1415. bubble_state, true, false);
  1416. return rc;
  1417. }
  1418. static int __cam_isp_ctx_handle_buf_done_in_activated_state(
  1419. struct cam_isp_context *ctx_isp,
  1420. struct cam_isp_hw_done_event_data *done,
  1421. uint32_t bubble_state)
  1422. {
  1423. int rc = 0;
  1424. if (ctx_isp->support_consumed_addr)
  1425. rc = __cam_isp_ctx_handle_buf_done_verify_addr(
  1426. ctx_isp, done, bubble_state);
  1427. else
  1428. rc = __cam_isp_ctx_handle_buf_done(
  1429. ctx_isp, done, bubble_state);
  1430. return rc;
  1431. }
  1432. static int __cam_isp_ctx_apply_req_offline(
  1433. void *priv, void *data)
  1434. {
  1435. int rc = 0;
  1436. int64_t prev_applied_req;
  1437. struct cam_context *ctx = NULL;
  1438. struct cam_isp_context *ctx_isp = priv;
  1439. struct cam_ctx_request *req;
  1440. struct cam_isp_ctx_req *req_isp;
  1441. struct cam_hw_config_args cfg;
  1442. if (!ctx_isp) {
  1443. CAM_ERR(CAM_ISP, "Invalid ctx_isp:%pK", ctx);
  1444. rc = -EINVAL;
  1445. goto end;
  1446. }
  1447. ctx = ctx_isp->base;
  1448. if (list_empty(&ctx->pending_req_list)) {
  1449. CAM_DBG(CAM_ISP, "No pending requests to apply");
  1450. rc = -EFAULT;
  1451. goto end;
  1452. }
  1453. if ((ctx->state != CAM_CTX_ACTIVATED) ||
  1454. (!atomic_read(&ctx_isp->rxd_epoch)) ||
  1455. (ctx_isp->substate_activated == CAM_ISP_CTX_ACTIVATED_APPLIED))
  1456. goto end;
  1457. if (ctx_isp->active_req_cnt >= 2)
  1458. goto end;
  1459. spin_lock_bh(&ctx->lock);
  1460. req = list_first_entry(&ctx->pending_req_list, struct cam_ctx_request,
  1461. list);
  1462. spin_unlock_bh(&ctx->lock);
  1463. CAM_DBG(CAM_REQ, "Apply request %lld in substate %d ctx %u",
  1464. req->request_id, ctx_isp->substate_activated, ctx->ctx_id);
  1465. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1466. memset(&cfg, 0, sizeof(cfg));
  1467. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  1468. cfg.request_id = req->request_id;
  1469. cfg.hw_update_entries = req_isp->cfg;
  1470. cfg.num_hw_update_entries = req_isp->num_cfg;
  1471. cfg.priv = &req_isp->hw_update_data;
  1472. cfg.init_packet = 0;
  1473. /*
  1474. * Offline mode may receive the SOF and REG_UPD earlier than
  1475. * CDM processing return back, so we set the substate before
  1476. * apply setting.
  1477. */
  1478. spin_lock_bh(&ctx->lock);
  1479. atomic_set(&ctx_isp->rxd_epoch, 0);
  1480. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_APPLIED;
  1481. prev_applied_req = ctx_isp->last_applied_req_id;
  1482. ctx_isp->last_applied_req_id = req->request_id;
  1483. atomic_set(&ctx_isp->apply_in_progress, 1);
  1484. list_del_init(&req->list);
  1485. list_add_tail(&req->list, &ctx->wait_req_list);
  1486. spin_unlock_bh(&ctx->lock);
  1487. rc = ctx->hw_mgr_intf->hw_config(ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  1488. if (rc) {
  1489. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not apply the configuration");
  1490. spin_lock_bh(&ctx->lock);
  1491. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  1492. ctx_isp->last_applied_req_id = prev_applied_req;
  1493. atomic_set(&ctx_isp->apply_in_progress, 0);
  1494. list_del_init(&req->list);
  1495. list_add(&req->list, &ctx->pending_req_list);
  1496. spin_unlock_bh(&ctx->lock);
  1497. } else {
  1498. atomic_set(&ctx_isp->apply_in_progress, 0);
  1499. CAM_DBG(CAM_ISP, "New substate state %d, applied req %lld",
  1500. CAM_ISP_CTX_ACTIVATED_APPLIED,
  1501. ctx_isp->last_applied_req_id);
  1502. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1503. CAM_ISP_STATE_CHANGE_TRIGGER_APPLIED,
  1504. req->request_id);
  1505. }
  1506. end:
  1507. return rc;
  1508. }
  1509. static int __cam_isp_ctx_schedule_apply_req_offline(
  1510. struct cam_isp_context *ctx_isp)
  1511. {
  1512. int rc = 0;
  1513. struct crm_workq_task *task;
  1514. task = cam_req_mgr_workq_get_task(ctx_isp->workq);
  1515. if (!task) {
  1516. CAM_ERR(CAM_ISP, "No task for worker");
  1517. return -ENOMEM;
  1518. }
  1519. task->process_cb = __cam_isp_ctx_apply_req_offline;
  1520. rc = cam_req_mgr_workq_enqueue_task(task, ctx_isp, CRM_TASK_PRIORITY_0);
  1521. if (rc)
  1522. CAM_ERR(CAM_ISP, "Failed to schedule task rc:%d", rc);
  1523. return rc;
  1524. }
  1525. static int __cam_isp_ctx_offline_epoch_in_activated_state(
  1526. struct cam_isp_context *ctx_isp, void *evt_data)
  1527. {
  1528. struct cam_context *ctx = ctx_isp->base;
  1529. struct cam_ctx_request *req, *req_temp;
  1530. uint64_t request_id = 0;
  1531. atomic_set(&ctx_isp->rxd_epoch, 1);
  1532. CAM_DBG(CAM_ISP, "SOF frame %lld ctx %u", ctx_isp->frame_id,
  1533. ctx->ctx_id);
  1534. list_for_each_entry_safe(req, req_temp, &ctx->active_req_list, list) {
  1535. if (req->request_id > ctx_isp->reported_req_id) {
  1536. request_id = req->request_id;
  1537. ctx_isp->reported_req_id = request_id;
  1538. break;
  1539. }
  1540. }
  1541. __cam_isp_ctx_schedule_apply_req_offline(ctx_isp);
  1542. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1543. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1544. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1545. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH,
  1546. request_id);
  1547. return 0;
  1548. }
  1549. static int __cam_isp_ctx_reg_upd_in_epoch_bubble_state(
  1550. struct cam_isp_context *ctx_isp, void *evt_data)
  1551. {
  1552. if (ctx_isp->frame_id == 1)
  1553. CAM_DBG(CAM_ISP, "Reg update in Substate[%s] for early PCR",
  1554. __cam_isp_ctx_substate_val_to_type(
  1555. ctx_isp->substate_activated));
  1556. else
  1557. CAM_WARN_RATE_LIMIT(CAM_ISP,
  1558. "ctx_id:%d Unexpected reg update in activated Substate[%s] for frame_id:%lld",
  1559. ctx_isp->base->ctx_id,
  1560. __cam_isp_ctx_substate_val_to_type(
  1561. ctx_isp->substate_activated),
  1562. ctx_isp->frame_id);
  1563. return 0;
  1564. }
  1565. static int __cam_isp_ctx_reg_upd_in_applied_state(
  1566. struct cam_isp_context *ctx_isp, void *evt_data)
  1567. {
  1568. int rc = 0;
  1569. struct cam_ctx_request *req;
  1570. struct cam_context *ctx = ctx_isp->base;
  1571. struct cam_isp_ctx_req *req_isp;
  1572. uint64_t request_id = 0;
  1573. struct cam_req_mgr_notify_rup notify_rup_info;
  1574. if (list_empty(&ctx->wait_req_list)) {
  1575. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request");
  1576. goto end;
  1577. }
  1578. req = list_first_entry(&ctx->wait_req_list,
  1579. struct cam_ctx_request, list);
  1580. list_del_init(&req->list);
  1581. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1582. if (req_isp->num_fence_map_out != 0) {
  1583. list_add_tail(&req->list, &ctx->active_req_list);
  1584. ctx_isp->active_req_cnt++;
  1585. if (req_isp->is_sync_mode) {
  1586. request_id = req->request_id;
  1587. notify_rup_info.link_hdl = ctx->link_hdl;
  1588. notify_rup_info.req_id = request_id;
  1589. ctx->ctx_crm_intf->notify_rup(&notify_rup_info);
  1590. atomic_set(&req_isp->buf_done_ready, notify_rup_info.state);
  1591. }
  1592. CAM_DBG(CAM_REQ,
  1593. "move request %lld to active list(cnt = %d), state %d sync mode %d ctx %u link %x",
  1594. req->request_id, ctx_isp->active_req_cnt,
  1595. notify_rup_info.state,
  1596. req_isp->is_sync_mode,
  1597. ctx->ctx_id, ctx->link_hdl);
  1598. __cam_isp_ctx_update_event_record(ctx_isp,
  1599. CAM_ISP_CTX_EVENT_RUP, req);
  1600. } else {
  1601. /* no io config, so the request is completed. */
  1602. list_add_tail(&req->list, &ctx->free_req_list);
  1603. CAM_DBG(CAM_ISP,
  1604. "move active request %lld to free list(cnt = %d), ctx %u",
  1605. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  1606. }
  1607. /*
  1608. * This function only called directly from applied and bubble applied
  1609. * state so change substate here.
  1610. */
  1611. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  1612. CAM_DBG(CAM_ISP, "next Substate[%s]",
  1613. __cam_isp_ctx_substate_val_to_type(
  1614. ctx_isp->substate_activated));
  1615. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1616. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE, request_id);
  1617. end:
  1618. return rc;
  1619. }
  1620. static int __cam_isp_ctx_notify_sof_in_activated_state(
  1621. struct cam_isp_context *ctx_isp, void *evt_data)
  1622. {
  1623. int rc = 0;
  1624. uint64_t request_id = 0;
  1625. struct cam_req_mgr_trigger_notify notify;
  1626. struct cam_context *ctx = ctx_isp->base;
  1627. struct cam_ctx_request *req;
  1628. struct cam_isp_ctx_req *req_isp;
  1629. struct cam_hw_cmd_args hw_cmd_args;
  1630. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  1631. uint64_t last_cdm_done_req = 0;
  1632. struct cam_isp_hw_epoch_event_data *epoch_done_event_data =
  1633. (struct cam_isp_hw_epoch_event_data *)evt_data;
  1634. if (!evt_data) {
  1635. CAM_ERR(CAM_ISP, "invalid event data");
  1636. return -EINVAL;
  1637. }
  1638. ctx_isp->frame_id_meta = epoch_done_event_data->frame_id_meta;
  1639. if (atomic_read(&ctx_isp->process_bubble)) {
  1640. if (list_empty(&ctx->active_req_list)) {
  1641. CAM_ERR(CAM_ISP,
  1642. "No available active req in bubble");
  1643. atomic_set(&ctx_isp->process_bubble, 0);
  1644. ctx_isp->bubble_frame_cnt = 0;
  1645. rc = -EINVAL;
  1646. return rc;
  1647. }
  1648. if (ctx_isp->last_sof_timestamp ==
  1649. ctx_isp->sof_timestamp_val) {
  1650. CAM_DBG(CAM_ISP,
  1651. "Tasklet delay detected! Bubble frame check skipped, sof_timestamp: %lld",
  1652. ctx_isp->sof_timestamp_val);
  1653. goto notify_only;
  1654. }
  1655. req = list_first_entry(&ctx->active_req_list,
  1656. struct cam_ctx_request, list);
  1657. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1658. if (ctx_isp->bubble_frame_cnt >= 1 &&
  1659. req_isp->bubble_detected) {
  1660. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  1661. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  1662. isp_hw_cmd_args.cmd_type =
  1663. CAM_ISP_HW_MGR_GET_LAST_CDM_DONE;
  1664. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  1665. rc = ctx->hw_mgr_intf->hw_cmd(
  1666. ctx->hw_mgr_intf->hw_mgr_priv,
  1667. &hw_cmd_args);
  1668. if (rc) {
  1669. CAM_ERR(CAM_ISP, "HW command failed");
  1670. return rc;
  1671. }
  1672. last_cdm_done_req = isp_hw_cmd_args.u.last_cdm_done;
  1673. CAM_DBG(CAM_ISP, "last_cdm_done req: %d",
  1674. last_cdm_done_req);
  1675. if (last_cdm_done_req >= req->request_id) {
  1676. CAM_DBG(CAM_ISP,
  1677. "CDM callback detected for req: %lld, possible buf_done delay, waiting for buf_done",
  1678. req->request_id);
  1679. ctx_isp->bubble_frame_cnt = 0;
  1680. } else {
  1681. CAM_DBG(CAM_ISP,
  1682. "CDM callback not happened for req: %lld, possible CDM stuck or workqueue delay",
  1683. req->request_id);
  1684. req_isp->num_acked = 0;
  1685. req_isp->num_deferred_acks = 0;
  1686. ctx_isp->bubble_frame_cnt = 0;
  1687. req_isp->bubble_detected = false;
  1688. req_isp->cdm_reset_before_apply = true;
  1689. list_del_init(&req->list);
  1690. list_add(&req->list, &ctx->pending_req_list);
  1691. atomic_set(&ctx_isp->process_bubble, 0);
  1692. ctx_isp->active_req_cnt--;
  1693. CAM_DBG(CAM_REQ,
  1694. "Move active req: %lld to pending list(cnt = %d) [bubble re-apply],ctx %u",
  1695. req->request_id,
  1696. ctx_isp->active_req_cnt, ctx->ctx_id);
  1697. }
  1698. } else if (req_isp->bubble_detected) {
  1699. ctx_isp->bubble_frame_cnt++;
  1700. CAM_DBG(CAM_ISP,
  1701. "Waiting on bufdone for bubble req: %lld, since frame_cnt = %lld",
  1702. req->request_id,
  1703. ctx_isp->bubble_frame_cnt);
  1704. } else {
  1705. CAM_DBG(CAM_ISP, "Delayed bufdone for req: %lld",
  1706. req->request_id);
  1707. }
  1708. }
  1709. notify_only:
  1710. /*
  1711. * notify reqmgr with sof signal. Note, due to scheduling delay
  1712. * we can run into situation that two active requests has already
  1713. * be in the active queue while we try to do the notification.
  1714. * In this case, we need to skip the current notification. This
  1715. * helps the state machine to catch up the delay.
  1716. */
  1717. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger &&
  1718. ctx_isp->active_req_cnt <= 2) {
  1719. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF) {
  1720. notify.link_hdl = ctx->link_hdl;
  1721. notify.dev_hdl = ctx->dev_hdl;
  1722. notify.frame_id = ctx_isp->frame_id;
  1723. notify.trigger = CAM_TRIGGER_POINT_SOF;
  1724. notify.req_id = ctx_isp->req_info.last_bufdone_req_id;
  1725. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  1726. notify.sof_boottime = ctx_isp->boot_timestamp;
  1727. notify.trigger_id = ctx_isp->trigger_id;
  1728. if (!list_empty(&ctx->active_req_list)) {
  1729. req = list_first_entry(&ctx->active_req_list,
  1730. struct cam_ctx_request, list);
  1731. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1732. notify.fps = req_isp->hw_update_data.fps;
  1733. } else {
  1734. notify.fps = 0;
  1735. }
  1736. ctx->ctx_crm_intf->notify_trigger(&notify);
  1737. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld ctx %u",
  1738. ctx_isp->frame_id, ctx->ctx_id);
  1739. }
  1740. list_for_each_entry(req, &ctx->active_req_list, list) {
  1741. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1742. if ((!req_isp->bubble_detected) &&
  1743. (req->request_id > ctx_isp->reported_req_id)) {
  1744. request_id = req->request_id;
  1745. __cam_isp_ctx_update_event_record(ctx_isp,
  1746. CAM_ISP_CTX_EVENT_EPOCH, req);
  1747. break;
  1748. }
  1749. }
  1750. if (ctx_isp->substate_activated == CAM_ISP_CTX_ACTIVATED_BUBBLE)
  1751. request_id = 0;
  1752. if (request_id != 0)
  1753. ctx_isp->reported_req_id = request_id;
  1754. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1755. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1756. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1757. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH,
  1758. request_id);
  1759. } else {
  1760. CAM_ERR_RATE_LIMIT(CAM_ISP,
  1761. "Can not notify SOF to CRM for ctx %u",
  1762. ctx->ctx_id);
  1763. rc = -EFAULT;
  1764. }
  1765. ctx_isp->last_sof_timestamp = ctx_isp->sof_timestamp_val;
  1766. return 0;
  1767. }
  1768. static int __cam_isp_ctx_notify_eof_in_activated_state(
  1769. struct cam_isp_context *ctx_isp, void *evt_data)
  1770. {
  1771. int rc = 0;
  1772. struct cam_req_mgr_trigger_notify notify;
  1773. struct cam_context *ctx = ctx_isp->base;
  1774. if (!(ctx_isp->subscribe_event & CAM_TRIGGER_POINT_EOF))
  1775. return rc;
  1776. /* notify reqmgr with eof signal */
  1777. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger) {
  1778. notify.link_hdl = ctx->link_hdl;
  1779. notify.dev_hdl = ctx->dev_hdl;
  1780. notify.frame_id = ctx_isp->frame_id;
  1781. notify.trigger = CAM_TRIGGER_POINT_EOF;
  1782. notify.trigger_id = ctx_isp->trigger_id;
  1783. ctx->ctx_crm_intf->notify_trigger(&notify);
  1784. CAM_DBG(CAM_ISP, "Notify CRM EOF frame %lld ctx %u",
  1785. ctx_isp->frame_id, ctx->ctx_id);
  1786. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1787. CAM_ISP_STATE_CHANGE_TRIGGER_EOF, 0);
  1788. } else {
  1789. CAM_ERR(CAM_ISP, "Can not notify EOF to CRM for ctx %u",
  1790. ctx->ctx_id);
  1791. rc = -EFAULT;
  1792. }
  1793. return rc;
  1794. }
  1795. static int __cam_isp_ctx_reg_upd_in_hw_error(
  1796. struct cam_isp_context *ctx_isp, void *evt_data)
  1797. {
  1798. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  1799. return 0;
  1800. }
  1801. static int __cam_isp_ctx_sof_in_activated_state(
  1802. struct cam_isp_context *ctx_isp, void *evt_data)
  1803. {
  1804. int rc = 0;
  1805. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  1806. struct cam_ctx_request *req = NULL;
  1807. struct cam_context *ctx = ctx_isp->base;
  1808. uint64_t request_id = 0;
  1809. /* First check if there is a valid request in active list */
  1810. list_for_each_entry(req, &ctx->active_req_list, list) {
  1811. if (req->request_id > ctx_isp->reported_req_id) {
  1812. request_id = req->request_id;
  1813. break;
  1814. }
  1815. }
  1816. /*
  1817. * If nothing in active list, current request might have not moved
  1818. * from wait to active list. This could happen if REG_UPDATE to sw
  1819. * is coming immediately after SOF
  1820. */
  1821. if (request_id == 0) {
  1822. req = list_first_entry(&ctx->wait_req_list,
  1823. struct cam_ctx_request, list);
  1824. if (req)
  1825. request_id = req->request_id;
  1826. }
  1827. if (!evt_data) {
  1828. CAM_ERR(CAM_ISP, "in valid sof event data");
  1829. return -EINVAL;
  1830. }
  1831. ctx_isp->frame_id++;
  1832. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  1833. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  1834. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1835. CAM_ISP_STATE_CHANGE_TRIGGER_SOF, request_id);
  1836. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx, ctx %u link %x",
  1837. ctx_isp->frame_id, ctx_isp->sof_timestamp_val,
  1838. ctx->ctx_id, ctx->link_hdl);
  1839. return rc;
  1840. }
  1841. static int __cam_isp_ctx_reg_upd_in_sof(struct cam_isp_context *ctx_isp,
  1842. void *evt_data)
  1843. {
  1844. int rc = 0;
  1845. struct cam_ctx_request *req = NULL;
  1846. struct cam_isp_ctx_req *req_isp;
  1847. struct cam_context *ctx = ctx_isp->base;
  1848. if (ctx->state != CAM_CTX_ACTIVATED && ctx_isp->frame_id > 1) {
  1849. CAM_DBG(CAM_ISP, "invalid RUP");
  1850. goto end;
  1851. }
  1852. /*
  1853. * This is for the first update. The initial setting will
  1854. * cause the reg_upd in the first frame.
  1855. */
  1856. if (!list_empty(&ctx->wait_req_list)) {
  1857. req = list_first_entry(&ctx->wait_req_list,
  1858. struct cam_ctx_request, list);
  1859. list_del_init(&req->list);
  1860. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1861. if (req_isp->num_fence_map_out == req_isp->num_acked)
  1862. list_add_tail(&req->list, &ctx->free_req_list);
  1863. else
  1864. CAM_ERR(CAM_ISP,
  1865. "receive rup in unexpected state");
  1866. }
  1867. if (req != NULL) {
  1868. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1869. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  1870. req->request_id);
  1871. }
  1872. end:
  1873. return rc;
  1874. }
  1875. static int __cam_isp_ctx_epoch_in_applied(struct cam_isp_context *ctx_isp,
  1876. void *evt_data)
  1877. {
  1878. bool rc = false;
  1879. uint64_t request_id = 0;
  1880. uint32_t sof_event_status = CAM_REQ_MGR_SOF_EVENT_SUCCESS;
  1881. struct cam_req_mgr_trigger_notify notify;
  1882. struct cam_ctx_request *req;
  1883. struct cam_isp_ctx_req *req_isp;
  1884. struct cam_context *ctx = ctx_isp->base;
  1885. struct cam_isp_hw_epoch_event_data *epoch_done_event_data =
  1886. (struct cam_isp_hw_epoch_event_data *)evt_data;
  1887. if (!evt_data) {
  1888. CAM_ERR(CAM_ISP, "invalid event data");
  1889. return -EINVAL;
  1890. }
  1891. ctx_isp->frame_id_meta = epoch_done_event_data->frame_id_meta;
  1892. if (list_empty(&ctx->wait_req_list)) {
  1893. /*
  1894. * If no wait req in epoch, this is an error case.
  1895. * The recovery is to go back to sof state
  1896. */
  1897. CAM_ERR(CAM_ISP, "Ctx:%d No wait request", ctx->ctx_id);
  1898. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  1899. /* Send SOF event as empty frame*/
  1900. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1901. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1902. __cam_isp_ctx_update_event_record(ctx_isp,
  1903. CAM_ISP_CTX_EVENT_EPOCH, NULL);
  1904. goto end;
  1905. }
  1906. /* Update state prior to notifying CRM */
  1907. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  1908. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  1909. list);
  1910. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  1911. req_isp->bubble_detected = true;
  1912. req_isp->reapply = true;
  1913. req_isp->cdm_reset_before_apply = false;
  1914. CAM_INFO_RATE_LIMIT(CAM_ISP, "ctx:%d link %x Report Bubble flag %d req id:%lld",
  1915. ctx->ctx_id, ctx->link_hdl,
  1916. req_isp->bubble_report, req->request_id);
  1917. if (req_isp->bubble_report && ctx->ctx_crm_intf &&
  1918. ctx->ctx_crm_intf->notify_err) {
  1919. struct cam_req_mgr_error_notify notify;
  1920. notify.link_hdl = ctx->link_hdl;
  1921. notify.dev_hdl = ctx->dev_hdl;
  1922. notify.req_id = req->request_id;
  1923. notify.error = CRM_KMD_ERR_BUBBLE;
  1924. notify.trigger = 0;
  1925. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF)
  1926. notify.trigger = CAM_TRIGGER_POINT_SOF;
  1927. notify.frame_id = ctx_isp->frame_id;
  1928. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  1929. notify.sof_boottime_val = ctx_isp->boot_timestamp;
  1930. notify.need_recovery = true;
  1931. CAM_WARN_RATE_LIMIT(CAM_ISP,
  1932. "Notify CRM about Bubble req %lld frame %lld, ctx %u",
  1933. req->request_id, ctx_isp->frame_id, ctx->ctx_id);
  1934. trace_cam_log_event("Bubble", "Rcvd epoch in applied state",
  1935. req->request_id, ctx->ctx_id);
  1936. rc = ctx->ctx_crm_intf->notify_err(&notify);
  1937. CAM_DBG(CAM_CRM, "Need bubble recovery %d", rc);
  1938. if (rc) {
  1939. req_isp->bubble_detected = false;
  1940. req_isp->reapply = false;
  1941. ctx_isp->substate_activated =
  1942. CAM_ISP_CTX_ACTIVATED_APPLIED;
  1943. CAM_DBG(CAM_ISP, "Disable bubble for ctx %d link %d",
  1944. ctx->ctx_id, ctx->link_hdl);
  1945. return 0;
  1946. }
  1947. atomic_set(&ctx_isp->process_bubble, 1);
  1948. } else {
  1949. req_isp->bubble_report = 0;
  1950. CAM_DBG(CAM_ISP, "Skip bubble recovery for req %lld ctx %u",
  1951. req->request_id, ctx->ctx_id);
  1952. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger &&
  1953. ctx_isp->active_req_cnt <= 1) {
  1954. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF) {
  1955. notify.link_hdl = ctx->link_hdl;
  1956. notify.dev_hdl = ctx->dev_hdl;
  1957. notify.frame_id = ctx_isp->frame_id;
  1958. notify.trigger = CAM_TRIGGER_POINT_SOF;
  1959. notify.req_id =
  1960. ctx_isp->req_info.last_bufdone_req_id;
  1961. notify.sof_timestamp_val =
  1962. ctx_isp->sof_timestamp_val;
  1963. notify.trigger_id = ctx_isp->trigger_id;
  1964. ctx->ctx_crm_intf->notify_trigger(&notify);
  1965. CAM_DBG(CAM_ISP,
  1966. "Notify CRM SOF frame %lld ctx %u",
  1967. ctx_isp->frame_id, ctx->ctx_id);
  1968. }
  1969. }
  1970. }
  1971. /*
  1972. * Always move the request to active list. Let buf done
  1973. * function handles the rest.
  1974. */
  1975. list_del_init(&req->list);
  1976. list_add_tail(&req->list, &ctx->active_req_list);
  1977. ctx_isp->active_req_cnt++;
  1978. CAM_DBG(CAM_REQ, "move request %lld to active list(cnt = %d), ctx %u",
  1979. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  1980. /*
  1981. * Update the record before req pointer to
  1982. * other invalid req.
  1983. */
  1984. __cam_isp_ctx_update_event_record(ctx_isp,
  1985. CAM_ISP_CTX_EVENT_EPOCH, req);
  1986. /*
  1987. * Get the req again from active_req_list in case
  1988. * the active req cnt is 2.
  1989. */
  1990. list_for_each_entry(req, &ctx->active_req_list, list) {
  1991. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1992. if ((!req_isp->bubble_report) &&
  1993. (req->request_id > ctx_isp->reported_req_id)) {
  1994. request_id = req->request_id;
  1995. ctx_isp->reported_req_id = request_id;
  1996. CAM_DBG(CAM_ISP,
  1997. "ctx %d reported_req_id update to %lld",
  1998. ctx->ctx_id, ctx_isp->reported_req_id);
  1999. break;
  2000. }
  2001. }
  2002. if ((request_id != 0) && req_isp->bubble_detected)
  2003. sof_event_status = CAM_REQ_MGR_SOF_EVENT_ERROR;
  2004. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2005. sof_event_status);
  2006. cam_req_mgr_debug_delay_detect();
  2007. trace_cam_delay_detect("ISP",
  2008. "bubble epoch_in_applied", req->request_id,
  2009. ctx->ctx_id, ctx->link_hdl, ctx->session_hdl,
  2010. CAM_DEFAULT_VALUE);
  2011. end:
  2012. if (request_id == 0) {
  2013. req = list_last_entry(&ctx->active_req_list,
  2014. struct cam_ctx_request, list);
  2015. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2016. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, req->request_id);
  2017. } else {
  2018. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2019. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, request_id);
  2020. }
  2021. CAM_DBG(CAM_ISP, "next Substate[%s]",
  2022. __cam_isp_ctx_substate_val_to_type(
  2023. ctx_isp->substate_activated));
  2024. return 0;
  2025. }
  2026. static int __cam_isp_ctx_buf_done_in_applied(struct cam_isp_context *ctx_isp,
  2027. void *evt_data)
  2028. {
  2029. int rc = 0;
  2030. struct cam_isp_hw_done_event_data *done =
  2031. (struct cam_isp_hw_done_event_data *) evt_data;
  2032. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  2033. return rc;
  2034. }
  2035. static int __cam_isp_ctx_sof_in_epoch(struct cam_isp_context *ctx_isp,
  2036. void *evt_data)
  2037. {
  2038. int rc = 0;
  2039. struct cam_context *ctx = ctx_isp->base;
  2040. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  2041. struct cam_ctx_request *req;
  2042. if (!evt_data) {
  2043. CAM_ERR(CAM_ISP, "in valid sof event data");
  2044. return -EINVAL;
  2045. }
  2046. if (atomic_read(&ctx_isp->apply_in_progress))
  2047. CAM_INFO(CAM_ISP, "Apply is in progress at the time of SOF");
  2048. ctx_isp->frame_id++;
  2049. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  2050. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  2051. if (list_empty(&ctx->active_req_list))
  2052. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  2053. else
  2054. CAM_DBG(CAM_ISP, "Still need to wait for the buf done");
  2055. req = list_last_entry(&ctx->active_req_list,
  2056. struct cam_ctx_request, list);
  2057. if (req)
  2058. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2059. CAM_ISP_STATE_CHANGE_TRIGGER_SOF,
  2060. req->request_id);
  2061. if (ctx_isp->frame_id == 1)
  2062. CAM_INFO(CAM_ISP,
  2063. "First SOF in EPCR ctx:%d frame_id:%lld next substate %s",
  2064. ctx->ctx_id, ctx_isp->frame_id,
  2065. __cam_isp_ctx_substate_val_to_type(
  2066. ctx_isp->substate_activated));
  2067. CAM_DBG(CAM_ISP, "SOF in epoch ctx:%d frame_id:%lld next substate:%s",
  2068. ctx->ctx_id, ctx_isp->frame_id,
  2069. __cam_isp_ctx_substate_val_to_type(
  2070. ctx_isp->substate_activated));
  2071. return rc;
  2072. }
  2073. static int __cam_isp_ctx_buf_done_in_epoch(struct cam_isp_context *ctx_isp,
  2074. void *evt_data)
  2075. {
  2076. int rc = 0;
  2077. struct cam_isp_hw_done_event_data *done =
  2078. (struct cam_isp_hw_done_event_data *) evt_data;
  2079. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  2080. return rc;
  2081. }
  2082. static int __cam_isp_ctx_buf_done_in_bubble(
  2083. struct cam_isp_context *ctx_isp, void *evt_data)
  2084. {
  2085. int rc = 0;
  2086. struct cam_isp_hw_done_event_data *done =
  2087. (struct cam_isp_hw_done_event_data *) evt_data;
  2088. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 1);
  2089. return rc;
  2090. }
  2091. static int __cam_isp_ctx_epoch_in_bubble_applied(
  2092. struct cam_isp_context *ctx_isp, void *evt_data)
  2093. {
  2094. int rc = 0;
  2095. uint64_t request_id = 0;
  2096. struct cam_req_mgr_trigger_notify notify;
  2097. struct cam_ctx_request *req;
  2098. struct cam_isp_ctx_req *req_isp;
  2099. struct cam_context *ctx = ctx_isp->base;
  2100. struct cam_isp_hw_epoch_event_data *epoch_done_event_data =
  2101. (struct cam_isp_hw_epoch_event_data *)evt_data;
  2102. if (!evt_data) {
  2103. CAM_ERR(CAM_ISP, "invalid event data");
  2104. return -EINVAL;
  2105. }
  2106. ctx_isp->frame_id_meta = epoch_done_event_data->frame_id_meta;
  2107. /*
  2108. * This means we missed the reg upd ack. So we need to
  2109. * transition to BUBBLE state again.
  2110. */
  2111. if (list_empty(&ctx->wait_req_list)) {
  2112. /*
  2113. * If no pending req in epoch, this is an error case.
  2114. * Just go back to the bubble state.
  2115. */
  2116. CAM_ERR(CAM_ISP, "ctx:%d No pending request.", ctx->ctx_id);
  2117. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2118. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2119. __cam_isp_ctx_update_event_record(ctx_isp,
  2120. CAM_ISP_CTX_EVENT_EPOCH, NULL);
  2121. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  2122. goto end;
  2123. }
  2124. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  2125. list);
  2126. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  2127. req_isp->bubble_detected = true;
  2128. CAM_INFO_RATE_LIMIT(CAM_ISP, "Ctx:%d Report Bubble flag %d req id:%lld",
  2129. ctx->ctx_id, req_isp->bubble_report, req->request_id);
  2130. req_isp->reapply = true;
  2131. req_isp->cdm_reset_before_apply = false;
  2132. if (req_isp->bubble_report && ctx->ctx_crm_intf &&
  2133. ctx->ctx_crm_intf->notify_err) {
  2134. struct cam_req_mgr_error_notify notify;
  2135. notify.link_hdl = ctx->link_hdl;
  2136. notify.dev_hdl = ctx->dev_hdl;
  2137. notify.req_id = req->request_id;
  2138. notify.error = CRM_KMD_ERR_BUBBLE;
  2139. notify.trigger = 0;
  2140. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF)
  2141. notify.trigger = CAM_TRIGGER_POINT_SOF;
  2142. notify.frame_id = ctx_isp->frame_id;
  2143. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  2144. notify.sof_boottime_val = ctx_isp->boot_timestamp;
  2145. notify.need_recovery = true;
  2146. CAM_WARN_RATE_LIMIT(CAM_REQ,
  2147. "Notify CRM about Bubble req_id %llu frame %lld, ctx %u",
  2148. req->request_id, ctx_isp->frame_id, ctx->ctx_id);
  2149. rc = ctx->ctx_crm_intf->notify_err(&notify);
  2150. if (rc) {
  2151. req_isp->bubble_detected = false;
  2152. req_isp->reapply = false;
  2153. return 0;
  2154. }
  2155. atomic_set(&ctx_isp->process_bubble, 1);
  2156. } else {
  2157. req_isp->bubble_report = 0;
  2158. CAM_DBG(CAM_ISP, "Skip bubble recovery for req %lld ctx %u",
  2159. req->request_id, ctx->ctx_id);
  2160. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger &&
  2161. ctx_isp->active_req_cnt <= 1) {
  2162. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF) {
  2163. notify.link_hdl = ctx->link_hdl;
  2164. notify.dev_hdl = ctx->dev_hdl;
  2165. notify.frame_id = ctx_isp->frame_id;
  2166. notify.trigger = CAM_TRIGGER_POINT_SOF;
  2167. notify.req_id =
  2168. ctx_isp->req_info.last_bufdone_req_id;
  2169. notify.sof_timestamp_val =
  2170. ctx_isp->sof_timestamp_val;
  2171. notify.trigger_id = ctx_isp->trigger_id;
  2172. ctx->ctx_crm_intf->notify_trigger(&notify);
  2173. CAM_DBG(CAM_ISP,
  2174. "Notify CRM SOF frame %lld ctx %u",
  2175. ctx_isp->frame_id, ctx->ctx_id);
  2176. }
  2177. }
  2178. }
  2179. /*
  2180. * Always move the request to active list. Let buf done
  2181. * function handles the rest.
  2182. */
  2183. list_del_init(&req->list);
  2184. list_add_tail(&req->list, &ctx->active_req_list);
  2185. ctx_isp->active_req_cnt++;
  2186. CAM_DBG(CAM_ISP, "move request %lld to active list(cnt = %d) ctx %u",
  2187. req->request_id, ctx_isp->active_req_cnt);
  2188. if (!req_isp->bubble_report) {
  2189. if (req->request_id > ctx_isp->reported_req_id) {
  2190. request_id = req->request_id;
  2191. ctx_isp->reported_req_id = request_id;
  2192. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2193. CAM_REQ_MGR_SOF_EVENT_ERROR);
  2194. __cam_isp_ctx_update_event_record(ctx_isp,
  2195. CAM_ISP_CTX_EVENT_EPOCH, req);
  2196. } else {
  2197. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2198. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2199. __cam_isp_ctx_update_event_record(ctx_isp,
  2200. CAM_ISP_CTX_EVENT_EPOCH, NULL);
  2201. }
  2202. } else {
  2203. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2204. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2205. __cam_isp_ctx_update_event_record(ctx_isp,
  2206. CAM_ISP_CTX_EVENT_EPOCH, NULL);
  2207. }
  2208. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  2209. CAM_DBG(CAM_ISP, "next Substate[%s]",
  2210. __cam_isp_ctx_substate_val_to_type(
  2211. ctx_isp->substate_activated));
  2212. cam_req_mgr_debug_delay_detect();
  2213. trace_cam_delay_detect("ISP",
  2214. "bubble epoch_in_bubble_applied",
  2215. req->request_id, ctx->ctx_id,
  2216. ctx->link_hdl, ctx->session_hdl,
  2217. CAM_DEFAULT_VALUE);
  2218. end:
  2219. req = list_last_entry(&ctx->active_req_list, struct cam_ctx_request,
  2220. list);
  2221. if (req)
  2222. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2223. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, req->request_id);
  2224. return 0;
  2225. }
  2226. static int __cam_isp_ctx_buf_done_in_bubble_applied(
  2227. struct cam_isp_context *ctx_isp, void *evt_data)
  2228. {
  2229. int rc = 0;
  2230. struct cam_isp_hw_done_event_data *done =
  2231. (struct cam_isp_hw_done_event_data *) evt_data;
  2232. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 1);
  2233. return rc;
  2234. }
  2235. static uint32_t get_evt_param(uint32_t error_type)
  2236. {
  2237. switch (error_type) {
  2238. case CAM_ISP_HW_ERROR_OVERFLOW:
  2239. return CAM_SYNC_ISP_EVENT_OVERFLOW;
  2240. case CAM_ISP_HW_ERROR_P2I_ERROR:
  2241. return CAM_SYNC_ISP_EVENT_P2I_ERROR;
  2242. case CAM_ISP_HW_ERROR_VIOLATION:
  2243. return CAM_SYNC_ISP_EVENT_VIOLATION;
  2244. case CAM_ISP_HW_ERROR_BUSIF_OVERFLOW:
  2245. return CAM_SYNC_ISP_EVENT_BUSIF_OVERFLOW;
  2246. default:
  2247. return CAM_SYNC_ISP_EVENT_UNKNOWN;
  2248. }
  2249. }
  2250. static int __cam_isp_ctx_handle_error(struct cam_isp_context *ctx_isp,
  2251. void *evt_data)
  2252. {
  2253. int rc = 0;
  2254. uint32_t i = 0;
  2255. bool found = 0;
  2256. struct cam_ctx_request *req = NULL;
  2257. struct cam_ctx_request *req_to_report = NULL;
  2258. struct cam_ctx_request *req_to_dump = NULL;
  2259. struct cam_ctx_request *req_temp;
  2260. struct cam_isp_ctx_req *req_isp = NULL;
  2261. struct cam_isp_ctx_req *req_isp_to_report = NULL;
  2262. struct cam_req_mgr_error_notify notify = {};
  2263. uint64_t error_request_id;
  2264. struct cam_hw_fence_map_entry *fence_map_out = NULL;
  2265. struct cam_req_mgr_message req_msg;
  2266. uint32_t evt_param;
  2267. struct cam_req_mgr_timer_notify timer;
  2268. struct cam_context *ctx = ctx_isp->base;
  2269. struct cam_isp_hw_error_event_data *error_event_data =
  2270. (struct cam_isp_hw_error_event_data *)evt_data;
  2271. uint32_t error_type = error_event_data->error_type;
  2272. CAM_DBG(CAM_ISP, "Enter error_type = %d", error_type);
  2273. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_timer) {
  2274. timer.link_hdl = ctx->link_hdl;
  2275. timer.dev_hdl = ctx->dev_hdl;
  2276. timer.state = false;
  2277. ctx->ctx_crm_intf->notify_timer(&timer);
  2278. CAM_DBG(CAM_ISP, "Notify CRM to pause timer for ctx %u",
  2279. ctx->ctx_id);
  2280. }
  2281. if ((error_type == CAM_ISP_HW_ERROR_OVERFLOW) ||
  2282. (error_type == CAM_ISP_HW_ERROR_BUSIF_OVERFLOW) ||
  2283. (error_type == CAM_ISP_HW_ERROR_VIOLATION)) {
  2284. struct cam_hw_cmd_args hw_cmd_args;
  2285. memset(&hw_cmd_args, 0, sizeof(hw_cmd_args));
  2286. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  2287. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_REG_DUMP_ON_ERROR;
  2288. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  2289. &hw_cmd_args);
  2290. if (rc) {
  2291. CAM_ERR(CAM_ISP, "Reg dump on error failed rc: %d", rc);
  2292. rc = 0;
  2293. }
  2294. }
  2295. evt_param = get_evt_param(error_type);
  2296. /*
  2297. * The error is likely caused by first request on the active list.
  2298. * If active list is empty check wait list (maybe error hit as soon
  2299. * as RUP and we handle error before RUP.
  2300. */
  2301. if (list_empty(&ctx->active_req_list)) {
  2302. CAM_DBG(CAM_ISP,
  2303. "handling error with no active request");
  2304. if (list_empty(&ctx->wait_req_list)) {
  2305. CAM_ERR_RATE_LIMIT(CAM_ISP,
  2306. "Error with no active/wait request");
  2307. goto end;
  2308. } else {
  2309. req_to_dump = list_first_entry(&ctx->wait_req_list,
  2310. struct cam_ctx_request, list);
  2311. }
  2312. } else {
  2313. req_to_dump = list_first_entry(&ctx->active_req_list,
  2314. struct cam_ctx_request, list);
  2315. }
  2316. req_isp = (struct cam_isp_ctx_req *) req_to_dump->req_priv;
  2317. if (error_event_data->enable_req_dump)
  2318. rc = cam_isp_ctx_dump_req(req_isp, 0, 0, NULL, false);
  2319. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2320. CAM_ISP_STATE_CHANGE_TRIGGER_ERROR, req_to_dump->request_id);
  2321. list_for_each_entry_safe(req, req_temp,
  2322. &ctx->active_req_list, list) {
  2323. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2324. if (!req_isp->bubble_report) {
  2325. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  2326. fence_map_out =
  2327. &req_isp->fence_map_out[i];
  2328. CAM_ERR(CAM_ISP,
  2329. "req %llu, Sync fd 0x%x ctx %u",
  2330. req->request_id,
  2331. req_isp->fence_map_out[i].sync_id,
  2332. ctx->ctx_id);
  2333. if (req_isp->fence_map_out[i].sync_id != -1) {
  2334. rc = cam_sync_signal(
  2335. fence_map_out->sync_id,
  2336. CAM_SYNC_STATE_SIGNALED_ERROR,
  2337. evt_param);
  2338. fence_map_out->sync_id = -1;
  2339. }
  2340. }
  2341. list_del_init(&req->list);
  2342. list_add_tail(&req->list, &ctx->free_req_list);
  2343. ctx_isp->active_req_cnt--;
  2344. } else {
  2345. found = 1;
  2346. break;
  2347. }
  2348. }
  2349. if (found)
  2350. goto move_to_pending;
  2351. list_for_each_entry_safe(req, req_temp,
  2352. &ctx->wait_req_list, list) {
  2353. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2354. if (!req_isp->bubble_report) {
  2355. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  2356. fence_map_out =
  2357. &req_isp->fence_map_out[i];
  2358. CAM_ERR(CAM_ISP,
  2359. "req %llu, Sync fd 0x%x ctx %u",
  2360. req->request_id,
  2361. req_isp->fence_map_out[i].sync_id,
  2362. ctx->ctx_id);
  2363. if (req_isp->fence_map_out[i].sync_id != -1) {
  2364. rc = cam_sync_signal(
  2365. fence_map_out->sync_id,
  2366. CAM_SYNC_STATE_SIGNALED_ERROR,
  2367. evt_param);
  2368. fence_map_out->sync_id = -1;
  2369. }
  2370. }
  2371. list_del_init(&req->list);
  2372. list_add_tail(&req->list, &ctx->free_req_list);
  2373. ctx_isp->active_req_cnt--;
  2374. } else {
  2375. found = 1;
  2376. break;
  2377. }
  2378. }
  2379. move_to_pending:
  2380. /*
  2381. * If bubble recovery is enabled on any request we need to move that
  2382. * request and all the subsequent requests to the pending list.
  2383. * Note:
  2384. * We need to traverse the active list in reverse order and add
  2385. * to head of pending list.
  2386. * e.g. pending current state: 10, 11 | active current state: 8, 9
  2387. * intermittent for loop iteration- pending: 9, 10, 11 | active: 8
  2388. * final state - pending: 8, 9, 10, 11 | active: NULL
  2389. */
  2390. if (found) {
  2391. list_for_each_entry_safe_reverse(req, req_temp,
  2392. &ctx->active_req_list, list) {
  2393. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2394. list_del_init(&req->list);
  2395. list_add(&req->list, &ctx->pending_req_list);
  2396. ctx_isp->active_req_cnt--;
  2397. }
  2398. list_for_each_entry_safe_reverse(req, req_temp,
  2399. &ctx->wait_req_list, list) {
  2400. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2401. list_del_init(&req->list);
  2402. list_add(&req->list, &ctx->pending_req_list);
  2403. ctx_isp->active_req_cnt--;
  2404. }
  2405. }
  2406. end:
  2407. do {
  2408. if (list_empty(&ctx->pending_req_list)) {
  2409. error_request_id = ctx_isp->last_applied_req_id;
  2410. req_isp = NULL;
  2411. break;
  2412. }
  2413. req = list_first_entry(&ctx->pending_req_list,
  2414. struct cam_ctx_request, list);
  2415. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2416. error_request_id = ctx_isp->last_applied_req_id;
  2417. if (req_isp->bubble_report) {
  2418. req_to_report = req;
  2419. req_isp_to_report = req_to_report->req_priv;
  2420. break;
  2421. }
  2422. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  2423. if (req_isp->fence_map_out[i].sync_id != -1)
  2424. rc = cam_sync_signal(
  2425. req_isp->fence_map_out[i].sync_id,
  2426. CAM_SYNC_STATE_SIGNALED_ERROR,
  2427. evt_param);
  2428. req_isp->fence_map_out[i].sync_id = -1;
  2429. }
  2430. list_del_init(&req->list);
  2431. list_add_tail(&req->list, &ctx->free_req_list);
  2432. } while (req->request_id < ctx_isp->last_applied_req_id);
  2433. if (ctx_isp->offline_context)
  2434. goto exit;
  2435. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_err) {
  2436. notify.link_hdl = ctx->link_hdl;
  2437. notify.dev_hdl = ctx->dev_hdl;
  2438. notify.req_id = error_request_id;
  2439. notify.error = CRM_KMD_ERR_FATAL;
  2440. if (req_isp_to_report && req_isp_to_report->bubble_report)
  2441. if (error_event_data->recovery_enabled)
  2442. notify.error = CRM_KMD_ERR_BUBBLE;
  2443. CAM_WARN(CAM_ISP,
  2444. "Notify CRM: req %lld, frame %lld ctx %u, error %d",
  2445. error_request_id, ctx_isp->frame_id, ctx->ctx_id,
  2446. notify.error);
  2447. ctx->ctx_crm_intf->notify_err(&notify);
  2448. /*
  2449. * Need to send error occurred in KMD
  2450. * This will help UMD to take necessary action
  2451. * and to dump relevant info
  2452. */
  2453. if (notify.error == CRM_KMD_ERR_FATAL) {
  2454. req_msg.session_hdl = ctx_isp->base->session_hdl;
  2455. req_msg.u.err_msg.device_hdl = ctx_isp->base->dev_hdl;
  2456. if (error_type == CAM_ISP_HW_ERROR_CSID_FATAL)
  2457. req_msg.u.err_msg.error_type =
  2458. CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  2459. else {
  2460. req_msg.u.err_msg.error_type =
  2461. CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  2462. }
  2463. req_msg.u.err_msg.link_hdl = ctx_isp->base->link_hdl;
  2464. req_msg.u.err_msg.request_id = error_request_id;
  2465. req_msg.u.err_msg.resource_size = 0x0;
  2466. req_msg.u.err_msg.error_code =
  2467. error_event_data->error_code;
  2468. if (cam_req_mgr_notify_message(&req_msg,
  2469. V4L_EVENT_CAM_REQ_MGR_ERROR,
  2470. V4L_EVENT_CAM_REQ_MGR_EVENT))
  2471. CAM_ERR(CAM_ISP,
  2472. "Error in notifying the error time for req id:%lld ctx %u",
  2473. ctx_isp->last_applied_req_id,
  2474. ctx->ctx_id);
  2475. }
  2476. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HW_ERROR;
  2477. } else {
  2478. CAM_ERR_RATE_LIMIT(CAM_ISP,
  2479. "Can not notify ERRROR to CRM for ctx %u",
  2480. ctx->ctx_id);
  2481. rc = -EFAULT;
  2482. }
  2483. CAM_DBG(CAM_ISP, "Exit");
  2484. exit:
  2485. return rc;
  2486. }
  2487. static int __cam_isp_ctx_fs2_sof_in_sof_state(
  2488. struct cam_isp_context *ctx_isp, void *evt_data)
  2489. {
  2490. int rc = 0;
  2491. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  2492. struct cam_ctx_request *req;
  2493. struct cam_context *ctx = ctx_isp->base;
  2494. struct cam_req_mgr_trigger_notify notify;
  2495. uint64_t request_id = 0;
  2496. if (!evt_data) {
  2497. CAM_ERR(CAM_ISP, "in valid sof event data");
  2498. return -EINVAL;
  2499. }
  2500. ctx_isp->frame_id++;
  2501. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  2502. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  2503. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  2504. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  2505. if (!(list_empty(&ctx->wait_req_list)))
  2506. goto end;
  2507. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger &&
  2508. ctx_isp->active_req_cnt <= 2) {
  2509. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF) {
  2510. notify.link_hdl = ctx->link_hdl;
  2511. notify.dev_hdl = ctx->dev_hdl;
  2512. notify.frame_id = ctx_isp->frame_id;
  2513. notify.trigger = CAM_TRIGGER_POINT_SOF;
  2514. notify.req_id = ctx_isp->req_info.last_bufdone_req_id;
  2515. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  2516. ctx->ctx_crm_intf->notify_trigger(&notify);
  2517. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  2518. ctx_isp->frame_id);
  2519. }
  2520. list_for_each_entry(req, &ctx->active_req_list, list) {
  2521. if (req->request_id > ctx_isp->reported_req_id) {
  2522. request_id = req->request_id;
  2523. ctx_isp->reported_req_id = request_id;
  2524. break;
  2525. }
  2526. }
  2527. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2528. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2529. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2530. CAM_ISP_STATE_CHANGE_TRIGGER_SOF, request_id);
  2531. } else {
  2532. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not notify SOF to CRM");
  2533. rc = -EFAULT;
  2534. }
  2535. end:
  2536. return rc;
  2537. }
  2538. static int __cam_isp_ctx_fs2_buf_done(struct cam_isp_context *ctx_isp,
  2539. void *evt_data)
  2540. {
  2541. int rc = 0;
  2542. struct cam_isp_hw_done_event_data *done =
  2543. (struct cam_isp_hw_done_event_data *) evt_data;
  2544. struct cam_context *ctx = ctx_isp->base;
  2545. int prev_active_req_cnt = 0;
  2546. int curr_req_id = 0;
  2547. struct cam_ctx_request *req;
  2548. prev_active_req_cnt = ctx_isp->active_req_cnt;
  2549. req = list_first_entry(&ctx->active_req_list,
  2550. struct cam_ctx_request, list);
  2551. if (req)
  2552. curr_req_id = req->request_id;
  2553. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  2554. if (prev_active_req_cnt == ctx_isp->active_req_cnt + 1) {
  2555. if (list_empty(&ctx->wait_req_list) &&
  2556. list_empty(&ctx->active_req_list)) {
  2557. CAM_DBG(CAM_ISP, "No request, move to SOF");
  2558. ctx_isp->substate_activated =
  2559. CAM_ISP_CTX_ACTIVATED_SOF;
  2560. if (ctx_isp->reported_req_id < curr_req_id) {
  2561. ctx_isp->reported_req_id = curr_req_id;
  2562. __cam_isp_ctx_send_sof_timestamp(ctx_isp,
  2563. curr_req_id,
  2564. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2565. }
  2566. }
  2567. }
  2568. return rc;
  2569. }
  2570. static int __cam_isp_ctx_fs2_buf_done_in_epoch(struct cam_isp_context *ctx_isp,
  2571. void *evt_data)
  2572. {
  2573. int rc = 0;
  2574. rc = __cam_isp_ctx_fs2_buf_done(ctx_isp, evt_data);
  2575. return rc;
  2576. }
  2577. static int __cam_isp_ctx_fs2_buf_done_in_applied(
  2578. struct cam_isp_context *ctx_isp,
  2579. void *evt_data)
  2580. {
  2581. int rc = 0;
  2582. rc = __cam_isp_ctx_fs2_buf_done(ctx_isp, evt_data);
  2583. return rc;
  2584. }
  2585. static int __cam_isp_ctx_fs2_reg_upd_in_sof(struct cam_isp_context *ctx_isp,
  2586. void *evt_data)
  2587. {
  2588. int rc = 0;
  2589. struct cam_ctx_request *req = NULL;
  2590. struct cam_isp_ctx_req *req_isp;
  2591. struct cam_context *ctx = ctx_isp->base;
  2592. if (ctx->state != CAM_CTX_ACTIVATED && ctx_isp->frame_id > 1) {
  2593. CAM_DBG(CAM_ISP, "invalid RUP");
  2594. goto end;
  2595. }
  2596. /*
  2597. * This is for the first update. The initial setting will
  2598. * cause the reg_upd in the first frame.
  2599. */
  2600. if (!list_empty(&ctx->wait_req_list)) {
  2601. req = list_first_entry(&ctx->wait_req_list,
  2602. struct cam_ctx_request, list);
  2603. list_del_init(&req->list);
  2604. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2605. if (req_isp->num_fence_map_out == req_isp->num_acked)
  2606. list_add_tail(&req->list, &ctx->free_req_list);
  2607. else
  2608. CAM_ERR(CAM_ISP,
  2609. "receive rup in unexpected state");
  2610. }
  2611. if (req != NULL) {
  2612. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2613. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  2614. req->request_id);
  2615. }
  2616. end:
  2617. return rc;
  2618. }
  2619. static int __cam_isp_ctx_fs2_reg_upd_in_applied_state(
  2620. struct cam_isp_context *ctx_isp, void *evt_data)
  2621. {
  2622. int rc = 0;
  2623. struct cam_ctx_request *req = NULL;
  2624. struct cam_context *ctx = ctx_isp->base;
  2625. struct cam_isp_ctx_req *req_isp;
  2626. struct cam_req_mgr_trigger_notify notify;
  2627. uint64_t request_id = 0;
  2628. if (list_empty(&ctx->wait_req_list)) {
  2629. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request");
  2630. goto end;
  2631. }
  2632. req = list_first_entry(&ctx->wait_req_list,
  2633. struct cam_ctx_request, list);
  2634. list_del_init(&req->list);
  2635. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2636. if (req_isp->num_fence_map_out != 0) {
  2637. list_add_tail(&req->list, &ctx->active_req_list);
  2638. ctx_isp->active_req_cnt++;
  2639. CAM_DBG(CAM_REQ, "move request %lld to active list(cnt = %d)",
  2640. req->request_id, ctx_isp->active_req_cnt);
  2641. } else {
  2642. /* no io config, so the request is completed. */
  2643. list_add_tail(&req->list, &ctx->free_req_list);
  2644. }
  2645. /*
  2646. * This function only called directly from applied and bubble applied
  2647. * state so change substate here.
  2648. */
  2649. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  2650. if (req_isp->num_fence_map_out != 1)
  2651. goto end;
  2652. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger &&
  2653. ctx_isp->active_req_cnt <= 2) {
  2654. list_for_each_entry(req, &ctx->active_req_list, list) {
  2655. if (req->request_id > ctx_isp->reported_req_id) {
  2656. request_id = req->request_id;
  2657. ctx_isp->reported_req_id = request_id;
  2658. break;
  2659. }
  2660. }
  2661. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2662. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2663. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF) {
  2664. notify.link_hdl = ctx->link_hdl;
  2665. notify.dev_hdl = ctx->dev_hdl;
  2666. notify.frame_id = ctx_isp->frame_id;
  2667. notify.trigger = CAM_TRIGGER_POINT_SOF;
  2668. notify.req_id = ctx_isp->req_info.last_bufdone_req_id;
  2669. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  2670. ctx->ctx_crm_intf->notify_trigger(&notify);
  2671. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  2672. ctx_isp->frame_id);
  2673. }
  2674. } else {
  2675. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not notify SOF to CRM");
  2676. rc = -EFAULT;
  2677. }
  2678. CAM_DBG(CAM_ISP, "next Substate[%s]",
  2679. __cam_isp_ctx_substate_val_to_type(
  2680. ctx_isp->substate_activated));
  2681. end:
  2682. if (req != NULL && !rc) {
  2683. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2684. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  2685. req->request_id);
  2686. }
  2687. return rc;
  2688. }
  2689. static struct cam_isp_ctx_irq_ops
  2690. cam_isp_ctx_activated_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  2691. /* SOF */
  2692. {
  2693. .irq_ops = {
  2694. __cam_isp_ctx_handle_error,
  2695. __cam_isp_ctx_sof_in_activated_state,
  2696. __cam_isp_ctx_reg_upd_in_sof,
  2697. __cam_isp_ctx_notify_sof_in_activated_state,
  2698. __cam_isp_ctx_notify_eof_in_activated_state,
  2699. NULL,
  2700. },
  2701. },
  2702. /* APPLIED */
  2703. {
  2704. .irq_ops = {
  2705. __cam_isp_ctx_handle_error,
  2706. __cam_isp_ctx_sof_in_activated_state,
  2707. __cam_isp_ctx_reg_upd_in_applied_state,
  2708. __cam_isp_ctx_epoch_in_applied,
  2709. __cam_isp_ctx_notify_eof_in_activated_state,
  2710. __cam_isp_ctx_buf_done_in_applied,
  2711. },
  2712. },
  2713. /* EPOCH */
  2714. {
  2715. .irq_ops = {
  2716. __cam_isp_ctx_handle_error,
  2717. __cam_isp_ctx_sof_in_epoch,
  2718. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  2719. __cam_isp_ctx_notify_sof_in_activated_state,
  2720. __cam_isp_ctx_notify_eof_in_activated_state,
  2721. __cam_isp_ctx_buf_done_in_epoch,
  2722. },
  2723. },
  2724. /* BUBBLE */
  2725. {
  2726. .irq_ops = {
  2727. __cam_isp_ctx_handle_error,
  2728. __cam_isp_ctx_sof_in_activated_state,
  2729. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  2730. __cam_isp_ctx_notify_sof_in_activated_state,
  2731. __cam_isp_ctx_notify_eof_in_activated_state,
  2732. __cam_isp_ctx_buf_done_in_bubble,
  2733. },
  2734. },
  2735. /* Bubble Applied */
  2736. {
  2737. .irq_ops = {
  2738. __cam_isp_ctx_handle_error,
  2739. __cam_isp_ctx_sof_in_activated_state,
  2740. __cam_isp_ctx_reg_upd_in_applied_state,
  2741. __cam_isp_ctx_epoch_in_bubble_applied,
  2742. NULL,
  2743. __cam_isp_ctx_buf_done_in_bubble_applied,
  2744. },
  2745. },
  2746. /* HW ERROR */
  2747. {
  2748. .irq_ops = {
  2749. NULL,
  2750. __cam_isp_ctx_sof_in_activated_state,
  2751. __cam_isp_ctx_reg_upd_in_hw_error,
  2752. NULL,
  2753. NULL,
  2754. NULL,
  2755. },
  2756. },
  2757. /* HALT */
  2758. {
  2759. },
  2760. };
  2761. static struct cam_isp_ctx_irq_ops
  2762. cam_isp_ctx_fs2_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  2763. /* SOF */
  2764. {
  2765. .irq_ops = {
  2766. __cam_isp_ctx_handle_error,
  2767. __cam_isp_ctx_fs2_sof_in_sof_state,
  2768. __cam_isp_ctx_fs2_reg_upd_in_sof,
  2769. __cam_isp_ctx_fs2_sof_in_sof_state,
  2770. __cam_isp_ctx_notify_eof_in_activated_state,
  2771. NULL,
  2772. },
  2773. },
  2774. /* APPLIED */
  2775. {
  2776. .irq_ops = {
  2777. __cam_isp_ctx_handle_error,
  2778. __cam_isp_ctx_sof_in_activated_state,
  2779. __cam_isp_ctx_fs2_reg_upd_in_applied_state,
  2780. __cam_isp_ctx_epoch_in_applied,
  2781. __cam_isp_ctx_notify_eof_in_activated_state,
  2782. __cam_isp_ctx_fs2_buf_done_in_applied,
  2783. },
  2784. },
  2785. /* EPOCH */
  2786. {
  2787. .irq_ops = {
  2788. __cam_isp_ctx_handle_error,
  2789. __cam_isp_ctx_sof_in_epoch,
  2790. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  2791. __cam_isp_ctx_notify_sof_in_activated_state,
  2792. __cam_isp_ctx_notify_eof_in_activated_state,
  2793. __cam_isp_ctx_fs2_buf_done_in_epoch,
  2794. },
  2795. },
  2796. /* BUBBLE */
  2797. {
  2798. .irq_ops = {
  2799. __cam_isp_ctx_handle_error,
  2800. __cam_isp_ctx_sof_in_activated_state,
  2801. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  2802. __cam_isp_ctx_notify_sof_in_activated_state,
  2803. __cam_isp_ctx_notify_eof_in_activated_state,
  2804. __cam_isp_ctx_buf_done_in_bubble,
  2805. },
  2806. },
  2807. /* Bubble Applied */
  2808. {
  2809. .irq_ops = {
  2810. __cam_isp_ctx_handle_error,
  2811. __cam_isp_ctx_sof_in_activated_state,
  2812. __cam_isp_ctx_reg_upd_in_applied_state,
  2813. __cam_isp_ctx_epoch_in_bubble_applied,
  2814. NULL,
  2815. __cam_isp_ctx_buf_done_in_bubble_applied,
  2816. },
  2817. },
  2818. /* HW ERROR */
  2819. {
  2820. .irq_ops = {
  2821. NULL,
  2822. __cam_isp_ctx_sof_in_activated_state,
  2823. __cam_isp_ctx_reg_upd_in_hw_error,
  2824. NULL,
  2825. NULL,
  2826. NULL,
  2827. },
  2828. },
  2829. /* HALT */
  2830. {
  2831. },
  2832. };
  2833. static struct cam_isp_ctx_irq_ops
  2834. cam_isp_ctx_offline_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  2835. /* SOF */
  2836. {
  2837. .irq_ops = {
  2838. __cam_isp_ctx_handle_error,
  2839. NULL,
  2840. NULL,
  2841. NULL,
  2842. NULL,
  2843. NULL,
  2844. },
  2845. },
  2846. /* APPLIED */
  2847. {
  2848. .irq_ops = {
  2849. __cam_isp_ctx_handle_error,
  2850. __cam_isp_ctx_sof_in_activated_state,
  2851. __cam_isp_ctx_reg_upd_in_applied_state,
  2852. NULL,
  2853. NULL,
  2854. __cam_isp_ctx_buf_done_in_applied,
  2855. },
  2856. },
  2857. /* EPOCH */
  2858. {
  2859. .irq_ops = {
  2860. __cam_isp_ctx_handle_error,
  2861. NULL,
  2862. NULL,
  2863. __cam_isp_ctx_offline_epoch_in_activated_state,
  2864. NULL,
  2865. __cam_isp_ctx_buf_done_in_epoch,
  2866. },
  2867. },
  2868. /* BUBBLE */
  2869. {
  2870. },
  2871. /* Bubble Applied */
  2872. {
  2873. },
  2874. /* HW ERROR */
  2875. {
  2876. .irq_ops = {
  2877. NULL,
  2878. __cam_isp_ctx_sof_in_activated_state,
  2879. __cam_isp_ctx_reg_upd_in_hw_error,
  2880. NULL,
  2881. NULL,
  2882. NULL,
  2883. },
  2884. },
  2885. /* HALT */
  2886. {
  2887. },
  2888. };
  2889. static int __cam_isp_ctx_apply_req_in_activated_state(
  2890. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply,
  2891. enum cam_isp_ctx_activated_substate next_state)
  2892. {
  2893. int rc = 0;
  2894. struct cam_ctx_request *req;
  2895. struct cam_ctx_request *active_req = NULL;
  2896. struct cam_isp_ctx_req *req_isp;
  2897. struct cam_isp_ctx_req *active_req_isp;
  2898. struct cam_isp_context *ctx_isp = NULL;
  2899. struct cam_hw_config_args cfg = {0};
  2900. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  2901. if (apply->re_apply)
  2902. if (apply->request_id <= ctx_isp->last_applied_req_id) {
  2903. CAM_INFO_RATE_LIMIT(CAM_ISP,
  2904. "ctx_id:%d Trying to reapply the same request %llu again",
  2905. ctx->ctx_id,
  2906. apply->request_id);
  2907. return 0;
  2908. }
  2909. if (list_empty(&ctx->pending_req_list)) {
  2910. CAM_ERR_RATE_LIMIT(CAM_ISP,
  2911. "ctx_id:%d No available request for Apply id %lld",
  2912. ctx->ctx_id,
  2913. apply->request_id);
  2914. rc = -EFAULT;
  2915. goto end;
  2916. }
  2917. /*
  2918. * When the pipeline has issue, the requests can be queued up in the
  2919. * pipeline. In this case, we should reject the additional request.
  2920. * The maximum number of request allowed to be outstanding is 2.
  2921. *
  2922. */
  2923. if (atomic_read(&ctx_isp->process_bubble)) {
  2924. CAM_INFO_RATE_LIMIT(CAM_ISP,
  2925. "ctx_id:%d Processing bubble cannot apply Request Id %llu",
  2926. ctx->ctx_id,
  2927. apply->request_id);
  2928. rc = -EAGAIN;
  2929. goto end;
  2930. }
  2931. spin_lock_bh(&ctx->lock);
  2932. req = list_first_entry(&ctx->pending_req_list, struct cam_ctx_request,
  2933. list);
  2934. spin_unlock_bh(&ctx->lock);
  2935. /*
  2936. * Check whether the request id is matching the tip, if not, this means
  2937. * we are in the middle of the error handling. Need to reject this apply
  2938. */
  2939. if (req->request_id != apply->request_id) {
  2940. CAM_ERR_RATE_LIMIT(CAM_ISP,
  2941. "ctx_id:%d Invalid Request Id asking %llu existing %llu",
  2942. ctx->ctx_id,
  2943. apply->request_id, req->request_id);
  2944. rc = -EFAULT;
  2945. goto end;
  2946. }
  2947. CAM_DBG(CAM_REQ, "Apply request %lld in Substate[%s] ctx %u link %x",
  2948. req->request_id,
  2949. __cam_isp_ctx_substate_val_to_type(ctx_isp->substate_activated),
  2950. ctx->ctx_id, ctx->link_hdl);
  2951. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2952. if (ctx_isp->active_req_cnt >= 2) {
  2953. CAM_WARN_RATE_LIMIT(CAM_ISP,
  2954. "Reject apply request (id %lld) due to congestion(cnt = %d) ctx %u",
  2955. req->request_id,
  2956. ctx_isp->active_req_cnt,
  2957. ctx->ctx_id);
  2958. spin_lock_bh(&ctx->lock);
  2959. if (!list_empty(&ctx->active_req_list))
  2960. active_req = list_first_entry(&ctx->active_req_list,
  2961. struct cam_ctx_request, list);
  2962. else
  2963. CAM_ERR_RATE_LIMIT(CAM_ISP,
  2964. "WARNING: should not happen (cnt = %d) but active_list empty",
  2965. ctx_isp->active_req_cnt);
  2966. spin_unlock_bh(&ctx->lock);
  2967. if (active_req) {
  2968. active_req_isp =
  2969. (struct cam_isp_ctx_req *) active_req->req_priv;
  2970. __cam_isp_ctx_handle_buf_done_fail_log(
  2971. active_req->request_id, active_req_isp,
  2972. ctx_isp->isp_device_type);
  2973. }
  2974. rc = -EFAULT;
  2975. goto end;
  2976. }
  2977. req_isp->bubble_report = apply->report_if_bubble;
  2978. req_isp->is_sync_mode = apply->is_sync_mode;
  2979. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2980. cfg.request_id = req->request_id;
  2981. cfg.hw_update_entries = req_isp->cfg;
  2982. cfg.num_hw_update_entries = req_isp->num_cfg;
  2983. cfg.priv = &req_isp->hw_update_data;
  2984. cfg.init_packet = 0;
  2985. cfg.reapply = req_isp->reapply;
  2986. cfg.cdm_reset_before_apply = req_isp->cdm_reset_before_apply;
  2987. atomic_set(&ctx_isp->apply_in_progress, 1);
  2988. rc = ctx->hw_mgr_intf->hw_config(ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  2989. if (!rc) {
  2990. spin_lock_bh(&ctx->lock);
  2991. ctx_isp->substate_activated = next_state;
  2992. ctx_isp->last_applied_req_id = apply->request_id;
  2993. list_del_init(&req->list);
  2994. list_add_tail(&req->list, &ctx->wait_req_list);
  2995. CAM_DBG(CAM_ISP, "new substate Substate[%s], applied req %lld",
  2996. __cam_isp_ctx_substate_val_to_type(next_state),
  2997. ctx_isp->last_applied_req_id);
  2998. spin_unlock_bh(&ctx->lock);
  2999. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3000. CAM_ISP_STATE_CHANGE_TRIGGER_APPLIED,
  3001. req->request_id);
  3002. __cam_isp_ctx_update_event_record(ctx_isp,
  3003. CAM_ISP_CTX_EVENT_APPLY, req);
  3004. } else if (rc == -EALREADY) {
  3005. spin_lock_bh(&ctx->lock);
  3006. req_isp->bubble_detected = true;
  3007. req_isp->cdm_reset_before_apply = false;
  3008. atomic_set(&ctx_isp->process_bubble, 1);
  3009. list_del_init(&req->list);
  3010. list_add(&req->list, &ctx->active_req_list);
  3011. ctx_isp->active_req_cnt++;
  3012. spin_unlock_bh(&ctx->lock);
  3013. CAM_DBG(CAM_REQ,
  3014. "move request %lld to active list(cnt = %d), ctx %u",
  3015. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  3016. } else {
  3017. CAM_ERR_RATE_LIMIT(CAM_ISP,
  3018. "ctx_id:%d ,Can not apply (req %lld) the configuration, rc %d",
  3019. ctx->ctx_id, apply->request_id, rc);
  3020. }
  3021. atomic_set(&ctx_isp->apply_in_progress, 0);
  3022. end:
  3023. return rc;
  3024. }
  3025. static int __cam_isp_ctx_signal_buf_done(
  3026. struct cam_context *ctx,
  3027. struct cam_req_mgr_signal_info *signal_buf_done)
  3028. {
  3029. struct cam_ctx_request *req = NULL;
  3030. struct cam_isp_ctx_req *req_isp = NULL;
  3031. if (!list_empty(&ctx->wait_req_list)) {
  3032. req = list_first_entry(&ctx->wait_req_list,
  3033. struct cam_ctx_request,
  3034. list);
  3035. if (req->request_id == signal_buf_done->req_id) {
  3036. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  3037. atomic_set(&req_isp->buf_done_ready, signal_buf_done->state);
  3038. goto end;
  3039. }
  3040. }
  3041. if (!list_empty(&ctx->active_req_list)) {
  3042. req = list_first_entry(&ctx->active_req_list,
  3043. struct cam_ctx_request,
  3044. list);
  3045. if (req->request_id == signal_buf_done->req_id) {
  3046. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  3047. atomic_set(&req_isp->buf_done_ready, signal_buf_done->state);
  3048. goto end;
  3049. }
  3050. }
  3051. CAM_WARN(CAM_ISP, "Request %lld not found", signal_buf_done->req_id);
  3052. end:
  3053. return 0;
  3054. }
  3055. static int __cam_isp_ctx_change_substate(
  3056. struct cam_context *ctx,
  3057. struct cam_req_mgr_request_change_state *state_info)
  3058. {
  3059. int rc = 0;
  3060. uint64_t request_id = 0;
  3061. struct cam_ctx_request *req = NULL;
  3062. struct cam_ctx_request *req_temp = NULL;
  3063. struct cam_ctx_request *bubble_req = NULL;
  3064. struct cam_isp_ctx_req *req_isp = NULL;
  3065. struct cam_isp_context *ctx_isp =
  3066. (struct cam_isp_context *) ctx->ctx_priv;
  3067. if (!list_empty(&ctx->wait_req_list)) {
  3068. req = list_first_entry(&ctx->wait_req_list,
  3069. struct cam_ctx_request,
  3070. list);
  3071. if (req->request_id == state_info->req_id) {
  3072. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  3073. req_isp->bubble_detected = true;
  3074. req_isp->reapply = true;
  3075. bubble_req = req;
  3076. list_del_init(&req->list);
  3077. list_add_tail(&req->list, &ctx->active_req_list);
  3078. goto end;
  3079. }
  3080. } else {
  3081. CAM_ERR(CAM_ISP, "Ctx:%d No wait request", ctx->ctx_id);
  3082. }
  3083. if (!bubble_req) {
  3084. list_for_each_entry_safe(req, req_temp,
  3085. &ctx->active_req_list, list) {
  3086. if (req->request_id == state_info->req_id) {
  3087. req_isp =
  3088. (struct cam_isp_ctx_req *)req->req_priv;
  3089. req_isp->bubble_detected = true;
  3090. req_isp->reapply = true;
  3091. bubble_req = req;
  3092. break;
  3093. }
  3094. }
  3095. }
  3096. if (!bubble_req) {
  3097. CAM_ERR(CAM_ISP, "Req %lld not in active list ctx : %d",
  3098. state_info->req_id,
  3099. ctx->ctx_id);
  3100. goto done;
  3101. }
  3102. end:
  3103. if (req_isp->bubble_report)
  3104. atomic_set(&ctx_isp->process_bubble, 1);
  3105. if ((req->request_id > ctx_isp->reported_req_id)
  3106. && !req_isp->bubble_report) {
  3107. request_id = req->request_id;
  3108. ctx_isp->reported_req_id = request_id;
  3109. }
  3110. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3111. CAM_REQ_MGR_SOF_EVENT_ERROR);
  3112. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  3113. CAM_DBG(CAM_ISP, "next Substate[%s] ctx : %d",
  3114. __cam_isp_ctx_substate_val_to_type(
  3115. ctx_isp->substate_activated), ctx->ctx_id);
  3116. done:
  3117. return rc;
  3118. }
  3119. static int __cam_isp_ctx_apply_req_in_sof(
  3120. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  3121. {
  3122. int rc = 0;
  3123. struct cam_isp_context *ctx_isp =
  3124. (struct cam_isp_context *) ctx->ctx_priv;
  3125. CAM_DBG(CAM_ISP, "current Substate[%s]",
  3126. __cam_isp_ctx_substate_val_to_type(
  3127. ctx_isp->substate_activated));
  3128. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  3129. CAM_ISP_CTX_ACTIVATED_APPLIED);
  3130. CAM_DBG(CAM_ISP, "new Substate[%s]",
  3131. __cam_isp_ctx_substate_val_to_type(
  3132. ctx_isp->substate_activated));
  3133. if (rc)
  3134. CAM_DBG(CAM_ISP, "Apply failed in Substate[%s], rc %d",
  3135. __cam_isp_ctx_substate_val_to_type(
  3136. ctx_isp->substate_activated), rc);
  3137. return rc;
  3138. }
  3139. static int __cam_isp_ctx_apply_req_in_epoch(
  3140. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  3141. {
  3142. int rc = 0;
  3143. struct cam_isp_context *ctx_isp =
  3144. (struct cam_isp_context *) ctx->ctx_priv;
  3145. CAM_DBG(CAM_ISP, "current Substate[%s]",
  3146. __cam_isp_ctx_substate_val_to_type(
  3147. ctx_isp->substate_activated));
  3148. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  3149. CAM_ISP_CTX_ACTIVATED_APPLIED);
  3150. CAM_DBG(CAM_ISP, "new Substate[%s]",
  3151. __cam_isp_ctx_substate_val_to_type(
  3152. ctx_isp->substate_activated));
  3153. if (rc)
  3154. CAM_DBG(CAM_ISP, "Apply failed in Substate[%s], rc %d",
  3155. __cam_isp_ctx_substate_val_to_type(
  3156. ctx_isp->substate_activated), rc);
  3157. return rc;
  3158. }
  3159. static int __cam_isp_ctx_apply_req_in_bubble(
  3160. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  3161. {
  3162. int rc = 0;
  3163. struct cam_isp_context *ctx_isp =
  3164. (struct cam_isp_context *) ctx->ctx_priv;
  3165. CAM_DBG(CAM_ISP, "current Substate[%s]",
  3166. __cam_isp_ctx_substate_val_to_type(
  3167. ctx_isp->substate_activated));
  3168. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  3169. CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED);
  3170. CAM_DBG(CAM_ISP, "ctx %d link %x new Substate[%s]",
  3171. ctx->ctx_id, ctx->link_hdl, __cam_isp_ctx_substate_val_to_type(
  3172. ctx_isp->substate_activated));
  3173. if (rc)
  3174. CAM_DBG(CAM_ISP, "Apply failed in Substate[%s], rc %d",
  3175. __cam_isp_ctx_substate_val_to_type(
  3176. ctx_isp->substate_activated), rc);
  3177. return rc;
  3178. }
  3179. static int __cam_isp_ctx_apply_default_req_settings(
  3180. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  3181. {
  3182. int rc = 0;
  3183. struct cam_isp_context *isp_ctx =
  3184. (struct cam_isp_context *) ctx->ctx_priv;
  3185. struct cam_hw_cmd_args hw_cmd_args;
  3186. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  3187. hw_cmd_args.ctxt_to_hw_map = isp_ctx->hw_ctx;
  3188. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  3189. isp_hw_cmd_args.cmd_type =
  3190. CAM_ISP_HW_MGR_CMD_PROG_DEFAULT_CFG;
  3191. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  3192. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  3193. &hw_cmd_args);
  3194. if (rc)
  3195. CAM_ERR(CAM_ISP,
  3196. "Failed to apply default settings rc %d", rc);
  3197. else
  3198. CAM_DBG(CAM_ISP, "Applied default settings rc %d", rc);
  3199. return rc;
  3200. }
  3201. static int __cam_isp_ctx_dump_req_info(
  3202. struct cam_context *ctx,
  3203. struct cam_ctx_request *req,
  3204. uintptr_t cpu_addr,
  3205. size_t buf_len,
  3206. size_t *offset)
  3207. {
  3208. int i, rc;
  3209. uint8_t *dst;
  3210. int32_t *addr, *start;
  3211. uint32_t min_len;
  3212. size_t remain_len;
  3213. struct cam_isp_ctx_req *req_isp;
  3214. struct cam_isp_context *ctx_isp;
  3215. struct cam_isp_context_dump_header *hdr;
  3216. if (!req || !ctx || !offset || !cpu_addr || !buf_len) {
  3217. CAM_ERR(CAM_ISP, "Invalid parameters %pK %pK %pK %zu",
  3218. req, ctx, offset, buf_len);
  3219. return -EINVAL;
  3220. }
  3221. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  3222. ctx_isp = (struct cam_isp_context *)ctx->ctx_priv;
  3223. if (buf_len <= *offset) {
  3224. CAM_WARN(CAM_ISP, "Dump buffer overshoot len %zu offset %zu",
  3225. buf_len, *offset);
  3226. return -ENOSPC;
  3227. }
  3228. remain_len = buf_len - *offset;
  3229. min_len = sizeof(struct cam_isp_context_dump_header) +
  3230. (CAM_ISP_CTX_DUMP_REQUEST_NUM_WORDS *
  3231. req_isp->num_fence_map_out *
  3232. sizeof(int32_t));
  3233. if (remain_len < min_len) {
  3234. CAM_WARN(CAM_ISP, "Dump buffer exhaust remain %zu min %u",
  3235. remain_len, min_len);
  3236. return -ENOSPC;
  3237. }
  3238. dst = (uint8_t *)cpu_addr + *offset;
  3239. hdr = (struct cam_isp_context_dump_header *)dst;
  3240. hdr->word_size = sizeof(int32_t);
  3241. scnprintf(hdr->tag, CAM_ISP_CONTEXT_DUMP_TAG_MAX_LEN,
  3242. "ISP_OUT_FENCE:");
  3243. addr = (int32_t *)(dst + sizeof(struct cam_isp_context_dump_header));
  3244. start = addr;
  3245. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  3246. *addr++ = req_isp->fence_map_out[i].resource_handle;
  3247. *addr++ = req_isp->fence_map_out[i].sync_id;
  3248. }
  3249. hdr->size = hdr->word_size * (addr - start);
  3250. *offset += hdr->size + sizeof(struct cam_isp_context_dump_header);
  3251. rc = cam_isp_ctx_dump_req(req_isp, cpu_addr, buf_len,
  3252. offset, true);
  3253. return rc;
  3254. }
  3255. static int __cam_isp_ctx_dump_in_top_state(
  3256. struct cam_context *ctx,
  3257. struct cam_req_mgr_dump_info *dump_info)
  3258. {
  3259. int rc = 0;
  3260. bool dump_only_event_record = false;
  3261. size_t buf_len;
  3262. size_t remain_len;
  3263. uint8_t *dst;
  3264. ktime_t cur_time;
  3265. uint32_t min_len;
  3266. uint64_t diff;
  3267. uint64_t *addr, *start;
  3268. uintptr_t cpu_addr;
  3269. struct timespec64 ts;
  3270. struct cam_isp_context *ctx_isp;
  3271. struct cam_ctx_request *req = NULL;
  3272. struct cam_isp_ctx_req *req_isp;
  3273. struct cam_ctx_request *req_temp;
  3274. struct cam_hw_dump_args dump_args;
  3275. struct cam_isp_context_dump_header *hdr;
  3276. spin_lock_bh(&ctx->lock);
  3277. list_for_each_entry_safe(req, req_temp,
  3278. &ctx->active_req_list, list) {
  3279. if (req->request_id == dump_info->req_id) {
  3280. CAM_INFO(CAM_ISP, "isp dump active list req: %lld",
  3281. dump_info->req_id);
  3282. goto hw_dump;
  3283. }
  3284. }
  3285. list_for_each_entry_safe(req, req_temp,
  3286. &ctx->wait_req_list, list) {
  3287. if (req->request_id == dump_info->req_id) {
  3288. CAM_INFO(CAM_ISP, "isp dump wait list req: %lld",
  3289. dump_info->req_id);
  3290. goto hw_dump;
  3291. }
  3292. }
  3293. spin_unlock_bh(&ctx->lock);
  3294. return rc;
  3295. hw_dump:
  3296. rc = cam_mem_get_cpu_buf(dump_info->buf_handle,
  3297. &cpu_addr, &buf_len);
  3298. if (rc) {
  3299. CAM_ERR(CAM_ISP, "Invalid handle %u rc %d",
  3300. dump_info->buf_handle, rc);
  3301. spin_unlock_bh(&ctx->lock);
  3302. return rc;
  3303. }
  3304. if (buf_len <= dump_info->offset) {
  3305. spin_unlock_bh(&ctx->lock);
  3306. CAM_WARN(CAM_ISP, "Dump buffer overshoot len %zu offset %zu",
  3307. buf_len, dump_info->offset);
  3308. return -ENOSPC;
  3309. }
  3310. remain_len = buf_len - dump_info->offset;
  3311. min_len = sizeof(struct cam_isp_context_dump_header) +
  3312. (CAM_ISP_CTX_DUMP_NUM_WORDS * sizeof(uint64_t));
  3313. if (remain_len < min_len) {
  3314. spin_unlock_bh(&ctx->lock);
  3315. CAM_WARN(CAM_ISP, "Dump buffer exhaust remain %zu min %u",
  3316. remain_len, min_len);
  3317. return -ENOSPC;
  3318. }
  3319. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  3320. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3321. cur_time = ktime_get();
  3322. diff = ktime_us_delta(
  3323. req_isp->event_timestamp[CAM_ISP_CTX_EVENT_APPLY],
  3324. cur_time);
  3325. if (diff < CAM_ISP_CTX_RESPONSE_TIME_THRESHOLD) {
  3326. CAM_INFO(CAM_ISP, "req %lld found no error",
  3327. req->request_id);
  3328. dump_only_event_record = true;
  3329. }
  3330. dst = (uint8_t *)cpu_addr + dump_info->offset;
  3331. hdr = (struct cam_isp_context_dump_header *)dst;
  3332. scnprintf(hdr->tag, CAM_ISP_CONTEXT_DUMP_TAG_MAX_LEN,
  3333. "ISP_CTX_DUMP:");
  3334. hdr->word_size = sizeof(uint64_t);
  3335. addr = (uint64_t *)(dst +
  3336. sizeof(struct cam_isp_context_dump_header));
  3337. start = addr;
  3338. *addr++ = req->request_id;
  3339. ts = ktime_to_timespec64(
  3340. req_isp->event_timestamp[CAM_ISP_CTX_EVENT_APPLY]);
  3341. *addr++ = ts.tv_sec;
  3342. *addr++ = ts.tv_nsec/NSEC_PER_USEC;
  3343. ts = ktime_to_timespec64(cur_time);
  3344. *addr++ = ts.tv_sec;
  3345. *addr++ = ts.tv_nsec/NSEC_PER_USEC;
  3346. hdr->size = hdr->word_size * (addr - start);
  3347. dump_info->offset += hdr->size +
  3348. sizeof(struct cam_isp_context_dump_header);
  3349. rc = __cam_isp_ctx_dump_event_record(ctx_isp, cpu_addr,
  3350. buf_len, &dump_info->offset);
  3351. if (rc) {
  3352. CAM_ERR(CAM_ISP, "Dump event fail %lld",
  3353. req->request_id);
  3354. spin_unlock_bh(&ctx->lock);
  3355. return rc;
  3356. }
  3357. if (dump_only_event_record) {
  3358. spin_unlock_bh(&ctx->lock);
  3359. return rc;
  3360. }
  3361. rc = __cam_isp_ctx_dump_req_info(ctx, req, cpu_addr,
  3362. buf_len, &dump_info->offset);
  3363. if (rc) {
  3364. CAM_ERR(CAM_ISP, "Dump Req info fail %lld",
  3365. req->request_id);
  3366. spin_unlock_bh(&ctx->lock);
  3367. return rc;
  3368. }
  3369. spin_unlock_bh(&ctx->lock);
  3370. if (ctx->hw_mgr_intf->hw_dump) {
  3371. dump_args.offset = dump_info->offset;
  3372. dump_args.request_id = dump_info->req_id;
  3373. dump_args.buf_handle = dump_info->buf_handle;
  3374. dump_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3375. rc = ctx->hw_mgr_intf->hw_dump(
  3376. ctx->hw_mgr_intf->hw_mgr_priv,
  3377. &dump_args);
  3378. dump_info->offset = dump_args.offset;
  3379. }
  3380. return rc;
  3381. }
  3382. static int __cam_isp_ctx_flush_req(struct cam_context *ctx,
  3383. struct list_head *req_list, struct cam_req_mgr_flush_request *flush_req)
  3384. {
  3385. int i, rc, tmp = 0;
  3386. uint32_t cancel_req_id_found = 0;
  3387. struct cam_ctx_request *req;
  3388. struct cam_ctx_request *req_temp;
  3389. struct cam_isp_ctx_req *req_isp;
  3390. struct list_head flush_list;
  3391. struct cam_isp_context *ctx_isp = NULL;
  3392. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  3393. INIT_LIST_HEAD(&flush_list);
  3394. if (list_empty(req_list)) {
  3395. CAM_DBG(CAM_ISP, "request list is empty");
  3396. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  3397. CAM_ERR(CAM_ISP, "no request to cancel");
  3398. return -EINVAL;
  3399. } else
  3400. return 0;
  3401. }
  3402. CAM_DBG(CAM_REQ, "Flush [%u] in progress for req_id %llu",
  3403. flush_req->type, flush_req->req_id);
  3404. list_for_each_entry_safe(req, req_temp, req_list, list) {
  3405. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  3406. if (req->request_id != flush_req->req_id) {
  3407. continue;
  3408. } else {
  3409. list_del_init(&req->list);
  3410. list_add_tail(&req->list, &flush_list);
  3411. cancel_req_id_found = 1;
  3412. __cam_isp_ctx_update_state_monitor_array(
  3413. ctx_isp,
  3414. CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH,
  3415. req->request_id);
  3416. break;
  3417. }
  3418. }
  3419. list_del_init(&req->list);
  3420. list_add_tail(&req->list, &flush_list);
  3421. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3422. CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH, req->request_id);
  3423. }
  3424. if (list_empty(&flush_list)) {
  3425. /*
  3426. * Maybe the req isn't sent to KMD since UMD already skip
  3427. * req in CSL layer.
  3428. */
  3429. CAM_INFO(CAM_ISP,
  3430. "flush list is empty, flush type %d for req %llu",
  3431. flush_req->type, flush_req->req_id);
  3432. return 0;
  3433. }
  3434. list_for_each_entry_safe(req, req_temp, &flush_list, list) {
  3435. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3436. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  3437. if (req_isp->fence_map_out[i].sync_id != -1) {
  3438. CAM_DBG(CAM_ISP, "Flush req 0x%llx, fence %d",
  3439. req->request_id,
  3440. req_isp->fence_map_out[i].sync_id);
  3441. rc = cam_sync_signal(
  3442. req_isp->fence_map_out[i].sync_id,
  3443. CAM_SYNC_STATE_SIGNALED_CANCEL,
  3444. CAM_SYNC_ISP_EVENT_FLUSH);
  3445. if (rc) {
  3446. tmp = req_isp->fence_map_out[i].sync_id;
  3447. CAM_ERR_RATE_LIMIT(CAM_ISP,
  3448. "signal fence %d failed", tmp);
  3449. }
  3450. req_isp->fence_map_out[i].sync_id = -1;
  3451. }
  3452. }
  3453. req_isp->reapply = false;
  3454. req_isp->cdm_reset_before_apply = false;
  3455. list_del_init(&req->list);
  3456. list_add_tail(&req->list, &ctx->free_req_list);
  3457. }
  3458. return 0;
  3459. }
  3460. static int __cam_isp_ctx_flush_req_in_top_state(
  3461. struct cam_context *ctx,
  3462. struct cam_req_mgr_flush_request *flush_req)
  3463. {
  3464. int rc = 0;
  3465. struct cam_isp_context *ctx_isp;
  3466. struct cam_isp_stop_args stop_isp;
  3467. struct cam_hw_stop_args stop_args;
  3468. struct cam_hw_reset_args reset_args;
  3469. struct cam_hw_cmd_args hw_cmd_args;
  3470. struct cam_req_mgr_timer_notify timer;
  3471. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  3472. CAM_DBG(CAM_ISP, "Flush pending list");
  3473. spin_lock_bh(&ctx->lock);
  3474. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  3475. spin_unlock_bh(&ctx->lock);
  3476. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_ALL) {
  3477. if (ctx->state <= CAM_CTX_READY) {
  3478. ctx->state = CAM_CTX_ACQUIRED;
  3479. goto end;
  3480. }
  3481. spin_lock_bh(&ctx->lock);
  3482. ctx->state = CAM_CTX_FLUSHED;
  3483. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  3484. spin_unlock_bh(&ctx->lock);
  3485. CAM_INFO(CAM_ISP, "Last request id to flush is %lld, ctx_id:%d",
  3486. flush_req->req_id, ctx->ctx_id);
  3487. ctx->last_flush_req = flush_req->req_id;
  3488. memset(&hw_cmd_args, 0, sizeof(hw_cmd_args));
  3489. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  3490. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_REG_DUMP_ON_FLUSH;
  3491. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  3492. &hw_cmd_args);
  3493. if (rc) {
  3494. CAM_ERR(CAM_ISP, "Reg dump on flush failed rc: %d", rc);
  3495. rc = 0;
  3496. }
  3497. stop_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3498. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  3499. stop_isp.stop_only = true;
  3500. stop_args.args = (void *)&stop_isp;
  3501. rc = ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  3502. &stop_args);
  3503. if (rc)
  3504. CAM_ERR(CAM_ISP, "Failed to stop HW in Flush rc: %d",
  3505. rc);
  3506. CAM_INFO(CAM_ISP, "Stop HW complete. Reset HW next.");
  3507. CAM_DBG(CAM_ISP, "Flush wait and active lists");
  3508. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_timer) {
  3509. timer.link_hdl = ctx->link_hdl;
  3510. timer.dev_hdl = ctx->dev_hdl;
  3511. timer.state = false;
  3512. ctx->ctx_crm_intf->notify_timer(&timer);
  3513. }
  3514. spin_lock_bh(&ctx->lock);
  3515. if (!list_empty(&ctx->wait_req_list))
  3516. rc = __cam_isp_ctx_flush_req(ctx, &ctx->wait_req_list,
  3517. flush_req);
  3518. if (!list_empty(&ctx->active_req_list))
  3519. rc = __cam_isp_ctx_flush_req(ctx, &ctx->active_req_list,
  3520. flush_req);
  3521. ctx_isp->active_req_cnt = 0;
  3522. spin_unlock_bh(&ctx->lock);
  3523. reset_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3524. rc = ctx->hw_mgr_intf->hw_reset(ctx->hw_mgr_intf->hw_mgr_priv,
  3525. &reset_args);
  3526. if (rc)
  3527. CAM_ERR(CAM_ISP, "Failed to reset HW rc: %d", rc);
  3528. ctx_isp->init_received = false;
  3529. }
  3530. end:
  3531. ctx_isp->bubble_frame_cnt = 0;
  3532. atomic_set(&ctx_isp->process_bubble, 0);
  3533. atomic_set(&ctx_isp->rxd_epoch, 0);
  3534. return rc;
  3535. }
  3536. static int __cam_isp_ctx_flush_req_in_ready(
  3537. struct cam_context *ctx,
  3538. struct cam_req_mgr_flush_request *flush_req)
  3539. {
  3540. int rc = 0;
  3541. CAM_DBG(CAM_ISP, "try to flush pending list");
  3542. spin_lock_bh(&ctx->lock);
  3543. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  3544. /* if nothing is in pending req list, change state to acquire */
  3545. if (list_empty(&ctx->pending_req_list))
  3546. ctx->state = CAM_CTX_ACQUIRED;
  3547. spin_unlock_bh(&ctx->lock);
  3548. trace_cam_context_state("ISP", ctx);
  3549. CAM_DBG(CAM_ISP, "Flush request in ready state. next state %d",
  3550. ctx->state);
  3551. return rc;
  3552. }
  3553. static struct cam_ctx_ops
  3554. cam_isp_ctx_activated_state_machine[CAM_ISP_CTX_ACTIVATED_MAX] = {
  3555. /* SOF */
  3556. {
  3557. .ioctl_ops = {},
  3558. .crm_ops = {
  3559. .apply_req = __cam_isp_ctx_apply_req_in_sof,
  3560. .notify_frame_skip =
  3561. __cam_isp_ctx_apply_default_req_settings,
  3562. .change_state = __cam_isp_ctx_change_substate,
  3563. },
  3564. .irq_ops = NULL,
  3565. },
  3566. /* APPLIED */
  3567. {
  3568. .ioctl_ops = {},
  3569. .crm_ops = {
  3570. .change_state = __cam_isp_ctx_change_substate,
  3571. .signal_buf_done = __cam_isp_ctx_signal_buf_done,
  3572. },
  3573. .irq_ops = NULL,
  3574. },
  3575. /* EPOCH */
  3576. {
  3577. .ioctl_ops = {},
  3578. .crm_ops = {
  3579. .apply_req = __cam_isp_ctx_apply_req_in_epoch,
  3580. .notify_frame_skip =
  3581. __cam_isp_ctx_apply_default_req_settings,
  3582. .change_state = __cam_isp_ctx_change_substate,
  3583. .signal_buf_done = __cam_isp_ctx_signal_buf_done,
  3584. },
  3585. .irq_ops = NULL,
  3586. },
  3587. /* BUBBLE */
  3588. {
  3589. .ioctl_ops = {},
  3590. .crm_ops = {
  3591. .apply_req = __cam_isp_ctx_apply_req_in_bubble,
  3592. .notify_frame_skip =
  3593. __cam_isp_ctx_apply_default_req_settings,
  3594. .change_state = __cam_isp_ctx_change_substate,
  3595. },
  3596. .irq_ops = NULL,
  3597. },
  3598. /* Bubble Applied */
  3599. {
  3600. .ioctl_ops = {},
  3601. .crm_ops = {
  3602. .change_state = __cam_isp_ctx_change_substate,
  3603. .signal_buf_done = __cam_isp_ctx_signal_buf_done,
  3604. },
  3605. .irq_ops = NULL,
  3606. },
  3607. /* HW ERROR */
  3608. {
  3609. .ioctl_ops = {},
  3610. .crm_ops = {},
  3611. .irq_ops = NULL,
  3612. },
  3613. /* HALT */
  3614. {
  3615. .ioctl_ops = {},
  3616. .crm_ops = {},
  3617. .irq_ops = NULL,
  3618. },
  3619. };
  3620. static struct cam_ctx_ops
  3621. cam_isp_ctx_fs2_state_machine[CAM_ISP_CTX_ACTIVATED_MAX] = {
  3622. /* SOF */
  3623. {
  3624. .ioctl_ops = {},
  3625. .crm_ops = {
  3626. .apply_req = __cam_isp_ctx_apply_req_in_sof,
  3627. },
  3628. .irq_ops = NULL,
  3629. },
  3630. /* APPLIED */
  3631. {
  3632. .ioctl_ops = {},
  3633. .crm_ops = {},
  3634. .irq_ops = NULL,
  3635. },
  3636. /* EPOCH */
  3637. {
  3638. .ioctl_ops = {},
  3639. .crm_ops = {
  3640. .apply_req = __cam_isp_ctx_apply_req_in_epoch,
  3641. },
  3642. .irq_ops = NULL,
  3643. },
  3644. /* BUBBLE */
  3645. {
  3646. .ioctl_ops = {},
  3647. .crm_ops = {
  3648. .apply_req = __cam_isp_ctx_apply_req_in_bubble,
  3649. },
  3650. .irq_ops = NULL,
  3651. },
  3652. /* Bubble Applied */
  3653. {
  3654. .ioctl_ops = {},
  3655. .crm_ops = {},
  3656. .irq_ops = NULL,
  3657. },
  3658. /* HW ERROR */
  3659. {
  3660. .ioctl_ops = {},
  3661. .crm_ops = {},
  3662. .irq_ops = NULL,
  3663. },
  3664. /* HALT */
  3665. {
  3666. .ioctl_ops = {},
  3667. .crm_ops = {},
  3668. .irq_ops = NULL,
  3669. },
  3670. };
  3671. static int __cam_isp_ctx_rdi_only_sof_in_top_state(
  3672. struct cam_isp_context *ctx_isp, void *evt_data)
  3673. {
  3674. int rc = 0;
  3675. struct cam_context *ctx = ctx_isp->base;
  3676. struct cam_req_mgr_trigger_notify notify;
  3677. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3678. uint64_t request_id = 0;
  3679. if (!evt_data) {
  3680. CAM_ERR(CAM_ISP, "in valid sof event data");
  3681. return -EINVAL;
  3682. }
  3683. ctx_isp->frame_id++;
  3684. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  3685. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  3686. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3687. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3688. /*
  3689. * notify reqmgr with sof signal. Note, due to scheduling delay
  3690. * we can run into situation that two active requests has already
  3691. * be in the active queue while we try to do the notification.
  3692. * In this case, we need to skip the current notification. This
  3693. * helps the state machine to catch up the delay.
  3694. */
  3695. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger &&
  3696. ctx_isp->active_req_cnt <= 2) {
  3697. notify.link_hdl = ctx->link_hdl;
  3698. notify.dev_hdl = ctx->dev_hdl;
  3699. notify.frame_id = ctx_isp->frame_id;
  3700. notify.trigger = CAM_TRIGGER_POINT_SOF;
  3701. notify.req_id = ctx_isp->req_info.last_bufdone_req_id;
  3702. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  3703. ctx->ctx_crm_intf->notify_trigger(&notify);
  3704. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  3705. ctx_isp->frame_id);
  3706. /*
  3707. * It is idle frame with out any applied request id, send
  3708. * request id as zero
  3709. */
  3710. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3711. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3712. } else {
  3713. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not notify SOF to CRM");
  3714. }
  3715. if (list_empty(&ctx->active_req_list))
  3716. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  3717. else
  3718. CAM_DBG(CAM_ISP, "Still need to wait for the buf done");
  3719. CAM_DBG(CAM_ISP, "next Substate[%s]",
  3720. __cam_isp_ctx_substate_val_to_type(
  3721. ctx_isp->substate_activated));
  3722. return rc;
  3723. }
  3724. static int __cam_isp_ctx_rdi_only_sof_in_applied_state(
  3725. struct cam_isp_context *ctx_isp, void *evt_data)
  3726. {
  3727. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3728. if (!evt_data) {
  3729. CAM_ERR(CAM_ISP, "in valid sof event data");
  3730. return -EINVAL;
  3731. }
  3732. ctx_isp->frame_id++;
  3733. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  3734. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  3735. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3736. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3737. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED;
  3738. CAM_DBG(CAM_ISP, "next Substate[%s]",
  3739. __cam_isp_ctx_substate_val_to_type(
  3740. ctx_isp->substate_activated));
  3741. return 0;
  3742. }
  3743. static int __cam_isp_ctx_rdi_only_sof_in_bubble_applied(
  3744. struct cam_isp_context *ctx_isp, void *evt_data)
  3745. {
  3746. struct cam_ctx_request *req;
  3747. struct cam_isp_ctx_req *req_isp;
  3748. struct cam_context *ctx = ctx_isp->base;
  3749. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3750. uint64_t request_id = 0;
  3751. /*
  3752. * Sof in bubble applied state means, reg update not received.
  3753. * before increment frame id and override time stamp value, send
  3754. * the previous sof time stamp that got captured in the
  3755. * sof in applied state.
  3756. */
  3757. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3758. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3759. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3760. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3761. ctx_isp->frame_id++;
  3762. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  3763. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  3764. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3765. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3766. if (list_empty(&ctx->wait_req_list)) {
  3767. /*
  3768. * If no pending req in epoch, this is an error case.
  3769. * The recovery is to go back to sof state
  3770. */
  3771. CAM_ERR(CAM_ISP, "No wait request");
  3772. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  3773. /* Send SOF event as empty frame*/
  3774. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3775. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3776. goto end;
  3777. }
  3778. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  3779. list);
  3780. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  3781. req_isp->bubble_detected = true;
  3782. CAM_INFO_RATE_LIMIT(CAM_ISP, "Ctx:%d Report Bubble flag %d req id:%lld",
  3783. ctx->ctx_id, req_isp->bubble_report, req->request_id);
  3784. req_isp->reapply = true;
  3785. req_isp->cdm_reset_before_apply = false;
  3786. if (req_isp->bubble_report && ctx->ctx_crm_intf &&
  3787. ctx->ctx_crm_intf->notify_err) {
  3788. struct cam_req_mgr_error_notify notify;
  3789. notify.link_hdl = ctx->link_hdl;
  3790. notify.dev_hdl = ctx->dev_hdl;
  3791. notify.req_id = req->request_id;
  3792. notify.error = CRM_KMD_ERR_BUBBLE;
  3793. notify.trigger = 0;
  3794. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF)
  3795. notify.trigger = CAM_TRIGGER_POINT_SOF;
  3796. notify.frame_id = ctx_isp->frame_id;
  3797. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  3798. CAM_WARN_RATE_LIMIT(CAM_ISP,
  3799. "Notify CRM about Bubble req %lld frame %lld ctx %u",
  3800. req->request_id,
  3801. ctx_isp->frame_id,
  3802. ctx->ctx_id);
  3803. ctx->ctx_crm_intf->notify_err(&notify);
  3804. atomic_set(&ctx_isp->process_bubble, 1);
  3805. } else {
  3806. req_isp->bubble_report = 0;
  3807. }
  3808. /*
  3809. * Always move the request to active list. Let buf done
  3810. * function handles the rest.
  3811. */
  3812. list_del_init(&req->list);
  3813. list_add_tail(&req->list, &ctx->active_req_list);
  3814. ctx_isp->active_req_cnt++;
  3815. CAM_DBG(CAM_ISP, "move request %lld to active list(cnt = %d)",
  3816. req->request_id, ctx_isp->active_req_cnt);
  3817. if (!req_isp->bubble_report) {
  3818. if (req->request_id > ctx_isp->reported_req_id) {
  3819. request_id = req->request_id;
  3820. ctx_isp->reported_req_id = request_id;
  3821. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3822. CAM_REQ_MGR_SOF_EVENT_ERROR);
  3823. } else
  3824. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3825. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3826. } else
  3827. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3828. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3829. /* change the state to bubble, as reg update has not come */
  3830. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  3831. CAM_DBG(CAM_ISP, "next Substate[%s]",
  3832. __cam_isp_ctx_substate_val_to_type(
  3833. ctx_isp->substate_activated));
  3834. end:
  3835. return 0;
  3836. }
  3837. static int __cam_isp_ctx_rdi_only_sof_in_bubble_state(
  3838. struct cam_isp_context *ctx_isp, void *evt_data)
  3839. {
  3840. uint32_t i;
  3841. struct cam_ctx_request *req;
  3842. struct cam_context *ctx = ctx_isp->base;
  3843. struct cam_req_mgr_trigger_notify notify;
  3844. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3845. struct cam_isp_ctx_req *req_isp;
  3846. struct cam_hw_cmd_args hw_cmd_args;
  3847. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  3848. uint64_t request_id = 0;
  3849. uint64_t last_cdm_done_req = 0;
  3850. int rc = 0;
  3851. if (!evt_data) {
  3852. CAM_ERR(CAM_ISP, "in valid sof event data");
  3853. return -EINVAL;
  3854. }
  3855. ctx_isp->frame_id++;
  3856. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  3857. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  3858. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3859. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3860. if (atomic_read(&ctx_isp->process_bubble)) {
  3861. if (list_empty(&ctx->active_req_list)) {
  3862. CAM_ERR(CAM_ISP, "No available active req in bubble");
  3863. atomic_set(&ctx_isp->process_bubble, 0);
  3864. return -EINVAL;
  3865. }
  3866. if (ctx_isp->last_sof_timestamp ==
  3867. ctx_isp->sof_timestamp_val) {
  3868. CAM_DBG(CAM_ISP,
  3869. "Tasklet delay detected! Bubble frame: %lld check skipped, sof_timestamp: %lld, ctx_id: %d",
  3870. ctx_isp->frame_id,
  3871. ctx_isp->sof_timestamp_val,
  3872. ctx->ctx_id);
  3873. goto end;
  3874. }
  3875. req = list_first_entry(&ctx->active_req_list,
  3876. struct cam_ctx_request, list);
  3877. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3878. if (req_isp->bubble_detected) {
  3879. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3880. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  3881. isp_hw_cmd_args.cmd_type =
  3882. CAM_ISP_HW_MGR_GET_LAST_CDM_DONE;
  3883. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  3884. rc = ctx->hw_mgr_intf->hw_cmd(
  3885. ctx->hw_mgr_intf->hw_mgr_priv,
  3886. &hw_cmd_args);
  3887. if (rc) {
  3888. CAM_ERR(CAM_ISP, "HW command failed");
  3889. return rc;
  3890. }
  3891. last_cdm_done_req = isp_hw_cmd_args.u.last_cdm_done;
  3892. CAM_DBG(CAM_ISP, "last_cdm_done req: %d ctx_id: %d",
  3893. last_cdm_done_req, ctx->ctx_id);
  3894. if (last_cdm_done_req >= req->request_id) {
  3895. CAM_DBG(CAM_ISP,
  3896. "CDM callback detected for req: %lld, possible buf_done delay, waiting for buf_done",
  3897. req->request_id);
  3898. goto end;
  3899. } else {
  3900. CAM_WARN(CAM_ISP,
  3901. "CDM callback not happened for req: %lld, possible CDM stuck or workqueue delay",
  3902. req->request_id);
  3903. req_isp->num_acked = 0;
  3904. req_isp->num_deferred_acks = 0;
  3905. req_isp->bubble_detected = false;
  3906. req_isp->cdm_reset_before_apply = true;
  3907. list_del_init(&req->list);
  3908. list_add(&req->list, &ctx->pending_req_list);
  3909. atomic_set(&ctx_isp->process_bubble, 0);
  3910. ctx_isp->active_req_cnt--;
  3911. CAM_DBG(CAM_REQ,
  3912. "Move active req: %lld to pending list(cnt = %d) [bubble re-apply],ctx %u",
  3913. req->request_id,
  3914. ctx_isp->active_req_cnt, ctx->ctx_id);
  3915. }
  3916. goto end;
  3917. }
  3918. }
  3919. /*
  3920. * Signal all active requests with error and move the all the active
  3921. * requests to free list
  3922. */
  3923. while (!list_empty(&ctx->active_req_list)) {
  3924. req = list_first_entry(&ctx->active_req_list,
  3925. struct cam_ctx_request, list);
  3926. list_del_init(&req->list);
  3927. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3928. CAM_DBG(CAM_ISP, "signal fence in active list. fence num %d",
  3929. req_isp->num_fence_map_out);
  3930. for (i = 0; i < req_isp->num_fence_map_out; i++)
  3931. if (req_isp->fence_map_out[i].sync_id != -1) {
  3932. cam_sync_signal(
  3933. req_isp->fence_map_out[i].sync_id,
  3934. CAM_SYNC_STATE_SIGNALED_ERROR,
  3935. CAM_SYNC_ISP_EVENT_BUBBLE);
  3936. }
  3937. list_add_tail(&req->list, &ctx->free_req_list);
  3938. ctx_isp->active_req_cnt--;
  3939. }
  3940. end:
  3941. /* notify reqmgr with sof signal */
  3942. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger) {
  3943. notify.link_hdl = ctx->link_hdl;
  3944. notify.dev_hdl = ctx->dev_hdl;
  3945. notify.frame_id = ctx_isp->frame_id;
  3946. notify.trigger = CAM_TRIGGER_POINT_SOF;
  3947. notify.req_id = ctx_isp->req_info.last_bufdone_req_id;
  3948. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  3949. ctx->ctx_crm_intf->notify_trigger(&notify);
  3950. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  3951. ctx_isp->frame_id);
  3952. } else {
  3953. CAM_ERR(CAM_ISP, "Can not notify SOF to CRM");
  3954. }
  3955. /*
  3956. * It is idle frame with out any applied request id, send
  3957. * request id as zero
  3958. */
  3959. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3960. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3961. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  3962. CAM_DBG(CAM_ISP, "next Substate[%s]",
  3963. __cam_isp_ctx_substate_val_to_type(
  3964. ctx_isp->substate_activated));
  3965. ctx_isp->last_sof_timestamp = ctx_isp->sof_timestamp_val;
  3966. return 0;
  3967. }
  3968. static int __cam_isp_ctx_rdi_only_reg_upd_in_bubble_state(
  3969. struct cam_isp_context *ctx_isp, void *evt_data)
  3970. {
  3971. struct cam_ctx_request *req = NULL;
  3972. struct cam_context *ctx = ctx_isp->base;
  3973. req = list_first_entry(&ctx->active_req_list,
  3974. struct cam_ctx_request, list);
  3975. CAM_INFO(CAM_ISP, "Received RUP for Bubble Request", req->request_id);
  3976. return 0;
  3977. }
  3978. static int __cam_isp_ctx_rdi_only_reg_upd_in_bubble_applied_state(
  3979. struct cam_isp_context *ctx_isp, void *evt_data)
  3980. {
  3981. struct cam_ctx_request *req = NULL;
  3982. struct cam_context *ctx = ctx_isp->base;
  3983. struct cam_isp_ctx_req *req_isp;
  3984. struct cam_req_mgr_trigger_notify notify;
  3985. uint64_t request_id = 0;
  3986. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  3987. /* notify reqmgr with sof signal*/
  3988. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger) {
  3989. if (list_empty(&ctx->wait_req_list)) {
  3990. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request");
  3991. goto error;
  3992. }
  3993. req = list_first_entry(&ctx->wait_req_list,
  3994. struct cam_ctx_request, list);
  3995. list_del_init(&req->list);
  3996. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3997. request_id =
  3998. (req_isp->hw_update_data.packet_opcode_type ==
  3999. CAM_ISP_PACKET_INIT_DEV) ?
  4000. 0 : req->request_id;
  4001. if (req_isp->num_fence_map_out != 0) {
  4002. list_add_tail(&req->list, &ctx->active_req_list);
  4003. ctx_isp->active_req_cnt++;
  4004. CAM_DBG(CAM_ISP,
  4005. "move request %lld to active list(cnt = %d)",
  4006. req->request_id, ctx_isp->active_req_cnt);
  4007. /* if packet has buffers, set correct request id */
  4008. request_id = req->request_id;
  4009. } else {
  4010. /* no io config, so the request is completed. */
  4011. list_add_tail(&req->list, &ctx->free_req_list);
  4012. CAM_DBG(CAM_ISP,
  4013. "move active req %lld to free list(cnt=%d)",
  4014. req->request_id, ctx_isp->active_req_cnt);
  4015. }
  4016. notify.link_hdl = ctx->link_hdl;
  4017. notify.dev_hdl = ctx->dev_hdl;
  4018. notify.frame_id = ctx_isp->frame_id;
  4019. notify.trigger = CAM_TRIGGER_POINT_SOF;
  4020. notify.req_id = ctx_isp->req_info.last_bufdone_req_id;
  4021. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  4022. ctx->ctx_crm_intf->notify_trigger(&notify);
  4023. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  4024. ctx_isp->frame_id);
  4025. } else {
  4026. CAM_ERR(CAM_ISP, "Can not notify SOF to CRM");
  4027. }
  4028. if (request_id)
  4029. ctx_isp->reported_req_id = request_id;
  4030. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4031. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  4032. CAM_DBG(CAM_ISP, "next Substate[%s]",
  4033. __cam_isp_ctx_substate_val_to_type(
  4034. ctx_isp->substate_activated));
  4035. __cam_isp_ctx_update_event_record(ctx_isp,
  4036. CAM_ISP_CTX_EVENT_RUP, req);
  4037. return 0;
  4038. error:
  4039. /* Send SOF event as idle frame*/
  4040. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4041. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  4042. __cam_isp_ctx_update_event_record(ctx_isp,
  4043. CAM_ISP_CTX_EVENT_RUP, NULL);
  4044. /*
  4045. * There is no request in the pending list, move the sub state machine
  4046. * to SOF sub state
  4047. */
  4048. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  4049. return 0;
  4050. }
  4051. static struct cam_isp_ctx_irq_ops
  4052. cam_isp_ctx_rdi_only_activated_state_machine_irq
  4053. [CAM_ISP_CTX_ACTIVATED_MAX] = {
  4054. /* SOF */
  4055. {
  4056. .irq_ops = {
  4057. NULL,
  4058. __cam_isp_ctx_rdi_only_sof_in_top_state,
  4059. __cam_isp_ctx_reg_upd_in_sof,
  4060. NULL,
  4061. NULL,
  4062. NULL,
  4063. },
  4064. },
  4065. /* APPLIED */
  4066. {
  4067. .irq_ops = {
  4068. __cam_isp_ctx_handle_error,
  4069. __cam_isp_ctx_rdi_only_sof_in_applied_state,
  4070. __cam_isp_ctx_reg_upd_in_applied_state,
  4071. NULL,
  4072. NULL,
  4073. __cam_isp_ctx_buf_done_in_applied,
  4074. },
  4075. },
  4076. /* EPOCH */
  4077. {
  4078. .irq_ops = {
  4079. __cam_isp_ctx_handle_error,
  4080. __cam_isp_ctx_rdi_only_sof_in_top_state,
  4081. NULL,
  4082. NULL,
  4083. NULL,
  4084. __cam_isp_ctx_buf_done_in_epoch,
  4085. },
  4086. },
  4087. /* BUBBLE*/
  4088. {
  4089. .irq_ops = {
  4090. __cam_isp_ctx_handle_error,
  4091. __cam_isp_ctx_rdi_only_sof_in_bubble_state,
  4092. __cam_isp_ctx_rdi_only_reg_upd_in_bubble_state,
  4093. NULL,
  4094. NULL,
  4095. __cam_isp_ctx_buf_done_in_bubble,
  4096. },
  4097. },
  4098. /* BUBBLE APPLIED ie PRE_BUBBLE */
  4099. {
  4100. .irq_ops = {
  4101. __cam_isp_ctx_handle_error,
  4102. __cam_isp_ctx_rdi_only_sof_in_bubble_applied,
  4103. __cam_isp_ctx_rdi_only_reg_upd_in_bubble_applied_state,
  4104. NULL,
  4105. NULL,
  4106. __cam_isp_ctx_buf_done_in_bubble_applied,
  4107. },
  4108. },
  4109. /* HW ERROR */
  4110. {
  4111. },
  4112. /* HALT */
  4113. {
  4114. },
  4115. };
  4116. static int __cam_isp_ctx_rdi_only_apply_req_top_state(
  4117. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  4118. {
  4119. int rc = 0;
  4120. struct cam_isp_context *ctx_isp =
  4121. (struct cam_isp_context *) ctx->ctx_priv;
  4122. CAM_DBG(CAM_ISP, "current Substate[%s]",
  4123. __cam_isp_ctx_substate_val_to_type(
  4124. ctx_isp->substate_activated));
  4125. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  4126. CAM_ISP_CTX_ACTIVATED_APPLIED);
  4127. CAM_DBG(CAM_ISP, "new Substate[%s]",
  4128. __cam_isp_ctx_substate_val_to_type(
  4129. ctx_isp->substate_activated));
  4130. if (rc)
  4131. CAM_ERR_RATE_LIMIT(CAM_ISP,
  4132. "ctx_id:%d Apply failed in Substate[%s], rc %d",
  4133. ctx->ctx_id,
  4134. __cam_isp_ctx_substate_val_to_type(
  4135. ctx_isp->substate_activated), rc);
  4136. return rc;
  4137. }
  4138. static struct cam_ctx_ops
  4139. cam_isp_ctx_rdi_only_activated_state_machine
  4140. [CAM_ISP_CTX_ACTIVATED_MAX] = {
  4141. /* SOF */
  4142. {
  4143. .ioctl_ops = {},
  4144. .crm_ops = {
  4145. .apply_req = __cam_isp_ctx_rdi_only_apply_req_top_state,
  4146. },
  4147. .irq_ops = NULL,
  4148. },
  4149. /* APPLIED */
  4150. {
  4151. .ioctl_ops = {},
  4152. .crm_ops = {},
  4153. .irq_ops = NULL,
  4154. },
  4155. /* EPOCH */
  4156. {
  4157. .ioctl_ops = {},
  4158. .crm_ops = {
  4159. .apply_req = __cam_isp_ctx_rdi_only_apply_req_top_state,
  4160. },
  4161. .irq_ops = NULL,
  4162. },
  4163. /* PRE BUBBLE */
  4164. {
  4165. .ioctl_ops = {},
  4166. .crm_ops = {},
  4167. .irq_ops = NULL,
  4168. },
  4169. /* BUBBLE */
  4170. {
  4171. .ioctl_ops = {},
  4172. .crm_ops = {},
  4173. .irq_ops = NULL,
  4174. },
  4175. /* HW ERROR */
  4176. {
  4177. .ioctl_ops = {},
  4178. .crm_ops = {},
  4179. .irq_ops = NULL,
  4180. },
  4181. /* HALT */
  4182. {
  4183. .ioctl_ops = {},
  4184. .crm_ops = {},
  4185. .irq_ops = NULL,
  4186. },
  4187. };
  4188. static void __cam_isp_ctx_free_mem_hw_entries(struct cam_context *ctx)
  4189. {
  4190. kfree(ctx->out_map_entries);
  4191. kfree(ctx->in_map_entries);
  4192. kfree(ctx->hw_update_entry);
  4193. ctx->out_map_entries = NULL;
  4194. ctx->in_map_entries = NULL;
  4195. ctx->hw_update_entry = NULL;
  4196. ctx->max_out_map_entries = 0;
  4197. ctx->max_in_map_entries = 0;
  4198. ctx->max_hw_update_entries = 0;
  4199. }
  4200. static int __cam_isp_ctx_release_hw_in_top_state(struct cam_context *ctx,
  4201. void *cmd)
  4202. {
  4203. int rc = 0;
  4204. struct cam_hw_release_args rel_arg;
  4205. struct cam_isp_context *ctx_isp =
  4206. (struct cam_isp_context *) ctx->ctx_priv;
  4207. struct cam_req_mgr_flush_request flush_req;
  4208. int i;
  4209. if (ctx_isp->hw_ctx) {
  4210. rel_arg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4211. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  4212. &rel_arg);
  4213. ctx_isp->hw_ctx = NULL;
  4214. } else {
  4215. CAM_ERR(CAM_ISP, "No hw resources acquired for ctx[%u]", ctx->ctx_id);
  4216. }
  4217. ctx->last_flush_req = 0;
  4218. ctx_isp->custom_enabled = false;
  4219. ctx_isp->use_frame_header_ts = false;
  4220. ctx_isp->use_default_apply = false;
  4221. ctx_isp->frame_id = 0;
  4222. ctx_isp->active_req_cnt = 0;
  4223. ctx_isp->reported_req_id = 0;
  4224. ctx_isp->hw_acquired = false;
  4225. ctx_isp->init_received = false;
  4226. ctx_isp->support_consumed_addr = false;
  4227. ctx_isp->req_info.last_bufdone_req_id = 0;
  4228. atomic64_set(&ctx_isp->state_monitor_head, -1);
  4229. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  4230. atomic64_set(&ctx_isp->event_record_head[i], -1);
  4231. /*
  4232. * Ideally, we should never have any active request here.
  4233. * But we still add some sanity check code here to help the debug
  4234. */
  4235. if (!list_empty(&ctx->active_req_list))
  4236. CAM_WARN(CAM_ISP, "Active list is not empty");
  4237. /* Flush all the pending request list */
  4238. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  4239. flush_req.link_hdl = ctx->link_hdl;
  4240. flush_req.dev_hdl = ctx->dev_hdl;
  4241. flush_req.req_id = 0;
  4242. CAM_DBG(CAM_ISP, "try to flush pending list");
  4243. spin_lock_bh(&ctx->lock);
  4244. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, &flush_req);
  4245. spin_unlock_bh(&ctx->lock);
  4246. __cam_isp_ctx_free_mem_hw_entries(ctx);
  4247. ctx->state = CAM_CTX_ACQUIRED;
  4248. trace_cam_context_state("ISP", ctx);
  4249. CAM_DBG(CAM_ISP, "Release device success[%u] next state %d",
  4250. ctx->ctx_id, ctx->state);
  4251. return rc;
  4252. }
  4253. /* top level state machine */
  4254. static int __cam_isp_ctx_release_dev_in_top_state(struct cam_context *ctx,
  4255. struct cam_release_dev_cmd *cmd)
  4256. {
  4257. int rc = 0;
  4258. int i;
  4259. struct cam_hw_release_args rel_arg;
  4260. struct cam_isp_context *ctx_isp =
  4261. (struct cam_isp_context *) ctx->ctx_priv;
  4262. struct cam_req_mgr_flush_request flush_req;
  4263. if (cmd && ctx_isp->hw_ctx) {
  4264. CAM_ERR(CAM_ISP, "releasing hw");
  4265. __cam_isp_ctx_release_hw_in_top_state(ctx, NULL);
  4266. }
  4267. if (ctx_isp->hw_ctx) {
  4268. rel_arg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4269. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  4270. &rel_arg);
  4271. ctx_isp->hw_ctx = NULL;
  4272. }
  4273. ctx->session_hdl = -1;
  4274. ctx->dev_hdl = -1;
  4275. ctx->link_hdl = -1;
  4276. ctx->ctx_crm_intf = NULL;
  4277. ctx->last_flush_req = 0;
  4278. ctx_isp->frame_id = 0;
  4279. ctx_isp->active_req_cnt = 0;
  4280. ctx_isp->reported_req_id = 0;
  4281. ctx_isp->hw_acquired = false;
  4282. ctx_isp->init_received = false;
  4283. ctx_isp->offline_context = false;
  4284. ctx_isp->rdi_only_context = false;
  4285. ctx_isp->req_info.last_bufdone_req_id = 0;
  4286. atomic64_set(&ctx_isp->state_monitor_head, -1);
  4287. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  4288. atomic64_set(&ctx_isp->event_record_head[i], -1);
  4289. /*
  4290. * Ideally, we should never have any active request here.
  4291. * But we still add some sanity check code here to help the debug
  4292. */
  4293. if (!list_empty(&ctx->active_req_list))
  4294. CAM_ERR(CAM_ISP, "Active list is not empty");
  4295. /* Flush all the pending request list */
  4296. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  4297. flush_req.link_hdl = ctx->link_hdl;
  4298. flush_req.dev_hdl = ctx->dev_hdl;
  4299. flush_req.req_id = 0;
  4300. CAM_DBG(CAM_ISP, "try to flush pending list");
  4301. spin_lock_bh(&ctx->lock);
  4302. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, &flush_req);
  4303. spin_unlock_bh(&ctx->lock);
  4304. __cam_isp_ctx_free_mem_hw_entries(ctx);
  4305. ctx->state = CAM_CTX_AVAILABLE;
  4306. trace_cam_context_state("ISP", ctx);
  4307. CAM_DBG(CAM_ISP, "Release device success[%u] next state %d",
  4308. ctx->ctx_id, ctx->state);
  4309. return rc;
  4310. }
  4311. static int __cam_isp_ctx_config_dev_in_top_state(
  4312. struct cam_context *ctx, struct cam_config_dev_cmd *cmd)
  4313. {
  4314. int rc = 0, i;
  4315. struct cam_ctx_request *req = NULL;
  4316. struct cam_isp_ctx_req *req_isp;
  4317. uintptr_t packet_addr;
  4318. struct cam_packet *packet;
  4319. size_t len = 0;
  4320. size_t remain_len = 0;
  4321. struct cam_hw_prepare_update_args cfg = {0};
  4322. struct cam_req_mgr_add_request add_req;
  4323. struct cam_isp_context *ctx_isp =
  4324. (struct cam_isp_context *) ctx->ctx_priv;
  4325. struct cam_hw_cmd_args hw_cmd_args;
  4326. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4327. uint32_t packet_opcode = 0;
  4328. CAM_DBG(CAM_ISP, "get free request object......");
  4329. /* get free request */
  4330. spin_lock_bh(&ctx->lock);
  4331. if (!list_empty(&ctx->free_req_list)) {
  4332. req = list_first_entry(&ctx->free_req_list,
  4333. struct cam_ctx_request, list);
  4334. list_del_init(&req->list);
  4335. }
  4336. spin_unlock_bh(&ctx->lock);
  4337. if (!req) {
  4338. CAM_ERR(CAM_ISP, "No more request obj free");
  4339. return -ENOMEM;
  4340. }
  4341. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4342. /* for config dev, only memory handle is supported */
  4343. /* map packet from the memhandle */
  4344. rc = cam_mem_get_cpu_buf((int32_t) cmd->packet_handle,
  4345. &packet_addr, &len);
  4346. if (rc != 0) {
  4347. CAM_ERR(CAM_ISP, "Can not get packet address");
  4348. rc = -EINVAL;
  4349. goto free_req;
  4350. }
  4351. remain_len = len;
  4352. if ((len < sizeof(struct cam_packet)) ||
  4353. ((size_t)cmd->offset >= len - sizeof(struct cam_packet))) {
  4354. CAM_ERR(CAM_ISP, "invalid buff length: %zu or offset", len);
  4355. rc = -EINVAL;
  4356. goto free_req;
  4357. }
  4358. remain_len -= (size_t)cmd->offset;
  4359. packet = (struct cam_packet *)(packet_addr + (uint32_t)cmd->offset);
  4360. CAM_DBG(CAM_ISP, "pack_handle %llx", cmd->packet_handle);
  4361. CAM_DBG(CAM_ISP, "packet address is 0x%zx", packet_addr);
  4362. CAM_DBG(CAM_ISP, "packet with length %zu, offset 0x%llx",
  4363. len, cmd->offset);
  4364. CAM_DBG(CAM_ISP, "Packet request id %lld",
  4365. packet->header.request_id);
  4366. CAM_DBG(CAM_ISP, "Packet size 0x%x", packet->header.size);
  4367. CAM_DBG(CAM_ISP, "packet op %d", packet->header.op_code);
  4368. /* Query the packet opcode */
  4369. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4370. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4371. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_GET_PACKET_OPCODE;
  4372. isp_hw_cmd_args.cmd_data = (void *)packet;
  4373. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4374. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4375. &hw_cmd_args);
  4376. if (rc) {
  4377. CAM_ERR(CAM_ISP, "HW command failed");
  4378. goto free_req;
  4379. }
  4380. packet_opcode = isp_hw_cmd_args.u.packet_op_code;
  4381. CAM_DBG(CAM_ISP, "packet op %d", packet_opcode);
  4382. if ((packet_opcode == CAM_ISP_PACKET_UPDATE_DEV)
  4383. && (packet->header.request_id <= ctx->last_flush_req)) {
  4384. CAM_INFO(CAM_ISP,
  4385. "request %lld has been flushed, reject packet",
  4386. packet->header.request_id);
  4387. rc = -EBADR;
  4388. goto free_req;
  4389. }
  4390. cfg.packet = packet;
  4391. cfg.remain_len = remain_len;
  4392. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4393. cfg.max_hw_update_entries = ctx->max_hw_update_entries;
  4394. cfg.hw_update_entries = req_isp->cfg;
  4395. cfg.max_out_map_entries = ctx->max_out_map_entries;
  4396. cfg.max_in_map_entries = ctx->max_in_map_entries;
  4397. cfg.out_map_entries = req_isp->fence_map_out;
  4398. cfg.in_map_entries = req_isp->fence_map_in;
  4399. cfg.priv = &req_isp->hw_update_data;
  4400. cfg.pf_data = &(req->pf_data);
  4401. cfg.num_out_map_entries = 0;
  4402. cfg.num_in_map_entries = 0;
  4403. req_isp->hw_update_data.fps = -1;
  4404. CAM_DBG(CAM_ISP, "try to prepare config packet......");
  4405. rc = ctx->hw_mgr_intf->hw_prepare_update(
  4406. ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  4407. if (rc != 0) {
  4408. CAM_ERR(CAM_ISP, "Prepare config packet failed in HW layer");
  4409. rc = -EFAULT;
  4410. goto free_req;
  4411. }
  4412. req_isp->num_cfg = cfg.num_hw_update_entries;
  4413. req_isp->num_fence_map_out = cfg.num_out_map_entries;
  4414. req_isp->num_fence_map_in = cfg.num_in_map_entries;
  4415. req_isp->num_acked = 0;
  4416. req_isp->num_deferred_acks = 0;
  4417. req_isp->bubble_detected = false;
  4418. req_isp->cdm_reset_before_apply = false;
  4419. req_isp->hw_update_data.packet = packet;
  4420. atomic_set(&req_isp->buf_done_ready, INIT_BUF_DONE);
  4421. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  4422. rc = cam_sync_get_obj_ref(req_isp->fence_map_out[i].sync_id);
  4423. if (rc) {
  4424. CAM_ERR(CAM_ISP, "Can't get ref for fence %d",
  4425. req_isp->fence_map_out[i].sync_id);
  4426. goto put_ref;
  4427. }
  4428. }
  4429. CAM_DBG(CAM_ISP, "num_entry: %d, num fence out: %d, num fence in: %d",
  4430. req_isp->num_cfg, req_isp->num_fence_map_out,
  4431. req_isp->num_fence_map_in);
  4432. req->request_id = packet->header.request_id;
  4433. req->status = 1;
  4434. CAM_DBG(CAM_ISP, "Packet request id %lld packet opcode:%d",
  4435. packet->header.request_id,
  4436. req_isp->hw_update_data.packet_opcode_type);
  4437. if (req_isp->hw_update_data.packet_opcode_type ==
  4438. CAM_ISP_PACKET_INIT_DEV) {
  4439. if (ctx->state < CAM_CTX_ACTIVATED) {
  4440. rc = __cam_isp_ctx_enqueue_init_request(ctx, req);
  4441. if (rc)
  4442. CAM_ERR(CAM_ISP, "Enqueue INIT pkt failed");
  4443. ctx_isp->init_received = true;
  4444. } else {
  4445. rc = -EINVAL;
  4446. CAM_ERR(CAM_ISP, "Recevied INIT pkt in wrong state:%d",
  4447. ctx->state);
  4448. }
  4449. } else {
  4450. if (ctx_isp->offline_context) {
  4451. __cam_isp_ctx_enqueue_request_in_order(ctx, req);
  4452. } else if ((ctx->state != CAM_CTX_FLUSHED) &&
  4453. (ctx->state >= CAM_CTX_READY) &&
  4454. ctx->ctx_crm_intf->add_req) {
  4455. memset(&add_req, 0, sizeof(add_req));
  4456. add_req.link_hdl = ctx->link_hdl;
  4457. add_req.dev_hdl = ctx->dev_hdl;
  4458. add_req.req_id = req->request_id;
  4459. rc = ctx->ctx_crm_intf->add_req(&add_req);
  4460. if (rc) {
  4461. CAM_ERR(CAM_ISP, "Add req failed: req id=%llu",
  4462. req->request_id);
  4463. } else {
  4464. __cam_isp_ctx_enqueue_request_in_order(
  4465. ctx, req);
  4466. }
  4467. } else {
  4468. rc = -EINVAL;
  4469. CAM_ERR(CAM_ISP,
  4470. "Recevied update req %lld in wrong state:%d",
  4471. req->request_id, ctx->state);
  4472. }
  4473. }
  4474. if (rc)
  4475. goto put_ref;
  4476. CAM_DBG(CAM_REQ,
  4477. "Preprocessing Config req_id %lld successful on ctx %u",
  4478. req->request_id, ctx->ctx_id);
  4479. if (ctx_isp->offline_context && atomic_read(&ctx_isp->rxd_epoch)) {
  4480. __cam_isp_ctx_schedule_apply_req_offline(ctx_isp);
  4481. }
  4482. return rc;
  4483. put_ref:
  4484. for (--i; i >= 0; i--) {
  4485. if (cam_sync_put_obj_ref(req_isp->fence_map_out[i].sync_id))
  4486. CAM_ERR(CAM_CTXT, "Failed to put ref of fence %d",
  4487. req_isp->fence_map_out[i].sync_id);
  4488. }
  4489. free_req:
  4490. spin_lock_bh(&ctx->lock);
  4491. list_add_tail(&req->list, &ctx->free_req_list);
  4492. spin_unlock_bh(&ctx->lock);
  4493. return rc;
  4494. }
  4495. static int __cam_isp_ctx_allocate_mem_hw_entries(
  4496. struct cam_context *ctx,
  4497. struct cam_hw_acquire_args *param)
  4498. {
  4499. int rc = 0;
  4500. uint32_t max_res = 0;
  4501. uint32_t max_hw_upd_entries = CAM_ISP_CTX_CFG_MAX;
  4502. struct cam_ctx_request *req;
  4503. struct cam_ctx_request *temp_req;
  4504. struct cam_isp_ctx_req *req_isp;
  4505. size_t num_entries = 0;
  4506. if (!param->op_params.param_list[0])
  4507. max_res = CAM_ISP_CTX_RES_MAX;
  4508. else {
  4509. max_res = param->op_params.param_list[0];
  4510. if (param->op_flags & CAM_IFE_CTX_SFE_EN) {
  4511. max_res += param->op_params.param_list[1];
  4512. max_hw_upd_entries = CAM_ISP_SFE_CTX_CFG_MAX;
  4513. }
  4514. }
  4515. ctx->max_in_map_entries = max_res;
  4516. ctx->max_out_map_entries = max_res;
  4517. ctx->max_hw_update_entries = max_hw_upd_entries;
  4518. CAM_DBG(CAM_ISP,
  4519. "Allocate max_entries: 0x%x max_res: 0x%x is_sfe_en: %d",
  4520. max_hw_upd_entries, max_res, (param->op_flags & CAM_IFE_CTX_SFE_EN));
  4521. num_entries = ctx->max_hw_update_entries * CAM_ISP_CTX_REQ_MAX;
  4522. ctx->hw_update_entry = kcalloc(num_entries,
  4523. sizeof(struct cam_hw_update_entry),
  4524. GFP_KERNEL);
  4525. if (!ctx->hw_update_entry) {
  4526. CAM_ERR(CAM_CTXT, "%s[%d] no memory", ctx->dev_name, ctx->ctx_id);
  4527. return -ENOMEM;
  4528. }
  4529. num_entries = ctx->max_in_map_entries * CAM_ISP_CTX_REQ_MAX;
  4530. ctx->in_map_entries = kcalloc(num_entries,
  4531. sizeof(struct cam_hw_fence_map_entry),
  4532. GFP_KERNEL);
  4533. if (!ctx->in_map_entries) {
  4534. CAM_ERR(CAM_CTXT, "%s[%d] no memory", ctx->dev_name, ctx->ctx_id);
  4535. rc = -ENOMEM;
  4536. goto end;
  4537. }
  4538. num_entries = ctx->max_out_map_entries * CAM_ISP_CTX_REQ_MAX;
  4539. ctx->out_map_entries = kcalloc(num_entries,
  4540. sizeof(struct cam_hw_fence_map_entry),
  4541. GFP_KERNEL);
  4542. if (!ctx->out_map_entries) {
  4543. CAM_ERR(CAM_CTXT, "%s[%d] no memory", ctx->dev_name, ctx->ctx_id);
  4544. rc = -ENOMEM;
  4545. goto end;
  4546. }
  4547. list_for_each_entry_safe(req, temp_req,
  4548. &ctx->free_req_list, list) {
  4549. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4550. req_isp->cfg =
  4551. &ctx->hw_update_entry[req->index * ctx->max_hw_update_entries];
  4552. req_isp->fence_map_in =
  4553. &ctx->in_map_entries[req->index * ctx->max_in_map_entries];
  4554. req_isp->fence_map_out =
  4555. &ctx->out_map_entries[req->index * ctx->max_out_map_entries];
  4556. }
  4557. return rc;
  4558. end:
  4559. __cam_isp_ctx_free_mem_hw_entries(ctx);
  4560. return rc;
  4561. }
  4562. static int __cam_isp_ctx_acquire_dev_in_available(struct cam_context *ctx,
  4563. struct cam_acquire_dev_cmd *cmd)
  4564. {
  4565. int rc = 0;
  4566. int i;
  4567. struct cam_hw_acquire_args param;
  4568. struct cam_isp_resource *isp_res = NULL;
  4569. struct cam_create_dev_hdl req_hdl_param;
  4570. struct cam_hw_release_args release;
  4571. struct cam_isp_context *ctx_isp =
  4572. (struct cam_isp_context *) ctx->ctx_priv;
  4573. struct cam_hw_cmd_args hw_cmd_args;
  4574. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4575. if (!ctx->hw_mgr_intf) {
  4576. CAM_ERR(CAM_ISP, "HW interface is not ready");
  4577. rc = -EFAULT;
  4578. goto end;
  4579. }
  4580. CAM_DBG(CAM_ISP,
  4581. "session_hdl 0x%x, num_resources %d, hdl type %d, res %lld",
  4582. cmd->session_handle, cmd->num_resources,
  4583. cmd->handle_type, cmd->resource_hdl);
  4584. if (cmd->num_resources == CAM_API_COMPAT_CONSTANT) {
  4585. ctx_isp->split_acquire = true;
  4586. CAM_DBG(CAM_ISP, "Acquire dev handle");
  4587. goto get_dev_handle;
  4588. }
  4589. if (cmd->num_resources > CAM_ISP_CTX_RES_MAX) {
  4590. CAM_ERR(CAM_ISP, "Too much resources in the acquire");
  4591. rc = -ENOMEM;
  4592. goto end;
  4593. }
  4594. /* for now we only support user pointer */
  4595. if (cmd->handle_type != 1) {
  4596. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  4597. rc = -EINVAL;
  4598. goto end;
  4599. }
  4600. isp_res = kzalloc(
  4601. sizeof(*isp_res)*cmd->num_resources, GFP_KERNEL);
  4602. if (!isp_res) {
  4603. rc = -ENOMEM;
  4604. goto end;
  4605. }
  4606. CAM_DBG(CAM_ISP, "start copy %d resources from user",
  4607. cmd->num_resources);
  4608. if (copy_from_user(isp_res, u64_to_user_ptr(cmd->resource_hdl),
  4609. sizeof(*isp_res)*cmd->num_resources)) {
  4610. rc = -EFAULT;
  4611. goto free_res;
  4612. }
  4613. memset(&param, 0, sizeof(param));
  4614. param.context_data = ctx;
  4615. param.event_cb = ctx->irq_cb_intf;
  4616. param.num_acq = cmd->num_resources;
  4617. param.acquire_info = (uintptr_t) isp_res;
  4618. rc = __cam_isp_ctx_allocate_mem_hw_entries(ctx, &param);
  4619. if (rc) {
  4620. CAM_ERR(CAM_ISP, "Ctx[%d] allocate hw entry fail",
  4621. ctx->ctx_id);
  4622. goto free_res;
  4623. }
  4624. /* call HW manager to reserve the resource */
  4625. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  4626. &param);
  4627. if (rc != 0) {
  4628. CAM_ERR(CAM_ISP, "Acquire device failed");
  4629. goto free_res;
  4630. }
  4631. /* Query the context has rdi only resource */
  4632. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  4633. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4634. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  4635. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4636. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4637. &hw_cmd_args);
  4638. if (rc) {
  4639. CAM_ERR(CAM_ISP, "HW command failed");
  4640. goto free_hw;
  4641. }
  4642. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  4643. /*
  4644. * this context has rdi only resource assign rdi only
  4645. * state machine
  4646. */
  4647. CAM_DBG(CAM_ISP, "RDI only session Context");
  4648. ctx_isp->substate_machine_irq =
  4649. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  4650. ctx_isp->substate_machine =
  4651. cam_isp_ctx_rdi_only_activated_state_machine;
  4652. ctx_isp->rdi_only_context = true;
  4653. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  4654. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI");
  4655. ctx_isp->substate_machine_irq =
  4656. cam_isp_ctx_fs2_state_machine_irq;
  4657. ctx_isp->substate_machine =
  4658. cam_isp_ctx_fs2_state_machine;
  4659. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  4660. CAM_DBG(CAM_ISP, "offline Session has PIX and RD resources");
  4661. ctx_isp->substate_machine_irq =
  4662. cam_isp_ctx_offline_state_machine_irq;
  4663. } else {
  4664. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  4665. ctx_isp->substate_machine_irq =
  4666. cam_isp_ctx_activated_state_machine_irq;
  4667. ctx_isp->substate_machine =
  4668. cam_isp_ctx_activated_state_machine;
  4669. }
  4670. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  4671. ctx_isp->hw_acquired = true;
  4672. ctx_isp->split_acquire = false;
  4673. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  4674. atomic64_set(&ctx_isp->state_monitor_head, -1);
  4675. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  4676. atomic64_set(&ctx_isp->event_record_head[i], -1);
  4677. kfree(isp_res);
  4678. isp_res = NULL;
  4679. get_dev_handle:
  4680. req_hdl_param.session_hdl = cmd->session_handle;
  4681. /* bridge is not ready for these flags. so false for now */
  4682. req_hdl_param.v4l2_sub_dev_flag = 0;
  4683. req_hdl_param.media_entity_flag = 0;
  4684. req_hdl_param.ops = ctx->crm_ctx_intf;
  4685. req_hdl_param.priv = ctx;
  4686. req_hdl_param.dev_id = CAM_ISP;
  4687. CAM_DBG(CAM_ISP, "get device handle form bridge");
  4688. ctx->dev_hdl = cam_create_device_hdl(&req_hdl_param);
  4689. if (ctx->dev_hdl <= 0) {
  4690. rc = -EFAULT;
  4691. CAM_ERR(CAM_ISP, "Can not create device handle");
  4692. goto free_hw;
  4693. }
  4694. cmd->dev_handle = ctx->dev_hdl;
  4695. /* store session information */
  4696. ctx->session_hdl = cmd->session_handle;
  4697. ctx->state = CAM_CTX_ACQUIRED;
  4698. trace_cam_context_state("ISP", ctx);
  4699. CAM_DBG(CAM_ISP,
  4700. "Acquire success on session_hdl 0x%x num_rsrces %d ctx %u",
  4701. cmd->session_handle, cmd->num_resources, ctx->ctx_id);
  4702. return rc;
  4703. free_hw:
  4704. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4705. if (ctx_isp->hw_acquired)
  4706. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  4707. &release);
  4708. ctx_isp->hw_ctx = NULL;
  4709. ctx_isp->hw_acquired = false;
  4710. free_res:
  4711. kfree(isp_res);
  4712. end:
  4713. return rc;
  4714. }
  4715. static int __cam_isp_ctx_acquire_hw_v1(struct cam_context *ctx,
  4716. void *args)
  4717. {
  4718. int rc = 0;
  4719. int i;
  4720. struct cam_acquire_hw_cmd_v1 *cmd =
  4721. (struct cam_acquire_hw_cmd_v1 *)args;
  4722. struct cam_hw_acquire_args param;
  4723. struct cam_hw_release_args release;
  4724. struct cam_isp_context *ctx_isp =
  4725. (struct cam_isp_context *) ctx->ctx_priv;
  4726. struct cam_hw_cmd_args hw_cmd_args;
  4727. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4728. struct cam_isp_acquire_hw_info *acquire_hw_info = NULL;
  4729. if (!ctx->hw_mgr_intf) {
  4730. CAM_ERR(CAM_ISP, "HW interface is not ready");
  4731. rc = -EFAULT;
  4732. goto end;
  4733. }
  4734. CAM_DBG(CAM_ISP,
  4735. "session_hdl 0x%x, hdl type %d, res %lld",
  4736. cmd->session_handle, cmd->handle_type, cmd->resource_hdl);
  4737. /* for now we only support user pointer */
  4738. if (cmd->handle_type != 1) {
  4739. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  4740. rc = -EINVAL;
  4741. goto end;
  4742. }
  4743. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  4744. CAM_ERR(CAM_ISP, "data_size is not a valid value");
  4745. goto end;
  4746. }
  4747. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  4748. if (!acquire_hw_info) {
  4749. rc = -ENOMEM;
  4750. goto end;
  4751. }
  4752. CAM_DBG(CAM_ISP, "start copy resources from user");
  4753. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  4754. cmd->data_size)) {
  4755. rc = -EFAULT;
  4756. goto free_res;
  4757. }
  4758. memset(&param, 0, sizeof(param));
  4759. param.context_data = ctx;
  4760. param.event_cb = ctx->irq_cb_intf;
  4761. param.num_acq = CAM_API_COMPAT_CONSTANT;
  4762. param.acquire_info_size = cmd->data_size;
  4763. param.acquire_info = (uint64_t) acquire_hw_info;
  4764. rc = __cam_isp_ctx_allocate_mem_hw_entries(ctx,
  4765. &param);
  4766. if (rc) {
  4767. CAM_ERR(CAM_ISP, "Ctx[%d] allocate hw entry fail",
  4768. ctx->ctx_id);
  4769. goto free_res;
  4770. }
  4771. /* call HW manager to reserve the resource */
  4772. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  4773. &param);
  4774. if (rc != 0) {
  4775. CAM_ERR(CAM_ISP, "Acquire device failed");
  4776. goto free_res;
  4777. }
  4778. ctx_isp->support_consumed_addr =
  4779. (param.op_flags & CAM_IFE_CTX_FRAME_HEADER_EN);
  4780. /* Query the context has rdi only resource */
  4781. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  4782. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4783. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  4784. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4785. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4786. &hw_cmd_args);
  4787. if (rc) {
  4788. CAM_ERR(CAM_ISP, "HW command failed");
  4789. goto free_hw;
  4790. }
  4791. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  4792. /*
  4793. * this context has rdi only resource assign rdi only
  4794. * state machine
  4795. */
  4796. CAM_DBG(CAM_ISP, "RDI only session Context");
  4797. ctx_isp->substate_machine_irq =
  4798. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  4799. ctx_isp->substate_machine =
  4800. cam_isp_ctx_rdi_only_activated_state_machine;
  4801. ctx_isp->rdi_only_context = true;
  4802. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  4803. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI");
  4804. ctx_isp->substate_machine_irq =
  4805. cam_isp_ctx_fs2_state_machine_irq;
  4806. ctx_isp->substate_machine =
  4807. cam_isp_ctx_fs2_state_machine;
  4808. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  4809. CAM_DBG(CAM_ISP, "Offline session has PIX and RD resources");
  4810. ctx_isp->substate_machine_irq =
  4811. cam_isp_ctx_offline_state_machine_irq;
  4812. ctx_isp->substate_machine = NULL;
  4813. } else {
  4814. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  4815. ctx_isp->substate_machine_irq =
  4816. cam_isp_ctx_activated_state_machine_irq;
  4817. ctx_isp->substate_machine =
  4818. cam_isp_ctx_activated_state_machine;
  4819. }
  4820. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  4821. ctx_isp->hw_acquired = true;
  4822. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  4823. atomic64_set(&ctx_isp->state_monitor_head, -1);
  4824. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  4825. atomic64_set(&ctx_isp->event_record_head[i], -1);
  4826. trace_cam_context_state("ISP", ctx);
  4827. CAM_DBG(CAM_ISP,
  4828. "Acquire success on session_hdl 0x%xs ctx_type %d ctx_id %u",
  4829. ctx->session_hdl, isp_hw_cmd_args.u.ctx_type, ctx->ctx_id);
  4830. kfree(acquire_hw_info);
  4831. return rc;
  4832. free_hw:
  4833. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4834. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv, &release);
  4835. ctx_isp->hw_ctx = NULL;
  4836. ctx_isp->hw_acquired = false;
  4837. free_res:
  4838. kfree(acquire_hw_info);
  4839. end:
  4840. return rc;
  4841. }
  4842. static void cam_req_mgr_process_workq_offline_ife_worker(struct work_struct *w)
  4843. {
  4844. cam_req_mgr_process_workq(w);
  4845. }
  4846. static int __cam_isp_ctx_acquire_hw_v2(struct cam_context *ctx,
  4847. void *args)
  4848. {
  4849. int rc = 0, i, j;
  4850. struct cam_acquire_hw_cmd_v2 *cmd =
  4851. (struct cam_acquire_hw_cmd_v2 *)args;
  4852. struct cam_hw_acquire_args param;
  4853. struct cam_hw_release_args release;
  4854. struct cam_isp_context *ctx_isp =
  4855. (struct cam_isp_context *) ctx->ctx_priv;
  4856. struct cam_hw_cmd_args hw_cmd_args;
  4857. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4858. struct cam_isp_acquire_hw_info *acquire_hw_info = NULL;
  4859. if (!ctx->hw_mgr_intf) {
  4860. CAM_ERR(CAM_ISP, "HW interface is not ready");
  4861. rc = -EFAULT;
  4862. goto end;
  4863. }
  4864. CAM_DBG(CAM_ISP,
  4865. "session_hdl 0x%x, hdl type %d, res %lld",
  4866. cmd->session_handle, cmd->handle_type, cmd->resource_hdl);
  4867. /* for now we only support user pointer */
  4868. if (cmd->handle_type != 1) {
  4869. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  4870. rc = -EINVAL;
  4871. goto end;
  4872. }
  4873. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  4874. CAM_ERR(CAM_ISP, "data_size is not a valid value");
  4875. goto end;
  4876. }
  4877. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  4878. if (!acquire_hw_info) {
  4879. rc = -ENOMEM;
  4880. goto end;
  4881. }
  4882. CAM_DBG(CAM_ISP, "start copy resources from user");
  4883. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  4884. cmd->data_size)) {
  4885. rc = -EFAULT;
  4886. goto free_res;
  4887. }
  4888. memset(&param, 0, sizeof(param));
  4889. param.context_data = ctx;
  4890. param.event_cb = ctx->irq_cb_intf;
  4891. param.num_acq = CAM_API_COMPAT_CONSTANT;
  4892. param.acquire_info_size = cmd->data_size;
  4893. param.acquire_info = (uint64_t) acquire_hw_info;
  4894. /* call HW manager to reserve the resource */
  4895. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  4896. &param);
  4897. if (rc != 0) {
  4898. CAM_ERR(CAM_ISP, "Acquire device failed");
  4899. goto free_res;
  4900. }
  4901. rc = __cam_isp_ctx_allocate_mem_hw_entries(ctx, &param);
  4902. if (rc) {
  4903. CAM_ERR(CAM_ISP, "Ctx[%d] allocate hw entry fail",
  4904. ctx->ctx_id);
  4905. goto free_hw;
  4906. }
  4907. /*
  4908. * Set feature flag if applicable
  4909. * custom hw is supported only on v2
  4910. */
  4911. ctx_isp->custom_enabled =
  4912. (param.op_flags & CAM_IFE_CTX_CUSTOM_EN);
  4913. ctx_isp->use_frame_header_ts =
  4914. (param.op_flags & CAM_IFE_CTX_FRAME_HEADER_EN);
  4915. ctx_isp->use_default_apply =
  4916. (param.op_flags & CAM_IFE_CTX_APPLY_DEFAULT_CFG);
  4917. ctx_isp->support_consumed_addr =
  4918. (param.op_flags & CAM_IFE_CTX_CONSUME_ADDR_EN);
  4919. /* Query the context has rdi only resource */
  4920. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  4921. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4922. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  4923. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4924. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4925. &hw_cmd_args);
  4926. if (rc) {
  4927. CAM_ERR(CAM_ISP, "HW command failed");
  4928. goto free_hw;
  4929. }
  4930. if (param.valid_acquired_hw) {
  4931. for (i = 0; i < CAM_MAX_ACQ_RES; i++)
  4932. cmd->hw_info.acquired_hw_id[i] =
  4933. param.acquired_hw_id[i];
  4934. for (i = 0; i < CAM_MAX_ACQ_RES; i++)
  4935. for (j = 0; j < CAM_MAX_HW_SPLIT; j++)
  4936. cmd->hw_info.acquired_hw_path[i][j] =
  4937. param.acquired_hw_path[i][j];
  4938. }
  4939. cmd->hw_info.valid_acquired_hw =
  4940. param.valid_acquired_hw;
  4941. cmd->hw_info.valid_acquired_hw = param.valid_acquired_hw;
  4942. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  4943. /*
  4944. * this context has rdi only resource assign rdi only
  4945. * state machine
  4946. */
  4947. CAM_DBG(CAM_ISP, "RDI only session Context");
  4948. ctx_isp->substate_machine_irq =
  4949. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  4950. ctx_isp->substate_machine =
  4951. cam_isp_ctx_rdi_only_activated_state_machine;
  4952. ctx_isp->rdi_only_context = true;
  4953. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  4954. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI");
  4955. ctx_isp->substate_machine_irq =
  4956. cam_isp_ctx_fs2_state_machine_irq;
  4957. ctx_isp->substate_machine =
  4958. cam_isp_ctx_fs2_state_machine;
  4959. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  4960. CAM_DBG(CAM_ISP, "Offline Session has PIX and RD resources");
  4961. ctx_isp->substate_machine_irq =
  4962. cam_isp_ctx_offline_state_machine_irq;
  4963. ctx_isp->substate_machine = NULL;
  4964. ctx_isp->offline_context = true;
  4965. rc = cam_req_mgr_workq_create("offline_ife", 20,
  4966. &ctx_isp->workq, CRM_WORKQ_USAGE_IRQ, 0,
  4967. cam_req_mgr_process_workq_offline_ife_worker);
  4968. if (rc)
  4969. CAM_ERR(CAM_ISP,
  4970. "Failed to create workq for offline IFE rc:%d",
  4971. rc);
  4972. } else {
  4973. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  4974. ctx_isp->substate_machine_irq =
  4975. cam_isp_ctx_activated_state_machine_irq;
  4976. ctx_isp->substate_machine =
  4977. cam_isp_ctx_activated_state_machine;
  4978. }
  4979. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  4980. ctx_isp->hw_acquired = true;
  4981. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  4982. trace_cam_context_state("ISP", ctx);
  4983. CAM_DBG(CAM_ISP,
  4984. "Acquire success on session_hdl 0x%xs ctx_type %d ctx_id %u",
  4985. ctx->session_hdl, isp_hw_cmd_args.u.ctx_type, ctx->ctx_id);
  4986. kfree(acquire_hw_info);
  4987. return rc;
  4988. free_hw:
  4989. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4990. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv, &release);
  4991. ctx_isp->hw_ctx = NULL;
  4992. ctx_isp->hw_acquired = false;
  4993. free_res:
  4994. kfree(acquire_hw_info);
  4995. end:
  4996. return rc;
  4997. }
  4998. static int __cam_isp_ctx_acquire_hw_in_acquired(struct cam_context *ctx,
  4999. void *args)
  5000. {
  5001. int rc = -EINVAL;
  5002. uint32_t api_version;
  5003. if (!ctx || !args) {
  5004. CAM_ERR(CAM_ISP, "Invalid input pointer");
  5005. return rc;
  5006. }
  5007. api_version = *((uint32_t *)args);
  5008. if (api_version == 1)
  5009. rc = __cam_isp_ctx_acquire_hw_v1(ctx, args);
  5010. else if (api_version == 2)
  5011. rc = __cam_isp_ctx_acquire_hw_v2(ctx, args);
  5012. else
  5013. CAM_ERR(CAM_ISP, "Unsupported api version %d", api_version);
  5014. return rc;
  5015. }
  5016. static int __cam_isp_ctx_config_dev_in_acquired(struct cam_context *ctx,
  5017. struct cam_config_dev_cmd *cmd)
  5018. {
  5019. int rc = 0;
  5020. struct cam_isp_context *ctx_isp =
  5021. (struct cam_isp_context *) ctx->ctx_priv;
  5022. if (!ctx_isp->hw_acquired) {
  5023. CAM_ERR(CAM_ISP, "HW is not acquired, reject packet");
  5024. return -EINVAL;
  5025. }
  5026. rc = __cam_isp_ctx_config_dev_in_top_state(ctx, cmd);
  5027. if (!rc && ((ctx->link_hdl >= 0) || ctx_isp->offline_context)) {
  5028. ctx->state = CAM_CTX_READY;
  5029. trace_cam_context_state("ISP", ctx);
  5030. }
  5031. CAM_DBG(CAM_ISP, "next state %d", ctx->state);
  5032. return rc;
  5033. }
  5034. static int __cam_isp_ctx_config_dev_in_flushed(struct cam_context *ctx,
  5035. struct cam_config_dev_cmd *cmd)
  5036. {
  5037. int rc = 0;
  5038. struct cam_start_stop_dev_cmd start_cmd;
  5039. struct cam_hw_cmd_args hw_cmd_args;
  5040. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5041. struct cam_isp_context *ctx_isp =
  5042. (struct cam_isp_context *) ctx->ctx_priv;
  5043. if (!ctx_isp->hw_acquired) {
  5044. CAM_ERR(CAM_ISP, "HW is not acquired, reject packet");
  5045. rc = -EINVAL;
  5046. goto end;
  5047. }
  5048. rc = __cam_isp_ctx_config_dev_in_top_state(ctx, cmd);
  5049. if (rc)
  5050. goto end;
  5051. if (!ctx_isp->init_received) {
  5052. CAM_WARN(CAM_ISP,
  5053. "Received update packet in flushed state, skip start");
  5054. goto end;
  5055. }
  5056. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5057. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5058. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  5059. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5060. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5061. &hw_cmd_args);
  5062. if (rc) {
  5063. CAM_ERR(CAM_ISP, "Failed to resume HW rc: %d", rc);
  5064. goto end;
  5065. }
  5066. start_cmd.dev_handle = cmd->dev_handle;
  5067. start_cmd.session_handle = cmd->session_handle;
  5068. rc = __cam_isp_ctx_start_dev_in_ready(ctx, &start_cmd);
  5069. if (rc)
  5070. CAM_ERR(CAM_ISP,
  5071. "Failed to re-start HW after flush rc: %d", rc);
  5072. else
  5073. CAM_INFO(CAM_ISP,
  5074. "Received init after flush. Re-start HW complete in ctx:%d",
  5075. ctx->ctx_id);
  5076. end:
  5077. CAM_DBG(CAM_ISP, "next state %d sub_state:%d", ctx->state,
  5078. ctx_isp->substate_activated);
  5079. return rc;
  5080. }
  5081. static int __cam_isp_ctx_link_in_acquired(struct cam_context *ctx,
  5082. struct cam_req_mgr_core_dev_link_setup *link)
  5083. {
  5084. int rc = 0;
  5085. struct cam_isp_context *ctx_isp =
  5086. (struct cam_isp_context *) ctx->ctx_priv;
  5087. CAM_DBG(CAM_ISP, "Enter.........");
  5088. ctx->link_hdl = link->link_hdl;
  5089. ctx->ctx_crm_intf = link->crm_cb;
  5090. ctx_isp->subscribe_event =
  5091. CAM_TRIGGER_POINT_SOF | CAM_TRIGGER_POINT_EOF;
  5092. ctx_isp->trigger_id = link->trigger_id;
  5093. /* change state only if we had the init config */
  5094. if (ctx_isp->init_received) {
  5095. ctx->state = CAM_CTX_READY;
  5096. trace_cam_context_state("ISP", ctx);
  5097. }
  5098. CAM_DBG(CAM_ISP, "next state %d", ctx->state);
  5099. return rc;
  5100. }
  5101. static int __cam_isp_ctx_unlink_in_acquired(struct cam_context *ctx,
  5102. struct cam_req_mgr_core_dev_link_setup *unlink)
  5103. {
  5104. int rc = 0;
  5105. struct cam_isp_context *ctx_isp =
  5106. (struct cam_isp_context *) ctx->ctx_priv;
  5107. ctx->link_hdl = -1;
  5108. ctx->ctx_crm_intf = NULL;
  5109. ctx_isp->trigger_id = -1;
  5110. return rc;
  5111. }
  5112. static int __cam_isp_ctx_get_isp_info(int32_t dev_hdl, void *data)
  5113. {
  5114. int rc = 0;
  5115. struct cam_context *ctx;
  5116. struct cam_req_mgr_dev_info *isp_dev = data;
  5117. struct cam_isp_context *isp_ctx = NULL;
  5118. ctx = (struct cam_context *)cam_get_device_priv(dev_hdl);
  5119. isp_ctx = (struct cam_isp_context *)ctx->ctx_priv;
  5120. isp_dev->state = isp_ctx->substate_activated;
  5121. isp_dev->timestamp = isp_ctx->sof_timestamp_val;
  5122. isp_dev->boot_time = isp_ctx->boot_timestamp;
  5123. isp_dev->frame_id = isp_ctx->frame_id;
  5124. if ((isp_ctx->substate_activated ==
  5125. CAM_ISP_CTX_ACTIVATED_APPLIED) ||
  5126. (isp_ctx->substate_activated ==
  5127. CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED))
  5128. isp_dev->is_applied = true;
  5129. else
  5130. isp_dev->is_applied = false;
  5131. return rc;
  5132. }
  5133. static int __cam_isp_ctx_get_dev_info_in_acquired(struct cam_context *ctx,
  5134. struct cam_req_mgr_device_info *dev_info)
  5135. {
  5136. int rc = 0;
  5137. dev_info->dev_hdl = ctx->dev_hdl;
  5138. strlcpy(dev_info->name, CAM_ISP_DEV_NAME, sizeof(dev_info->name));
  5139. dev_info->dev_id = CAM_REQ_MGR_DEVICE_IFE;
  5140. dev_info->p_delay = 1;
  5141. dev_info->trigger = CAM_TRIGGER_POINT_SOF;
  5142. dev_info->trigger_on = true;
  5143. dev_info->sof_ts_cb = &__cam_isp_ctx_get_isp_info;
  5144. return rc;
  5145. }
  5146. static int __cam_isp_ctx_start_dev_in_ready(struct cam_context *ctx,
  5147. struct cam_start_stop_dev_cmd *cmd)
  5148. {
  5149. int rc = 0;
  5150. int i;
  5151. struct cam_isp_start_args start_isp;
  5152. struct cam_ctx_request *req;
  5153. struct cam_isp_ctx_req *req_isp;
  5154. struct cam_isp_context *ctx_isp =
  5155. (struct cam_isp_context *) ctx->ctx_priv;
  5156. if (cmd->session_handle != ctx->session_hdl ||
  5157. cmd->dev_handle != ctx->dev_hdl) {
  5158. rc = -EPERM;
  5159. goto end;
  5160. }
  5161. if (list_empty(&ctx->pending_req_list)) {
  5162. /* should never happen */
  5163. CAM_ERR(CAM_ISP, "Start device with empty configuration");
  5164. rc = -EFAULT;
  5165. goto end;
  5166. } else {
  5167. req = list_first_entry(&ctx->pending_req_list,
  5168. struct cam_ctx_request, list);
  5169. }
  5170. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5171. if (!ctx_isp->hw_ctx) {
  5172. CAM_ERR(CAM_ISP, "Wrong hw context pointer.");
  5173. rc = -EFAULT;
  5174. goto end;
  5175. }
  5176. start_isp.hw_config.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5177. start_isp.hw_config.request_id = req->request_id;
  5178. start_isp.hw_config.hw_update_entries = req_isp->cfg;
  5179. start_isp.hw_config.num_hw_update_entries = req_isp->num_cfg;
  5180. start_isp.hw_config.priv = &req_isp->hw_update_data;
  5181. start_isp.hw_config.init_packet = 1;
  5182. start_isp.hw_config.reapply = 0;
  5183. start_isp.hw_config.cdm_reset_before_apply = false;
  5184. ctx_isp->last_applied_req_id = req->request_id;
  5185. if (ctx->state == CAM_CTX_FLUSHED)
  5186. start_isp.start_only = true;
  5187. else
  5188. start_isp.start_only = false;
  5189. atomic_set(&ctx_isp->process_bubble, 0);
  5190. atomic_set(&ctx_isp->rxd_epoch, 0);
  5191. ctx_isp->frame_id = 0;
  5192. ctx_isp->sof_timestamp_val = 0;
  5193. ctx_isp->boot_timestamp = 0;
  5194. ctx_isp->active_req_cnt = 0;
  5195. ctx_isp->reported_req_id = 0;
  5196. ctx_isp->bubble_frame_cnt = 0;
  5197. ctx_isp->substate_activated = ctx_isp->rdi_only_context ?
  5198. CAM_ISP_CTX_ACTIVATED_APPLIED :
  5199. (req_isp->num_fence_map_out) ? CAM_ISP_CTX_ACTIVATED_EPOCH :
  5200. CAM_ISP_CTX_ACTIVATED_SOF;
  5201. atomic64_set(&ctx_isp->state_monitor_head, -1);
  5202. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  5203. atomic64_set(&ctx_isp->event_record_head[i], -1);
  5204. /*
  5205. * In case of CSID TPG we might receive SOF and RUP IRQs
  5206. * before hw_mgr_intf->hw_start has returned. So move
  5207. * req out of pending list before hw_start and add it
  5208. * back to pending list if hw_start fails.
  5209. */
  5210. list_del_init(&req->list);
  5211. if (ctx_isp->offline_context && !req_isp->num_fence_map_out) {
  5212. list_add_tail(&req->list, &ctx->free_req_list);
  5213. atomic_set(&ctx_isp->rxd_epoch, 1);
  5214. CAM_DBG(CAM_REQ,
  5215. "Move pending req: %lld to free list(cnt: %d) offline ctx %u",
  5216. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  5217. } else if (ctx_isp->rdi_only_context || !req_isp->num_fence_map_out) {
  5218. list_add_tail(&req->list, &ctx->wait_req_list);
  5219. CAM_DBG(CAM_REQ,
  5220. "Move pending req: %lld to wait list(cnt: %d) ctx %u",
  5221. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  5222. } else {
  5223. list_add_tail(&req->list, &ctx->active_req_list);
  5224. ctx_isp->active_req_cnt++;
  5225. CAM_DBG(CAM_REQ,
  5226. "Move pending req: %lld to active list(cnt: %d) ctx %u offline %d",
  5227. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id,
  5228. ctx_isp->offline_context);
  5229. }
  5230. /*
  5231. * Only place to change state before calling the hw due to
  5232. * hardware tasklet has higher priority that can cause the
  5233. * irq handling comes early
  5234. */
  5235. ctx->state = CAM_CTX_ACTIVATED;
  5236. trace_cam_context_state("ISP", ctx);
  5237. rc = ctx->hw_mgr_intf->hw_start(ctx->hw_mgr_intf->hw_mgr_priv,
  5238. &start_isp);
  5239. if (rc) {
  5240. /* HW failure. user need to clean up the resource */
  5241. CAM_ERR(CAM_ISP, "Start HW failed");
  5242. ctx->state = CAM_CTX_READY;
  5243. if ((rc == -ETIMEDOUT) &&
  5244. (isp_ctx_debug.enable_cdm_cmd_buff_dump))
  5245. rc = cam_isp_ctx_dump_req(req_isp, 0, 0, NULL, false);
  5246. trace_cam_context_state("ISP", ctx);
  5247. list_del_init(&req->list);
  5248. list_add(&req->list, &ctx->pending_req_list);
  5249. goto end;
  5250. }
  5251. CAM_DBG(CAM_ISP, "start device success ctx %u", ctx->ctx_id);
  5252. end:
  5253. return rc;
  5254. }
  5255. static int __cam_isp_ctx_unlink_in_ready(struct cam_context *ctx,
  5256. struct cam_req_mgr_core_dev_link_setup *unlink)
  5257. {
  5258. int rc = 0;
  5259. ctx->link_hdl = -1;
  5260. ctx->ctx_crm_intf = NULL;
  5261. ctx->state = CAM_CTX_ACQUIRED;
  5262. trace_cam_context_state("ISP", ctx);
  5263. return rc;
  5264. }
  5265. static int __cam_isp_ctx_stop_dev_in_activated_unlock(
  5266. struct cam_context *ctx, struct cam_start_stop_dev_cmd *stop_cmd)
  5267. {
  5268. int rc = 0;
  5269. uint32_t i;
  5270. struct cam_hw_stop_args stop;
  5271. struct cam_ctx_request *req;
  5272. struct cam_isp_ctx_req *req_isp;
  5273. struct cam_isp_context *ctx_isp =
  5274. (struct cam_isp_context *) ctx->ctx_priv;
  5275. struct cam_isp_stop_args stop_isp;
  5276. /* Mask off all the incoming hardware events */
  5277. spin_lock_bh(&ctx->lock);
  5278. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  5279. spin_unlock_bh(&ctx->lock);
  5280. /* stop hw first */
  5281. if (ctx_isp->hw_ctx) {
  5282. stop.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5283. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  5284. stop_isp.stop_only = false;
  5285. stop.args = (void *) &stop_isp;
  5286. ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  5287. &stop);
  5288. }
  5289. CAM_DBG(CAM_ISP, "next Substate[%s]",
  5290. __cam_isp_ctx_substate_val_to_type(
  5291. ctx_isp->substate_activated));
  5292. if (ctx->ctx_crm_intf &&
  5293. ctx->ctx_crm_intf->notify_stop) {
  5294. struct cam_req_mgr_notify_stop notify;
  5295. notify.link_hdl = ctx->link_hdl;
  5296. CAM_DBG(CAM_ISP,
  5297. "Notify CRM about device stop ctx %u link 0x%x",
  5298. ctx->ctx_id, ctx->link_hdl);
  5299. ctx->ctx_crm_intf->notify_stop(&notify);
  5300. } else
  5301. CAM_ERR(CAM_ISP, "cb not present");
  5302. while (!list_empty(&ctx->pending_req_list)) {
  5303. req = list_first_entry(&ctx->pending_req_list,
  5304. struct cam_ctx_request, list);
  5305. list_del_init(&req->list);
  5306. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5307. CAM_DBG(CAM_ISP, "signal fence in pending list. fence num %d",
  5308. req_isp->num_fence_map_out);
  5309. for (i = 0; i < req_isp->num_fence_map_out; i++)
  5310. if (req_isp->fence_map_out[i].sync_id != -1) {
  5311. cam_sync_signal(
  5312. req_isp->fence_map_out[i].sync_id,
  5313. CAM_SYNC_STATE_SIGNALED_CANCEL,
  5314. CAM_SYNC_ISP_EVENT_HW_STOP);
  5315. }
  5316. list_add_tail(&req->list, &ctx->free_req_list);
  5317. }
  5318. while (!list_empty(&ctx->wait_req_list)) {
  5319. req = list_first_entry(&ctx->wait_req_list,
  5320. struct cam_ctx_request, list);
  5321. list_del_init(&req->list);
  5322. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5323. CAM_DBG(CAM_ISP, "signal fence in wait list. fence num %d",
  5324. req_isp->num_fence_map_out);
  5325. for (i = 0; i < req_isp->num_fence_map_out; i++)
  5326. if (req_isp->fence_map_out[i].sync_id != -1) {
  5327. cam_sync_signal(
  5328. req_isp->fence_map_out[i].sync_id,
  5329. CAM_SYNC_STATE_SIGNALED_CANCEL,
  5330. CAM_SYNC_ISP_EVENT_HW_STOP);
  5331. }
  5332. list_add_tail(&req->list, &ctx->free_req_list);
  5333. }
  5334. while (!list_empty(&ctx->active_req_list)) {
  5335. req = list_first_entry(&ctx->active_req_list,
  5336. struct cam_ctx_request, list);
  5337. list_del_init(&req->list);
  5338. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5339. CAM_DBG(CAM_ISP, "signal fence in active list. fence num %d",
  5340. req_isp->num_fence_map_out);
  5341. for (i = 0; i < req_isp->num_fence_map_out; i++)
  5342. if (req_isp->fence_map_out[i].sync_id != -1) {
  5343. cam_sync_signal(
  5344. req_isp->fence_map_out[i].sync_id,
  5345. CAM_SYNC_STATE_SIGNALED_CANCEL,
  5346. CAM_SYNC_ISP_EVENT_HW_STOP);
  5347. }
  5348. list_add_tail(&req->list, &ctx->free_req_list);
  5349. }
  5350. ctx_isp->frame_id = 0;
  5351. ctx_isp->active_req_cnt = 0;
  5352. ctx_isp->reported_req_id = 0;
  5353. ctx_isp->last_applied_req_id = 0;
  5354. ctx_isp->req_info.last_bufdone_req_id = 0;
  5355. ctx_isp->bubble_frame_cnt = 0;
  5356. atomic_set(&ctx_isp->process_bubble, 0);
  5357. atomic_set(&ctx_isp->rxd_epoch, 0);
  5358. atomic64_set(&ctx_isp->state_monitor_head, -1);
  5359. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  5360. atomic64_set(&ctx_isp->event_record_head[i], -1);
  5361. CAM_DBG(CAM_ISP, "Stop device success next state %d on ctx %u",
  5362. ctx->state, ctx->ctx_id);
  5363. if (!stop_cmd) {
  5364. rc = __cam_isp_ctx_unlink_in_ready(ctx, NULL);
  5365. if (rc)
  5366. CAM_ERR(CAM_ISP, "Unlink failed rc=%d", rc);
  5367. }
  5368. return rc;
  5369. }
  5370. static int __cam_isp_ctx_stop_dev_in_activated(struct cam_context *ctx,
  5371. struct cam_start_stop_dev_cmd *cmd)
  5372. {
  5373. int rc = 0;
  5374. struct cam_isp_context *ctx_isp =
  5375. (struct cam_isp_context *)ctx->ctx_priv;
  5376. __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, cmd);
  5377. ctx_isp->init_received = false;
  5378. ctx->state = CAM_CTX_ACQUIRED;
  5379. trace_cam_context_state("ISP", ctx);
  5380. return rc;
  5381. }
  5382. static int __cam_isp_ctx_release_dev_in_activated(struct cam_context *ctx,
  5383. struct cam_release_dev_cmd *cmd)
  5384. {
  5385. int rc = 0;
  5386. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  5387. if (rc)
  5388. CAM_ERR(CAM_ISP, "Stop device failed rc=%d", rc);
  5389. rc = __cam_isp_ctx_release_dev_in_top_state(ctx, cmd);
  5390. if (rc)
  5391. CAM_ERR(CAM_ISP, "Release device failed rc=%d", rc);
  5392. return rc;
  5393. }
  5394. static int __cam_isp_ctx_release_hw_in_activated(struct cam_context *ctx,
  5395. void *cmd)
  5396. {
  5397. int rc = 0;
  5398. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  5399. if (rc)
  5400. CAM_ERR(CAM_ISP, "Stop device failed rc=%d", rc);
  5401. rc = __cam_isp_ctx_release_hw_in_top_state(ctx, cmd);
  5402. if (rc)
  5403. CAM_ERR(CAM_ISP, "Release hw failed rc=%d", rc);
  5404. return rc;
  5405. }
  5406. static int __cam_isp_ctx_link_pause(struct cam_context *ctx)
  5407. {
  5408. int rc = 0;
  5409. struct cam_hw_cmd_args hw_cmd_args;
  5410. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5411. struct cam_isp_context *ctx_isp =
  5412. (struct cam_isp_context *) ctx->ctx_priv;
  5413. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5414. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5415. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_PAUSE_HW;
  5416. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5417. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5418. &hw_cmd_args);
  5419. return rc;
  5420. }
  5421. static int __cam_isp_ctx_link_resume(struct cam_context *ctx)
  5422. {
  5423. int rc = 0;
  5424. struct cam_hw_cmd_args hw_cmd_args;
  5425. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5426. struct cam_isp_context *ctx_isp =
  5427. (struct cam_isp_context *) ctx->ctx_priv;
  5428. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5429. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5430. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  5431. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5432. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5433. &hw_cmd_args);
  5434. return rc;
  5435. }
  5436. static int __cam_isp_ctx_handle_sof_freeze_evt(
  5437. struct cam_context *ctx)
  5438. {
  5439. int rc = 0;
  5440. struct cam_hw_cmd_args hw_cmd_args;
  5441. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5442. struct cam_isp_context *ctx_isp =
  5443. (struct cam_isp_context *) ctx->ctx_priv;
  5444. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5445. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5446. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_SOF_DEBUG;
  5447. isp_hw_cmd_args.u.sof_irq_enable = 1;
  5448. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5449. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5450. &hw_cmd_args);
  5451. return rc;
  5452. }
  5453. static int __cam_isp_ctx_process_evt(struct cam_context *ctx,
  5454. struct cam_req_mgr_link_evt_data *link_evt_data)
  5455. {
  5456. int rc = 0;
  5457. switch (link_evt_data->evt_type) {
  5458. case CAM_REQ_MGR_LINK_EVT_ERR:
  5459. /* No need to handle this message now */
  5460. break;
  5461. case CAM_REQ_MGR_LINK_EVT_PAUSE:
  5462. __cam_isp_ctx_link_pause(ctx);
  5463. break;
  5464. case CAM_REQ_MGR_LINK_EVT_RESUME:
  5465. __cam_isp_ctx_link_resume(ctx);
  5466. break;
  5467. case CAM_REQ_MGR_LINK_EVT_SOF_FREEZE:
  5468. __cam_isp_ctx_handle_sof_freeze_evt(ctx);
  5469. break;
  5470. default:
  5471. CAM_WARN(CAM_ISP, "Unknown event from CRM");
  5472. break;
  5473. }
  5474. return rc;
  5475. }
  5476. static int __cam_isp_ctx_unlink_in_activated(struct cam_context *ctx,
  5477. struct cam_req_mgr_core_dev_link_setup *unlink)
  5478. {
  5479. int rc = 0;
  5480. CAM_WARN(CAM_ISP,
  5481. "Received unlink in activated state. It's unexpected");
  5482. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  5483. if (rc)
  5484. CAM_WARN(CAM_ISP, "Stop device failed rc=%d", rc);
  5485. rc = __cam_isp_ctx_unlink_in_ready(ctx, unlink);
  5486. if (rc)
  5487. CAM_ERR(CAM_ISP, "Unlink failed rc=%d", rc);
  5488. return rc;
  5489. }
  5490. static int __cam_isp_ctx_signal_buf_done_req(struct cam_context *ctx,
  5491. struct cam_req_mgr_signal_info *signal_buf_done)
  5492. {
  5493. int rc = 0;
  5494. struct cam_ctx_ops *ctx_ops = NULL;
  5495. struct cam_isp_context *ctx_isp =
  5496. (struct cam_isp_context *) ctx->ctx_priv;
  5497. CAM_DBG(CAM_ISP, "Enter: signal buf done ctx id %d link 0x%x",
  5498. ctx->ctx_id, ctx->link_hdl);
  5499. ctx_ops = &ctx_isp->substate_machine[ctx_isp->substate_activated];
  5500. if (ctx_ops->crm_ops.signal_buf_done) {
  5501. rc = ctx_ops->crm_ops.signal_buf_done(ctx, signal_buf_done);
  5502. } else {
  5503. CAM_WARN_RATE_LIMIT(CAM_ISP,
  5504. "No handle function in activated Substate[%s]",
  5505. __cam_isp_ctx_substate_val_to_type(
  5506. ctx_isp->substate_activated));
  5507. rc = -EFAULT;
  5508. }
  5509. if (rc)
  5510. CAM_WARN_RATE_LIMIT(CAM_ISP,
  5511. "signal buf done failed");
  5512. return rc;
  5513. }
  5514. static int __cam_isp_ctx_change_state_req(struct cam_context *ctx,
  5515. struct cam_req_mgr_request_change_state *state_info)
  5516. {
  5517. int rc = 0;
  5518. struct cam_ctx_ops *ctx_ops = NULL;
  5519. struct cam_isp_context *ctx_isp =
  5520. (struct cam_isp_context *) ctx->ctx_priv;
  5521. CAM_DBG(CAM_ISP, "Enter: changes state ctx id %d link 0x%x",
  5522. ctx->ctx_id, ctx->link_hdl);
  5523. ctx_ops = &ctx_isp->substate_machine[ctx_isp->substate_activated];
  5524. if (ctx_ops->crm_ops.change_state) {
  5525. rc = ctx_ops->crm_ops.change_state(ctx, state_info);
  5526. } else {
  5527. CAM_WARN_RATE_LIMIT(CAM_ISP,
  5528. "No handle function in activated Substate[%s]",
  5529. __cam_isp_ctx_substate_val_to_type(
  5530. ctx_isp->substate_activated));
  5531. rc = -EFAULT;
  5532. }
  5533. if (rc)
  5534. CAM_WARN_RATE_LIMIT(CAM_ISP,
  5535. "changes state failed");
  5536. return rc;
  5537. }
  5538. static int __cam_isp_ctx_apply_req(struct cam_context *ctx,
  5539. struct cam_req_mgr_apply_request *apply)
  5540. {
  5541. int rc = 0;
  5542. struct cam_ctx_ops *ctx_ops = NULL;
  5543. struct cam_isp_context *ctx_isp =
  5544. (struct cam_isp_context *) ctx->ctx_priv;
  5545. trace_cam_apply_req("ISP", ctx->ctx_id, apply->request_id, apply->link_hdl);
  5546. CAM_DBG(CAM_ISP, "Enter: apply req in Substate[%s] request_id:%lld",
  5547. __cam_isp_ctx_substate_val_to_type(
  5548. ctx_isp->substate_activated), apply->request_id);
  5549. ctx_ops = &ctx_isp->substate_machine[ctx_isp->substate_activated];
  5550. if (ctx_ops->crm_ops.apply_req) {
  5551. rc = ctx_ops->crm_ops.apply_req(ctx, apply);
  5552. } else {
  5553. CAM_WARN_RATE_LIMIT(CAM_ISP,
  5554. "No handle function in activated Substate[%s]",
  5555. __cam_isp_ctx_substate_val_to_type(
  5556. ctx_isp->substate_activated));
  5557. rc = -EFAULT;
  5558. }
  5559. if (rc)
  5560. CAM_WARN_RATE_LIMIT(CAM_ISP,
  5561. "Apply failed in active Substate[%s] rc %d",
  5562. __cam_isp_ctx_substate_val_to_type(
  5563. ctx_isp->substate_activated), rc);
  5564. return rc;
  5565. }
  5566. static int __cam_isp_ctx_apply_default_settings(
  5567. struct cam_context *ctx,
  5568. struct cam_req_mgr_apply_request *apply)
  5569. {
  5570. int rc = 0;
  5571. struct cam_ctx_ops *ctx_ops = NULL;
  5572. struct cam_isp_context *ctx_isp =
  5573. (struct cam_isp_context *) ctx->ctx_priv;
  5574. if (!ctx_isp->use_default_apply)
  5575. return 0;
  5576. CAM_DBG(CAM_ISP,
  5577. "Enter: apply req in Substate %d request _id:%lld",
  5578. ctx_isp->substate_activated, apply->request_id);
  5579. ctx_ops = &ctx_isp->substate_machine[
  5580. ctx_isp->substate_activated];
  5581. if (ctx_ops->crm_ops.notify_frame_skip) {
  5582. rc = ctx_ops->crm_ops.notify_frame_skip(ctx, apply);
  5583. } else {
  5584. CAM_WARN_RATE_LIMIT(CAM_ISP,
  5585. "No handle function in activated substate %d",
  5586. ctx_isp->substate_activated);
  5587. rc = -EFAULT;
  5588. }
  5589. if (rc)
  5590. CAM_WARN_RATE_LIMIT(CAM_ISP,
  5591. "Apply default failed in active substate %d rc %d",
  5592. ctx_isp->substate_activated, rc);
  5593. return rc;
  5594. }
  5595. static int __cam_isp_ctx_handle_irq_in_activated(void *context,
  5596. uint32_t evt_id, void *evt_data)
  5597. {
  5598. int rc = 0;
  5599. struct cam_isp_ctx_irq_ops *irq_ops = NULL;
  5600. struct cam_context *ctx = (struct cam_context *)context;
  5601. struct cam_isp_context *ctx_isp =
  5602. (struct cam_isp_context *)ctx->ctx_priv;
  5603. spin_lock(&ctx->lock);
  5604. trace_cam_isp_activated_irq(ctx, ctx_isp->substate_activated, evt_id,
  5605. __cam_isp_ctx_get_event_ts(evt_id, evt_data));
  5606. CAM_DBG(CAM_ISP, "Enter: State %d, Substate[%s], evt id %d",
  5607. ctx->state, __cam_isp_ctx_substate_val_to_type(
  5608. ctx_isp->substate_activated), evt_id);
  5609. irq_ops = &ctx_isp->substate_machine_irq[ctx_isp->substate_activated];
  5610. if (irq_ops->irq_ops[evt_id]) {
  5611. rc = irq_ops->irq_ops[evt_id](ctx_isp, evt_data);
  5612. } else {
  5613. CAM_DBG(CAM_ISP, "No handle function for Substate[%s]",
  5614. __cam_isp_ctx_substate_val_to_type(
  5615. ctx_isp->substate_activated));
  5616. if (isp_ctx_debug.enable_state_monitor_dump)
  5617. __cam_isp_ctx_dump_state_monitor_array(ctx_isp);
  5618. }
  5619. CAM_DBG(CAM_ISP, "Exit: State %d Substate[%s]",
  5620. ctx->state, __cam_isp_ctx_substate_val_to_type(
  5621. ctx_isp->substate_activated));
  5622. spin_unlock(&ctx->lock);
  5623. return rc;
  5624. }
  5625. /* top state machine */
  5626. static struct cam_ctx_ops
  5627. cam_isp_ctx_top_state_machine[CAM_CTX_STATE_MAX] = {
  5628. /* Uninit */
  5629. {
  5630. .ioctl_ops = {},
  5631. .crm_ops = {},
  5632. .irq_ops = NULL,
  5633. },
  5634. /* Available */
  5635. {
  5636. .ioctl_ops = {
  5637. .acquire_dev = __cam_isp_ctx_acquire_dev_in_available,
  5638. },
  5639. .crm_ops = {},
  5640. .irq_ops = NULL,
  5641. },
  5642. /* Acquired */
  5643. {
  5644. .ioctl_ops = {
  5645. .acquire_hw = __cam_isp_ctx_acquire_hw_in_acquired,
  5646. .release_dev = __cam_isp_ctx_release_dev_in_top_state,
  5647. .config_dev = __cam_isp_ctx_config_dev_in_acquired,
  5648. .release_hw = __cam_isp_ctx_release_hw_in_top_state,
  5649. },
  5650. .crm_ops = {
  5651. .link = __cam_isp_ctx_link_in_acquired,
  5652. .unlink = __cam_isp_ctx_unlink_in_acquired,
  5653. .get_dev_info = __cam_isp_ctx_get_dev_info_in_acquired,
  5654. .flush_req = __cam_isp_ctx_flush_req_in_top_state,
  5655. .dump_req = __cam_isp_ctx_dump_in_top_state,
  5656. },
  5657. .irq_ops = NULL,
  5658. .pagefault_ops = cam_isp_context_dump_requests,
  5659. .dumpinfo_ops = cam_isp_context_info_dump,
  5660. },
  5661. /* Ready */
  5662. {
  5663. .ioctl_ops = {
  5664. .start_dev = __cam_isp_ctx_start_dev_in_ready,
  5665. .release_dev = __cam_isp_ctx_release_dev_in_top_state,
  5666. .config_dev = __cam_isp_ctx_config_dev_in_top_state,
  5667. .release_hw = __cam_isp_ctx_release_hw_in_top_state,
  5668. },
  5669. .crm_ops = {
  5670. .unlink = __cam_isp_ctx_unlink_in_ready,
  5671. .flush_req = __cam_isp_ctx_flush_req_in_ready,
  5672. .dump_req = __cam_isp_ctx_dump_in_top_state,
  5673. },
  5674. .irq_ops = NULL,
  5675. .pagefault_ops = cam_isp_context_dump_requests,
  5676. .dumpinfo_ops = cam_isp_context_info_dump,
  5677. },
  5678. /* Flushed */
  5679. {
  5680. .ioctl_ops = {
  5681. .stop_dev = __cam_isp_ctx_stop_dev_in_activated,
  5682. .release_dev = __cam_isp_ctx_release_dev_in_activated,
  5683. .config_dev = __cam_isp_ctx_config_dev_in_flushed,
  5684. .release_hw = __cam_isp_ctx_release_hw_in_activated,
  5685. },
  5686. .crm_ops = {
  5687. .unlink = __cam_isp_ctx_unlink_in_ready,
  5688. .process_evt = __cam_isp_ctx_process_evt,
  5689. },
  5690. .irq_ops = NULL,
  5691. .pagefault_ops = cam_isp_context_dump_requests,
  5692. .dumpinfo_ops = cam_isp_context_info_dump,
  5693. },
  5694. /* Activated */
  5695. {
  5696. .ioctl_ops = {
  5697. .stop_dev = __cam_isp_ctx_stop_dev_in_activated,
  5698. .release_dev = __cam_isp_ctx_release_dev_in_activated,
  5699. .config_dev = __cam_isp_ctx_config_dev_in_top_state,
  5700. .release_hw = __cam_isp_ctx_release_hw_in_activated,
  5701. },
  5702. .crm_ops = {
  5703. .unlink = __cam_isp_ctx_unlink_in_activated,
  5704. .apply_req = __cam_isp_ctx_apply_req,
  5705. .notify_frame_skip =
  5706. __cam_isp_ctx_apply_default_settings,
  5707. .flush_req = __cam_isp_ctx_flush_req_in_top_state,
  5708. .process_evt = __cam_isp_ctx_process_evt,
  5709. .dump_req = __cam_isp_ctx_dump_in_top_state,
  5710. .change_state = __cam_isp_ctx_change_state_req,
  5711. .signal_buf_done = __cam_isp_ctx_signal_buf_done_req,
  5712. },
  5713. .irq_ops = __cam_isp_ctx_handle_irq_in_activated,
  5714. .pagefault_ops = cam_isp_context_dump_requests,
  5715. .dumpinfo_ops = cam_isp_context_info_dump,
  5716. },
  5717. };
  5718. static int cam_isp_context_dump_requests(void *data,
  5719. struct cam_smmu_pf_info *pf_info)
  5720. {
  5721. struct cam_context *ctx = (struct cam_context *)data;
  5722. struct cam_ctx_request *req = NULL;
  5723. struct cam_ctx_request *req_temp = NULL;
  5724. struct cam_isp_ctx_req *req_isp = NULL;
  5725. struct cam_isp_prepare_hw_update_data *hw_update_data = NULL;
  5726. struct cam_hw_mgr_dump_pf_data *pf_dbg_entry = NULL;
  5727. struct cam_req_mgr_message req_msg = {0};
  5728. struct cam_isp_context *ctx_isp;
  5729. uint32_t resource_type = 0;
  5730. bool mem_found = false, ctx_found = false, send_error = false;
  5731. int rc = 0;
  5732. struct cam_isp_context *isp_ctx =
  5733. (struct cam_isp_context *)ctx->ctx_priv;
  5734. if (!isp_ctx) {
  5735. CAM_ERR(CAM_ISP, "Invalid isp ctx");
  5736. return -EINVAL;
  5737. }
  5738. CAM_INFO(CAM_ISP, "iommu fault handler for isp ctx %d state %d",
  5739. ctx->ctx_id, ctx->state);
  5740. list_for_each_entry_safe(req, req_temp,
  5741. &ctx->active_req_list, list) {
  5742. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5743. hw_update_data = &req_isp->hw_update_data;
  5744. pf_dbg_entry = &(req->pf_data);
  5745. CAM_INFO(CAM_ISP, "Active List: req_id : %lld ",
  5746. req->request_id);
  5747. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry,
  5748. &mem_found, &ctx_found, &resource_type, pf_info);
  5749. if (rc)
  5750. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  5751. if (ctx_found)
  5752. send_error = true;
  5753. }
  5754. CAM_INFO(CAM_ISP, "Iterating over wait_list of isp ctx %d state %d",
  5755. ctx->ctx_id, ctx->state);
  5756. list_for_each_entry_safe(req, req_temp,
  5757. &ctx->wait_req_list, list) {
  5758. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5759. hw_update_data = &req_isp->hw_update_data;
  5760. pf_dbg_entry = &(req->pf_data);
  5761. CAM_INFO(CAM_ISP, "Wait List: req_id : %lld ", req->request_id);
  5762. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry,
  5763. &mem_found, &ctx_found, &resource_type, pf_info);
  5764. if (rc)
  5765. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  5766. if (ctx_found)
  5767. send_error = true;
  5768. }
  5769. /*
  5770. * In certain scenarios we observe both overflow and SMMU pagefault
  5771. * for a particular request. If overflow is handled before page fault
  5772. * we need to traverse through pending request list because if
  5773. * bubble recovery is enabled on any request we move that request
  5774. * and all the subsequent requests to the pending list while handling
  5775. * overflow error.
  5776. */
  5777. CAM_INFO(CAM_ISP,
  5778. "Iterating over pending req list of isp ctx %d state %d",
  5779. ctx->ctx_id, ctx->state);
  5780. list_for_each_entry_safe(req, req_temp,
  5781. &ctx->pending_req_list, list) {
  5782. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5783. hw_update_data = &req_isp->hw_update_data;
  5784. pf_dbg_entry = &(req->pf_data);
  5785. CAM_INFO(CAM_ISP, "Pending List: req_id : %lld ",
  5786. req->request_id);
  5787. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry,
  5788. &mem_found, &ctx_found, &resource_type, pf_info);
  5789. if (rc)
  5790. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  5791. if (ctx_found)
  5792. send_error = true;
  5793. }
  5794. if (resource_type) {
  5795. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  5796. if (ctx_isp->isp_device_type == CAM_IFE_DEVICE_TYPE)
  5797. CAM_ERR(CAM_ISP,
  5798. "Page fault on resource id:%s (0x%x) ctx id:%d frame id:%d reported id:%lld applied id:%lld",
  5799. __cam_isp_resource_handle_id_to_type(
  5800. resource_type),
  5801. resource_type, ctx->ctx_id, ctx_isp->frame_id,
  5802. ctx_isp->reported_req_id,
  5803. ctx_isp->last_applied_req_id);
  5804. else
  5805. CAM_ERR(CAM_ISP,
  5806. "Page fault on resource id:%s (0x%x) ctx id:%d frame id:%d reported id:%lld applied id:%lld",
  5807. __cam_isp_tfe_resource_handle_id_to_type(
  5808. resource_type),
  5809. resource_type, ctx->ctx_id, ctx_isp->frame_id,
  5810. ctx_isp->reported_req_id,
  5811. ctx_isp->last_applied_req_id);
  5812. }
  5813. if (send_error) {
  5814. CAM_INFO(CAM_ISP,
  5815. "page fault notifying to umd ctx %u session_hdl:%d device_hdl:%d link_hdl:%d",
  5816. ctx->ctx_id, ctx->session_hdl,
  5817. ctx->dev_hdl, ctx->link_hdl);
  5818. req_msg.session_hdl = ctx->session_hdl;
  5819. req_msg.u.err_msg.device_hdl = ctx->dev_hdl;
  5820. req_msg.u.err_msg.error_type =
  5821. CAM_REQ_MGR_ERROR_TYPE_PAGE_FAULT;
  5822. req_msg.u.err_msg.link_hdl = ctx->link_hdl;
  5823. req_msg.u.err_msg.request_id = 0;
  5824. req_msg.u.err_msg.resource_size = 0x0;
  5825. req_msg.u.err_msg.error_code = CAM_REQ_MGR_ISP_UNREPORTED_ERROR;
  5826. if (cam_req_mgr_notify_message(&req_msg,
  5827. V4L_EVENT_CAM_REQ_MGR_ERROR,
  5828. V4L_EVENT_CAM_REQ_MGR_EVENT))
  5829. CAM_ERR(CAM_ISP,
  5830. "could not send page fault notification ctx %u session_hdl:%d device_hdl:%d link_hdl:%d",
  5831. ctx->ctx_id, ctx->session_hdl,
  5832. ctx->dev_hdl, ctx->link_hdl);
  5833. }
  5834. return rc;
  5835. }
  5836. static int cam_isp_context_debug_register(void)
  5837. {
  5838. int rc = 0;
  5839. struct dentry *dbgfileptr = NULL;
  5840. dbgfileptr = debugfs_create_dir("camera_isp_ctx", NULL);
  5841. if (!dbgfileptr) {
  5842. CAM_ERR(CAM_ISP, "DebugFS could not create directory!");
  5843. rc = -ENOENT;
  5844. goto end;
  5845. }
  5846. /* Store parent inode for cleanup in caller */
  5847. isp_ctx_debug.dentry = dbgfileptr;
  5848. debugfs_create_u32("enable_state_monitor_dump", 0644,
  5849. isp_ctx_debug.dentry, &isp_ctx_debug.enable_state_monitor_dump);
  5850. debugfs_create_u8("enable_cdm_cmd_buffer_dump", 0644,
  5851. isp_ctx_debug.dentry, &isp_ctx_debug.enable_cdm_cmd_buff_dump);
  5852. if (IS_ERR(dbgfileptr)) {
  5853. if (PTR_ERR(dbgfileptr) == -ENODEV)
  5854. CAM_WARN(CAM_ISP, "DebugFS not enabled in kernel!");
  5855. else
  5856. rc = PTR_ERR(dbgfileptr);
  5857. }
  5858. end:
  5859. return rc;
  5860. }
  5861. int cam_isp_context_init(struct cam_isp_context *ctx,
  5862. struct cam_context *ctx_base,
  5863. struct cam_req_mgr_kmd_ops *crm_node_intf,
  5864. struct cam_hw_mgr_intf *hw_intf,
  5865. uint32_t ctx_id,
  5866. uint32_t isp_device_type)
  5867. {
  5868. int rc = -1;
  5869. int i;
  5870. if (!ctx || !ctx_base) {
  5871. CAM_ERR(CAM_ISP, "Invalid Context");
  5872. goto err;
  5873. }
  5874. /* ISP context setup */
  5875. memset(ctx, 0, sizeof(*ctx));
  5876. ctx->base = ctx_base;
  5877. ctx->frame_id = 0;
  5878. ctx->sof_timestamp_val = 0;
  5879. ctx->boot_timestamp = 0;
  5880. ctx->custom_enabled = false;
  5881. ctx->use_frame_header_ts = false;
  5882. ctx->use_default_apply = false;
  5883. ctx->active_req_cnt = 0;
  5884. ctx->reported_req_id = 0;
  5885. ctx->bubble_frame_cnt = 0;
  5886. ctx->req_info.last_bufdone_req_id = 0;
  5887. ctx->hw_ctx = NULL;
  5888. ctx->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  5889. ctx->substate_machine = cam_isp_ctx_activated_state_machine;
  5890. ctx->substate_machine_irq = cam_isp_ctx_activated_state_machine_irq;
  5891. ctx->init_timestamp = jiffies_to_msecs(jiffies);
  5892. ctx->isp_device_type = isp_device_type;
  5893. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  5894. ctx->req_base[i].req_priv = &ctx->req_isp[i];
  5895. ctx->req_isp[i].base = &ctx->req_base[i];
  5896. }
  5897. /* camera context setup */
  5898. rc = cam_context_init(ctx_base, isp_dev_name, CAM_ISP, ctx_id,
  5899. crm_node_intf, hw_intf, ctx->req_base, CAM_ISP_CTX_REQ_MAX);
  5900. if (rc) {
  5901. CAM_ERR(CAM_ISP, "Camera Context Base init failed");
  5902. goto err;
  5903. }
  5904. /* link camera context with isp context */
  5905. ctx_base->state_machine = cam_isp_ctx_top_state_machine;
  5906. ctx_base->ctx_priv = ctx;
  5907. /* initializing current state for error logging */
  5908. for (i = 0; i < CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES; i++) {
  5909. ctx->cam_isp_ctx_state_monitor[i].curr_state =
  5910. CAM_ISP_CTX_ACTIVATED_MAX;
  5911. }
  5912. atomic64_set(&ctx->state_monitor_head, -1);
  5913. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  5914. atomic64_set(&ctx->event_record_head[i], -1);
  5915. if (!isp_ctx_debug.dentry)
  5916. cam_isp_context_debug_register();
  5917. err:
  5918. return rc;
  5919. }
  5920. int cam_isp_context_deinit(struct cam_isp_context *ctx)
  5921. {
  5922. if (ctx->base)
  5923. cam_context_deinit(ctx->base);
  5924. if (ctx->substate_activated != CAM_ISP_CTX_ACTIVATED_SOF)
  5925. CAM_ERR(CAM_ISP, "ISP context Substate[%s] is invalid",
  5926. __cam_isp_ctx_substate_val_to_type(
  5927. ctx->substate_activated));
  5928. debugfs_remove_recursive(isp_ctx_debug.dentry);
  5929. isp_ctx_debug.dentry = NULL;
  5930. memset(ctx, 0, sizeof(*ctx));
  5931. return 0;
  5932. }