cam_isp_context.c 175 KB

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