cam_isp_context.c 125 KB

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