cam_isp_context.c 125 KB

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