cam_isp_context.c 149 KB

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