cvp_hfi.c 117 KB

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