cam_isp_context.c 150 KB

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