cam_isp_context.c 150 KB

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