driver.c 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * System Control and Management Interface (SCMI) Message Protocol driver
  4. *
  5. * SCMI Message Protocol is used between the System Control Processor(SCP)
  6. * and the Application Processors(AP). The Message Handling Unit(MHU)
  7. * provides a mechanism for inter-processor communication between SCP's
  8. * Cortex M3 and AP.
  9. *
  10. * SCP offers control and management of the core/cluster power states,
  11. * various power domain DVFS including the core/cluster, certain system
  12. * clocks configuration, thermal sensors and many others.
  13. *
  14. * Copyright (C) 2018-2025 ARM Ltd.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/bitmap.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/device.h>
  20. #include <linux/export.h>
  21. #include <linux/idr.h>
  22. #include <linux/io.h>
  23. #include <linux/io-64-nonatomic-hi-lo.h>
  24. #include <linux/kernel.h>
  25. #include <linux/kmod.h>
  26. #include <linux/ktime.h>
  27. #include <linux/hashtable.h>
  28. #include <linux/list.h>
  29. #include <linux/module.h>
  30. #include <linux/of.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/processor.h>
  33. #include <linux/refcount.h>
  34. #include <linux/slab.h>
  35. #include <linux/xarray.h>
  36. #include "common.h"
  37. #include "notify.h"
  38. #include "quirks.h"
  39. #include "raw_mode.h"
  40. #define CREATE_TRACE_POINTS
  41. #include <trace/events/scmi.h>
  42. #define SCMI_VENDOR_MODULE_ALIAS_FMT "scmi-protocol-0x%02x-%s"
  43. static DEFINE_IDA(scmi_id);
  44. static DEFINE_XARRAY(scmi_protocols);
  45. /* List of all SCMI devices active in system */
  46. static LIST_HEAD(scmi_list);
  47. /* Protection for the entire list */
  48. static DEFINE_MUTEX(scmi_list_mutex);
  49. /* Track the unique id for the transfers for debug & profiling purpose */
  50. static atomic_t transfer_last_id;
  51. static struct dentry *scmi_top_dentry;
  52. /**
  53. * struct scmi_xfers_info - Structure to manage transfer information
  54. *
  55. * @xfer_alloc_table: Bitmap table for allocated messages.
  56. * Index of this bitmap table is also used for message
  57. * sequence identifier.
  58. * @xfer_lock: Protection for message allocation
  59. * @max_msg: Maximum number of messages that can be pending
  60. * @free_xfers: A free list for available to use xfers. It is initialized with
  61. * a number of xfers equal to the maximum allowed in-flight
  62. * messages.
  63. * @pending_xfers: An hashtable, indexed by msg_hdr.seq, used to keep all the
  64. * currently in-flight messages.
  65. */
  66. struct scmi_xfers_info {
  67. unsigned long *xfer_alloc_table;
  68. spinlock_t xfer_lock;
  69. int max_msg;
  70. struct hlist_head free_xfers;
  71. DECLARE_HASHTABLE(pending_xfers, SCMI_PENDING_XFERS_HT_ORDER_SZ);
  72. };
  73. /**
  74. * struct scmi_protocol_instance - Describe an initialized protocol instance.
  75. * @handle: Reference to the SCMI handle associated to this protocol instance.
  76. * @proto: A reference to the protocol descriptor.
  77. * @gid: A reference for per-protocol devres management.
  78. * @users: A refcount to track effective users of this protocol.
  79. * @priv: Reference for optional protocol private data.
  80. * @version: Protocol version supported by the platform as detected at runtime.
  81. * @negotiated_version: When the platform supports a newer protocol version,
  82. * the agent will try to negotiate with the platform the
  83. * usage of the newest version known to it, since
  84. * backward compatibility is NOT automatically assured.
  85. * This field is NON-zero when a successful negotiation
  86. * has completed.
  87. * @ph: An embedded protocol handle that will be passed down to protocol
  88. * initialization code to identify this instance.
  89. *
  90. * Each protocol is initialized independently once for each SCMI platform in
  91. * which is defined by DT and implemented by the SCMI server fw.
  92. */
  93. struct scmi_protocol_instance {
  94. const struct scmi_handle *handle;
  95. const struct scmi_protocol *proto;
  96. void *gid;
  97. refcount_t users;
  98. void *priv;
  99. unsigned int version;
  100. unsigned int negotiated_version;
  101. struct scmi_protocol_handle ph;
  102. };
  103. #define ph_to_pi(h) container_of(h, struct scmi_protocol_instance, ph)
  104. /**
  105. * struct scmi_info - Structure representing a SCMI instance
  106. *
  107. * @id: A sequence number starting from zero identifying this instance
  108. * @dev: Device pointer
  109. * @desc: SoC description for this instance
  110. * @version: SCMI revision information containing protocol version,
  111. * implementation version and (sub-)vendor identification.
  112. * @handle: Instance of SCMI handle to send to clients
  113. * @tx_minfo: Universal Transmit Message management info
  114. * @rx_minfo: Universal Receive Message management info
  115. * @tx_idr: IDR object to map protocol id to Tx channel info pointer
  116. * @rx_idr: IDR object to map protocol id to Rx channel info pointer
  117. * @protocols: IDR for protocols' instance descriptors initialized for
  118. * this SCMI instance: populated on protocol's first attempted
  119. * usage.
  120. * @protocols_mtx: A mutex to protect protocols instances initialization.
  121. * @protocols_imp: List of protocols implemented, currently maximum of
  122. * scmi_revision_info.num_protocols elements allocated by the
  123. * base protocol
  124. * @active_protocols: IDR storing device_nodes for protocols actually defined
  125. * in the DT and confirmed as implemented by fw.
  126. * @notify_priv: Pointer to private data structure specific to notifications.
  127. * @node: List head
  128. * @users: Number of users of this instance
  129. * @bus_nb: A notifier to listen for device bind/unbind on the scmi bus
  130. * @dev_req_nb: A notifier to listen for device request/unrequest on the scmi
  131. * bus
  132. * @devreq_mtx: A mutex to serialize device creation for this SCMI instance
  133. * @dbg: A pointer to debugfs related data (if any)
  134. * @raw: An opaque reference handle used by SCMI Raw mode.
  135. */
  136. struct scmi_info {
  137. int id;
  138. struct device *dev;
  139. const struct scmi_desc *desc;
  140. struct scmi_revision_info version;
  141. struct scmi_handle handle;
  142. struct scmi_xfers_info tx_minfo;
  143. struct scmi_xfers_info rx_minfo;
  144. struct idr tx_idr;
  145. struct idr rx_idr;
  146. struct idr protocols;
  147. /* Ensure mutual exclusive access to protocols instance array */
  148. struct mutex protocols_mtx;
  149. u8 *protocols_imp;
  150. struct idr active_protocols;
  151. void *notify_priv;
  152. struct list_head node;
  153. int users;
  154. struct notifier_block bus_nb;
  155. struct notifier_block dev_req_nb;
  156. /* Serialize device creation process for this instance */
  157. struct mutex devreq_mtx;
  158. struct scmi_debug_info *dbg;
  159. void *raw;
  160. };
  161. #define handle_to_scmi_info(h) container_of(h, struct scmi_info, handle)
  162. #define tx_minfo_to_scmi_info(h) container_of(h, struct scmi_info, tx_minfo)
  163. #define bus_nb_to_scmi_info(nb) container_of(nb, struct scmi_info, bus_nb)
  164. #define req_nb_to_scmi_info(nb) container_of(nb, struct scmi_info, dev_req_nb)
  165. static void scmi_rx_callback(struct scmi_chan_info *cinfo,
  166. u32 msg_hdr, void *priv);
  167. static void scmi_bad_message_trace(struct scmi_chan_info *cinfo,
  168. u32 msg_hdr, enum scmi_bad_msg err);
  169. static struct scmi_transport_core_operations scmi_trans_core_ops = {
  170. .bad_message_trace = scmi_bad_message_trace,
  171. .rx_callback = scmi_rx_callback,
  172. };
  173. static unsigned long
  174. scmi_vendor_protocol_signature(unsigned int protocol_id, char *vendor_id,
  175. char *sub_vendor_id, u32 impl_ver)
  176. {
  177. char *signature, *p;
  178. unsigned long hash = 0;
  179. /* vendor_id/sub_vendor_id guaranteed <= SCMI_SHORT_NAME_MAX_SIZE */
  180. signature = kasprintf(GFP_KERNEL, "%02X|%s|%s|0x%08X", protocol_id,
  181. vendor_id ?: "", sub_vendor_id ?: "", impl_ver);
  182. if (!signature)
  183. return 0;
  184. p = signature;
  185. while (*p)
  186. hash = partial_name_hash(tolower(*p++), hash);
  187. hash = end_name_hash(hash);
  188. kfree(signature);
  189. return hash;
  190. }
  191. static unsigned long
  192. scmi_protocol_key_calculate(int protocol_id, char *vendor_id,
  193. char *sub_vendor_id, u32 impl_ver)
  194. {
  195. if (protocol_id < SCMI_PROTOCOL_VENDOR_BASE)
  196. return protocol_id;
  197. else
  198. return scmi_vendor_protocol_signature(protocol_id, vendor_id,
  199. sub_vendor_id, impl_ver);
  200. }
  201. static const struct scmi_protocol *
  202. __scmi_vendor_protocol_lookup(int protocol_id, char *vendor_id,
  203. char *sub_vendor_id, u32 impl_ver)
  204. {
  205. unsigned long key;
  206. struct scmi_protocol *proto = NULL;
  207. key = scmi_protocol_key_calculate(protocol_id, vendor_id,
  208. sub_vendor_id, impl_ver);
  209. if (key)
  210. proto = xa_load(&scmi_protocols, key);
  211. return proto;
  212. }
  213. static const struct scmi_protocol *
  214. scmi_vendor_protocol_lookup(int protocol_id, char *vendor_id,
  215. char *sub_vendor_id, u32 impl_ver)
  216. {
  217. const struct scmi_protocol *proto = NULL;
  218. /* Searching for closest match ...*/
  219. proto = __scmi_vendor_protocol_lookup(protocol_id, vendor_id,
  220. sub_vendor_id, impl_ver);
  221. if (proto)
  222. return proto;
  223. /* Any match just on vendor/sub_vendor ? */
  224. if (impl_ver) {
  225. proto = __scmi_vendor_protocol_lookup(protocol_id, vendor_id,
  226. sub_vendor_id, 0);
  227. if (proto)
  228. return proto;
  229. }
  230. /* Any match just on the vendor ? */
  231. if (sub_vendor_id)
  232. proto = __scmi_vendor_protocol_lookup(protocol_id, vendor_id,
  233. NULL, 0);
  234. return proto;
  235. }
  236. static const struct scmi_protocol *
  237. scmi_vendor_protocol_get(int protocol_id, struct scmi_revision_info *version)
  238. {
  239. const struct scmi_protocol *proto;
  240. proto = scmi_vendor_protocol_lookup(protocol_id, version->vendor_id,
  241. version->sub_vendor_id,
  242. version->impl_ver);
  243. if (!proto) {
  244. int ret;
  245. pr_debug("Looking for '" SCMI_VENDOR_MODULE_ALIAS_FMT "'\n",
  246. protocol_id, version->vendor_id);
  247. /* Note that vendor_id is mandatory for vendor protocols */
  248. ret = request_module(SCMI_VENDOR_MODULE_ALIAS_FMT,
  249. protocol_id, version->vendor_id);
  250. if (ret) {
  251. pr_warn("Problem loading module for protocol 0x%x\n",
  252. protocol_id);
  253. return NULL;
  254. }
  255. /* Lookup again, once modules loaded */
  256. proto = scmi_vendor_protocol_lookup(protocol_id,
  257. version->vendor_id,
  258. version->sub_vendor_id,
  259. version->impl_ver);
  260. }
  261. if (proto)
  262. pr_info("Loaded SCMI Vendor Protocol 0x%x - %s %s %X\n",
  263. protocol_id, proto->vendor_id ?: "",
  264. proto->sub_vendor_id ?: "", proto->impl_ver);
  265. return proto;
  266. }
  267. static const struct scmi_protocol *
  268. scmi_protocol_get(int protocol_id, struct scmi_revision_info *version)
  269. {
  270. const struct scmi_protocol *proto = NULL;
  271. if (protocol_id < SCMI_PROTOCOL_VENDOR_BASE)
  272. proto = xa_load(&scmi_protocols, protocol_id);
  273. else
  274. proto = scmi_vendor_protocol_get(protocol_id, version);
  275. if (!proto || !try_module_get(proto->owner)) {
  276. pr_warn("SCMI Protocol 0x%x not found!\n", protocol_id);
  277. return NULL;
  278. }
  279. pr_debug("Found SCMI Protocol 0x%x\n", protocol_id);
  280. return proto;
  281. }
  282. static void scmi_protocol_put(const struct scmi_protocol *proto)
  283. {
  284. if (proto)
  285. module_put(proto->owner);
  286. }
  287. static int scmi_vendor_protocol_check(const struct scmi_protocol *proto)
  288. {
  289. if (!proto->vendor_id) {
  290. pr_err("missing vendor_id for protocol 0x%x\n", proto->id);
  291. return -EINVAL;
  292. }
  293. if (strlen(proto->vendor_id) >= SCMI_SHORT_NAME_MAX_SIZE) {
  294. pr_err("malformed vendor_id for protocol 0x%x\n", proto->id);
  295. return -EINVAL;
  296. }
  297. if (proto->sub_vendor_id &&
  298. strlen(proto->sub_vendor_id) >= SCMI_SHORT_NAME_MAX_SIZE) {
  299. pr_err("malformed sub_vendor_id for protocol 0x%x\n",
  300. proto->id);
  301. return -EINVAL;
  302. }
  303. return 0;
  304. }
  305. int scmi_protocol_register(const struct scmi_protocol *proto)
  306. {
  307. int ret;
  308. unsigned long key;
  309. if (!proto) {
  310. pr_err("invalid protocol\n");
  311. return -EINVAL;
  312. }
  313. if (!proto->instance_init) {
  314. pr_err("missing init for protocol 0x%x\n", proto->id);
  315. return -EINVAL;
  316. }
  317. if (proto->id >= SCMI_PROTOCOL_VENDOR_BASE &&
  318. scmi_vendor_protocol_check(proto))
  319. return -EINVAL;
  320. /*
  321. * Calculate a protocol key to register this protocol with the core;
  322. * key value 0 is considered invalid.
  323. */
  324. key = scmi_protocol_key_calculate(proto->id, proto->vendor_id,
  325. proto->sub_vendor_id,
  326. proto->impl_ver);
  327. if (!key)
  328. return -EINVAL;
  329. ret = xa_insert(&scmi_protocols, key, (void *)proto, GFP_KERNEL);
  330. if (ret) {
  331. pr_err("unable to allocate SCMI protocol slot for 0x%x - err %d\n",
  332. proto->id, ret);
  333. return ret;
  334. }
  335. pr_debug("Registered SCMI Protocol 0x%x - %s %s 0x%08X\n",
  336. proto->id, proto->vendor_id, proto->sub_vendor_id,
  337. proto->impl_ver);
  338. return 0;
  339. }
  340. EXPORT_SYMBOL_GPL(scmi_protocol_register);
  341. void scmi_protocol_unregister(const struct scmi_protocol *proto)
  342. {
  343. unsigned long key;
  344. key = scmi_protocol_key_calculate(proto->id, proto->vendor_id,
  345. proto->sub_vendor_id,
  346. proto->impl_ver);
  347. if (!key)
  348. return;
  349. xa_erase(&scmi_protocols, key);
  350. pr_debug("Unregistered SCMI Protocol 0x%x\n", proto->id);
  351. }
  352. EXPORT_SYMBOL_GPL(scmi_protocol_unregister);
  353. /**
  354. * scmi_create_protocol_devices - Create devices for all pending requests for
  355. * this SCMI instance.
  356. *
  357. * @np: The device node describing the protocol
  358. * @info: The SCMI instance descriptor
  359. * @prot_id: The protocol ID
  360. * @name: The optional name of the device to be created: if not provided this
  361. * call will lead to the creation of all the devices currently requested
  362. * for the specified protocol.
  363. */
  364. static void scmi_create_protocol_devices(struct device_node *np,
  365. struct scmi_info *info,
  366. int prot_id, const char *name)
  367. {
  368. mutex_lock(&info->devreq_mtx);
  369. scmi_device_create(np, info->dev, prot_id, name);
  370. mutex_unlock(&info->devreq_mtx);
  371. }
  372. static void scmi_destroy_protocol_devices(struct scmi_info *info,
  373. int prot_id, const char *name)
  374. {
  375. mutex_lock(&info->devreq_mtx);
  376. scmi_device_destroy(info->dev, prot_id, name);
  377. mutex_unlock(&info->devreq_mtx);
  378. }
  379. void scmi_notification_instance_data_set(const struct scmi_handle *handle,
  380. void *priv)
  381. {
  382. struct scmi_info *info = handle_to_scmi_info(handle);
  383. info->notify_priv = priv;
  384. /* Ensure updated protocol private date are visible */
  385. smp_wmb();
  386. }
  387. void *scmi_notification_instance_data_get(const struct scmi_handle *handle)
  388. {
  389. struct scmi_info *info = handle_to_scmi_info(handle);
  390. /* Ensure protocols_private_data has been updated */
  391. smp_rmb();
  392. return info->notify_priv;
  393. }
  394. /**
  395. * scmi_xfer_token_set - Reserve and set new token for the xfer at hand
  396. *
  397. * @minfo: Pointer to Tx/Rx Message management info based on channel type
  398. * @xfer: The xfer to act upon
  399. *
  400. * Pick the next unused monotonically increasing token and set it into
  401. * xfer->hdr.seq: picking a monotonically increasing value avoids immediate
  402. * reuse of freshly completed or timed-out xfers, thus mitigating the risk
  403. * of incorrect association of a late and expired xfer with a live in-flight
  404. * transaction, both happening to re-use the same token identifier.
  405. *
  406. * Since platform is NOT required to answer our request in-order we should
  407. * account for a few rare but possible scenarios:
  408. *
  409. * - exactly 'next_token' may be NOT available so pick xfer_id >= next_token
  410. * using find_next_zero_bit() starting from candidate next_token bit
  411. *
  412. * - all tokens ahead upto (MSG_TOKEN_ID_MASK - 1) are used in-flight but we
  413. * are plenty of free tokens at start, so try a second pass using
  414. * find_next_zero_bit() and starting from 0.
  415. *
  416. * X = used in-flight
  417. *
  418. * Normal
  419. * ------
  420. *
  421. * |- xfer_id picked
  422. * -----------+----------------------------------------------------------
  423. * | | |X|X|X| | | | | | ... ... ... ... ... ... ... ... ... ... ...|X|X|
  424. * ----------------------------------------------------------------------
  425. * ^
  426. * |- next_token
  427. *
  428. * Out-of-order pending at start
  429. * -----------------------------
  430. *
  431. * |- xfer_id picked, last_token fixed
  432. * -----+----------------------------------------------------------------
  433. * |X|X| | | | |X|X| ... ... ... ... ... ... ... ... ... ... ... ...|X| |
  434. * ----------------------------------------------------------------------
  435. * ^
  436. * |- next_token
  437. *
  438. *
  439. * Out-of-order pending at end
  440. * ---------------------------
  441. *
  442. * |- xfer_id picked, last_token fixed
  443. * -----+----------------------------------------------------------------
  444. * |X|X| | | | |X|X| ... ... ... ... ... ... ... ... ... ... |X|X|X||X|X|
  445. * ----------------------------------------------------------------------
  446. * ^
  447. * |- next_token
  448. *
  449. * Context: Assumes to be called with @xfer_lock already acquired.
  450. *
  451. * Return: 0 on Success or error
  452. */
  453. static int scmi_xfer_token_set(struct scmi_xfers_info *minfo,
  454. struct scmi_xfer *xfer)
  455. {
  456. unsigned long xfer_id, next_token;
  457. /*
  458. * Pick a candidate monotonic token in range [0, MSG_TOKEN_MAX - 1]
  459. * using the pre-allocated transfer_id as a base.
  460. * Note that the global transfer_id is shared across all message types
  461. * so there could be holes in the allocated set of monotonic sequence
  462. * numbers, but that is going to limit the effectiveness of the
  463. * mitigation only in very rare limit conditions.
  464. */
  465. next_token = (xfer->transfer_id & (MSG_TOKEN_MAX - 1));
  466. /* Pick the next available xfer_id >= next_token */
  467. xfer_id = find_next_zero_bit(minfo->xfer_alloc_table,
  468. MSG_TOKEN_MAX, next_token);
  469. if (xfer_id == MSG_TOKEN_MAX) {
  470. /*
  471. * After heavily out-of-order responses, there are no free
  472. * tokens ahead, but only at start of xfer_alloc_table so
  473. * try again from the beginning.
  474. */
  475. xfer_id = find_next_zero_bit(minfo->xfer_alloc_table,
  476. MSG_TOKEN_MAX, 0);
  477. /*
  478. * Something is wrong if we got here since there can be a
  479. * maximum number of (MSG_TOKEN_MAX - 1) in-flight messages
  480. * but we have not found any free token [0, MSG_TOKEN_MAX - 1].
  481. */
  482. if (WARN_ON_ONCE(xfer_id == MSG_TOKEN_MAX))
  483. return -ENOMEM;
  484. }
  485. /* Update +/- last_token accordingly if we skipped some hole */
  486. if (xfer_id != next_token)
  487. atomic_add((int)(xfer_id - next_token), &transfer_last_id);
  488. xfer->hdr.seq = (u16)xfer_id;
  489. return 0;
  490. }
  491. /**
  492. * scmi_xfer_token_clear - Release the token
  493. *
  494. * @minfo: Pointer to Tx/Rx Message management info based on channel type
  495. * @xfer: The xfer to act upon
  496. */
  497. static inline void scmi_xfer_token_clear(struct scmi_xfers_info *minfo,
  498. struct scmi_xfer *xfer)
  499. {
  500. clear_bit(xfer->hdr.seq, minfo->xfer_alloc_table);
  501. }
  502. /**
  503. * scmi_xfer_inflight_register_unlocked - Register the xfer as in-flight
  504. *
  505. * @xfer: The xfer to register
  506. * @minfo: Pointer to Tx/Rx Message management info based on channel type
  507. *
  508. * Note that this helper assumes that the xfer to be registered as in-flight
  509. * had been built using an xfer sequence number which still corresponds to a
  510. * free slot in the xfer_alloc_table.
  511. *
  512. * Context: Assumes to be called with @xfer_lock already acquired.
  513. */
  514. static inline void
  515. scmi_xfer_inflight_register_unlocked(struct scmi_xfer *xfer,
  516. struct scmi_xfers_info *minfo)
  517. {
  518. /* In this context minfo will be tx_minfo due to the xfer pending */
  519. struct scmi_info *info = tx_minfo_to_scmi_info(minfo);
  520. /* Set in-flight */
  521. set_bit(xfer->hdr.seq, minfo->xfer_alloc_table);
  522. hash_add(minfo->pending_xfers, &xfer->node, xfer->hdr.seq);
  523. scmi_inc_count(info->dbg, XFERS_INFLIGHT);
  524. xfer->pending = true;
  525. }
  526. /**
  527. * scmi_xfer_inflight_register - Try to register an xfer as in-flight
  528. *
  529. * @xfer: The xfer to register
  530. * @minfo: Pointer to Tx/Rx Message management info based on channel type
  531. *
  532. * Note that this helper does NOT assume anything about the sequence number
  533. * that was baked into the provided xfer, so it checks at first if it can
  534. * be mapped to a free slot and fails with an error if another xfer with the
  535. * same sequence number is currently still registered as in-flight.
  536. *
  537. * Return: 0 on Success or -EBUSY if sequence number embedded in the xfer
  538. * could not rbe mapped to a free slot in the xfer_alloc_table.
  539. */
  540. static int scmi_xfer_inflight_register(struct scmi_xfer *xfer,
  541. struct scmi_xfers_info *minfo)
  542. {
  543. int ret = 0;
  544. unsigned long flags;
  545. spin_lock_irqsave(&minfo->xfer_lock, flags);
  546. if (!test_bit(xfer->hdr.seq, minfo->xfer_alloc_table))
  547. scmi_xfer_inflight_register_unlocked(xfer, minfo);
  548. else
  549. ret = -EBUSY;
  550. spin_unlock_irqrestore(&minfo->xfer_lock, flags);
  551. return ret;
  552. }
  553. /**
  554. * scmi_xfer_raw_inflight_register - An helper to register the given xfer as in
  555. * flight on the TX channel, if possible.
  556. *
  557. * @handle: Pointer to SCMI entity handle
  558. * @xfer: The xfer to register
  559. *
  560. * Return: 0 on Success, error otherwise
  561. */
  562. int scmi_xfer_raw_inflight_register(const struct scmi_handle *handle,
  563. struct scmi_xfer *xfer)
  564. {
  565. struct scmi_info *info = handle_to_scmi_info(handle);
  566. return scmi_xfer_inflight_register(xfer, &info->tx_minfo);
  567. }
  568. /**
  569. * scmi_xfer_pending_set - Pick a proper sequence number and mark the xfer
  570. * as pending in-flight
  571. *
  572. * @xfer: The xfer to act upon
  573. * @minfo: Pointer to Tx/Rx Message management info based on channel type
  574. *
  575. * Return: 0 on Success or error otherwise
  576. */
  577. static inline int scmi_xfer_pending_set(struct scmi_xfer *xfer,
  578. struct scmi_xfers_info *minfo)
  579. {
  580. int ret;
  581. unsigned long flags;
  582. spin_lock_irqsave(&minfo->xfer_lock, flags);
  583. /* Set a new monotonic token as the xfer sequence number */
  584. ret = scmi_xfer_token_set(minfo, xfer);
  585. if (!ret)
  586. scmi_xfer_inflight_register_unlocked(xfer, minfo);
  587. spin_unlock_irqrestore(&minfo->xfer_lock, flags);
  588. return ret;
  589. }
  590. /**
  591. * scmi_xfer_get() - Allocate one message
  592. *
  593. * @handle: Pointer to SCMI entity handle
  594. * @minfo: Pointer to Tx/Rx Message management info based on channel type
  595. *
  596. * Helper function which is used by various message functions that are
  597. * exposed to clients of this driver for allocating a message traffic event.
  598. *
  599. * Picks an xfer from the free list @free_xfers (if any available) and perform
  600. * a basic initialization.
  601. *
  602. * Note that, at this point, still no sequence number is assigned to the
  603. * allocated xfer, nor it is registered as a pending transaction.
  604. *
  605. * The successfully initialized xfer is refcounted.
  606. *
  607. * Context: Holds @xfer_lock while manipulating @free_xfers.
  608. *
  609. * Return: An initialized xfer if all went fine, else pointer error.
  610. */
  611. static struct scmi_xfer *scmi_xfer_get(const struct scmi_handle *handle,
  612. struct scmi_xfers_info *minfo)
  613. {
  614. unsigned long flags;
  615. struct scmi_xfer *xfer;
  616. spin_lock_irqsave(&minfo->xfer_lock, flags);
  617. if (hlist_empty(&minfo->free_xfers)) {
  618. spin_unlock_irqrestore(&minfo->xfer_lock, flags);
  619. return ERR_PTR(-ENOMEM);
  620. }
  621. /* grab an xfer from the free_list */
  622. xfer = hlist_entry(minfo->free_xfers.first, struct scmi_xfer, node);
  623. hlist_del_init(&xfer->node);
  624. /*
  625. * Allocate transfer_id early so that can be used also as base for
  626. * monotonic sequence number generation if needed.
  627. */
  628. xfer->transfer_id = atomic_inc_return(&transfer_last_id);
  629. refcount_set(&xfer->users, 1);
  630. atomic_set(&xfer->busy, SCMI_XFER_FREE);
  631. spin_unlock_irqrestore(&minfo->xfer_lock, flags);
  632. return xfer;
  633. }
  634. /**
  635. * scmi_xfer_raw_get - Helper to get a bare free xfer from the TX channel
  636. *
  637. * @handle: Pointer to SCMI entity handle
  638. *
  639. * Note that xfer is taken from the TX channel structures.
  640. *
  641. * Return: A valid xfer on Success, or an error-pointer otherwise
  642. */
  643. struct scmi_xfer *scmi_xfer_raw_get(const struct scmi_handle *handle)
  644. {
  645. struct scmi_xfer *xfer;
  646. struct scmi_info *info = handle_to_scmi_info(handle);
  647. xfer = scmi_xfer_get(handle, &info->tx_minfo);
  648. if (!IS_ERR(xfer))
  649. xfer->flags |= SCMI_XFER_FLAG_IS_RAW;
  650. return xfer;
  651. }
  652. /**
  653. * scmi_xfer_raw_channel_get - Helper to get a reference to the proper channel
  654. * to use for a specific protocol_id Raw transaction.
  655. *
  656. * @handle: Pointer to SCMI entity handle
  657. * @protocol_id: Identifier of the protocol
  658. *
  659. * Note that in a regular SCMI stack, usually, a protocol has to be defined in
  660. * the DT to have an associated channel and be usable; but in Raw mode any
  661. * protocol in range is allowed, re-using the Base channel, so as to enable
  662. * fuzzing on any protocol without the need of a fully compiled DT.
  663. *
  664. * Return: A reference to the channel to use, or an ERR_PTR
  665. */
  666. struct scmi_chan_info *
  667. scmi_xfer_raw_channel_get(const struct scmi_handle *handle, u8 protocol_id)
  668. {
  669. struct scmi_chan_info *cinfo;
  670. struct scmi_info *info = handle_to_scmi_info(handle);
  671. cinfo = idr_find(&info->tx_idr, protocol_id);
  672. if (!cinfo) {
  673. if (protocol_id == SCMI_PROTOCOL_BASE)
  674. return ERR_PTR(-EINVAL);
  675. /* Use Base channel for protocols not defined for DT */
  676. cinfo = idr_find(&info->tx_idr, SCMI_PROTOCOL_BASE);
  677. if (!cinfo)
  678. return ERR_PTR(-EINVAL);
  679. dev_warn_once(handle->dev,
  680. "Using Base channel for protocol 0x%X\n",
  681. protocol_id);
  682. }
  683. return cinfo;
  684. }
  685. /**
  686. * __scmi_xfer_put() - Release a message
  687. *
  688. * @minfo: Pointer to Tx/Rx Message management info based on channel type
  689. * @xfer: message that was reserved by scmi_xfer_get
  690. *
  691. * After refcount check, possibly release an xfer, clearing the token slot,
  692. * removing xfer from @pending_xfers and putting it back into free_xfers.
  693. *
  694. * This holds a spinlock to maintain integrity of internal data structures.
  695. */
  696. static void
  697. __scmi_xfer_put(struct scmi_xfers_info *minfo, struct scmi_xfer *xfer)
  698. {
  699. unsigned long flags;
  700. spin_lock_irqsave(&minfo->xfer_lock, flags);
  701. if (refcount_dec_and_test(&xfer->users)) {
  702. if (xfer->pending) {
  703. struct scmi_info *info = tx_minfo_to_scmi_info(minfo);
  704. scmi_xfer_token_clear(minfo, xfer);
  705. hash_del(&xfer->node);
  706. xfer->pending = false;
  707. scmi_dec_count(info->dbg, XFERS_INFLIGHT);
  708. }
  709. xfer->flags = 0;
  710. hlist_add_head(&xfer->node, &minfo->free_xfers);
  711. }
  712. spin_unlock_irqrestore(&minfo->xfer_lock, flags);
  713. }
  714. /**
  715. * scmi_xfer_raw_put - Release an xfer that was taken by @scmi_xfer_raw_get
  716. *
  717. * @handle: Pointer to SCMI entity handle
  718. * @xfer: A reference to the xfer to put
  719. *
  720. * Note that as with other xfer_put() handlers the xfer is really effectively
  721. * released only if there are no more users on the system.
  722. */
  723. void scmi_xfer_raw_put(const struct scmi_handle *handle, struct scmi_xfer *xfer)
  724. {
  725. struct scmi_info *info = handle_to_scmi_info(handle);
  726. return __scmi_xfer_put(&info->tx_minfo, xfer);
  727. }
  728. /**
  729. * scmi_xfer_lookup_unlocked - Helper to lookup an xfer_id
  730. *
  731. * @minfo: Pointer to Tx/Rx Message management info based on channel type
  732. * @xfer_id: Token ID to lookup in @pending_xfers
  733. *
  734. * Refcounting is untouched.
  735. *
  736. * Context: Assumes to be called with @xfer_lock already acquired.
  737. *
  738. * Return: A valid xfer on Success or error otherwise
  739. */
  740. static struct scmi_xfer *
  741. scmi_xfer_lookup_unlocked(struct scmi_xfers_info *minfo, u16 xfer_id)
  742. {
  743. struct scmi_xfer *xfer = NULL;
  744. if (test_bit(xfer_id, minfo->xfer_alloc_table))
  745. xfer = XFER_FIND(minfo->pending_xfers, xfer_id);
  746. return xfer ?: ERR_PTR(-EINVAL);
  747. }
  748. /**
  749. * scmi_bad_message_trace - A helper to trace weird messages
  750. *
  751. * @cinfo: A reference to the channel descriptor on which the message was
  752. * received
  753. * @msg_hdr: Message header to track
  754. * @err: A specific error code used as a status value in traces.
  755. *
  756. * This helper can be used to trace any kind of weird, incomplete, unexpected,
  757. * timed-out message that arrives and as such, can be traced only referring to
  758. * the header content, since the payload is missing/unreliable.
  759. */
  760. static void scmi_bad_message_trace(struct scmi_chan_info *cinfo, u32 msg_hdr,
  761. enum scmi_bad_msg err)
  762. {
  763. char *tag;
  764. struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
  765. switch (MSG_XTRACT_TYPE(msg_hdr)) {
  766. case MSG_TYPE_COMMAND:
  767. tag = "!RESP";
  768. break;
  769. case MSG_TYPE_DELAYED_RESP:
  770. tag = "!DLYD";
  771. break;
  772. case MSG_TYPE_NOTIFICATION:
  773. tag = "!NOTI";
  774. break;
  775. default:
  776. tag = "!UNKN";
  777. break;
  778. }
  779. trace_scmi_msg_dump(info->id, cinfo->id,
  780. MSG_XTRACT_PROT_ID(msg_hdr),
  781. MSG_XTRACT_ID(msg_hdr), tag,
  782. MSG_XTRACT_TOKEN(msg_hdr), err, NULL, 0);
  783. }
  784. /**
  785. * scmi_msg_response_validate - Validate message type against state of related
  786. * xfer
  787. *
  788. * @cinfo: A reference to the channel descriptor.
  789. * @msg_type: Message type to check
  790. * @xfer: A reference to the xfer to validate against @msg_type
  791. *
  792. * This function checks if @msg_type is congruent with the current state of
  793. * a pending @xfer; if an asynchronous delayed response is received before the
  794. * related synchronous response (Out-of-Order Delayed Response) the missing
  795. * synchronous response is assumed to be OK and completed, carrying on with the
  796. * Delayed Response: this is done to address the case in which the underlying
  797. * SCMI transport can deliver such out-of-order responses.
  798. *
  799. * Context: Assumes to be called with xfer->lock already acquired.
  800. *
  801. * Return: 0 on Success, error otherwise
  802. */
  803. static inline int scmi_msg_response_validate(struct scmi_chan_info *cinfo,
  804. u8 msg_type,
  805. struct scmi_xfer *xfer)
  806. {
  807. /*
  808. * Even if a response was indeed expected on this slot at this point,
  809. * a buggy platform could wrongly reply feeding us an unexpected
  810. * delayed response we're not prepared to handle: bail-out safely
  811. * blaming firmware.
  812. */
  813. if (msg_type == MSG_TYPE_DELAYED_RESP && !xfer->async_done) {
  814. dev_err(cinfo->dev,
  815. "Delayed Response for %d not expected! Buggy F/W ?\n",
  816. xfer->hdr.seq);
  817. return -EINVAL;
  818. }
  819. switch (xfer->state) {
  820. case SCMI_XFER_SENT_OK:
  821. if (msg_type == MSG_TYPE_DELAYED_RESP) {
  822. /*
  823. * Delayed Response expected but delivered earlier.
  824. * Assume message RESPONSE was OK and skip state.
  825. */
  826. xfer->hdr.status = SCMI_SUCCESS;
  827. xfer->state = SCMI_XFER_RESP_OK;
  828. complete(&xfer->done);
  829. dev_warn(cinfo->dev,
  830. "Received valid OoO Delayed Response for %d\n",
  831. xfer->hdr.seq);
  832. }
  833. break;
  834. case SCMI_XFER_RESP_OK:
  835. if (msg_type != MSG_TYPE_DELAYED_RESP)
  836. return -EINVAL;
  837. break;
  838. case SCMI_XFER_DRESP_OK:
  839. /* No further message expected once in SCMI_XFER_DRESP_OK */
  840. return -EINVAL;
  841. }
  842. return 0;
  843. }
  844. /**
  845. * scmi_xfer_state_update - Update xfer state
  846. *
  847. * @xfer: A reference to the xfer to update
  848. * @msg_type: Type of message being processed.
  849. *
  850. * Note that this message is assumed to have been already successfully validated
  851. * by @scmi_msg_response_validate(), so here we just update the state.
  852. *
  853. * Context: Assumes to be called on an xfer exclusively acquired using the
  854. * busy flag.
  855. */
  856. static inline void scmi_xfer_state_update(struct scmi_xfer *xfer, u8 msg_type)
  857. {
  858. xfer->hdr.type = msg_type;
  859. /* Unknown command types were already discarded earlier */
  860. if (xfer->hdr.type == MSG_TYPE_COMMAND)
  861. xfer->state = SCMI_XFER_RESP_OK;
  862. else
  863. xfer->state = SCMI_XFER_DRESP_OK;
  864. }
  865. static bool scmi_xfer_acquired(struct scmi_xfer *xfer)
  866. {
  867. int ret;
  868. ret = atomic_cmpxchg(&xfer->busy, SCMI_XFER_FREE, SCMI_XFER_BUSY);
  869. return ret == SCMI_XFER_FREE;
  870. }
  871. /**
  872. * scmi_xfer_command_acquire - Helper to lookup and acquire a command xfer
  873. *
  874. * @cinfo: A reference to the channel descriptor.
  875. * @msg_hdr: A message header to use as lookup key
  876. *
  877. * When a valid xfer is found for the sequence number embedded in the provided
  878. * msg_hdr, reference counting is properly updated and exclusive access to this
  879. * xfer is granted till released with @scmi_xfer_command_release.
  880. *
  881. * Return: A valid @xfer on Success or error otherwise.
  882. */
  883. static inline struct scmi_xfer *
  884. scmi_xfer_command_acquire(struct scmi_chan_info *cinfo, u32 msg_hdr)
  885. {
  886. int ret;
  887. unsigned long flags;
  888. struct scmi_xfer *xfer;
  889. struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
  890. struct scmi_xfers_info *minfo = &info->tx_minfo;
  891. u8 msg_type = MSG_XTRACT_TYPE(msg_hdr);
  892. u16 xfer_id = MSG_XTRACT_TOKEN(msg_hdr);
  893. /* Are we even expecting this? */
  894. spin_lock_irqsave(&minfo->xfer_lock, flags);
  895. xfer = scmi_xfer_lookup_unlocked(minfo, xfer_id);
  896. if (IS_ERR(xfer)) {
  897. dev_err(cinfo->dev,
  898. "Message for %d type %d is not expected!\n",
  899. xfer_id, msg_type);
  900. spin_unlock_irqrestore(&minfo->xfer_lock, flags);
  901. scmi_bad_message_trace(cinfo, msg_hdr, MSG_UNEXPECTED);
  902. scmi_inc_count(info->dbg, ERR_MSG_UNEXPECTED);
  903. return xfer;
  904. }
  905. refcount_inc(&xfer->users);
  906. spin_unlock_irqrestore(&minfo->xfer_lock, flags);
  907. spin_lock_irqsave(&xfer->lock, flags);
  908. ret = scmi_msg_response_validate(cinfo, msg_type, xfer);
  909. /*
  910. * If a pending xfer was found which was also in a congruent state with
  911. * the received message, acquire exclusive access to it setting the busy
  912. * flag.
  913. * Spins only on the rare limit condition of concurrent reception of
  914. * RESP and DRESP for the same xfer.
  915. */
  916. if (!ret) {
  917. spin_until_cond(scmi_xfer_acquired(xfer));
  918. scmi_xfer_state_update(xfer, msg_type);
  919. }
  920. spin_unlock_irqrestore(&xfer->lock, flags);
  921. if (ret) {
  922. dev_err(cinfo->dev,
  923. "Invalid message type:%d for %d - HDR:0x%X state:%d\n",
  924. msg_type, xfer_id, msg_hdr, xfer->state);
  925. scmi_bad_message_trace(cinfo, msg_hdr, MSG_INVALID);
  926. scmi_inc_count(info->dbg, ERR_MSG_INVALID);
  927. /* On error the refcount incremented above has to be dropped */
  928. __scmi_xfer_put(minfo, xfer);
  929. xfer = ERR_PTR(-EINVAL);
  930. }
  931. return xfer;
  932. }
  933. static inline void scmi_xfer_command_release(struct scmi_info *info,
  934. struct scmi_xfer *xfer)
  935. {
  936. atomic_set(&xfer->busy, SCMI_XFER_FREE);
  937. __scmi_xfer_put(&info->tx_minfo, xfer);
  938. }
  939. static inline void scmi_clear_channel(struct scmi_info *info,
  940. struct scmi_chan_info *cinfo)
  941. {
  942. if (!cinfo->is_p2a) {
  943. dev_warn(cinfo->dev, "Invalid clear on A2P channel !\n");
  944. return;
  945. }
  946. if (info->desc->ops->clear_channel)
  947. info->desc->ops->clear_channel(cinfo);
  948. }
  949. static void scmi_handle_notification(struct scmi_chan_info *cinfo,
  950. u32 msg_hdr, void *priv)
  951. {
  952. struct scmi_xfer *xfer;
  953. struct device *dev = cinfo->dev;
  954. struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
  955. struct scmi_xfers_info *minfo = &info->rx_minfo;
  956. ktime_t ts;
  957. ts = ktime_get_boottime();
  958. xfer = scmi_xfer_get(cinfo->handle, minfo);
  959. if (IS_ERR(xfer)) {
  960. dev_err(dev, "failed to get free message slot (%ld)\n",
  961. PTR_ERR(xfer));
  962. scmi_bad_message_trace(cinfo, msg_hdr, MSG_NOMEM);
  963. scmi_inc_count(info->dbg, ERR_MSG_NOMEM);
  964. scmi_clear_channel(info, cinfo);
  965. return;
  966. }
  967. unpack_scmi_header(msg_hdr, &xfer->hdr);
  968. if (priv)
  969. /* Ensure order between xfer->priv store and following ops */
  970. smp_store_mb(xfer->priv, priv);
  971. info->desc->ops->fetch_notification(cinfo, info->desc->max_msg_size,
  972. xfer);
  973. trace_scmi_msg_dump(info->id, cinfo->id, xfer->hdr.protocol_id,
  974. xfer->hdr.id, "NOTI", xfer->hdr.seq,
  975. xfer->hdr.status, xfer->rx.buf, xfer->rx.len);
  976. scmi_inc_count(info->dbg, NOTIFICATION_OK);
  977. scmi_notify(cinfo->handle, xfer->hdr.protocol_id,
  978. xfer->hdr.id, xfer->rx.buf, xfer->rx.len, ts);
  979. trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id,
  980. xfer->hdr.protocol_id, xfer->hdr.seq,
  981. MSG_TYPE_NOTIFICATION);
  982. if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT)) {
  983. xfer->hdr.seq = MSG_XTRACT_TOKEN(msg_hdr);
  984. scmi_raw_message_report(info->raw, xfer, SCMI_RAW_NOTIF_QUEUE,
  985. cinfo->id);
  986. }
  987. __scmi_xfer_put(minfo, xfer);
  988. scmi_clear_channel(info, cinfo);
  989. }
  990. static void scmi_handle_response(struct scmi_chan_info *cinfo,
  991. u32 msg_hdr, void *priv)
  992. {
  993. struct scmi_xfer *xfer;
  994. struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
  995. xfer = scmi_xfer_command_acquire(cinfo, msg_hdr);
  996. if (IS_ERR(xfer)) {
  997. if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT))
  998. scmi_raw_error_report(info->raw, cinfo, msg_hdr, priv);
  999. if (MSG_XTRACT_TYPE(msg_hdr) == MSG_TYPE_DELAYED_RESP)
  1000. scmi_clear_channel(info, cinfo);
  1001. return;
  1002. }
  1003. /* rx.len could be shrunk in the sync do_xfer, so reset to maxsz */
  1004. if (xfer->hdr.type == MSG_TYPE_DELAYED_RESP)
  1005. xfer->rx.len = info->desc->max_msg_size;
  1006. if (priv)
  1007. /* Ensure order between xfer->priv store and following ops */
  1008. smp_store_mb(xfer->priv, priv);
  1009. info->desc->ops->fetch_response(cinfo, xfer);
  1010. trace_scmi_msg_dump(info->id, cinfo->id, xfer->hdr.protocol_id,
  1011. xfer->hdr.id,
  1012. xfer->hdr.type == MSG_TYPE_DELAYED_RESP ?
  1013. (!SCMI_XFER_IS_RAW(xfer) ? "DLYD" : "dlyd") :
  1014. (!SCMI_XFER_IS_RAW(xfer) ? "RESP" : "resp"),
  1015. xfer->hdr.seq, xfer->hdr.status,
  1016. xfer->rx.buf, xfer->rx.len);
  1017. trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id,
  1018. xfer->hdr.protocol_id, xfer->hdr.seq,
  1019. xfer->hdr.type);
  1020. if (xfer->hdr.type == MSG_TYPE_DELAYED_RESP) {
  1021. scmi_clear_channel(info, cinfo);
  1022. complete(xfer->async_done);
  1023. scmi_inc_count(info->dbg, DELAYED_RESPONSE_OK);
  1024. } else {
  1025. complete(&xfer->done);
  1026. scmi_inc_count(info->dbg, RESPONSE_OK);
  1027. }
  1028. if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT)) {
  1029. /*
  1030. * When in polling mode avoid to queue the Raw xfer on the IRQ
  1031. * RX path since it will be already queued at the end of the TX
  1032. * poll loop.
  1033. */
  1034. if (!xfer->hdr.poll_completion ||
  1035. xfer->hdr.type == MSG_TYPE_DELAYED_RESP)
  1036. scmi_raw_message_report(info->raw, xfer,
  1037. SCMI_RAW_REPLY_QUEUE,
  1038. cinfo->id);
  1039. }
  1040. scmi_xfer_command_release(info, xfer);
  1041. }
  1042. /**
  1043. * scmi_rx_callback() - callback for receiving messages
  1044. *
  1045. * @cinfo: SCMI channel info
  1046. * @msg_hdr: Message header
  1047. * @priv: Transport specific private data.
  1048. *
  1049. * Processes one received message to appropriate transfer information and
  1050. * signals completion of the transfer.
  1051. *
  1052. * NOTE: This function will be invoked in IRQ context, hence should be
  1053. * as optimal as possible.
  1054. */
  1055. static void scmi_rx_callback(struct scmi_chan_info *cinfo, u32 msg_hdr,
  1056. void *priv)
  1057. {
  1058. u8 msg_type = MSG_XTRACT_TYPE(msg_hdr);
  1059. switch (msg_type) {
  1060. case MSG_TYPE_NOTIFICATION:
  1061. scmi_handle_notification(cinfo, msg_hdr, priv);
  1062. break;
  1063. case MSG_TYPE_COMMAND:
  1064. case MSG_TYPE_DELAYED_RESP:
  1065. scmi_handle_response(cinfo, msg_hdr, priv);
  1066. break;
  1067. default:
  1068. WARN_ONCE(1, "received unknown msg_type:%d\n", msg_type);
  1069. scmi_bad_message_trace(cinfo, msg_hdr, MSG_UNKNOWN);
  1070. break;
  1071. }
  1072. }
  1073. /**
  1074. * xfer_put() - Release a transmit message
  1075. *
  1076. * @ph: Pointer to SCMI protocol handle
  1077. * @xfer: message that was reserved by xfer_get_init
  1078. */
  1079. static void xfer_put(const struct scmi_protocol_handle *ph,
  1080. struct scmi_xfer *xfer)
  1081. {
  1082. const struct scmi_protocol_instance *pi = ph_to_pi(ph);
  1083. struct scmi_info *info = handle_to_scmi_info(pi->handle);
  1084. __scmi_xfer_put(&info->tx_minfo, xfer);
  1085. }
  1086. static bool scmi_xfer_done_no_timeout(struct scmi_chan_info *cinfo,
  1087. struct scmi_xfer *xfer, ktime_t stop,
  1088. bool *ooo)
  1089. {
  1090. struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
  1091. /*
  1092. * Poll also on xfer->done so that polling can be forcibly terminated
  1093. * in case of out-of-order receptions of delayed responses
  1094. */
  1095. return info->desc->ops->poll_done(cinfo, xfer) ||
  1096. (*ooo = try_wait_for_completion(&xfer->done)) ||
  1097. ktime_after(ktime_get(), stop);
  1098. }
  1099. static int scmi_wait_for_reply(struct device *dev, const struct scmi_desc *desc,
  1100. struct scmi_chan_info *cinfo,
  1101. struct scmi_xfer *xfer, unsigned int timeout_ms)
  1102. {
  1103. int ret = 0;
  1104. struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
  1105. if (xfer->hdr.poll_completion) {
  1106. /*
  1107. * Real polling is needed only if transport has NOT declared
  1108. * itself to support synchronous commands replies.
  1109. */
  1110. if (!desc->sync_cmds_completed_on_ret) {
  1111. bool ooo = false;
  1112. /*
  1113. * Poll on xfer using transport provided .poll_done();
  1114. * assumes no completion interrupt was available.
  1115. */
  1116. ktime_t stop = ktime_add_ms(ktime_get(), timeout_ms);
  1117. spin_until_cond(scmi_xfer_done_no_timeout(cinfo, xfer,
  1118. stop, &ooo));
  1119. if (!ooo && !info->desc->ops->poll_done(cinfo, xfer)) {
  1120. dev_err(dev,
  1121. "timed out in resp(caller: %pS) - polling\n",
  1122. (void *)_RET_IP_);
  1123. ret = -ETIMEDOUT;
  1124. scmi_inc_count(info->dbg, XFERS_RESPONSE_POLLED_TIMEOUT);
  1125. }
  1126. }
  1127. if (!ret) {
  1128. unsigned long flags;
  1129. /*
  1130. * Do not fetch_response if an out-of-order delayed
  1131. * response is being processed.
  1132. */
  1133. spin_lock_irqsave(&xfer->lock, flags);
  1134. if (xfer->state == SCMI_XFER_SENT_OK) {
  1135. desc->ops->fetch_response(cinfo, xfer);
  1136. xfer->state = SCMI_XFER_RESP_OK;
  1137. }
  1138. spin_unlock_irqrestore(&xfer->lock, flags);
  1139. /* Trace polled replies. */
  1140. trace_scmi_msg_dump(info->id, cinfo->id,
  1141. xfer->hdr.protocol_id, xfer->hdr.id,
  1142. !SCMI_XFER_IS_RAW(xfer) ?
  1143. "RESP" : "resp",
  1144. xfer->hdr.seq, xfer->hdr.status,
  1145. xfer->rx.buf, xfer->rx.len);
  1146. scmi_inc_count(info->dbg, RESPONSE_POLLED_OK);
  1147. if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT)) {
  1148. scmi_raw_message_report(info->raw, xfer,
  1149. SCMI_RAW_REPLY_QUEUE,
  1150. cinfo->id);
  1151. }
  1152. }
  1153. } else {
  1154. /* And we wait for the response. */
  1155. if (!wait_for_completion_timeout(&xfer->done,
  1156. msecs_to_jiffies(timeout_ms))) {
  1157. dev_err(dev, "timed out in resp(caller: %pS)\n",
  1158. (void *)_RET_IP_);
  1159. ret = -ETIMEDOUT;
  1160. scmi_inc_count(info->dbg, XFERS_RESPONSE_TIMEOUT);
  1161. }
  1162. }
  1163. return ret;
  1164. }
  1165. /**
  1166. * scmi_wait_for_message_response - An helper to group all the possible ways of
  1167. * waiting for a synchronous message response.
  1168. *
  1169. * @cinfo: SCMI channel info
  1170. * @xfer: Reference to the transfer being waited for.
  1171. *
  1172. * Chooses waiting strategy (sleep-waiting vs busy-waiting) depending on
  1173. * configuration flags like xfer->hdr.poll_completion.
  1174. *
  1175. * Return: 0 on Success, error otherwise.
  1176. */
  1177. static int scmi_wait_for_message_response(struct scmi_chan_info *cinfo,
  1178. struct scmi_xfer *xfer)
  1179. {
  1180. struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
  1181. struct device *dev = info->dev;
  1182. trace_scmi_xfer_response_wait(xfer->transfer_id, xfer->hdr.id,
  1183. xfer->hdr.protocol_id, xfer->hdr.seq,
  1184. info->desc->max_rx_timeout_ms,
  1185. xfer->hdr.poll_completion);
  1186. return scmi_wait_for_reply(dev, info->desc, cinfo, xfer,
  1187. info->desc->max_rx_timeout_ms);
  1188. }
  1189. /**
  1190. * scmi_xfer_raw_wait_for_message_response - An helper to wait for a message
  1191. * reply to an xfer raw request on a specific channel for the required timeout.
  1192. *
  1193. * @cinfo: SCMI channel info
  1194. * @xfer: Reference to the transfer being waited for.
  1195. * @timeout_ms: The maximum timeout in milliseconds
  1196. *
  1197. * Return: 0 on Success, error otherwise.
  1198. */
  1199. int scmi_xfer_raw_wait_for_message_response(struct scmi_chan_info *cinfo,
  1200. struct scmi_xfer *xfer,
  1201. unsigned int timeout_ms)
  1202. {
  1203. int ret;
  1204. struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
  1205. struct device *dev = info->dev;
  1206. ret = scmi_wait_for_reply(dev, info->desc, cinfo, xfer, timeout_ms);
  1207. if (ret)
  1208. dev_dbg(dev, "timed out in RAW response - HDR:%08X\n",
  1209. pack_scmi_header(&xfer->hdr));
  1210. return ret;
  1211. }
  1212. /**
  1213. * do_xfer() - Do one transfer
  1214. *
  1215. * @ph: Pointer to SCMI protocol handle
  1216. * @xfer: Transfer to initiate and wait for response
  1217. *
  1218. * Return: -ETIMEDOUT in case of no response, if transmit error,
  1219. * return corresponding error, else if all goes well,
  1220. * return 0.
  1221. */
  1222. static int do_xfer(const struct scmi_protocol_handle *ph,
  1223. struct scmi_xfer *xfer)
  1224. {
  1225. int ret;
  1226. const struct scmi_protocol_instance *pi = ph_to_pi(ph);
  1227. struct scmi_info *info = handle_to_scmi_info(pi->handle);
  1228. struct device *dev = info->dev;
  1229. struct scmi_chan_info *cinfo;
  1230. /* Check for polling request on custom command xfers at first */
  1231. if (xfer->hdr.poll_completion &&
  1232. !is_transport_polling_capable(info->desc)) {
  1233. dev_warn_once(dev,
  1234. "Polling mode is not supported by transport.\n");
  1235. scmi_inc_count(info->dbg, SENT_FAIL_POLLING_UNSUPPORTED);
  1236. return -EINVAL;
  1237. }
  1238. cinfo = idr_find(&info->tx_idr, pi->proto->id);
  1239. if (unlikely(!cinfo)) {
  1240. scmi_inc_count(info->dbg, SENT_FAIL_CHANNEL_NOT_FOUND);
  1241. return -EINVAL;
  1242. }
  1243. /* True ONLY if also supported by transport. */
  1244. if (is_polling_enabled(cinfo, info->desc))
  1245. xfer->hdr.poll_completion = true;
  1246. /*
  1247. * Initialise protocol id now from protocol handle to avoid it being
  1248. * overridden by mistake (or malice) by the protocol code mangling with
  1249. * the scmi_xfer structure prior to this.
  1250. */
  1251. xfer->hdr.protocol_id = pi->proto->id;
  1252. reinit_completion(&xfer->done);
  1253. trace_scmi_xfer_begin(xfer->transfer_id, xfer->hdr.id,
  1254. xfer->hdr.protocol_id, xfer->hdr.seq,
  1255. xfer->hdr.poll_completion,
  1256. scmi_inflight_count(&info->handle));
  1257. /* Clear any stale status */
  1258. xfer->hdr.status = SCMI_SUCCESS;
  1259. xfer->state = SCMI_XFER_SENT_OK;
  1260. /*
  1261. * Even though spinlocking is not needed here since no race is possible
  1262. * on xfer->state due to the monotonically increasing tokens allocation,
  1263. * we must anyway ensure xfer->state initialization is not re-ordered
  1264. * after the .send_message() to be sure that on the RX path an early
  1265. * ISR calling scmi_rx_callback() cannot see an old stale xfer->state.
  1266. */
  1267. smp_mb();
  1268. ret = info->desc->ops->send_message(cinfo, xfer);
  1269. if (ret < 0) {
  1270. dev_dbg(dev, "Failed to send message %d\n", ret);
  1271. scmi_inc_count(info->dbg, SENT_FAIL);
  1272. return ret;
  1273. }
  1274. trace_scmi_msg_dump(info->id, cinfo->id, xfer->hdr.protocol_id,
  1275. xfer->hdr.id, "CMND", xfer->hdr.seq,
  1276. xfer->hdr.status, xfer->tx.buf, xfer->tx.len);
  1277. scmi_inc_count(info->dbg, SENT_OK);
  1278. ret = scmi_wait_for_message_response(cinfo, xfer);
  1279. if (!ret && xfer->hdr.status) {
  1280. ret = scmi_to_linux_errno(xfer->hdr.status);
  1281. scmi_inc_count(info->dbg, ERR_PROTOCOL);
  1282. }
  1283. if (info->desc->ops->mark_txdone)
  1284. info->desc->ops->mark_txdone(cinfo, ret, xfer);
  1285. trace_scmi_xfer_end(xfer->transfer_id, xfer->hdr.id,
  1286. xfer->hdr.protocol_id, xfer->hdr.seq, ret,
  1287. scmi_inflight_count(&info->handle));
  1288. return ret;
  1289. }
  1290. static void reset_rx_to_maxsz(const struct scmi_protocol_handle *ph,
  1291. struct scmi_xfer *xfer)
  1292. {
  1293. const struct scmi_protocol_instance *pi = ph_to_pi(ph);
  1294. struct scmi_info *info = handle_to_scmi_info(pi->handle);
  1295. xfer->rx.len = info->desc->max_msg_size;
  1296. }
  1297. /**
  1298. * do_xfer_with_response() - Do one transfer and wait until the delayed
  1299. * response is received
  1300. *
  1301. * @ph: Pointer to SCMI protocol handle
  1302. * @xfer: Transfer to initiate and wait for response
  1303. *
  1304. * Using asynchronous commands in atomic/polling mode should be avoided since
  1305. * it could cause long busy-waiting here, so ignore polling for the delayed
  1306. * response and WARN if it was requested for this command transaction since
  1307. * upper layers should refrain from issuing such kind of requests.
  1308. *
  1309. * The only other option would have been to refrain from using any asynchronous
  1310. * command even if made available, when an atomic transport is detected, and
  1311. * instead forcibly use the synchronous version (thing that can be easily
  1312. * attained at the protocol layer), but this would also have led to longer
  1313. * stalls of the channel for synchronous commands and possibly timeouts.
  1314. * (in other words there is usually a good reason if a platform provides an
  1315. * asynchronous version of a command and we should prefer to use it...just not
  1316. * when using atomic/polling mode)
  1317. *
  1318. * Return: -ETIMEDOUT in case of no delayed response, if transmit error,
  1319. * return corresponding error, else if all goes well, return 0.
  1320. */
  1321. static int do_xfer_with_response(const struct scmi_protocol_handle *ph,
  1322. struct scmi_xfer *xfer)
  1323. {
  1324. int ret, timeout = msecs_to_jiffies(SCMI_MAX_RESPONSE_TIMEOUT);
  1325. DECLARE_COMPLETION_ONSTACK(async_response);
  1326. xfer->async_done = &async_response;
  1327. /*
  1328. * Delayed responses should not be polled, so an async command should
  1329. * not have been used when requiring an atomic/poll context; WARN and
  1330. * perform instead a sleeping wait.
  1331. * (Note Async + IgnoreDelayedResponses are sent via do_xfer)
  1332. */
  1333. WARN_ON_ONCE(xfer->hdr.poll_completion);
  1334. ret = do_xfer(ph, xfer);
  1335. if (!ret) {
  1336. if (!wait_for_completion_timeout(xfer->async_done, timeout)) {
  1337. dev_err(ph->dev,
  1338. "timed out in delayed resp(caller: %pS)\n",
  1339. (void *)_RET_IP_);
  1340. ret = -ETIMEDOUT;
  1341. } else if (xfer->hdr.status) {
  1342. ret = scmi_to_linux_errno(xfer->hdr.status);
  1343. }
  1344. }
  1345. xfer->async_done = NULL;
  1346. return ret;
  1347. }
  1348. /**
  1349. * xfer_get_init() - Allocate and initialise one message for transmit
  1350. *
  1351. * @ph: Pointer to SCMI protocol handle
  1352. * @msg_id: Message identifier
  1353. * @tx_size: transmit message size
  1354. * @rx_size: receive message size
  1355. * @p: pointer to the allocated and initialised message
  1356. *
  1357. * This function allocates the message using @scmi_xfer_get and
  1358. * initialise the header.
  1359. *
  1360. * Return: 0 if all went fine with @p pointing to message, else
  1361. * corresponding error.
  1362. */
  1363. static int xfer_get_init(const struct scmi_protocol_handle *ph,
  1364. u8 msg_id, size_t tx_size, size_t rx_size,
  1365. struct scmi_xfer **p)
  1366. {
  1367. int ret;
  1368. struct scmi_xfer *xfer;
  1369. const struct scmi_protocol_instance *pi = ph_to_pi(ph);
  1370. struct scmi_info *info = handle_to_scmi_info(pi->handle);
  1371. struct scmi_xfers_info *minfo = &info->tx_minfo;
  1372. struct device *dev = info->dev;
  1373. /* Ensure we have sane transfer sizes */
  1374. if (rx_size > info->desc->max_msg_size ||
  1375. tx_size > info->desc->max_msg_size)
  1376. return -ERANGE;
  1377. xfer = scmi_xfer_get(pi->handle, minfo);
  1378. if (IS_ERR(xfer)) {
  1379. ret = PTR_ERR(xfer);
  1380. dev_err(dev, "failed to get free message slot(%d)\n", ret);
  1381. return ret;
  1382. }
  1383. /* Pick a sequence number and register this xfer as in-flight */
  1384. ret = scmi_xfer_pending_set(xfer, minfo);
  1385. if (ret) {
  1386. dev_err(pi->handle->dev,
  1387. "Failed to get monotonic token %d\n", ret);
  1388. __scmi_xfer_put(minfo, xfer);
  1389. return ret;
  1390. }
  1391. xfer->tx.len = tx_size;
  1392. xfer->rx.len = rx_size ? : info->desc->max_msg_size;
  1393. xfer->hdr.type = MSG_TYPE_COMMAND;
  1394. xfer->hdr.id = msg_id;
  1395. xfer->hdr.poll_completion = false;
  1396. *p = xfer;
  1397. return 0;
  1398. }
  1399. /**
  1400. * version_get() - command to get the revision of the SCMI entity
  1401. *
  1402. * @ph: Pointer to SCMI protocol handle
  1403. * @version: Holds returned version of protocol.
  1404. *
  1405. * Updates the SCMI information in the internal data structure.
  1406. *
  1407. * Return: 0 if all went fine, else return appropriate error.
  1408. */
  1409. static int version_get(const struct scmi_protocol_handle *ph, u32 *version)
  1410. {
  1411. int ret;
  1412. __le32 *rev_info;
  1413. struct scmi_xfer *t;
  1414. ret = xfer_get_init(ph, PROTOCOL_VERSION, 0, sizeof(*version), &t);
  1415. if (ret)
  1416. return ret;
  1417. ret = do_xfer(ph, t);
  1418. if (!ret) {
  1419. rev_info = t->rx.buf;
  1420. *version = le32_to_cpu(*rev_info);
  1421. }
  1422. xfer_put(ph, t);
  1423. return ret;
  1424. }
  1425. /**
  1426. * scmi_set_protocol_priv - Set protocol specific data at init time
  1427. *
  1428. * @ph: A reference to the protocol handle.
  1429. * @priv: The private data to set.
  1430. *
  1431. * Return: 0 on Success
  1432. */
  1433. static int scmi_set_protocol_priv(const struct scmi_protocol_handle *ph,
  1434. void *priv)
  1435. {
  1436. struct scmi_protocol_instance *pi = ph_to_pi(ph);
  1437. pi->priv = priv;
  1438. return 0;
  1439. }
  1440. /**
  1441. * scmi_get_protocol_priv - Set protocol specific data at init time
  1442. *
  1443. * @ph: A reference to the protocol handle.
  1444. *
  1445. * Return: Protocol private data if any was set.
  1446. */
  1447. static void *scmi_get_protocol_priv(const struct scmi_protocol_handle *ph)
  1448. {
  1449. const struct scmi_protocol_instance *pi = ph_to_pi(ph);
  1450. return pi->priv;
  1451. }
  1452. static const struct scmi_xfer_ops xfer_ops = {
  1453. .xfer_get_init = xfer_get_init,
  1454. .reset_rx_to_maxsz = reset_rx_to_maxsz,
  1455. .do_xfer = do_xfer,
  1456. .do_xfer_with_response = do_xfer_with_response,
  1457. .xfer_put = xfer_put,
  1458. };
  1459. struct scmi_msg_resp_domain_name_get {
  1460. __le32 flags;
  1461. u8 name[SCMI_MAX_STR_SIZE];
  1462. };
  1463. /**
  1464. * scmi_common_extended_name_get - Common helper to get extended resources name
  1465. * @ph: A protocol handle reference.
  1466. * @cmd_id: The specific command ID to use.
  1467. * @res_id: The specific resource ID to use.
  1468. * @flags: A pointer to specific flags to use, if any.
  1469. * @name: A pointer to the preallocated area where the retrieved name will be
  1470. * stored as a NULL terminated string.
  1471. * @len: The len in bytes of the @name char array.
  1472. *
  1473. * Return: 0 on Succcess
  1474. */
  1475. static int scmi_common_extended_name_get(const struct scmi_protocol_handle *ph,
  1476. u8 cmd_id, u32 res_id, u32 *flags,
  1477. char *name, size_t len)
  1478. {
  1479. int ret;
  1480. size_t txlen;
  1481. struct scmi_xfer *t;
  1482. struct scmi_msg_resp_domain_name_get *resp;
  1483. txlen = !flags ? sizeof(res_id) : sizeof(res_id) + sizeof(*flags);
  1484. ret = ph->xops->xfer_get_init(ph, cmd_id, txlen, sizeof(*resp), &t);
  1485. if (ret)
  1486. goto out;
  1487. put_unaligned_le32(res_id, t->tx.buf);
  1488. if (flags)
  1489. put_unaligned_le32(*flags, t->tx.buf + sizeof(res_id));
  1490. resp = t->rx.buf;
  1491. ret = ph->xops->do_xfer(ph, t);
  1492. if (!ret)
  1493. strscpy(name, resp->name, len);
  1494. ph->xops->xfer_put(ph, t);
  1495. out:
  1496. if (ret)
  1497. dev_warn(ph->dev,
  1498. "Failed to get extended name - id:%u (ret:%d). Using %s\n",
  1499. res_id, ret, name);
  1500. return ret;
  1501. }
  1502. /**
  1503. * scmi_common_get_max_msg_size - Get maximum message size
  1504. * @ph: A protocol handle reference.
  1505. *
  1506. * Return: Maximum message size for the current protocol.
  1507. */
  1508. static int scmi_common_get_max_msg_size(const struct scmi_protocol_handle *ph)
  1509. {
  1510. const struct scmi_protocol_instance *pi = ph_to_pi(ph);
  1511. struct scmi_info *info = handle_to_scmi_info(pi->handle);
  1512. return info->desc->max_msg_size;
  1513. }
  1514. /**
  1515. * scmi_protocol_msg_check - Check protocol message attributes
  1516. *
  1517. * @ph: A reference to the protocol handle.
  1518. * @message_id: The ID of the message to check.
  1519. * @attributes: A parameter to optionally return the retrieved message
  1520. * attributes, in case of Success.
  1521. *
  1522. * An helper to check protocol message attributes for a specific protocol
  1523. * and message pair.
  1524. *
  1525. * Return: 0 on SUCCESS
  1526. */
  1527. static int scmi_protocol_msg_check(const struct scmi_protocol_handle *ph,
  1528. u32 message_id, u32 *attributes)
  1529. {
  1530. int ret;
  1531. struct scmi_xfer *t;
  1532. ret = xfer_get_init(ph, PROTOCOL_MESSAGE_ATTRIBUTES,
  1533. sizeof(__le32), 0, &t);
  1534. if (ret)
  1535. return ret;
  1536. put_unaligned_le32(message_id, t->tx.buf);
  1537. ret = do_xfer(ph, t);
  1538. if (!ret && attributes)
  1539. *attributes = get_unaligned_le32(t->rx.buf);
  1540. xfer_put(ph, t);
  1541. return ret;
  1542. }
  1543. /**
  1544. * struct scmi_iterator - Iterator descriptor
  1545. * @msg: A reference to the message TX buffer; filled by @prepare_message with
  1546. * a proper custom command payload for each multi-part command request.
  1547. * @resp: A reference to the response RX buffer; used by @update_state and
  1548. * @process_response to parse the multi-part replies.
  1549. * @t: A reference to the underlying xfer initialized and used transparently by
  1550. * the iterator internal routines.
  1551. * @ph: A reference to the associated protocol handle to be used.
  1552. * @ops: A reference to the custom provided iterator operations.
  1553. * @state: The current iterator state; used and updated in turn by the iterators
  1554. * internal routines and by the caller-provided @scmi_iterator_ops.
  1555. * @priv: A reference to optional private data as provided by the caller and
  1556. * passed back to the @@scmi_iterator_ops.
  1557. */
  1558. struct scmi_iterator {
  1559. void *msg;
  1560. void *resp;
  1561. struct scmi_xfer *t;
  1562. const struct scmi_protocol_handle *ph;
  1563. struct scmi_iterator_ops *ops;
  1564. struct scmi_iterator_state state;
  1565. void *priv;
  1566. };
  1567. static void *scmi_iterator_init(const struct scmi_protocol_handle *ph,
  1568. struct scmi_iterator_ops *ops,
  1569. unsigned int max_resources, u8 msg_id,
  1570. size_t tx_size, void *priv)
  1571. {
  1572. int ret;
  1573. struct scmi_iterator *i;
  1574. i = devm_kzalloc(ph->dev, sizeof(*i), GFP_KERNEL);
  1575. if (!i)
  1576. return ERR_PTR(-ENOMEM);
  1577. i->ph = ph;
  1578. i->ops = ops;
  1579. i->priv = priv;
  1580. ret = ph->xops->xfer_get_init(ph, msg_id, tx_size, 0, &i->t);
  1581. if (ret) {
  1582. devm_kfree(ph->dev, i);
  1583. return ERR_PTR(ret);
  1584. }
  1585. i->state.max_resources = max_resources;
  1586. i->msg = i->t->tx.buf;
  1587. i->resp = i->t->rx.buf;
  1588. return i;
  1589. }
  1590. static int scmi_iterator_run(void *iter)
  1591. {
  1592. int ret = -EINVAL;
  1593. struct scmi_iterator_ops *iops;
  1594. const struct scmi_protocol_handle *ph;
  1595. struct scmi_iterator_state *st;
  1596. struct scmi_iterator *i = iter;
  1597. if (!i || !i->ops || !i->ph)
  1598. return ret;
  1599. iops = i->ops;
  1600. ph = i->ph;
  1601. st = &i->state;
  1602. do {
  1603. iops->prepare_message(i->msg, st->desc_index, i->priv);
  1604. ret = ph->xops->do_xfer(ph, i->t);
  1605. if (ret)
  1606. break;
  1607. st->rx_len = i->t->rx.len;
  1608. ret = iops->update_state(st, i->resp, i->priv);
  1609. if (ret)
  1610. break;
  1611. if (st->num_returned > st->max_resources - st->desc_index) {
  1612. dev_err(ph->dev,
  1613. "No. of resources can't exceed %d\n",
  1614. st->max_resources);
  1615. ret = -EINVAL;
  1616. break;
  1617. }
  1618. for (st->loop_idx = 0; st->loop_idx < st->num_returned;
  1619. st->loop_idx++) {
  1620. ret = iops->process_response(ph, i->resp, st, i->priv);
  1621. if (ret)
  1622. goto out;
  1623. }
  1624. st->desc_index += st->num_returned;
  1625. ph->xops->reset_rx_to_maxsz(ph, i->t);
  1626. /*
  1627. * check for both returned and remaining to avoid infinite
  1628. * loop due to buggy firmware
  1629. */
  1630. } while (st->num_returned && st->num_remaining);
  1631. out:
  1632. /* Finalize and destroy iterator */
  1633. ph->xops->xfer_put(ph, i->t);
  1634. devm_kfree(ph->dev, i);
  1635. return ret;
  1636. }
  1637. struct scmi_msg_get_fc_info {
  1638. __le32 domain;
  1639. __le32 message_id;
  1640. };
  1641. struct scmi_msg_resp_desc_fc {
  1642. __le32 attr;
  1643. #define SUPPORTS_DOORBELL(x) ((x) & BIT(0))
  1644. #define DOORBELL_REG_WIDTH(x) FIELD_GET(GENMASK(2, 1), (x))
  1645. __le32 rate_limit;
  1646. __le32 chan_addr_low;
  1647. __le32 chan_addr_high;
  1648. __le32 chan_size;
  1649. __le32 db_addr_low;
  1650. __le32 db_addr_high;
  1651. __le32 db_set_lmask;
  1652. __le32 db_set_hmask;
  1653. __le32 db_preserve_lmask;
  1654. __le32 db_preserve_hmask;
  1655. };
  1656. #define QUIRK_PERF_FC_FORCE \
  1657. ({ \
  1658. if (pi->proto->id == SCMI_PROTOCOL_PERF && \
  1659. message_id == 0x8 /* PERF_LEVEL_GET */) \
  1660. attributes |= BIT(0); \
  1661. })
  1662. static void
  1663. scmi_common_fastchannel_init(const struct scmi_protocol_handle *ph,
  1664. u8 describe_id, u32 message_id, u32 valid_size,
  1665. u32 domain, void __iomem **p_addr,
  1666. struct scmi_fc_db_info **p_db, u32 *rate_limit)
  1667. {
  1668. int ret;
  1669. u32 flags;
  1670. u64 phys_addr;
  1671. u32 attributes;
  1672. u8 size;
  1673. void __iomem *addr;
  1674. struct scmi_xfer *t;
  1675. struct scmi_fc_db_info *db = NULL;
  1676. struct scmi_msg_get_fc_info *info;
  1677. struct scmi_msg_resp_desc_fc *resp;
  1678. const struct scmi_protocol_instance *pi = ph_to_pi(ph);
  1679. /* Check if the MSG_ID supports fastchannel */
  1680. ret = scmi_protocol_msg_check(ph, message_id, &attributes);
  1681. SCMI_QUIRK(perf_level_get_fc_force, QUIRK_PERF_FC_FORCE);
  1682. if (ret || !MSG_SUPPORTS_FASTCHANNEL(attributes)) {
  1683. dev_dbg(ph->dev,
  1684. "Skip FC init for 0x%02X/%d domain:%d - ret:%d\n",
  1685. pi->proto->id, message_id, domain, ret);
  1686. return;
  1687. }
  1688. if (!p_addr) {
  1689. ret = -EINVAL;
  1690. goto err_out;
  1691. }
  1692. ret = ph->xops->xfer_get_init(ph, describe_id,
  1693. sizeof(*info), sizeof(*resp), &t);
  1694. if (ret)
  1695. goto err_out;
  1696. info = t->tx.buf;
  1697. info->domain = cpu_to_le32(domain);
  1698. info->message_id = cpu_to_le32(message_id);
  1699. /*
  1700. * Bail out on error leaving fc_info addresses zeroed; this includes
  1701. * the case in which the requested domain/message_id does NOT support
  1702. * fastchannels at all.
  1703. */
  1704. ret = ph->xops->do_xfer(ph, t);
  1705. if (ret)
  1706. goto err_xfer;
  1707. resp = t->rx.buf;
  1708. flags = le32_to_cpu(resp->attr);
  1709. size = le32_to_cpu(resp->chan_size);
  1710. if (size != valid_size) {
  1711. ret = -EINVAL;
  1712. goto err_xfer;
  1713. }
  1714. if (rate_limit)
  1715. *rate_limit = le32_to_cpu(resp->rate_limit) & GENMASK(19, 0);
  1716. phys_addr = le32_to_cpu(resp->chan_addr_low);
  1717. phys_addr |= (u64)le32_to_cpu(resp->chan_addr_high) << 32;
  1718. addr = devm_ioremap(ph->dev, phys_addr, size);
  1719. if (!addr) {
  1720. ret = -EADDRNOTAVAIL;
  1721. goto err_xfer;
  1722. }
  1723. *p_addr = addr;
  1724. if (p_db && SUPPORTS_DOORBELL(flags)) {
  1725. db = devm_kzalloc(ph->dev, sizeof(*db), GFP_KERNEL);
  1726. if (!db) {
  1727. ret = -ENOMEM;
  1728. goto err_db;
  1729. }
  1730. size = 1 << DOORBELL_REG_WIDTH(flags);
  1731. phys_addr = le32_to_cpu(resp->db_addr_low);
  1732. phys_addr |= (u64)le32_to_cpu(resp->db_addr_high) << 32;
  1733. addr = devm_ioremap(ph->dev, phys_addr, size);
  1734. if (!addr) {
  1735. ret = -EADDRNOTAVAIL;
  1736. goto err_db_mem;
  1737. }
  1738. db->addr = addr;
  1739. db->width = size;
  1740. db->set = le32_to_cpu(resp->db_set_lmask);
  1741. db->set |= (u64)le32_to_cpu(resp->db_set_hmask) << 32;
  1742. db->mask = le32_to_cpu(resp->db_preserve_lmask);
  1743. db->mask |= (u64)le32_to_cpu(resp->db_preserve_hmask) << 32;
  1744. *p_db = db;
  1745. }
  1746. ph->xops->xfer_put(ph, t);
  1747. dev_dbg(ph->dev,
  1748. "Using valid FC for protocol %X [MSG_ID:%u / RES_ID:%u]\n",
  1749. pi->proto->id, message_id, domain);
  1750. return;
  1751. err_db_mem:
  1752. devm_kfree(ph->dev, db);
  1753. err_db:
  1754. *p_addr = NULL;
  1755. err_xfer:
  1756. ph->xops->xfer_put(ph, t);
  1757. err_out:
  1758. dev_warn(ph->dev,
  1759. "Failed to get FC for protocol %X [MSG_ID:%u / RES_ID:%u] - ret:%d. Using regular messaging.\n",
  1760. pi->proto->id, message_id, domain, ret);
  1761. }
  1762. #define SCMI_PROTO_FC_RING_DB(w) \
  1763. do { \
  1764. u##w val = 0; \
  1765. \
  1766. if (db->mask) \
  1767. val = ioread##w(db->addr) & db->mask; \
  1768. iowrite##w((u##w)db->set | val, db->addr); \
  1769. } while (0)
  1770. static void scmi_common_fastchannel_db_ring(struct scmi_fc_db_info *db)
  1771. {
  1772. if (!db || !db->addr)
  1773. return;
  1774. if (db->width == 1)
  1775. SCMI_PROTO_FC_RING_DB(8);
  1776. else if (db->width == 2)
  1777. SCMI_PROTO_FC_RING_DB(16);
  1778. else if (db->width == 4)
  1779. SCMI_PROTO_FC_RING_DB(32);
  1780. else /* db->width == 8 */
  1781. SCMI_PROTO_FC_RING_DB(64);
  1782. }
  1783. static const struct scmi_proto_helpers_ops helpers_ops = {
  1784. .extended_name_get = scmi_common_extended_name_get,
  1785. .get_max_msg_size = scmi_common_get_max_msg_size,
  1786. .iter_response_init = scmi_iterator_init,
  1787. .iter_response_run = scmi_iterator_run,
  1788. .protocol_msg_check = scmi_protocol_msg_check,
  1789. .fastchannel_init = scmi_common_fastchannel_init,
  1790. .fastchannel_db_ring = scmi_common_fastchannel_db_ring,
  1791. };
  1792. /**
  1793. * scmi_revision_area_get - Retrieve version memory area.
  1794. *
  1795. * @ph: A reference to the protocol handle.
  1796. *
  1797. * A helper to grab the version memory area reference during SCMI Base protocol
  1798. * initialization.
  1799. *
  1800. * Return: A reference to the version memory area associated to the SCMI
  1801. * instance underlying this protocol handle.
  1802. */
  1803. struct scmi_revision_info *
  1804. scmi_revision_area_get(const struct scmi_protocol_handle *ph)
  1805. {
  1806. const struct scmi_protocol_instance *pi = ph_to_pi(ph);
  1807. return pi->handle->version;
  1808. }
  1809. /**
  1810. * scmi_protocol_version_negotiate - Negotiate protocol version
  1811. *
  1812. * @ph: A reference to the protocol handle.
  1813. *
  1814. * An helper to negotiate a protocol version different from the latest
  1815. * advertised as supported from the platform: on Success backward
  1816. * compatibility is assured by the platform.
  1817. *
  1818. * Return: 0 on Success
  1819. */
  1820. static int scmi_protocol_version_negotiate(struct scmi_protocol_handle *ph)
  1821. {
  1822. int ret;
  1823. struct scmi_xfer *t;
  1824. struct scmi_protocol_instance *pi = ph_to_pi(ph);
  1825. /* At first check if NEGOTIATE_PROTOCOL_VERSION is supported ... */
  1826. ret = scmi_protocol_msg_check(ph, NEGOTIATE_PROTOCOL_VERSION, NULL);
  1827. if (ret)
  1828. return ret;
  1829. /* ... then attempt protocol version negotiation */
  1830. ret = xfer_get_init(ph, NEGOTIATE_PROTOCOL_VERSION,
  1831. sizeof(__le32), 0, &t);
  1832. if (ret)
  1833. return ret;
  1834. put_unaligned_le32(pi->proto->supported_version, t->tx.buf);
  1835. ret = do_xfer(ph, t);
  1836. if (!ret)
  1837. pi->negotiated_version = pi->proto->supported_version;
  1838. xfer_put(ph, t);
  1839. return ret;
  1840. }
  1841. /**
  1842. * scmi_protocol_version_initialize - Initialize protocol version
  1843. * @dev: A device reference.
  1844. * @pi: A reference to the protocol instance being initialized
  1845. *
  1846. * At first retrieve the newest protocol version supported by the platform for
  1847. * this specific protoocol.
  1848. *
  1849. * Negotiation is attempted only when the platform advertised a protocol
  1850. * version newer than the most recent version known to this agent, since
  1851. * backward compatibility is NOT assured in general between versions.
  1852. *
  1853. * Failing to negotiate a fallback version or to query supported version at
  1854. * all will result in an attempt to use the newest version known to this agent
  1855. * even though compatibility is NOT assured.
  1856. *
  1857. * Versions are defined as:
  1858. *
  1859. * pi->version: the version supported by the platform as returned by the query.
  1860. * pi->proto->supported_version: the newest version supported by this agent
  1861. * for this protocol.
  1862. * pi->negotiated_version: The version successfully negotiated with the platform.
  1863. * ph->version: The final version effectively chosen for this session.
  1864. */
  1865. static void scmi_protocol_version_initialize(struct device *dev,
  1866. struct scmi_protocol_instance *pi)
  1867. {
  1868. struct scmi_protocol_handle *ph = &pi->ph;
  1869. int ret;
  1870. /*
  1871. * Query and store platform supported protocol version: this is usually
  1872. * the newest version the platfom can support.
  1873. */
  1874. ret = version_get(ph, &pi->version);
  1875. if (ret) {
  1876. dev_warn(dev,
  1877. "Failed to query supported version for protocol 0x%X.\n",
  1878. pi->proto->id);
  1879. goto best_effort;
  1880. }
  1881. /* Need to negotiate at all ? */
  1882. if (pi->version <= pi->proto->supported_version) {
  1883. ph->version = pi->version;
  1884. return;
  1885. }
  1886. /* Attempt negotiation */
  1887. ret = scmi_protocol_version_negotiate(ph);
  1888. if (!ret) {
  1889. ph->version = pi->negotiated_version;
  1890. dev_info(dev,
  1891. "Protocol 0x%X successfully negotiated version 0x%X\n",
  1892. pi->proto->id, ph->version);
  1893. return;
  1894. }
  1895. dev_warn(dev,
  1896. "Detected UNSUPPORTED higher version 0x%X for protocol 0x%X.\n",
  1897. pi->version, pi->proto->id);
  1898. best_effort:
  1899. /* Fallback to use newest version known to this agent */
  1900. ph->version = pi->proto->supported_version;
  1901. dev_warn(dev,
  1902. "Trying version 0x%X. Backward compatibility is NOT assured.\n",
  1903. ph->version);
  1904. }
  1905. /**
  1906. * scmi_alloc_init_protocol_instance - Allocate and initialize a protocol
  1907. * instance descriptor.
  1908. * @info: The reference to the related SCMI instance.
  1909. * @proto: The protocol descriptor.
  1910. *
  1911. * Allocate a new protocol instance descriptor, using the provided @proto
  1912. * description, against the specified SCMI instance @info, and initialize it;
  1913. * all resources management is handled via a dedicated per-protocol devres
  1914. * group.
  1915. *
  1916. * Context: Assumes to be called with @protocols_mtx already acquired.
  1917. * Return: A reference to a freshly allocated and initialized protocol instance
  1918. * or ERR_PTR on failure. On failure the @proto reference is at first
  1919. * put using @scmi_protocol_put() before releasing all the devres group.
  1920. */
  1921. static struct scmi_protocol_instance *
  1922. scmi_alloc_init_protocol_instance(struct scmi_info *info,
  1923. const struct scmi_protocol *proto)
  1924. {
  1925. int ret = -ENOMEM;
  1926. void *gid;
  1927. struct scmi_protocol_instance *pi;
  1928. const struct scmi_handle *handle = &info->handle;
  1929. /* Protocol specific devres group */
  1930. gid = devres_open_group(handle->dev, NULL, GFP_KERNEL);
  1931. if (!gid) {
  1932. scmi_protocol_put(proto);
  1933. goto out;
  1934. }
  1935. pi = devm_kzalloc(handle->dev, sizeof(*pi), GFP_KERNEL);
  1936. if (!pi)
  1937. goto clean;
  1938. pi->gid = gid;
  1939. pi->proto = proto;
  1940. pi->handle = handle;
  1941. pi->ph.dev = handle->dev;
  1942. pi->ph.xops = &xfer_ops;
  1943. pi->ph.hops = &helpers_ops;
  1944. pi->ph.set_priv = scmi_set_protocol_priv;
  1945. pi->ph.get_priv = scmi_get_protocol_priv;
  1946. refcount_set(&pi->users, 1);
  1947. /*
  1948. * Initialize effectively used protocol version performing any
  1949. * possibly needed negotiations.
  1950. */
  1951. scmi_protocol_version_initialize(handle->dev, pi);
  1952. /* proto->init is assured NON NULL by scmi_protocol_register */
  1953. ret = pi->proto->instance_init(&pi->ph);
  1954. if (ret)
  1955. goto clean;
  1956. ret = idr_alloc(&info->protocols, pi, proto->id, proto->id + 1,
  1957. GFP_KERNEL);
  1958. if (ret != proto->id)
  1959. goto clean;
  1960. /*
  1961. * Warn but ignore events registration errors since we do not want
  1962. * to skip whole protocols if their notifications are messed up.
  1963. */
  1964. if (pi->proto->events) {
  1965. ret = scmi_register_protocol_events(handle, pi->proto->id,
  1966. &pi->ph,
  1967. pi->proto->events);
  1968. if (ret)
  1969. dev_warn(handle->dev,
  1970. "Protocol:%X - Events Registration Failed - err:%d\n",
  1971. pi->proto->id, ret);
  1972. }
  1973. devres_close_group(handle->dev, pi->gid);
  1974. dev_dbg(handle->dev, "Initialized protocol: 0x%X\n", pi->proto->id);
  1975. return pi;
  1976. clean:
  1977. /* Take care to put the protocol module's owner before releasing all */
  1978. scmi_protocol_put(proto);
  1979. devres_release_group(handle->dev, gid);
  1980. out:
  1981. return ERR_PTR(ret);
  1982. }
  1983. /**
  1984. * scmi_get_protocol_instance - Protocol initialization helper.
  1985. * @handle: A reference to the SCMI platform instance.
  1986. * @protocol_id: The protocol being requested.
  1987. *
  1988. * In case the required protocol has never been requested before for this
  1989. * instance, allocate and initialize all the needed structures while handling
  1990. * resource allocation with a dedicated per-protocol devres subgroup.
  1991. *
  1992. * Return: A reference to an initialized protocol instance or error on failure:
  1993. * in particular returns -EPROBE_DEFER when the desired protocol could
  1994. * NOT be found.
  1995. */
  1996. static struct scmi_protocol_instance * __must_check
  1997. scmi_get_protocol_instance(const struct scmi_handle *handle, u8 protocol_id)
  1998. {
  1999. struct scmi_protocol_instance *pi;
  2000. struct scmi_info *info = handle_to_scmi_info(handle);
  2001. mutex_lock(&info->protocols_mtx);
  2002. pi = idr_find(&info->protocols, protocol_id);
  2003. if (pi) {
  2004. refcount_inc(&pi->users);
  2005. } else {
  2006. const struct scmi_protocol *proto;
  2007. /* Fails if protocol not registered on bus */
  2008. proto = scmi_protocol_get(protocol_id, &info->version);
  2009. if (proto)
  2010. pi = scmi_alloc_init_protocol_instance(info, proto);
  2011. else
  2012. pi = ERR_PTR(-EPROBE_DEFER);
  2013. }
  2014. mutex_unlock(&info->protocols_mtx);
  2015. return pi;
  2016. }
  2017. /**
  2018. * scmi_protocol_acquire - Protocol acquire
  2019. * @handle: A reference to the SCMI platform instance.
  2020. * @protocol_id: The protocol being requested.
  2021. *
  2022. * Register a new user for the requested protocol on the specified SCMI
  2023. * platform instance, possibly triggering its initialization on first user.
  2024. *
  2025. * Return: 0 if protocol was acquired successfully.
  2026. */
  2027. int scmi_protocol_acquire(const struct scmi_handle *handle, u8 protocol_id)
  2028. {
  2029. return PTR_ERR_OR_ZERO(scmi_get_protocol_instance(handle, protocol_id));
  2030. }
  2031. /**
  2032. * scmi_protocol_release - Protocol de-initialization helper.
  2033. * @handle: A reference to the SCMI platform instance.
  2034. * @protocol_id: The protocol being requested.
  2035. *
  2036. * Remove one user for the specified protocol and triggers de-initialization
  2037. * and resources de-allocation once the last user has gone.
  2038. */
  2039. void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id)
  2040. {
  2041. struct scmi_info *info = handle_to_scmi_info(handle);
  2042. struct scmi_protocol_instance *pi;
  2043. mutex_lock(&info->protocols_mtx);
  2044. pi = idr_find(&info->protocols, protocol_id);
  2045. if (WARN_ON(!pi))
  2046. goto out;
  2047. if (refcount_dec_and_test(&pi->users)) {
  2048. void *gid = pi->gid;
  2049. if (pi->proto->events)
  2050. scmi_deregister_protocol_events(handle, protocol_id);
  2051. if (pi->proto->instance_deinit)
  2052. pi->proto->instance_deinit(&pi->ph);
  2053. idr_remove(&info->protocols, protocol_id);
  2054. scmi_protocol_put(pi->proto);
  2055. devres_release_group(handle->dev, gid);
  2056. dev_dbg(handle->dev, "De-Initialized protocol: 0x%X\n",
  2057. protocol_id);
  2058. }
  2059. out:
  2060. mutex_unlock(&info->protocols_mtx);
  2061. }
  2062. void scmi_setup_protocol_implemented(const struct scmi_protocol_handle *ph,
  2063. u8 *prot_imp)
  2064. {
  2065. const struct scmi_protocol_instance *pi = ph_to_pi(ph);
  2066. struct scmi_info *info = handle_to_scmi_info(pi->handle);
  2067. info->protocols_imp = prot_imp;
  2068. }
  2069. static bool
  2070. scmi_is_protocol_implemented(const struct scmi_handle *handle, u8 prot_id)
  2071. {
  2072. int i;
  2073. struct scmi_info *info = handle_to_scmi_info(handle);
  2074. struct scmi_revision_info *rev = handle->version;
  2075. if (!info->protocols_imp)
  2076. return false;
  2077. for (i = 0; i < rev->num_protocols; i++)
  2078. if (info->protocols_imp[i] == prot_id)
  2079. return true;
  2080. return false;
  2081. }
  2082. struct scmi_protocol_devres {
  2083. const struct scmi_handle *handle;
  2084. u8 protocol_id;
  2085. };
  2086. static void scmi_devm_release_protocol(struct device *dev, void *res)
  2087. {
  2088. struct scmi_protocol_devres *dres = res;
  2089. scmi_protocol_release(dres->handle, dres->protocol_id);
  2090. }
  2091. static struct scmi_protocol_instance __must_check *
  2092. scmi_devres_protocol_instance_get(struct scmi_device *sdev, u8 protocol_id)
  2093. {
  2094. struct scmi_protocol_instance *pi;
  2095. struct scmi_protocol_devres *dres;
  2096. dres = devres_alloc(scmi_devm_release_protocol,
  2097. sizeof(*dres), GFP_KERNEL);
  2098. if (!dres)
  2099. return ERR_PTR(-ENOMEM);
  2100. pi = scmi_get_protocol_instance(sdev->handle, protocol_id);
  2101. if (IS_ERR(pi)) {
  2102. devres_free(dres);
  2103. return pi;
  2104. }
  2105. dres->handle = sdev->handle;
  2106. dres->protocol_id = protocol_id;
  2107. devres_add(&sdev->dev, dres);
  2108. return pi;
  2109. }
  2110. /**
  2111. * scmi_devm_protocol_get - Devres managed get protocol operations and handle
  2112. * @sdev: A reference to an scmi_device whose embedded struct device is to
  2113. * be used for devres accounting.
  2114. * @protocol_id: The protocol being requested.
  2115. * @ph: A pointer reference used to pass back the associated protocol handle.
  2116. *
  2117. * Get hold of a protocol accounting for its usage, eventually triggering its
  2118. * initialization, and returning the protocol specific operations and related
  2119. * protocol handle which will be used as first argument in most of the
  2120. * protocols operations methods.
  2121. * Being a devres based managed method, protocol hold will be automatically
  2122. * released, and possibly de-initialized on last user, once the SCMI driver
  2123. * owning the scmi_device is unbound from it.
  2124. *
  2125. * Return: A reference to the requested protocol operations or error.
  2126. * Must be checked for errors by caller.
  2127. */
  2128. static const void __must_check *
  2129. scmi_devm_protocol_get(struct scmi_device *sdev, u8 protocol_id,
  2130. struct scmi_protocol_handle **ph)
  2131. {
  2132. struct scmi_protocol_instance *pi;
  2133. if (!ph)
  2134. return ERR_PTR(-EINVAL);
  2135. pi = scmi_devres_protocol_instance_get(sdev, protocol_id);
  2136. if (IS_ERR(pi))
  2137. return pi;
  2138. *ph = &pi->ph;
  2139. return pi->proto->ops;
  2140. }
  2141. /**
  2142. * scmi_devm_protocol_acquire - Devres managed helper to get hold of a protocol
  2143. * @sdev: A reference to an scmi_device whose embedded struct device is to
  2144. * be used for devres accounting.
  2145. * @protocol_id: The protocol being requested.
  2146. *
  2147. * Get hold of a protocol accounting for its usage, possibly triggering its
  2148. * initialization but without getting access to its protocol specific operations
  2149. * and handle.
  2150. *
  2151. * Being a devres based managed method, protocol hold will be automatically
  2152. * released, and possibly de-initialized on last user, once the SCMI driver
  2153. * owning the scmi_device is unbound from it.
  2154. *
  2155. * Return: 0 on SUCCESS
  2156. */
  2157. static int __must_check scmi_devm_protocol_acquire(struct scmi_device *sdev,
  2158. u8 protocol_id)
  2159. {
  2160. struct scmi_protocol_instance *pi;
  2161. pi = scmi_devres_protocol_instance_get(sdev, protocol_id);
  2162. if (IS_ERR(pi))
  2163. return PTR_ERR(pi);
  2164. return 0;
  2165. }
  2166. static int scmi_devm_protocol_match(struct device *dev, void *res, void *data)
  2167. {
  2168. struct scmi_protocol_devres *dres = res;
  2169. if (WARN_ON(!dres || !data))
  2170. return 0;
  2171. return dres->protocol_id == *((u8 *)data);
  2172. }
  2173. /**
  2174. * scmi_devm_protocol_put - Devres managed put protocol operations and handle
  2175. * @sdev: A reference to an scmi_device whose embedded struct device is to
  2176. * be used for devres accounting.
  2177. * @protocol_id: The protocol being requested.
  2178. *
  2179. * Explicitly release a protocol hold previously obtained calling the above
  2180. * @scmi_devm_protocol_get.
  2181. */
  2182. static void scmi_devm_protocol_put(struct scmi_device *sdev, u8 protocol_id)
  2183. {
  2184. int ret;
  2185. ret = devres_release(&sdev->dev, scmi_devm_release_protocol,
  2186. scmi_devm_protocol_match, &protocol_id);
  2187. WARN_ON(ret);
  2188. }
  2189. /**
  2190. * scmi_is_transport_atomic - Method to check if underlying transport for an
  2191. * SCMI instance is configured as atomic.
  2192. *
  2193. * @handle: A reference to the SCMI platform instance.
  2194. * @atomic_threshold: An optional return value for the system wide currently
  2195. * configured threshold for atomic operations.
  2196. *
  2197. * Return: True if transport is configured as atomic
  2198. */
  2199. static bool scmi_is_transport_atomic(const struct scmi_handle *handle,
  2200. unsigned int *atomic_threshold)
  2201. {
  2202. bool ret;
  2203. struct scmi_info *info = handle_to_scmi_info(handle);
  2204. ret = info->desc->atomic_enabled &&
  2205. is_transport_polling_capable(info->desc);
  2206. if (ret && atomic_threshold)
  2207. *atomic_threshold = info->desc->atomic_threshold;
  2208. return ret;
  2209. }
  2210. /**
  2211. * scmi_handle_get() - Get the SCMI handle for a device
  2212. *
  2213. * @dev: pointer to device for which we want SCMI handle
  2214. *
  2215. * NOTE: The function does not track individual clients of the framework
  2216. * and is expected to be maintained by caller of SCMI protocol library.
  2217. * scmi_handle_put must be balanced with successful scmi_handle_get
  2218. *
  2219. * Return: pointer to handle if successful, NULL on error
  2220. */
  2221. static struct scmi_handle *scmi_handle_get(struct device *dev)
  2222. {
  2223. struct list_head *p;
  2224. struct scmi_info *info;
  2225. struct scmi_handle *handle = NULL;
  2226. mutex_lock(&scmi_list_mutex);
  2227. list_for_each(p, &scmi_list) {
  2228. info = list_entry(p, struct scmi_info, node);
  2229. if (dev->parent == info->dev) {
  2230. info->users++;
  2231. handle = &info->handle;
  2232. break;
  2233. }
  2234. }
  2235. mutex_unlock(&scmi_list_mutex);
  2236. return handle;
  2237. }
  2238. /**
  2239. * scmi_handle_put() - Release the handle acquired by scmi_handle_get
  2240. *
  2241. * @handle: handle acquired by scmi_handle_get
  2242. *
  2243. * NOTE: The function does not track individual clients of the framework
  2244. * and is expected to be maintained by caller of SCMI protocol library.
  2245. * scmi_handle_put must be balanced with successful scmi_handle_get
  2246. *
  2247. * Return: 0 is successfully released
  2248. * if null was passed, it returns -EINVAL;
  2249. */
  2250. static int scmi_handle_put(const struct scmi_handle *handle)
  2251. {
  2252. struct scmi_info *info;
  2253. if (!handle)
  2254. return -EINVAL;
  2255. info = handle_to_scmi_info(handle);
  2256. mutex_lock(&scmi_list_mutex);
  2257. if (!WARN_ON(!info->users))
  2258. info->users--;
  2259. mutex_unlock(&scmi_list_mutex);
  2260. return 0;
  2261. }
  2262. static void scmi_device_link_add(struct device *consumer,
  2263. struct device *supplier)
  2264. {
  2265. struct device_link *link;
  2266. link = device_link_add(consumer, supplier, DL_FLAG_AUTOREMOVE_CONSUMER);
  2267. WARN_ON(!link);
  2268. }
  2269. static void scmi_set_handle(struct scmi_device *scmi_dev)
  2270. {
  2271. scmi_dev->handle = scmi_handle_get(&scmi_dev->dev);
  2272. if (scmi_dev->handle)
  2273. scmi_device_link_add(&scmi_dev->dev, scmi_dev->handle->dev);
  2274. }
  2275. static int __scmi_xfer_info_init(struct scmi_info *sinfo,
  2276. struct scmi_xfers_info *info)
  2277. {
  2278. int i;
  2279. struct scmi_xfer *xfer;
  2280. struct device *dev = sinfo->dev;
  2281. const struct scmi_desc *desc = sinfo->desc;
  2282. /* Pre-allocated messages, no more than what hdr.seq can support */
  2283. if (WARN_ON(!info->max_msg || info->max_msg > MSG_TOKEN_MAX)) {
  2284. dev_err(dev,
  2285. "Invalid maximum messages %d, not in range [1 - %lu]\n",
  2286. info->max_msg, MSG_TOKEN_MAX);
  2287. return -EINVAL;
  2288. }
  2289. hash_init(info->pending_xfers);
  2290. /* Allocate a bitmask sized to hold MSG_TOKEN_MAX tokens */
  2291. info->xfer_alloc_table = devm_bitmap_zalloc(dev, MSG_TOKEN_MAX,
  2292. GFP_KERNEL);
  2293. if (!info->xfer_alloc_table)
  2294. return -ENOMEM;
  2295. /*
  2296. * Preallocate a number of xfers equal to max inflight messages,
  2297. * pre-initialize the buffer pointer to pre-allocated buffers and
  2298. * attach all of them to the free list
  2299. */
  2300. INIT_HLIST_HEAD(&info->free_xfers);
  2301. for (i = 0; i < info->max_msg; i++) {
  2302. xfer = devm_kzalloc(dev, sizeof(*xfer), GFP_KERNEL);
  2303. if (!xfer)
  2304. return -ENOMEM;
  2305. xfer->rx.buf = devm_kcalloc(dev, sizeof(u8), desc->max_msg_size,
  2306. GFP_KERNEL);
  2307. if (!xfer->rx.buf)
  2308. return -ENOMEM;
  2309. xfer->tx.buf = xfer->rx.buf;
  2310. init_completion(&xfer->done);
  2311. spin_lock_init(&xfer->lock);
  2312. /* Add initialized xfer to the free list */
  2313. hlist_add_head(&xfer->node, &info->free_xfers);
  2314. }
  2315. spin_lock_init(&info->xfer_lock);
  2316. return 0;
  2317. }
  2318. static int scmi_channels_max_msg_configure(struct scmi_info *sinfo)
  2319. {
  2320. const struct scmi_desc *desc = sinfo->desc;
  2321. if (!desc->ops->get_max_msg) {
  2322. sinfo->tx_minfo.max_msg = desc->max_msg;
  2323. sinfo->rx_minfo.max_msg = desc->max_msg;
  2324. } else {
  2325. struct scmi_chan_info *base_cinfo;
  2326. base_cinfo = idr_find(&sinfo->tx_idr, SCMI_PROTOCOL_BASE);
  2327. if (!base_cinfo)
  2328. return -EINVAL;
  2329. sinfo->tx_minfo.max_msg = desc->ops->get_max_msg(base_cinfo);
  2330. /* RX channel is optional so can be skipped */
  2331. base_cinfo = idr_find(&sinfo->rx_idr, SCMI_PROTOCOL_BASE);
  2332. if (base_cinfo)
  2333. sinfo->rx_minfo.max_msg =
  2334. desc->ops->get_max_msg(base_cinfo);
  2335. }
  2336. return 0;
  2337. }
  2338. static int scmi_xfer_info_init(struct scmi_info *sinfo)
  2339. {
  2340. int ret;
  2341. ret = scmi_channels_max_msg_configure(sinfo);
  2342. if (ret)
  2343. return ret;
  2344. ret = __scmi_xfer_info_init(sinfo, &sinfo->tx_minfo);
  2345. if (!ret && !idr_is_empty(&sinfo->rx_idr))
  2346. ret = __scmi_xfer_info_init(sinfo, &sinfo->rx_minfo);
  2347. return ret;
  2348. }
  2349. static int scmi_chan_setup(struct scmi_info *info, struct device_node *of_node,
  2350. int prot_id, bool tx)
  2351. {
  2352. int ret, idx;
  2353. char name[32];
  2354. struct scmi_chan_info *cinfo;
  2355. struct idr *idr;
  2356. struct scmi_device *tdev = NULL;
  2357. /* Transmit channel is first entry i.e. index 0 */
  2358. idx = tx ? 0 : 1;
  2359. idr = tx ? &info->tx_idr : &info->rx_idr;
  2360. if (!info->desc->ops->chan_available(of_node, idx)) {
  2361. cinfo = idr_find(idr, SCMI_PROTOCOL_BASE);
  2362. if (unlikely(!cinfo)) /* Possible only if platform has no Rx */
  2363. return -EINVAL;
  2364. goto idr_alloc;
  2365. }
  2366. cinfo = devm_kzalloc(info->dev, sizeof(*cinfo), GFP_KERNEL);
  2367. if (!cinfo)
  2368. return -ENOMEM;
  2369. cinfo->is_p2a = !tx;
  2370. cinfo->rx_timeout_ms = info->desc->max_rx_timeout_ms;
  2371. cinfo->max_msg_size = info->desc->max_msg_size;
  2372. /* Create a unique name for this transport device */
  2373. snprintf(name, 32, "__scmi_transport_device_%s_%02X",
  2374. idx ? "rx" : "tx", prot_id);
  2375. /* Create a uniquely named, dedicated transport device for this chan */
  2376. tdev = scmi_device_create(of_node, info->dev, prot_id, name);
  2377. if (!tdev) {
  2378. dev_err(info->dev,
  2379. "failed to create transport device (%s)\n", name);
  2380. devm_kfree(info->dev, cinfo);
  2381. return -EINVAL;
  2382. }
  2383. of_node_get(of_node);
  2384. cinfo->id = prot_id;
  2385. cinfo->dev = &tdev->dev;
  2386. ret = info->desc->ops->chan_setup(cinfo, info->dev, tx);
  2387. if (ret) {
  2388. of_node_put(of_node);
  2389. scmi_device_destroy(info->dev, prot_id, name);
  2390. devm_kfree(info->dev, cinfo);
  2391. return ret;
  2392. }
  2393. if (tx && is_polling_required(cinfo, info->desc)) {
  2394. if (is_transport_polling_capable(info->desc))
  2395. dev_info(&tdev->dev,
  2396. "Enabled polling mode TX channel - prot_id:%d\n",
  2397. prot_id);
  2398. else
  2399. dev_warn(&tdev->dev,
  2400. "Polling mode NOT supported by transport.\n");
  2401. }
  2402. idr_alloc:
  2403. ret = idr_alloc(idr, cinfo, prot_id, prot_id + 1, GFP_KERNEL);
  2404. if (ret != prot_id) {
  2405. dev_err(info->dev,
  2406. "unable to allocate SCMI idr slot err %d\n", ret);
  2407. /* Destroy channel and device only if created by this call. */
  2408. if (tdev) {
  2409. of_node_put(of_node);
  2410. scmi_device_destroy(info->dev, prot_id, name);
  2411. devm_kfree(info->dev, cinfo);
  2412. }
  2413. return ret;
  2414. }
  2415. cinfo->handle = &info->handle;
  2416. return 0;
  2417. }
  2418. static inline int
  2419. scmi_txrx_setup(struct scmi_info *info, struct device_node *of_node,
  2420. int prot_id)
  2421. {
  2422. int ret = scmi_chan_setup(info, of_node, prot_id, true);
  2423. if (!ret) {
  2424. /* Rx is optional, report only memory errors */
  2425. ret = scmi_chan_setup(info, of_node, prot_id, false);
  2426. if (ret && ret != -ENOMEM)
  2427. ret = 0;
  2428. }
  2429. if (ret)
  2430. dev_err(info->dev,
  2431. "failed to setup channel for protocol:0x%X\n", prot_id);
  2432. return ret;
  2433. }
  2434. /**
  2435. * scmi_channels_setup - Helper to initialize all required channels
  2436. *
  2437. * @info: The SCMI instance descriptor.
  2438. *
  2439. * Initialize all the channels found described in the DT against the underlying
  2440. * configured transport using custom defined dedicated devices instead of
  2441. * borrowing devices from the SCMI drivers; this way channels are initialized
  2442. * upfront during core SCMI stack probing and are no more coupled with SCMI
  2443. * devices used by SCMI drivers.
  2444. *
  2445. * Note that, even though a pair of TX/RX channels is associated to each
  2446. * protocol defined in the DT, a distinct freshly initialized channel is
  2447. * created only if the DT node for the protocol at hand describes a dedicated
  2448. * channel: in all the other cases the common BASE protocol channel is reused.
  2449. *
  2450. * Return: 0 on Success
  2451. */
  2452. static int scmi_channels_setup(struct scmi_info *info)
  2453. {
  2454. int ret;
  2455. struct device_node *top_np = info->dev->of_node;
  2456. /* Initialize a common generic channel at first */
  2457. ret = scmi_txrx_setup(info, top_np, SCMI_PROTOCOL_BASE);
  2458. if (ret)
  2459. return ret;
  2460. for_each_available_child_of_node_scoped(top_np, child) {
  2461. u32 prot_id;
  2462. if (of_property_read_u32(child, "reg", &prot_id))
  2463. continue;
  2464. if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK, prot_id))
  2465. dev_err(info->dev,
  2466. "Out of range protocol %d\n", prot_id);
  2467. ret = scmi_txrx_setup(info, child, prot_id);
  2468. if (ret)
  2469. return ret;
  2470. }
  2471. return 0;
  2472. }
  2473. static int scmi_chan_destroy(int id, void *p, void *idr)
  2474. {
  2475. struct scmi_chan_info *cinfo = p;
  2476. if (cinfo->dev) {
  2477. struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
  2478. struct scmi_device *sdev = to_scmi_dev(cinfo->dev);
  2479. of_node_put(cinfo->dev->of_node);
  2480. scmi_device_destroy(info->dev, id, sdev->name);
  2481. cinfo->dev = NULL;
  2482. }
  2483. idr_remove(idr, id);
  2484. return 0;
  2485. }
  2486. static void scmi_cleanup_channels(struct scmi_info *info, struct idr *idr)
  2487. {
  2488. /* At first free all channels at the transport layer ... */
  2489. idr_for_each(idr, info->desc->ops->chan_free, idr);
  2490. /* ...then destroy all underlying devices */
  2491. idr_for_each(idr, scmi_chan_destroy, idr);
  2492. idr_destroy(idr);
  2493. }
  2494. static void scmi_cleanup_txrx_channels(struct scmi_info *info)
  2495. {
  2496. scmi_cleanup_channels(info, &info->tx_idr);
  2497. scmi_cleanup_channels(info, &info->rx_idr);
  2498. }
  2499. static int scmi_bus_notifier(struct notifier_block *nb,
  2500. unsigned long action, void *data)
  2501. {
  2502. struct scmi_info *info = bus_nb_to_scmi_info(nb);
  2503. struct scmi_device *sdev = to_scmi_dev(data);
  2504. /* Skip devices of different SCMI instances */
  2505. if (sdev->dev.parent != info->dev)
  2506. return NOTIFY_DONE;
  2507. switch (action) {
  2508. case BUS_NOTIFY_BIND_DRIVER:
  2509. /* setup handle now as the transport is ready */
  2510. scmi_set_handle(sdev);
  2511. break;
  2512. case BUS_NOTIFY_UNBOUND_DRIVER:
  2513. scmi_handle_put(sdev->handle);
  2514. sdev->handle = NULL;
  2515. break;
  2516. default:
  2517. return NOTIFY_DONE;
  2518. }
  2519. dev_dbg(info->dev, "Device %s (%s) is now %s\n", dev_name(&sdev->dev),
  2520. sdev->name, action == BUS_NOTIFY_BIND_DRIVER ?
  2521. "about to be BOUND." : "UNBOUND.");
  2522. return NOTIFY_OK;
  2523. }
  2524. static int scmi_device_request_notifier(struct notifier_block *nb,
  2525. unsigned long action, void *data)
  2526. {
  2527. struct device_node *np;
  2528. struct scmi_device_id *id_table = data;
  2529. struct scmi_info *info = req_nb_to_scmi_info(nb);
  2530. np = idr_find(&info->active_protocols, id_table->protocol_id);
  2531. if (!np)
  2532. return NOTIFY_DONE;
  2533. dev_dbg(info->dev, "%sRequested device (%s) for protocol 0x%x\n",
  2534. action == SCMI_BUS_NOTIFY_DEVICE_REQUEST ? "" : "UN-",
  2535. id_table->name, id_table->protocol_id);
  2536. switch (action) {
  2537. case SCMI_BUS_NOTIFY_DEVICE_REQUEST:
  2538. scmi_create_protocol_devices(np, info, id_table->protocol_id,
  2539. id_table->name);
  2540. break;
  2541. case SCMI_BUS_NOTIFY_DEVICE_UNREQUEST:
  2542. scmi_destroy_protocol_devices(info, id_table->protocol_id,
  2543. id_table->name);
  2544. break;
  2545. default:
  2546. return NOTIFY_DONE;
  2547. }
  2548. return NOTIFY_OK;
  2549. }
  2550. static const char * const dbg_counter_strs[] = {
  2551. "sent_ok",
  2552. "sent_fail",
  2553. "sent_fail_polling_unsupported",
  2554. "sent_fail_channel_not_found",
  2555. "response_ok",
  2556. "notification_ok",
  2557. "delayed_response_ok",
  2558. "xfers_response_timeout",
  2559. "xfers_response_polled_timeout",
  2560. "response_polled_ok",
  2561. "err_msg_unexpected",
  2562. "err_msg_invalid",
  2563. "err_msg_nomem",
  2564. "err_protocol",
  2565. "xfers_inflight",
  2566. };
  2567. static ssize_t reset_all_on_write(struct file *filp, const char __user *buf,
  2568. size_t count, loff_t *ppos)
  2569. {
  2570. struct scmi_debug_info *dbg = filp->private_data;
  2571. for (int i = 0; i < SCMI_DEBUG_COUNTERS_LAST; i++)
  2572. atomic_set(&dbg->counters[i], 0);
  2573. return count;
  2574. }
  2575. static const struct file_operations fops_reset_counts = {
  2576. .owner = THIS_MODULE,
  2577. .open = simple_open,
  2578. .write = reset_all_on_write,
  2579. };
  2580. static void scmi_debugfs_counters_setup(struct scmi_debug_info *dbg,
  2581. struct dentry *trans)
  2582. {
  2583. struct dentry *counters;
  2584. int idx;
  2585. counters = debugfs_create_dir("counters", trans);
  2586. for (idx = 0; idx < SCMI_DEBUG_COUNTERS_LAST; idx++)
  2587. debugfs_create_atomic_t(dbg_counter_strs[idx], 0600, counters,
  2588. &dbg->counters[idx]);
  2589. debugfs_create_file("reset", 0200, counters, dbg, &fops_reset_counts);
  2590. }
  2591. static void scmi_debugfs_common_cleanup(void *d)
  2592. {
  2593. struct scmi_debug_info *dbg = d;
  2594. if (!dbg)
  2595. return;
  2596. debugfs_remove_recursive(dbg->top_dentry);
  2597. kfree(dbg->name);
  2598. kfree(dbg->type);
  2599. }
  2600. static struct scmi_debug_info *scmi_debugfs_common_setup(struct scmi_info *info)
  2601. {
  2602. char top_dir[16];
  2603. struct dentry *trans, *top_dentry;
  2604. struct scmi_debug_info *dbg;
  2605. const char *c_ptr = NULL;
  2606. dbg = devm_kzalloc(info->dev, sizeof(*dbg), GFP_KERNEL);
  2607. if (!dbg)
  2608. return NULL;
  2609. dbg->name = kstrdup(of_node_full_name(info->dev->of_node), GFP_KERNEL);
  2610. if (!dbg->name) {
  2611. devm_kfree(info->dev, dbg);
  2612. return NULL;
  2613. }
  2614. of_property_read_string(info->dev->of_node, "compatible", &c_ptr);
  2615. dbg->type = kstrdup(c_ptr, GFP_KERNEL);
  2616. if (!dbg->type) {
  2617. kfree(dbg->name);
  2618. devm_kfree(info->dev, dbg);
  2619. return NULL;
  2620. }
  2621. snprintf(top_dir, 16, "%d", info->id);
  2622. top_dentry = debugfs_create_dir(top_dir, scmi_top_dentry);
  2623. trans = debugfs_create_dir("transport", top_dentry);
  2624. dbg->is_atomic = info->desc->atomic_enabled &&
  2625. is_transport_polling_capable(info->desc);
  2626. debugfs_create_str("instance_name", 0400, top_dentry,
  2627. (char **)&dbg->name);
  2628. debugfs_create_u32("atomic_threshold_us", 0400, top_dentry,
  2629. (u32 *)&info->desc->atomic_threshold);
  2630. debugfs_create_str("type", 0400, trans, (char **)&dbg->type);
  2631. debugfs_create_bool("is_atomic", 0400, trans, &dbg->is_atomic);
  2632. debugfs_create_u32("max_rx_timeout_ms", 0400, trans,
  2633. (u32 *)&info->desc->max_rx_timeout_ms);
  2634. debugfs_create_u32("max_msg_size", 0400, trans,
  2635. (u32 *)&info->desc->max_msg_size);
  2636. debugfs_create_u32("tx_max_msg", 0400, trans,
  2637. (u32 *)&info->tx_minfo.max_msg);
  2638. debugfs_create_u32("rx_max_msg", 0400, trans,
  2639. (u32 *)&info->rx_minfo.max_msg);
  2640. if (IS_ENABLED(CONFIG_ARM_SCMI_DEBUG_COUNTERS))
  2641. scmi_debugfs_counters_setup(dbg, trans);
  2642. dbg->top_dentry = top_dentry;
  2643. if (devm_add_action_or_reset(info->dev,
  2644. scmi_debugfs_common_cleanup, dbg))
  2645. return NULL;
  2646. return dbg;
  2647. }
  2648. static int scmi_debugfs_raw_mode_setup(struct scmi_info *info)
  2649. {
  2650. int id, num_chans = 0, ret = 0;
  2651. struct scmi_chan_info *cinfo;
  2652. u8 channels[SCMI_MAX_CHANNELS] = {};
  2653. DECLARE_BITMAP(protos, SCMI_MAX_CHANNELS) = {};
  2654. /* Enumerate all channels to collect their ids */
  2655. idr_for_each_entry(&info->tx_idr, cinfo, id) {
  2656. /*
  2657. * Cannot happen, but be defensive.
  2658. * Zero as num_chans is ok, warn and carry on.
  2659. */
  2660. if (num_chans >= SCMI_MAX_CHANNELS || !cinfo) {
  2661. dev_warn(info->dev,
  2662. "SCMI RAW - Error enumerating channels\n");
  2663. break;
  2664. }
  2665. if (!test_bit(cinfo->id, protos)) {
  2666. channels[num_chans++] = cinfo->id;
  2667. set_bit(cinfo->id, protos);
  2668. }
  2669. }
  2670. info->raw = scmi_raw_mode_init(&info->handle, info->dbg->top_dentry,
  2671. info->id, channels, num_chans,
  2672. info->desc, info->tx_minfo.max_msg);
  2673. if (IS_ERR(info->raw)) {
  2674. dev_err(info->dev, "Failed to initialize SCMI RAW Mode !\n");
  2675. ret = PTR_ERR(info->raw);
  2676. info->raw = NULL;
  2677. }
  2678. return ret;
  2679. }
  2680. static const struct scmi_desc *scmi_transport_setup(struct device *dev)
  2681. {
  2682. struct scmi_transport *trans;
  2683. int ret;
  2684. trans = dev_get_platdata(dev);
  2685. if (!trans || !trans->supplier || !trans->core_ops)
  2686. return NULL;
  2687. if (!device_link_add(dev, trans->supplier, DL_FLAG_AUTOREMOVE_CONSUMER)) {
  2688. dev_err(dev,
  2689. "Adding link to supplier transport device failed\n");
  2690. return NULL;
  2691. }
  2692. /* Provide core transport ops */
  2693. *trans->core_ops = &scmi_trans_core_ops;
  2694. dev_info(dev, "Using %s\n", dev_driver_string(trans->supplier));
  2695. ret = of_property_read_u32(dev->of_node, "arm,max-rx-timeout-ms",
  2696. &trans->desc.max_rx_timeout_ms);
  2697. if (ret && ret != -EINVAL)
  2698. dev_err(dev, "Malformed arm,max-rx-timeout-ms DT property.\n");
  2699. ret = of_property_read_u32(dev->of_node, "arm,max-msg-size",
  2700. &trans->desc.max_msg_size);
  2701. if (ret && ret != -EINVAL)
  2702. dev_err(dev, "Malformed arm,max-msg-size DT property.\n");
  2703. ret = of_property_read_u32(dev->of_node, "arm,max-msg",
  2704. &trans->desc.max_msg);
  2705. if (ret && ret != -EINVAL)
  2706. dev_err(dev, "Malformed arm,max-msg DT property.\n");
  2707. dev_info(dev,
  2708. "SCMI max-rx-timeout: %dms / max-msg-size: %dbytes / max-msg: %d\n",
  2709. trans->desc.max_rx_timeout_ms, trans->desc.max_msg_size,
  2710. trans->desc.max_msg);
  2711. /* System wide atomic threshold for atomic ops .. if any */
  2712. if (!of_property_read_u32(dev->of_node, "atomic-threshold-us",
  2713. &trans->desc.atomic_threshold))
  2714. dev_info(dev,
  2715. "SCMI System wide atomic threshold set to %u us\n",
  2716. trans->desc.atomic_threshold);
  2717. return &trans->desc;
  2718. }
  2719. static void scmi_enable_matching_quirks(struct scmi_info *info)
  2720. {
  2721. struct scmi_revision_info *rev = &info->version;
  2722. dev_dbg(info->dev, "Looking for quirks matching: %s/%s/0x%08X\n",
  2723. rev->vendor_id, rev->sub_vendor_id, rev->impl_ver);
  2724. /* Enable applicable quirks */
  2725. scmi_quirks_enable(info->dev, rev->vendor_id,
  2726. rev->sub_vendor_id, rev->impl_ver);
  2727. }
  2728. static int scmi_probe(struct platform_device *pdev)
  2729. {
  2730. int ret;
  2731. char *err_str = "probe failure\n";
  2732. struct scmi_handle *handle;
  2733. const struct scmi_desc *desc;
  2734. struct scmi_info *info;
  2735. bool coex = IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT_COEX);
  2736. struct device *dev = &pdev->dev;
  2737. struct device_node *child, *np = dev->of_node;
  2738. desc = scmi_transport_setup(dev);
  2739. if (!desc) {
  2740. err_str = "transport invalid\n";
  2741. ret = -EINVAL;
  2742. goto out_err;
  2743. }
  2744. info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  2745. if (!info)
  2746. return -ENOMEM;
  2747. info->id = ida_alloc_min(&scmi_id, 0, GFP_KERNEL);
  2748. if (info->id < 0)
  2749. return info->id;
  2750. info->dev = dev;
  2751. info->desc = desc;
  2752. info->bus_nb.notifier_call = scmi_bus_notifier;
  2753. info->dev_req_nb.notifier_call = scmi_device_request_notifier;
  2754. INIT_LIST_HEAD(&info->node);
  2755. idr_init(&info->protocols);
  2756. mutex_init(&info->protocols_mtx);
  2757. idr_init(&info->active_protocols);
  2758. mutex_init(&info->devreq_mtx);
  2759. platform_set_drvdata(pdev, info);
  2760. idr_init(&info->tx_idr);
  2761. idr_init(&info->rx_idr);
  2762. handle = &info->handle;
  2763. handle->dev = info->dev;
  2764. handle->version = &info->version;
  2765. handle->devm_protocol_acquire = scmi_devm_protocol_acquire;
  2766. handle->devm_protocol_get = scmi_devm_protocol_get;
  2767. handle->devm_protocol_put = scmi_devm_protocol_put;
  2768. handle->is_transport_atomic = scmi_is_transport_atomic;
  2769. /* Setup all channels described in the DT at first */
  2770. ret = scmi_channels_setup(info);
  2771. if (ret) {
  2772. err_str = "failed to setup channels\n";
  2773. goto clear_ida;
  2774. }
  2775. ret = bus_register_notifier(&scmi_bus_type, &info->bus_nb);
  2776. if (ret) {
  2777. err_str = "failed to register bus notifier\n";
  2778. goto clear_txrx_setup;
  2779. }
  2780. ret = blocking_notifier_chain_register(&scmi_requested_devices_nh,
  2781. &info->dev_req_nb);
  2782. if (ret) {
  2783. err_str = "failed to register device notifier\n";
  2784. goto clear_bus_notifier;
  2785. }
  2786. ret = scmi_xfer_info_init(info);
  2787. if (ret) {
  2788. err_str = "failed to init xfers pool\n";
  2789. goto clear_dev_req_notifier;
  2790. }
  2791. if (scmi_top_dentry) {
  2792. info->dbg = scmi_debugfs_common_setup(info);
  2793. if (!info->dbg)
  2794. dev_warn(dev, "Failed to setup SCMI debugfs.\n");
  2795. if (info->dbg && IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT)) {
  2796. ret = scmi_debugfs_raw_mode_setup(info);
  2797. if (!coex) {
  2798. if (ret)
  2799. goto clear_dev_req_notifier;
  2800. /* Bail out anyway when coex disabled. */
  2801. return 0;
  2802. }
  2803. /* Coex enabled, carry on in any case. */
  2804. dev_info(dev, "SCMI RAW Mode COEX enabled !\n");
  2805. }
  2806. }
  2807. if (scmi_notification_init(handle))
  2808. dev_err(dev, "SCMI Notifications NOT available.\n");
  2809. if (info->desc->atomic_enabled &&
  2810. !is_transport_polling_capable(info->desc))
  2811. dev_err(dev,
  2812. "Transport is not polling capable. Atomic mode not supported.\n");
  2813. /*
  2814. * Trigger SCMI Base protocol initialization.
  2815. * It's mandatory and won't be ever released/deinit until the
  2816. * SCMI stack is shutdown/unloaded as a whole.
  2817. */
  2818. ret = scmi_protocol_acquire(handle, SCMI_PROTOCOL_BASE);
  2819. if (ret) {
  2820. err_str = "unable to communicate with SCMI\n";
  2821. if (coex) {
  2822. dev_err(dev, "%s", err_str);
  2823. return 0;
  2824. }
  2825. goto notification_exit;
  2826. }
  2827. mutex_lock(&scmi_list_mutex);
  2828. list_add_tail(&info->node, &scmi_list);
  2829. mutex_unlock(&scmi_list_mutex);
  2830. scmi_enable_matching_quirks(info);
  2831. for_each_available_child_of_node(np, child) {
  2832. u32 prot_id;
  2833. if (of_property_read_u32(child, "reg", &prot_id))
  2834. continue;
  2835. if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK, prot_id))
  2836. dev_err(dev, "Out of range protocol %d\n", prot_id);
  2837. if (!scmi_is_protocol_implemented(handle, prot_id)) {
  2838. dev_err(dev, "SCMI protocol %d not implemented\n",
  2839. prot_id);
  2840. continue;
  2841. }
  2842. /*
  2843. * Save this valid DT protocol descriptor amongst
  2844. * @active_protocols for this SCMI instance/
  2845. */
  2846. ret = idr_alloc(&info->active_protocols, child,
  2847. prot_id, prot_id + 1, GFP_KERNEL);
  2848. if (ret != prot_id) {
  2849. dev_err(dev, "SCMI protocol %d already activated. Skip\n",
  2850. prot_id);
  2851. continue;
  2852. }
  2853. of_node_get(child);
  2854. scmi_create_protocol_devices(child, info, prot_id, NULL);
  2855. }
  2856. return 0;
  2857. notification_exit:
  2858. if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT))
  2859. scmi_raw_mode_cleanup(info->raw);
  2860. scmi_notification_exit(&info->handle);
  2861. clear_dev_req_notifier:
  2862. blocking_notifier_chain_unregister(&scmi_requested_devices_nh,
  2863. &info->dev_req_nb);
  2864. clear_bus_notifier:
  2865. bus_unregister_notifier(&scmi_bus_type, &info->bus_nb);
  2866. clear_txrx_setup:
  2867. scmi_cleanup_txrx_channels(info);
  2868. clear_ida:
  2869. ida_free(&scmi_id, info->id);
  2870. out_err:
  2871. return dev_err_probe(dev, ret, "%s", err_str);
  2872. }
  2873. static void scmi_remove(struct platform_device *pdev)
  2874. {
  2875. int id;
  2876. struct scmi_info *info = platform_get_drvdata(pdev);
  2877. struct device_node *child;
  2878. if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT))
  2879. scmi_raw_mode_cleanup(info->raw);
  2880. mutex_lock(&scmi_list_mutex);
  2881. if (info->users)
  2882. dev_warn(&pdev->dev,
  2883. "Still active SCMI users will be forcibly unbound.\n");
  2884. list_del(&info->node);
  2885. mutex_unlock(&scmi_list_mutex);
  2886. scmi_notification_exit(&info->handle);
  2887. mutex_lock(&info->protocols_mtx);
  2888. idr_destroy(&info->protocols);
  2889. mutex_unlock(&info->protocols_mtx);
  2890. idr_for_each_entry(&info->active_protocols, child, id)
  2891. of_node_put(child);
  2892. idr_destroy(&info->active_protocols);
  2893. blocking_notifier_chain_unregister(&scmi_requested_devices_nh,
  2894. &info->dev_req_nb);
  2895. bus_unregister_notifier(&scmi_bus_type, &info->bus_nb);
  2896. /* Safe to free channels since no more users */
  2897. scmi_cleanup_txrx_channels(info);
  2898. ida_free(&scmi_id, info->id);
  2899. }
  2900. static ssize_t protocol_version_show(struct device *dev,
  2901. struct device_attribute *attr, char *buf)
  2902. {
  2903. struct scmi_info *info = dev_get_drvdata(dev);
  2904. return sprintf(buf, "%u.%u\n", info->version.major_ver,
  2905. info->version.minor_ver);
  2906. }
  2907. static DEVICE_ATTR_RO(protocol_version);
  2908. static ssize_t firmware_version_show(struct device *dev,
  2909. struct device_attribute *attr, char *buf)
  2910. {
  2911. struct scmi_info *info = dev_get_drvdata(dev);
  2912. return sprintf(buf, "0x%x\n", info->version.impl_ver);
  2913. }
  2914. static DEVICE_ATTR_RO(firmware_version);
  2915. static ssize_t vendor_id_show(struct device *dev,
  2916. struct device_attribute *attr, char *buf)
  2917. {
  2918. struct scmi_info *info = dev_get_drvdata(dev);
  2919. return sprintf(buf, "%s\n", info->version.vendor_id);
  2920. }
  2921. static DEVICE_ATTR_RO(vendor_id);
  2922. static ssize_t sub_vendor_id_show(struct device *dev,
  2923. struct device_attribute *attr, char *buf)
  2924. {
  2925. struct scmi_info *info = dev_get_drvdata(dev);
  2926. return sprintf(buf, "%s\n", info->version.sub_vendor_id);
  2927. }
  2928. static DEVICE_ATTR_RO(sub_vendor_id);
  2929. static struct attribute *versions_attrs[] = {
  2930. &dev_attr_firmware_version.attr,
  2931. &dev_attr_protocol_version.attr,
  2932. &dev_attr_vendor_id.attr,
  2933. &dev_attr_sub_vendor_id.attr,
  2934. NULL,
  2935. };
  2936. ATTRIBUTE_GROUPS(versions);
  2937. static struct platform_driver scmi_driver = {
  2938. .driver = {
  2939. .name = "arm-scmi",
  2940. .suppress_bind_attrs = true,
  2941. .dev_groups = versions_groups,
  2942. },
  2943. .probe = scmi_probe,
  2944. .remove = scmi_remove,
  2945. };
  2946. static struct dentry *scmi_debugfs_init(void)
  2947. {
  2948. struct dentry *d;
  2949. d = debugfs_create_dir("scmi", NULL);
  2950. if (IS_ERR(d)) {
  2951. pr_err("Could NOT create SCMI top dentry.\n");
  2952. return NULL;
  2953. }
  2954. return d;
  2955. }
  2956. int scmi_inflight_count(const struct scmi_handle *handle)
  2957. {
  2958. if (IS_ENABLED(CONFIG_ARM_SCMI_DEBUG_COUNTERS)) {
  2959. struct scmi_info *info = handle_to_scmi_info(handle);
  2960. if (!info->dbg)
  2961. return 0;
  2962. return atomic_read(&info->dbg->counters[XFERS_INFLIGHT]);
  2963. } else {
  2964. return 0;
  2965. }
  2966. }
  2967. static int __init scmi_driver_init(void)
  2968. {
  2969. scmi_quirks_initialize();
  2970. /* Bail out if no SCMI transport was configured */
  2971. if (WARN_ON(!IS_ENABLED(CONFIG_ARM_SCMI_HAVE_TRANSPORT)))
  2972. return -EINVAL;
  2973. if (IS_ENABLED(CONFIG_ARM_SCMI_HAVE_SHMEM))
  2974. scmi_trans_core_ops.shmem = scmi_shared_mem_operations_get();
  2975. if (IS_ENABLED(CONFIG_ARM_SCMI_HAVE_MSG))
  2976. scmi_trans_core_ops.msg = scmi_message_operations_get();
  2977. if (IS_ENABLED(CONFIG_ARM_SCMI_NEED_DEBUGFS))
  2978. scmi_top_dentry = scmi_debugfs_init();
  2979. scmi_base_register();
  2980. scmi_clock_register();
  2981. scmi_perf_register();
  2982. scmi_power_register();
  2983. scmi_reset_register();
  2984. scmi_sensors_register();
  2985. scmi_voltage_register();
  2986. scmi_system_register();
  2987. scmi_powercap_register();
  2988. scmi_pinctrl_register();
  2989. return platform_driver_register(&scmi_driver);
  2990. }
  2991. module_init(scmi_driver_init);
  2992. static void __exit scmi_driver_exit(void)
  2993. {
  2994. scmi_base_unregister();
  2995. scmi_clock_unregister();
  2996. scmi_perf_unregister();
  2997. scmi_power_unregister();
  2998. scmi_reset_unregister();
  2999. scmi_sensors_unregister();
  3000. scmi_voltage_unregister();
  3001. scmi_system_unregister();
  3002. scmi_powercap_unregister();
  3003. scmi_pinctrl_unregister();
  3004. platform_driver_unregister(&scmi_driver);
  3005. debugfs_remove_recursive(scmi_top_dentry);
  3006. }
  3007. module_exit(scmi_driver_exit);
  3008. MODULE_ALIAS("platform:arm-scmi");
  3009. MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
  3010. MODULE_DESCRIPTION("ARM SCMI protocol driver");
  3011. MODULE_LICENSE("GPL v2");