cam_isp_context.c 118 KB

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