cam_isp_context.c 184 KB

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