cam_isp_context.c 161 KB

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