ti_sci.c 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Texas Instruments System Control Interface Protocol Driver
  4. * Based on drivers/firmware/ti_sci.c from Linux.
  5. *
  6. * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/
  7. * Lokesh Vutla <lokeshvutla@ti.com>
  8. */
  9. #include <common.h>
  10. #include <dm.h>
  11. #include <errno.h>
  12. #include <mailbox.h>
  13. #include <malloc.h>
  14. #include <dm/device.h>
  15. #include <dm/device_compat.h>
  16. #include <dm/devres.h>
  17. #include <linux/compat.h>
  18. #include <linux/err.h>
  19. #include <linux/soc/ti/k3-sec-proxy.h>
  20. #include <linux/soc/ti/ti_sci_protocol.h>
  21. #include "ti_sci.h"
  22. /* List of all TI SCI devices active in system */
  23. static LIST_HEAD(ti_sci_list);
  24. /**
  25. * struct ti_sci_xfer - Structure representing a message flow
  26. * @tx_message: Transmit message
  27. * @rx_len: Receive message length
  28. */
  29. struct ti_sci_xfer {
  30. struct k3_sec_proxy_msg tx_message;
  31. u8 rx_len;
  32. };
  33. /**
  34. * struct ti_sci_rm_type_map - Structure representing TISCI Resource
  35. * management representation of dev_ids.
  36. * @dev_id: TISCI device ID
  37. * @type: Corresponding id as identified by TISCI RM.
  38. *
  39. * Note: This is used only as a work around for using RM range apis
  40. * for AM654 SoC. For future SoCs dev_id will be used as type
  41. * for RM range APIs. In order to maintain ABI backward compatibility
  42. * type is not being changed for AM654 SoC.
  43. */
  44. struct ti_sci_rm_type_map {
  45. u32 dev_id;
  46. u16 type;
  47. };
  48. /**
  49. * struct ti_sci_desc - Description of SoC integration
  50. * @default_host_id: Host identifier representing the compute entity
  51. * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds)
  52. * @max_msgs: Maximum number of messages that can be pending
  53. * simultaneously in the system
  54. * @max_msg_size: Maximum size of data per message that can be handled.
  55. * @rm_type_map: RM resource type mapping structure.
  56. */
  57. struct ti_sci_desc {
  58. u8 default_host_id;
  59. int max_rx_timeout_ms;
  60. int max_msgs;
  61. int max_msg_size;
  62. struct ti_sci_rm_type_map *rm_type_map;
  63. };
  64. /**
  65. * struct ti_sci_info - Structure representing a TI SCI instance
  66. * @dev: Device pointer
  67. * @desc: SoC description for this instance
  68. * @handle: Instance of TI SCI handle to send to clients.
  69. * @chan_tx: Transmit mailbox channel
  70. * @chan_rx: Receive mailbox channel
  71. * @xfer: xfer info
  72. * @list: list head
  73. * @is_secure: Determines if the communication is through secure threads.
  74. * @host_id: Host identifier representing the compute entity
  75. * @seq: Seq id used for verification for tx and rx message.
  76. */
  77. struct ti_sci_info {
  78. struct udevice *dev;
  79. const struct ti_sci_desc *desc;
  80. struct ti_sci_handle handle;
  81. struct mbox_chan chan_tx;
  82. struct mbox_chan chan_rx;
  83. struct mbox_chan chan_notify;
  84. struct ti_sci_xfer xfer;
  85. struct list_head list;
  86. struct list_head dev_list;
  87. bool is_secure;
  88. u8 host_id;
  89. u8 seq;
  90. };
  91. struct ti_sci_exclusive_dev {
  92. u32 id;
  93. u32 count;
  94. struct list_head list;
  95. };
  96. #define handle_to_ti_sci_info(h) container_of(h, struct ti_sci_info, handle)
  97. /**
  98. * ti_sci_setup_one_xfer() - Setup one message type
  99. * @info: Pointer to SCI entity information
  100. * @msg_type: Message type
  101. * @msg_flags: Flag to set for the message
  102. * @buf: Buffer to be send to mailbox channel
  103. * @tx_message_size: transmit message size
  104. * @rx_message_size: receive message size. may be set to zero for send-only
  105. * transactions.
  106. *
  107. * Helper function which is used by various command functions that are
  108. * exposed to clients of this driver for allocating a message traffic event.
  109. *
  110. * Return: Corresponding ti_sci_xfer pointer if all went fine,
  111. * else appropriate error pointer.
  112. */
  113. static struct ti_sci_xfer *ti_sci_setup_one_xfer(struct ti_sci_info *info,
  114. u16 msg_type, u32 msg_flags,
  115. u32 *buf,
  116. size_t tx_message_size,
  117. size_t rx_message_size)
  118. {
  119. struct ti_sci_xfer *xfer = &info->xfer;
  120. struct ti_sci_msg_hdr *hdr;
  121. /* Ensure we have sane transfer sizes */
  122. if (rx_message_size > info->desc->max_msg_size ||
  123. tx_message_size > info->desc->max_msg_size ||
  124. (rx_message_size > 0 && rx_message_size < sizeof(*hdr)) ||
  125. tx_message_size < sizeof(*hdr))
  126. return ERR_PTR(-ERANGE);
  127. info->seq = ~info->seq;
  128. xfer->tx_message.buf = buf;
  129. xfer->tx_message.len = tx_message_size;
  130. xfer->rx_len = (u8)rx_message_size;
  131. hdr = (struct ti_sci_msg_hdr *)buf;
  132. hdr->seq = info->seq;
  133. hdr->type = msg_type;
  134. hdr->host = info->host_id;
  135. hdr->flags = msg_flags;
  136. return xfer;
  137. }
  138. /**
  139. * ti_sci_get_response() - Receive response from mailbox channel
  140. * @info: Pointer to SCI entity information
  141. * @xfer: Transfer to initiate and wait for response
  142. * @chan: Channel to receive the response
  143. *
  144. * Return: -ETIMEDOUT in case of no response, if transmit error,
  145. * return corresponding error, else if all goes well,
  146. * return 0.
  147. */
  148. static inline int ti_sci_get_response(struct ti_sci_info *info,
  149. struct ti_sci_xfer *xfer,
  150. struct mbox_chan *chan)
  151. {
  152. struct k3_sec_proxy_msg *msg = &xfer->tx_message;
  153. struct ti_sci_secure_msg_hdr *secure_hdr;
  154. struct ti_sci_msg_hdr *hdr;
  155. int ret;
  156. /* Receive the response */
  157. ret = mbox_recv(chan, msg, info->desc->max_rx_timeout_ms * 1000);
  158. if (ret) {
  159. dev_err(info->dev, "%s: Message receive failed. ret = %d\n",
  160. __func__, ret);
  161. return ret;
  162. }
  163. /* ToDo: Verify checksum */
  164. if (info->is_secure) {
  165. secure_hdr = (struct ti_sci_secure_msg_hdr *)msg->buf;
  166. msg->buf = (u32 *)((void *)msg->buf + sizeof(*secure_hdr));
  167. }
  168. /* msg is updated by mailbox driver */
  169. hdr = (struct ti_sci_msg_hdr *)msg->buf;
  170. /* Sanity check for message response */
  171. if (hdr->seq != info->seq) {
  172. dev_dbg(info->dev, "%s: Message for %d is not expected\n",
  173. __func__, hdr->seq);
  174. return ret;
  175. }
  176. if (msg->len > info->desc->max_msg_size) {
  177. dev_err(info->dev, "%s: Unable to handle %zu xfer (max %d)\n",
  178. __func__, msg->len, info->desc->max_msg_size);
  179. return -EINVAL;
  180. }
  181. if (msg->len < xfer->rx_len) {
  182. dev_err(info->dev, "%s: Recv xfer %zu < expected %d length\n",
  183. __func__, msg->len, xfer->rx_len);
  184. }
  185. return ret;
  186. }
  187. /**
  188. * ti_sci_do_xfer() - Do one transfer
  189. * @info: Pointer to SCI entity information
  190. * @xfer: Transfer to initiate and wait for response
  191. *
  192. * Return: 0 if all went fine, else return appropriate error.
  193. */
  194. static inline int ti_sci_do_xfer(struct ti_sci_info *info,
  195. struct ti_sci_xfer *xfer)
  196. {
  197. struct k3_sec_proxy_msg *msg = &xfer->tx_message;
  198. u8 secure_buf[info->desc->max_msg_size];
  199. struct ti_sci_secure_msg_hdr secure_hdr;
  200. int ret;
  201. if (info->is_secure) {
  202. /* ToDo: get checksum of the entire message */
  203. secure_hdr.checksum = 0;
  204. secure_hdr.reserved = 0;
  205. memcpy(&secure_buf[sizeof(secure_hdr)], xfer->tx_message.buf,
  206. xfer->tx_message.len);
  207. xfer->tx_message.buf = (u32 *)secure_buf;
  208. xfer->tx_message.len += sizeof(secure_hdr);
  209. if (xfer->rx_len)
  210. xfer->rx_len += sizeof(secure_hdr);
  211. }
  212. /* Send the message */
  213. ret = mbox_send(&info->chan_tx, msg);
  214. if (ret) {
  215. dev_err(info->dev, "%s: Message sending failed. ret = %d\n",
  216. __func__, ret);
  217. return ret;
  218. }
  219. /* Get response if requested */
  220. if (xfer->rx_len)
  221. ret = ti_sci_get_response(info, xfer, &info->chan_rx);
  222. return ret;
  223. }
  224. /**
  225. * ti_sci_cmd_get_revision() - command to get the revision of the SCI entity
  226. * @handle: pointer to TI SCI handle
  227. *
  228. * Updates the SCI information in the internal data structure.
  229. *
  230. * Return: 0 if all went fine, else return appropriate error.
  231. */
  232. static int ti_sci_cmd_get_revision(struct ti_sci_handle *handle)
  233. {
  234. struct ti_sci_msg_resp_version *rev_info;
  235. struct ti_sci_version_info *ver;
  236. struct ti_sci_msg_hdr hdr;
  237. struct ti_sci_info *info;
  238. struct ti_sci_xfer *xfer;
  239. int ret;
  240. if (IS_ERR(handle))
  241. return PTR_ERR(handle);
  242. if (!handle)
  243. return -EINVAL;
  244. info = handle_to_ti_sci_info(handle);
  245. xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_VERSION,
  246. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  247. (u32 *)&hdr, sizeof(struct ti_sci_msg_hdr),
  248. sizeof(*rev_info));
  249. if (IS_ERR(xfer)) {
  250. ret = PTR_ERR(xfer);
  251. dev_err(info->dev, "Message alloc failed(%d)\n", ret);
  252. return ret;
  253. }
  254. ret = ti_sci_do_xfer(info, xfer);
  255. if (ret) {
  256. dev_err(info->dev, "Mbox communication fail %d\n", ret);
  257. return ret;
  258. }
  259. rev_info = (struct ti_sci_msg_resp_version *)xfer->tx_message.buf;
  260. ver = &handle->version;
  261. ver->abi_major = rev_info->abi_major;
  262. ver->abi_minor = rev_info->abi_minor;
  263. ver->firmware_revision = rev_info->firmware_revision;
  264. strncpy(ver->firmware_description, rev_info->firmware_description,
  265. sizeof(ver->firmware_description));
  266. return 0;
  267. }
  268. /**
  269. * ti_sci_is_response_ack() - Generic ACK/NACK message checkup
  270. * @r: pointer to response buffer
  271. *
  272. * Return: true if the response was an ACK, else returns false.
  273. */
  274. static inline bool ti_sci_is_response_ack(void *r)
  275. {
  276. struct ti_sci_msg_hdr *hdr = r;
  277. return hdr->flags & TI_SCI_FLAG_RESP_GENERIC_ACK ? true : false;
  278. }
  279. /**
  280. * cmd_set_board_config_using_msg() - Common command to send board configuration
  281. * message
  282. * @handle: pointer to TI SCI handle
  283. * @msg_type: One of the TISCI message types to set board configuration
  284. * @addr: Address where the board config structure is located
  285. * @size: Size of the board config structure
  286. *
  287. * Return: 0 if all went well, else returns appropriate error value.
  288. */
  289. static int cmd_set_board_config_using_msg(const struct ti_sci_handle *handle,
  290. u16 msg_type, u64 addr, u32 size)
  291. {
  292. struct ti_sci_msg_board_config req;
  293. struct ti_sci_msg_hdr *resp;
  294. struct ti_sci_info *info;
  295. struct ti_sci_xfer *xfer;
  296. int ret = 0;
  297. if (IS_ERR(handle))
  298. return PTR_ERR(handle);
  299. if (!handle)
  300. return -EINVAL;
  301. info = handle_to_ti_sci_info(handle);
  302. xfer = ti_sci_setup_one_xfer(info, msg_type,
  303. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  304. (u32 *)&req, sizeof(req), sizeof(*resp));
  305. if (IS_ERR(xfer)) {
  306. ret = PTR_ERR(xfer);
  307. dev_err(info->dev, "Message alloc failed(%d)\n", ret);
  308. return ret;
  309. }
  310. req.boardcfgp_high = (addr >> 32) & 0xffffffff;
  311. req.boardcfgp_low = addr & 0xffffffff;
  312. req.boardcfg_size = size;
  313. ret = ti_sci_do_xfer(info, xfer);
  314. if (ret) {
  315. dev_err(info->dev, "Mbox send fail %d\n", ret);
  316. return ret;
  317. }
  318. resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
  319. if (!ti_sci_is_response_ack(resp))
  320. return -ENODEV;
  321. return ret;
  322. }
  323. /**
  324. * ti_sci_cmd_set_board_config() - Command to send board configuration message
  325. * @handle: pointer to TI SCI handle
  326. * @addr: Address where the board config structure is located
  327. * @size: Size of the board config structure
  328. *
  329. * Return: 0 if all went well, else returns appropriate error value.
  330. */
  331. static int ti_sci_cmd_set_board_config(const struct ti_sci_handle *handle,
  332. u64 addr, u32 size)
  333. {
  334. return cmd_set_board_config_using_msg(handle,
  335. TI_SCI_MSG_BOARD_CONFIG,
  336. addr, size);
  337. }
  338. /**
  339. * ti_sci_cmd_set_board_config_rm() - Command to send board resource
  340. * management configuration
  341. * @handle: pointer to TI SCI handle
  342. * @addr: Address where the board RM config structure is located
  343. * @size: Size of the RM config structure
  344. *
  345. * Return: 0 if all went well, else returns appropriate error value.
  346. */
  347. static
  348. int ti_sci_cmd_set_board_config_rm(const struct ti_sci_handle *handle,
  349. u64 addr, u32 size)
  350. {
  351. return cmd_set_board_config_using_msg(handle,
  352. TI_SCI_MSG_BOARD_CONFIG_RM,
  353. addr, size);
  354. }
  355. /**
  356. * ti_sci_cmd_set_board_config_security() - Command to send board security
  357. * configuration message
  358. * @handle: pointer to TI SCI handle
  359. * @addr: Address where the board security config structure is located
  360. * @size: Size of the security config structure
  361. *
  362. * Return: 0 if all went well, else returns appropriate error value.
  363. */
  364. static
  365. int ti_sci_cmd_set_board_config_security(const struct ti_sci_handle *handle,
  366. u64 addr, u32 size)
  367. {
  368. return cmd_set_board_config_using_msg(handle,
  369. TI_SCI_MSG_BOARD_CONFIG_SECURITY,
  370. addr, size);
  371. }
  372. /**
  373. * ti_sci_cmd_set_board_config_pm() - Command to send board power and clock
  374. * configuration message
  375. * @handle: pointer to TI SCI handle
  376. * @addr: Address where the board PM config structure is located
  377. * @size: Size of the PM config structure
  378. *
  379. * Return: 0 if all went well, else returns appropriate error value.
  380. */
  381. static int ti_sci_cmd_set_board_config_pm(const struct ti_sci_handle *handle,
  382. u64 addr, u32 size)
  383. {
  384. return cmd_set_board_config_using_msg(handle,
  385. TI_SCI_MSG_BOARD_CONFIG_PM,
  386. addr, size);
  387. }
  388. static struct ti_sci_exclusive_dev
  389. *ti_sci_get_exclusive_dev(struct list_head *dev_list, u32 id)
  390. {
  391. struct ti_sci_exclusive_dev *dev;
  392. list_for_each_entry(dev, dev_list, list)
  393. if (dev->id == id)
  394. return dev;
  395. return NULL;
  396. }
  397. static void ti_sci_add_exclusive_dev(struct ti_sci_info *info, u32 id)
  398. {
  399. struct ti_sci_exclusive_dev *dev;
  400. dev = ti_sci_get_exclusive_dev(&info->dev_list, id);
  401. if (dev) {
  402. dev->count++;
  403. return;
  404. }
  405. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  406. dev->id = id;
  407. dev->count = 1;
  408. INIT_LIST_HEAD(&dev->list);
  409. list_add_tail(&dev->list, &info->dev_list);
  410. }
  411. static void ti_sci_delete_exclusive_dev(struct ti_sci_info *info, u32 id)
  412. {
  413. struct ti_sci_exclusive_dev *dev;
  414. dev = ti_sci_get_exclusive_dev(&info->dev_list, id);
  415. if (!dev)
  416. return;
  417. if (dev->count > 0)
  418. dev->count--;
  419. }
  420. /**
  421. * ti_sci_set_device_state() - Set device state helper
  422. * @handle: pointer to TI SCI handle
  423. * @id: Device identifier
  424. * @flags: flags to setup for the device
  425. * @state: State to move the device to
  426. *
  427. * Return: 0 if all went well, else returns appropriate error value.
  428. */
  429. static int ti_sci_set_device_state(const struct ti_sci_handle *handle,
  430. u32 id, u32 flags, u8 state)
  431. {
  432. struct ti_sci_msg_req_set_device_state req;
  433. struct ti_sci_msg_hdr *resp;
  434. struct ti_sci_info *info;
  435. struct ti_sci_xfer *xfer;
  436. int ret = 0;
  437. if (IS_ERR(handle))
  438. return PTR_ERR(handle);
  439. if (!handle)
  440. return -EINVAL;
  441. info = handle_to_ti_sci_info(handle);
  442. xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_SET_DEVICE_STATE,
  443. flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  444. (u32 *)&req, sizeof(req), sizeof(*resp));
  445. if (IS_ERR(xfer)) {
  446. ret = PTR_ERR(xfer);
  447. dev_err(info->dev, "Message alloc failed(%d)\n", ret);
  448. return ret;
  449. }
  450. req.id = id;
  451. req.state = state;
  452. ret = ti_sci_do_xfer(info, xfer);
  453. if (ret) {
  454. dev_err(info->dev, "Mbox send fail %d\n", ret);
  455. return ret;
  456. }
  457. resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
  458. if (!ti_sci_is_response_ack(resp))
  459. return -ENODEV;
  460. if (state == MSG_DEVICE_SW_STATE_AUTO_OFF)
  461. ti_sci_delete_exclusive_dev(info, id);
  462. else if (flags & MSG_FLAG_DEVICE_EXCLUSIVE)
  463. ti_sci_add_exclusive_dev(info, id);
  464. return ret;
  465. }
  466. /**
  467. * ti_sci_set_device_state_no_wait() - Set device state helper without
  468. * requesting or waiting for a response.
  469. * @handle: pointer to TI SCI handle
  470. * @id: Device identifier
  471. * @flags: flags to setup for the device
  472. * @state: State to move the device to
  473. *
  474. * Return: 0 if all went well, else returns appropriate error value.
  475. */
  476. static int ti_sci_set_device_state_no_wait(const struct ti_sci_handle *handle,
  477. u32 id, u32 flags, u8 state)
  478. {
  479. struct ti_sci_msg_req_set_device_state req;
  480. struct ti_sci_info *info;
  481. struct ti_sci_xfer *xfer;
  482. int ret = 0;
  483. if (IS_ERR(handle))
  484. return PTR_ERR(handle);
  485. if (!handle)
  486. return -EINVAL;
  487. info = handle_to_ti_sci_info(handle);
  488. xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_SET_DEVICE_STATE,
  489. flags | TI_SCI_FLAG_REQ_GENERIC_NORESPONSE,
  490. (u32 *)&req, sizeof(req), 0);
  491. if (IS_ERR(xfer)) {
  492. ret = PTR_ERR(xfer);
  493. dev_err(info->dev, "Message alloc failed(%d)\n", ret);
  494. return ret;
  495. }
  496. req.id = id;
  497. req.state = state;
  498. ret = ti_sci_do_xfer(info, xfer);
  499. if (ret)
  500. dev_err(info->dev, "Mbox send fail %d\n", ret);
  501. return ret;
  502. }
  503. /**
  504. * ti_sci_get_device_state() - Get device state helper
  505. * @handle: Handle to the device
  506. * @id: Device Identifier
  507. * @clcnt: Pointer to Context Loss Count
  508. * @resets: pointer to resets
  509. * @p_state: pointer to p_state
  510. * @c_state: pointer to c_state
  511. *
  512. * Return: 0 if all went fine, else return appropriate error.
  513. */
  514. static int ti_sci_get_device_state(const struct ti_sci_handle *handle,
  515. u32 id, u32 *clcnt, u32 *resets,
  516. u8 *p_state, u8 *c_state)
  517. {
  518. struct ti_sci_msg_resp_get_device_state *resp;
  519. struct ti_sci_msg_req_get_device_state req;
  520. struct ti_sci_info *info;
  521. struct ti_sci_xfer *xfer;
  522. int ret = 0;
  523. if (IS_ERR(handle))
  524. return PTR_ERR(handle);
  525. if (!handle)
  526. return -EINVAL;
  527. if (!clcnt && !resets && !p_state && !c_state)
  528. return -EINVAL;
  529. info = handle_to_ti_sci_info(handle);
  530. xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_GET_DEVICE_STATE,
  531. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  532. (u32 *)&req, sizeof(req), sizeof(*resp));
  533. if (IS_ERR(xfer)) {
  534. ret = PTR_ERR(xfer);
  535. dev_err(info->dev, "Message alloc failed(%d)\n", ret);
  536. return ret;
  537. }
  538. req.id = id;
  539. ret = ti_sci_do_xfer(info, xfer);
  540. if (ret) {
  541. dev_err(dev, "Mbox send fail %d\n", ret);
  542. return ret;
  543. }
  544. resp = (struct ti_sci_msg_resp_get_device_state *)xfer->tx_message.buf;
  545. if (!ti_sci_is_response_ack(resp))
  546. return -ENODEV;
  547. if (clcnt)
  548. *clcnt = resp->context_loss_count;
  549. if (resets)
  550. *resets = resp->resets;
  551. if (p_state)
  552. *p_state = resp->programmed_state;
  553. if (c_state)
  554. *c_state = resp->current_state;
  555. return ret;
  556. }
  557. /**
  558. * ti_sci_cmd_get_device() - command to request for device managed by TISCI
  559. * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
  560. * @id: Device Identifier
  561. *
  562. * Request for the device - NOTE: the client MUST maintain integrity of
  563. * usage count by balancing get_device with put_device. No refcounting is
  564. * managed by driver for that purpose.
  565. *
  566. * NOTE: The request is for exclusive access for the processor.
  567. *
  568. * Return: 0 if all went fine, else return appropriate error.
  569. */
  570. static int ti_sci_cmd_get_device(const struct ti_sci_handle *handle, u32 id)
  571. {
  572. return ti_sci_set_device_state(handle, id, 0,
  573. MSG_DEVICE_SW_STATE_ON);
  574. }
  575. static int ti_sci_cmd_get_device_exclusive(const struct ti_sci_handle *handle,
  576. u32 id)
  577. {
  578. return ti_sci_set_device_state(handle, id, MSG_FLAG_DEVICE_EXCLUSIVE,
  579. MSG_DEVICE_SW_STATE_ON);
  580. }
  581. /**
  582. * ti_sci_cmd_idle_device() - Command to idle a device managed by TISCI
  583. * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
  584. * @id: Device Identifier
  585. *
  586. * Request for the device - NOTE: the client MUST maintain integrity of
  587. * usage count by balancing get_device with put_device. No refcounting is
  588. * managed by driver for that purpose.
  589. *
  590. * Return: 0 if all went fine, else return appropriate error.
  591. */
  592. static int ti_sci_cmd_idle_device(const struct ti_sci_handle *handle, u32 id)
  593. {
  594. return ti_sci_set_device_state(handle, id,
  595. 0,
  596. MSG_DEVICE_SW_STATE_RETENTION);
  597. }
  598. static int ti_sci_cmd_idle_device_exclusive(const struct ti_sci_handle *handle,
  599. u32 id)
  600. {
  601. return ti_sci_set_device_state(handle, id, MSG_FLAG_DEVICE_EXCLUSIVE,
  602. MSG_DEVICE_SW_STATE_RETENTION);
  603. }
  604. /**
  605. * ti_sci_cmd_put_device() - command to release a device managed by TISCI
  606. * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
  607. * @id: Device Identifier
  608. *
  609. * Request for the device - NOTE: the client MUST maintain integrity of
  610. * usage count by balancing get_device with put_device. No refcounting is
  611. * managed by driver for that purpose.
  612. *
  613. * Return: 0 if all went fine, else return appropriate error.
  614. */
  615. static int ti_sci_cmd_put_device(const struct ti_sci_handle *handle, u32 id)
  616. {
  617. return ti_sci_set_device_state(handle, id, 0,
  618. MSG_DEVICE_SW_STATE_AUTO_OFF);
  619. }
  620. static
  621. int ti_sci_cmd_release_exclusive_devices(const struct ti_sci_handle *handle)
  622. {
  623. struct ti_sci_exclusive_dev *dev, *tmp;
  624. struct ti_sci_info *info;
  625. int i, cnt;
  626. info = handle_to_ti_sci_info(handle);
  627. list_for_each_entry_safe(dev, tmp, &info->dev_list, list) {
  628. cnt = dev->count;
  629. debug("%s: id = %d, cnt = %d\n", __func__, dev->id, cnt);
  630. for (i = 0; i < cnt; i++)
  631. ti_sci_cmd_put_device(handle, dev->id);
  632. }
  633. return 0;
  634. }
  635. /**
  636. * ti_sci_cmd_dev_is_valid() - Is the device valid
  637. * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
  638. * @id: Device Identifier
  639. *
  640. * Return: 0 if all went fine and the device ID is valid, else return
  641. * appropriate error.
  642. */
  643. static int ti_sci_cmd_dev_is_valid(const struct ti_sci_handle *handle, u32 id)
  644. {
  645. u8 unused;
  646. /* check the device state which will also tell us if the ID is valid */
  647. return ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &unused);
  648. }
  649. /**
  650. * ti_sci_cmd_dev_get_clcnt() - Get context loss counter
  651. * @handle: Pointer to TISCI handle
  652. * @id: Device Identifier
  653. * @count: Pointer to Context Loss counter to populate
  654. *
  655. * Return: 0 if all went fine, else return appropriate error.
  656. */
  657. static int ti_sci_cmd_dev_get_clcnt(const struct ti_sci_handle *handle, u32 id,
  658. u32 *count)
  659. {
  660. return ti_sci_get_device_state(handle, id, count, NULL, NULL, NULL);
  661. }
  662. /**
  663. * ti_sci_cmd_dev_is_idle() - Check if the device is requested to be idle
  664. * @handle: Pointer to TISCI handle
  665. * @id: Device Identifier
  666. * @r_state: true if requested to be idle
  667. *
  668. * Return: 0 if all went fine, else return appropriate error.
  669. */
  670. static int ti_sci_cmd_dev_is_idle(const struct ti_sci_handle *handle, u32 id,
  671. bool *r_state)
  672. {
  673. int ret;
  674. u8 state;
  675. if (!r_state)
  676. return -EINVAL;
  677. ret = ti_sci_get_device_state(handle, id, NULL, NULL, &state, NULL);
  678. if (ret)
  679. return ret;
  680. *r_state = (state == MSG_DEVICE_SW_STATE_RETENTION);
  681. return 0;
  682. }
  683. /**
  684. * ti_sci_cmd_dev_is_stop() - Check if the device is requested to be stopped
  685. * @handle: Pointer to TISCI handle
  686. * @id: Device Identifier
  687. * @r_state: true if requested to be stopped
  688. * @curr_state: true if currently stopped.
  689. *
  690. * Return: 0 if all went fine, else return appropriate error.
  691. */
  692. static int ti_sci_cmd_dev_is_stop(const struct ti_sci_handle *handle, u32 id,
  693. bool *r_state, bool *curr_state)
  694. {
  695. int ret;
  696. u8 p_state, c_state;
  697. if (!r_state && !curr_state)
  698. return -EINVAL;
  699. ret =
  700. ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
  701. if (ret)
  702. return ret;
  703. if (r_state)
  704. *r_state = (p_state == MSG_DEVICE_SW_STATE_AUTO_OFF);
  705. if (curr_state)
  706. *curr_state = (c_state == MSG_DEVICE_HW_STATE_OFF);
  707. return 0;
  708. }
  709. /**
  710. * ti_sci_cmd_dev_is_on() - Check if the device is requested to be ON
  711. * @handle: Pointer to TISCI handle
  712. * @id: Device Identifier
  713. * @r_state: true if requested to be ON
  714. * @curr_state: true if currently ON and active
  715. *
  716. * Return: 0 if all went fine, else return appropriate error.
  717. */
  718. static int ti_sci_cmd_dev_is_on(const struct ti_sci_handle *handle, u32 id,
  719. bool *r_state, bool *curr_state)
  720. {
  721. int ret;
  722. u8 p_state, c_state;
  723. if (!r_state && !curr_state)
  724. return -EINVAL;
  725. ret =
  726. ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
  727. if (ret)
  728. return ret;
  729. if (r_state)
  730. *r_state = (p_state == MSG_DEVICE_SW_STATE_ON);
  731. if (curr_state)
  732. *curr_state = (c_state == MSG_DEVICE_HW_STATE_ON);
  733. return 0;
  734. }
  735. /**
  736. * ti_sci_cmd_dev_is_trans() - Check if the device is currently transitioning
  737. * @handle: Pointer to TISCI handle
  738. * @id: Device Identifier
  739. * @curr_state: true if currently transitioning.
  740. *
  741. * Return: 0 if all went fine, else return appropriate error.
  742. */
  743. static int ti_sci_cmd_dev_is_trans(const struct ti_sci_handle *handle, u32 id,
  744. bool *curr_state)
  745. {
  746. int ret;
  747. u8 state;
  748. if (!curr_state)
  749. return -EINVAL;
  750. ret = ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &state);
  751. if (ret)
  752. return ret;
  753. *curr_state = (state == MSG_DEVICE_HW_STATE_TRANS);
  754. return 0;
  755. }
  756. /**
  757. * ti_sci_cmd_set_device_resets() - command to set resets for device managed
  758. * by TISCI
  759. * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
  760. * @id: Device Identifier
  761. * @reset_state: Device specific reset bit field
  762. *
  763. * Return: 0 if all went fine, else return appropriate error.
  764. */
  765. static int ti_sci_cmd_set_device_resets(const struct ti_sci_handle *handle,
  766. u32 id, u32 reset_state)
  767. {
  768. struct ti_sci_msg_req_set_device_resets req;
  769. struct ti_sci_msg_hdr *resp;
  770. struct ti_sci_info *info;
  771. struct ti_sci_xfer *xfer;
  772. int ret = 0;
  773. if (IS_ERR(handle))
  774. return PTR_ERR(handle);
  775. if (!handle)
  776. return -EINVAL;
  777. info = handle_to_ti_sci_info(handle);
  778. xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_SET_DEVICE_RESETS,
  779. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  780. (u32 *)&req, sizeof(req), sizeof(*resp));
  781. if (IS_ERR(xfer)) {
  782. ret = PTR_ERR(xfer);
  783. dev_err(info->dev, "Message alloc failed(%d)\n", ret);
  784. return ret;
  785. }
  786. req.id = id;
  787. req.resets = reset_state;
  788. ret = ti_sci_do_xfer(info, xfer);
  789. if (ret) {
  790. dev_err(info->dev, "Mbox send fail %d\n", ret);
  791. return ret;
  792. }
  793. resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
  794. if (!ti_sci_is_response_ack(resp))
  795. return -ENODEV;
  796. return ret;
  797. }
  798. /**
  799. * ti_sci_cmd_get_device_resets() - Get reset state for device managed
  800. * by TISCI
  801. * @handle: Pointer to TISCI handle
  802. * @id: Device Identifier
  803. * @reset_state: Pointer to reset state to populate
  804. *
  805. * Return: 0 if all went fine, else return appropriate error.
  806. */
  807. static int ti_sci_cmd_get_device_resets(const struct ti_sci_handle *handle,
  808. u32 id, u32 *reset_state)
  809. {
  810. return ti_sci_get_device_state(handle, id, NULL, reset_state, NULL,
  811. NULL);
  812. }
  813. /**
  814. * ti_sci_set_clock_state() - Set clock state helper
  815. * @handle: pointer to TI SCI handle
  816. * @dev_id: Device identifier this request is for
  817. * @clk_id: Clock identifier for the device for this request.
  818. * Each device has it's own set of clock inputs. This indexes
  819. * which clock input to modify.
  820. * @flags: Header flags as needed
  821. * @state: State to request for the clock.
  822. *
  823. * Return: 0 if all went well, else returns appropriate error value.
  824. */
  825. static int ti_sci_set_clock_state(const struct ti_sci_handle *handle,
  826. u32 dev_id, u8 clk_id,
  827. u32 flags, u8 state)
  828. {
  829. struct ti_sci_msg_req_set_clock_state req;
  830. struct ti_sci_msg_hdr *resp;
  831. struct ti_sci_info *info;
  832. struct ti_sci_xfer *xfer;
  833. int ret = 0;
  834. if (IS_ERR(handle))
  835. return PTR_ERR(handle);
  836. if (!handle)
  837. return -EINVAL;
  838. info = handle_to_ti_sci_info(handle);
  839. xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_SET_CLOCK_STATE,
  840. flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  841. (u32 *)&req, sizeof(req), sizeof(*resp));
  842. if (IS_ERR(xfer)) {
  843. ret = PTR_ERR(xfer);
  844. dev_err(info->dev, "Message alloc failed(%d)\n", ret);
  845. return ret;
  846. }
  847. req.dev_id = dev_id;
  848. req.clk_id = clk_id;
  849. req.request_state = state;
  850. ret = ti_sci_do_xfer(info, xfer);
  851. if (ret) {
  852. dev_err(info->dev, "Mbox send fail %d\n", ret);
  853. return ret;
  854. }
  855. resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
  856. if (!ti_sci_is_response_ack(resp))
  857. return -ENODEV;
  858. return ret;
  859. }
  860. /**
  861. * ti_sci_cmd_get_clock_state() - Get clock state helper
  862. * @handle: pointer to TI SCI handle
  863. * @dev_id: Device identifier this request is for
  864. * @clk_id: Clock identifier for the device for this request.
  865. * Each device has it's own set of clock inputs. This indexes
  866. * which clock input to modify.
  867. * @programmed_state: State requested for clock to move to
  868. * @current_state: State that the clock is currently in
  869. *
  870. * Return: 0 if all went well, else returns appropriate error value.
  871. */
  872. static int ti_sci_cmd_get_clock_state(const struct ti_sci_handle *handle,
  873. u32 dev_id, u8 clk_id,
  874. u8 *programmed_state, u8 *current_state)
  875. {
  876. struct ti_sci_msg_resp_get_clock_state *resp;
  877. struct ti_sci_msg_req_get_clock_state req;
  878. struct ti_sci_info *info;
  879. struct ti_sci_xfer *xfer;
  880. int ret = 0;
  881. if (IS_ERR(handle))
  882. return PTR_ERR(handle);
  883. if (!handle)
  884. return -EINVAL;
  885. if (!programmed_state && !current_state)
  886. return -EINVAL;
  887. info = handle_to_ti_sci_info(handle);
  888. xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_GET_CLOCK_STATE,
  889. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  890. (u32 *)&req, sizeof(req), sizeof(*resp));
  891. if (IS_ERR(xfer)) {
  892. ret = PTR_ERR(xfer);
  893. dev_err(info->dev, "Message alloc failed(%d)\n", ret);
  894. return ret;
  895. }
  896. req.dev_id = dev_id;
  897. req.clk_id = clk_id;
  898. ret = ti_sci_do_xfer(info, xfer);
  899. if (ret) {
  900. dev_err(info->dev, "Mbox send fail %d\n", ret);
  901. return ret;
  902. }
  903. resp = (struct ti_sci_msg_resp_get_clock_state *)xfer->tx_message.buf;
  904. if (!ti_sci_is_response_ack(resp))
  905. return -ENODEV;
  906. if (programmed_state)
  907. *programmed_state = resp->programmed_state;
  908. if (current_state)
  909. *current_state = resp->current_state;
  910. return ret;
  911. }
  912. /**
  913. * ti_sci_cmd_get_clock() - Get control of a clock from TI SCI
  914. * @handle: pointer to TI SCI handle
  915. * @dev_id: Device identifier this request is for
  916. * @clk_id: Clock identifier for the device for this request.
  917. * Each device has it's own set of clock inputs. This indexes
  918. * which clock input to modify.
  919. * @needs_ssc: 'true' if Spread Spectrum clock is desired, else 'false'
  920. * @can_change_freq: 'true' if frequency change is desired, else 'false'
  921. * @enable_input_term: 'true' if input termination is desired, else 'false'
  922. *
  923. * Return: 0 if all went well, else returns appropriate error value.
  924. */
  925. static int ti_sci_cmd_get_clock(const struct ti_sci_handle *handle, u32 dev_id,
  926. u8 clk_id, bool needs_ssc, bool can_change_freq,
  927. bool enable_input_term)
  928. {
  929. u32 flags = 0;
  930. flags |= needs_ssc ? MSG_FLAG_CLOCK_ALLOW_SSC : 0;
  931. flags |= can_change_freq ? MSG_FLAG_CLOCK_ALLOW_FREQ_CHANGE : 0;
  932. flags |= enable_input_term ? MSG_FLAG_CLOCK_INPUT_TERM : 0;
  933. return ti_sci_set_clock_state(handle, dev_id, clk_id, flags,
  934. MSG_CLOCK_SW_STATE_REQ);
  935. }
  936. /**
  937. * ti_sci_cmd_idle_clock() - Idle a clock which is in our control
  938. * @handle: pointer to TI SCI handle
  939. * @dev_id: Device identifier this request is for
  940. * @clk_id: Clock identifier for the device for this request.
  941. * Each device has it's own set of clock inputs. This indexes
  942. * which clock input to modify.
  943. *
  944. * NOTE: This clock must have been requested by get_clock previously.
  945. *
  946. * Return: 0 if all went well, else returns appropriate error value.
  947. */
  948. static int ti_sci_cmd_idle_clock(const struct ti_sci_handle *handle,
  949. u32 dev_id, u8 clk_id)
  950. {
  951. return ti_sci_set_clock_state(handle, dev_id, clk_id, 0,
  952. MSG_CLOCK_SW_STATE_UNREQ);
  953. }
  954. /**
  955. * ti_sci_cmd_put_clock() - Release a clock from our control back to TISCI
  956. * @handle: pointer to TI SCI handle
  957. * @dev_id: Device identifier this request is for
  958. * @clk_id: Clock identifier for the device for this request.
  959. * Each device has it's own set of clock inputs. This indexes
  960. * which clock input to modify.
  961. *
  962. * NOTE: This clock must have been requested by get_clock previously.
  963. *
  964. * Return: 0 if all went well, else returns appropriate error value.
  965. */
  966. static int ti_sci_cmd_put_clock(const struct ti_sci_handle *handle,
  967. u32 dev_id, u8 clk_id)
  968. {
  969. return ti_sci_set_clock_state(handle, dev_id, clk_id, 0,
  970. MSG_CLOCK_SW_STATE_AUTO);
  971. }
  972. /**
  973. * ti_sci_cmd_clk_is_auto() - Is the clock being auto managed
  974. * @handle: pointer to TI SCI handle
  975. * @dev_id: Device identifier this request is for
  976. * @clk_id: Clock identifier for the device for this request.
  977. * Each device has it's own set of clock inputs. This indexes
  978. * which clock input to modify.
  979. * @req_state: state indicating if the clock is auto managed
  980. *
  981. * Return: 0 if all went well, else returns appropriate error value.
  982. */
  983. static int ti_sci_cmd_clk_is_auto(const struct ti_sci_handle *handle,
  984. u32 dev_id, u8 clk_id, bool *req_state)
  985. {
  986. u8 state = 0;
  987. int ret;
  988. if (!req_state)
  989. return -EINVAL;
  990. ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id, &state, NULL);
  991. if (ret)
  992. return ret;
  993. *req_state = (state == MSG_CLOCK_SW_STATE_AUTO);
  994. return 0;
  995. }
  996. /**
  997. * ti_sci_cmd_clk_is_on() - Is the clock ON
  998. * @handle: pointer to TI SCI handle
  999. * @dev_id: Device identifier this request is for
  1000. * @clk_id: Clock identifier for the device for this request.
  1001. * Each device has it's own set of clock inputs. This indexes
  1002. * which clock input to modify.
  1003. * @req_state: state indicating if the clock is managed by us and enabled
  1004. * @curr_state: state indicating if the clock is ready for operation
  1005. *
  1006. * Return: 0 if all went well, else returns appropriate error value.
  1007. */
  1008. static int ti_sci_cmd_clk_is_on(const struct ti_sci_handle *handle, u32 dev_id,
  1009. u8 clk_id, bool *req_state, bool *curr_state)
  1010. {
  1011. u8 c_state = 0, r_state = 0;
  1012. int ret;
  1013. if (!req_state && !curr_state)
  1014. return -EINVAL;
  1015. ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
  1016. &r_state, &c_state);
  1017. if (ret)
  1018. return ret;
  1019. if (req_state)
  1020. *req_state = (r_state == MSG_CLOCK_SW_STATE_REQ);
  1021. if (curr_state)
  1022. *curr_state = (c_state == MSG_CLOCK_HW_STATE_READY);
  1023. return 0;
  1024. }
  1025. /**
  1026. * ti_sci_cmd_clk_is_off() - Is the clock OFF
  1027. * @handle: pointer to TI SCI handle
  1028. * @dev_id: Device identifier this request is for
  1029. * @clk_id: Clock identifier for the device for this request.
  1030. * Each device has it's own set of clock inputs. This indexes
  1031. * which clock input to modify.
  1032. * @req_state: state indicating if the clock is managed by us and disabled
  1033. * @curr_state: state indicating if the clock is NOT ready for operation
  1034. *
  1035. * Return: 0 if all went well, else returns appropriate error value.
  1036. */
  1037. static int ti_sci_cmd_clk_is_off(const struct ti_sci_handle *handle, u32 dev_id,
  1038. u8 clk_id, bool *req_state, bool *curr_state)
  1039. {
  1040. u8 c_state = 0, r_state = 0;
  1041. int ret;
  1042. if (!req_state && !curr_state)
  1043. return -EINVAL;
  1044. ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
  1045. &r_state, &c_state);
  1046. if (ret)
  1047. return ret;
  1048. if (req_state)
  1049. *req_state = (r_state == MSG_CLOCK_SW_STATE_UNREQ);
  1050. if (curr_state)
  1051. *curr_state = (c_state == MSG_CLOCK_HW_STATE_NOT_READY);
  1052. return 0;
  1053. }
  1054. /**
  1055. * ti_sci_cmd_clk_set_parent() - Set the clock source of a specific device clock
  1056. * @handle: pointer to TI SCI handle
  1057. * @dev_id: Device identifier this request is for
  1058. * @clk_id: Clock identifier for the device for this request.
  1059. * Each device has it's own set of clock inputs. This indexes
  1060. * which clock input to modify.
  1061. * @parent_id: Parent clock identifier to set
  1062. *
  1063. * Return: 0 if all went well, else returns appropriate error value.
  1064. */
  1065. static int ti_sci_cmd_clk_set_parent(const struct ti_sci_handle *handle,
  1066. u32 dev_id, u8 clk_id, u8 parent_id)
  1067. {
  1068. struct ti_sci_msg_req_set_clock_parent req;
  1069. struct ti_sci_msg_hdr *resp;
  1070. struct ti_sci_info *info;
  1071. struct ti_sci_xfer *xfer;
  1072. int ret = 0;
  1073. if (IS_ERR(handle))
  1074. return PTR_ERR(handle);
  1075. if (!handle)
  1076. return -EINVAL;
  1077. info = handle_to_ti_sci_info(handle);
  1078. xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_SET_CLOCK_PARENT,
  1079. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  1080. (u32 *)&req, sizeof(req), sizeof(*resp));
  1081. if (IS_ERR(xfer)) {
  1082. ret = PTR_ERR(xfer);
  1083. dev_err(info->dev, "Message alloc failed(%d)\n", ret);
  1084. return ret;
  1085. }
  1086. req.dev_id = dev_id;
  1087. req.clk_id = clk_id;
  1088. req.parent_id = parent_id;
  1089. ret = ti_sci_do_xfer(info, xfer);
  1090. if (ret) {
  1091. dev_err(info->dev, "Mbox send fail %d\n", ret);
  1092. return ret;
  1093. }
  1094. resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
  1095. if (!ti_sci_is_response_ack(resp))
  1096. return -ENODEV;
  1097. return ret;
  1098. }
  1099. /**
  1100. * ti_sci_cmd_clk_get_parent() - Get current parent clock source
  1101. * @handle: pointer to TI SCI handle
  1102. * @dev_id: Device identifier this request is for
  1103. * @clk_id: Clock identifier for the device for this request.
  1104. * Each device has it's own set of clock inputs. This indexes
  1105. * which clock input to modify.
  1106. * @parent_id: Current clock parent
  1107. *
  1108. * Return: 0 if all went well, else returns appropriate error value.
  1109. */
  1110. static int ti_sci_cmd_clk_get_parent(const struct ti_sci_handle *handle,
  1111. u32 dev_id, u8 clk_id, u8 *parent_id)
  1112. {
  1113. struct ti_sci_msg_resp_get_clock_parent *resp;
  1114. struct ti_sci_msg_req_get_clock_parent req;
  1115. struct ti_sci_info *info;
  1116. struct ti_sci_xfer *xfer;
  1117. int ret = 0;
  1118. if (IS_ERR(handle))
  1119. return PTR_ERR(handle);
  1120. if (!handle || !parent_id)
  1121. return -EINVAL;
  1122. info = handle_to_ti_sci_info(handle);
  1123. xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_GET_CLOCK_PARENT,
  1124. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  1125. (u32 *)&req, sizeof(req), sizeof(*resp));
  1126. if (IS_ERR(xfer)) {
  1127. ret = PTR_ERR(xfer);
  1128. dev_err(info->dev, "Message alloc failed(%d)\n", ret);
  1129. return ret;
  1130. }
  1131. req.dev_id = dev_id;
  1132. req.clk_id = clk_id;
  1133. ret = ti_sci_do_xfer(info, xfer);
  1134. if (ret) {
  1135. dev_err(info->dev, "Mbox send fail %d\n", ret);
  1136. return ret;
  1137. }
  1138. resp = (struct ti_sci_msg_resp_get_clock_parent *)xfer->tx_message.buf;
  1139. if (!ti_sci_is_response_ack(resp))
  1140. ret = -ENODEV;
  1141. else
  1142. *parent_id = resp->parent_id;
  1143. return ret;
  1144. }
  1145. /**
  1146. * ti_sci_cmd_clk_get_num_parents() - Get num parents of the current clk source
  1147. * @handle: pointer to TI SCI handle
  1148. * @dev_id: Device identifier this request is for
  1149. * @clk_id: Clock identifier for the device for this request.
  1150. * Each device has it's own set of clock inputs. This indexes
  1151. * which clock input to modify.
  1152. * @num_parents: Returns he number of parents to the current clock.
  1153. *
  1154. * Return: 0 if all went well, else returns appropriate error value.
  1155. */
  1156. static int ti_sci_cmd_clk_get_num_parents(const struct ti_sci_handle *handle,
  1157. u32 dev_id, u8 clk_id,
  1158. u8 *num_parents)
  1159. {
  1160. struct ti_sci_msg_resp_get_clock_num_parents *resp;
  1161. struct ti_sci_msg_req_get_clock_num_parents req;
  1162. struct ti_sci_info *info;
  1163. struct ti_sci_xfer *xfer;
  1164. int ret = 0;
  1165. if (IS_ERR(handle))
  1166. return PTR_ERR(handle);
  1167. if (!handle || !num_parents)
  1168. return -EINVAL;
  1169. info = handle_to_ti_sci_info(handle);
  1170. xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_GET_NUM_CLOCK_PARENTS,
  1171. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  1172. (u32 *)&req, sizeof(req), sizeof(*resp));
  1173. if (IS_ERR(xfer)) {
  1174. ret = PTR_ERR(xfer);
  1175. dev_err(info->dev, "Message alloc failed(%d)\n", ret);
  1176. return ret;
  1177. }
  1178. req.dev_id = dev_id;
  1179. req.clk_id = clk_id;
  1180. ret = ti_sci_do_xfer(info, xfer);
  1181. if (ret) {
  1182. dev_err(info->dev, "Mbox send fail %d\n", ret);
  1183. return ret;
  1184. }
  1185. resp = (struct ti_sci_msg_resp_get_clock_num_parents *)
  1186. xfer->tx_message.buf;
  1187. if (!ti_sci_is_response_ack(resp))
  1188. ret = -ENODEV;
  1189. else
  1190. *num_parents = resp->num_parents;
  1191. return ret;
  1192. }
  1193. /**
  1194. * ti_sci_cmd_clk_get_match_freq() - Find a good match for frequency
  1195. * @handle: pointer to TI SCI handle
  1196. * @dev_id: Device identifier this request is for
  1197. * @clk_id: Clock identifier for the device for this request.
  1198. * Each device has it's own set of clock inputs. This indexes
  1199. * which clock input to modify.
  1200. * @min_freq: The minimum allowable frequency in Hz. This is the minimum
  1201. * allowable programmed frequency and does not account for clock
  1202. * tolerances and jitter.
  1203. * @target_freq: The target clock frequency in Hz. A frequency will be
  1204. * processed as close to this target frequency as possible.
  1205. * @max_freq: The maximum allowable frequency in Hz. This is the maximum
  1206. * allowable programmed frequency and does not account for clock
  1207. * tolerances and jitter.
  1208. * @match_freq: Frequency match in Hz response.
  1209. *
  1210. * Return: 0 if all went well, else returns appropriate error value.
  1211. */
  1212. static int ti_sci_cmd_clk_get_match_freq(const struct ti_sci_handle *handle,
  1213. u32 dev_id, u8 clk_id, u64 min_freq,
  1214. u64 target_freq, u64 max_freq,
  1215. u64 *match_freq)
  1216. {
  1217. struct ti_sci_msg_resp_query_clock_freq *resp;
  1218. struct ti_sci_msg_req_query_clock_freq req;
  1219. struct ti_sci_info *info;
  1220. struct ti_sci_xfer *xfer;
  1221. int ret = 0;
  1222. if (IS_ERR(handle))
  1223. return PTR_ERR(handle);
  1224. if (!handle || !match_freq)
  1225. return -EINVAL;
  1226. info = handle_to_ti_sci_info(handle);
  1227. xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_QUERY_CLOCK_FREQ,
  1228. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  1229. (u32 *)&req, sizeof(req), sizeof(*resp));
  1230. if (IS_ERR(xfer)) {
  1231. ret = PTR_ERR(xfer);
  1232. dev_err(info->dev, "Message alloc failed(%d)\n", ret);
  1233. return ret;
  1234. }
  1235. req.dev_id = dev_id;
  1236. req.clk_id = clk_id;
  1237. req.min_freq_hz = min_freq;
  1238. req.target_freq_hz = target_freq;
  1239. req.max_freq_hz = max_freq;
  1240. ret = ti_sci_do_xfer(info, xfer);
  1241. if (ret) {
  1242. dev_err(info->dev, "Mbox send fail %d\n", ret);
  1243. return ret;
  1244. }
  1245. resp = (struct ti_sci_msg_resp_query_clock_freq *)xfer->tx_message.buf;
  1246. if (!ti_sci_is_response_ack(resp))
  1247. ret = -ENODEV;
  1248. else
  1249. *match_freq = resp->freq_hz;
  1250. return ret;
  1251. }
  1252. /**
  1253. * ti_sci_cmd_clk_set_freq() - Set a frequency for clock
  1254. * @handle: pointer to TI SCI handle
  1255. * @dev_id: Device identifier this request is for
  1256. * @clk_id: Clock identifier for the device for this request.
  1257. * Each device has it's own set of clock inputs. This indexes
  1258. * which clock input to modify.
  1259. * @min_freq: The minimum allowable frequency in Hz. This is the minimum
  1260. * allowable programmed frequency and does not account for clock
  1261. * tolerances and jitter.
  1262. * @target_freq: The target clock frequency in Hz. A frequency will be
  1263. * processed as close to this target frequency as possible.
  1264. * @max_freq: The maximum allowable frequency in Hz. This is the maximum
  1265. * allowable programmed frequency and does not account for clock
  1266. * tolerances and jitter.
  1267. *
  1268. * Return: 0 if all went well, else returns appropriate error value.
  1269. */
  1270. static int ti_sci_cmd_clk_set_freq(const struct ti_sci_handle *handle,
  1271. u32 dev_id, u8 clk_id, u64 min_freq,
  1272. u64 target_freq, u64 max_freq)
  1273. {
  1274. struct ti_sci_msg_req_set_clock_freq req;
  1275. struct ti_sci_msg_hdr *resp;
  1276. struct ti_sci_info *info;
  1277. struct ti_sci_xfer *xfer;
  1278. int ret = 0;
  1279. if (IS_ERR(handle))
  1280. return PTR_ERR(handle);
  1281. if (!handle)
  1282. return -EINVAL;
  1283. info = handle_to_ti_sci_info(handle);
  1284. xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_SET_CLOCK_FREQ,
  1285. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  1286. (u32 *)&req, sizeof(req), sizeof(*resp));
  1287. if (IS_ERR(xfer)) {
  1288. ret = PTR_ERR(xfer);
  1289. dev_err(info->dev, "Message alloc failed(%d)\n", ret);
  1290. return ret;
  1291. }
  1292. req.dev_id = dev_id;
  1293. req.clk_id = clk_id;
  1294. req.min_freq_hz = min_freq;
  1295. req.target_freq_hz = target_freq;
  1296. req.max_freq_hz = max_freq;
  1297. ret = ti_sci_do_xfer(info, xfer);
  1298. if (ret) {
  1299. dev_err(info->dev, "Mbox send fail %d\n", ret);
  1300. return ret;
  1301. }
  1302. resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
  1303. if (!ti_sci_is_response_ack(resp))
  1304. return -ENODEV;
  1305. return ret;
  1306. }
  1307. /**
  1308. * ti_sci_cmd_clk_get_freq() - Get current frequency
  1309. * @handle: pointer to TI SCI handle
  1310. * @dev_id: Device identifier this request is for
  1311. * @clk_id: Clock identifier for the device for this request.
  1312. * Each device has it's own set of clock inputs. This indexes
  1313. * which clock input to modify.
  1314. * @freq: Currently frequency in Hz
  1315. *
  1316. * Return: 0 if all went well, else returns appropriate error value.
  1317. */
  1318. static int ti_sci_cmd_clk_get_freq(const struct ti_sci_handle *handle,
  1319. u32 dev_id, u8 clk_id, u64 *freq)
  1320. {
  1321. struct ti_sci_msg_resp_get_clock_freq *resp;
  1322. struct ti_sci_msg_req_get_clock_freq req;
  1323. struct ti_sci_info *info;
  1324. struct ti_sci_xfer *xfer;
  1325. int ret = 0;
  1326. if (IS_ERR(handle))
  1327. return PTR_ERR(handle);
  1328. if (!handle || !freq)
  1329. return -EINVAL;
  1330. info = handle_to_ti_sci_info(handle);
  1331. xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_GET_CLOCK_FREQ,
  1332. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  1333. (u32 *)&req, sizeof(req), sizeof(*resp));
  1334. if (IS_ERR(xfer)) {
  1335. ret = PTR_ERR(xfer);
  1336. dev_err(info->dev, "Message alloc failed(%d)\n", ret);
  1337. return ret;
  1338. }
  1339. req.dev_id = dev_id;
  1340. req.clk_id = clk_id;
  1341. ret = ti_sci_do_xfer(info, xfer);
  1342. if (ret) {
  1343. dev_err(info->dev, "Mbox send fail %d\n", ret);
  1344. return ret;
  1345. }
  1346. resp = (struct ti_sci_msg_resp_get_clock_freq *)xfer->tx_message.buf;
  1347. if (!ti_sci_is_response_ack(resp))
  1348. ret = -ENODEV;
  1349. else
  1350. *freq = resp->freq_hz;
  1351. return ret;
  1352. }
  1353. /**
  1354. * ti_sci_cmd_core_reboot() - Command to request system reset
  1355. * @handle: pointer to TI SCI handle
  1356. *
  1357. * Return: 0 if all went well, else returns appropriate error value.
  1358. */
  1359. static int ti_sci_cmd_core_reboot(const struct ti_sci_handle *handle)
  1360. {
  1361. struct ti_sci_msg_req_reboot req;
  1362. struct ti_sci_msg_hdr *resp;
  1363. struct ti_sci_info *info;
  1364. struct ti_sci_xfer *xfer;
  1365. int ret = 0;
  1366. if (IS_ERR(handle))
  1367. return PTR_ERR(handle);
  1368. if (!handle)
  1369. return -EINVAL;
  1370. info = handle_to_ti_sci_info(handle);
  1371. xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_SYS_RESET,
  1372. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  1373. (u32 *)&req, sizeof(req), sizeof(*resp));
  1374. if (IS_ERR(xfer)) {
  1375. ret = PTR_ERR(xfer);
  1376. dev_err(info->dev, "Message alloc failed(%d)\n", ret);
  1377. return ret;
  1378. }
  1379. ret = ti_sci_do_xfer(info, xfer);
  1380. if (ret) {
  1381. dev_err(dev, "Mbox send fail %d\n", ret);
  1382. return ret;
  1383. }
  1384. resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
  1385. if (!ti_sci_is_response_ack(resp))
  1386. return -ENODEV;
  1387. return ret;
  1388. }
  1389. static int ti_sci_get_resource_type(struct ti_sci_info *info, u16 dev_id,
  1390. u16 *type)
  1391. {
  1392. struct ti_sci_rm_type_map *rm_type_map = info->desc->rm_type_map;
  1393. bool found = false;
  1394. int i;
  1395. /* If map is not provided then assume dev_id is used as type */
  1396. if (!rm_type_map) {
  1397. *type = dev_id;
  1398. return 0;
  1399. }
  1400. for (i = 0; rm_type_map[i].dev_id; i++) {
  1401. if (rm_type_map[i].dev_id == dev_id) {
  1402. *type = rm_type_map[i].type;
  1403. found = true;
  1404. break;
  1405. }
  1406. }
  1407. if (!found)
  1408. return -EINVAL;
  1409. return 0;
  1410. }
  1411. /**
  1412. * ti_sci_get_resource_range - Helper to get a range of resources assigned
  1413. * to a host. Resource is uniquely identified by
  1414. * type and subtype.
  1415. * @handle: Pointer to TISCI handle.
  1416. * @dev_id: TISCI device ID.
  1417. * @subtype: Resource assignment subtype that is being requested
  1418. * from the given device.
  1419. * @s_host: Host processor ID to which the resources are allocated
  1420. * @range_start: Start index of the resource range
  1421. * @range_num: Number of resources in the range
  1422. *
  1423. * Return: 0 if all went fine, else return appropriate error.
  1424. */
  1425. static int ti_sci_get_resource_range(const struct ti_sci_handle *handle,
  1426. u32 dev_id, u8 subtype, u8 s_host,
  1427. u16 *range_start, u16 *range_num)
  1428. {
  1429. struct ti_sci_msg_resp_get_resource_range *resp;
  1430. struct ti_sci_msg_req_get_resource_range req;
  1431. struct ti_sci_xfer *xfer;
  1432. struct ti_sci_info *info;
  1433. u16 type;
  1434. int ret = 0;
  1435. if (IS_ERR(handle))
  1436. return PTR_ERR(handle);
  1437. if (!handle)
  1438. return -EINVAL;
  1439. info = handle_to_ti_sci_info(handle);
  1440. xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_GET_RESOURCE_RANGE,
  1441. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  1442. (u32 *)&req, sizeof(req), sizeof(*resp));
  1443. if (IS_ERR(xfer)) {
  1444. ret = PTR_ERR(xfer);
  1445. dev_err(dev, "Message alloc failed(%d)\n", ret);
  1446. return ret;
  1447. }
  1448. ret = ti_sci_get_resource_type(info, dev_id, &type);
  1449. if (ret) {
  1450. dev_err(dev, "rm type lookup failed for %u\n", dev_id);
  1451. goto fail;
  1452. }
  1453. req.secondary_host = s_host;
  1454. req.type = type & MSG_RM_RESOURCE_TYPE_MASK;
  1455. req.subtype = subtype & MSG_RM_RESOURCE_SUBTYPE_MASK;
  1456. ret = ti_sci_do_xfer(info, xfer);
  1457. if (ret) {
  1458. dev_err(dev, "Mbox send fail %d\n", ret);
  1459. goto fail;
  1460. }
  1461. resp = (struct ti_sci_msg_resp_get_resource_range *)xfer->tx_message.buf;
  1462. if (!ti_sci_is_response_ack(resp)) {
  1463. ret = -ENODEV;
  1464. } else if (!resp->range_start && !resp->range_num) {
  1465. ret = -ENODEV;
  1466. } else {
  1467. *range_start = resp->range_start;
  1468. *range_num = resp->range_num;
  1469. };
  1470. fail:
  1471. return ret;
  1472. }
  1473. /**
  1474. * ti_sci_cmd_get_resource_range - Get a range of resources assigned to host
  1475. * that is same as ti sci interface host.
  1476. * @handle: Pointer to TISCI handle.
  1477. * @dev_id: TISCI device ID.
  1478. * @subtype: Resource assignment subtype that is being requested
  1479. * from the given device.
  1480. * @range_start: Start index of the resource range
  1481. * @range_num: Number of resources in the range
  1482. *
  1483. * Return: 0 if all went fine, else return appropriate error.
  1484. */
  1485. static int ti_sci_cmd_get_resource_range(const struct ti_sci_handle *handle,
  1486. u32 dev_id, u8 subtype,
  1487. u16 *range_start, u16 *range_num)
  1488. {
  1489. return ti_sci_get_resource_range(handle, dev_id, subtype,
  1490. TI_SCI_IRQ_SECONDARY_HOST_INVALID,
  1491. range_start, range_num);
  1492. }
  1493. /**
  1494. * ti_sci_cmd_get_resource_range_from_shost - Get a range of resources
  1495. * assigned to a specified host.
  1496. * @handle: Pointer to TISCI handle.
  1497. * @dev_id: TISCI device ID.
  1498. * @subtype: Resource assignment subtype that is being requested
  1499. * from the given device.
  1500. * @s_host: Host processor ID to which the resources are allocated
  1501. * @range_start: Start index of the resource range
  1502. * @range_num: Number of resources in the range
  1503. *
  1504. * Return: 0 if all went fine, else return appropriate error.
  1505. */
  1506. static
  1507. int ti_sci_cmd_get_resource_range_from_shost(const struct ti_sci_handle *handle,
  1508. u32 dev_id, u8 subtype, u8 s_host,
  1509. u16 *range_start, u16 *range_num)
  1510. {
  1511. return ti_sci_get_resource_range(handle, dev_id, subtype, s_host,
  1512. range_start, range_num);
  1513. }
  1514. /**
  1515. * ti_sci_cmd_query_msmc() - Command to query currently available msmc memory
  1516. * @handle: pointer to TI SCI handle
  1517. * @msms_start: MSMC start as returned by tisci
  1518. * @msmc_end: MSMC end as returned by tisci
  1519. *
  1520. * Return: 0 if all went well, else returns appropriate error value.
  1521. */
  1522. static int ti_sci_cmd_query_msmc(const struct ti_sci_handle *handle,
  1523. u64 *msmc_start, u64 *msmc_end)
  1524. {
  1525. struct ti_sci_msg_resp_query_msmc *resp;
  1526. struct ti_sci_msg_hdr req;
  1527. struct ti_sci_info *info;
  1528. struct ti_sci_xfer *xfer;
  1529. int ret = 0;
  1530. if (IS_ERR(handle))
  1531. return PTR_ERR(handle);
  1532. if (!handle)
  1533. return -EINVAL;
  1534. info = handle_to_ti_sci_info(handle);
  1535. xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_QUERY_MSMC,
  1536. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  1537. (u32 *)&req, sizeof(req), sizeof(*resp));
  1538. if (IS_ERR(xfer)) {
  1539. ret = PTR_ERR(xfer);
  1540. dev_err(info->dev, "Message alloc failed(%d)\n", ret);
  1541. return ret;
  1542. }
  1543. ret = ti_sci_do_xfer(info, xfer);
  1544. if (ret) {
  1545. dev_err(dev, "Mbox send fail %d\n", ret);
  1546. return ret;
  1547. }
  1548. resp = (struct ti_sci_msg_resp_query_msmc *)xfer->tx_message.buf;
  1549. if (!ti_sci_is_response_ack(resp))
  1550. return -ENODEV;
  1551. *msmc_start = ((u64)resp->msmc_start_high << TISCI_ADDR_HIGH_SHIFT) |
  1552. resp->msmc_start_low;
  1553. *msmc_end = ((u64)resp->msmc_end_high << TISCI_ADDR_HIGH_SHIFT) |
  1554. resp->msmc_end_low;
  1555. return ret;
  1556. }
  1557. /**
  1558. * ti_sci_cmd_proc_request() - Command to request a physical processor control
  1559. * @handle: Pointer to TI SCI handle
  1560. * @proc_id: Processor ID this request is for
  1561. *
  1562. * Return: 0 if all went well, else returns appropriate error value.
  1563. */
  1564. static int ti_sci_cmd_proc_request(const struct ti_sci_handle *handle,
  1565. u8 proc_id)
  1566. {
  1567. struct ti_sci_msg_req_proc_request req;
  1568. struct ti_sci_msg_hdr *resp;
  1569. struct ti_sci_info *info;
  1570. struct ti_sci_xfer *xfer;
  1571. int ret = 0;
  1572. if (IS_ERR(handle))
  1573. return PTR_ERR(handle);
  1574. if (!handle)
  1575. return -EINVAL;
  1576. info = handle_to_ti_sci_info(handle);
  1577. xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_PROC_REQUEST,
  1578. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  1579. (u32 *)&req, sizeof(req), sizeof(*resp));
  1580. if (IS_ERR(xfer)) {
  1581. ret = PTR_ERR(xfer);
  1582. dev_err(info->dev, "Message alloc failed(%d)\n", ret);
  1583. return ret;
  1584. }
  1585. req.processor_id = proc_id;
  1586. ret = ti_sci_do_xfer(info, xfer);
  1587. if (ret) {
  1588. dev_err(info->dev, "Mbox send fail %d\n", ret);
  1589. return ret;
  1590. }
  1591. resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
  1592. if (!ti_sci_is_response_ack(resp))
  1593. ret = -ENODEV;
  1594. return ret;
  1595. }
  1596. /**
  1597. * ti_sci_cmd_proc_release() - Command to release a physical processor control
  1598. * @handle: Pointer to TI SCI handle
  1599. * @proc_id: Processor ID this request is for
  1600. *
  1601. * Return: 0 if all went well, else returns appropriate error value.
  1602. */
  1603. static int ti_sci_cmd_proc_release(const struct ti_sci_handle *handle,
  1604. u8 proc_id)
  1605. {
  1606. struct ti_sci_msg_req_proc_release req;
  1607. struct ti_sci_msg_hdr *resp;
  1608. struct ti_sci_info *info;
  1609. struct ti_sci_xfer *xfer;
  1610. int ret = 0;
  1611. if (IS_ERR(handle))
  1612. return PTR_ERR(handle);
  1613. if (!handle)
  1614. return -EINVAL;
  1615. info = handle_to_ti_sci_info(handle);
  1616. xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_PROC_RELEASE,
  1617. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  1618. (u32 *)&req, sizeof(req), sizeof(*resp));
  1619. if (IS_ERR(xfer)) {
  1620. ret = PTR_ERR(xfer);
  1621. dev_err(info->dev, "Message alloc failed(%d)\n", ret);
  1622. return ret;
  1623. }
  1624. req.processor_id = proc_id;
  1625. ret = ti_sci_do_xfer(info, xfer);
  1626. if (ret) {
  1627. dev_err(info->dev, "Mbox send fail %d\n", ret);
  1628. return ret;
  1629. }
  1630. resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
  1631. if (!ti_sci_is_response_ack(resp))
  1632. ret = -ENODEV;
  1633. return ret;
  1634. }
  1635. /**
  1636. * ti_sci_cmd_proc_handover() - Command to handover a physical processor
  1637. * control to a host in the processor's access
  1638. * control list.
  1639. * @handle: Pointer to TI SCI handle
  1640. * @proc_id: Processor ID this request is for
  1641. * @host_id: Host ID to get the control of the processor
  1642. *
  1643. * Return: 0 if all went well, else returns appropriate error value.
  1644. */
  1645. static int ti_sci_cmd_proc_handover(const struct ti_sci_handle *handle,
  1646. u8 proc_id, u8 host_id)
  1647. {
  1648. struct ti_sci_msg_req_proc_handover req;
  1649. struct ti_sci_msg_hdr *resp;
  1650. struct ti_sci_info *info;
  1651. struct ti_sci_xfer *xfer;
  1652. int ret = 0;
  1653. if (IS_ERR(handle))
  1654. return PTR_ERR(handle);
  1655. if (!handle)
  1656. return -EINVAL;
  1657. info = handle_to_ti_sci_info(handle);
  1658. xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_PROC_HANDOVER,
  1659. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  1660. (u32 *)&req, sizeof(req), sizeof(*resp));
  1661. if (IS_ERR(xfer)) {
  1662. ret = PTR_ERR(xfer);
  1663. dev_err(info->dev, "Message alloc failed(%d)\n", ret);
  1664. return ret;
  1665. }
  1666. req.processor_id = proc_id;
  1667. req.host_id = host_id;
  1668. ret = ti_sci_do_xfer(info, xfer);
  1669. if (ret) {
  1670. dev_err(info->dev, "Mbox send fail %d\n", ret);
  1671. return ret;
  1672. }
  1673. resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
  1674. if (!ti_sci_is_response_ack(resp))
  1675. ret = -ENODEV;
  1676. return ret;
  1677. }
  1678. /**
  1679. * ti_sci_cmd_set_proc_boot_cfg() - Command to set the processor boot
  1680. * configuration flags
  1681. * @handle: Pointer to TI SCI handle
  1682. * @proc_id: Processor ID this request is for
  1683. * @config_flags_set: Configuration flags to be set
  1684. * @config_flags_clear: Configuration flags to be cleared.
  1685. *
  1686. * Return: 0 if all went well, else returns appropriate error value.
  1687. */
  1688. static int ti_sci_cmd_set_proc_boot_cfg(const struct ti_sci_handle *handle,
  1689. u8 proc_id, u64 bootvector,
  1690. u32 config_flags_set,
  1691. u32 config_flags_clear)
  1692. {
  1693. struct ti_sci_msg_req_set_proc_boot_config req;
  1694. struct ti_sci_msg_hdr *resp;
  1695. struct ti_sci_info *info;
  1696. struct ti_sci_xfer *xfer;
  1697. int ret = 0;
  1698. if (IS_ERR(handle))
  1699. return PTR_ERR(handle);
  1700. if (!handle)
  1701. return -EINVAL;
  1702. info = handle_to_ti_sci_info(handle);
  1703. xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_SET_PROC_BOOT_CONFIG,
  1704. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  1705. (u32 *)&req, sizeof(req), sizeof(*resp));
  1706. if (IS_ERR(xfer)) {
  1707. ret = PTR_ERR(xfer);
  1708. dev_err(info->dev, "Message alloc failed(%d)\n", ret);
  1709. return ret;
  1710. }
  1711. req.processor_id = proc_id;
  1712. req.bootvector_low = bootvector & TISCI_ADDR_LOW_MASK;
  1713. req.bootvector_high = (bootvector & TISCI_ADDR_HIGH_MASK) >>
  1714. TISCI_ADDR_HIGH_SHIFT;
  1715. req.config_flags_set = config_flags_set;
  1716. req.config_flags_clear = config_flags_clear;
  1717. ret = ti_sci_do_xfer(info, xfer);
  1718. if (ret) {
  1719. dev_err(info->dev, "Mbox send fail %d\n", ret);
  1720. return ret;
  1721. }
  1722. resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
  1723. if (!ti_sci_is_response_ack(resp))
  1724. ret = -ENODEV;
  1725. return ret;
  1726. }
  1727. /**
  1728. * ti_sci_cmd_set_proc_boot_ctrl() - Command to set the processor boot
  1729. * control flags
  1730. * @handle: Pointer to TI SCI handle
  1731. * @proc_id: Processor ID this request is for
  1732. * @control_flags_set: Control flags to be set
  1733. * @control_flags_clear: Control flags to be cleared
  1734. *
  1735. * Return: 0 if all went well, else returns appropriate error value.
  1736. */
  1737. static int ti_sci_cmd_set_proc_boot_ctrl(const struct ti_sci_handle *handle,
  1738. u8 proc_id, u32 control_flags_set,
  1739. u32 control_flags_clear)
  1740. {
  1741. struct ti_sci_msg_req_set_proc_boot_ctrl req;
  1742. struct ti_sci_msg_hdr *resp;
  1743. struct ti_sci_info *info;
  1744. struct ti_sci_xfer *xfer;
  1745. int ret = 0;
  1746. if (IS_ERR(handle))
  1747. return PTR_ERR(handle);
  1748. if (!handle)
  1749. return -EINVAL;
  1750. info = handle_to_ti_sci_info(handle);
  1751. xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_SET_PROC_BOOT_CTRL,
  1752. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  1753. (u32 *)&req, sizeof(req), sizeof(*resp));
  1754. if (IS_ERR(xfer)) {
  1755. ret = PTR_ERR(xfer);
  1756. dev_err(info->dev, "Message alloc failed(%d)\n", ret);
  1757. return ret;
  1758. }
  1759. req.processor_id = proc_id;
  1760. req.control_flags_set = control_flags_set;
  1761. req.control_flags_clear = control_flags_clear;
  1762. ret = ti_sci_do_xfer(info, xfer);
  1763. if (ret) {
  1764. dev_err(info->dev, "Mbox send fail %d\n", ret);
  1765. return ret;
  1766. }
  1767. resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
  1768. if (!ti_sci_is_response_ack(resp))
  1769. ret = -ENODEV;
  1770. return ret;
  1771. }
  1772. /**
  1773. * ti_sci_cmd_proc_auth_boot_image() - Command to authenticate and load the
  1774. * image and then set the processor configuration flags.
  1775. * @handle: Pointer to TI SCI handle
  1776. * @image_addr: Memory address at which payload image and certificate is
  1777. * located in memory, this is updated if the image data is
  1778. * moved during authentication.
  1779. * @image_size: This is updated with the final size of the image after
  1780. * authentication.
  1781. *
  1782. * Return: 0 if all went well, else returns appropriate error value.
  1783. */
  1784. static int ti_sci_cmd_proc_auth_boot_image(const struct ti_sci_handle *handle,
  1785. u64 *image_addr, u32 *image_size)
  1786. {
  1787. struct ti_sci_msg_req_proc_auth_boot_image req;
  1788. struct ti_sci_msg_resp_proc_auth_boot_image *resp;
  1789. struct ti_sci_info *info;
  1790. struct ti_sci_xfer *xfer;
  1791. int ret = 0;
  1792. if (IS_ERR(handle))
  1793. return PTR_ERR(handle);
  1794. if (!handle)
  1795. return -EINVAL;
  1796. info = handle_to_ti_sci_info(handle);
  1797. xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_PROC_AUTH_BOOT_IMIAGE,
  1798. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  1799. (u32 *)&req, sizeof(req), sizeof(*resp));
  1800. if (IS_ERR(xfer)) {
  1801. ret = PTR_ERR(xfer);
  1802. dev_err(info->dev, "Message alloc failed(%d)\n", ret);
  1803. return ret;
  1804. }
  1805. req.cert_addr_low = *image_addr & TISCI_ADDR_LOW_MASK;
  1806. req.cert_addr_high = (*image_addr & TISCI_ADDR_HIGH_MASK) >>
  1807. TISCI_ADDR_HIGH_SHIFT;
  1808. ret = ti_sci_do_xfer(info, xfer);
  1809. if (ret) {
  1810. dev_err(info->dev, "Mbox send fail %d\n", ret);
  1811. return ret;
  1812. }
  1813. resp = (struct ti_sci_msg_resp_proc_auth_boot_image *)xfer->tx_message.buf;
  1814. if (!ti_sci_is_response_ack(resp))
  1815. return -ENODEV;
  1816. *image_addr = (resp->image_addr_low & TISCI_ADDR_LOW_MASK) |
  1817. (((u64)resp->image_addr_high <<
  1818. TISCI_ADDR_HIGH_SHIFT) & TISCI_ADDR_HIGH_MASK);
  1819. *image_size = resp->image_size;
  1820. return ret;
  1821. }
  1822. /**
  1823. * ti_sci_cmd_get_proc_boot_status() - Command to get the processor boot status
  1824. * @handle: Pointer to TI SCI handle
  1825. * @proc_id: Processor ID this request is for
  1826. *
  1827. * Return: 0 if all went well, else returns appropriate error value.
  1828. */
  1829. static int ti_sci_cmd_get_proc_boot_status(const struct ti_sci_handle *handle,
  1830. u8 proc_id, u64 *bv, u32 *cfg_flags,
  1831. u32 *ctrl_flags, u32 *sts_flags)
  1832. {
  1833. struct ti_sci_msg_resp_get_proc_boot_status *resp;
  1834. struct ti_sci_msg_req_get_proc_boot_status req;
  1835. struct ti_sci_info *info;
  1836. struct ti_sci_xfer *xfer;
  1837. int ret = 0;
  1838. if (IS_ERR(handle))
  1839. return PTR_ERR(handle);
  1840. if (!handle)
  1841. return -EINVAL;
  1842. info = handle_to_ti_sci_info(handle);
  1843. xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_GET_PROC_BOOT_STATUS,
  1844. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  1845. (u32 *)&req, sizeof(req), sizeof(*resp));
  1846. if (IS_ERR(xfer)) {
  1847. ret = PTR_ERR(xfer);
  1848. dev_err(info->dev, "Message alloc failed(%d)\n", ret);
  1849. return ret;
  1850. }
  1851. req.processor_id = proc_id;
  1852. ret = ti_sci_do_xfer(info, xfer);
  1853. if (ret) {
  1854. dev_err(info->dev, "Mbox send fail %d\n", ret);
  1855. return ret;
  1856. }
  1857. resp = (struct ti_sci_msg_resp_get_proc_boot_status *)
  1858. xfer->tx_message.buf;
  1859. if (!ti_sci_is_response_ack(resp))
  1860. return -ENODEV;
  1861. *bv = (resp->bootvector_low & TISCI_ADDR_LOW_MASK) |
  1862. (((u64)resp->bootvector_high <<
  1863. TISCI_ADDR_HIGH_SHIFT) & TISCI_ADDR_HIGH_MASK);
  1864. *cfg_flags = resp->config_flags;
  1865. *ctrl_flags = resp->control_flags;
  1866. *sts_flags = resp->status_flags;
  1867. return ret;
  1868. }
  1869. /**
  1870. * ti_sci_proc_wait_boot_status_no_wait() - Helper function to wait for a
  1871. * processor boot status without requesting or
  1872. * waiting for a response.
  1873. * @proc_id: Processor ID this request is for
  1874. * @num_wait_iterations: Total number of iterations we will check before
  1875. * we will timeout and give up
  1876. * @num_match_iterations: How many iterations should we have continued
  1877. * status to account for status bits glitching.
  1878. * This is to make sure that match occurs for
  1879. * consecutive checks. This implies that the
  1880. * worst case should consider that the stable
  1881. * time should at the worst be num_wait_iterations
  1882. * num_match_iterations to prevent timeout.
  1883. * @delay_per_iteration_us: Specifies how long to wait (in micro seconds)
  1884. * between each status checks. This is the minimum
  1885. * duration, and overhead of register reads and
  1886. * checks are on top of this and can vary based on
  1887. * varied conditions.
  1888. * @delay_before_iterations_us: Specifies how long to wait (in micro seconds)
  1889. * before the very first check in the first
  1890. * iteration of status check loop. This is the
  1891. * minimum duration, and overhead of register
  1892. * reads and checks are.
  1893. * @status_flags_1_set_all_wait:If non-zero, Specifies that all bits of the
  1894. * status matching this field requested MUST be 1.
  1895. * @status_flags_1_set_any_wait:If non-zero, Specifies that at least one of the
  1896. * bits matching this field requested MUST be 1.
  1897. * @status_flags_1_clr_all_wait:If non-zero, Specifies that all bits of the
  1898. * status matching this field requested MUST be 0.
  1899. * @status_flags_1_clr_any_wait:If non-zero, Specifies that at least one of the
  1900. * bits matching this field requested MUST be 0.
  1901. *
  1902. * Return: 0 if all goes well, else appropriate error message
  1903. */
  1904. static int
  1905. ti_sci_proc_wait_boot_status_no_wait(const struct ti_sci_handle *handle,
  1906. u8 proc_id,
  1907. u8 num_wait_iterations,
  1908. u8 num_match_iterations,
  1909. u8 delay_per_iteration_us,
  1910. u8 delay_before_iterations_us,
  1911. u32 status_flags_1_set_all_wait,
  1912. u32 status_flags_1_set_any_wait,
  1913. u32 status_flags_1_clr_all_wait,
  1914. u32 status_flags_1_clr_any_wait)
  1915. {
  1916. struct ti_sci_msg_req_wait_proc_boot_status req;
  1917. struct ti_sci_info *info;
  1918. struct ti_sci_xfer *xfer;
  1919. int ret = 0;
  1920. if (IS_ERR(handle))
  1921. return PTR_ERR(handle);
  1922. if (!handle)
  1923. return -EINVAL;
  1924. info = handle_to_ti_sci_info(handle);
  1925. xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_WAIT_PROC_BOOT_STATUS,
  1926. TI_SCI_FLAG_REQ_GENERIC_NORESPONSE,
  1927. (u32 *)&req, sizeof(req), 0);
  1928. if (IS_ERR(xfer)) {
  1929. ret = PTR_ERR(xfer);
  1930. dev_err(info->dev, "Message alloc failed(%d)\n", ret);
  1931. return ret;
  1932. }
  1933. req.processor_id = proc_id;
  1934. req.num_wait_iterations = num_wait_iterations;
  1935. req.num_match_iterations = num_match_iterations;
  1936. req.delay_per_iteration_us = delay_per_iteration_us;
  1937. req.delay_before_iterations_us = delay_before_iterations_us;
  1938. req.status_flags_1_set_all_wait = status_flags_1_set_all_wait;
  1939. req.status_flags_1_set_any_wait = status_flags_1_set_any_wait;
  1940. req.status_flags_1_clr_all_wait = status_flags_1_clr_all_wait;
  1941. req.status_flags_1_clr_any_wait = status_flags_1_clr_any_wait;
  1942. ret = ti_sci_do_xfer(info, xfer);
  1943. if (ret)
  1944. dev_err(info->dev, "Mbox send fail %d\n", ret);
  1945. return ret;
  1946. }
  1947. /**
  1948. * ti_sci_cmd_proc_shutdown_no_wait() - Command to shutdown a core without
  1949. * requesting or waiting for a response. Note that this API call
  1950. * should be followed by placing the respective processor into
  1951. * either WFE or WFI mode.
  1952. * @handle: Pointer to TI SCI handle
  1953. * @proc_id: Processor ID this request is for
  1954. *
  1955. * Return: 0 if all went well, else returns appropriate error value.
  1956. */
  1957. static int ti_sci_cmd_proc_shutdown_no_wait(const struct ti_sci_handle *handle,
  1958. u8 proc_id)
  1959. {
  1960. int ret;
  1961. /*
  1962. * Send the core boot status wait message waiting for either WFE or
  1963. * WFI without requesting or waiting for a TISCI response with the
  1964. * maximum wait time to give us the best chance to get to the WFE/WFI
  1965. * command that should follow the invocation of this API before the
  1966. * DMSC-internal processing of this command times out. Note that
  1967. * waiting for the R5 WFE/WFI flags will also work on an ARMV8 type
  1968. * core as the related flag bit positions are the same.
  1969. */
  1970. ret = ti_sci_proc_wait_boot_status_no_wait(handle, proc_id,
  1971. U8_MAX, 100, U8_MAX, U8_MAX,
  1972. 0, PROC_BOOT_STATUS_FLAG_R5_WFE | PROC_BOOT_STATUS_FLAG_R5_WFI,
  1973. 0, 0);
  1974. if (ret) {
  1975. dev_err(info->dev, "Sending core %u wait message fail %d\n",
  1976. proc_id, ret);
  1977. return ret;
  1978. }
  1979. /*
  1980. * Release a processor managed by TISCI without requesting or waiting
  1981. * for a response.
  1982. */
  1983. ret = ti_sci_set_device_state_no_wait(handle, proc_id, 0,
  1984. MSG_DEVICE_SW_STATE_AUTO_OFF);
  1985. if (ret)
  1986. dev_err(info->dev, "Sending core %u shutdown message fail %d\n",
  1987. proc_id, ret);
  1988. return ret;
  1989. }
  1990. /**
  1991. * ti_sci_cmd_ring_config() - configure RA ring
  1992. * @handle: pointer to TI SCI handle
  1993. * @valid_params: Bitfield defining validity of ring configuration parameters.
  1994. * @nav_id: Device ID of Navigator Subsystem from which the ring is allocated
  1995. * @index: Ring index.
  1996. * @addr_lo: The ring base address lo 32 bits
  1997. * @addr_hi: The ring base address hi 32 bits
  1998. * @count: Number of ring elements.
  1999. * @mode: The mode of the ring
  2000. * @size: The ring element size.
  2001. * @order_id: Specifies the ring's bus order ID.
  2002. *
  2003. * Return: 0 if all went well, else returns appropriate error value.
  2004. *
  2005. * See @ti_sci_msg_rm_ring_cfg_req for more info.
  2006. */
  2007. static int ti_sci_cmd_ring_config(const struct ti_sci_handle *handle,
  2008. u32 valid_params, u16 nav_id, u16 index,
  2009. u32 addr_lo, u32 addr_hi, u32 count,
  2010. u8 mode, u8 size, u8 order_id)
  2011. {
  2012. struct ti_sci_msg_rm_ring_cfg_resp *resp;
  2013. struct ti_sci_msg_rm_ring_cfg_req req;
  2014. struct ti_sci_xfer *xfer;
  2015. struct ti_sci_info *info;
  2016. int ret = 0;
  2017. if (IS_ERR(handle))
  2018. return PTR_ERR(handle);
  2019. if (!handle)
  2020. return -EINVAL;
  2021. info = handle_to_ti_sci_info(handle);
  2022. xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_RM_RING_CFG,
  2023. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  2024. (u32 *)&req, sizeof(req), sizeof(*resp));
  2025. if (IS_ERR(xfer)) {
  2026. ret = PTR_ERR(xfer);
  2027. dev_err(info->dev, "RM_RA:Message config failed(%d)\n", ret);
  2028. return ret;
  2029. }
  2030. req.valid_params = valid_params;
  2031. req.nav_id = nav_id;
  2032. req.index = index;
  2033. req.addr_lo = addr_lo;
  2034. req.addr_hi = addr_hi;
  2035. req.count = count;
  2036. req.mode = mode;
  2037. req.size = size;
  2038. req.order_id = order_id;
  2039. ret = ti_sci_do_xfer(info, xfer);
  2040. if (ret) {
  2041. dev_err(info->dev, "RM_RA:Mbox config send fail %d\n", ret);
  2042. goto fail;
  2043. }
  2044. resp = (struct ti_sci_msg_rm_ring_cfg_resp *)xfer->tx_message.buf;
  2045. ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
  2046. fail:
  2047. dev_dbg(info->dev, "RM_RA:config ring %u ret:%d\n", index, ret);
  2048. return ret;
  2049. }
  2050. static int ti_sci_cmd_rm_psil_pair(const struct ti_sci_handle *handle,
  2051. u32 nav_id, u32 src_thread, u32 dst_thread)
  2052. {
  2053. struct ti_sci_msg_hdr *resp;
  2054. struct ti_sci_msg_psil_pair req;
  2055. struct ti_sci_xfer *xfer;
  2056. struct ti_sci_info *info;
  2057. int ret = 0;
  2058. if (IS_ERR(handle))
  2059. return PTR_ERR(handle);
  2060. if (!handle)
  2061. return -EINVAL;
  2062. info = handle_to_ti_sci_info(handle);
  2063. xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_RM_PSIL_PAIR,
  2064. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  2065. (u32 *)&req, sizeof(req), sizeof(*resp));
  2066. if (IS_ERR(xfer)) {
  2067. ret = PTR_ERR(xfer);
  2068. dev_err(info->dev, "RM_PSIL:Message alloc failed(%d)\n", ret);
  2069. return ret;
  2070. }
  2071. req.nav_id = nav_id;
  2072. req.src_thread = src_thread;
  2073. req.dst_thread = dst_thread;
  2074. ret = ti_sci_do_xfer(info, xfer);
  2075. if (ret) {
  2076. dev_err(info->dev, "RM_PSIL:Mbox send fail %d\n", ret);
  2077. goto fail;
  2078. }
  2079. resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
  2080. ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
  2081. fail:
  2082. dev_dbg(info->dev, "RM_PSIL: nav: %u link pair %u->%u ret:%u\n",
  2083. nav_id, src_thread, dst_thread, ret);
  2084. return ret;
  2085. }
  2086. static int ti_sci_cmd_rm_psil_unpair(const struct ti_sci_handle *handle,
  2087. u32 nav_id, u32 src_thread, u32 dst_thread)
  2088. {
  2089. struct ti_sci_msg_hdr *resp;
  2090. struct ti_sci_msg_psil_unpair req;
  2091. struct ti_sci_xfer *xfer;
  2092. struct ti_sci_info *info;
  2093. int ret = 0;
  2094. if (IS_ERR(handle))
  2095. return PTR_ERR(handle);
  2096. if (!handle)
  2097. return -EINVAL;
  2098. info = handle_to_ti_sci_info(handle);
  2099. xfer = ti_sci_setup_one_xfer(info, TI_SCI_MSG_RM_PSIL_UNPAIR,
  2100. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  2101. (u32 *)&req, sizeof(req), sizeof(*resp));
  2102. if (IS_ERR(xfer)) {
  2103. ret = PTR_ERR(xfer);
  2104. dev_err(info->dev, "RM_PSIL:Message alloc failed(%d)\n", ret);
  2105. return ret;
  2106. }
  2107. req.nav_id = nav_id;
  2108. req.src_thread = src_thread;
  2109. req.dst_thread = dst_thread;
  2110. ret = ti_sci_do_xfer(info, xfer);
  2111. if (ret) {
  2112. dev_err(info->dev, "RM_PSIL:Mbox send fail %d\n", ret);
  2113. goto fail;
  2114. }
  2115. resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
  2116. ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
  2117. fail:
  2118. dev_dbg(info->dev, "RM_PSIL: link unpair %u->%u ret:%u\n",
  2119. src_thread, dst_thread, ret);
  2120. return ret;
  2121. }
  2122. static int ti_sci_cmd_rm_udmap_tx_ch_cfg(
  2123. const struct ti_sci_handle *handle,
  2124. const struct ti_sci_msg_rm_udmap_tx_ch_cfg *params)
  2125. {
  2126. struct ti_sci_msg_rm_udmap_tx_ch_cfg_resp *resp;
  2127. struct ti_sci_msg_rm_udmap_tx_ch_cfg_req req;
  2128. struct ti_sci_xfer *xfer;
  2129. struct ti_sci_info *info;
  2130. int ret = 0;
  2131. if (IS_ERR(handle))
  2132. return PTR_ERR(handle);
  2133. if (!handle)
  2134. return -EINVAL;
  2135. info = handle_to_ti_sci_info(handle);
  2136. xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_RM_UDMAP_TX_CH_CFG,
  2137. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  2138. (u32 *)&req, sizeof(req), sizeof(*resp));
  2139. if (IS_ERR(xfer)) {
  2140. ret = PTR_ERR(xfer);
  2141. dev_err(info->dev, "Message TX_CH_CFG alloc failed(%d)\n", ret);
  2142. return ret;
  2143. }
  2144. req.valid_params = params->valid_params;
  2145. req.nav_id = params->nav_id;
  2146. req.index = params->index;
  2147. req.tx_pause_on_err = params->tx_pause_on_err;
  2148. req.tx_filt_einfo = params->tx_filt_einfo;
  2149. req.tx_filt_pswords = params->tx_filt_pswords;
  2150. req.tx_atype = params->tx_atype;
  2151. req.tx_chan_type = params->tx_chan_type;
  2152. req.tx_supr_tdpkt = params->tx_supr_tdpkt;
  2153. req.tx_fetch_size = params->tx_fetch_size;
  2154. req.tx_credit_count = params->tx_credit_count;
  2155. req.txcq_qnum = params->txcq_qnum;
  2156. req.tx_priority = params->tx_priority;
  2157. req.tx_qos = params->tx_qos;
  2158. req.tx_orderid = params->tx_orderid;
  2159. req.fdepth = params->fdepth;
  2160. req.tx_sched_priority = params->tx_sched_priority;
  2161. ret = ti_sci_do_xfer(info, xfer);
  2162. if (ret) {
  2163. dev_err(info->dev, "Mbox send TX_CH_CFG fail %d\n", ret);
  2164. goto fail;
  2165. }
  2166. resp =
  2167. (struct ti_sci_msg_rm_udmap_tx_ch_cfg_resp *)xfer->tx_message.buf;
  2168. ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
  2169. fail:
  2170. dev_dbg(info->dev, "TX_CH_CFG: chn %u ret:%u\n", params->index, ret);
  2171. return ret;
  2172. }
  2173. static int ti_sci_cmd_rm_udmap_rx_ch_cfg(
  2174. const struct ti_sci_handle *handle,
  2175. const struct ti_sci_msg_rm_udmap_rx_ch_cfg *params)
  2176. {
  2177. struct ti_sci_msg_rm_udmap_rx_ch_cfg_resp *resp;
  2178. struct ti_sci_msg_rm_udmap_rx_ch_cfg_req req;
  2179. struct ti_sci_xfer *xfer;
  2180. struct ti_sci_info *info;
  2181. int ret = 0;
  2182. if (IS_ERR(handle))
  2183. return PTR_ERR(handle);
  2184. if (!handle)
  2185. return -EINVAL;
  2186. info = handle_to_ti_sci_info(handle);
  2187. xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_RM_UDMAP_RX_CH_CFG,
  2188. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  2189. (u32 *)&req, sizeof(req), sizeof(*resp));
  2190. if (IS_ERR(xfer)) {
  2191. ret = PTR_ERR(xfer);
  2192. dev_err(info->dev, "Message RX_CH_CFG alloc failed(%d)\n", ret);
  2193. return ret;
  2194. }
  2195. req.valid_params = params->valid_params;
  2196. req.nav_id = params->nav_id;
  2197. req.index = params->index;
  2198. req.rx_fetch_size = params->rx_fetch_size;
  2199. req.rxcq_qnum = params->rxcq_qnum;
  2200. req.rx_priority = params->rx_priority;
  2201. req.rx_qos = params->rx_qos;
  2202. req.rx_orderid = params->rx_orderid;
  2203. req.rx_sched_priority = params->rx_sched_priority;
  2204. req.flowid_start = params->flowid_start;
  2205. req.flowid_cnt = params->flowid_cnt;
  2206. req.rx_pause_on_err = params->rx_pause_on_err;
  2207. req.rx_atype = params->rx_atype;
  2208. req.rx_chan_type = params->rx_chan_type;
  2209. req.rx_ignore_short = params->rx_ignore_short;
  2210. req.rx_ignore_long = params->rx_ignore_long;
  2211. ret = ti_sci_do_xfer(info, xfer);
  2212. if (ret) {
  2213. dev_err(info->dev, "Mbox send RX_CH_CFG fail %d\n", ret);
  2214. goto fail;
  2215. }
  2216. resp =
  2217. (struct ti_sci_msg_rm_udmap_rx_ch_cfg_resp *)xfer->tx_message.buf;
  2218. ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
  2219. fail:
  2220. dev_dbg(info->dev, "RX_CH_CFG: chn %u ret:%d\n", params->index, ret);
  2221. return ret;
  2222. }
  2223. static int ti_sci_cmd_rm_udmap_rx_flow_cfg(
  2224. const struct ti_sci_handle *handle,
  2225. const struct ti_sci_msg_rm_udmap_flow_cfg *params)
  2226. {
  2227. struct ti_sci_msg_rm_udmap_flow_cfg_resp *resp;
  2228. struct ti_sci_msg_rm_udmap_flow_cfg_req req;
  2229. struct ti_sci_xfer *xfer;
  2230. struct ti_sci_info *info;
  2231. int ret = 0;
  2232. if (IS_ERR(handle))
  2233. return PTR_ERR(handle);
  2234. if (!handle)
  2235. return -EINVAL;
  2236. info = handle_to_ti_sci_info(handle);
  2237. xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_RM_UDMAP_FLOW_CFG,
  2238. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  2239. (u32 *)&req, sizeof(req), sizeof(*resp));
  2240. if (IS_ERR(xfer)) {
  2241. ret = PTR_ERR(xfer);
  2242. dev_err(dev, "RX_FL_CFG: Message alloc failed(%d)\n", ret);
  2243. return ret;
  2244. }
  2245. req.valid_params = params->valid_params;
  2246. req.nav_id = params->nav_id;
  2247. req.flow_index = params->flow_index;
  2248. req.rx_einfo_present = params->rx_einfo_present;
  2249. req.rx_psinfo_present = params->rx_psinfo_present;
  2250. req.rx_error_handling = params->rx_error_handling;
  2251. req.rx_desc_type = params->rx_desc_type;
  2252. req.rx_sop_offset = params->rx_sop_offset;
  2253. req.rx_dest_qnum = params->rx_dest_qnum;
  2254. req.rx_src_tag_hi = params->rx_src_tag_hi;
  2255. req.rx_src_tag_lo = params->rx_src_tag_lo;
  2256. req.rx_dest_tag_hi = params->rx_dest_tag_hi;
  2257. req.rx_dest_tag_lo = params->rx_dest_tag_lo;
  2258. req.rx_src_tag_hi_sel = params->rx_src_tag_hi_sel;
  2259. req.rx_src_tag_lo_sel = params->rx_src_tag_lo_sel;
  2260. req.rx_dest_tag_hi_sel = params->rx_dest_tag_hi_sel;
  2261. req.rx_dest_tag_lo_sel = params->rx_dest_tag_lo_sel;
  2262. req.rx_fdq0_sz0_qnum = params->rx_fdq0_sz0_qnum;
  2263. req.rx_fdq1_qnum = params->rx_fdq1_qnum;
  2264. req.rx_fdq2_qnum = params->rx_fdq2_qnum;
  2265. req.rx_fdq3_qnum = params->rx_fdq3_qnum;
  2266. req.rx_ps_location = params->rx_ps_location;
  2267. ret = ti_sci_do_xfer(info, xfer);
  2268. if (ret) {
  2269. dev_err(dev, "RX_FL_CFG: Mbox send fail %d\n", ret);
  2270. goto fail;
  2271. }
  2272. resp =
  2273. (struct ti_sci_msg_rm_udmap_flow_cfg_resp *)xfer->tx_message.buf;
  2274. ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
  2275. fail:
  2276. dev_dbg(info->dev, "RX_FL_CFG: %u ret:%d\n", params->flow_index, ret);
  2277. return ret;
  2278. }
  2279. /**
  2280. * ti_sci_cmd_set_fwl_region() - Request for configuring a firewall region
  2281. * @handle: pointer to TI SCI handle
  2282. * @region: region configuration parameters
  2283. *
  2284. * Return: 0 if all went well, else returns appropriate error value.
  2285. */
  2286. static int ti_sci_cmd_set_fwl_region(const struct ti_sci_handle *handle,
  2287. const struct ti_sci_msg_fwl_region *region)
  2288. {
  2289. struct ti_sci_msg_fwl_set_firewall_region_req req;
  2290. struct ti_sci_msg_hdr *resp;
  2291. struct ti_sci_info *info;
  2292. struct ti_sci_xfer *xfer;
  2293. int ret = 0;
  2294. if (IS_ERR(handle))
  2295. return PTR_ERR(handle);
  2296. if (!handle)
  2297. return -EINVAL;
  2298. info = handle_to_ti_sci_info(handle);
  2299. xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_FWL_SET,
  2300. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  2301. (u32 *)&req, sizeof(req), sizeof(*resp));
  2302. if (IS_ERR(xfer)) {
  2303. ret = PTR_ERR(xfer);
  2304. dev_err(info->dev, "Message alloc failed(%d)\n", ret);
  2305. return ret;
  2306. }
  2307. req.fwl_id = region->fwl_id;
  2308. req.region = region->region;
  2309. req.n_permission_regs = region->n_permission_regs;
  2310. req.control = region->control;
  2311. req.permissions[0] = region->permissions[0];
  2312. req.permissions[1] = region->permissions[1];
  2313. req.permissions[2] = region->permissions[2];
  2314. req.start_address = region->start_address;
  2315. req.end_address = region->end_address;
  2316. ret = ti_sci_do_xfer(info, xfer);
  2317. if (ret) {
  2318. dev_err(info->dev, "Mbox send fail %d\n", ret);
  2319. return ret;
  2320. }
  2321. resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
  2322. if (!ti_sci_is_response_ack(resp))
  2323. return -ENODEV;
  2324. return 0;
  2325. }
  2326. /**
  2327. * ti_sci_cmd_get_fwl_region() - Request for getting a firewall region
  2328. * @handle: pointer to TI SCI handle
  2329. * @region: region configuration parameters
  2330. *
  2331. * Return: 0 if all went well, else returns appropriate error value.
  2332. */
  2333. static int ti_sci_cmd_get_fwl_region(const struct ti_sci_handle *handle,
  2334. struct ti_sci_msg_fwl_region *region)
  2335. {
  2336. struct ti_sci_msg_fwl_get_firewall_region_req req;
  2337. struct ti_sci_msg_fwl_get_firewall_region_resp *resp;
  2338. struct ti_sci_info *info;
  2339. struct ti_sci_xfer *xfer;
  2340. int ret = 0;
  2341. if (IS_ERR(handle))
  2342. return PTR_ERR(handle);
  2343. if (!handle)
  2344. return -EINVAL;
  2345. info = handle_to_ti_sci_info(handle);
  2346. xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_FWL_GET,
  2347. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  2348. (u32 *)&req, sizeof(req), sizeof(*resp));
  2349. if (IS_ERR(xfer)) {
  2350. ret = PTR_ERR(xfer);
  2351. dev_err(info->dev, "Message alloc failed(%d)\n", ret);
  2352. return ret;
  2353. }
  2354. req.fwl_id = region->fwl_id;
  2355. req.region = region->region;
  2356. req.n_permission_regs = region->n_permission_regs;
  2357. ret = ti_sci_do_xfer(info, xfer);
  2358. if (ret) {
  2359. dev_err(info->dev, "Mbox send fail %d\n", ret);
  2360. return ret;
  2361. }
  2362. resp = (struct ti_sci_msg_fwl_get_firewall_region_resp *)xfer->tx_message.buf;
  2363. if (!ti_sci_is_response_ack(resp))
  2364. return -ENODEV;
  2365. region->fwl_id = resp->fwl_id;
  2366. region->region = resp->region;
  2367. region->n_permission_regs = resp->n_permission_regs;
  2368. region->control = resp->control;
  2369. region->permissions[0] = resp->permissions[0];
  2370. region->permissions[1] = resp->permissions[1];
  2371. region->permissions[2] = resp->permissions[2];
  2372. region->start_address = resp->start_address;
  2373. region->end_address = resp->end_address;
  2374. return 0;
  2375. }
  2376. /**
  2377. * ti_sci_cmd_change_fwl_owner() - Request for changing a firewall owner
  2378. * @handle: pointer to TI SCI handle
  2379. * @region: region configuration parameters
  2380. *
  2381. * Return: 0 if all went well, else returns appropriate error value.
  2382. */
  2383. static int ti_sci_cmd_change_fwl_owner(const struct ti_sci_handle *handle,
  2384. struct ti_sci_msg_fwl_owner *owner)
  2385. {
  2386. struct ti_sci_msg_fwl_change_owner_info_req req;
  2387. struct ti_sci_msg_fwl_change_owner_info_resp *resp;
  2388. struct ti_sci_info *info;
  2389. struct ti_sci_xfer *xfer;
  2390. int ret = 0;
  2391. if (IS_ERR(handle))
  2392. return PTR_ERR(handle);
  2393. if (!handle)
  2394. return -EINVAL;
  2395. info = handle_to_ti_sci_info(handle);
  2396. xfer = ti_sci_setup_one_xfer(info, TISCI_MSG_FWL_CHANGE_OWNER,
  2397. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  2398. (u32 *)&req, sizeof(req), sizeof(*resp));
  2399. if (IS_ERR(xfer)) {
  2400. ret = PTR_ERR(xfer);
  2401. dev_err(info->dev, "Message alloc failed(%d)\n", ret);
  2402. return ret;
  2403. }
  2404. req.fwl_id = owner->fwl_id;
  2405. req.region = owner->region;
  2406. req.owner_index = owner->owner_index;
  2407. ret = ti_sci_do_xfer(info, xfer);
  2408. if (ret) {
  2409. dev_err(info->dev, "Mbox send fail %d\n", ret);
  2410. return ret;
  2411. }
  2412. resp = (struct ti_sci_msg_fwl_change_owner_info_resp *)xfer->tx_message.buf;
  2413. if (!ti_sci_is_response_ack(resp))
  2414. return -ENODEV;
  2415. owner->fwl_id = resp->fwl_id;
  2416. owner->region = resp->region;
  2417. owner->owner_index = resp->owner_index;
  2418. owner->owner_privid = resp->owner_privid;
  2419. owner->owner_permission_bits = resp->owner_permission_bits;
  2420. return ret;
  2421. }
  2422. /*
  2423. * ti_sci_setup_ops() - Setup the operations structures
  2424. * @info: pointer to TISCI pointer
  2425. */
  2426. static void ti_sci_setup_ops(struct ti_sci_info *info)
  2427. {
  2428. struct ti_sci_ops *ops = &info->handle.ops;
  2429. struct ti_sci_board_ops *bops = &ops->board_ops;
  2430. struct ti_sci_dev_ops *dops = &ops->dev_ops;
  2431. struct ti_sci_clk_ops *cops = &ops->clk_ops;
  2432. struct ti_sci_core_ops *core_ops = &ops->core_ops;
  2433. struct ti_sci_rm_core_ops *rm_core_ops = &ops->rm_core_ops;
  2434. struct ti_sci_proc_ops *pops = &ops->proc_ops;
  2435. struct ti_sci_rm_ringacc_ops *rops = &ops->rm_ring_ops;
  2436. struct ti_sci_rm_psil_ops *psilops = &ops->rm_psil_ops;
  2437. struct ti_sci_rm_udmap_ops *udmap_ops = &ops->rm_udmap_ops;
  2438. struct ti_sci_fwl_ops *fwl_ops = &ops->fwl_ops;
  2439. bops->board_config = ti_sci_cmd_set_board_config;
  2440. bops->board_config_rm = ti_sci_cmd_set_board_config_rm;
  2441. bops->board_config_security = ti_sci_cmd_set_board_config_security;
  2442. bops->board_config_pm = ti_sci_cmd_set_board_config_pm;
  2443. dops->get_device = ti_sci_cmd_get_device;
  2444. dops->get_device_exclusive = ti_sci_cmd_get_device_exclusive;
  2445. dops->idle_device = ti_sci_cmd_idle_device;
  2446. dops->idle_device_exclusive = ti_sci_cmd_idle_device_exclusive;
  2447. dops->put_device = ti_sci_cmd_put_device;
  2448. dops->is_valid = ti_sci_cmd_dev_is_valid;
  2449. dops->get_context_loss_count = ti_sci_cmd_dev_get_clcnt;
  2450. dops->is_idle = ti_sci_cmd_dev_is_idle;
  2451. dops->is_stop = ti_sci_cmd_dev_is_stop;
  2452. dops->is_on = ti_sci_cmd_dev_is_on;
  2453. dops->is_transitioning = ti_sci_cmd_dev_is_trans;
  2454. dops->set_device_resets = ti_sci_cmd_set_device_resets;
  2455. dops->get_device_resets = ti_sci_cmd_get_device_resets;
  2456. dops->release_exclusive_devices = ti_sci_cmd_release_exclusive_devices;
  2457. cops->get_clock = ti_sci_cmd_get_clock;
  2458. cops->idle_clock = ti_sci_cmd_idle_clock;
  2459. cops->put_clock = ti_sci_cmd_put_clock;
  2460. cops->is_auto = ti_sci_cmd_clk_is_auto;
  2461. cops->is_on = ti_sci_cmd_clk_is_on;
  2462. cops->is_off = ti_sci_cmd_clk_is_off;
  2463. cops->set_parent = ti_sci_cmd_clk_set_parent;
  2464. cops->get_parent = ti_sci_cmd_clk_get_parent;
  2465. cops->get_num_parents = ti_sci_cmd_clk_get_num_parents;
  2466. cops->get_best_match_freq = ti_sci_cmd_clk_get_match_freq;
  2467. cops->set_freq = ti_sci_cmd_clk_set_freq;
  2468. cops->get_freq = ti_sci_cmd_clk_get_freq;
  2469. core_ops->reboot_device = ti_sci_cmd_core_reboot;
  2470. core_ops->query_msmc = ti_sci_cmd_query_msmc;
  2471. rm_core_ops->get_range = ti_sci_cmd_get_resource_range;
  2472. rm_core_ops->get_range_from_shost =
  2473. ti_sci_cmd_get_resource_range_from_shost;
  2474. pops->proc_request = ti_sci_cmd_proc_request;
  2475. pops->proc_release = ti_sci_cmd_proc_release;
  2476. pops->proc_handover = ti_sci_cmd_proc_handover;
  2477. pops->set_proc_boot_cfg = ti_sci_cmd_set_proc_boot_cfg;
  2478. pops->set_proc_boot_ctrl = ti_sci_cmd_set_proc_boot_ctrl;
  2479. pops->proc_auth_boot_image = ti_sci_cmd_proc_auth_boot_image;
  2480. pops->get_proc_boot_status = ti_sci_cmd_get_proc_boot_status;
  2481. pops->proc_shutdown_no_wait = ti_sci_cmd_proc_shutdown_no_wait;
  2482. rops->config = ti_sci_cmd_ring_config;
  2483. psilops->pair = ti_sci_cmd_rm_psil_pair;
  2484. psilops->unpair = ti_sci_cmd_rm_psil_unpair;
  2485. udmap_ops->tx_ch_cfg = ti_sci_cmd_rm_udmap_tx_ch_cfg;
  2486. udmap_ops->rx_ch_cfg = ti_sci_cmd_rm_udmap_rx_ch_cfg;
  2487. udmap_ops->rx_flow_cfg = ti_sci_cmd_rm_udmap_rx_flow_cfg;
  2488. fwl_ops->set_fwl_region = ti_sci_cmd_set_fwl_region;
  2489. fwl_ops->get_fwl_region = ti_sci_cmd_get_fwl_region;
  2490. fwl_ops->change_fwl_owner = ti_sci_cmd_change_fwl_owner;
  2491. }
  2492. /**
  2493. * ti_sci_get_handle_from_sysfw() - Get the TI SCI handle of the SYSFW
  2494. * @dev: Pointer to the SYSFW device
  2495. *
  2496. * Return: pointer to handle if successful, else EINVAL if invalid conditions
  2497. * are encountered.
  2498. */
  2499. const
  2500. struct ti_sci_handle *ti_sci_get_handle_from_sysfw(struct udevice *sci_dev)
  2501. {
  2502. if (!sci_dev)
  2503. return ERR_PTR(-EINVAL);
  2504. struct ti_sci_info *info = dev_get_priv(sci_dev);
  2505. if (!info)
  2506. return ERR_PTR(-EINVAL);
  2507. struct ti_sci_handle *handle = &info->handle;
  2508. if (!handle)
  2509. return ERR_PTR(-EINVAL);
  2510. return handle;
  2511. }
  2512. /**
  2513. * ti_sci_get_handle() - Get the TI SCI handle for a device
  2514. * @dev: Pointer to device for which we want SCI handle
  2515. *
  2516. * Return: pointer to handle if successful, else EINVAL if invalid conditions
  2517. * are encountered.
  2518. */
  2519. const struct ti_sci_handle *ti_sci_get_handle(struct udevice *dev)
  2520. {
  2521. if (!dev)
  2522. return ERR_PTR(-EINVAL);
  2523. struct udevice *sci_dev = dev_get_parent(dev);
  2524. return ti_sci_get_handle_from_sysfw(sci_dev);
  2525. }
  2526. /**
  2527. * ti_sci_get_by_phandle() - Get the TI SCI handle using DT phandle
  2528. * @dev: device node
  2529. * @propname: property name containing phandle on TISCI node
  2530. *
  2531. * Return: pointer to handle if successful, else appropriate error value.
  2532. */
  2533. const struct ti_sci_handle *ti_sci_get_by_phandle(struct udevice *dev,
  2534. const char *property)
  2535. {
  2536. struct ti_sci_info *entry, *info = NULL;
  2537. u32 phandle, err;
  2538. ofnode node;
  2539. err = ofnode_read_u32(dev_ofnode(dev), property, &phandle);
  2540. if (err)
  2541. return ERR_PTR(err);
  2542. node = ofnode_get_by_phandle(phandle);
  2543. if (!ofnode_valid(node))
  2544. return ERR_PTR(-EINVAL);
  2545. list_for_each_entry(entry, &ti_sci_list, list)
  2546. if (ofnode_equal(dev_ofnode(entry->dev), node)) {
  2547. info = entry;
  2548. break;
  2549. }
  2550. if (!info)
  2551. return ERR_PTR(-ENODEV);
  2552. return &info->handle;
  2553. }
  2554. /**
  2555. * ti_sci_of_to_info() - generate private data from device tree
  2556. * @dev: corresponding system controller interface device
  2557. * @info: pointer to driver specific private data
  2558. *
  2559. * Return: 0 if all goes good, else appropriate error message.
  2560. */
  2561. static int ti_sci_of_to_info(struct udevice *dev, struct ti_sci_info *info)
  2562. {
  2563. int ret;
  2564. ret = mbox_get_by_name(dev, "tx", &info->chan_tx);
  2565. if (ret) {
  2566. dev_err(dev, "%s: Acquiring Tx channel failed. ret = %d\n",
  2567. __func__, ret);
  2568. return ret;
  2569. }
  2570. ret = mbox_get_by_name(dev, "rx", &info->chan_rx);
  2571. if (ret) {
  2572. dev_err(dev, "%s: Acquiring Rx channel failed. ret = %d\n",
  2573. __func__, ret);
  2574. return ret;
  2575. }
  2576. /* Notify channel is optional. Enable only if populated */
  2577. ret = mbox_get_by_name(dev, "notify", &info->chan_notify);
  2578. if (ret) {
  2579. dev_dbg(dev, "%s: Acquiring notify channel failed. ret = %d\n",
  2580. __func__, ret);
  2581. }
  2582. info->host_id = dev_read_u32_default(dev, "ti,host-id",
  2583. info->desc->default_host_id);
  2584. info->is_secure = dev_read_bool(dev, "ti,secure-host");
  2585. return 0;
  2586. }
  2587. /**
  2588. * ti_sci_probe() - Basic probe
  2589. * @dev: corresponding system controller interface device
  2590. *
  2591. * Return: 0 if all goes good, else appropriate error message.
  2592. */
  2593. static int ti_sci_probe(struct udevice *dev)
  2594. {
  2595. struct ti_sci_info *info;
  2596. int ret;
  2597. debug("%s(dev=%p)\n", __func__, dev);
  2598. info = dev_get_priv(dev);
  2599. info->desc = (void *)dev_get_driver_data(dev);
  2600. ret = ti_sci_of_to_info(dev, info);
  2601. if (ret) {
  2602. dev_err(dev, "%s: Probe failed with error %d\n", __func__, ret);
  2603. return ret;
  2604. }
  2605. info->dev = dev;
  2606. info->seq = 0xA;
  2607. list_add_tail(&info->list, &ti_sci_list);
  2608. ti_sci_setup_ops(info);
  2609. ret = ti_sci_cmd_get_revision(&info->handle);
  2610. INIT_LIST_HEAD(&info->dev_list);
  2611. return ret;
  2612. }
  2613. /*
  2614. * ti_sci_get_free_resource() - Get a free resource from TISCI resource.
  2615. * @res: Pointer to the TISCI resource
  2616. *
  2617. * Return: resource num if all went ok else TI_SCI_RESOURCE_NULL.
  2618. */
  2619. u16 ti_sci_get_free_resource(struct ti_sci_resource *res)
  2620. {
  2621. u16 set, free_bit;
  2622. for (set = 0; set < res->sets; set++) {
  2623. free_bit = find_first_zero_bit(res->desc[set].res_map,
  2624. res->desc[set].num);
  2625. if (free_bit != res->desc[set].num) {
  2626. set_bit(free_bit, res->desc[set].res_map);
  2627. return res->desc[set].start + free_bit;
  2628. }
  2629. }
  2630. return TI_SCI_RESOURCE_NULL;
  2631. }
  2632. /**
  2633. * ti_sci_release_resource() - Release a resource from TISCI resource.
  2634. * @res: Pointer to the TISCI resource
  2635. */
  2636. void ti_sci_release_resource(struct ti_sci_resource *res, u16 id)
  2637. {
  2638. u16 set;
  2639. for (set = 0; set < res->sets; set++) {
  2640. if (res->desc[set].start <= id &&
  2641. (res->desc[set].num + res->desc[set].start) > id)
  2642. clear_bit(id - res->desc[set].start,
  2643. res->desc[set].res_map);
  2644. }
  2645. }
  2646. /**
  2647. * devm_ti_sci_get_of_resource() - Get a TISCI resource assigned to a device
  2648. * @handle: TISCI handle
  2649. * @dev: Device pointer to which the resource is assigned
  2650. * @of_prop: property name by which the resource are represented
  2651. *
  2652. * Note: This function expects of_prop to be in the form of tuples
  2653. * <type, subtype>. Allocates and initializes ti_sci_resource structure
  2654. * for each of_prop. Client driver can directly call
  2655. * ti_sci_(get_free, release)_resource apis for handling the resource.
  2656. *
  2657. * Return: Pointer to ti_sci_resource if all went well else appropriate
  2658. * error pointer.
  2659. */
  2660. struct ti_sci_resource *
  2661. devm_ti_sci_get_of_resource(const struct ti_sci_handle *handle,
  2662. struct udevice *dev, u32 dev_id, char *of_prop)
  2663. {
  2664. u32 resource_subtype;
  2665. u16 resource_type;
  2666. struct ti_sci_resource *res;
  2667. bool valid_set = false;
  2668. int sets, i, ret;
  2669. u32 *temp;
  2670. res = devm_kzalloc(dev, sizeof(*res), GFP_KERNEL);
  2671. if (!res)
  2672. return ERR_PTR(-ENOMEM);
  2673. sets = dev_read_size(dev, of_prop);
  2674. if (sets < 0) {
  2675. dev_err(dev, "%s resource type ids not available\n", of_prop);
  2676. return ERR_PTR(sets);
  2677. }
  2678. temp = malloc(sets);
  2679. sets /= sizeof(u32);
  2680. res->sets = sets;
  2681. res->desc = devm_kcalloc(dev, res->sets, sizeof(*res->desc),
  2682. GFP_KERNEL);
  2683. if (!res->desc)
  2684. return ERR_PTR(-ENOMEM);
  2685. ret = ti_sci_get_resource_type(handle_to_ti_sci_info(handle), dev_id,
  2686. &resource_type);
  2687. if (ret) {
  2688. dev_err(dev, "No valid resource type for %u\n", dev_id);
  2689. return ERR_PTR(-EINVAL);
  2690. }
  2691. ret = dev_read_u32_array(dev, of_prop, temp, res->sets);
  2692. if (ret)
  2693. return ERR_PTR(-EINVAL);
  2694. for (i = 0; i < res->sets; i++) {
  2695. resource_subtype = temp[i];
  2696. ret = handle->ops.rm_core_ops.get_range(handle, dev_id,
  2697. resource_subtype,
  2698. &res->desc[i].start,
  2699. &res->desc[i].num);
  2700. if (ret) {
  2701. dev_dbg(dev, "type %d subtype %d not allocated for host %d\n",
  2702. resource_type, resource_subtype,
  2703. handle_to_ti_sci_info(handle)->host_id);
  2704. res->desc[i].start = 0;
  2705. res->desc[i].num = 0;
  2706. continue;
  2707. }
  2708. valid_set = true;
  2709. dev_dbg(dev, "res type = %d, subtype = %d, start = %d, num = %d\n",
  2710. resource_type, resource_subtype, res->desc[i].start,
  2711. res->desc[i].num);
  2712. res->desc[i].res_map =
  2713. devm_kzalloc(dev, BITS_TO_LONGS(res->desc[i].num) *
  2714. sizeof(*res->desc[i].res_map), GFP_KERNEL);
  2715. if (!res->desc[i].res_map)
  2716. return ERR_PTR(-ENOMEM);
  2717. }
  2718. if (valid_set)
  2719. return res;
  2720. return ERR_PTR(-EINVAL);
  2721. }
  2722. /* Description for K2G */
  2723. static const struct ti_sci_desc ti_sci_pmmc_k2g_desc = {
  2724. .default_host_id = 2,
  2725. /* Conservative duration */
  2726. .max_rx_timeout_ms = 10000,
  2727. /* Limited by MBOX_TX_QUEUE_LEN. K2G can handle upto 128 messages! */
  2728. .max_msgs = 20,
  2729. .max_msg_size = 64,
  2730. .rm_type_map = NULL,
  2731. };
  2732. static struct ti_sci_rm_type_map ti_sci_am654_rm_type_map[] = {
  2733. {.dev_id = 56, .type = 0x00b}, /* GIC_IRQ */
  2734. {.dev_id = 179, .type = 0x000}, /* MAIN_NAV_UDMASS_IA0 */
  2735. {.dev_id = 187, .type = 0x009}, /* MAIN_NAV_RA */
  2736. {.dev_id = 188, .type = 0x006}, /* MAIN_NAV_UDMAP */
  2737. {.dev_id = 194, .type = 0x007}, /* MCU_NAV_UDMAP */
  2738. {.dev_id = 195, .type = 0x00a}, /* MCU_NAV_RA */
  2739. {.dev_id = 0, .type = 0x000}, /* end of table */
  2740. };
  2741. /* Description for AM654 */
  2742. static const struct ti_sci_desc ti_sci_pmmc_am654_desc = {
  2743. .default_host_id = 12,
  2744. /* Conservative duration */
  2745. .max_rx_timeout_ms = 10000,
  2746. /* Limited by MBOX_TX_QUEUE_LEN. K2G can handle upto 128 messages! */
  2747. .max_msgs = 20,
  2748. .max_msg_size = 60,
  2749. .rm_type_map = ti_sci_am654_rm_type_map,
  2750. };
  2751. static const struct udevice_id ti_sci_ids[] = {
  2752. {
  2753. .compatible = "ti,k2g-sci",
  2754. .data = (ulong)&ti_sci_pmmc_k2g_desc
  2755. },
  2756. {
  2757. .compatible = "ti,am654-sci",
  2758. .data = (ulong)&ti_sci_pmmc_am654_desc
  2759. },
  2760. { /* Sentinel */ },
  2761. };
  2762. U_BOOT_DRIVER(ti_sci) = {
  2763. .name = "ti_sci",
  2764. .id = UCLASS_FIRMWARE,
  2765. .of_match = ti_sci_ids,
  2766. .probe = ti_sci_probe,
  2767. .priv_auto_alloc_size = sizeof(struct ti_sci_info),
  2768. };