cam_isp_context.c 169 KB

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