cam_isp_context.c 191 KB

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