cam_isp_context.c 195 KB

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