cam_isp_context.c 151 KB

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