cvp_hfi.c 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <asm/memory.h>
  6. #include <linux/coresight-stm.h>
  7. #include <linux/delay.h>
  8. #include <linux/devfreq.h>
  9. #include <linux/hash.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/io.h>
  12. #include <linux/iommu.h>
  13. #include <linux/iopoll.h>
  14. #include <linux/of.h>
  15. #include <linux/pm_qos.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <linux/slab.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/soc/qcom/llcc-qcom.h>
  21. #include <linux/qcom_scm.h>
  22. #include <linux/soc/qcom/smem.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/reset.h>
  25. #include "hfi_packetization.h"
  26. #include "msm_cvp_debug.h"
  27. #include "cvp_core_hfi.h"
  28. #include "cvp_hfi_helper.h"
  29. #include "cvp_hfi_io.h"
  30. #include "msm_cvp_dsp.h"
  31. #include "msm_cvp_clocks.h"
  32. #define FIRMWARE_SIZE 0X00A00000
  33. #define REG_ADDR_OFFSET_BITMASK 0x000FFFFF
  34. #define QDSS_IOVA_START 0x80001000
  35. #define MIN_PAYLOAD_SIZE 3
  36. struct cvp_tzbsp_memprot {
  37. u32 cp_start;
  38. u32 cp_size;
  39. u32 cp_nonpixel_start;
  40. u32 cp_nonpixel_size;
  41. };
  42. #define TZBSP_PIL_SET_STATE 0xA
  43. #define TZBSP_CVP_PAS_ID 26
  44. /* Poll interval in uS */
  45. #define POLL_INTERVAL_US 50
  46. enum tzbsp_subsys_state {
  47. TZ_SUBSYS_STATE_SUSPEND = 0,
  48. TZ_SUBSYS_STATE_RESUME = 1,
  49. TZ_SUBSYS_STATE_RESTORE_THRESHOLD = 2,
  50. };
  51. const struct msm_cvp_gov_data CVP_DEFAULT_BUS_VOTE = {
  52. .data = NULL,
  53. .data_count = 0,
  54. };
  55. const int cvp_max_packets = 32;
  56. static void iris_hfi_pm_handler(struct work_struct *work);
  57. static DECLARE_DELAYED_WORK(iris_hfi_pm_work, iris_hfi_pm_handler);
  58. static inline int __resume(struct iris_hfi_device *device);
  59. static inline int __suspend(struct iris_hfi_device *device);
  60. static int __disable_regulator(struct iris_hfi_device *device,
  61. const char *name);
  62. static int __enable_regulator(struct iris_hfi_device *device,
  63. const char *name);
  64. static void __flush_debug_queue(struct iris_hfi_device *device, u8 *packet);
  65. static int __initialize_packetization(struct iris_hfi_device *device);
  66. static struct cvp_hal_session *__get_session(struct iris_hfi_device *device,
  67. u32 session_id);
  68. static bool __is_session_valid(struct iris_hfi_device *device,
  69. struct cvp_hal_session *session, const char *func);
  70. static int __iface_cmdq_write(struct iris_hfi_device *device,
  71. void *pkt);
  72. static int __load_fw(struct iris_hfi_device *device);
  73. static void __unload_fw(struct iris_hfi_device *device);
  74. static int __tzbsp_set_cvp_state(enum tzbsp_subsys_state state);
  75. static int __enable_subcaches(struct iris_hfi_device *device);
  76. static int __set_subcaches(struct iris_hfi_device *device);
  77. static int __release_subcaches(struct iris_hfi_device *device);
  78. static int __disable_subcaches(struct iris_hfi_device *device);
  79. static int __power_collapse(struct iris_hfi_device *device, bool force);
  80. static int iris_hfi_noc_error_info(void *dev);
  81. static void interrupt_init_iris2(struct iris_hfi_device *device);
  82. static void setup_dsp_uc_memmap_vpu5(struct iris_hfi_device *device);
  83. static void clock_config_on_enable_vpu5(struct iris_hfi_device *device);
  84. static int reset_ahb2axi_bridge(struct iris_hfi_device *device);
  85. static void power_off_iris2(struct iris_hfi_device *device);
  86. static int __set_ubwc_config(struct iris_hfi_device *device);
  87. static void __noc_error_info_iris2(struct iris_hfi_device *device);
  88. static int __enable_hw_power_collapse(struct iris_hfi_device *device);
  89. static int __power_off_controller(struct iris_hfi_device *device);
  90. static struct iris_hfi_vpu_ops iris2_ops = {
  91. .interrupt_init = interrupt_init_iris2,
  92. .setup_dsp_uc_memmap = setup_dsp_uc_memmap_vpu5,
  93. .clock_config_on_enable = clock_config_on_enable_vpu5,
  94. .reset_ahb2axi_bridge = reset_ahb2axi_bridge,
  95. .power_off = power_off_iris2,
  96. .noc_error_info = __noc_error_info_iris2,
  97. };
  98. /**
  99. * Utility function to enforce some of our assumptions. Spam calls to this
  100. * in hotspots in code to double check some of the assumptions that we hold.
  101. */
  102. static inline void __strict_check(struct iris_hfi_device *device)
  103. {
  104. msm_cvp_res_handle_fatal_hw_error(device->res,
  105. !mutex_is_locked(&device->lock));
  106. }
  107. static inline void __set_state(struct iris_hfi_device *device,
  108. enum iris_hfi_state state)
  109. {
  110. device->state = state;
  111. }
  112. static inline bool __core_in_valid_state(struct iris_hfi_device *device)
  113. {
  114. return device->state != IRIS_STATE_DEINIT;
  115. }
  116. static inline bool is_sys_cache_present(struct iris_hfi_device *device)
  117. {
  118. return device->res->sys_cache_present;
  119. }
  120. #define ROW_SIZE 32
  121. int get_hfi_version(void)
  122. {
  123. struct msm_cvp_core *core;
  124. struct iris_hfi_device *hfi;
  125. core = list_first_entry(&cvp_driver->cores, struct msm_cvp_core, list);
  126. hfi = (struct iris_hfi_device *)core->device->hfi_device_data;
  127. return hfi->version;
  128. }
  129. unsigned int get_msg_size(struct cvp_hfi_msg_session_hdr *hdr)
  130. {
  131. struct msm_cvp_core *core;
  132. struct iris_hfi_device *device;
  133. u32 minor_ver;
  134. core = list_first_entry(&cvp_driver->cores, struct msm_cvp_core, list);
  135. if (core)
  136. device = core->device->hfi_device_data;
  137. else
  138. return 0;
  139. if (!device) {
  140. dprintk(CVP_ERR, "%s: NULL device\n", __func__);
  141. return 0;
  142. }
  143. minor_ver = (device->version & HFI_VERSION_MINOR_MASK) >>
  144. HFI_VERSION_MINOR_SHIFT;
  145. if (minor_ver < 2)
  146. return sizeof(struct cvp_hfi_msg_session_hdr);
  147. if (hdr->packet_type == HFI_MSG_SESSION_CVP_FD)
  148. return sizeof(struct cvp_hfi_msg_session_hdr_ext);
  149. else
  150. return sizeof(struct cvp_hfi_msg_session_hdr);
  151. }
  152. unsigned int get_msg_session_id(void *msg)
  153. {
  154. struct cvp_hfi_msg_session_hdr *hdr =
  155. (struct cvp_hfi_msg_session_hdr *)msg;
  156. return hdr->session_id;
  157. }
  158. unsigned int get_msg_errorcode(void *msg)
  159. {
  160. struct cvp_hfi_msg_session_hdr *hdr =
  161. (struct cvp_hfi_msg_session_hdr *)msg;
  162. return hdr->error_type;
  163. }
  164. int get_msg_opconfigs(void *msg, unsigned int *session_id,
  165. unsigned int *error_type, unsigned int *config_id)
  166. {
  167. struct cvp_hfi_msg_session_op_cfg_packet *cfg =
  168. (struct cvp_hfi_msg_session_op_cfg_packet *)msg;
  169. *session_id = cfg->session_id;
  170. *error_type = cfg->error_type;
  171. *config_id = cfg->op_conf_id;
  172. return 0;
  173. }
  174. static void __dump_packet(u8 *packet, enum cvp_msg_prio log_level)
  175. {
  176. u32 c = 0, packet_size = *(u32 *)packet;
  177. /*
  178. * row must contain enough for 0xdeadbaad * 8 to be converted into
  179. * "de ad ba ab " * 8 + '\0'
  180. */
  181. char row[3 * ROW_SIZE];
  182. for (c = 0; c * ROW_SIZE < packet_size; ++c) {
  183. int bytes_to_read = ((c + 1) * ROW_SIZE > packet_size) ?
  184. packet_size % ROW_SIZE : ROW_SIZE;
  185. hex_dump_to_buffer(packet + c * ROW_SIZE, bytes_to_read,
  186. ROW_SIZE, 4, row, sizeof(row), false);
  187. dprintk(log_level, "%s\n", row);
  188. }
  189. }
  190. static int __dsp_suspend(struct iris_hfi_device *device, bool force, u32 flags)
  191. {
  192. int rc;
  193. struct cvp_hal_session *temp;
  194. if (msm_cvp_dsp_disable)
  195. return 0;
  196. list_for_each_entry(temp, &device->sess_head, list) {
  197. /* if forceful suspend, don't check session pause info */
  198. if (force)
  199. continue;
  200. /* don't suspend if cvp session is not paused */
  201. if (!(temp->flags & SESSION_PAUSE)) {
  202. dprintk(CVP_DSP,
  203. "%s: cvp session %x not paused\n",
  204. __func__, hash32_ptr(temp));
  205. return -EBUSY;
  206. }
  207. }
  208. dprintk(CVP_DSP, "%s: suspend dsp\n", __func__);
  209. rc = cvp_dsp_suspend(flags);
  210. if (rc) {
  211. dprintk(CVP_ERR, "%s: dsp suspend failed with error %d\n",
  212. __func__, rc);
  213. return -EINVAL;
  214. }
  215. dprintk(CVP_DSP, "%s: dsp suspended\n", __func__);
  216. return 0;
  217. }
  218. static int __dsp_resume(struct iris_hfi_device *device, u32 flags)
  219. {
  220. int rc;
  221. if (msm_cvp_dsp_disable)
  222. return 0;
  223. dprintk(CVP_DSP, "%s: resume dsp\n", __func__);
  224. rc = cvp_dsp_resume(flags);
  225. if (rc) {
  226. dprintk(CVP_ERR,
  227. "%s: dsp resume failed with error %d\n",
  228. __func__, rc);
  229. return rc;
  230. }
  231. dprintk(CVP_DSP, "%s: dsp resumed\n", __func__);
  232. return rc;
  233. }
  234. static int __dsp_shutdown(struct iris_hfi_device *device, u32 flags)
  235. {
  236. int rc;
  237. if (msm_cvp_dsp_disable)
  238. return 0;
  239. dprintk(CVP_DSP, "%s: shutdown dsp\n", __func__);
  240. rc = cvp_dsp_shutdown(flags);
  241. if (rc) {
  242. dprintk(CVP_ERR,
  243. "%s: dsp shutdown failed with error %d\n",
  244. __func__, rc);
  245. WARN_ON(1);
  246. }
  247. dprintk(CVP_DSP, "%s: dsp shutdown successful\n", __func__);
  248. return rc;
  249. }
  250. static int __acquire_regulator(struct regulator_info *rinfo,
  251. struct iris_hfi_device *device)
  252. {
  253. int rc = 0;
  254. if (rinfo->has_hw_power_collapse) {
  255. rc = regulator_set_mode(rinfo->regulator,
  256. REGULATOR_MODE_NORMAL);
  257. if (rc) {
  258. /*
  259. * This is somewhat fatal, but nothing we can do
  260. * about it. We can't disable the regulator w/o
  261. * getting it back under s/w control
  262. */
  263. dprintk(CVP_WARN,
  264. "Failed to acquire regulator control: %s\n",
  265. rinfo->name);
  266. } else {
  267. dprintk(CVP_PWR,
  268. "Acquire regulator control from HW: %s\n",
  269. rinfo->name);
  270. }
  271. }
  272. if (!regulator_is_enabled(rinfo->regulator)) {
  273. dprintk(CVP_WARN, "Regulator is not enabled %s\n",
  274. rinfo->name);
  275. msm_cvp_res_handle_fatal_hw_error(device->res, true);
  276. }
  277. return rc;
  278. }
  279. static int __hand_off_regulator(struct regulator_info *rinfo)
  280. {
  281. int rc = 0;
  282. if (rinfo->has_hw_power_collapse) {
  283. rc = regulator_set_mode(rinfo->regulator,
  284. REGULATOR_MODE_FAST);
  285. if (rc) {
  286. dprintk(CVP_WARN,
  287. "Failed to hand off regulator control: %s\n",
  288. rinfo->name);
  289. } else {
  290. dprintk(CVP_PWR,
  291. "Hand off regulator control to HW: %s\n",
  292. rinfo->name);
  293. }
  294. }
  295. return rc;
  296. }
  297. static int __hand_off_regulators(struct iris_hfi_device *device)
  298. {
  299. struct regulator_info *rinfo;
  300. int rc = 0, c = 0;
  301. iris_hfi_for_each_regulator(device, rinfo) {
  302. rc = __hand_off_regulator(rinfo);
  303. /*
  304. * If one regulator hand off failed, driver should take
  305. * the control for other regulators back.
  306. */
  307. if (rc)
  308. goto err_reg_handoff_failed;
  309. c++;
  310. }
  311. return rc;
  312. err_reg_handoff_failed:
  313. iris_hfi_for_each_regulator_reverse_continue(device, rinfo, c)
  314. __acquire_regulator(rinfo, device);
  315. return rc;
  316. }
  317. static int __write_queue(struct cvp_iface_q_info *qinfo, u8 *packet,
  318. bool *rx_req_is_set)
  319. {
  320. struct cvp_hfi_queue_header *queue;
  321. u32 packet_size_in_words, new_write_idx;
  322. u32 empty_space, read_idx, write_idx;
  323. u32 *write_ptr;
  324. if (!qinfo || !packet) {
  325. dprintk(CVP_ERR, "Invalid Params\n");
  326. return -EINVAL;
  327. } else if (!qinfo->q_array.align_virtual_addr) {
  328. dprintk(CVP_WARN, "Queues have already been freed\n");
  329. return -EINVAL;
  330. }
  331. queue = (struct cvp_hfi_queue_header *) qinfo->q_hdr;
  332. if (!queue) {
  333. dprintk(CVP_ERR, "queue not present\n");
  334. return -ENOENT;
  335. }
  336. if (msm_cvp_debug & CVP_PKT) {
  337. dprintk(CVP_PKT, "%s: %pK\n", __func__, qinfo);
  338. __dump_packet(packet, CVP_PKT);
  339. }
  340. packet_size_in_words = (*(u32 *)packet) >> 2;
  341. if (!packet_size_in_words || packet_size_in_words >
  342. qinfo->q_array.mem_size>>2) {
  343. dprintk(CVP_ERR, "Invalid packet size\n");
  344. return -ENODATA;
  345. }
  346. spin_lock(&qinfo->hfi_lock);
  347. read_idx = queue->qhdr_read_idx;
  348. write_idx = queue->qhdr_write_idx;
  349. empty_space = (write_idx >= read_idx) ?
  350. ((qinfo->q_array.mem_size>>2) - (write_idx - read_idx)) :
  351. (read_idx - write_idx);
  352. if (empty_space <= packet_size_in_words) {
  353. queue->qhdr_tx_req = 1;
  354. spin_unlock(&qinfo->hfi_lock);
  355. dprintk(CVP_ERR, "Insufficient size (%d) to write (%d)\n",
  356. empty_space, packet_size_in_words);
  357. return -ENOTEMPTY;
  358. }
  359. queue->qhdr_tx_req = 0;
  360. new_write_idx = write_idx + packet_size_in_words;
  361. write_ptr = (u32 *)((qinfo->q_array.align_virtual_addr) +
  362. (write_idx << 2));
  363. if (write_ptr < (u32 *)qinfo->q_array.align_virtual_addr ||
  364. write_ptr > (u32 *)(qinfo->q_array.align_virtual_addr +
  365. qinfo->q_array.mem_size)) {
  366. spin_unlock(&qinfo->hfi_lock);
  367. dprintk(CVP_ERR, "Invalid write index\n");
  368. return -ENODATA;
  369. }
  370. if (new_write_idx < (qinfo->q_array.mem_size >> 2)) {
  371. memcpy(write_ptr, packet, packet_size_in_words << 2);
  372. } else {
  373. new_write_idx -= qinfo->q_array.mem_size >> 2;
  374. memcpy(write_ptr, packet, (packet_size_in_words -
  375. new_write_idx) << 2);
  376. memcpy((void *)qinfo->q_array.align_virtual_addr,
  377. packet + ((packet_size_in_words - new_write_idx) << 2),
  378. new_write_idx << 2);
  379. }
  380. /*
  381. * Memory barrier to make sure packet is written before updating the
  382. * write index
  383. */
  384. mb();
  385. queue->qhdr_write_idx = new_write_idx;
  386. if (rx_req_is_set)
  387. *rx_req_is_set = queue->qhdr_rx_req == 1;
  388. /*
  389. * Memory barrier to make sure write index is updated before an
  390. * interrupt is raised.
  391. */
  392. mb();
  393. spin_unlock(&qinfo->hfi_lock);
  394. return 0;
  395. }
  396. static int __read_queue(struct cvp_iface_q_info *qinfo, u8 *packet,
  397. u32 *pb_tx_req_is_set)
  398. {
  399. struct cvp_hfi_queue_header *queue;
  400. u32 packet_size_in_words, new_read_idx;
  401. u32 *read_ptr;
  402. u32 receive_request = 0;
  403. u32 read_idx, write_idx;
  404. int rc = 0;
  405. if (!qinfo || !packet || !pb_tx_req_is_set) {
  406. dprintk(CVP_ERR, "Invalid Params\n");
  407. return -EINVAL;
  408. } else if (!qinfo->q_array.align_virtual_addr) {
  409. dprintk(CVP_WARN, "Queues have already been freed\n");
  410. return -EINVAL;
  411. }
  412. /*
  413. * Memory barrier to make sure data is valid before
  414. *reading it
  415. */
  416. mb();
  417. queue = (struct cvp_hfi_queue_header *) qinfo->q_hdr;
  418. if (!queue) {
  419. dprintk(CVP_ERR, "Queue memory is not allocated\n");
  420. return -ENOMEM;
  421. }
  422. /*
  423. * Do not set receive request for debug queue, if set,
  424. * Iris generates interrupt for debug messages even
  425. * when there is no response message available.
  426. * In general debug queue will not become full as it
  427. * is being emptied out for every interrupt from Iris.
  428. * Iris will anyway generates interrupt if it is full.
  429. */
  430. spin_lock(&qinfo->hfi_lock);
  431. if (queue->qhdr_type & HFI_Q_ID_CTRL_TO_HOST_MSG_Q)
  432. receive_request = 1;
  433. read_idx = queue->qhdr_read_idx;
  434. write_idx = queue->qhdr_write_idx;
  435. if (read_idx == write_idx) {
  436. queue->qhdr_rx_req = receive_request;
  437. /*
  438. * mb() to ensure qhdr is updated in main memory
  439. * so that iris reads the updated header values
  440. */
  441. mb();
  442. *pb_tx_req_is_set = 0;
  443. if (write_idx != queue->qhdr_write_idx) {
  444. queue->qhdr_rx_req = 0;
  445. } else {
  446. spin_unlock(&qinfo->hfi_lock);
  447. dprintk(CVP_HFI,
  448. "%s queue is empty, rx_req = %u, tx_req = %u, read_idx = %u\n",
  449. receive_request ? "message" : "debug",
  450. queue->qhdr_rx_req, queue->qhdr_tx_req,
  451. queue->qhdr_read_idx);
  452. return -ENODATA;
  453. }
  454. }
  455. read_ptr = (u32 *)((qinfo->q_array.align_virtual_addr) +
  456. (read_idx << 2));
  457. if (read_ptr < (u32 *)qinfo->q_array.align_virtual_addr ||
  458. read_ptr > (u32 *)(qinfo->q_array.align_virtual_addr +
  459. qinfo->q_array.mem_size - sizeof(*read_ptr))) {
  460. spin_unlock(&qinfo->hfi_lock);
  461. dprintk(CVP_ERR, "Invalid read index\n");
  462. return -ENODATA;
  463. }
  464. packet_size_in_words = (*read_ptr) >> 2;
  465. if (!packet_size_in_words) {
  466. spin_unlock(&qinfo->hfi_lock);
  467. dprintk(CVP_ERR, "Zero packet size\n");
  468. return -ENODATA;
  469. }
  470. new_read_idx = read_idx + packet_size_in_words;
  471. if (((packet_size_in_words << 2) <= CVP_IFACEQ_VAR_HUGE_PKT_SIZE)
  472. && read_idx <= (qinfo->q_array.mem_size >> 2)) {
  473. if (new_read_idx < (qinfo->q_array.mem_size >> 2)) {
  474. memcpy(packet, read_ptr,
  475. packet_size_in_words << 2);
  476. } else {
  477. new_read_idx -= (qinfo->q_array.mem_size >> 2);
  478. memcpy(packet, read_ptr,
  479. (packet_size_in_words - new_read_idx) << 2);
  480. memcpy(packet + ((packet_size_in_words -
  481. new_read_idx) << 2),
  482. (u8 *)qinfo->q_array.align_virtual_addr,
  483. new_read_idx << 2);
  484. }
  485. } else {
  486. dprintk(CVP_WARN,
  487. "BAD packet received, read_idx: %#x, pkt_size: %d\n",
  488. read_idx, packet_size_in_words << 2);
  489. dprintk(CVP_WARN, "Dropping this packet\n");
  490. new_read_idx = write_idx;
  491. rc = -ENODATA;
  492. }
  493. if (new_read_idx != queue->qhdr_write_idx)
  494. queue->qhdr_rx_req = 0;
  495. else
  496. queue->qhdr_rx_req = receive_request;
  497. queue->qhdr_read_idx = new_read_idx;
  498. /*
  499. * mb() to ensure qhdr is updated in main memory
  500. * so that iris reads the updated header values
  501. */
  502. mb();
  503. *pb_tx_req_is_set = (queue->qhdr_tx_req == 1) ? 1 : 0;
  504. spin_unlock(&qinfo->hfi_lock);
  505. if ((msm_cvp_debug & CVP_PKT) &&
  506. !(queue->qhdr_type & HFI_Q_ID_CTRL_TO_HOST_DEBUG_Q)) {
  507. dprintk(CVP_PKT, "%s: %pK\n", __func__, qinfo);
  508. __dump_packet(packet, CVP_PKT);
  509. }
  510. return rc;
  511. }
  512. static int __smem_alloc(struct iris_hfi_device *dev, struct cvp_mem_addr *mem,
  513. u32 size, u32 align, u32 flags)
  514. {
  515. struct msm_cvp_smem *alloc = &mem->mem_data;
  516. int rc = 0;
  517. if (!dev || !mem || !size) {
  518. dprintk(CVP_ERR, "Invalid Params\n");
  519. return -EINVAL;
  520. }
  521. dprintk(CVP_INFO, "start to alloc size: %d, flags: %d\n", size, flags);
  522. alloc->flags = flags;
  523. rc = msm_cvp_smem_alloc(size, align, 1, (void *)dev->res, alloc);
  524. if (rc) {
  525. dprintk(CVP_ERR, "Alloc failed\n");
  526. rc = -ENOMEM;
  527. goto fail_smem_alloc;
  528. }
  529. dprintk(CVP_MEM, "%s: ptr = %pK, size = %d\n", __func__,
  530. alloc->kvaddr, size);
  531. mem->mem_size = alloc->size;
  532. mem->align_virtual_addr = alloc->kvaddr;
  533. mem->align_device_addr = alloc->device_addr;
  534. return rc;
  535. fail_smem_alloc:
  536. return rc;
  537. }
  538. static void __smem_free(struct iris_hfi_device *dev, struct msm_cvp_smem *mem)
  539. {
  540. if (!dev || !mem) {
  541. dprintk(CVP_ERR, "invalid param %pK %pK\n", dev, mem);
  542. return;
  543. }
  544. msm_cvp_smem_free(mem);
  545. }
  546. static void __write_register(struct iris_hfi_device *device,
  547. u32 reg, u32 value)
  548. {
  549. u32 hwiosymaddr = reg;
  550. u8 *base_addr;
  551. if (!device) {
  552. dprintk(CVP_ERR, "Invalid params: %pK\n", device);
  553. return;
  554. }
  555. __strict_check(device);
  556. if (!device->power_enabled) {
  557. dprintk(CVP_WARN,
  558. "HFI Write register failed : Power is OFF\n");
  559. msm_cvp_res_handle_fatal_hw_error(device->res, true);
  560. return;
  561. }
  562. base_addr = device->cvp_hal_data->register_base;
  563. dprintk(CVP_REG, "Base addr: %pK, written to: %#x, Value: %#x...\n",
  564. base_addr, hwiosymaddr, value);
  565. base_addr += hwiosymaddr;
  566. writel_relaxed(value, base_addr);
  567. /*
  568. * Memory barrier to make sure value is written into the register.
  569. */
  570. wmb();
  571. }
  572. static int __read_gcc_register(struct iris_hfi_device *device, u32 reg)
  573. {
  574. int rc = 0;
  575. u8 *base_addr;
  576. if (!device) {
  577. dprintk(CVP_ERR, "Invalid params: %pK\n", device);
  578. return -EINVAL;
  579. }
  580. __strict_check(device);
  581. if (!device->power_enabled) {
  582. dprintk(CVP_WARN,
  583. "%s HFI Read register failed : Power is OFF\n",
  584. __func__);
  585. msm_cvp_res_handle_fatal_hw_error(device->res, true);
  586. return -EINVAL;
  587. }
  588. base_addr = device->cvp_hal_data->gcc_reg_base;
  589. rc = readl_relaxed(base_addr + reg);
  590. /*
  591. * Memory barrier to make sure value is read correctly from the
  592. * register.
  593. */
  594. rmb();
  595. dprintk(CVP_REG,
  596. "GCC Base addr: %pK, read from: %#x, value: %#x...\n",
  597. base_addr, reg, rc);
  598. return rc;
  599. }
  600. static int __read_register(struct iris_hfi_device *device, u32 reg)
  601. {
  602. int rc = 0;
  603. u8 *base_addr;
  604. if (!device) {
  605. dprintk(CVP_ERR, "Invalid params: %pK\n", device);
  606. return -EINVAL;
  607. }
  608. __strict_check(device);
  609. if (!device->power_enabled) {
  610. dprintk(CVP_WARN,
  611. "HFI Read register failed : Power is OFF\n");
  612. msm_cvp_res_handle_fatal_hw_error(device->res, true);
  613. return -EINVAL;
  614. }
  615. base_addr = device->cvp_hal_data->register_base;
  616. rc = readl_relaxed(base_addr + reg);
  617. /*
  618. * Memory barrier to make sure value is read correctly from the
  619. * register.
  620. */
  621. rmb();
  622. dprintk(CVP_REG, "Base addr: %pK, read from: %#x, value: %#x...\n",
  623. base_addr, reg, rc);
  624. return rc;
  625. }
  626. static void __set_registers(struct iris_hfi_device *device)
  627. {
  628. struct reg_set *reg_set;
  629. int i;
  630. if (!device->res) {
  631. dprintk(CVP_ERR,
  632. "device resources null, cannot set registers\n");
  633. return;
  634. }
  635. reg_set = &device->res->reg_set;
  636. for (i = 0; i < reg_set->count; i++) {
  637. __write_register(device, reg_set->reg_tbl[i].reg,
  638. reg_set->reg_tbl[i].value);
  639. dprintk(CVP_REG, "write_reg offset=%x, val=%x\n",
  640. reg_set->reg_tbl[i].reg,
  641. reg_set->reg_tbl[i].value);
  642. }
  643. }
  644. /*
  645. * The existence of this function is a hack for 8996 (or certain Iris versions)
  646. * to overcome a hardware bug. Whenever the GDSCs momentarily power collapse
  647. * (after calling __hand_off_regulators()), the values of the threshold
  648. * registers (typically programmed by TZ) are incorrectly reset. As a result
  649. * reprogram these registers at certain agreed upon points.
  650. */
  651. static void __set_threshold_registers(struct iris_hfi_device *device)
  652. {
  653. u32 version = __read_register(device, CVP_WRAPPER_HW_VERSION);
  654. version &= ~GENMASK(15, 0);
  655. if (version != (0x3 << 28 | 0x43 << 16))
  656. return;
  657. if (__tzbsp_set_cvp_state(TZ_SUBSYS_STATE_RESTORE_THRESHOLD))
  658. dprintk(CVP_ERR, "Failed to restore threshold values\n");
  659. }
  660. static int __unvote_buses(struct iris_hfi_device *device)
  661. {
  662. int rc = 0;
  663. struct bus_info *bus = NULL;
  664. kfree(device->bus_vote.data);
  665. device->bus_vote.data = NULL;
  666. device->bus_vote.data_count = 0;
  667. iris_hfi_for_each_bus(device, bus) {
  668. rc = icc_set_bw(bus->client, 0, 0);
  669. if (rc) {
  670. dprintk(CVP_ERR,
  671. "%s: Failed unvoting bus\n", __func__);
  672. goto err_unknown_device;
  673. }
  674. }
  675. err_unknown_device:
  676. return rc;
  677. }
  678. static int __vote_buses(struct iris_hfi_device *device,
  679. struct cvp_bus_vote_data *data, int num_data)
  680. {
  681. int rc = 0;
  682. struct bus_info *bus = NULL;
  683. struct cvp_bus_vote_data *new_data = NULL;
  684. if (!num_data) {
  685. dprintk(CVP_PWR, "No vote data available\n");
  686. goto no_data_count;
  687. } else if (!data) {
  688. dprintk(CVP_ERR, "Invalid voting data\n");
  689. return -EINVAL;
  690. }
  691. new_data = kmemdup(data, num_data * sizeof(*new_data), GFP_KERNEL);
  692. if (!new_data) {
  693. dprintk(CVP_ERR, "Can't alloc memory to cache bus votes\n");
  694. rc = -ENOMEM;
  695. goto err_no_mem;
  696. }
  697. no_data_count:
  698. kfree(device->bus_vote.data);
  699. device->bus_vote.data = new_data;
  700. device->bus_vote.data_count = num_data;
  701. iris_hfi_for_each_bus(device, bus) {
  702. if (bus) {
  703. rc = icc_set_bw(bus->client, bus->range[1], 0);
  704. if (rc)
  705. dprintk(CVP_ERR,
  706. "Failed voting bus %s to ab %u\n",
  707. bus->name, bus->range[1]*1000);
  708. }
  709. }
  710. err_no_mem:
  711. return rc;
  712. }
  713. static int iris_hfi_vote_buses(void *dev, struct cvp_bus_vote_data *d, int n)
  714. {
  715. int rc = 0;
  716. struct iris_hfi_device *device = dev;
  717. if (!device)
  718. return -EINVAL;
  719. mutex_lock(&device->lock);
  720. rc = __vote_buses(device, d, n);
  721. mutex_unlock(&device->lock);
  722. return rc;
  723. }
  724. static int __core_set_resource(struct iris_hfi_device *device,
  725. struct cvp_resource_hdr *resource_hdr, void *resource_value)
  726. {
  727. struct cvp_hfi_cmd_sys_set_resource_packet *pkt;
  728. u8 packet[CVP_IFACEQ_VAR_SMALL_PKT_SIZE];
  729. int rc = 0;
  730. if (!device || !resource_hdr || !resource_value) {
  731. dprintk(CVP_ERR, "set_res: Invalid Params\n");
  732. return -EINVAL;
  733. }
  734. pkt = (struct cvp_hfi_cmd_sys_set_resource_packet *) packet;
  735. rc = call_hfi_pkt_op(device, sys_set_resource,
  736. pkt, resource_hdr, resource_value);
  737. if (rc) {
  738. dprintk(CVP_ERR, "set_res: failed to create packet\n");
  739. goto err_create_pkt;
  740. }
  741. rc = __iface_cmdq_write(device, pkt);
  742. if (rc)
  743. rc = -ENOTEMPTY;
  744. err_create_pkt:
  745. return rc;
  746. }
  747. static int __core_release_resource(struct iris_hfi_device *device,
  748. struct cvp_resource_hdr *resource_hdr)
  749. {
  750. struct cvp_hfi_cmd_sys_release_resource_packet *pkt;
  751. u8 packet[CVP_IFACEQ_VAR_SMALL_PKT_SIZE];
  752. int rc = 0;
  753. if (!device || !resource_hdr) {
  754. dprintk(CVP_ERR, "release_res: Invalid Params\n");
  755. return -EINVAL;
  756. }
  757. pkt = (struct cvp_hfi_cmd_sys_release_resource_packet *) packet;
  758. rc = call_hfi_pkt_op(device, sys_release_resource,
  759. pkt, resource_hdr);
  760. if (rc) {
  761. dprintk(CVP_ERR, "release_res: failed to create packet\n");
  762. goto err_create_pkt;
  763. }
  764. rc = __iface_cmdq_write(device, pkt);
  765. if (rc)
  766. rc = -ENOTEMPTY;
  767. err_create_pkt:
  768. return rc;
  769. }
  770. static int __tzbsp_set_cvp_state(enum tzbsp_subsys_state state)
  771. {
  772. int rc = 0;
  773. rc = qcom_scm_set_remote_state(state, TZBSP_CVP_PAS_ID);
  774. dprintk(CVP_CORE, "Set state %d, resp %d\n", state, rc);
  775. if (rc) {
  776. dprintk(CVP_ERR, "Failed qcom_scm_set_remote_state %d\n", rc);
  777. return rc;
  778. }
  779. return 0;
  780. }
  781. static inline int __boot_firmware(struct iris_hfi_device *device)
  782. {
  783. int rc = 0, loop = 10;
  784. u32 ctrl_init_val = 0, ctrl_status = 0, count = 0, max_tries = 1000;
  785. u32 reg_gdsc;
  786. /*
  787. * Hand off control of regulators to h/w _after_ enabling clocks.
  788. * Note that the GDSC will turn off when switching from normal
  789. * (s/w triggered) to fast (HW triggered) unless the h/w vote is
  790. * present. Since Iris isn't up yet, the GDSC will be off briefly.
  791. */
  792. if (__enable_hw_power_collapse(device))
  793. dprintk(CVP_ERR, "Failed to enabled inter-frame PC\n");
  794. while (loop) {
  795. reg_gdsc = __read_register(device, CVP_CC_MVS1_GDSCR);
  796. if (reg_gdsc & 0x80000000) {
  797. usleep_range(100, 200);
  798. loop--;
  799. } else {
  800. break;
  801. }
  802. }
  803. if (!loop)
  804. dprintk(CVP_ERR, "fail to power off CORE during resume\n");
  805. ctrl_init_val = BIT(0);
  806. __write_register(device, CVP_CTRL_INIT, ctrl_init_val);
  807. while (!ctrl_status && count < max_tries) {
  808. ctrl_status = __read_register(device, CVP_CTRL_STATUS);
  809. if ((ctrl_status & CVP_CTRL_ERROR_STATUS__M) == 0x4) {
  810. dprintk(CVP_ERR, "invalid setting for UC_REGION\n");
  811. rc = -ENODATA;
  812. break;
  813. }
  814. /* Reduce to 1/100th and x100 of max_tries */
  815. usleep_range(500, 1000);
  816. count++;
  817. }
  818. if (!(ctrl_status & CVP_CTRL_INIT_STATUS__M)) {
  819. dprintk(CVP_ERR, "Failed to boot FW status: %x\n",
  820. ctrl_status);
  821. rc = -ENODEV;
  822. }
  823. /* Enable interrupt before sending commands to tensilica */
  824. __write_register(device, CVP_CPU_CS_H2XSOFTINTEN, 0x1);
  825. __write_register(device, CVP_CPU_CS_X2RPMh, 0x0);
  826. return rc;
  827. }
  828. static int iris_hfi_resume(void *dev)
  829. {
  830. int rc = 0;
  831. struct iris_hfi_device *device = (struct iris_hfi_device *) dev;
  832. if (!device) {
  833. dprintk(CVP_ERR, "%s invalid device\n", __func__);
  834. return -EINVAL;
  835. }
  836. dprintk(CVP_CORE, "Resuming Iris\n");
  837. mutex_lock(&device->lock);
  838. rc = __resume(device);
  839. mutex_unlock(&device->lock);
  840. return rc;
  841. }
  842. static int iris_hfi_suspend(void *dev)
  843. {
  844. int rc = 0;
  845. struct iris_hfi_device *device = (struct iris_hfi_device *) dev;
  846. if (!device) {
  847. dprintk(CVP_ERR, "%s invalid device\n", __func__);
  848. return -EINVAL;
  849. } else if (!device->res->sw_power_collapsible) {
  850. return -ENOTSUPP;
  851. }
  852. dprintk(CVP_CORE, "Suspending Iris\n");
  853. mutex_lock(&device->lock);
  854. rc = __power_collapse(device, true);
  855. if (rc) {
  856. dprintk(CVP_WARN, "%s: Iris is busy\n", __func__);
  857. rc = -EBUSY;
  858. }
  859. mutex_unlock(&device->lock);
  860. /* Cancel pending delayed works if any */
  861. if (!rc)
  862. cancel_delayed_work(&iris_hfi_pm_work);
  863. return rc;
  864. }
  865. static void cvp_dump_csr(struct iris_hfi_device *dev)
  866. {
  867. u32 reg;
  868. if (!dev)
  869. return;
  870. if (!dev->power_enabled || dev->reg_dumped)
  871. return;
  872. reg = __read_register(dev, CVP_WRAPPER_CPU_STATUS);
  873. dprintk(CVP_ERR, "CVP_WRAPPER_CPU_STATUS: %x\n", reg);
  874. reg = __read_register(dev, CVP_CPU_CS_SCIACMDARG0);
  875. dprintk(CVP_ERR, "CVP_CPU_CS_SCIACMDARG0: %x\n", reg);
  876. reg = __read_register(dev, CVP_WRAPPER_CPU_CLOCK_CONFIG);
  877. dprintk(CVP_ERR, "CVP_WRAPPER_CPU_CLOCK_CONFIG: %x\n", reg);
  878. reg = __read_register(dev, CVP_WRAPPER_CORE_CLOCK_CONFIG);
  879. dprintk(CVP_ERR, "CVP_WRAPPER_CORE_CLOCK_CONFIG: %x\n", reg);
  880. reg = __read_register(dev, CVP_WRAPPER_INTR_STATUS);
  881. dprintk(CVP_ERR, "CVP_WRAPPER_INTR_STATUS: %x\n", reg);
  882. reg = __read_register(dev, CVP_CPU_CS_H2ASOFTINT);
  883. dprintk(CVP_ERR, "CVP_CPU_CS_H2ASOFTINT: %x\n", reg);
  884. reg = __read_register(dev, CVP_CPU_CS_A2HSOFTINT);
  885. dprintk(CVP_ERR, "CVP_CPU_CS_A2HSOFTINT: %x\n", reg);
  886. reg = __read_register(dev, CVP_CC_MVS1C_GDSCR);
  887. dprintk(CVP_ERR, "CVP_CC_MVS1C_GDSCR: %x\n", reg);
  888. reg = __read_register(dev, CVP_CC_MVS1C_CBCR);
  889. dprintk(CVP_ERR, "CVP_CC_MVS1C_CBCR: %x\n", reg);
  890. dev->reg_dumped = true;
  891. }
  892. static int iris_hfi_flush_debug_queue(void *dev)
  893. {
  894. int rc = 0;
  895. struct iris_hfi_device *device = (struct iris_hfi_device *) dev;
  896. if (!device) {
  897. dprintk(CVP_ERR, "%s invalid device\n", __func__);
  898. return -EINVAL;
  899. }
  900. cvp_dump_csr(device);
  901. mutex_lock(&device->lock);
  902. if (!device->power_enabled) {
  903. dprintk(CVP_WARN, "%s: iris power off\n", __func__);
  904. rc = -EINVAL;
  905. goto exit;
  906. }
  907. __flush_debug_queue(device, NULL);
  908. exit:
  909. mutex_unlock(&device->lock);
  910. return rc;
  911. }
  912. static int iris_hfi_scale_clocks(void *dev, u32 freq)
  913. {
  914. int rc = 0;
  915. struct iris_hfi_device *device = dev;
  916. if (!device) {
  917. dprintk(CVP_ERR, "Invalid args: %pK\n", device);
  918. return -EINVAL;
  919. }
  920. mutex_lock(&device->lock);
  921. if (__resume(device)) {
  922. dprintk(CVP_ERR, "Resume from power collapse failed\n");
  923. rc = -ENODEV;
  924. goto exit;
  925. }
  926. rc = msm_cvp_set_clocks_impl(device, freq);
  927. exit:
  928. mutex_unlock(&device->lock);
  929. return rc;
  930. }
  931. /* Writes into cmdq without raising an interrupt */
  932. static int __iface_cmdq_write_relaxed(struct iris_hfi_device *device,
  933. void *pkt, bool *requires_interrupt)
  934. {
  935. struct cvp_iface_q_info *q_info;
  936. struct cvp_hal_cmd_pkt_hdr *cmd_packet;
  937. int result = -E2BIG;
  938. if (!device || !pkt) {
  939. dprintk(CVP_ERR, "Invalid Params\n");
  940. return -EINVAL;
  941. }
  942. __strict_check(device);
  943. if (!__core_in_valid_state(device)) {
  944. dprintk(CVP_ERR, "%s - fw not in init state\n", __func__);
  945. result = -EINVAL;
  946. goto err_q_null;
  947. }
  948. cmd_packet = (struct cvp_hal_cmd_pkt_hdr *)pkt;
  949. device->last_packet_type = cmd_packet->packet_type;
  950. q_info = &device->iface_queues[CVP_IFACEQ_CMDQ_IDX];
  951. if (!q_info) {
  952. dprintk(CVP_ERR, "cannot write to shared Q's\n");
  953. goto err_q_null;
  954. }
  955. if (!q_info->q_array.align_virtual_addr) {
  956. dprintk(CVP_ERR, "cannot write to shared CMD Q's\n");
  957. result = -ENODATA;
  958. goto err_q_null;
  959. }
  960. if (__resume(device)) {
  961. dprintk(CVP_ERR, "%s: Power on failed\n", __func__);
  962. goto err_q_write;
  963. }
  964. if (!__write_queue(q_info, (u8 *)pkt, requires_interrupt)) {
  965. if (device->res->sw_power_collapsible) {
  966. cancel_delayed_work(&iris_hfi_pm_work);
  967. if (!queue_delayed_work(device->iris_pm_workq,
  968. &iris_hfi_pm_work,
  969. msecs_to_jiffies(
  970. device->res->msm_cvp_pwr_collapse_delay))) {
  971. dprintk(CVP_PWR,
  972. "PM work already scheduled\n");
  973. }
  974. }
  975. result = 0;
  976. } else {
  977. dprintk(CVP_ERR, "__iface_cmdq_write: queue full\n");
  978. }
  979. err_q_write:
  980. err_q_null:
  981. return result;
  982. }
  983. static int __iface_cmdq_write(struct iris_hfi_device *device, void *pkt)
  984. {
  985. bool needs_interrupt = false;
  986. int rc = __iface_cmdq_write_relaxed(device, pkt, &needs_interrupt);
  987. if (!rc && needs_interrupt) {
  988. /* Consumer of cmdq prefers that we raise an interrupt */
  989. rc = 0;
  990. __write_register(device, CVP_CPU_CS_H2ASOFTINT, 1);
  991. }
  992. return rc;
  993. }
  994. static int __iface_msgq_read(struct iris_hfi_device *device, void *pkt)
  995. {
  996. u32 tx_req_is_set = 0;
  997. int rc = 0;
  998. struct cvp_iface_q_info *q_info;
  999. if (!pkt) {
  1000. dprintk(CVP_ERR, "Invalid Params\n");
  1001. return -EINVAL;
  1002. }
  1003. __strict_check(device);
  1004. if (!__core_in_valid_state(device)) {
  1005. dprintk(CVP_WARN, "%s - fw not in init state\n", __func__);
  1006. rc = -EINVAL;
  1007. goto read_error_null;
  1008. }
  1009. q_info = &device->iface_queues[CVP_IFACEQ_MSGQ_IDX];
  1010. if (q_info->q_array.align_virtual_addr == NULL) {
  1011. dprintk(CVP_ERR, "cannot read from shared MSG Q's\n");
  1012. rc = -ENODATA;
  1013. goto read_error_null;
  1014. }
  1015. if (!__read_queue(q_info, (u8 *)pkt, &tx_req_is_set)) {
  1016. if (tx_req_is_set)
  1017. __write_register(device, CVP_CPU_CS_H2ASOFTINT, 1);
  1018. rc = 0;
  1019. } else
  1020. rc = -ENODATA;
  1021. read_error_null:
  1022. return rc;
  1023. }
  1024. static int __iface_dbgq_read(struct iris_hfi_device *device, void *pkt)
  1025. {
  1026. u32 tx_req_is_set = 0;
  1027. int rc = 0;
  1028. struct cvp_iface_q_info *q_info;
  1029. if (!pkt) {
  1030. dprintk(CVP_ERR, "Invalid Params\n");
  1031. return -EINVAL;
  1032. }
  1033. __strict_check(device);
  1034. q_info = &device->iface_queues[CVP_IFACEQ_DBGQ_IDX];
  1035. if (q_info->q_array.align_virtual_addr == NULL) {
  1036. dprintk(CVP_ERR, "cannot read from shared DBG Q's\n");
  1037. rc = -ENODATA;
  1038. goto dbg_error_null;
  1039. }
  1040. if (!__read_queue(q_info, (u8 *)pkt, &tx_req_is_set)) {
  1041. if (tx_req_is_set)
  1042. __write_register(device, CVP_CPU_CS_H2ASOFTINT, 1);
  1043. rc = 0;
  1044. } else
  1045. rc = -ENODATA;
  1046. dbg_error_null:
  1047. return rc;
  1048. }
  1049. static void __set_queue_hdr_defaults(struct cvp_hfi_queue_header *q_hdr)
  1050. {
  1051. q_hdr->qhdr_status = 0x1;
  1052. q_hdr->qhdr_type = CVP_IFACEQ_DFLT_QHDR;
  1053. q_hdr->qhdr_q_size = CVP_IFACEQ_QUEUE_SIZE / 4;
  1054. q_hdr->qhdr_pkt_size = 0;
  1055. q_hdr->qhdr_rx_wm = 0x1;
  1056. q_hdr->qhdr_tx_wm = 0x1;
  1057. q_hdr->qhdr_rx_req = 0x1;
  1058. q_hdr->qhdr_tx_req = 0x0;
  1059. q_hdr->qhdr_rx_irq_status = 0x0;
  1060. q_hdr->qhdr_tx_irq_status = 0x0;
  1061. q_hdr->qhdr_read_idx = 0x0;
  1062. q_hdr->qhdr_write_idx = 0x0;
  1063. }
  1064. static void __interface_dsp_queues_release(struct iris_hfi_device *device)
  1065. {
  1066. int i;
  1067. struct msm_cvp_smem *mem_data = &device->dsp_iface_q_table.mem_data;
  1068. struct context_bank_info *cb = mem_data->mapping_info.cb_info;
  1069. if (!device->dsp_iface_q_table.align_virtual_addr) {
  1070. dprintk(CVP_ERR, "%s: already released\n", __func__);
  1071. return;
  1072. }
  1073. dma_unmap_single_attrs(cb->dev, mem_data->device_addr,
  1074. mem_data->size, DMA_BIDIRECTIONAL, 0);
  1075. dma_free_coherent(device->res->mem_cdsp.dev, mem_data->size,
  1076. mem_data->kvaddr, mem_data->dma_handle);
  1077. for (i = 0; i < CVP_IFACEQ_NUMQ; i++) {
  1078. device->dsp_iface_queues[i].q_hdr = NULL;
  1079. device->dsp_iface_queues[i].q_array.align_virtual_addr = NULL;
  1080. device->dsp_iface_queues[i].q_array.align_device_addr = 0;
  1081. }
  1082. device->dsp_iface_q_table.align_virtual_addr = NULL;
  1083. device->dsp_iface_q_table.align_device_addr = 0;
  1084. }
  1085. static int __interface_dsp_queues_init(struct iris_hfi_device *dev)
  1086. {
  1087. int rc = 0;
  1088. u32 i;
  1089. struct cvp_iface_q_info *iface_q;
  1090. int offset = 0;
  1091. phys_addr_t fw_bias = 0;
  1092. size_t q_size;
  1093. struct msm_cvp_smem *mem_data;
  1094. void *kvaddr;
  1095. dma_addr_t dma_handle;
  1096. dma_addr_t iova;
  1097. struct context_bank_info *cb;
  1098. q_size = ALIGN(QUEUE_SIZE, SZ_1M);
  1099. mem_data = &dev->dsp_iface_q_table.mem_data;
  1100. /* Allocate dsp queues from CDSP device memory */
  1101. kvaddr = dma_alloc_coherent(dev->res->mem_cdsp.dev, q_size,
  1102. &dma_handle, GFP_KERNEL);
  1103. if (IS_ERR_OR_NULL(kvaddr)) {
  1104. dprintk(CVP_ERR, "%s: failed dma allocation\n", __func__);
  1105. goto fail_dma_alloc;
  1106. }
  1107. cb = msm_cvp_smem_get_context_bank(dev->res, 0);
  1108. if (!cb) {
  1109. dprintk(CVP_ERR,
  1110. "%s: failed to get context bank\n", __func__);
  1111. goto fail_dma_map;
  1112. }
  1113. iova = dma_map_single_attrs(cb->dev, phys_to_virt(dma_handle),
  1114. q_size, DMA_BIDIRECTIONAL, 0);
  1115. if (dma_mapping_error(cb->dev, iova)) {
  1116. dprintk(CVP_ERR, "%s: failed dma mapping\n", __func__);
  1117. goto fail_dma_map;
  1118. }
  1119. dprintk(CVP_DSP,
  1120. "%s: kvaddr %pK dma_handle %#llx iova %#llx size %zd\n",
  1121. __func__, kvaddr, dma_handle, iova, q_size);
  1122. memset(mem_data, 0, sizeof(struct msm_cvp_smem));
  1123. mem_data->kvaddr = kvaddr;
  1124. mem_data->device_addr = iova;
  1125. mem_data->dma_handle = dma_handle;
  1126. mem_data->size = q_size;
  1127. mem_data->mapping_info.cb_info = cb;
  1128. if (!is_iommu_present(dev->res))
  1129. fw_bias = dev->cvp_hal_data->firmware_base;
  1130. dev->dsp_iface_q_table.align_virtual_addr = kvaddr;
  1131. dev->dsp_iface_q_table.align_device_addr = iova - fw_bias;
  1132. dev->dsp_iface_q_table.mem_size = CVP_IFACEQ_TABLE_SIZE;
  1133. offset = dev->dsp_iface_q_table.mem_size;
  1134. for (i = 0; i < CVP_IFACEQ_NUMQ; i++) {
  1135. iface_q = &dev->dsp_iface_queues[i];
  1136. iface_q->q_array.align_device_addr = iova + offset - fw_bias;
  1137. iface_q->q_array.align_virtual_addr = kvaddr + offset;
  1138. iface_q->q_array.mem_size = CVP_IFACEQ_QUEUE_SIZE;
  1139. offset += iface_q->q_array.mem_size;
  1140. spin_lock_init(&iface_q->hfi_lock);
  1141. }
  1142. cvp_dsp_init_hfi_queue_hdr(dev);
  1143. return rc;
  1144. fail_dma_map:
  1145. dma_free_coherent(dev->res->mem_cdsp.dev, q_size, kvaddr, dma_handle);
  1146. fail_dma_alloc:
  1147. return -ENOMEM;
  1148. }
  1149. static void __interface_queues_release(struct iris_hfi_device *device)
  1150. {
  1151. int i;
  1152. struct cvp_hfi_mem_map_table *qdss;
  1153. struct cvp_hfi_mem_map *mem_map;
  1154. int num_entries = device->res->qdss_addr_set.count;
  1155. unsigned long mem_map_table_base_addr;
  1156. struct context_bank_info *cb;
  1157. if (device->qdss.align_virtual_addr) {
  1158. qdss = (struct cvp_hfi_mem_map_table *)
  1159. device->qdss.align_virtual_addr;
  1160. qdss->mem_map_num_entries = num_entries;
  1161. mem_map_table_base_addr =
  1162. device->qdss.align_device_addr +
  1163. sizeof(struct cvp_hfi_mem_map_table);
  1164. qdss->mem_map_table_base_addr =
  1165. (u32)mem_map_table_base_addr;
  1166. if ((unsigned long)qdss->mem_map_table_base_addr !=
  1167. mem_map_table_base_addr) {
  1168. dprintk(CVP_ERR,
  1169. "Invalid mem_map_table_base_addr %#lx",
  1170. mem_map_table_base_addr);
  1171. }
  1172. mem_map = (struct cvp_hfi_mem_map *)(qdss + 1);
  1173. cb = msm_cvp_smem_get_context_bank(device->res, 0);
  1174. for (i = 0; cb && i < num_entries; i++) {
  1175. iommu_unmap(cb->domain,
  1176. mem_map[i].virtual_addr,
  1177. mem_map[i].size);
  1178. }
  1179. __smem_free(device, &device->qdss.mem_data);
  1180. }
  1181. __smem_free(device, &device->iface_q_table.mem_data);
  1182. __smem_free(device, &device->sfr.mem_data);
  1183. for (i = 0; i < CVP_IFACEQ_NUMQ; i++) {
  1184. device->iface_queues[i].q_hdr = NULL;
  1185. device->iface_queues[i].q_array.align_virtual_addr = NULL;
  1186. device->iface_queues[i].q_array.align_device_addr = 0;
  1187. }
  1188. device->iface_q_table.align_virtual_addr = NULL;
  1189. device->iface_q_table.align_device_addr = 0;
  1190. device->qdss.align_virtual_addr = NULL;
  1191. device->qdss.align_device_addr = 0;
  1192. device->sfr.align_virtual_addr = NULL;
  1193. device->sfr.align_device_addr = 0;
  1194. device->mem_addr.align_virtual_addr = NULL;
  1195. device->mem_addr.align_device_addr = 0;
  1196. __interface_dsp_queues_release(device);
  1197. }
  1198. static int __get_qdss_iommu_virtual_addr(struct iris_hfi_device *dev,
  1199. struct cvp_hfi_mem_map *mem_map,
  1200. struct iommu_domain *domain)
  1201. {
  1202. int i;
  1203. int rc = 0;
  1204. dma_addr_t iova = QDSS_IOVA_START;
  1205. int num_entries = dev->res->qdss_addr_set.count;
  1206. struct addr_range *qdss_addr_tbl = dev->res->qdss_addr_set.addr_tbl;
  1207. if (!num_entries)
  1208. return -ENODATA;
  1209. for (i = 0; i < num_entries; i++) {
  1210. if (domain) {
  1211. rc = iommu_map(domain, iova,
  1212. qdss_addr_tbl[i].start,
  1213. qdss_addr_tbl[i].size,
  1214. IOMMU_READ | IOMMU_WRITE);
  1215. if (rc) {
  1216. dprintk(CVP_ERR,
  1217. "IOMMU QDSS mapping failed for addr %#x\n",
  1218. qdss_addr_tbl[i].start);
  1219. rc = -ENOMEM;
  1220. break;
  1221. }
  1222. } else {
  1223. iova = qdss_addr_tbl[i].start;
  1224. }
  1225. mem_map[i].virtual_addr = (u32)iova;
  1226. mem_map[i].physical_addr = qdss_addr_tbl[i].start;
  1227. mem_map[i].size = qdss_addr_tbl[i].size;
  1228. mem_map[i].attr = 0x0;
  1229. iova += mem_map[i].size;
  1230. }
  1231. if (i < num_entries) {
  1232. dprintk(CVP_ERR,
  1233. "QDSS mapping failed, Freeing other entries %d\n", i);
  1234. for (--i; domain && i >= 0; i--) {
  1235. iommu_unmap(domain,
  1236. mem_map[i].virtual_addr,
  1237. mem_map[i].size);
  1238. }
  1239. }
  1240. return rc;
  1241. }
  1242. static void __setup_ucregion_memory_map(struct iris_hfi_device *device)
  1243. {
  1244. __write_register(device, CVP_UC_REGION_ADDR,
  1245. (u32)device->iface_q_table.align_device_addr);
  1246. __write_register(device, CVP_UC_REGION_SIZE, SHARED_QSIZE);
  1247. __write_register(device, CVP_QTBL_ADDR,
  1248. (u32)device->iface_q_table.align_device_addr);
  1249. __write_register(device, CVP_QTBL_INFO, 0x01);
  1250. if (device->sfr.align_device_addr)
  1251. __write_register(device, CVP_SFR_ADDR,
  1252. (u32)device->sfr.align_device_addr);
  1253. if (device->qdss.align_device_addr)
  1254. __write_register(device, CVP_MMAP_ADDR,
  1255. (u32)device->qdss.align_device_addr);
  1256. call_iris_op(device, setup_dsp_uc_memmap, device);
  1257. }
  1258. static int __interface_queues_init(struct iris_hfi_device *dev)
  1259. {
  1260. struct cvp_hfi_queue_table_header *q_tbl_hdr;
  1261. struct cvp_hfi_queue_header *q_hdr;
  1262. u32 i;
  1263. int rc = 0;
  1264. struct cvp_hfi_mem_map_table *qdss;
  1265. struct cvp_hfi_mem_map *mem_map;
  1266. struct cvp_iface_q_info *iface_q;
  1267. struct cvp_hfi_sfr_struct *vsfr;
  1268. struct cvp_mem_addr *mem_addr;
  1269. int offset = 0;
  1270. int num_entries = dev->res->qdss_addr_set.count;
  1271. phys_addr_t fw_bias = 0;
  1272. size_t q_size;
  1273. unsigned long mem_map_table_base_addr;
  1274. struct context_bank_info *cb;
  1275. q_size = SHARED_QSIZE - ALIGNED_SFR_SIZE - ALIGNED_QDSS_SIZE;
  1276. mem_addr = &dev->mem_addr;
  1277. if (!is_iommu_present(dev->res))
  1278. fw_bias = dev->cvp_hal_data->firmware_base;
  1279. rc = __smem_alloc(dev, mem_addr, q_size, 1, SMEM_UNCACHED);
  1280. if (rc) {
  1281. dprintk(CVP_ERR, "iface_q_table_alloc_fail\n");
  1282. goto fail_alloc_queue;
  1283. }
  1284. dev->iface_q_table.align_virtual_addr = mem_addr->align_virtual_addr;
  1285. dev->iface_q_table.align_device_addr = mem_addr->align_device_addr -
  1286. fw_bias;
  1287. dev->iface_q_table.mem_size = CVP_IFACEQ_TABLE_SIZE;
  1288. dev->iface_q_table.mem_data = mem_addr->mem_data;
  1289. offset += dev->iface_q_table.mem_size;
  1290. for (i = 0; i < CVP_IFACEQ_NUMQ; i++) {
  1291. iface_q = &dev->iface_queues[i];
  1292. iface_q->q_array.align_device_addr = mem_addr->align_device_addr
  1293. + offset - fw_bias;
  1294. iface_q->q_array.align_virtual_addr =
  1295. mem_addr->align_virtual_addr + offset;
  1296. iface_q->q_array.mem_size = CVP_IFACEQ_QUEUE_SIZE;
  1297. offset += iface_q->q_array.mem_size;
  1298. iface_q->q_hdr = CVP_IFACEQ_GET_QHDR_START_ADDR(
  1299. dev->iface_q_table.align_virtual_addr, i);
  1300. __set_queue_hdr_defaults(iface_q->q_hdr);
  1301. spin_lock_init(&iface_q->hfi_lock);
  1302. }
  1303. if ((msm_cvp_fw_debug_mode & HFI_DEBUG_MODE_QDSS) && num_entries) {
  1304. rc = __smem_alloc(dev, mem_addr, ALIGNED_QDSS_SIZE, 1,
  1305. SMEM_UNCACHED);
  1306. if (rc) {
  1307. dprintk(CVP_WARN,
  1308. "qdss_alloc_fail: QDSS messages logging will not work\n");
  1309. dev->qdss.align_device_addr = 0;
  1310. } else {
  1311. dev->qdss.align_device_addr =
  1312. mem_addr->align_device_addr - fw_bias;
  1313. dev->qdss.align_virtual_addr =
  1314. mem_addr->align_virtual_addr;
  1315. dev->qdss.mem_size = ALIGNED_QDSS_SIZE;
  1316. dev->qdss.mem_data = mem_addr->mem_data;
  1317. }
  1318. }
  1319. rc = __smem_alloc(dev, mem_addr, ALIGNED_SFR_SIZE, 1, SMEM_UNCACHED);
  1320. if (rc) {
  1321. dprintk(CVP_WARN, "sfr_alloc_fail: SFR not will work\n");
  1322. dev->sfr.align_device_addr = 0;
  1323. } else {
  1324. dev->sfr.align_device_addr = mem_addr->align_device_addr -
  1325. fw_bias;
  1326. dev->sfr.align_virtual_addr = mem_addr->align_virtual_addr;
  1327. dev->sfr.mem_size = ALIGNED_SFR_SIZE;
  1328. dev->sfr.mem_data = mem_addr->mem_data;
  1329. }
  1330. q_tbl_hdr = (struct cvp_hfi_queue_table_header *)
  1331. dev->iface_q_table.align_virtual_addr;
  1332. q_tbl_hdr->qtbl_version = 0;
  1333. q_tbl_hdr->device_addr = (void *)dev;
  1334. strlcpy(q_tbl_hdr->name, "msm_cvp", sizeof(q_tbl_hdr->name));
  1335. q_tbl_hdr->qtbl_size = CVP_IFACEQ_TABLE_SIZE;
  1336. q_tbl_hdr->qtbl_qhdr0_offset =
  1337. sizeof(struct cvp_hfi_queue_table_header);
  1338. q_tbl_hdr->qtbl_qhdr_size = sizeof(struct cvp_hfi_queue_header);
  1339. q_tbl_hdr->qtbl_num_q = CVP_IFACEQ_NUMQ;
  1340. q_tbl_hdr->qtbl_num_active_q = CVP_IFACEQ_NUMQ;
  1341. iface_q = &dev->iface_queues[CVP_IFACEQ_CMDQ_IDX];
  1342. q_hdr = iface_q->q_hdr;
  1343. q_hdr->qhdr_start_addr = iface_q->q_array.align_device_addr;
  1344. q_hdr->qhdr_type |= HFI_Q_ID_HOST_TO_CTRL_CMD_Q;
  1345. iface_q = &dev->iface_queues[CVP_IFACEQ_MSGQ_IDX];
  1346. q_hdr = iface_q->q_hdr;
  1347. q_hdr->qhdr_start_addr = iface_q->q_array.align_device_addr;
  1348. q_hdr->qhdr_type |= HFI_Q_ID_CTRL_TO_HOST_MSG_Q;
  1349. iface_q = &dev->iface_queues[CVP_IFACEQ_DBGQ_IDX];
  1350. q_hdr = iface_q->q_hdr;
  1351. q_hdr->qhdr_start_addr = iface_q->q_array.align_device_addr;
  1352. q_hdr->qhdr_type |= HFI_Q_ID_CTRL_TO_HOST_DEBUG_Q;
  1353. /*
  1354. * Set receive request to zero on debug queue as there is no
  1355. * need of interrupt from cvp hardware for debug messages
  1356. */
  1357. q_hdr->qhdr_rx_req = 0;
  1358. if (dev->qdss.align_virtual_addr) {
  1359. qdss =
  1360. (struct cvp_hfi_mem_map_table *)dev->qdss.align_virtual_addr;
  1361. qdss->mem_map_num_entries = num_entries;
  1362. mem_map_table_base_addr = dev->qdss.align_device_addr +
  1363. sizeof(struct cvp_hfi_mem_map_table);
  1364. qdss->mem_map_table_base_addr = mem_map_table_base_addr;
  1365. mem_map = (struct cvp_hfi_mem_map *)(qdss + 1);
  1366. cb = msm_cvp_smem_get_context_bank(dev->res, 0);
  1367. if (!cb) {
  1368. dprintk(CVP_ERR,
  1369. "%s: failed to get context bank\n", __func__);
  1370. return -EINVAL;
  1371. }
  1372. rc = __get_qdss_iommu_virtual_addr(dev, mem_map, cb->domain);
  1373. if (rc) {
  1374. dprintk(CVP_ERR,
  1375. "IOMMU mapping failed, Freeing qdss memdata\n");
  1376. __smem_free(dev, &dev->qdss.mem_data);
  1377. dev->qdss.align_virtual_addr = NULL;
  1378. dev->qdss.align_device_addr = 0;
  1379. }
  1380. }
  1381. vsfr = (struct cvp_hfi_sfr_struct *) dev->sfr.align_virtual_addr;
  1382. if (vsfr)
  1383. vsfr->bufSize = ALIGNED_SFR_SIZE;
  1384. rc = __interface_dsp_queues_init(dev);
  1385. if (rc) {
  1386. dprintk(CVP_ERR, "dsp_queues_init failed\n");
  1387. goto fail_alloc_queue;
  1388. }
  1389. __setup_ucregion_memory_map(dev);
  1390. return 0;
  1391. fail_alloc_queue:
  1392. return -ENOMEM;
  1393. }
  1394. static int __sys_set_debug(struct iris_hfi_device *device, u32 debug)
  1395. {
  1396. u8 packet[CVP_IFACEQ_VAR_SMALL_PKT_SIZE];
  1397. int rc = 0;
  1398. struct cvp_hfi_cmd_sys_set_property_packet *pkt =
  1399. (struct cvp_hfi_cmd_sys_set_property_packet *) &packet;
  1400. rc = call_hfi_pkt_op(device, sys_debug_config, pkt, debug);
  1401. if (rc) {
  1402. dprintk(CVP_WARN,
  1403. "Debug mode setting to FW failed\n");
  1404. return -ENOTEMPTY;
  1405. }
  1406. if (__iface_cmdq_write(device, pkt))
  1407. return -ENOTEMPTY;
  1408. return 0;
  1409. }
  1410. static int __sys_set_idle_indicator(struct iris_hfi_device *device,
  1411. bool enable)
  1412. {
  1413. u8 packet[CVP_IFACEQ_VAR_SMALL_PKT_SIZE];
  1414. int rc = 0;
  1415. struct cvp_hfi_cmd_sys_set_property_packet *pkt =
  1416. (struct cvp_hfi_cmd_sys_set_property_packet *) &packet;
  1417. rc = call_hfi_pkt_op(device, sys_set_idle_indicator, pkt, enable);
  1418. if (__iface_cmdq_write(device, pkt))
  1419. return -ENOTEMPTY;
  1420. return 0;
  1421. }
  1422. static int __sys_set_coverage(struct iris_hfi_device *device, u32 mode)
  1423. {
  1424. u8 packet[CVP_IFACEQ_VAR_SMALL_PKT_SIZE];
  1425. int rc = 0;
  1426. struct cvp_hfi_cmd_sys_set_property_packet *pkt =
  1427. (struct cvp_hfi_cmd_sys_set_property_packet *) &packet;
  1428. rc = call_hfi_pkt_op(device, sys_coverage_config,
  1429. pkt, mode);
  1430. if (rc) {
  1431. dprintk(CVP_WARN,
  1432. "Coverage mode setting to FW failed\n");
  1433. return -ENOTEMPTY;
  1434. }
  1435. if (__iface_cmdq_write(device, pkt)) {
  1436. dprintk(CVP_WARN, "Failed to send coverage pkt to f/w\n");
  1437. return -ENOTEMPTY;
  1438. }
  1439. return 0;
  1440. }
  1441. static int __sys_set_power_control(struct iris_hfi_device *device,
  1442. bool enable)
  1443. {
  1444. struct regulator_info *rinfo;
  1445. bool supported = false;
  1446. u8 packet[CVP_IFACEQ_VAR_SMALL_PKT_SIZE];
  1447. struct cvp_hfi_cmd_sys_set_property_packet *pkt =
  1448. (struct cvp_hfi_cmd_sys_set_property_packet *) &packet;
  1449. iris_hfi_for_each_regulator(device, rinfo) {
  1450. if (rinfo->has_hw_power_collapse) {
  1451. supported = true;
  1452. break;
  1453. }
  1454. }
  1455. if (!supported)
  1456. return 0;
  1457. call_hfi_pkt_op(device, sys_power_control, pkt, enable);
  1458. if (__iface_cmdq_write(device, pkt))
  1459. return -ENOTEMPTY;
  1460. return 0;
  1461. }
  1462. static int iris_hfi_core_init(void *device)
  1463. {
  1464. int rc = 0;
  1465. u32 ipcc_iova;
  1466. struct cvp_hfi_cmd_sys_init_packet pkt;
  1467. struct cvp_hfi_cmd_sys_get_property_packet version_pkt;
  1468. struct iris_hfi_device *dev;
  1469. if (!device) {
  1470. dprintk(CVP_ERR, "Invalid device\n");
  1471. return -ENODEV;
  1472. }
  1473. dev = device;
  1474. dprintk(CVP_CORE, "Core initializing\n");
  1475. mutex_lock(&dev->lock);
  1476. dev->bus_vote.data =
  1477. kzalloc(sizeof(struct cvp_bus_vote_data), GFP_KERNEL);
  1478. if (!dev->bus_vote.data) {
  1479. dprintk(CVP_ERR, "Bus vote data memory is not allocated\n");
  1480. rc = -ENOMEM;
  1481. goto err_no_mem;
  1482. }
  1483. dev->bus_vote.data_count = 1;
  1484. dev->bus_vote.data->power_mode = CVP_POWER_TURBO;
  1485. rc = __load_fw(dev);
  1486. if (rc) {
  1487. dprintk(CVP_ERR, "Failed to load Iris FW\n");
  1488. goto err_load_fw;
  1489. }
  1490. /* mmrm registration */
  1491. if (msm_cvp_mmrm_enabled) {
  1492. rc = msm_cvp_mmrm_register(device);
  1493. if (rc) {
  1494. dprintk(CVP_ERR, "Failed to register mmrm client\n");
  1495. goto err_core_init;
  1496. }
  1497. }
  1498. __set_state(dev, IRIS_STATE_INIT);
  1499. dev->reg_dumped = false;
  1500. dprintk(CVP_CORE, "Dev_Virt: %pa, Reg_Virt: %pK\n",
  1501. &dev->cvp_hal_data->firmware_base,
  1502. dev->cvp_hal_data->register_base);
  1503. rc = __interface_queues_init(dev);
  1504. if (rc) {
  1505. dprintk(CVP_ERR, "failed to init queues\n");
  1506. rc = -ENOMEM;
  1507. goto err_mmrm_dereg;
  1508. }
  1509. rc = msm_cvp_map_ipcc_regs(&ipcc_iova);
  1510. if (!rc) {
  1511. dprintk(CVP_CORE, "IPCC iova 0x%x\n", ipcc_iova);
  1512. __write_register(dev, CVP_MMAP_ADDR, ipcc_iova);
  1513. }
  1514. rc = __boot_firmware(dev);
  1515. if (rc) {
  1516. dprintk(CVP_ERR, "Failed to start core\n");
  1517. rc = -ENODEV;
  1518. goto err_mmrm_dereg;
  1519. }
  1520. dev->version = __read_register(dev, CVP_VERSION_INFO);
  1521. rc = call_hfi_pkt_op(dev, sys_init, &pkt, 0);
  1522. if (rc) {
  1523. dprintk(CVP_ERR, "Failed to create sys init pkt\n");
  1524. goto err_mmrm_dereg;
  1525. }
  1526. if (__iface_cmdq_write(dev, &pkt)) {
  1527. rc = -ENOTEMPTY;
  1528. goto err_mmrm_dereg;
  1529. }
  1530. rc = call_hfi_pkt_op(dev, sys_image_version, &version_pkt);
  1531. if (rc || __iface_cmdq_write(dev, &version_pkt))
  1532. dprintk(CVP_WARN, "Failed to send image version pkt to f/w\n");
  1533. __sys_set_debug(device, msm_cvp_fw_debug);
  1534. __enable_subcaches(device);
  1535. __set_subcaches(device);
  1536. __set_ubwc_config(device);
  1537. __sys_set_idle_indicator(device, true);
  1538. if (dev->res->pm_qos_latency_us)
  1539. cpu_latency_qos_add_request(&dev->qos,
  1540. dev->res->pm_qos_latency_us);
  1541. mutex_unlock(&dev->lock);
  1542. cvp_dsp_send_hfi_queue();
  1543. dprintk(CVP_CORE, "Core inited successfully\n");
  1544. return 0;
  1545. err_mmrm_dereg:
  1546. msm_cvp_mmrm_deregister(dev);
  1547. err_core_init:
  1548. __set_state(dev, IRIS_STATE_DEINIT);
  1549. __unload_fw(dev);
  1550. err_load_fw:
  1551. err_no_mem:
  1552. dprintk(CVP_ERR, "Core init failed\n");
  1553. mutex_unlock(&dev->lock);
  1554. return rc;
  1555. }
  1556. static int iris_hfi_core_release(void *dev)
  1557. {
  1558. int rc = 0;
  1559. struct iris_hfi_device *device = dev;
  1560. struct cvp_hal_session *session, *next;
  1561. if (!device) {
  1562. dprintk(CVP_ERR, "invalid device\n");
  1563. return -ENODEV;
  1564. }
  1565. mutex_lock(&device->lock);
  1566. dprintk(CVP_WARN, "Core releasing\n");
  1567. if (device->res->pm_qos_latency_us &&
  1568. cpu_latency_qos_request_active(&device->qos))
  1569. cpu_latency_qos_remove_request(&device->qos);
  1570. __resume(device);
  1571. __set_state(device, IRIS_STATE_DEINIT);
  1572. __dsp_shutdown(device, 0);
  1573. if (msm_cvp_mmrm_enabled) {
  1574. rc = msm_cvp_mmrm_deregister(device);
  1575. if (rc) {
  1576. dprintk(CVP_ERR,
  1577. "%s: Failed msm_cvp_mmrm_deregister:%d\n",
  1578. __func__, rc);
  1579. }
  1580. }
  1581. __disable_subcaches(device);
  1582. __unload_fw(device);
  1583. /* unlink all sessions from device */
  1584. list_for_each_entry_safe(session, next, &device->sess_head, list) {
  1585. list_del(&session->list);
  1586. session->device = NULL;
  1587. }
  1588. dprintk(CVP_CORE, "Core released successfully\n");
  1589. mutex_unlock(&device->lock);
  1590. return rc;
  1591. }
  1592. static void __core_clear_interrupt(struct iris_hfi_device *device)
  1593. {
  1594. u32 intr_status = 0, mask = 0;
  1595. if (!device) {
  1596. dprintk(CVP_ERR, "%s: NULL device\n", __func__);
  1597. return;
  1598. }
  1599. intr_status = __read_register(device, CVP_WRAPPER_INTR_STATUS);
  1600. mask = (CVP_WRAPPER_INTR_MASK_A2HCPU_BMSK | CVP_FATAL_INTR_BMSK);
  1601. if (intr_status & mask) {
  1602. device->intr_status |= intr_status;
  1603. device->reg_count++;
  1604. dprintk(CVP_CORE,
  1605. "INTERRUPT for device: %pK: times: %d status: %d\n",
  1606. device, device->reg_count, intr_status);
  1607. } else {
  1608. device->spur_count++;
  1609. }
  1610. __write_register(device, CVP_CPU_CS_A2HSOFTINTCLR, 1);
  1611. }
  1612. static int iris_hfi_core_trigger_ssr(void *device,
  1613. enum hal_ssr_trigger_type type)
  1614. {
  1615. struct cvp_hfi_cmd_sys_test_ssr_packet pkt;
  1616. int rc = 0;
  1617. struct iris_hfi_device *dev;
  1618. if (!device) {
  1619. dprintk(CVP_ERR, "invalid device\n");
  1620. return -ENODEV;
  1621. }
  1622. dev = device;
  1623. if (mutex_trylock(&dev->lock)) {
  1624. rc = call_hfi_pkt_op(dev, ssr_cmd, type, &pkt);
  1625. if (rc) {
  1626. dprintk(CVP_ERR, "%s: failed to create packet\n",
  1627. __func__);
  1628. goto err_create_pkt;
  1629. }
  1630. if (__iface_cmdq_write(dev, &pkt))
  1631. rc = -ENOTEMPTY;
  1632. } else {
  1633. return -EAGAIN;
  1634. }
  1635. err_create_pkt:
  1636. mutex_unlock(&dev->lock);
  1637. return rc;
  1638. }
  1639. static void __set_default_sys_properties(struct iris_hfi_device *device)
  1640. {
  1641. if (__sys_set_debug(device, msm_cvp_fw_debug))
  1642. dprintk(CVP_WARN, "Setting fw_debug msg ON failed\n");
  1643. if (__sys_set_power_control(device, msm_cvp_fw_low_power_mode))
  1644. dprintk(CVP_WARN, "Setting h/w power collapse ON failed\n");
  1645. }
  1646. static void __session_clean(struct cvp_hal_session *session)
  1647. {
  1648. struct cvp_hal_session *temp, *next;
  1649. struct iris_hfi_device *device;
  1650. if (!session || !session->device) {
  1651. dprintk(CVP_WARN, "%s: invalid params\n", __func__);
  1652. return;
  1653. }
  1654. device = session->device;
  1655. dprintk(CVP_SESS, "deleted the session: %pK\n", session);
  1656. /*
  1657. * session might have been removed from the device list in
  1658. * core_release, so check and remove if it is in the list
  1659. */
  1660. list_for_each_entry_safe(temp, next, &device->sess_head, list) {
  1661. if (session == temp) {
  1662. list_del(&session->list);
  1663. break;
  1664. }
  1665. }
  1666. /* Poison the session handle with zeros */
  1667. *session = (struct cvp_hal_session){ {0} };
  1668. kfree(session);
  1669. }
  1670. static int iris_hfi_session_clean(void *session)
  1671. {
  1672. struct cvp_hal_session *sess_close;
  1673. struct iris_hfi_device *device;
  1674. if (!session) {
  1675. dprintk(CVP_ERR, "Invalid Params %s\n", __func__);
  1676. return -EINVAL;
  1677. }
  1678. sess_close = session;
  1679. device = sess_close->device;
  1680. if (!device) {
  1681. dprintk(CVP_ERR, "Invalid device handle %s\n", __func__);
  1682. return -EINVAL;
  1683. }
  1684. mutex_lock(&device->lock);
  1685. __session_clean(sess_close);
  1686. mutex_unlock(&device->lock);
  1687. return 0;
  1688. }
  1689. static int iris_hfi_session_init(void *device, void *session_id,
  1690. void **new_session)
  1691. {
  1692. struct cvp_hfi_cmd_sys_session_init_packet pkt;
  1693. struct iris_hfi_device *dev;
  1694. struct cvp_hal_session *s;
  1695. if (!device || !new_session) {
  1696. dprintk(CVP_ERR, "%s - invalid input\n", __func__);
  1697. return -EINVAL;
  1698. }
  1699. dev = device;
  1700. mutex_lock(&dev->lock);
  1701. s = kzalloc(sizeof(*s), GFP_KERNEL);
  1702. if (!s) {
  1703. dprintk(CVP_ERR, "new session fail: Out of memory\n");
  1704. goto err_session_init_fail;
  1705. }
  1706. s->session_id = session_id;
  1707. s->device = dev;
  1708. dprintk(CVP_SESS,
  1709. "%s: inst %pK, session %pK\n", __func__, session_id, s);
  1710. list_add_tail(&s->list, &dev->sess_head);
  1711. __set_default_sys_properties(device);
  1712. if (call_hfi_pkt_op(dev, session_init, &pkt, s)) {
  1713. dprintk(CVP_ERR, "session_init: failed to create packet\n");
  1714. goto err_session_init_fail;
  1715. }
  1716. *new_session = s;
  1717. if (__iface_cmdq_write(dev, &pkt))
  1718. goto err_session_init_fail;
  1719. mutex_unlock(&dev->lock);
  1720. return 0;
  1721. err_session_init_fail:
  1722. if (s)
  1723. __session_clean(s);
  1724. *new_session = NULL;
  1725. mutex_unlock(&dev->lock);
  1726. return -EINVAL;
  1727. }
  1728. static int __send_session_cmd(struct cvp_hal_session *session, int pkt_type)
  1729. {
  1730. struct cvp_hal_session_cmd_pkt pkt;
  1731. int rc = 0;
  1732. struct iris_hfi_device *device = session->device;
  1733. if (!__is_session_valid(device, session, __func__))
  1734. return -ECONNRESET;
  1735. rc = call_hfi_pkt_op(device, session_cmd,
  1736. &pkt, pkt_type, session);
  1737. if (rc == -EPERM)
  1738. return 0;
  1739. if (rc) {
  1740. dprintk(CVP_ERR, "send session cmd: create pkt failed\n");
  1741. goto err_create_pkt;
  1742. }
  1743. if (__iface_cmdq_write(session->device, &pkt))
  1744. rc = -ENOTEMPTY;
  1745. err_create_pkt:
  1746. return rc;
  1747. }
  1748. static int iris_hfi_session_end(void *session)
  1749. {
  1750. struct cvp_hal_session *sess;
  1751. struct iris_hfi_device *device;
  1752. int rc = 0;
  1753. if (!session) {
  1754. dprintk(CVP_ERR, "Invalid Params %s\n", __func__);
  1755. return -EINVAL;
  1756. }
  1757. sess = session;
  1758. device = sess->device;
  1759. if (!device) {
  1760. dprintk(CVP_ERR, "Invalid session %s\n", __func__);
  1761. return -EINVAL;
  1762. }
  1763. mutex_lock(&device->lock);
  1764. if (msm_cvp_fw_coverage) {
  1765. if (__sys_set_coverage(sess->device, msm_cvp_fw_coverage))
  1766. dprintk(CVP_WARN, "Fw_coverage msg ON failed\n");
  1767. }
  1768. rc = __send_session_cmd(session, HFI_CMD_SYS_SESSION_END);
  1769. mutex_unlock(&device->lock);
  1770. return rc;
  1771. }
  1772. static int iris_hfi_session_abort(void *sess)
  1773. {
  1774. struct cvp_hal_session *session = sess;
  1775. struct iris_hfi_device *device;
  1776. int rc = 0;
  1777. if (!session || !session->device) {
  1778. dprintk(CVP_ERR, "Invalid Params %s\n", __func__);
  1779. return -EINVAL;
  1780. }
  1781. device = session->device;
  1782. mutex_lock(&device->lock);
  1783. rc = __send_session_cmd(session, HFI_CMD_SYS_SESSION_ABORT);
  1784. mutex_unlock(&device->lock);
  1785. return rc;
  1786. }
  1787. static int iris_hfi_session_set_buffers(void *sess, u32 iova, u32 size)
  1788. {
  1789. struct cvp_hfi_cmd_session_set_buffers_packet pkt;
  1790. int rc = 0;
  1791. struct cvp_hal_session *session = sess;
  1792. struct iris_hfi_device *device;
  1793. if (!session || !session->device || !iova || !size) {
  1794. dprintk(CVP_ERR, "Invalid Params\n");
  1795. return -EINVAL;
  1796. }
  1797. device = session->device;
  1798. mutex_lock(&device->lock);
  1799. if (!__is_session_valid(device, session, __func__)) {
  1800. rc = -ECONNRESET;
  1801. goto err_create_pkt;
  1802. }
  1803. rc = call_hfi_pkt_op(device, session_set_buffers,
  1804. &pkt, session, iova, size);
  1805. if (rc) {
  1806. dprintk(CVP_ERR, "set buffers: failed to create packet\n");
  1807. goto err_create_pkt;
  1808. }
  1809. if (__iface_cmdq_write(session->device, &pkt))
  1810. rc = -ENOTEMPTY;
  1811. err_create_pkt:
  1812. mutex_unlock(&device->lock);
  1813. return rc;
  1814. }
  1815. static int iris_hfi_session_release_buffers(void *sess)
  1816. {
  1817. struct cvp_session_release_buffers_packet pkt;
  1818. int rc = 0;
  1819. struct cvp_hal_session *session = sess;
  1820. struct iris_hfi_device *device;
  1821. if (!session || !session->device) {
  1822. dprintk(CVP_ERR, "Invalid Params\n");
  1823. return -EINVAL;
  1824. }
  1825. device = session->device;
  1826. mutex_lock(&device->lock);
  1827. if (!__is_session_valid(device, session, __func__)) {
  1828. rc = -ECONNRESET;
  1829. goto err_create_pkt;
  1830. }
  1831. rc = call_hfi_pkt_op(device, session_release_buffers, &pkt, session);
  1832. if (rc) {
  1833. dprintk(CVP_ERR, "release buffers: failed to create packet\n");
  1834. goto err_create_pkt;
  1835. }
  1836. if (__iface_cmdq_write(session->device, &pkt))
  1837. rc = -ENOTEMPTY;
  1838. err_create_pkt:
  1839. mutex_unlock(&device->lock);
  1840. return rc;
  1841. }
  1842. static int iris_hfi_session_send(void *sess,
  1843. struct eva_kmd_hfi_packet *in_pkt)
  1844. {
  1845. int rc = 0;
  1846. struct eva_kmd_hfi_packet pkt;
  1847. struct cvp_hal_session *session = sess;
  1848. struct iris_hfi_device *device;
  1849. if (!session || !session->device) {
  1850. dprintk(CVP_ERR, "invalid session");
  1851. return -ENODEV;
  1852. }
  1853. device = session->device;
  1854. mutex_lock(&device->lock);
  1855. if (!__is_session_valid(device, session, __func__)) {
  1856. rc = -ECONNRESET;
  1857. goto err_send_pkt;
  1858. }
  1859. rc = call_hfi_pkt_op(device, session_send,
  1860. &pkt, session, in_pkt);
  1861. if (rc) {
  1862. dprintk(CVP_ERR,
  1863. "failed to create pkt\n");
  1864. goto err_send_pkt;
  1865. }
  1866. if (__iface_cmdq_write(session->device, &pkt))
  1867. rc = -ENOTEMPTY;
  1868. err_send_pkt:
  1869. mutex_unlock(&device->lock);
  1870. return rc;
  1871. return rc;
  1872. }
  1873. static int iris_hfi_session_flush(void *sess)
  1874. {
  1875. struct cvp_hal_session *session = sess;
  1876. struct iris_hfi_device *device;
  1877. int rc = 0;
  1878. if (!session || !session->device) {
  1879. dprintk(CVP_ERR, "Invalid Params %s\n", __func__);
  1880. return -EINVAL;
  1881. }
  1882. device = session->device;
  1883. mutex_lock(&device->lock);
  1884. rc = __send_session_cmd(session, HFI_CMD_SESSION_CVP_FLUSH);
  1885. mutex_unlock(&device->lock);
  1886. return rc;
  1887. }
  1888. static int __check_core_registered(struct iris_hfi_device *device,
  1889. phys_addr_t fw_addr, u8 *reg_addr, u32 reg_size,
  1890. phys_addr_t irq)
  1891. {
  1892. struct cvp_hal_data *cvp_hal_data;
  1893. if (!device) {
  1894. dprintk(CVP_INFO, "no device Registered\n");
  1895. return -EINVAL;
  1896. }
  1897. cvp_hal_data = device->cvp_hal_data;
  1898. if (!cvp_hal_data)
  1899. return -EINVAL;
  1900. if (cvp_hal_data->irq == irq &&
  1901. (CONTAINS(cvp_hal_data->firmware_base,
  1902. FIRMWARE_SIZE, fw_addr) ||
  1903. CONTAINS(fw_addr, FIRMWARE_SIZE,
  1904. cvp_hal_data->firmware_base) ||
  1905. CONTAINS(cvp_hal_data->register_base,
  1906. reg_size, reg_addr) ||
  1907. CONTAINS(reg_addr, reg_size,
  1908. cvp_hal_data->register_base) ||
  1909. OVERLAPS(cvp_hal_data->register_base,
  1910. reg_size, reg_addr, reg_size) ||
  1911. OVERLAPS(reg_addr, reg_size,
  1912. cvp_hal_data->register_base,
  1913. reg_size) ||
  1914. OVERLAPS(cvp_hal_data->firmware_base,
  1915. FIRMWARE_SIZE, fw_addr,
  1916. FIRMWARE_SIZE) ||
  1917. OVERLAPS(fw_addr, FIRMWARE_SIZE,
  1918. cvp_hal_data->firmware_base,
  1919. FIRMWARE_SIZE))) {
  1920. return 0;
  1921. }
  1922. dprintk(CVP_INFO, "Device not registered\n");
  1923. return -EINVAL;
  1924. }
  1925. static void __process_fatal_error(
  1926. struct iris_hfi_device *device)
  1927. {
  1928. struct msm_cvp_cb_cmd_done cmd_done = {0};
  1929. cmd_done.device_id = device->device_id;
  1930. device->callback(HAL_SYS_ERROR, &cmd_done);
  1931. }
  1932. static int __prepare_pc(struct iris_hfi_device *device)
  1933. {
  1934. int rc = 0;
  1935. struct cvp_hfi_cmd_sys_pc_prep_packet pkt;
  1936. rc = call_hfi_pkt_op(device, sys_pc_prep, &pkt);
  1937. if (rc) {
  1938. dprintk(CVP_ERR, "Failed to create sys pc prep pkt\n");
  1939. goto err_pc_prep;
  1940. }
  1941. if (__iface_cmdq_write(device, &pkt))
  1942. rc = -ENOTEMPTY;
  1943. if (rc)
  1944. dprintk(CVP_ERR, "Failed to prepare iris for power off");
  1945. err_pc_prep:
  1946. return rc;
  1947. }
  1948. static void iris_hfi_pm_handler(struct work_struct *work)
  1949. {
  1950. int rc = 0;
  1951. struct msm_cvp_core *core;
  1952. struct iris_hfi_device *device;
  1953. core = list_first_entry(&cvp_driver->cores, struct msm_cvp_core, list);
  1954. if (core)
  1955. device = core->device->hfi_device_data;
  1956. else
  1957. return;
  1958. if (!device) {
  1959. dprintk(CVP_ERR, "%s: NULL device\n", __func__);
  1960. return;
  1961. }
  1962. dprintk(CVP_PWR,
  1963. "Entering %s\n", __func__);
  1964. /*
  1965. * It is ok to check this variable outside the lock since
  1966. * it is being updated in this context only
  1967. */
  1968. if (device->skip_pc_count >= CVP_MAX_PC_SKIP_COUNT) {
  1969. dprintk(CVP_WARN, "Failed to PC for %d times\n",
  1970. device->skip_pc_count);
  1971. device->skip_pc_count = 0;
  1972. __process_fatal_error(device);
  1973. return;
  1974. }
  1975. mutex_lock(&device->lock);
  1976. if (gfa_cv.state == DSP_SUSPEND)
  1977. rc = __power_collapse(device, true);
  1978. else
  1979. rc = __power_collapse(device, false);
  1980. mutex_unlock(&device->lock);
  1981. switch (rc) {
  1982. case 0:
  1983. device->skip_pc_count = 0;
  1984. /* Cancel pending delayed works if any */
  1985. cancel_delayed_work(&iris_hfi_pm_work);
  1986. dprintk(CVP_PWR, "%s: power collapse successful!\n",
  1987. __func__);
  1988. break;
  1989. case -EBUSY:
  1990. device->skip_pc_count = 0;
  1991. dprintk(CVP_PWR, "%s: retry PC as cvp is busy\n", __func__);
  1992. queue_delayed_work(device->iris_pm_workq,
  1993. &iris_hfi_pm_work, msecs_to_jiffies(
  1994. device->res->msm_cvp_pwr_collapse_delay));
  1995. break;
  1996. case -EAGAIN:
  1997. device->skip_pc_count++;
  1998. dprintk(CVP_WARN, "%s: retry power collapse (count %d)\n",
  1999. __func__, device->skip_pc_count);
  2000. queue_delayed_work(device->iris_pm_workq,
  2001. &iris_hfi_pm_work, msecs_to_jiffies(
  2002. device->res->msm_cvp_pwr_collapse_delay));
  2003. break;
  2004. default:
  2005. dprintk(CVP_ERR, "%s: power collapse failed\n", __func__);
  2006. break;
  2007. }
  2008. }
  2009. static int __power_collapse(struct iris_hfi_device *device, bool force)
  2010. {
  2011. int rc = 0;
  2012. u32 wfi_status = 0, idle_status = 0, pc_ready = 0;
  2013. u32 flags = 0;
  2014. int count = 0;
  2015. const int max_tries = 150;
  2016. if (!device) {
  2017. dprintk(CVP_ERR, "%s: invalid params\n", __func__);
  2018. return -EINVAL;
  2019. }
  2020. if (!device->power_enabled) {
  2021. dprintk(CVP_PWR, "%s: Power already disabled\n",
  2022. __func__);
  2023. goto exit;
  2024. }
  2025. rc = __core_in_valid_state(device);
  2026. if (!rc) {
  2027. dprintk(CVP_WARN,
  2028. "Core is in bad state, Skipping power collapse\n");
  2029. return -EINVAL;
  2030. }
  2031. rc = __dsp_suspend(device, force, flags);
  2032. if (rc == -EBUSY)
  2033. goto exit;
  2034. else if (rc)
  2035. goto skip_power_off;
  2036. pc_ready = __read_register(device, CVP_CTRL_STATUS) &
  2037. CVP_CTRL_STATUS_PC_READY;
  2038. if (!pc_ready) {
  2039. wfi_status = __read_register(device,
  2040. CVP_WRAPPER_CPU_STATUS);
  2041. idle_status = __read_register(device,
  2042. CVP_CTRL_STATUS);
  2043. if (!(wfi_status & BIT(0))) {
  2044. dprintk(CVP_WARN,
  2045. "Skipping PC as wfi_status (%#x) bit not set\n",
  2046. wfi_status);
  2047. goto skip_power_off;
  2048. }
  2049. if (!(idle_status & BIT(30))) {
  2050. dprintk(CVP_WARN,
  2051. "Skipping PC as idle_status (%#x) bit not set\n",
  2052. idle_status);
  2053. goto skip_power_off;
  2054. }
  2055. rc = __prepare_pc(device);
  2056. if (rc) {
  2057. dprintk(CVP_WARN, "Failed PC %d\n", rc);
  2058. goto skip_power_off;
  2059. }
  2060. while (count < max_tries) {
  2061. wfi_status = __read_register(device,
  2062. CVP_WRAPPER_CPU_STATUS);
  2063. pc_ready = __read_register(device,
  2064. CVP_CTRL_STATUS);
  2065. if ((wfi_status & BIT(0)) && (pc_ready &
  2066. CVP_CTRL_STATUS_PC_READY))
  2067. break;
  2068. usleep_range(150, 250);
  2069. count++;
  2070. }
  2071. if (count == max_tries) {
  2072. dprintk(CVP_ERR,
  2073. "Skip PC. Core is not in right state (%#x, %#x)\n",
  2074. wfi_status, pc_ready);
  2075. goto skip_power_off;
  2076. }
  2077. }
  2078. __flush_debug_queue(device, device->raw_packet);
  2079. rc = __suspend(device);
  2080. if (rc)
  2081. dprintk(CVP_ERR, "Failed __suspend\n");
  2082. exit:
  2083. return rc;
  2084. skip_power_off:
  2085. dprintk(CVP_PWR, "Skip PC(%#x, %#x, %#x)\n",
  2086. wfi_status, idle_status, pc_ready);
  2087. __flush_debug_queue(device, device->raw_packet);
  2088. return -EAGAIN;
  2089. }
  2090. static void __process_sys_error(struct iris_hfi_device *device)
  2091. {
  2092. struct cvp_hfi_sfr_struct *vsfr = NULL;
  2093. vsfr = (struct cvp_hfi_sfr_struct *)device->sfr.align_virtual_addr;
  2094. if (vsfr) {
  2095. void *p = memchr(vsfr->rg_data, '\0', vsfr->bufSize);
  2096. /*
  2097. * SFR isn't guaranteed to be NULL terminated
  2098. * since SYS_ERROR indicates that Iris is in the
  2099. * process of crashing.
  2100. */
  2101. if (p == NULL)
  2102. vsfr->rg_data[vsfr->bufSize - 1] = '\0';
  2103. dprintk(CVP_ERR, "SFR Message from FW: %s\n",
  2104. vsfr->rg_data);
  2105. }
  2106. }
  2107. static void __flush_debug_queue(struct iris_hfi_device *device, u8 *packet)
  2108. {
  2109. bool local_packet = false;
  2110. enum cvp_msg_prio log_level = CVP_FW;
  2111. if (!device) {
  2112. dprintk(CVP_ERR, "%s: Invalid params\n", __func__);
  2113. return;
  2114. }
  2115. if (!packet) {
  2116. packet = kzalloc(CVP_IFACEQ_VAR_HUGE_PKT_SIZE, GFP_KERNEL);
  2117. if (!packet) {
  2118. dprintk(CVP_ERR, "In %s() Fail to allocate mem\n",
  2119. __func__);
  2120. return;
  2121. }
  2122. local_packet = true;
  2123. /*
  2124. * Local packek is used when something FATAL occurred.
  2125. * It is good to print these logs by default.
  2126. */
  2127. log_level = CVP_ERR;
  2128. }
  2129. #define SKIP_INVALID_PKT(pkt_size, payload_size, pkt_hdr_size) ({ \
  2130. if (pkt_size < pkt_hdr_size || \
  2131. payload_size < MIN_PAYLOAD_SIZE || \
  2132. payload_size > \
  2133. (pkt_size - pkt_hdr_size + sizeof(u8))) { \
  2134. dprintk(CVP_ERR, \
  2135. "%s: invalid msg size - %d\n", \
  2136. __func__, pkt->msg_size); \
  2137. continue; \
  2138. } \
  2139. })
  2140. while (!__iface_dbgq_read(device, packet)) {
  2141. struct cvp_hfi_packet_header *pkt =
  2142. (struct cvp_hfi_packet_header *) packet;
  2143. if (pkt->size < sizeof(struct cvp_hfi_packet_header)) {
  2144. dprintk(CVP_ERR, "Invalid pkt size - %s\n",
  2145. __func__);
  2146. continue;
  2147. }
  2148. if (pkt->packet_type == HFI_MSG_SYS_DEBUG) {
  2149. struct cvp_hfi_msg_sys_debug_packet *pkt =
  2150. (struct cvp_hfi_msg_sys_debug_packet *) packet;
  2151. SKIP_INVALID_PKT(pkt->size,
  2152. pkt->msg_size, sizeof(*pkt));
  2153. /*
  2154. * All fw messages starts with new line character. This
  2155. * causes dprintk to print this message in two lines
  2156. * in the kernel log. Ignoring the first character
  2157. * from the message fixes this to print it in a single
  2158. * line.
  2159. */
  2160. pkt->rg_msg_data[pkt->msg_size-1] = '\0';
  2161. dprintk(log_level, "%s", &pkt->rg_msg_data[1]);
  2162. }
  2163. }
  2164. #undef SKIP_INVALID_PKT
  2165. if (local_packet)
  2166. kfree(packet);
  2167. }
  2168. static bool __is_session_valid(struct iris_hfi_device *device,
  2169. struct cvp_hal_session *session, const char *func)
  2170. {
  2171. struct cvp_hal_session *temp = NULL;
  2172. if (!device || !session)
  2173. goto invalid;
  2174. list_for_each_entry(temp, &device->sess_head, list)
  2175. if (session == temp)
  2176. return true;
  2177. invalid:
  2178. dprintk(CVP_WARN, "%s: device %pK, invalid session %pK\n",
  2179. func, device, session);
  2180. return false;
  2181. }
  2182. static struct cvp_hal_session *__get_session(struct iris_hfi_device *device,
  2183. u32 session_id)
  2184. {
  2185. struct cvp_hal_session *temp = NULL;
  2186. list_for_each_entry(temp, &device->sess_head, list) {
  2187. if (session_id == hash32_ptr(temp))
  2188. return temp;
  2189. }
  2190. return NULL;
  2191. }
  2192. #define _INVALID_MSG_ "Unrecognized MSG (%#x) session (%pK), discarding\n"
  2193. #define _INVALID_STATE_ "Ignore responses from %d to %d invalid state\n"
  2194. #define _DEVFREQ_FAIL_ "Failed to add devfreq device bus %s governor %s: %d\n"
  2195. static void process_system_msg(struct msm_cvp_cb_info *info,
  2196. struct iris_hfi_device *device,
  2197. void *raw_packet)
  2198. {
  2199. struct cvp_hal_sys_init_done sys_init_done = {0};
  2200. switch (info->response_type) {
  2201. case HAL_SYS_ERROR:
  2202. __process_sys_error(device);
  2203. break;
  2204. case HAL_SYS_RELEASE_RESOURCE_DONE:
  2205. dprintk(CVP_CORE, "Received SYS_RELEASE_RESOURCE\n");
  2206. break;
  2207. case HAL_SYS_INIT_DONE:
  2208. dprintk(CVP_CORE, "Received SYS_INIT_DONE\n");
  2209. sys_init_done.capabilities =
  2210. device->sys_init_capabilities;
  2211. cvp_hfi_process_sys_init_done_prop_read(
  2212. (struct cvp_hfi_msg_sys_init_done_packet *)
  2213. raw_packet, &sys_init_done);
  2214. info->response.cmd.data.sys_init_done = sys_init_done;
  2215. break;
  2216. default:
  2217. break;
  2218. }
  2219. }
  2220. static void **get_session_id(struct msm_cvp_cb_info *info)
  2221. {
  2222. void **session_id = NULL;
  2223. /* For session-related packets, validate session */
  2224. switch (info->response_type) {
  2225. case HAL_SESSION_INIT_DONE:
  2226. case HAL_SESSION_END_DONE:
  2227. case HAL_SESSION_ABORT_DONE:
  2228. case HAL_SESSION_STOP_DONE:
  2229. case HAL_SESSION_FLUSH_DONE:
  2230. case HAL_SESSION_SET_BUFFER_DONE:
  2231. case HAL_SESSION_SUSPEND_DONE:
  2232. case HAL_SESSION_RESUME_DONE:
  2233. case HAL_SESSION_SET_PROP_DONE:
  2234. case HAL_SESSION_GET_PROP_DONE:
  2235. case HAL_SESSION_RELEASE_BUFFER_DONE:
  2236. case HAL_SESSION_REGISTER_BUFFER_DONE:
  2237. case HAL_SESSION_UNREGISTER_BUFFER_DONE:
  2238. case HAL_SESSION_PROPERTY_INFO:
  2239. case HAL_SESSION_EVENT_CHANGE:
  2240. case HAL_SESSION_DUMP_NOTIFY:
  2241. session_id = &info->response.cmd.session_id;
  2242. break;
  2243. case HAL_SESSION_ERROR:
  2244. session_id = &info->response.data.session_id;
  2245. break;
  2246. case HAL_RESPONSE_UNUSED:
  2247. default:
  2248. session_id = NULL;
  2249. break;
  2250. }
  2251. return session_id;
  2252. }
  2253. static void print_msg_hdr(void *hdr)
  2254. {
  2255. struct cvp_hfi_msg_session_hdr *new_hdr =
  2256. (struct cvp_hfi_msg_session_hdr *)hdr;
  2257. dprintk(CVP_HFI, "HFI MSG received: %x %x %x %x %x %x %x\n",
  2258. new_hdr->size, new_hdr->packet_type,
  2259. new_hdr->session_id,
  2260. new_hdr->client_data.transaction_id,
  2261. new_hdr->client_data.data1,
  2262. new_hdr->client_data.data2,
  2263. new_hdr->error_type);
  2264. }
  2265. static int __response_handler(struct iris_hfi_device *device)
  2266. {
  2267. struct msm_cvp_cb_info *packets;
  2268. int packet_count = 0;
  2269. u8 *raw_packet = NULL;
  2270. bool requeue_pm_work = true;
  2271. if (!device || device->state != IRIS_STATE_INIT)
  2272. return 0;
  2273. packets = device->response_pkt;
  2274. raw_packet = device->raw_packet;
  2275. if (!raw_packet || !packets) {
  2276. dprintk(CVP_ERR,
  2277. "%s: Invalid args : Res pkt = %pK, Raw pkt = %pK\n",
  2278. __func__, packets, raw_packet);
  2279. return 0;
  2280. }
  2281. if (device->intr_status & CVP_FATAL_INTR_BMSK) {
  2282. struct cvp_hfi_sfr_struct *vsfr = (struct cvp_hfi_sfr_struct *)
  2283. device->sfr.align_virtual_addr;
  2284. struct msm_cvp_cb_info info = {
  2285. .response_type = HAL_SYS_WATCHDOG_TIMEOUT,
  2286. .response.cmd = {
  2287. .device_id = device->device_id,
  2288. }
  2289. };
  2290. if (vsfr)
  2291. dprintk(CVP_ERR, "SFR Message from FW: %s\n",
  2292. vsfr->rg_data);
  2293. if (device->intr_status & CVP_WRAPPER_INTR_MASK_CPU_NOC_BMSK)
  2294. dprintk(CVP_ERR, "Received Xtensa NOC error\n");
  2295. if (device->intr_status & CVP_WRAPPER_INTR_MASK_CORE_NOC_BMSK)
  2296. dprintk(CVP_ERR, "Received CVP core NOC error\n");
  2297. if (device->intr_status & CVP_WRAPPER_INTR_MASK_A2HWD_BMSK)
  2298. dprintk(CVP_ERR, "Received CVP watchdog timeout\n");
  2299. packets[packet_count++] = info;
  2300. goto exit;
  2301. }
  2302. /* Bleed the msg queue dry of packets */
  2303. while (!__iface_msgq_read(device, raw_packet)) {
  2304. void **session_id = NULL;
  2305. struct msm_cvp_cb_info *info = &packets[packet_count++];
  2306. struct cvp_hfi_msg_session_hdr *hdr =
  2307. (struct cvp_hfi_msg_session_hdr *)raw_packet;
  2308. int rc = 0;
  2309. print_msg_hdr(hdr);
  2310. rc = cvp_hfi_process_msg_packet(device->device_id,
  2311. raw_packet, info);
  2312. if (rc) {
  2313. dprintk(CVP_WARN,
  2314. "Corrupt/unknown packet found, discarding\n");
  2315. --packet_count;
  2316. continue;
  2317. } else if (info->response_type == HAL_NO_RESP) {
  2318. --packet_count;
  2319. continue;
  2320. }
  2321. /* Process the packet types that we're interested in */
  2322. process_system_msg(info, device, raw_packet);
  2323. session_id = get_session_id(info);
  2324. /*
  2325. * hfi_process_msg_packet provides a session_id that's a hashed
  2326. * value of struct cvp_hal_session, we need to coerce the hashed
  2327. * value back to pointer that we can use. Ideally, hfi_process\
  2328. * _msg_packet should take care of this, but it doesn't have
  2329. * required information for it
  2330. */
  2331. if (session_id) {
  2332. struct cvp_hal_session *session = NULL;
  2333. if (upper_32_bits((uintptr_t)*session_id) != 0) {
  2334. dprintk(CVP_ERR,
  2335. "Upper 32-bits != 0 for sess_id=%pK\n",
  2336. *session_id);
  2337. }
  2338. session = __get_session(device,
  2339. (u32)(uintptr_t)*session_id);
  2340. if (!session) {
  2341. dprintk(CVP_ERR, _INVALID_MSG_,
  2342. info->response_type,
  2343. *session_id);
  2344. --packet_count;
  2345. continue;
  2346. }
  2347. *session_id = session->session_id;
  2348. }
  2349. if (packet_count >= cvp_max_packets) {
  2350. dprintk(CVP_WARN,
  2351. "Too many packets in message queue!\n");
  2352. break;
  2353. }
  2354. /* do not read packets after sys error packet */
  2355. if (info->response_type == HAL_SYS_ERROR)
  2356. break;
  2357. }
  2358. if (requeue_pm_work && device->res->sw_power_collapsible) {
  2359. cancel_delayed_work(&iris_hfi_pm_work);
  2360. if (!queue_delayed_work(device->iris_pm_workq,
  2361. &iris_hfi_pm_work,
  2362. msecs_to_jiffies(
  2363. device->res->msm_cvp_pwr_collapse_delay))) {
  2364. dprintk(CVP_ERR, "PM work already scheduled\n");
  2365. }
  2366. }
  2367. exit:
  2368. __flush_debug_queue(device, raw_packet);
  2369. return packet_count;
  2370. }
  2371. static void iris_hfi_core_work_handler(struct work_struct *work)
  2372. {
  2373. struct msm_cvp_core *core;
  2374. struct iris_hfi_device *device;
  2375. int num_responses = 0, i = 0;
  2376. u32 intr_status;
  2377. static bool warning_on = true;
  2378. core = list_first_entry(&cvp_driver->cores, struct msm_cvp_core, list);
  2379. if (core)
  2380. device = core->device->hfi_device_data;
  2381. else
  2382. return;
  2383. mutex_lock(&device->lock);
  2384. if (!__core_in_valid_state(device)) {
  2385. if (warning_on) {
  2386. dprintk(CVP_WARN, "%s Core not in init state\n",
  2387. __func__);
  2388. warning_on = false;
  2389. }
  2390. goto err_no_work;
  2391. }
  2392. warning_on = true;
  2393. if (!device->callback) {
  2394. dprintk(CVP_ERR, "No interrupt callback function: %pK\n",
  2395. device);
  2396. goto err_no_work;
  2397. }
  2398. if (__resume(device)) {
  2399. dprintk(CVP_ERR, "%s: Power enable failed\n", __func__);
  2400. goto err_no_work;
  2401. }
  2402. __core_clear_interrupt(device);
  2403. num_responses = __response_handler(device);
  2404. dprintk(CVP_HFI, "%s:: cvp_driver_debug num_responses = %d ",
  2405. __func__, num_responses);
  2406. err_no_work:
  2407. /* Keep the interrupt status before releasing device lock */
  2408. intr_status = device->intr_status;
  2409. mutex_unlock(&device->lock);
  2410. /*
  2411. * Issue the callbacks outside of the locked contex to preserve
  2412. * re-entrancy.
  2413. */
  2414. for (i = 0; !IS_ERR_OR_NULL(device->response_pkt) &&
  2415. i < num_responses; ++i) {
  2416. struct msm_cvp_cb_info *r = &device->response_pkt[i];
  2417. void *rsp = (void *)&r->response;
  2418. if (!__core_in_valid_state(device)) {
  2419. dprintk(CVP_ERR,
  2420. _INVALID_STATE_, (i + 1), num_responses);
  2421. break;
  2422. }
  2423. dprintk(CVP_HFI, "Processing response %d of %d, type %d\n",
  2424. (i + 1), num_responses, r->response_type);
  2425. device->callback(r->response_type, rsp);
  2426. }
  2427. /* We need re-enable the irq which was disabled in ISR handler */
  2428. if (!(intr_status & CVP_WRAPPER_INTR_STATUS_A2HWD_BMSK))
  2429. enable_irq(device->cvp_hal_data->irq);
  2430. /*
  2431. * XXX: Don't add any code beyond here. Reacquiring locks after release
  2432. * it above doesn't guarantee the atomicity that we're aiming for.
  2433. */
  2434. }
  2435. static DECLARE_WORK(iris_hfi_work, iris_hfi_core_work_handler);
  2436. static irqreturn_t iris_hfi_isr(int irq, void *dev)
  2437. {
  2438. struct iris_hfi_device *device = dev;
  2439. disable_irq_nosync(irq);
  2440. queue_work(device->cvp_workq, &iris_hfi_work);
  2441. return IRQ_HANDLED;
  2442. }
  2443. static int __init_regs_and_interrupts(struct iris_hfi_device *device,
  2444. struct msm_cvp_platform_resources *res)
  2445. {
  2446. struct cvp_hal_data *hal = NULL;
  2447. int rc = 0;
  2448. rc = __check_core_registered(device, res->firmware_base,
  2449. (u8 *)(uintptr_t)res->register_base,
  2450. res->register_size, res->irq);
  2451. if (!rc) {
  2452. dprintk(CVP_ERR, "Core present/Already added\n");
  2453. rc = -EEXIST;
  2454. goto err_core_init;
  2455. }
  2456. hal = kzalloc(sizeof(*hal), GFP_KERNEL);
  2457. if (!hal) {
  2458. dprintk(CVP_ERR, "Failed to alloc\n");
  2459. rc = -ENOMEM;
  2460. goto err_core_init;
  2461. }
  2462. hal->irq = res->irq;
  2463. hal->firmware_base = res->firmware_base;
  2464. hal->register_base = devm_ioremap(&res->pdev->dev,
  2465. res->register_base, res->register_size);
  2466. hal->register_size = res->register_size;
  2467. if (!hal->register_base) {
  2468. dprintk(CVP_ERR,
  2469. "could not map reg addr %pa of size %d\n",
  2470. &res->register_base, res->register_size);
  2471. goto error_irq_fail;
  2472. }
  2473. if (res->gcc_reg_base) {
  2474. hal->gcc_reg_base = devm_ioremap(&res->pdev->dev,
  2475. res->gcc_reg_base, res->gcc_reg_size);
  2476. hal->gcc_reg_size = res->gcc_reg_size;
  2477. if (!hal->gcc_reg_base)
  2478. dprintk(CVP_ERR,
  2479. "could not map gcc reg addr %pa of size %d\n",
  2480. &res->gcc_reg_base, res->gcc_reg_size);
  2481. }
  2482. device->cvp_hal_data = hal;
  2483. rc = request_irq(res->irq, iris_hfi_isr, IRQF_TRIGGER_HIGH,
  2484. "msm_cvp", device);
  2485. if (unlikely(rc)) {
  2486. dprintk(CVP_ERR, "() :request_irq failed\n");
  2487. goto error_irq_fail;
  2488. }
  2489. disable_irq_nosync(res->irq);
  2490. dprintk(CVP_INFO,
  2491. "firmware_base = %pa, register_base = %pa, register_size = %d\n",
  2492. &res->firmware_base, &res->register_base,
  2493. res->register_size);
  2494. return rc;
  2495. error_irq_fail:
  2496. kfree(hal);
  2497. err_core_init:
  2498. return rc;
  2499. }
  2500. static int __handle_reset_clk(struct msm_cvp_platform_resources *res,
  2501. int reset_index, enum reset_state state,
  2502. enum power_state pwr_state)
  2503. {
  2504. int rc = 0;
  2505. struct reset_control *rst;
  2506. struct reset_info rst_info;
  2507. struct reset_set *rst_set = &res->reset_set;
  2508. if (!rst_set->reset_tbl)
  2509. return 0;
  2510. rst_info = rst_set->reset_tbl[reset_index];
  2511. rst = rst_info.rst;
  2512. dprintk(CVP_PWR, "reset_clk: name %s reset_state %d rst %pK ps=%d\n",
  2513. rst_set->reset_tbl[reset_index].name, state, rst, pwr_state);
  2514. switch (state) {
  2515. case INIT:
  2516. if (rst)
  2517. goto skip_reset_init;
  2518. rst = devm_reset_control_get(&res->pdev->dev,
  2519. rst_set->reset_tbl[reset_index].name);
  2520. if (IS_ERR(rst))
  2521. rc = PTR_ERR(rst);
  2522. rst_set->reset_tbl[reset_index].rst = rst;
  2523. break;
  2524. case ASSERT:
  2525. if (!rst) {
  2526. rc = PTR_ERR(rst);
  2527. goto failed_to_reset;
  2528. }
  2529. if (pwr_state != rst_info.required_state)
  2530. break;
  2531. rc = reset_control_assert(rst);
  2532. break;
  2533. case DEASSERT:
  2534. if (!rst) {
  2535. rc = PTR_ERR(rst);
  2536. goto failed_to_reset;
  2537. }
  2538. if (pwr_state != rst_info.required_state)
  2539. break;
  2540. rc = reset_control_deassert(rst);
  2541. break;
  2542. default:
  2543. dprintk(CVP_ERR, "Invalid reset request\n");
  2544. if (rc)
  2545. goto failed_to_reset;
  2546. }
  2547. return 0;
  2548. skip_reset_init:
  2549. failed_to_reset:
  2550. return rc;
  2551. }
  2552. static int reset_ahb2axi_bridge(struct iris_hfi_device *device)
  2553. {
  2554. int rc, i;
  2555. enum power_state s;
  2556. if (!device) {
  2557. dprintk(CVP_ERR, "NULL device\n");
  2558. rc = -EINVAL;
  2559. goto failed_to_reset;
  2560. }
  2561. if (device->power_enabled)
  2562. s = CVP_POWER_ON;
  2563. else
  2564. s = CVP_POWER_OFF;
  2565. for (i = 0; i < device->res->reset_set.count; i++) {
  2566. rc = __handle_reset_clk(device->res, i, ASSERT, s);
  2567. if (rc) {
  2568. dprintk(CVP_ERR,
  2569. "failed to assert reset clocks\n");
  2570. goto failed_to_reset;
  2571. }
  2572. /* wait for deassert */
  2573. usleep_range(1000, 1050);
  2574. rc = __handle_reset_clk(device->res, i, DEASSERT, s);
  2575. if (rc) {
  2576. dprintk(CVP_ERR,
  2577. "failed to deassert reset clocks\n");
  2578. goto failed_to_reset;
  2579. }
  2580. }
  2581. return 0;
  2582. failed_to_reset:
  2583. return rc;
  2584. }
  2585. static void __deinit_bus(struct iris_hfi_device *device)
  2586. {
  2587. struct bus_info *bus = NULL;
  2588. if (!device)
  2589. return;
  2590. kfree(device->bus_vote.data);
  2591. device->bus_vote = CVP_DEFAULT_BUS_VOTE;
  2592. iris_hfi_for_each_bus_reverse(device, bus) {
  2593. dev_set_drvdata(bus->dev, NULL);
  2594. icc_put(bus->client);
  2595. bus->client = NULL;
  2596. }
  2597. }
  2598. static int __init_bus(struct iris_hfi_device *device)
  2599. {
  2600. struct bus_info *bus = NULL;
  2601. int rc = 0;
  2602. if (!device)
  2603. return -EINVAL;
  2604. iris_hfi_for_each_bus(device, bus) {
  2605. /*
  2606. * This is stupid, but there's no other easy way to ahold
  2607. * of struct bus_info in iris_hfi_devfreq_*()
  2608. */
  2609. WARN(dev_get_drvdata(bus->dev), "%s's drvdata already set\n",
  2610. dev_name(bus->dev));
  2611. dev_set_drvdata(bus->dev, device);
  2612. bus->client = icc_get(&device->res->pdev->dev,
  2613. bus->master, bus->slave);
  2614. if (IS_ERR_OR_NULL(bus->client)) {
  2615. rc = PTR_ERR(bus->client) ?: -EBADHANDLE;
  2616. dprintk(CVP_ERR, "Failed to register bus %s: %d\n",
  2617. bus->name, rc);
  2618. bus->client = NULL;
  2619. goto err_add_dev;
  2620. }
  2621. }
  2622. return 0;
  2623. err_add_dev:
  2624. __deinit_bus(device);
  2625. return rc;
  2626. }
  2627. static void __deinit_regulators(struct iris_hfi_device *device)
  2628. {
  2629. struct regulator_info *rinfo = NULL;
  2630. iris_hfi_for_each_regulator_reverse(device, rinfo) {
  2631. if (rinfo->regulator) {
  2632. regulator_put(rinfo->regulator);
  2633. rinfo->regulator = NULL;
  2634. }
  2635. }
  2636. }
  2637. static int __init_regulators(struct iris_hfi_device *device)
  2638. {
  2639. int rc = 0;
  2640. struct regulator_info *rinfo = NULL;
  2641. iris_hfi_for_each_regulator(device, rinfo) {
  2642. rinfo->regulator = regulator_get(&device->res->pdev->dev,
  2643. rinfo->name);
  2644. if (IS_ERR_OR_NULL(rinfo->regulator)) {
  2645. rc = PTR_ERR(rinfo->regulator) ?: -EBADHANDLE;
  2646. dprintk(CVP_ERR, "Failed to get regulator: %s\n",
  2647. rinfo->name);
  2648. rinfo->regulator = NULL;
  2649. goto err_reg_get;
  2650. }
  2651. }
  2652. return 0;
  2653. err_reg_get:
  2654. __deinit_regulators(device);
  2655. return rc;
  2656. }
  2657. static void __deinit_subcaches(struct iris_hfi_device *device)
  2658. {
  2659. struct subcache_info *sinfo = NULL;
  2660. if (!device) {
  2661. dprintk(CVP_ERR, "deinit_subcaches: invalid device %pK\n",
  2662. device);
  2663. goto exit;
  2664. }
  2665. if (!is_sys_cache_present(device))
  2666. goto exit;
  2667. iris_hfi_for_each_subcache_reverse(device, sinfo) {
  2668. if (sinfo->subcache) {
  2669. dprintk(CVP_CORE, "deinit_subcaches: %s\n",
  2670. sinfo->name);
  2671. llcc_slice_putd(sinfo->subcache);
  2672. sinfo->subcache = NULL;
  2673. }
  2674. }
  2675. exit:
  2676. return;
  2677. }
  2678. static int __init_subcaches(struct iris_hfi_device *device)
  2679. {
  2680. int rc = 0;
  2681. struct subcache_info *sinfo = NULL;
  2682. if (!device) {
  2683. dprintk(CVP_ERR, "init_subcaches: invalid device %pK\n",
  2684. device);
  2685. return -EINVAL;
  2686. }
  2687. if (!is_sys_cache_present(device))
  2688. return 0;
  2689. iris_hfi_for_each_subcache(device, sinfo) {
  2690. if (!strcmp("cvp", sinfo->name)) {
  2691. sinfo->subcache = llcc_slice_getd(LLCC_CVP);
  2692. } else if (!strcmp("cvpfw", sinfo->name)) {
  2693. sinfo->subcache = llcc_slice_getd(LLCC_CVPFW);
  2694. } else {
  2695. dprintk(CVP_ERR, "Invalid subcache name %s\n",
  2696. sinfo->name);
  2697. }
  2698. if (IS_ERR_OR_NULL(sinfo->subcache)) {
  2699. rc = PTR_ERR(sinfo->subcache) ?
  2700. PTR_ERR(sinfo->subcache) : -EBADHANDLE;
  2701. dprintk(CVP_ERR,
  2702. "init_subcaches: invalid subcache: %s rc %d\n",
  2703. sinfo->name, rc);
  2704. sinfo->subcache = NULL;
  2705. goto err_subcache_get;
  2706. }
  2707. dprintk(CVP_CORE, "init_subcaches: %s\n",
  2708. sinfo->name);
  2709. }
  2710. return 0;
  2711. err_subcache_get:
  2712. __deinit_subcaches(device);
  2713. return rc;
  2714. }
  2715. static int __init_resources(struct iris_hfi_device *device,
  2716. struct msm_cvp_platform_resources *res)
  2717. {
  2718. int i, rc = 0;
  2719. rc = __init_regulators(device);
  2720. if (rc) {
  2721. dprintk(CVP_ERR, "Failed to get all regulators\n");
  2722. return -ENODEV;
  2723. }
  2724. rc = msm_cvp_init_clocks(device);
  2725. if (rc) {
  2726. dprintk(CVP_ERR, "Failed to init clocks\n");
  2727. rc = -ENODEV;
  2728. goto err_init_clocks;
  2729. }
  2730. for (i = 0; i < device->res->reset_set.count; i++) {
  2731. rc = __handle_reset_clk(res, i, INIT, 0);
  2732. if (rc) {
  2733. dprintk(CVP_ERR, "Failed to init reset clocks\n");
  2734. rc = -ENODEV;
  2735. goto err_init_reset_clk;
  2736. }
  2737. }
  2738. rc = __init_bus(device);
  2739. if (rc) {
  2740. dprintk(CVP_ERR, "Failed to init bus: %d\n", rc);
  2741. goto err_init_bus;
  2742. }
  2743. rc = __init_subcaches(device);
  2744. if (rc)
  2745. dprintk(CVP_WARN, "Failed to init subcaches: %d\n", rc);
  2746. device->sys_init_capabilities =
  2747. kzalloc(sizeof(struct msm_cvp_capability)
  2748. * CVP_MAX_SESSIONS, GFP_KERNEL);
  2749. return rc;
  2750. err_init_reset_clk:
  2751. err_init_bus:
  2752. msm_cvp_deinit_clocks(device);
  2753. err_init_clocks:
  2754. __deinit_regulators(device);
  2755. return rc;
  2756. }
  2757. static void __deinit_resources(struct iris_hfi_device *device)
  2758. {
  2759. __deinit_subcaches(device);
  2760. __deinit_bus(device);
  2761. msm_cvp_deinit_clocks(device);
  2762. __deinit_regulators(device);
  2763. kfree(device->sys_init_capabilities);
  2764. device->sys_init_capabilities = NULL;
  2765. }
  2766. static int __disable_regulator_impl(struct regulator_info *rinfo,
  2767. struct iris_hfi_device *device)
  2768. {
  2769. int rc = 0;
  2770. dprintk(CVP_PWR, "Disabling regulator %s\n", rinfo->name);
  2771. /*
  2772. * This call is needed. Driver needs to acquire the control back
  2773. * from HW in order to disable the regualtor. Else the behavior
  2774. * is unknown.
  2775. */
  2776. rc = __acquire_regulator(rinfo, device);
  2777. if (rc) {
  2778. /*
  2779. * This is somewhat fatal, but nothing we can do
  2780. * about it. We can't disable the regulator w/o
  2781. * getting it back under s/w control
  2782. */
  2783. dprintk(CVP_WARN,
  2784. "Failed to acquire control on %s\n",
  2785. rinfo->name);
  2786. goto disable_regulator_failed;
  2787. }
  2788. rc = regulator_disable(rinfo->regulator);
  2789. if (rc) {
  2790. dprintk(CVP_WARN,
  2791. "Failed to disable %s: %d\n",
  2792. rinfo->name, rc);
  2793. goto disable_regulator_failed;
  2794. }
  2795. return 0;
  2796. disable_regulator_failed:
  2797. /* Bring attention to this issue */
  2798. msm_cvp_res_handle_fatal_hw_error(device->res, true);
  2799. return rc;
  2800. }
  2801. static int __enable_hw_power_collapse(struct iris_hfi_device *device)
  2802. {
  2803. int rc = 0;
  2804. if (!msm_cvp_fw_low_power_mode) {
  2805. dprintk(CVP_PWR, "Not enabling hardware power collapse\n");
  2806. return 0;
  2807. }
  2808. rc = __hand_off_regulators(device);
  2809. if (rc)
  2810. dprintk(CVP_WARN,
  2811. "%s : Failed to enable HW power collapse %d\n",
  2812. __func__, rc);
  2813. return rc;
  2814. }
  2815. static int __enable_regulator(struct iris_hfi_device *device,
  2816. const char *name)
  2817. {
  2818. int rc = 0;
  2819. struct regulator_info *rinfo;
  2820. iris_hfi_for_each_regulator(device, rinfo) {
  2821. if (strcmp(rinfo->name, name))
  2822. continue;
  2823. rc = regulator_enable(rinfo->regulator);
  2824. if (rc) {
  2825. dprintk(CVP_ERR, "Failed to enable %s: %d\n",
  2826. rinfo->name, rc);
  2827. return rc;
  2828. }
  2829. if (!regulator_is_enabled(rinfo->regulator)) {
  2830. dprintk(CVP_ERR,"%s: regulator %s not enabled\n",
  2831. __func__, rinfo->name);
  2832. regulator_disable(rinfo->regulator);
  2833. return -EINVAL;
  2834. }
  2835. dprintk(CVP_PWR, "Enabled regulator %s\n", rinfo->name);
  2836. return 0;
  2837. }
  2838. dprintk(CVP_ERR, "regulator %s not found\n");
  2839. return -EINVAL;
  2840. }
  2841. static int __disable_regulator(struct iris_hfi_device *device,
  2842. const char *name)
  2843. {
  2844. struct regulator_info *rinfo;
  2845. iris_hfi_for_each_regulator_reverse(device, rinfo) {
  2846. if (strcmp(rinfo->name, name))
  2847. continue;
  2848. __disable_regulator_impl(rinfo, device);
  2849. dprintk(CVP_PWR, "%s Disabled regulator %s\n", __func__, name);
  2850. return 0;
  2851. }
  2852. dprintk(CVP_ERR, "%s regulator %s not found\n", __func__, name);
  2853. return -EINVAL;
  2854. }
  2855. static int __enable_subcaches(struct iris_hfi_device *device)
  2856. {
  2857. int rc = 0;
  2858. u32 c = 0;
  2859. struct subcache_info *sinfo;
  2860. if (msm_cvp_syscache_disable || !is_sys_cache_present(device))
  2861. return 0;
  2862. /* Activate subcaches */
  2863. iris_hfi_for_each_subcache(device, sinfo) {
  2864. rc = llcc_slice_activate(sinfo->subcache);
  2865. if (rc) {
  2866. dprintk(CVP_WARN, "Failed to activate %s: %d\n",
  2867. sinfo->name, rc);
  2868. msm_cvp_res_handle_fatal_hw_error(device->res, true);
  2869. goto err_activate_fail;
  2870. }
  2871. sinfo->isactive = true;
  2872. dprintk(CVP_CORE, "Activated subcache %s\n", sinfo->name);
  2873. c++;
  2874. }
  2875. dprintk(CVP_CORE, "Activated %d Subcaches to CVP\n", c);
  2876. return 0;
  2877. err_activate_fail:
  2878. __release_subcaches(device);
  2879. __disable_subcaches(device);
  2880. return 0;
  2881. }
  2882. static int __set_subcaches(struct iris_hfi_device *device)
  2883. {
  2884. int rc = 0;
  2885. u32 c = 0;
  2886. struct subcache_info *sinfo;
  2887. u32 resource[CVP_MAX_SUBCACHE_SIZE];
  2888. struct cvp_hfi_resource_syscache_info_type *sc_res_info;
  2889. struct cvp_hfi_resource_subcache_type *sc_res;
  2890. struct cvp_resource_hdr rhdr;
  2891. if (device->res->sys_cache_res_set || msm_cvp_syscache_disable) {
  2892. dprintk(CVP_CORE, "Subcaches already set or disabled\n");
  2893. return 0;
  2894. }
  2895. memset((void *)resource, 0x0, (sizeof(u32) * CVP_MAX_SUBCACHE_SIZE));
  2896. sc_res_info = (struct cvp_hfi_resource_syscache_info_type *)resource;
  2897. sc_res = &(sc_res_info->rg_subcache_entries[0]);
  2898. iris_hfi_for_each_subcache(device, sinfo) {
  2899. if (sinfo->isactive) {
  2900. sc_res[c].size = sinfo->subcache->slice_size;
  2901. sc_res[c].sc_id = sinfo->subcache->slice_id;
  2902. c++;
  2903. }
  2904. }
  2905. /* Set resource to CVP for activated subcaches */
  2906. if (c) {
  2907. dprintk(CVP_CORE, "Setting %d Subcaches\n", c);
  2908. rhdr.resource_handle = sc_res_info; /* cookie */
  2909. rhdr.resource_id = CVP_RESOURCE_SYSCACHE;
  2910. sc_res_info->num_entries = c;
  2911. rc = __core_set_resource(device, &rhdr, (void *)sc_res_info);
  2912. if (rc) {
  2913. dprintk(CVP_WARN, "Failed to set subcaches %d\n", rc);
  2914. goto err_fail_set_subacaches;
  2915. }
  2916. iris_hfi_for_each_subcache(device, sinfo) {
  2917. if (sinfo->isactive)
  2918. sinfo->isset = true;
  2919. }
  2920. dprintk(CVP_CORE, "Set Subcaches done to CVP\n");
  2921. device->res->sys_cache_res_set = true;
  2922. }
  2923. return 0;
  2924. err_fail_set_subacaches:
  2925. __disable_subcaches(device);
  2926. return 0;
  2927. }
  2928. static int __release_subcaches(struct iris_hfi_device *device)
  2929. {
  2930. struct subcache_info *sinfo;
  2931. int rc = 0;
  2932. u32 c = 0;
  2933. u32 resource[CVP_MAX_SUBCACHE_SIZE];
  2934. struct cvp_hfi_resource_syscache_info_type *sc_res_info;
  2935. struct cvp_hfi_resource_subcache_type *sc_res;
  2936. struct cvp_resource_hdr rhdr;
  2937. if (msm_cvp_syscache_disable || !is_sys_cache_present(device))
  2938. return 0;
  2939. memset((void *)resource, 0x0, (sizeof(u32) * CVP_MAX_SUBCACHE_SIZE));
  2940. sc_res_info = (struct cvp_hfi_resource_syscache_info_type *)resource;
  2941. sc_res = &(sc_res_info->rg_subcache_entries[0]);
  2942. /* Release resource command to Iris */
  2943. iris_hfi_for_each_subcache_reverse(device, sinfo) {
  2944. if (sinfo->isset) {
  2945. /* Update the entry */
  2946. sc_res[c].size = sinfo->subcache->slice_size;
  2947. sc_res[c].sc_id = sinfo->subcache->slice_id;
  2948. c++;
  2949. sinfo->isset = false;
  2950. }
  2951. }
  2952. if (c > 0) {
  2953. dprintk(CVP_CORE, "Releasing %d subcaches\n", c);
  2954. rhdr.resource_handle = sc_res_info; /* cookie */
  2955. rhdr.resource_id = CVP_RESOURCE_SYSCACHE;
  2956. rc = __core_release_resource(device, &rhdr);
  2957. if (rc)
  2958. dprintk(CVP_WARN,
  2959. "Failed to release %d subcaches\n", c);
  2960. }
  2961. device->res->sys_cache_res_set = false;
  2962. return 0;
  2963. }
  2964. static int __disable_subcaches(struct iris_hfi_device *device)
  2965. {
  2966. struct subcache_info *sinfo;
  2967. int rc = 0;
  2968. if (msm_cvp_syscache_disable || !is_sys_cache_present(device))
  2969. return 0;
  2970. /* De-activate subcaches */
  2971. iris_hfi_for_each_subcache_reverse(device, sinfo) {
  2972. if (sinfo->isactive) {
  2973. dprintk(CVP_CORE, "De-activate subcache %s\n",
  2974. sinfo->name);
  2975. rc = llcc_slice_deactivate(sinfo->subcache);
  2976. if (rc) {
  2977. dprintk(CVP_WARN,
  2978. "Failed to de-activate %s: %d\n",
  2979. sinfo->name, rc);
  2980. }
  2981. sinfo->isactive = false;
  2982. }
  2983. }
  2984. return 0;
  2985. }
  2986. static void interrupt_init_iris2(struct iris_hfi_device *device)
  2987. {
  2988. u32 mask_val = 0;
  2989. /* All interrupts should be disabled initially 0x1F6 : Reset value */
  2990. mask_val = __read_register(device, CVP_WRAPPER_INTR_MASK);
  2991. /* Write 0 to unmask CPU and WD interrupts */
  2992. mask_val &= ~(CVP_FATAL_INTR_BMSK | CVP_WRAPPER_INTR_MASK_A2HCPU_BMSK);
  2993. __write_register(device, CVP_WRAPPER_INTR_MASK, mask_val);
  2994. dprintk(CVP_REG, "Init irq: reg: %x, mask value %x\n",
  2995. CVP_WRAPPER_INTR_MASK, mask_val);
  2996. }
  2997. static void setup_dsp_uc_memmap_vpu5(struct iris_hfi_device *device)
  2998. {
  2999. /* initialize DSP QTBL & UCREGION with CPU queues */
  3000. __write_register(device, HFI_DSP_QTBL_ADDR,
  3001. (u32)device->dsp_iface_q_table.align_device_addr);
  3002. __write_register(device, HFI_DSP_UC_REGION_ADDR,
  3003. (u32)device->dsp_iface_q_table.align_device_addr);
  3004. __write_register(device, HFI_DSP_UC_REGION_SIZE,
  3005. device->dsp_iface_q_table.mem_data.size);
  3006. }
  3007. static void clock_config_on_enable_vpu5(struct iris_hfi_device *device)
  3008. {
  3009. __write_register(device, CVP_WRAPPER_CPU_CLOCK_CONFIG, 0);
  3010. }
  3011. static int __set_ubwc_config(struct iris_hfi_device *device)
  3012. {
  3013. u8 packet[CVP_IFACEQ_VAR_SMALL_PKT_SIZE];
  3014. int rc = 0;
  3015. struct cvp_hfi_cmd_sys_set_property_packet *pkt =
  3016. (struct cvp_hfi_cmd_sys_set_property_packet *) &packet;
  3017. if (!device->res->ubwc_config)
  3018. return 0;
  3019. rc = call_hfi_pkt_op(device, sys_ubwc_config, pkt,
  3020. device->res->ubwc_config);
  3021. if (rc) {
  3022. dprintk(CVP_WARN,
  3023. "ubwc config setting to FW failed\n");
  3024. rc = -ENOTEMPTY;
  3025. goto fail_to_set_ubwc_config;
  3026. }
  3027. if (__iface_cmdq_write(device, pkt)) {
  3028. rc = -ENOTEMPTY;
  3029. goto fail_to_set_ubwc_config;
  3030. }
  3031. fail_to_set_ubwc_config:
  3032. return rc;
  3033. }
  3034. static int __power_on_controller(struct iris_hfi_device *device)
  3035. {
  3036. int rc = 0;
  3037. rc = __enable_regulator(device, "cvp");
  3038. if (rc) {
  3039. dprintk(CVP_ERR, "Failed to enable ctrler: %d\n", rc);
  3040. return rc;
  3041. }
  3042. rc = call_iris_op(device, reset_ahb2axi_bridge, device);
  3043. if (rc) {
  3044. dprintk(CVP_ERR, "Failed to reset ahb2axi: %d\n", rc);
  3045. goto fail_reset_clks;
  3046. }
  3047. rc = msm_cvp_prepare_enable_clk(device, "gcc_video_axi1");
  3048. if (rc) {
  3049. dprintk(CVP_ERR, "Failed to enable axi1 clk: %d\n", rc);
  3050. goto fail_reset_clks;
  3051. }
  3052. rc = msm_cvp_prepare_enable_clk(device, "cvp_clk");
  3053. if (rc) {
  3054. dprintk(CVP_ERR, "Failed to enable cvp_clk: %d\n", rc);
  3055. goto fail_enable_clk;
  3056. }
  3057. dprintk(CVP_PWR, "EVA controller powered on\n");
  3058. return 0;
  3059. fail_enable_clk:
  3060. msm_cvp_disable_unprepare_clk(device, "gcc_video_axi1");
  3061. fail_reset_clks:
  3062. __disable_regulator(device, "cvp");
  3063. return rc;
  3064. }
  3065. static int __power_on_core(struct iris_hfi_device *device)
  3066. {
  3067. int rc = 0;
  3068. rc = __enable_regulator(device, "cvp-core");
  3069. if (rc) {
  3070. dprintk(CVP_ERR, "Failed to enable core: %d\n", rc);
  3071. return rc;
  3072. }
  3073. rc = msm_cvp_prepare_enable_clk(device, "core_clk");
  3074. if (rc) {
  3075. dprintk(CVP_ERR, "Failed to enable core_clk: %d\n", rc);
  3076. __disable_regulator(device, "cvp-core");
  3077. return rc;
  3078. }
  3079. dprintk(CVP_PWR, "EVA core powered on\n");
  3080. return 0;
  3081. }
  3082. static int __iris_power_on(struct iris_hfi_device *device)
  3083. {
  3084. int rc = 0;
  3085. if (device->power_enabled)
  3086. return 0;
  3087. /* Vote for all hardware resources */
  3088. rc = __vote_buses(device, device->bus_vote.data,
  3089. device->bus_vote.data_count);
  3090. if (rc) {
  3091. dprintk(CVP_ERR, "Failed to vote buses, err: %d\n", rc);
  3092. goto fail_vote_buses;
  3093. }
  3094. rc = __power_on_controller(device);
  3095. if (rc)
  3096. goto fail_enable_controller;
  3097. rc = __power_on_core(device);
  3098. if (rc)
  3099. goto fail_enable_core;
  3100. rc = msm_cvp_scale_clocks(device);
  3101. if (rc) {
  3102. dprintk(CVP_WARN,
  3103. "Failed to scale clocks, perf may regress\n");
  3104. rc = 0;
  3105. } else {
  3106. dprintk(CVP_PWR, "Done with scaling\n");
  3107. }
  3108. /*Do not access registers before this point!*/
  3109. device->power_enabled = true;
  3110. /*
  3111. * Re-program all of the registers that get reset as a result of
  3112. * regulator_disable() and _enable()
  3113. */
  3114. __set_registers(device);
  3115. dprintk(CVP_CORE, "Done with register set\n");
  3116. call_iris_op(device, interrupt_init, device);
  3117. dprintk(CVP_CORE, "Done with interrupt enabling\n");
  3118. device->intr_status = 0;
  3119. enable_irq(device->cvp_hal_data->irq);
  3120. pr_info(CVP_DBG_TAG "cvp (eva) powered on\n", "pwr");
  3121. return 0;
  3122. fail_enable_core:
  3123. __power_off_controller(device);
  3124. fail_enable_controller:
  3125. __unvote_buses(device);
  3126. fail_vote_buses:
  3127. device->power_enabled = false;
  3128. return rc;
  3129. }
  3130. static inline int __suspend(struct iris_hfi_device *device)
  3131. {
  3132. int rc = 0;
  3133. if (!device) {
  3134. dprintk(CVP_ERR, "Invalid params: %pK\n", device);
  3135. return -EINVAL;
  3136. } else if (!device->power_enabled) {
  3137. dprintk(CVP_PWR, "Power already disabled\n");
  3138. return 0;
  3139. }
  3140. dprintk(CVP_PWR, "Entering suspend\n");
  3141. if (device->res->pm_qos_latency_us &&
  3142. cpu_latency_qos_request_active(&device->qos))
  3143. cpu_latency_qos_remove_request(&device->qos);
  3144. rc = __tzbsp_set_cvp_state(TZ_SUBSYS_STATE_SUSPEND);
  3145. if (rc) {
  3146. dprintk(CVP_WARN, "Failed to suspend cvp core %d\n", rc);
  3147. goto err_tzbsp_suspend;
  3148. }
  3149. __disable_subcaches(device);
  3150. call_iris_op(device, power_off, device);
  3151. return rc;
  3152. err_tzbsp_suspend:
  3153. return rc;
  3154. }
  3155. static void __print_sidebandmanager_regs(struct iris_hfi_device *device)
  3156. {
  3157. u32 sbm_ln0_low, axi_cbcr;
  3158. u32 main_sbm_ln0_low = 0xdeadbeef, main_sbm_ln0_high = 0xdeadbeef;
  3159. u32 main_sbm_ln1_high = 0xdeadbeef, cpu_cs_x2rpmh;
  3160. sbm_ln0_low =
  3161. __read_register(device, CVP_NOC_SBM_SENSELN0_LOW);
  3162. cpu_cs_x2rpmh = __read_register(device, CVP_CPU_CS_X2RPMh);
  3163. __write_register(device, CVP_CPU_CS_X2RPMh,
  3164. (cpu_cs_x2rpmh | CVP_CPU_CS_X2RPMh_SWOVERRIDE_BMSK));
  3165. usleep_range(500, 1000);
  3166. cpu_cs_x2rpmh = __read_register(device, CVP_CPU_CS_X2RPMh);
  3167. if (!(cpu_cs_x2rpmh & CVP_CPU_CS_X2RPMh_SWOVERRIDE_BMSK)) {
  3168. dprintk(CVP_WARN,
  3169. "failed set CVP_CPU_CS_X2RPMH mask %x\n",
  3170. cpu_cs_x2rpmh);
  3171. goto exit;
  3172. }
  3173. axi_cbcr = __read_gcc_register(device, CVP_GCC_VIDEO_AXI1_CBCR);
  3174. if (axi_cbcr & 0x80000000) {
  3175. dprintk(CVP_WARN, "failed to turn on AXI clock %x\n",
  3176. axi_cbcr);
  3177. goto exit;
  3178. }
  3179. main_sbm_ln0_low = __read_register(device,
  3180. CVP_NOC_MAIN_SIDEBANDMANAGER_SENSELN0_LOW);
  3181. main_sbm_ln0_high = __read_register(device,
  3182. CVP_NOC_MAIN_SIDEBANDMANAGER_SENSELN0_HIGH);
  3183. main_sbm_ln1_high = __read_register(device,
  3184. CVP_NOC_MAIN_SIDEBANDMANAGER_SENSELN1_HIGH);
  3185. exit:
  3186. cpu_cs_x2rpmh = cpu_cs_x2rpmh & (~CVP_CPU_CS_X2RPMh_SWOVERRIDE_BMSK);
  3187. __write_register(device, CVP_CPU_CS_X2RPMh, cpu_cs_x2rpmh);
  3188. dprintk(CVP_WARN, "Sidebandmanager regs %x %x %x %x %x\n",
  3189. sbm_ln0_low, main_sbm_ln0_low,
  3190. main_sbm_ln0_high, main_sbm_ln1_high,
  3191. cpu_cs_x2rpmh);
  3192. }
  3193. static int __power_off_controller(struct iris_hfi_device *device)
  3194. {
  3195. u32 lpi_status, reg_status = 0, count = 0, max_count = 1000;
  3196. /* HPG 6.2.2 Step 1 */
  3197. __write_register(device, CVP_CPU_CS_X2RPMh, 0x3);
  3198. /* HPG 6.2.2 Step 2, noc to low power */
  3199. __write_register(device, CVP_AON_WRAPPER_MVP_NOC_LPI_CONTROL, 0x1);
  3200. while (!reg_status && count < max_count) {
  3201. lpi_status =
  3202. __read_register(device,
  3203. CVP_AON_WRAPPER_MVP_NOC_LPI_STATUS);
  3204. reg_status = lpi_status & BIT(0);
  3205. /* Wait for noc lpi status to be set */
  3206. usleep_range(50, 100);
  3207. count++;
  3208. }
  3209. dprintk(CVP_PWR,
  3210. "Noc: lpi_status %x noc_status %x (count %d)\n",
  3211. lpi_status, reg_status, count);
  3212. if (count == max_count) {
  3213. u32 pc_ready, wfi_status;
  3214. wfi_status = __read_register(device, CVP_WRAPPER_CPU_STATUS);
  3215. pc_ready = __read_register(device, CVP_CTRL_STATUS);
  3216. dprintk(CVP_WARN,
  3217. "NOC not in qaccept status %x %x %x %x\n",
  3218. reg_status, lpi_status, wfi_status, pc_ready);
  3219. __print_sidebandmanager_regs(device);
  3220. }
  3221. /* HPG 6.2.2 Step 3, debug bridge to low power BYPASSED */
  3222. /* HPG 6.2.2 Step 4, debug bridge to lpi release */
  3223. __write_register(device,
  3224. CVP_WRAPPER_DEBUG_BRIDGE_LPI_CONTROL, 0x0);
  3225. lpi_status = 0x1;
  3226. count = 0;
  3227. while (lpi_status && count < max_count) {
  3228. lpi_status = __read_register(device,
  3229. CVP_WRAPPER_DEBUG_BRIDGE_LPI_STATUS);
  3230. usleep_range(50, 100);
  3231. count++;
  3232. }
  3233. dprintk(CVP_PWR,
  3234. "DBLP Release: lpi_status %d(count %d)\n",
  3235. lpi_status, count);
  3236. if (count == max_count) {
  3237. dprintk(CVP_WARN,
  3238. "DBLP Release: lpi_status %x\n", lpi_status);
  3239. }
  3240. /* HPG 6.2.2 Step 5 */
  3241. msm_cvp_disable_unprepare_clk(device, "cvp_clk");
  3242. /* HPG 6.2.2 Step 6 */
  3243. __disable_regulator(device, "cvp");
  3244. /* HPG 6.2.2 Step 7 */
  3245. msm_cvp_disable_unprepare_clk(device, "gcc_video_axi1");
  3246. return 0;
  3247. }
  3248. static int __power_off_core(struct iris_hfi_device *device)
  3249. {
  3250. u32 config, value = 0, count = 0, warn_flag = 0;
  3251. const u32 max_count = 10;
  3252. value = __read_register(device, CVP_CC_MVS1_GDSCR);
  3253. if (!(value & 0x80000000)) {
  3254. /*
  3255. * Core has been powered off by f/w.
  3256. * Check NOC reset registers to ensure
  3257. * NO outstanding NoC transactions
  3258. */
  3259. value = __read_register(device, CVP_NOC_RESET_ACK);
  3260. if (value) {
  3261. dprintk(CVP_WARN,
  3262. "Core off with NOC RESET ACK non-zero %x\n",
  3263. value);
  3264. __print_sidebandmanager_regs(device);
  3265. }
  3266. __disable_regulator(device, "cvp-core");
  3267. msm_cvp_disable_unprepare_clk(device, "core_clk");
  3268. return 0;
  3269. }
  3270. dprintk(CVP_PWR, "Driver controls Core power off now\n");
  3271. /*
  3272. * check to make sure core clock branch enabled else
  3273. * we cannot read core idle register
  3274. */
  3275. config = __read_register(device, CVP_WRAPPER_CORE_CLOCK_CONFIG);
  3276. if (config) {
  3277. dprintk(CVP_PWR,
  3278. "core clock config not enabled, enable it to access core\n");
  3279. __write_register(device, CVP_WRAPPER_CORE_CLOCK_CONFIG, 0);
  3280. }
  3281. /*
  3282. * add MNoC idle check before collapsing MVS1 per HPG update
  3283. * poll for NoC DMA idle -> HPG 6.2.1
  3284. *
  3285. */
  3286. do {
  3287. value = __read_register(device, CVP_SS_IDLE_STATUS);
  3288. if (value & 0x400000)
  3289. break;
  3290. else
  3291. usleep_range(1000, 2000);
  3292. count++;
  3293. } while (count < max_count);
  3294. if (count == max_count) {
  3295. dprintk(CVP_WARN, "Core fail to go idle %x\n", value);
  3296. warn_flag = 1;
  3297. }
  3298. /* Apply partial reset on MSF interface and wait for ACK */
  3299. __write_register(device, CVP_NOC_RESET_REQ, 0x7);
  3300. count = 0;
  3301. do {
  3302. value = __read_register(device, CVP_NOC_RESET_ACK);
  3303. if ((value & 0x7) == 0x7)
  3304. break;
  3305. else
  3306. usleep_range(100, 200);
  3307. count++;
  3308. } while (count < max_count);
  3309. if (count == max_count) {
  3310. dprintk(CVP_WARN, "Core NoC reset assert failed %x\n", value);
  3311. warn_flag = 1;
  3312. }
  3313. /* De-assert partial reset on MSF interface and wait for ACK */
  3314. __write_register(device, CVP_NOC_RESET_REQ, 0x0);
  3315. count = 0;
  3316. do {
  3317. value = __read_register(device, CVP_NOC_RESET_ACK);
  3318. if ((value & 0x1) == 0x0)
  3319. break;
  3320. else
  3321. usleep_range(100, 200);
  3322. count++;
  3323. } while (count < max_count);
  3324. if (count == max_count) {
  3325. dprintk(CVP_WARN, "Core NoC reset de-assert failed\n");
  3326. warn_flag = 1;
  3327. }
  3328. if (warn_flag)
  3329. __print_sidebandmanager_regs(device);
  3330. /* Reset both sides of 2 ahb2ahb_bridges (TZ and non-TZ) */
  3331. __write_register(device, CVP_AHB_BRIDGE_SYNC_RESET, 0x3);
  3332. __write_register(device, CVP_AHB_BRIDGE_SYNC_RESET, 0x2);
  3333. __write_register(device, CVP_AHB_BRIDGE_SYNC_RESET, 0x0);
  3334. __write_register(device, CVP_WRAPPER_CORE_CLOCK_CONFIG, config);
  3335. __disable_regulator(device, "cvp-core");
  3336. msm_cvp_disable_unprepare_clk(device, "core_clk");
  3337. return 0;
  3338. }
  3339. static void power_off_iris2(struct iris_hfi_device *device)
  3340. {
  3341. if (!device->power_enabled || !device->res->sw_power_collapsible)
  3342. return;
  3343. if (!(device->intr_status & CVP_WRAPPER_INTR_STATUS_A2HWD_BMSK))
  3344. disable_irq_nosync(device->cvp_hal_data->irq);
  3345. device->intr_status = 0;
  3346. __power_off_core(device);
  3347. __power_off_controller(device);
  3348. if (__unvote_buses(device))
  3349. dprintk(CVP_WARN, "Failed to unvote for buses\n");
  3350. /*Do not access registers after this point!*/
  3351. device->power_enabled = false;
  3352. pr_info(CVP_DBG_TAG "cvp (eva) power collapsed\n", "pwr");
  3353. }
  3354. static inline int __resume(struct iris_hfi_device *device)
  3355. {
  3356. int rc = 0;
  3357. u32 flags = 0, reg_gdsc, reg_cbcr;
  3358. if (!device) {
  3359. dprintk(CVP_ERR, "Invalid params: %pK\n", device);
  3360. return -EINVAL;
  3361. } else if (device->power_enabled) {
  3362. goto exit;
  3363. } else if (!__core_in_valid_state(device)) {
  3364. dprintk(CVP_PWR, "iris_hfi_device in deinit state.");
  3365. return -EINVAL;
  3366. }
  3367. dprintk(CVP_PWR, "Resuming from power collapse\n");
  3368. rc = __iris_power_on(device);
  3369. if (rc) {
  3370. dprintk(CVP_ERR, "Failed to power on cvp\n");
  3371. goto err_iris_power_on;
  3372. }
  3373. reg_gdsc = __read_register(device, CVP_CC_MVS1C_GDSCR);
  3374. reg_cbcr = __read_register(device, CVP_CC_MVS1C_CBCR);
  3375. if (!(reg_gdsc & 0x80000000) || (reg_cbcr & 0x80000000))
  3376. dprintk(CVP_ERR, "CVP power on failed gdsc %x cbcr %x\n",
  3377. reg_gdsc, reg_cbcr);
  3378. /* Reboot the firmware */
  3379. rc = __tzbsp_set_cvp_state(TZ_SUBSYS_STATE_RESUME);
  3380. if (rc) {
  3381. dprintk(CVP_ERR, "Failed to resume cvp core %d\n", rc);
  3382. goto err_set_cvp_state;
  3383. }
  3384. __setup_ucregion_memory_map(device);
  3385. /* Wait for boot completion */
  3386. rc = __boot_firmware(device);
  3387. if (rc) {
  3388. dprintk(CVP_ERR, "Failed to reset cvp core\n");
  3389. goto err_reset_core;
  3390. }
  3391. /*
  3392. * Work around for H/W bug, need to reprogram these registers once
  3393. * firmware is out reset
  3394. */
  3395. __set_threshold_registers(device);
  3396. if (device->res->pm_qos_latency_us)
  3397. cpu_latency_qos_add_request(&device->qos,
  3398. device->res->pm_qos_latency_us);
  3399. __sys_set_debug(device, msm_cvp_fw_debug);
  3400. __enable_subcaches(device);
  3401. __set_subcaches(device);
  3402. __dsp_resume(device, flags);
  3403. dprintk(CVP_PWR, "Resumed from power collapse\n");
  3404. exit:
  3405. /* Don't reset skip_pc_count for SYS_PC_PREP cmd */
  3406. if (device->last_packet_type != HFI_CMD_SYS_PC_PREP)
  3407. device->skip_pc_count = 0;
  3408. return rc;
  3409. err_reset_core:
  3410. __tzbsp_set_cvp_state(TZ_SUBSYS_STATE_SUSPEND);
  3411. err_set_cvp_state:
  3412. call_iris_op(device, power_off, device);
  3413. err_iris_power_on:
  3414. dprintk(CVP_ERR, "Failed to resume from power collapse\n");
  3415. return rc;
  3416. }
  3417. static int __load_fw(struct iris_hfi_device *device)
  3418. {
  3419. int rc = 0;
  3420. /* Initialize resources */
  3421. rc = __init_resources(device, device->res);
  3422. if (rc) {
  3423. dprintk(CVP_ERR, "Failed to init resources: %d\n", rc);
  3424. goto fail_init_res;
  3425. }
  3426. rc = __initialize_packetization(device);
  3427. if (rc) {
  3428. dprintk(CVP_ERR, "Failed to initialize packetization\n");
  3429. goto fail_init_pkt;
  3430. }
  3431. rc = __iris_power_on(device);
  3432. if (rc) {
  3433. dprintk(CVP_ERR, "Failed to power on iris in in load_fw\n");
  3434. goto fail_iris_power_on;
  3435. }
  3436. if ((!device->res->use_non_secure_pil && !device->res->firmware_base)
  3437. || device->res->use_non_secure_pil) {
  3438. rc = load_cvp_fw_impl(device);
  3439. if (rc)
  3440. goto fail_load_fw;
  3441. }
  3442. return rc;
  3443. fail_load_fw:
  3444. call_iris_op(device, power_off, device);
  3445. fail_iris_power_on:
  3446. fail_init_pkt:
  3447. __deinit_resources(device);
  3448. fail_init_res:
  3449. return rc;
  3450. }
  3451. static void __unload_fw(struct iris_hfi_device *device)
  3452. {
  3453. if (!device->resources.fw.cookie)
  3454. return;
  3455. cancel_delayed_work(&iris_hfi_pm_work);
  3456. if (device->state != IRIS_STATE_DEINIT)
  3457. flush_workqueue(device->iris_pm_workq);
  3458. unload_cvp_fw_impl(device);
  3459. __interface_queues_release(device);
  3460. call_iris_op(device, power_off, device);
  3461. __deinit_resources(device);
  3462. dprintk(CVP_WARN, "Firmware unloaded\n");
  3463. }
  3464. static int iris_hfi_get_fw_info(void *dev, struct cvp_hal_fw_info *fw_info)
  3465. {
  3466. int i = 0;
  3467. struct iris_hfi_device *device = dev;
  3468. if (!device || !fw_info) {
  3469. dprintk(CVP_ERR,
  3470. "%s Invalid parameter: device = %pK fw_info = %pK\n",
  3471. __func__, device, fw_info);
  3472. return -EINVAL;
  3473. }
  3474. mutex_lock(&device->lock);
  3475. while (cvp_driver->fw_version[i++] != 'V' && i < CVP_VERSION_LENGTH)
  3476. ;
  3477. if (i == CVP_VERSION_LENGTH - 1) {
  3478. dprintk(CVP_WARN, "Iris version string is not proper\n");
  3479. fw_info->version[0] = '\0';
  3480. goto fail_version_string;
  3481. }
  3482. memcpy(&fw_info->version[0], &cvp_driver->fw_version[0],
  3483. CVP_VERSION_LENGTH);
  3484. fw_info->version[CVP_VERSION_LENGTH - 1] = '\0';
  3485. fail_version_string:
  3486. dprintk(CVP_CORE, "F/W version retrieved : %s\n", fw_info->version);
  3487. fw_info->base_addr = device->cvp_hal_data->firmware_base;
  3488. fw_info->register_base = device->res->register_base;
  3489. fw_info->register_size = device->cvp_hal_data->register_size;
  3490. fw_info->irq = device->cvp_hal_data->irq;
  3491. mutex_unlock(&device->lock);
  3492. return 0;
  3493. }
  3494. static int iris_hfi_get_core_capabilities(void *dev)
  3495. {
  3496. dprintk(CVP_CORE, "%s not supported yet!\n", __func__);
  3497. return 0;
  3498. }
  3499. static const char * const mid_names[16] = {
  3500. "CVP_FW",
  3501. "ARP_DATA",
  3502. "CVP_OD_NON_PIXEL",
  3503. "CVP_OD_ORIG_PIXEL",
  3504. "CVP_OD_WR_PIXEL",
  3505. "CVP_MPU_ORIG_PIXEL",
  3506. "CVP_MPU_REF_PIXEL",
  3507. "CVP_MPU_NON_PIXEL",
  3508. "CVP_MPU_DFS",
  3509. "CVP_FDU_NON_PIXEL",
  3510. "CVP_FDU_PIXEL",
  3511. "CVP_ICA_PIXEL",
  3512. "Invalid",
  3513. "Invalid",
  3514. "Invalid",
  3515. "Invalid"
  3516. };
  3517. static void __print_reg_details(u32 val)
  3518. {
  3519. u32 mid, sid;
  3520. mid = (val >> 5) & 0xF;
  3521. sid = (val >> 2) & 0x7;
  3522. dprintk(CVP_ERR, "CVP_NOC_CORE_ERL_MAIN_ERRLOG3_LOW: %#x\n", val);
  3523. dprintk(CVP_ERR, "Sub-client:%s, SID: %d\n", mid_names[mid], sid);
  3524. }
  3525. static void __err_log(bool logging, u32 *data, const char *name, u32 val)
  3526. {
  3527. if (logging)
  3528. *data = val;
  3529. dprintk(CVP_ERR, "%s: %#x\n", name, val);
  3530. }
  3531. static void __noc_error_info_iris2(struct iris_hfi_device *device)
  3532. {
  3533. struct msm_cvp_core *core;
  3534. struct cvp_noc_log *noc_log;
  3535. u32 val = 0, regi, i;
  3536. bool log_required = false;
  3537. core = list_first_entry(&cvp_driver->cores, struct msm_cvp_core, list);
  3538. if (!core->ssr_count && core->resources.max_ssr_allowed > 1)
  3539. log_required = true;
  3540. noc_log = &core->log.noc_log;
  3541. val = __read_register(device, CVP_NOC_ERR_SWID_LOW_OFFS);
  3542. __err_log(log_required, &noc_log->err_ctrl_swid_low,
  3543. "CVP_NOC_ERL_MAIN_SWID_LOW", val);
  3544. val = __read_register(device, CVP_NOC_ERR_SWID_HIGH_OFFS);
  3545. __err_log(log_required, &noc_log->err_ctrl_swid_high,
  3546. "CVP_NOC_ERL_MAIN_SWID_HIGH", val);
  3547. val = __read_register(device, CVP_NOC_ERR_MAINCTL_LOW_OFFS);
  3548. __err_log(log_required, &noc_log->err_ctrl_mainctl_low,
  3549. "CVP_NOC_ERL_MAIN_MAINCTL_LOW", val);
  3550. val = __read_register(device, CVP_NOC_ERR_ERRVLD_LOW_OFFS);
  3551. __err_log(log_required, &noc_log->err_ctrl_errvld_low,
  3552. "CVP_NOC_ERL_MAIN_ERRVLD_LOW", val);
  3553. val = __read_register(device, CVP_NOC_ERR_ERRCLR_LOW_OFFS);
  3554. __err_log(log_required, &noc_log->err_ctrl_errclr_low,
  3555. "CVP_NOC_ERL_MAIN_ERRCLR_LOW", val);
  3556. val = __read_register(device, CVP_NOC_ERR_ERRLOG0_LOW_OFFS);
  3557. __err_log(log_required, &noc_log->err_ctrl_errlog0_low,
  3558. "CVP_NOC_ERL_MAIN_ERRLOG0_LOW", val);
  3559. val = __read_register(device, CVP_NOC_ERR_ERRLOG0_HIGH_OFFS);
  3560. __err_log(log_required, &noc_log->err_ctrl_errlog0_high,
  3561. "CVP_NOC_ERL_MAIN_ERRLOG0_HIGH", val);
  3562. val = __read_register(device, CVP_NOC_ERR_ERRLOG1_LOW_OFFS);
  3563. __err_log(log_required, &noc_log->err_ctrl_errlog1_low,
  3564. "CVP_NOC_ERL_MAIN_ERRLOG1_LOW", val);
  3565. val = __read_register(device, CVP_NOC_ERR_ERRLOG1_HIGH_OFFS);
  3566. __err_log(log_required, &noc_log->err_ctrl_errlog1_high,
  3567. "CVP_NOC_ERL_MAIN_ERRLOG1_HIGH", val);
  3568. val = __read_register(device, CVP_NOC_ERR_ERRLOG2_LOW_OFFS);
  3569. __err_log(log_required, &noc_log->err_ctrl_errlog2_low,
  3570. "CVP_NOC_ERL_MAIN_ERRLOG2_LOW", val);
  3571. val = __read_register(device, CVP_NOC_ERR_ERRLOG2_HIGH_OFFS);
  3572. __err_log(log_required, &noc_log->err_ctrl_errlog2_high,
  3573. "CVP_NOC_ERL_MAIN_ERRLOG2_HIGH", val);
  3574. val = __read_register(device, CVP_NOC_ERR_ERRLOG3_LOW_OFFS);
  3575. __err_log(log_required, &noc_log->err_ctrl_errlog3_low,
  3576. "CVP_NOC_ERL_MAIN_ERRLOG3_LOW", val);
  3577. val = __read_register(device, CVP_NOC_ERR_ERRLOG3_HIGH_OFFS);
  3578. __err_log(log_required, &noc_log->err_ctrl_errlog3_high,
  3579. "CVP_NOC_ERL_MAIN_ERRLOG3_HIGH", val);
  3580. val = __read_register(device, CVP_NOC_CORE_ERR_SWID_LOW_OFFS);
  3581. __err_log(log_required, &noc_log->err_core_swid_low,
  3582. "CVP_NOC__CORE_ERL_MAIN_SWID_LOW", val);
  3583. val = __read_register(device, CVP_NOC_CORE_ERR_SWID_HIGH_OFFS);
  3584. __err_log(log_required, &noc_log->err_core_swid_high,
  3585. "CVP_NOC_CORE_ERL_MAIN_SWID_HIGH", val);
  3586. val = __read_register(device, CVP_NOC_CORE_ERR_MAINCTL_LOW_OFFS);
  3587. __err_log(log_required, &noc_log->err_core_mainctl_low,
  3588. "CVP_NOC_CORE_ERL_MAIN_MAINCTL_LOW", val);
  3589. val = __read_register(device, CVP_NOC_CORE_ERR_ERRVLD_LOW_OFFS);
  3590. __err_log(log_required, &noc_log->err_core_errvld_low,
  3591. "CVP_NOC_CORE_ERL_MAIN_ERRVLD_LOW", val);
  3592. val = __read_register(device, CVP_NOC_CORE_ERR_ERRCLR_LOW_OFFS);
  3593. __err_log(log_required, &noc_log->err_core_errclr_low,
  3594. "CVP_NOC_CORE_ERL_MAIN_ERRCLR_LOW", val);
  3595. val = __read_register(device, CVP_NOC_CORE_ERR_ERRLOG0_LOW_OFFS);
  3596. __err_log(log_required, &noc_log->err_core_errlog0_low,
  3597. "CVP_NOC_CORE_ERL_MAIN_ERRLOG0_LOW", val);
  3598. val = __read_register(device, CVP_NOC_CORE_ERR_ERRLOG0_HIGH_OFFS);
  3599. __err_log(log_required, &noc_log->err_core_errlog0_high,
  3600. "CVP_NOC_CORE_ERL_MAIN_ERRLOG0_HIGH", val);
  3601. val = __read_register(device, CVP_NOC_CORE_ERR_ERRLOG1_LOW_OFFS);
  3602. __err_log(log_required, &noc_log->err_core_errlog1_low,
  3603. "CVP_NOC_CORE_ERL_MAIN_ERRLOG1_LOW", val);
  3604. val = __read_register(device, CVP_NOC_CORE_ERR_ERRLOG1_HIGH_OFFS);
  3605. __err_log(log_required, &noc_log->err_core_errlog1_high,
  3606. "CVP_NOC_CORE_ERL_MAIN_ERRLOG1_HIGH", val);
  3607. val = __read_register(device, CVP_NOC_CORE_ERR_ERRLOG2_LOW_OFFS);
  3608. __err_log(log_required, &noc_log->err_core_errlog2_low,
  3609. "CVP_NOC_CORE_ERL_MAIN_ERRLOG2_LOW", val);
  3610. val = __read_register(device, CVP_NOC_CORE_ERR_ERRLOG2_HIGH_OFFS);
  3611. __err_log(log_required, &noc_log->err_core_errlog2_high,
  3612. "CVP_NOC_CORE_ERL_MAIN_ERRLOG2_HIGH", val);
  3613. val = __read_register(device, CVP_NOC_CORE_ERR_ERRLOG3_LOW_OFFS);
  3614. __err_log(log_required, &noc_log->err_core_errlog3_low,
  3615. "CORE ERRLOG3_LOW, below details", val);
  3616. __print_reg_details(val);
  3617. val = __read_register(device, CVP_NOC_CORE_ERR_ERRLOG3_HIGH_OFFS);
  3618. __err_log(log_required, &noc_log->err_core_errlog3_high,
  3619. "CVP_NOC_CORE_ERL_MAIN_ERRLOG3_HIGH", val);
  3620. #define CVP_SS_CLK_HALT 0x8
  3621. #define CVP_SS_CLK_EN 0xC
  3622. #define CVP_SS_ARP_TEST_BUS_CONTROL 0x700
  3623. #define CVP_SS_ARP_TEST_BUS_REGISTER 0x704
  3624. #define CVP_DMA_TEST_BUS_CONTROL 0x66A0
  3625. #define CVP_DMA_TEST_BUS_REGISTER 0x66A4
  3626. #define CVP_VPU_WRAPPER_CORE_CONFIG 0xB0088
  3627. __write_register(device, CVP_SS_CLK_HALT, 0);
  3628. __write_register(device, CVP_SS_CLK_EN, 0x3f);
  3629. __write_register(device, CVP_VPU_WRAPPER_CORE_CONFIG, 0);
  3630. for (i = 0; i < 15; i++) {
  3631. regi = 0xC0000000 + i;
  3632. __write_register(device, CVP_SS_ARP_TEST_BUS_CONTROL, regi);
  3633. val = __read_register(device, CVP_SS_ARP_TEST_BUS_REGISTER);
  3634. noc_log->arp_test_bus[i] = val;
  3635. dprintk(CVP_ERR, "ARP_CTL:%x - %x\n", regi, val);
  3636. }
  3637. for (i = 0; i < 512; i++) {
  3638. regi = 0x40000000 + i;
  3639. __write_register(device, CVP_DMA_TEST_BUS_CONTROL, regi);
  3640. val = __read_register(device, CVP_DMA_TEST_BUS_REGISTER);
  3641. noc_log->dma_test_bus[i] = val;
  3642. dprintk(CVP_ERR, "DMA_CTL:%x - %x\n", regi, val);
  3643. }
  3644. }
  3645. static int iris_hfi_noc_error_info(void *dev)
  3646. {
  3647. struct iris_hfi_device *device;
  3648. if (!dev) {
  3649. dprintk(CVP_ERR, "%s: null device\n", __func__);
  3650. return -EINVAL;
  3651. }
  3652. device = dev;
  3653. mutex_lock(&device->lock);
  3654. dprintk(CVP_ERR, "%s: non error information\n", __func__);
  3655. call_iris_op(device, noc_error_info, device);
  3656. mutex_unlock(&device->lock);
  3657. return 0;
  3658. }
  3659. static int __initialize_packetization(struct iris_hfi_device *device)
  3660. {
  3661. int rc = 0;
  3662. if (!device || !device->res) {
  3663. dprintk(CVP_ERR, "%s - invalid param\n", __func__);
  3664. return -EINVAL;
  3665. }
  3666. device->packetization_type = HFI_PACKETIZATION_4XX;
  3667. device->pkt_ops = cvp_hfi_get_pkt_ops_handle(
  3668. device->packetization_type);
  3669. if (!device->pkt_ops) {
  3670. rc = -EINVAL;
  3671. dprintk(CVP_ERR, "Failed to get pkt_ops handle\n");
  3672. }
  3673. return rc;
  3674. }
  3675. void __init_cvp_ops(struct iris_hfi_device *device)
  3676. {
  3677. device->vpu_ops = &iris2_ops;
  3678. }
  3679. static struct iris_hfi_device *__add_device(u32 device_id,
  3680. struct msm_cvp_platform_resources *res,
  3681. hfi_cmd_response_callback callback)
  3682. {
  3683. struct iris_hfi_device *hdevice = NULL;
  3684. int rc = 0;
  3685. if (!res || !callback) {
  3686. dprintk(CVP_ERR, "Invalid Parameters\n");
  3687. return NULL;
  3688. }
  3689. dprintk(CVP_INFO, "%s: device_id: %d\n", __func__, device_id);
  3690. hdevice = kzalloc(sizeof(*hdevice), GFP_KERNEL);
  3691. if (!hdevice) {
  3692. dprintk(CVP_ERR, "failed to allocate new device\n");
  3693. goto exit;
  3694. }
  3695. hdevice->response_pkt = kmalloc_array(cvp_max_packets,
  3696. sizeof(*hdevice->response_pkt), GFP_KERNEL);
  3697. if (!hdevice->response_pkt) {
  3698. dprintk(CVP_ERR, "failed to allocate response_pkt\n");
  3699. goto err_cleanup;
  3700. }
  3701. hdevice->raw_packet =
  3702. kzalloc(CVP_IFACEQ_VAR_HUGE_PKT_SIZE, GFP_KERNEL);
  3703. if (!hdevice->raw_packet) {
  3704. dprintk(CVP_ERR, "failed to allocate raw packet\n");
  3705. goto err_cleanup;
  3706. }
  3707. rc = __init_regs_and_interrupts(hdevice, res);
  3708. if (rc)
  3709. goto err_cleanup;
  3710. hdevice->res = res;
  3711. hdevice->device_id = device_id;
  3712. hdevice->callback = callback;
  3713. __init_cvp_ops(hdevice);
  3714. hdevice->cvp_workq = create_singlethread_workqueue(
  3715. "msm_cvp_workerq_iris");
  3716. if (!hdevice->cvp_workq) {
  3717. dprintk(CVP_ERR, ": create cvp workq failed\n");
  3718. goto err_cleanup;
  3719. }
  3720. hdevice->iris_pm_workq = create_singlethread_workqueue(
  3721. "pm_workerq_iris");
  3722. if (!hdevice->iris_pm_workq) {
  3723. dprintk(CVP_ERR, ": create pm workq failed\n");
  3724. goto err_cleanup;
  3725. }
  3726. mutex_init(&hdevice->lock);
  3727. INIT_LIST_HEAD(&hdevice->sess_head);
  3728. return hdevice;
  3729. err_cleanup:
  3730. if (hdevice->iris_pm_workq)
  3731. destroy_workqueue(hdevice->iris_pm_workq);
  3732. if (hdevice->cvp_workq)
  3733. destroy_workqueue(hdevice->cvp_workq);
  3734. kfree(hdevice->response_pkt);
  3735. kfree(hdevice->raw_packet);
  3736. kfree(hdevice);
  3737. exit:
  3738. return NULL;
  3739. }
  3740. static struct iris_hfi_device *__get_device(u32 device_id,
  3741. struct msm_cvp_platform_resources *res,
  3742. hfi_cmd_response_callback callback)
  3743. {
  3744. if (!res || !callback) {
  3745. dprintk(CVP_ERR, "Invalid params: %pK %pK\n", res, callback);
  3746. return NULL;
  3747. }
  3748. return __add_device(device_id, res, callback);
  3749. }
  3750. void cvp_iris_hfi_delete_device(void *device)
  3751. {
  3752. struct msm_cvp_core *core;
  3753. struct iris_hfi_device *dev = NULL;
  3754. if (!device)
  3755. return;
  3756. core = list_first_entry(&cvp_driver->cores, struct msm_cvp_core, list);
  3757. if (core)
  3758. dev = core->device->hfi_device_data;
  3759. if (!dev)
  3760. return;
  3761. mutex_destroy(&dev->lock);
  3762. destroy_workqueue(dev->cvp_workq);
  3763. destroy_workqueue(dev->iris_pm_workq);
  3764. free_irq(dev->cvp_hal_data->irq, dev);
  3765. iounmap(dev->cvp_hal_data->register_base);
  3766. iounmap(dev->cvp_hal_data->gcc_reg_base);
  3767. kfree(dev->cvp_hal_data);
  3768. kfree(dev->response_pkt);
  3769. kfree(dev->raw_packet);
  3770. kfree(dev);
  3771. }
  3772. static int iris_hfi_validate_session(void *sess, const char *func)
  3773. {
  3774. struct cvp_hal_session *session = sess;
  3775. int rc = 0;
  3776. struct iris_hfi_device *device;
  3777. if (!session || !session->device) {
  3778. dprintk(CVP_ERR, " %s Invalid Params %pK\n", __func__, session);
  3779. return -EINVAL;
  3780. }
  3781. device = session->device;
  3782. mutex_lock(&device->lock);
  3783. if (!__is_session_valid(device, session, func))
  3784. rc = -ECONNRESET;
  3785. mutex_unlock(&device->lock);
  3786. return rc;
  3787. }
  3788. static void iris_init_hfi_callbacks(struct cvp_hfi_device *hdev)
  3789. {
  3790. hdev->core_init = iris_hfi_core_init;
  3791. hdev->core_release = iris_hfi_core_release;
  3792. hdev->core_trigger_ssr = iris_hfi_core_trigger_ssr;
  3793. hdev->session_init = iris_hfi_session_init;
  3794. hdev->session_end = iris_hfi_session_end;
  3795. hdev->session_abort = iris_hfi_session_abort;
  3796. hdev->session_clean = iris_hfi_session_clean;
  3797. hdev->session_set_buffers = iris_hfi_session_set_buffers;
  3798. hdev->session_release_buffers = iris_hfi_session_release_buffers;
  3799. hdev->session_send = iris_hfi_session_send;
  3800. hdev->session_flush = iris_hfi_session_flush;
  3801. hdev->scale_clocks = iris_hfi_scale_clocks;
  3802. hdev->vote_bus = iris_hfi_vote_buses;
  3803. hdev->get_fw_info = iris_hfi_get_fw_info;
  3804. hdev->get_core_capabilities = iris_hfi_get_core_capabilities;
  3805. hdev->suspend = iris_hfi_suspend;
  3806. hdev->resume = iris_hfi_resume;
  3807. hdev->flush_debug_queue = iris_hfi_flush_debug_queue;
  3808. hdev->noc_error_info = iris_hfi_noc_error_info;
  3809. hdev->validate_session = iris_hfi_validate_session;
  3810. }
  3811. int cvp_iris_hfi_initialize(struct cvp_hfi_device *hdev, u32 device_id,
  3812. struct msm_cvp_platform_resources *res,
  3813. hfi_cmd_response_callback callback)
  3814. {
  3815. int rc = 0;
  3816. if (!hdev || !res || !callback) {
  3817. dprintk(CVP_ERR, "Invalid params: %pK %pK %pK\n",
  3818. hdev, res, callback);
  3819. rc = -EINVAL;
  3820. goto err_iris_hfi_init;
  3821. }
  3822. hdev->hfi_device_data = __get_device(device_id, res, callback);
  3823. if (IS_ERR_OR_NULL(hdev->hfi_device_data)) {
  3824. rc = PTR_ERR(hdev->hfi_device_data) ?: -EINVAL;
  3825. goto err_iris_hfi_init;
  3826. }
  3827. iris_init_hfi_callbacks(hdev);
  3828. err_iris_hfi_init:
  3829. return rc;
  3830. }