driver.c 94 KB

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