cam_isp_context.c 190 KB

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