cam_isp_context.c 150 KB

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