cam_isp_context.c 158 KB

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