cam_isp_context.c 113 KB

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