cam_isp_context.c 209 KB

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