driver.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * System Control and Management Interface (SCMI) Message Protocol driver
  4. *
  5. * SCMI Message Protocol is used between the System Control Processor(SCP)
  6. * and the Application Processors(AP). The Message Handling Unit(MHU)
  7. * provides a mechanism for inter-processor communication between SCP's
  8. * Cortex M3 and AP.
  9. *
  10. * SCP offers control and management of the core/cluster power states,
  11. * various power domain DVFS including the core/cluster, certain system
  12. * clocks configuration, thermal sensors and many others.
  13. *
  14. * Copyright (C) 2018 ARM Ltd.
  15. */
  16. #include <linux/bitmap.h>
  17. #include <linux/export.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.h>
  20. #include <linux/ktime.h>
  21. #include <linux/mailbox_client.h>
  22. #include <linux/module.h>
  23. #include <linux/of_address.h>
  24. #include <linux/of_device.h>
  25. #include <linux/processor.h>
  26. #include <linux/semaphore.h>
  27. #include <linux/slab.h>
  28. #include "common.h"
  29. #define MSG_ID_MASK GENMASK(7, 0)
  30. #define MSG_TYPE_MASK GENMASK(9, 8)
  31. #define MSG_PROTOCOL_ID_MASK GENMASK(17, 10)
  32. #define MSG_TOKEN_ID_MASK GENMASK(27, 18)
  33. #define MSG_XTRACT_TOKEN(hdr) FIELD_GET(MSG_TOKEN_ID_MASK, (hdr))
  34. #define MSG_TOKEN_MAX (MSG_XTRACT_TOKEN(MSG_TOKEN_ID_MASK) + 1)
  35. enum scmi_error_codes {
  36. SCMI_SUCCESS = 0, /* Success */
  37. SCMI_ERR_SUPPORT = -1, /* Not supported */
  38. SCMI_ERR_PARAMS = -2, /* Invalid Parameters */
  39. SCMI_ERR_ACCESS = -3, /* Invalid access/permission denied */
  40. SCMI_ERR_ENTRY = -4, /* Not found */
  41. SCMI_ERR_RANGE = -5, /* Value out of range */
  42. SCMI_ERR_BUSY = -6, /* Device busy */
  43. SCMI_ERR_COMMS = -7, /* Communication Error */
  44. SCMI_ERR_GENERIC = -8, /* Generic Error */
  45. SCMI_ERR_HARDWARE = -9, /* Hardware Error */
  46. SCMI_ERR_PROTOCOL = -10,/* Protocol Error */
  47. SCMI_ERR_MAX
  48. };
  49. /* List of all SCMI devices active in system */
  50. static LIST_HEAD(scmi_list);
  51. /* Protection for the entire list */
  52. static DEFINE_MUTEX(scmi_list_mutex);
  53. /**
  54. * struct scmi_xfers_info - Structure to manage transfer information
  55. *
  56. * @xfer_block: Preallocated Message array
  57. * @xfer_alloc_table: Bitmap table for allocated messages.
  58. * Index of this bitmap table is also used for message
  59. * sequence identifier.
  60. * @xfer_lock: Protection for message allocation
  61. */
  62. struct scmi_xfers_info {
  63. struct scmi_xfer *xfer_block;
  64. unsigned long *xfer_alloc_table;
  65. spinlock_t xfer_lock;
  66. };
  67. /**
  68. * struct scmi_desc - Description of SoC integration
  69. *
  70. * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds)
  71. * @max_msg: Maximum number of messages that can be pending
  72. * simultaneously in the system
  73. * @max_msg_size: Maximum size of data per message that can be handled.
  74. */
  75. struct scmi_desc {
  76. int max_rx_timeout_ms;
  77. int max_msg;
  78. int max_msg_size;
  79. };
  80. /**
  81. * struct scmi_chan_info - Structure representing a SCMI channel informfation
  82. *
  83. * @cl: Mailbox Client
  84. * @chan: Transmit/Receive mailbox channel
  85. * @payload: Transmit/Receive mailbox channel payload area
  86. * @dev: Reference to device in the SCMI hierarchy corresponding to this
  87. * channel
  88. * @handle: Pointer to SCMI entity handle
  89. */
  90. struct scmi_chan_info {
  91. struct mbox_client cl;
  92. struct mbox_chan *chan;
  93. void __iomem *payload;
  94. struct device *dev;
  95. struct scmi_handle *handle;
  96. };
  97. /**
  98. * struct scmi_info - Structure representing a SCMI instance
  99. *
  100. * @dev: Device pointer
  101. * @desc: SoC description for this instance
  102. * @handle: Instance of SCMI handle to send to clients
  103. * @version: SCMI revision information containing protocol version,
  104. * implementation version and (sub-)vendor identification.
  105. * @minfo: Message info
  106. * @tx_idr: IDR object to map protocol id to channel info pointer
  107. * @protocols_imp: List of protocols implemented, currently maximum of
  108. * MAX_PROTOCOLS_IMP elements allocated by the base protocol
  109. * @node: List head
  110. * @users: Number of users of this instance
  111. */
  112. struct scmi_info {
  113. struct device *dev;
  114. const struct scmi_desc *desc;
  115. struct scmi_revision_info version;
  116. struct scmi_handle handle;
  117. struct scmi_xfers_info minfo;
  118. struct idr tx_idr;
  119. u8 *protocols_imp;
  120. struct list_head node;
  121. int users;
  122. };
  123. #define client_to_scmi_chan_info(c) container_of(c, struct scmi_chan_info, cl)
  124. #define handle_to_scmi_info(h) container_of(h, struct scmi_info, handle)
  125. /*
  126. * SCMI specification requires all parameters, message headers, return
  127. * arguments or any protocol data to be expressed in little endian
  128. * format only.
  129. */
  130. struct scmi_shared_mem {
  131. __le32 reserved;
  132. __le32 channel_status;
  133. #define SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR BIT(1)
  134. #define SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE BIT(0)
  135. __le32 reserved1[2];
  136. __le32 flags;
  137. #define SCMI_SHMEM_FLAG_INTR_ENABLED BIT(0)
  138. __le32 length;
  139. __le32 msg_header;
  140. u8 msg_payload[0];
  141. };
  142. static const int scmi_linux_errmap[] = {
  143. /* better than switch case as long as return value is continuous */
  144. 0, /* SCMI_SUCCESS */
  145. -EOPNOTSUPP, /* SCMI_ERR_SUPPORT */
  146. -EINVAL, /* SCMI_ERR_PARAM */
  147. -EACCES, /* SCMI_ERR_ACCESS */
  148. -ENOENT, /* SCMI_ERR_ENTRY */
  149. -ERANGE, /* SCMI_ERR_RANGE */
  150. -EBUSY, /* SCMI_ERR_BUSY */
  151. -ECOMM, /* SCMI_ERR_COMMS */
  152. -EIO, /* SCMI_ERR_GENERIC */
  153. -EREMOTEIO, /* SCMI_ERR_HARDWARE */
  154. -EPROTO, /* SCMI_ERR_PROTOCOL */
  155. };
  156. static inline int scmi_to_linux_errno(int errno)
  157. {
  158. if (errno < SCMI_SUCCESS && errno > SCMI_ERR_MAX)
  159. return scmi_linux_errmap[-errno];
  160. return -EIO;
  161. }
  162. /**
  163. * scmi_dump_header_dbg() - Helper to dump a message header.
  164. *
  165. * @dev: Device pointer corresponding to the SCMI entity
  166. * @hdr: pointer to header.
  167. */
  168. static inline void scmi_dump_header_dbg(struct device *dev,
  169. struct scmi_msg_hdr *hdr)
  170. {
  171. dev_dbg(dev, "Command ID: %x Sequence ID: %x Protocol: %x\n",
  172. hdr->id, hdr->seq, hdr->protocol_id);
  173. }
  174. static void scmi_fetch_response(struct scmi_xfer *xfer,
  175. struct scmi_shared_mem __iomem *mem)
  176. {
  177. xfer->hdr.status = ioread32(mem->msg_payload);
  178. /* Skip the length of header and statues in payload area i.e 8 bytes*/
  179. xfer->rx.len = min_t(size_t, xfer->rx.len, ioread32(&mem->length) - 8);
  180. /* Take a copy to the rx buffer.. */
  181. memcpy_fromio(xfer->rx.buf, mem->msg_payload + 4, xfer->rx.len);
  182. }
  183. /**
  184. * scmi_rx_callback() - mailbox client callback for receive messages
  185. *
  186. * @cl: client pointer
  187. * @m: mailbox message
  188. *
  189. * Processes one received message to appropriate transfer information and
  190. * signals completion of the transfer.
  191. *
  192. * NOTE: This function will be invoked in IRQ context, hence should be
  193. * as optimal as possible.
  194. */
  195. static void scmi_rx_callback(struct mbox_client *cl, void *m)
  196. {
  197. u16 xfer_id;
  198. struct scmi_xfer *xfer;
  199. struct scmi_chan_info *cinfo = client_to_scmi_chan_info(cl);
  200. struct device *dev = cinfo->dev;
  201. struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
  202. struct scmi_xfers_info *minfo = &info->minfo;
  203. struct scmi_shared_mem __iomem *mem = cinfo->payload;
  204. xfer_id = MSG_XTRACT_TOKEN(ioread32(&mem->msg_header));
  205. /* Are we even expecting this? */
  206. if (!test_bit(xfer_id, minfo->xfer_alloc_table)) {
  207. dev_err(dev, "message for %d is not expected!\n", xfer_id);
  208. return;
  209. }
  210. xfer = &minfo->xfer_block[xfer_id];
  211. scmi_dump_header_dbg(dev, &xfer->hdr);
  212. /* Is the message of valid length? */
  213. if (xfer->rx.len > info->desc->max_msg_size) {
  214. dev_err(dev, "unable to handle %zu xfer(max %d)\n",
  215. xfer->rx.len, info->desc->max_msg_size);
  216. return;
  217. }
  218. scmi_fetch_response(xfer, mem);
  219. complete(&xfer->done);
  220. }
  221. /**
  222. * pack_scmi_header() - packs and returns 32-bit header
  223. *
  224. * @hdr: pointer to header containing all the information on message id,
  225. * protocol id and sequence id.
  226. *
  227. * Return: 32-bit packed command header to be sent to the platform.
  228. */
  229. static inline u32 pack_scmi_header(struct scmi_msg_hdr *hdr)
  230. {
  231. return FIELD_PREP(MSG_ID_MASK, hdr->id) |
  232. FIELD_PREP(MSG_TOKEN_ID_MASK, hdr->seq) |
  233. FIELD_PREP(MSG_PROTOCOL_ID_MASK, hdr->protocol_id);
  234. }
  235. /**
  236. * scmi_tx_prepare() - mailbox client callback to prepare for the transfer
  237. *
  238. * @cl: client pointer
  239. * @m: mailbox message
  240. *
  241. * This function prepares the shared memory which contains the header and the
  242. * payload.
  243. */
  244. static void scmi_tx_prepare(struct mbox_client *cl, void *m)
  245. {
  246. struct scmi_xfer *t = m;
  247. struct scmi_chan_info *cinfo = client_to_scmi_chan_info(cl);
  248. struct scmi_shared_mem __iomem *mem = cinfo->payload;
  249. /* Mark channel busy + clear error */
  250. iowrite32(0x0, &mem->channel_status);
  251. iowrite32(t->hdr.poll_completion ? 0 : SCMI_SHMEM_FLAG_INTR_ENABLED,
  252. &mem->flags);
  253. iowrite32(sizeof(mem->msg_header) + t->tx.len, &mem->length);
  254. iowrite32(pack_scmi_header(&t->hdr), &mem->msg_header);
  255. if (t->tx.buf)
  256. memcpy_toio(mem->msg_payload, t->tx.buf, t->tx.len);
  257. }
  258. /**
  259. * scmi_xfer_get() - Allocate one message
  260. *
  261. * @handle: Pointer to SCMI entity handle
  262. *
  263. * Helper function which is used by various command functions that are
  264. * exposed to clients of this driver for allocating a message traffic event.
  265. *
  266. * This function can sleep depending on pending requests already in the system
  267. * for the SCMI entity. Further, this also holds a spinlock to maintain
  268. * integrity of internal data structures.
  269. *
  270. * Return: 0 if all went fine, else corresponding error.
  271. */
  272. static struct scmi_xfer *scmi_xfer_get(const struct scmi_handle *handle)
  273. {
  274. u16 xfer_id;
  275. struct scmi_xfer *xfer;
  276. unsigned long flags, bit_pos;
  277. struct scmi_info *info = handle_to_scmi_info(handle);
  278. struct scmi_xfers_info *minfo = &info->minfo;
  279. /* Keep the locked section as small as possible */
  280. spin_lock_irqsave(&minfo->xfer_lock, flags);
  281. bit_pos = find_first_zero_bit(minfo->xfer_alloc_table,
  282. info->desc->max_msg);
  283. if (bit_pos == info->desc->max_msg) {
  284. spin_unlock_irqrestore(&minfo->xfer_lock, flags);
  285. return ERR_PTR(-ENOMEM);
  286. }
  287. set_bit(bit_pos, minfo->xfer_alloc_table);
  288. spin_unlock_irqrestore(&minfo->xfer_lock, flags);
  289. xfer_id = bit_pos;
  290. xfer = &minfo->xfer_block[xfer_id];
  291. xfer->hdr.seq = xfer_id;
  292. reinit_completion(&xfer->done);
  293. return xfer;
  294. }
  295. /**
  296. * scmi_xfer_put() - Release a message
  297. *
  298. * @handle: Pointer to SCMI entity handle
  299. * @xfer: message that was reserved by scmi_xfer_get
  300. *
  301. * This holds a spinlock to maintain integrity of internal data structures.
  302. */
  303. void scmi_xfer_put(const struct scmi_handle *handle, struct scmi_xfer *xfer)
  304. {
  305. unsigned long flags;
  306. struct scmi_info *info = handle_to_scmi_info(handle);
  307. struct scmi_xfers_info *minfo = &info->minfo;
  308. /*
  309. * Keep the locked section as small as possible
  310. * NOTE: we might escape with smp_mb and no lock here..
  311. * but just be conservative and symmetric.
  312. */
  313. spin_lock_irqsave(&minfo->xfer_lock, flags);
  314. clear_bit(xfer->hdr.seq, minfo->xfer_alloc_table);
  315. spin_unlock_irqrestore(&minfo->xfer_lock, flags);
  316. }
  317. static bool
  318. scmi_xfer_poll_done(const struct scmi_chan_info *cinfo, struct scmi_xfer *xfer)
  319. {
  320. struct scmi_shared_mem __iomem *mem = cinfo->payload;
  321. u16 xfer_id = MSG_XTRACT_TOKEN(ioread32(&mem->msg_header));
  322. if (xfer->hdr.seq != xfer_id)
  323. return false;
  324. return ioread32(&mem->channel_status) &
  325. (SCMI_SHMEM_CHAN_STAT_CHANNEL_ERROR |
  326. SCMI_SHMEM_CHAN_STAT_CHANNEL_FREE);
  327. }
  328. #define SCMI_MAX_POLL_TO_NS (100 * NSEC_PER_USEC)
  329. static bool scmi_xfer_done_no_timeout(const struct scmi_chan_info *cinfo,
  330. struct scmi_xfer *xfer, ktime_t stop)
  331. {
  332. ktime_t __cur = ktime_get();
  333. return scmi_xfer_poll_done(cinfo, xfer) || ktime_after(__cur, stop);
  334. }
  335. /**
  336. * scmi_do_xfer() - Do one transfer
  337. *
  338. * @handle: Pointer to SCMI entity handle
  339. * @xfer: Transfer to initiate and wait for response
  340. *
  341. * Return: -ETIMEDOUT in case of no response, if transmit error,
  342. * return corresponding error, else if all goes well,
  343. * return 0.
  344. */
  345. int scmi_do_xfer(const struct scmi_handle *handle, struct scmi_xfer *xfer)
  346. {
  347. int ret;
  348. int timeout;
  349. struct scmi_info *info = handle_to_scmi_info(handle);
  350. struct device *dev = info->dev;
  351. struct scmi_chan_info *cinfo;
  352. cinfo = idr_find(&info->tx_idr, xfer->hdr.protocol_id);
  353. if (unlikely(!cinfo))
  354. return -EINVAL;
  355. ret = mbox_send_message(cinfo->chan, xfer);
  356. if (ret < 0) {
  357. dev_dbg(dev, "mbox send fail %d\n", ret);
  358. return ret;
  359. }
  360. /* mbox_send_message returns non-negative value on success, so reset */
  361. ret = 0;
  362. if (xfer->hdr.poll_completion) {
  363. ktime_t stop = ktime_add_ns(ktime_get(), SCMI_MAX_POLL_TO_NS);
  364. spin_until_cond(scmi_xfer_done_no_timeout(cinfo, xfer, stop));
  365. if (ktime_before(ktime_get(), stop))
  366. scmi_fetch_response(xfer, cinfo->payload);
  367. else
  368. ret = -ETIMEDOUT;
  369. } else {
  370. /* And we wait for the response. */
  371. timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms);
  372. if (!wait_for_completion_timeout(&xfer->done, timeout)) {
  373. dev_err(dev, "mbox timed out in resp(caller: %pS)\n",
  374. (void *)_RET_IP_);
  375. ret = -ETIMEDOUT;
  376. }
  377. }
  378. if (!ret && xfer->hdr.status)
  379. ret = scmi_to_linux_errno(xfer->hdr.status);
  380. /*
  381. * NOTE: we might prefer not to need the mailbox ticker to manage the
  382. * transfer queueing since the protocol layer queues things by itself.
  383. * Unfortunately, we have to kick the mailbox framework after we have
  384. * received our message.
  385. */
  386. mbox_client_txdone(cinfo->chan, ret);
  387. return ret;
  388. }
  389. /**
  390. * scmi_xfer_get_init() - Allocate and initialise one message
  391. *
  392. * @handle: Pointer to SCMI entity handle
  393. * @msg_id: Message identifier
  394. * @prot_id: Protocol identifier for the message
  395. * @tx_size: transmit message size
  396. * @rx_size: receive message size
  397. * @p: pointer to the allocated and initialised message
  398. *
  399. * This function allocates the message using @scmi_xfer_get and
  400. * initialise the header.
  401. *
  402. * Return: 0 if all went fine with @p pointing to message, else
  403. * corresponding error.
  404. */
  405. int scmi_xfer_get_init(const struct scmi_handle *handle, u8 msg_id, u8 prot_id,
  406. size_t tx_size, size_t rx_size, struct scmi_xfer **p)
  407. {
  408. int ret;
  409. struct scmi_xfer *xfer;
  410. struct scmi_info *info = handle_to_scmi_info(handle);
  411. struct device *dev = info->dev;
  412. /* Ensure we have sane transfer sizes */
  413. if (rx_size > info->desc->max_msg_size ||
  414. tx_size > info->desc->max_msg_size)
  415. return -ERANGE;
  416. xfer = scmi_xfer_get(handle);
  417. if (IS_ERR(xfer)) {
  418. ret = PTR_ERR(xfer);
  419. dev_err(dev, "failed to get free message slot(%d)\n", ret);
  420. return ret;
  421. }
  422. xfer->tx.len = tx_size;
  423. xfer->rx.len = rx_size ? : info->desc->max_msg_size;
  424. xfer->hdr.id = msg_id;
  425. xfer->hdr.protocol_id = prot_id;
  426. xfer->hdr.poll_completion = false;
  427. *p = xfer;
  428. return 0;
  429. }
  430. /**
  431. * scmi_version_get() - command to get the revision of the SCMI entity
  432. *
  433. * @handle: Pointer to SCMI entity handle
  434. * @protocol: Protocol identifier for the message
  435. * @version: Holds returned version of protocol.
  436. *
  437. * Updates the SCMI information in the internal data structure.
  438. *
  439. * Return: 0 if all went fine, else return appropriate error.
  440. */
  441. int scmi_version_get(const struct scmi_handle *handle, u8 protocol,
  442. u32 *version)
  443. {
  444. int ret;
  445. __le32 *rev_info;
  446. struct scmi_xfer *t;
  447. ret = scmi_xfer_get_init(handle, PROTOCOL_VERSION, protocol, 0,
  448. sizeof(*version), &t);
  449. if (ret)
  450. return ret;
  451. ret = scmi_do_xfer(handle, t);
  452. if (!ret) {
  453. rev_info = t->rx.buf;
  454. *version = le32_to_cpu(*rev_info);
  455. }
  456. scmi_xfer_put(handle, t);
  457. return ret;
  458. }
  459. void scmi_setup_protocol_implemented(const struct scmi_handle *handle,
  460. u8 *prot_imp)
  461. {
  462. struct scmi_info *info = handle_to_scmi_info(handle);
  463. info->protocols_imp = prot_imp;
  464. }
  465. static bool
  466. scmi_is_protocol_implemented(const struct scmi_handle *handle, u8 prot_id)
  467. {
  468. int i;
  469. struct scmi_info *info = handle_to_scmi_info(handle);
  470. if (!info->protocols_imp)
  471. return false;
  472. for (i = 0; i < MAX_PROTOCOLS_IMP; i++)
  473. if (info->protocols_imp[i] == prot_id)
  474. return true;
  475. return false;
  476. }
  477. /**
  478. * scmi_handle_get() - Get the SCMI handle for a device
  479. *
  480. * @dev: pointer to device for which we want SCMI handle
  481. *
  482. * NOTE: The function does not track individual clients of the framework
  483. * and is expected to be maintained by caller of SCMI protocol library.
  484. * scmi_handle_put must be balanced with successful scmi_handle_get
  485. *
  486. * Return: pointer to handle if successful, NULL on error
  487. */
  488. struct scmi_handle *scmi_handle_get(struct device *dev)
  489. {
  490. struct list_head *p;
  491. struct scmi_info *info;
  492. struct scmi_handle *handle = NULL;
  493. mutex_lock(&scmi_list_mutex);
  494. list_for_each(p, &scmi_list) {
  495. info = list_entry(p, struct scmi_info, node);
  496. if (dev->parent == info->dev) {
  497. handle = &info->handle;
  498. info->users++;
  499. break;
  500. }
  501. }
  502. mutex_unlock(&scmi_list_mutex);
  503. return handle;
  504. }
  505. /**
  506. * scmi_handle_put() - Release the handle acquired by scmi_handle_get
  507. *
  508. * @handle: handle acquired by scmi_handle_get
  509. *
  510. * NOTE: The function does not track individual clients of the framework
  511. * and is expected to be maintained by caller of SCMI protocol library.
  512. * scmi_handle_put must be balanced with successful scmi_handle_get
  513. *
  514. * Return: 0 is successfully released
  515. * if null was passed, it returns -EINVAL;
  516. */
  517. int scmi_handle_put(const struct scmi_handle *handle)
  518. {
  519. struct scmi_info *info;
  520. if (!handle)
  521. return -EINVAL;
  522. info = handle_to_scmi_info(handle);
  523. mutex_lock(&scmi_list_mutex);
  524. if (!WARN_ON(!info->users))
  525. info->users--;
  526. mutex_unlock(&scmi_list_mutex);
  527. return 0;
  528. }
  529. static const struct scmi_desc scmi_generic_desc = {
  530. .max_rx_timeout_ms = 30, /* We may increase this if required */
  531. .max_msg = 20, /* Limited by MBOX_TX_QUEUE_LEN */
  532. .max_msg_size = 128,
  533. };
  534. /* Each compatible listed below must have descriptor associated with it */
  535. static const struct of_device_id scmi_of_match[] = {
  536. { .compatible = "arm,scmi", .data = &scmi_generic_desc },
  537. { /* Sentinel */ },
  538. };
  539. MODULE_DEVICE_TABLE(of, scmi_of_match);
  540. static int scmi_xfer_info_init(struct scmi_info *sinfo)
  541. {
  542. int i;
  543. struct scmi_xfer *xfer;
  544. struct device *dev = sinfo->dev;
  545. const struct scmi_desc *desc = sinfo->desc;
  546. struct scmi_xfers_info *info = &sinfo->minfo;
  547. /* Pre-allocated messages, no more than what hdr.seq can support */
  548. if (WARN_ON(desc->max_msg >= MSG_TOKEN_MAX)) {
  549. dev_err(dev, "Maximum message of %d exceeds supported %ld\n",
  550. desc->max_msg, MSG_TOKEN_MAX);
  551. return -EINVAL;
  552. }
  553. info->xfer_block = devm_kcalloc(dev, desc->max_msg,
  554. sizeof(*info->xfer_block), GFP_KERNEL);
  555. if (!info->xfer_block)
  556. return -ENOMEM;
  557. info->xfer_alloc_table = devm_kcalloc(dev, BITS_TO_LONGS(desc->max_msg),
  558. sizeof(long), GFP_KERNEL);
  559. if (!info->xfer_alloc_table)
  560. return -ENOMEM;
  561. /* Pre-initialize the buffer pointer to pre-allocated buffers */
  562. for (i = 0, xfer = info->xfer_block; i < desc->max_msg; i++, xfer++) {
  563. xfer->rx.buf = devm_kcalloc(dev, sizeof(u8), desc->max_msg_size,
  564. GFP_KERNEL);
  565. if (!xfer->rx.buf)
  566. return -ENOMEM;
  567. xfer->tx.buf = xfer->rx.buf;
  568. init_completion(&xfer->done);
  569. }
  570. spin_lock_init(&info->xfer_lock);
  571. return 0;
  572. }
  573. static int scmi_mailbox_check(struct device_node *np)
  574. {
  575. struct of_phandle_args arg;
  576. return of_parse_phandle_with_args(np, "mboxes", "#mbox-cells", 0, &arg);
  577. }
  578. static int scmi_mbox_free_channel(int id, void *p, void *data)
  579. {
  580. struct scmi_chan_info *cinfo = p;
  581. struct idr *idr = data;
  582. if (!IS_ERR_OR_NULL(cinfo->chan)) {
  583. mbox_free_channel(cinfo->chan);
  584. cinfo->chan = NULL;
  585. }
  586. idr_remove(idr, id);
  587. return 0;
  588. }
  589. static int scmi_remove(struct platform_device *pdev)
  590. {
  591. int ret = 0;
  592. struct scmi_info *info = platform_get_drvdata(pdev);
  593. struct idr *idr = &info->tx_idr;
  594. mutex_lock(&scmi_list_mutex);
  595. if (info->users)
  596. ret = -EBUSY;
  597. else
  598. list_del(&info->node);
  599. mutex_unlock(&scmi_list_mutex);
  600. if (ret)
  601. return ret;
  602. /* Safe to free channels since no more users */
  603. ret = idr_for_each(idr, scmi_mbox_free_channel, idr);
  604. idr_destroy(&info->tx_idr);
  605. return ret;
  606. }
  607. static inline int
  608. scmi_mbox_chan_setup(struct scmi_info *info, struct device *dev, int prot_id)
  609. {
  610. int ret;
  611. struct resource res;
  612. resource_size_t size;
  613. struct device_node *shmem, *np = dev->of_node;
  614. struct scmi_chan_info *cinfo;
  615. struct mbox_client *cl;
  616. if (scmi_mailbox_check(np)) {
  617. cinfo = idr_find(&info->tx_idr, SCMI_PROTOCOL_BASE);
  618. goto idr_alloc;
  619. }
  620. cinfo = devm_kzalloc(info->dev, sizeof(*cinfo), GFP_KERNEL);
  621. if (!cinfo)
  622. return -ENOMEM;
  623. cinfo->dev = dev;
  624. cl = &cinfo->cl;
  625. cl->dev = dev;
  626. cl->rx_callback = scmi_rx_callback;
  627. cl->tx_prepare = scmi_tx_prepare;
  628. cl->tx_block = false;
  629. cl->knows_txdone = true;
  630. shmem = of_parse_phandle(np, "shmem", 0);
  631. ret = of_address_to_resource(shmem, 0, &res);
  632. of_node_put(shmem);
  633. if (ret) {
  634. dev_err(dev, "failed to get SCMI Tx payload mem resource\n");
  635. return ret;
  636. }
  637. size = resource_size(&res);
  638. cinfo->payload = devm_ioremap(info->dev, res.start, size);
  639. if (!cinfo->payload) {
  640. dev_err(dev, "failed to ioremap SCMI Tx payload\n");
  641. return -EADDRNOTAVAIL;
  642. }
  643. /* Transmit channel is first entry i.e. index 0 */
  644. cinfo->chan = mbox_request_channel(cl, 0);
  645. if (IS_ERR(cinfo->chan)) {
  646. ret = PTR_ERR(cinfo->chan);
  647. if (ret != -EPROBE_DEFER)
  648. dev_err(dev, "failed to request SCMI Tx mailbox\n");
  649. return ret;
  650. }
  651. idr_alloc:
  652. ret = idr_alloc(&info->tx_idr, cinfo, prot_id, prot_id + 1, GFP_KERNEL);
  653. if (ret != prot_id) {
  654. dev_err(dev, "unable to allocate SCMI idr slot err %d\n", ret);
  655. return ret;
  656. }
  657. cinfo->handle = &info->handle;
  658. return 0;
  659. }
  660. static inline void
  661. scmi_create_protocol_device(struct device_node *np, struct scmi_info *info,
  662. int prot_id)
  663. {
  664. struct scmi_device *sdev;
  665. sdev = scmi_device_create(np, info->dev, prot_id);
  666. if (!sdev) {
  667. dev_err(info->dev, "failed to create %d protocol device\n",
  668. prot_id);
  669. return;
  670. }
  671. if (scmi_mbox_chan_setup(info, &sdev->dev, prot_id)) {
  672. dev_err(&sdev->dev, "failed to setup transport\n");
  673. scmi_device_destroy(sdev);
  674. return;
  675. }
  676. /* setup handle now as the transport is ready */
  677. scmi_set_handle(sdev);
  678. }
  679. static int scmi_probe(struct platform_device *pdev)
  680. {
  681. int ret;
  682. struct scmi_handle *handle;
  683. const struct scmi_desc *desc;
  684. struct scmi_info *info;
  685. struct device *dev = &pdev->dev;
  686. struct device_node *child, *np = dev->of_node;
  687. /* Only mailbox method supported, check for the presence of one */
  688. if (scmi_mailbox_check(np)) {
  689. dev_err(dev, "no mailbox found in %pOF\n", np);
  690. return -EINVAL;
  691. }
  692. desc = of_match_device(scmi_of_match, dev)->data;
  693. info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  694. if (!info)
  695. return -ENOMEM;
  696. info->dev = dev;
  697. info->desc = desc;
  698. INIT_LIST_HEAD(&info->node);
  699. ret = scmi_xfer_info_init(info);
  700. if (ret)
  701. return ret;
  702. platform_set_drvdata(pdev, info);
  703. idr_init(&info->tx_idr);
  704. handle = &info->handle;
  705. handle->dev = info->dev;
  706. handle->version = &info->version;
  707. ret = scmi_mbox_chan_setup(info, dev, SCMI_PROTOCOL_BASE);
  708. if (ret)
  709. return ret;
  710. ret = scmi_base_protocol_init(handle);
  711. if (ret) {
  712. dev_err(dev, "unable to communicate with SCMI(%d)\n", ret);
  713. return ret;
  714. }
  715. mutex_lock(&scmi_list_mutex);
  716. list_add_tail(&info->node, &scmi_list);
  717. mutex_unlock(&scmi_list_mutex);
  718. for_each_available_child_of_node(np, child) {
  719. u32 prot_id;
  720. if (of_property_read_u32(child, "reg", &prot_id))
  721. continue;
  722. if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK, prot_id))
  723. dev_err(dev, "Out of range protocol %d\n", prot_id);
  724. if (!scmi_is_protocol_implemented(handle, prot_id)) {
  725. dev_err(dev, "SCMI protocol %d not implemented\n",
  726. prot_id);
  727. continue;
  728. }
  729. scmi_create_protocol_device(child, info, prot_id);
  730. }
  731. return 0;
  732. }
  733. static struct platform_driver scmi_driver = {
  734. .driver = {
  735. .name = "arm-scmi",
  736. .of_match_table = scmi_of_match,
  737. },
  738. .probe = scmi_probe,
  739. .remove = scmi_remove,
  740. };
  741. module_platform_driver(scmi_driver);
  742. MODULE_ALIAS("platform: arm-scmi");
  743. MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
  744. MODULE_DESCRIPTION("ARM SCMI protocol driver");
  745. MODULE_LICENSE("GPL v2");