cam_isp_context.c 155 KB

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