cam_isp_context.c 122 KB

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