cam_isp_context.c 194 KB

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