arm_scpi.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * System Control and Power Interface (SCPI) Message Protocol driver
  4. *
  5. * SCPI 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) 2015 ARM Ltd.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/bitmap.h>
  18. #include <linux/bitfield.h>
  19. #include <linux/device.h>
  20. #include <linux/err.h>
  21. #include <linux/export.h>
  22. #include <linux/io.h>
  23. #include <linux/kernel.h>
  24. #include <linux/list.h>
  25. #include <linux/mailbox_client.h>
  26. #include <linux/module.h>
  27. #include <linux/of_address.h>
  28. #include <linux/of_platform.h>
  29. #include <linux/printk.h>
  30. #include <linux/pm_opp.h>
  31. #include <linux/scpi_protocol.h>
  32. #include <linux/slab.h>
  33. #include <linux/sort.h>
  34. #include <linux/spinlock.h>
  35. #define CMD_ID_MASK GENMASK(6, 0)
  36. #define CMD_TOKEN_ID_MASK GENMASK(15, 8)
  37. #define CMD_DATA_SIZE_MASK GENMASK(24, 16)
  38. #define CMD_LEGACY_DATA_SIZE_MASK GENMASK(28, 20)
  39. #define PACK_SCPI_CMD(cmd_id, tx_sz) \
  40. (FIELD_PREP(CMD_ID_MASK, cmd_id) | \
  41. FIELD_PREP(CMD_DATA_SIZE_MASK, tx_sz))
  42. #define PACK_LEGACY_SCPI_CMD(cmd_id, tx_sz) \
  43. (FIELD_PREP(CMD_ID_MASK, cmd_id) | \
  44. FIELD_PREP(CMD_LEGACY_DATA_SIZE_MASK, tx_sz))
  45. #define CMD_SIZE(cmd) FIELD_GET(CMD_DATA_SIZE_MASK, cmd)
  46. #define CMD_UNIQ_MASK (CMD_TOKEN_ID_MASK | CMD_ID_MASK)
  47. #define CMD_XTRACT_UNIQ(cmd) ((cmd) & CMD_UNIQ_MASK)
  48. #define SCPI_SLOT 0
  49. #define MAX_DVFS_DOMAINS 8
  50. #define MAX_DVFS_OPPS 16
  51. #define PROTO_REV_MAJOR_MASK GENMASK(31, 16)
  52. #define PROTO_REV_MINOR_MASK GENMASK(15, 0)
  53. #define FW_REV_MAJOR_MASK GENMASK(31, 24)
  54. #define FW_REV_MINOR_MASK GENMASK(23, 16)
  55. #define FW_REV_PATCH_MASK GENMASK(15, 0)
  56. #define MAX_RX_TIMEOUT (msecs_to_jiffies(30))
  57. enum scpi_error_codes {
  58. SCPI_SUCCESS = 0, /* Success */
  59. SCPI_ERR_PARAM = 1, /* Invalid parameter(s) */
  60. SCPI_ERR_ALIGN = 2, /* Invalid alignment */
  61. SCPI_ERR_SIZE = 3, /* Invalid size */
  62. SCPI_ERR_HANDLER = 4, /* Invalid handler/callback */
  63. SCPI_ERR_ACCESS = 5, /* Invalid access/permission denied */
  64. SCPI_ERR_RANGE = 6, /* Value out of range */
  65. SCPI_ERR_TIMEOUT = 7, /* Timeout has occurred */
  66. SCPI_ERR_NOMEM = 8, /* Invalid memory area or pointer */
  67. SCPI_ERR_PWRSTATE = 9, /* Invalid power state */
  68. SCPI_ERR_SUPPORT = 10, /* Not supported or disabled */
  69. SCPI_ERR_DEVICE = 11, /* Device error */
  70. SCPI_ERR_BUSY = 12, /* Device busy */
  71. SCPI_ERR_MAX
  72. };
  73. /* SCPI Standard commands */
  74. enum scpi_std_cmd {
  75. SCPI_CMD_INVALID = 0x00,
  76. SCPI_CMD_SCPI_READY = 0x01,
  77. SCPI_CMD_SCPI_CAPABILITIES = 0x02,
  78. SCPI_CMD_SET_CSS_PWR_STATE = 0x03,
  79. SCPI_CMD_GET_CSS_PWR_STATE = 0x04,
  80. SCPI_CMD_SET_SYS_PWR_STATE = 0x05,
  81. SCPI_CMD_SET_CPU_TIMER = 0x06,
  82. SCPI_CMD_CANCEL_CPU_TIMER = 0x07,
  83. SCPI_CMD_DVFS_CAPABILITIES = 0x08,
  84. SCPI_CMD_GET_DVFS_INFO = 0x09,
  85. SCPI_CMD_SET_DVFS = 0x0a,
  86. SCPI_CMD_GET_DVFS = 0x0b,
  87. SCPI_CMD_GET_DVFS_STAT = 0x0c,
  88. SCPI_CMD_CLOCK_CAPABILITIES = 0x0d,
  89. SCPI_CMD_GET_CLOCK_INFO = 0x0e,
  90. SCPI_CMD_SET_CLOCK_VALUE = 0x0f,
  91. SCPI_CMD_GET_CLOCK_VALUE = 0x10,
  92. SCPI_CMD_PSU_CAPABILITIES = 0x11,
  93. SCPI_CMD_GET_PSU_INFO = 0x12,
  94. SCPI_CMD_SET_PSU = 0x13,
  95. SCPI_CMD_GET_PSU = 0x14,
  96. SCPI_CMD_SENSOR_CAPABILITIES = 0x15,
  97. SCPI_CMD_SENSOR_INFO = 0x16,
  98. SCPI_CMD_SENSOR_VALUE = 0x17,
  99. SCPI_CMD_SENSOR_CFG_PERIODIC = 0x18,
  100. SCPI_CMD_SENSOR_CFG_BOUNDS = 0x19,
  101. SCPI_CMD_SENSOR_ASYNC_VALUE = 0x1a,
  102. SCPI_CMD_SET_DEVICE_PWR_STATE = 0x1b,
  103. SCPI_CMD_GET_DEVICE_PWR_STATE = 0x1c,
  104. SCPI_CMD_COUNT
  105. };
  106. /* SCPI Legacy Commands */
  107. enum legacy_scpi_std_cmd {
  108. LEGACY_SCPI_CMD_INVALID = 0x00,
  109. LEGACY_SCPI_CMD_SCPI_READY = 0x01,
  110. LEGACY_SCPI_CMD_SCPI_CAPABILITIES = 0x02,
  111. LEGACY_SCPI_CMD_EVENT = 0x03,
  112. LEGACY_SCPI_CMD_SET_CSS_PWR_STATE = 0x04,
  113. LEGACY_SCPI_CMD_GET_CSS_PWR_STATE = 0x05,
  114. LEGACY_SCPI_CMD_CFG_PWR_STATE_STAT = 0x06,
  115. LEGACY_SCPI_CMD_GET_PWR_STATE_STAT = 0x07,
  116. LEGACY_SCPI_CMD_SYS_PWR_STATE = 0x08,
  117. LEGACY_SCPI_CMD_L2_READY = 0x09,
  118. LEGACY_SCPI_CMD_SET_AP_TIMER = 0x0a,
  119. LEGACY_SCPI_CMD_CANCEL_AP_TIME = 0x0b,
  120. LEGACY_SCPI_CMD_DVFS_CAPABILITIES = 0x0c,
  121. LEGACY_SCPI_CMD_GET_DVFS_INFO = 0x0d,
  122. LEGACY_SCPI_CMD_SET_DVFS = 0x0e,
  123. LEGACY_SCPI_CMD_GET_DVFS = 0x0f,
  124. LEGACY_SCPI_CMD_GET_DVFS_STAT = 0x10,
  125. LEGACY_SCPI_CMD_SET_RTC = 0x11,
  126. LEGACY_SCPI_CMD_GET_RTC = 0x12,
  127. LEGACY_SCPI_CMD_CLOCK_CAPABILITIES = 0x13,
  128. LEGACY_SCPI_CMD_SET_CLOCK_INDEX = 0x14,
  129. LEGACY_SCPI_CMD_SET_CLOCK_VALUE = 0x15,
  130. LEGACY_SCPI_CMD_GET_CLOCK_VALUE = 0x16,
  131. LEGACY_SCPI_CMD_PSU_CAPABILITIES = 0x17,
  132. LEGACY_SCPI_CMD_SET_PSU = 0x18,
  133. LEGACY_SCPI_CMD_GET_PSU = 0x19,
  134. LEGACY_SCPI_CMD_SENSOR_CAPABILITIES = 0x1a,
  135. LEGACY_SCPI_CMD_SENSOR_INFO = 0x1b,
  136. LEGACY_SCPI_CMD_SENSOR_VALUE = 0x1c,
  137. LEGACY_SCPI_CMD_SENSOR_CFG_PERIODIC = 0x1d,
  138. LEGACY_SCPI_CMD_SENSOR_CFG_BOUNDS = 0x1e,
  139. LEGACY_SCPI_CMD_SENSOR_ASYNC_VALUE = 0x1f,
  140. LEGACY_SCPI_CMD_COUNT
  141. };
  142. /* List all commands that are required to go through the high priority link */
  143. static int legacy_hpriority_cmds[] = {
  144. LEGACY_SCPI_CMD_GET_CSS_PWR_STATE,
  145. LEGACY_SCPI_CMD_CFG_PWR_STATE_STAT,
  146. LEGACY_SCPI_CMD_GET_PWR_STATE_STAT,
  147. LEGACY_SCPI_CMD_SET_DVFS,
  148. LEGACY_SCPI_CMD_GET_DVFS,
  149. LEGACY_SCPI_CMD_SET_RTC,
  150. LEGACY_SCPI_CMD_GET_RTC,
  151. LEGACY_SCPI_CMD_SET_CLOCK_INDEX,
  152. LEGACY_SCPI_CMD_SET_CLOCK_VALUE,
  153. LEGACY_SCPI_CMD_GET_CLOCK_VALUE,
  154. LEGACY_SCPI_CMD_SET_PSU,
  155. LEGACY_SCPI_CMD_GET_PSU,
  156. LEGACY_SCPI_CMD_SENSOR_CFG_PERIODIC,
  157. LEGACY_SCPI_CMD_SENSOR_CFG_BOUNDS,
  158. };
  159. /* List all commands used by this driver, used as indexes */
  160. enum scpi_drv_cmds {
  161. CMD_SCPI_CAPABILITIES = 0,
  162. CMD_GET_CLOCK_INFO,
  163. CMD_GET_CLOCK_VALUE,
  164. CMD_SET_CLOCK_VALUE,
  165. CMD_GET_DVFS,
  166. CMD_SET_DVFS,
  167. CMD_GET_DVFS_INFO,
  168. CMD_SENSOR_CAPABILITIES,
  169. CMD_SENSOR_INFO,
  170. CMD_SENSOR_VALUE,
  171. CMD_SET_DEVICE_PWR_STATE,
  172. CMD_GET_DEVICE_PWR_STATE,
  173. CMD_MAX_COUNT,
  174. };
  175. static int scpi_std_commands[CMD_MAX_COUNT] = {
  176. SCPI_CMD_SCPI_CAPABILITIES,
  177. SCPI_CMD_GET_CLOCK_INFO,
  178. SCPI_CMD_GET_CLOCK_VALUE,
  179. SCPI_CMD_SET_CLOCK_VALUE,
  180. SCPI_CMD_GET_DVFS,
  181. SCPI_CMD_SET_DVFS,
  182. SCPI_CMD_GET_DVFS_INFO,
  183. SCPI_CMD_SENSOR_CAPABILITIES,
  184. SCPI_CMD_SENSOR_INFO,
  185. SCPI_CMD_SENSOR_VALUE,
  186. SCPI_CMD_SET_DEVICE_PWR_STATE,
  187. SCPI_CMD_GET_DEVICE_PWR_STATE,
  188. };
  189. static int scpi_legacy_commands[CMD_MAX_COUNT] = {
  190. LEGACY_SCPI_CMD_SCPI_CAPABILITIES,
  191. -1, /* GET_CLOCK_INFO */
  192. LEGACY_SCPI_CMD_GET_CLOCK_VALUE,
  193. LEGACY_SCPI_CMD_SET_CLOCK_VALUE,
  194. LEGACY_SCPI_CMD_GET_DVFS,
  195. LEGACY_SCPI_CMD_SET_DVFS,
  196. LEGACY_SCPI_CMD_GET_DVFS_INFO,
  197. LEGACY_SCPI_CMD_SENSOR_CAPABILITIES,
  198. LEGACY_SCPI_CMD_SENSOR_INFO,
  199. LEGACY_SCPI_CMD_SENSOR_VALUE,
  200. -1, /* SET_DEVICE_PWR_STATE */
  201. -1, /* GET_DEVICE_PWR_STATE */
  202. };
  203. struct scpi_xfer {
  204. u32 slot; /* has to be first element */
  205. u32 cmd;
  206. u32 status;
  207. const void *tx_buf;
  208. void *rx_buf;
  209. unsigned int tx_len;
  210. unsigned int rx_len;
  211. struct list_head node;
  212. struct completion done;
  213. };
  214. struct scpi_chan {
  215. struct mbox_client cl;
  216. struct mbox_chan *chan;
  217. void __iomem *tx_payload;
  218. void __iomem *rx_payload;
  219. struct list_head rx_pending;
  220. struct list_head xfers_list;
  221. struct scpi_xfer *xfers;
  222. spinlock_t rx_lock; /* locking for the rx pending list */
  223. struct mutex xfers_lock;
  224. u8 token;
  225. };
  226. struct scpi_drvinfo {
  227. u32 protocol_version;
  228. u32 firmware_version;
  229. bool is_legacy;
  230. int num_chans;
  231. int *commands;
  232. DECLARE_BITMAP(cmd_priority, LEGACY_SCPI_CMD_COUNT);
  233. atomic_t next_chan;
  234. struct scpi_ops *scpi_ops;
  235. struct scpi_chan *channels;
  236. struct scpi_dvfs_info *dvfs[MAX_DVFS_DOMAINS];
  237. };
  238. /*
  239. * The SCP firmware only executes in little-endian mode, so any buffers
  240. * shared through SCPI should have their contents converted to little-endian
  241. */
  242. struct scpi_shared_mem {
  243. __le32 command;
  244. __le32 status;
  245. u8 payload[];
  246. } __packed;
  247. struct legacy_scpi_shared_mem {
  248. __le32 status;
  249. u8 payload[];
  250. } __packed;
  251. struct scp_capabilities {
  252. __le32 protocol_version;
  253. __le32 event_version;
  254. __le32 platform_version;
  255. __le32 commands[4];
  256. } __packed;
  257. struct clk_get_info {
  258. __le16 id;
  259. __le16 flags;
  260. __le32 min_rate;
  261. __le32 max_rate;
  262. u8 name[20];
  263. } __packed;
  264. struct clk_set_value {
  265. __le16 id;
  266. __le16 reserved;
  267. __le32 rate;
  268. } __packed;
  269. struct legacy_clk_set_value {
  270. __le32 rate;
  271. __le16 id;
  272. __le16 reserved;
  273. } __packed;
  274. struct dvfs_info {
  275. u8 domain;
  276. u8 opp_count;
  277. __le16 latency;
  278. struct {
  279. __le32 freq;
  280. __le32 m_volt;
  281. } opps[MAX_DVFS_OPPS];
  282. } __packed;
  283. struct dvfs_set {
  284. u8 domain;
  285. u8 index;
  286. } __packed;
  287. struct _scpi_sensor_info {
  288. __le16 sensor_id;
  289. u8 class;
  290. u8 trigger_type;
  291. char name[20];
  292. };
  293. struct dev_pstate_set {
  294. __le16 dev_id;
  295. u8 pstate;
  296. } __packed;
  297. static struct scpi_drvinfo *scpi_info;
  298. static int scpi_linux_errmap[SCPI_ERR_MAX] = {
  299. /* better than switch case as long as return value is continuous */
  300. 0, /* SCPI_SUCCESS */
  301. -EINVAL, /* SCPI_ERR_PARAM */
  302. -ENOEXEC, /* SCPI_ERR_ALIGN */
  303. -EMSGSIZE, /* SCPI_ERR_SIZE */
  304. -EINVAL, /* SCPI_ERR_HANDLER */
  305. -EACCES, /* SCPI_ERR_ACCESS */
  306. -ERANGE, /* SCPI_ERR_RANGE */
  307. -ETIMEDOUT, /* SCPI_ERR_TIMEOUT */
  308. -ENOMEM, /* SCPI_ERR_NOMEM */
  309. -EINVAL, /* SCPI_ERR_PWRSTATE */
  310. -EOPNOTSUPP, /* SCPI_ERR_SUPPORT */
  311. -EIO, /* SCPI_ERR_DEVICE */
  312. -EBUSY, /* SCPI_ERR_BUSY */
  313. };
  314. static inline int scpi_to_linux_errno(int errno)
  315. {
  316. if (errno >= SCPI_SUCCESS && errno < SCPI_ERR_MAX)
  317. return scpi_linux_errmap[errno];
  318. return -EIO;
  319. }
  320. static void scpi_process_cmd(struct scpi_chan *ch, u32 cmd)
  321. {
  322. unsigned long flags;
  323. struct scpi_xfer *t, *match = NULL;
  324. spin_lock_irqsave(&ch->rx_lock, flags);
  325. if (list_empty(&ch->rx_pending)) {
  326. spin_unlock_irqrestore(&ch->rx_lock, flags);
  327. return;
  328. }
  329. /* Command type is not replied by the SCP Firmware in legacy Mode
  330. * We should consider that command is the head of pending RX commands
  331. * if the list is not empty. In TX only mode, the list would be empty.
  332. */
  333. if (scpi_info->is_legacy) {
  334. match = list_first_entry(&ch->rx_pending, struct scpi_xfer,
  335. node);
  336. list_del(&match->node);
  337. } else {
  338. list_for_each_entry(t, &ch->rx_pending, node)
  339. if (CMD_XTRACT_UNIQ(t->cmd) == CMD_XTRACT_UNIQ(cmd)) {
  340. list_del(&t->node);
  341. match = t;
  342. break;
  343. }
  344. }
  345. /* check if wait_for_completion is in progress or timed-out */
  346. if (match && !completion_done(&match->done)) {
  347. unsigned int len;
  348. if (scpi_info->is_legacy) {
  349. struct legacy_scpi_shared_mem __iomem *mem =
  350. ch->rx_payload;
  351. /* RX Length is not replied by the legacy Firmware */
  352. len = match->rx_len;
  353. match->status = ioread32(&mem->status);
  354. memcpy_fromio(match->rx_buf, mem->payload, len);
  355. } else {
  356. struct scpi_shared_mem __iomem *mem = ch->rx_payload;
  357. len = min_t(unsigned int, match->rx_len, CMD_SIZE(cmd));
  358. match->status = ioread32(&mem->status);
  359. memcpy_fromio(match->rx_buf, mem->payload, len);
  360. }
  361. if (match->rx_len > len)
  362. memset(match->rx_buf + len, 0, match->rx_len - len);
  363. complete(&match->done);
  364. }
  365. spin_unlock_irqrestore(&ch->rx_lock, flags);
  366. }
  367. static void scpi_handle_remote_msg(struct mbox_client *c, void *msg)
  368. {
  369. struct scpi_chan *ch = container_of(c, struct scpi_chan, cl);
  370. struct scpi_shared_mem __iomem *mem = ch->rx_payload;
  371. u32 cmd = 0;
  372. if (!scpi_info->is_legacy)
  373. cmd = ioread32(&mem->command);
  374. scpi_process_cmd(ch, cmd);
  375. }
  376. static void scpi_tx_prepare(struct mbox_client *c, void *msg)
  377. {
  378. unsigned long flags;
  379. struct scpi_xfer *t = msg;
  380. struct scpi_chan *ch = container_of(c, struct scpi_chan, cl);
  381. struct scpi_shared_mem __iomem *mem = ch->tx_payload;
  382. if (t->tx_buf) {
  383. if (scpi_info->is_legacy)
  384. memcpy_toio(ch->tx_payload, t->tx_buf, t->tx_len);
  385. else
  386. memcpy_toio(mem->payload, t->tx_buf, t->tx_len);
  387. }
  388. if (t->rx_buf) {
  389. if (!(++ch->token))
  390. ++ch->token;
  391. t->cmd |= FIELD_PREP(CMD_TOKEN_ID_MASK, ch->token);
  392. spin_lock_irqsave(&ch->rx_lock, flags);
  393. list_add_tail(&t->node, &ch->rx_pending);
  394. spin_unlock_irqrestore(&ch->rx_lock, flags);
  395. }
  396. if (!scpi_info->is_legacy)
  397. iowrite32(t->cmd, &mem->command);
  398. }
  399. static struct scpi_xfer *get_scpi_xfer(struct scpi_chan *ch)
  400. {
  401. struct scpi_xfer *t;
  402. mutex_lock(&ch->xfers_lock);
  403. if (list_empty(&ch->xfers_list)) {
  404. mutex_unlock(&ch->xfers_lock);
  405. return NULL;
  406. }
  407. t = list_first_entry(&ch->xfers_list, struct scpi_xfer, node);
  408. list_del(&t->node);
  409. mutex_unlock(&ch->xfers_lock);
  410. return t;
  411. }
  412. static void put_scpi_xfer(struct scpi_xfer *t, struct scpi_chan *ch)
  413. {
  414. mutex_lock(&ch->xfers_lock);
  415. list_add_tail(&t->node, &ch->xfers_list);
  416. mutex_unlock(&ch->xfers_lock);
  417. }
  418. static int scpi_send_message(u8 idx, void *tx_buf, unsigned int tx_len,
  419. void *rx_buf, unsigned int rx_len)
  420. {
  421. int ret;
  422. u8 chan;
  423. u8 cmd;
  424. struct scpi_xfer *msg;
  425. struct scpi_chan *scpi_chan;
  426. if (scpi_info->commands[idx] < 0)
  427. return -EOPNOTSUPP;
  428. cmd = scpi_info->commands[idx];
  429. if (scpi_info->is_legacy)
  430. chan = test_bit(cmd, scpi_info->cmd_priority) ? 1 : 0;
  431. else
  432. chan = atomic_inc_return(&scpi_info->next_chan) %
  433. scpi_info->num_chans;
  434. scpi_chan = scpi_info->channels + chan;
  435. msg = get_scpi_xfer(scpi_chan);
  436. if (!msg)
  437. return -ENOMEM;
  438. if (scpi_info->is_legacy) {
  439. msg->cmd = PACK_LEGACY_SCPI_CMD(cmd, tx_len);
  440. msg->slot = msg->cmd;
  441. } else {
  442. msg->slot = BIT(SCPI_SLOT);
  443. msg->cmd = PACK_SCPI_CMD(cmd, tx_len);
  444. }
  445. msg->tx_buf = tx_buf;
  446. msg->tx_len = tx_len;
  447. msg->rx_buf = rx_buf;
  448. msg->rx_len = rx_len;
  449. reinit_completion(&msg->done);
  450. ret = mbox_send_message(scpi_chan->chan, msg);
  451. if (ret < 0 || !rx_buf)
  452. goto out;
  453. if (!wait_for_completion_timeout(&msg->done, MAX_RX_TIMEOUT))
  454. ret = -ETIMEDOUT;
  455. else
  456. /* first status word */
  457. ret = msg->status;
  458. out:
  459. if (ret < 0 && rx_buf) /* remove entry from the list if timed-out */
  460. scpi_process_cmd(scpi_chan, msg->cmd);
  461. put_scpi_xfer(msg, scpi_chan);
  462. /* SCPI error codes > 0, translate them to Linux scale*/
  463. return ret > 0 ? scpi_to_linux_errno(ret) : ret;
  464. }
  465. static u32 scpi_get_version(void)
  466. {
  467. return scpi_info->protocol_version;
  468. }
  469. static int
  470. scpi_clk_get_range(u16 clk_id, unsigned long *min, unsigned long *max)
  471. {
  472. int ret;
  473. struct clk_get_info clk;
  474. __le16 le_clk_id = cpu_to_le16(clk_id);
  475. ret = scpi_send_message(CMD_GET_CLOCK_INFO, &le_clk_id,
  476. sizeof(le_clk_id), &clk, sizeof(clk));
  477. if (!ret) {
  478. *min = le32_to_cpu(clk.min_rate);
  479. *max = le32_to_cpu(clk.max_rate);
  480. }
  481. return ret;
  482. }
  483. static unsigned long scpi_clk_get_val(u16 clk_id)
  484. {
  485. int ret;
  486. __le32 rate;
  487. __le16 le_clk_id = cpu_to_le16(clk_id);
  488. ret = scpi_send_message(CMD_GET_CLOCK_VALUE, &le_clk_id,
  489. sizeof(le_clk_id), &rate, sizeof(rate));
  490. if (ret)
  491. return 0;
  492. return le32_to_cpu(rate);
  493. }
  494. static int scpi_clk_set_val(u16 clk_id, unsigned long rate)
  495. {
  496. int stat;
  497. struct clk_set_value clk = {
  498. .id = cpu_to_le16(clk_id),
  499. .rate = cpu_to_le32(rate)
  500. };
  501. return scpi_send_message(CMD_SET_CLOCK_VALUE, &clk, sizeof(clk),
  502. &stat, sizeof(stat));
  503. }
  504. static int legacy_scpi_clk_set_val(u16 clk_id, unsigned long rate)
  505. {
  506. int stat;
  507. struct legacy_clk_set_value clk = {
  508. .id = cpu_to_le16(clk_id),
  509. .rate = cpu_to_le32(rate)
  510. };
  511. return scpi_send_message(CMD_SET_CLOCK_VALUE, &clk, sizeof(clk),
  512. &stat, sizeof(stat));
  513. }
  514. static int scpi_dvfs_get_idx(u8 domain)
  515. {
  516. int ret;
  517. u8 dvfs_idx;
  518. ret = scpi_send_message(CMD_GET_DVFS, &domain, sizeof(domain),
  519. &dvfs_idx, sizeof(dvfs_idx));
  520. return ret ? ret : dvfs_idx;
  521. }
  522. static int scpi_dvfs_set_idx(u8 domain, u8 index)
  523. {
  524. int stat;
  525. struct dvfs_set dvfs = {domain, index};
  526. return scpi_send_message(CMD_SET_DVFS, &dvfs, sizeof(dvfs),
  527. &stat, sizeof(stat));
  528. }
  529. static int opp_cmp_func(const void *opp1, const void *opp2)
  530. {
  531. const struct scpi_opp *t1 = opp1, *t2 = opp2;
  532. return t1->freq - t2->freq;
  533. }
  534. static struct scpi_dvfs_info *scpi_dvfs_get_info(u8 domain)
  535. {
  536. struct scpi_dvfs_info *info;
  537. struct scpi_opp *opp;
  538. struct dvfs_info buf;
  539. int ret, i;
  540. if (domain >= MAX_DVFS_DOMAINS)
  541. return ERR_PTR(-EINVAL);
  542. if (scpi_info->dvfs[domain]) /* data already populated */
  543. return scpi_info->dvfs[domain];
  544. ret = scpi_send_message(CMD_GET_DVFS_INFO, &domain, sizeof(domain),
  545. &buf, sizeof(buf));
  546. if (ret)
  547. return ERR_PTR(ret);
  548. info = kmalloc(sizeof(*info), GFP_KERNEL);
  549. if (!info)
  550. return ERR_PTR(-ENOMEM);
  551. info->count = buf.opp_count;
  552. info->latency = le16_to_cpu(buf.latency) * 1000; /* uS to nS */
  553. info->opps = kcalloc(info->count, sizeof(*opp), GFP_KERNEL);
  554. if (!info->opps) {
  555. kfree(info);
  556. return ERR_PTR(-ENOMEM);
  557. }
  558. for (i = 0, opp = info->opps; i < info->count; i++, opp++) {
  559. opp->freq = le32_to_cpu(buf.opps[i].freq);
  560. opp->m_volt = le32_to_cpu(buf.opps[i].m_volt);
  561. }
  562. sort(info->opps, info->count, sizeof(*opp), opp_cmp_func, NULL);
  563. scpi_info->dvfs[domain] = info;
  564. return info;
  565. }
  566. static int scpi_dev_domain_id(struct device *dev)
  567. {
  568. struct of_phandle_args clkspec;
  569. if (of_parse_phandle_with_args(dev->of_node, "clocks", "#clock-cells",
  570. 0, &clkspec))
  571. return -EINVAL;
  572. return clkspec.args[0];
  573. }
  574. static struct scpi_dvfs_info *scpi_dvfs_info(struct device *dev)
  575. {
  576. int domain = scpi_dev_domain_id(dev);
  577. if (domain < 0)
  578. return ERR_PTR(domain);
  579. return scpi_dvfs_get_info(domain);
  580. }
  581. static int scpi_dvfs_get_transition_latency(struct device *dev)
  582. {
  583. struct scpi_dvfs_info *info = scpi_dvfs_info(dev);
  584. if (IS_ERR(info))
  585. return PTR_ERR(info);
  586. return info->latency;
  587. }
  588. static int scpi_dvfs_add_opps_to_device(struct device *dev)
  589. {
  590. int idx, ret;
  591. struct scpi_opp *opp;
  592. struct scpi_dvfs_info *info = scpi_dvfs_info(dev);
  593. if (IS_ERR(info))
  594. return PTR_ERR(info);
  595. if (!info->opps)
  596. return -EIO;
  597. for (opp = info->opps, idx = 0; idx < info->count; idx++, opp++) {
  598. ret = dev_pm_opp_add(dev, opp->freq, opp->m_volt * 1000);
  599. if (ret) {
  600. dev_warn(dev, "failed to add opp %uHz %umV\n",
  601. opp->freq, opp->m_volt);
  602. while (idx-- > 0)
  603. dev_pm_opp_remove(dev, (--opp)->freq);
  604. return ret;
  605. }
  606. }
  607. return 0;
  608. }
  609. static int scpi_sensor_get_capability(u16 *sensors)
  610. {
  611. __le16 cap;
  612. int ret;
  613. ret = scpi_send_message(CMD_SENSOR_CAPABILITIES, NULL, 0, &cap,
  614. sizeof(cap));
  615. if (!ret)
  616. *sensors = le16_to_cpu(cap);
  617. return ret;
  618. }
  619. static int scpi_sensor_get_info(u16 sensor_id, struct scpi_sensor_info *info)
  620. {
  621. __le16 id = cpu_to_le16(sensor_id);
  622. struct _scpi_sensor_info _info;
  623. int ret;
  624. ret = scpi_send_message(CMD_SENSOR_INFO, &id, sizeof(id),
  625. &_info, sizeof(_info));
  626. if (!ret) {
  627. memcpy(info, &_info, sizeof(*info));
  628. info->sensor_id = le16_to_cpu(_info.sensor_id);
  629. }
  630. return ret;
  631. }
  632. static int scpi_sensor_get_value(u16 sensor, u64 *val)
  633. {
  634. __le16 id = cpu_to_le16(sensor);
  635. __le64 value;
  636. int ret;
  637. ret = scpi_send_message(CMD_SENSOR_VALUE, &id, sizeof(id),
  638. &value, sizeof(value));
  639. if (ret)
  640. return ret;
  641. if (scpi_info->is_legacy)
  642. /* only 32-bits supported, upper 32 bits can be junk */
  643. *val = le32_to_cpup((__le32 *)&value);
  644. else
  645. *val = le64_to_cpu(value);
  646. return 0;
  647. }
  648. static int scpi_device_get_power_state(u16 dev_id)
  649. {
  650. int ret;
  651. u8 pstate;
  652. __le16 id = cpu_to_le16(dev_id);
  653. ret = scpi_send_message(CMD_GET_DEVICE_PWR_STATE, &id,
  654. sizeof(id), &pstate, sizeof(pstate));
  655. return ret ? ret : pstate;
  656. }
  657. static int scpi_device_set_power_state(u16 dev_id, u8 pstate)
  658. {
  659. int stat;
  660. struct dev_pstate_set dev_set = {
  661. .dev_id = cpu_to_le16(dev_id),
  662. .pstate = pstate,
  663. };
  664. return scpi_send_message(CMD_SET_DEVICE_PWR_STATE, &dev_set,
  665. sizeof(dev_set), &stat, sizeof(stat));
  666. }
  667. static struct scpi_ops scpi_ops = {
  668. .get_version = scpi_get_version,
  669. .clk_get_range = scpi_clk_get_range,
  670. .clk_get_val = scpi_clk_get_val,
  671. .clk_set_val = scpi_clk_set_val,
  672. .dvfs_get_idx = scpi_dvfs_get_idx,
  673. .dvfs_set_idx = scpi_dvfs_set_idx,
  674. .dvfs_get_info = scpi_dvfs_get_info,
  675. .device_domain_id = scpi_dev_domain_id,
  676. .get_transition_latency = scpi_dvfs_get_transition_latency,
  677. .add_opps_to_device = scpi_dvfs_add_opps_to_device,
  678. .sensor_get_capability = scpi_sensor_get_capability,
  679. .sensor_get_info = scpi_sensor_get_info,
  680. .sensor_get_value = scpi_sensor_get_value,
  681. .device_get_power_state = scpi_device_get_power_state,
  682. .device_set_power_state = scpi_device_set_power_state,
  683. };
  684. struct scpi_ops *get_scpi_ops(void)
  685. {
  686. return scpi_info ? scpi_info->scpi_ops : NULL;
  687. }
  688. EXPORT_SYMBOL_GPL(get_scpi_ops);
  689. static int scpi_init_versions(struct scpi_drvinfo *info)
  690. {
  691. int ret;
  692. struct scp_capabilities caps;
  693. ret = scpi_send_message(CMD_SCPI_CAPABILITIES, NULL, 0,
  694. &caps, sizeof(caps));
  695. if (!ret) {
  696. info->protocol_version = le32_to_cpu(caps.protocol_version);
  697. info->firmware_version = le32_to_cpu(caps.platform_version);
  698. }
  699. /* Ignore error if not implemented */
  700. if (info->is_legacy && ret == -EOPNOTSUPP)
  701. return 0;
  702. return ret;
  703. }
  704. static ssize_t protocol_version_show(struct device *dev,
  705. struct device_attribute *attr, char *buf)
  706. {
  707. struct scpi_drvinfo *scpi_info = dev_get_drvdata(dev);
  708. return sprintf(buf, "%lu.%lu\n",
  709. FIELD_GET(PROTO_REV_MAJOR_MASK, scpi_info->protocol_version),
  710. FIELD_GET(PROTO_REV_MINOR_MASK, scpi_info->protocol_version));
  711. }
  712. static DEVICE_ATTR_RO(protocol_version);
  713. static ssize_t firmware_version_show(struct device *dev,
  714. struct device_attribute *attr, char *buf)
  715. {
  716. struct scpi_drvinfo *scpi_info = dev_get_drvdata(dev);
  717. return sprintf(buf, "%lu.%lu.%lu\n",
  718. FIELD_GET(FW_REV_MAJOR_MASK, scpi_info->firmware_version),
  719. FIELD_GET(FW_REV_MINOR_MASK, scpi_info->firmware_version),
  720. FIELD_GET(FW_REV_PATCH_MASK, scpi_info->firmware_version));
  721. }
  722. static DEVICE_ATTR_RO(firmware_version);
  723. static struct attribute *versions_attrs[] = {
  724. &dev_attr_firmware_version.attr,
  725. &dev_attr_protocol_version.attr,
  726. NULL,
  727. };
  728. ATTRIBUTE_GROUPS(versions);
  729. static void scpi_free_channels(void *data)
  730. {
  731. struct scpi_drvinfo *info = data;
  732. int i;
  733. for (i = 0; i < info->num_chans; i++)
  734. mbox_free_channel(info->channels[i].chan);
  735. }
  736. static int scpi_remove(struct platform_device *pdev)
  737. {
  738. int i;
  739. struct scpi_drvinfo *info = platform_get_drvdata(pdev);
  740. scpi_info = NULL; /* stop exporting SCPI ops through get_scpi_ops */
  741. for (i = 0; i < MAX_DVFS_DOMAINS && info->dvfs[i]; i++) {
  742. kfree(info->dvfs[i]->opps);
  743. kfree(info->dvfs[i]);
  744. }
  745. return 0;
  746. }
  747. #define MAX_SCPI_XFERS 10
  748. static int scpi_alloc_xfer_list(struct device *dev, struct scpi_chan *ch)
  749. {
  750. int i;
  751. struct scpi_xfer *xfers;
  752. xfers = devm_kcalloc(dev, MAX_SCPI_XFERS, sizeof(*xfers), GFP_KERNEL);
  753. if (!xfers)
  754. return -ENOMEM;
  755. ch->xfers = xfers;
  756. for (i = 0; i < MAX_SCPI_XFERS; i++, xfers++) {
  757. init_completion(&xfers->done);
  758. list_add_tail(&xfers->node, &ch->xfers_list);
  759. }
  760. return 0;
  761. }
  762. static const struct of_device_id legacy_scpi_of_match[] = {
  763. {.compatible = "arm,scpi-pre-1.0"},
  764. {},
  765. };
  766. static const struct of_device_id shmem_of_match[] __maybe_unused = {
  767. { .compatible = "amlogic,meson-gxbb-scp-shmem", },
  768. { .compatible = "amlogic,meson-axg-scp-shmem", },
  769. { .compatible = "arm,juno-scp-shmem", },
  770. { .compatible = "arm,scp-shmem", },
  771. { }
  772. };
  773. static int scpi_probe(struct platform_device *pdev)
  774. {
  775. int count, idx, ret;
  776. struct resource res;
  777. struct device *dev = &pdev->dev;
  778. struct device_node *np = dev->of_node;
  779. struct scpi_drvinfo *scpi_drvinfo;
  780. scpi_drvinfo = devm_kzalloc(dev, sizeof(*scpi_drvinfo), GFP_KERNEL);
  781. if (!scpi_drvinfo)
  782. return -ENOMEM;
  783. if (of_match_device(legacy_scpi_of_match, &pdev->dev))
  784. scpi_drvinfo->is_legacy = true;
  785. count = of_count_phandle_with_args(np, "mboxes", "#mbox-cells");
  786. if (count < 0) {
  787. dev_err(dev, "no mboxes property in '%pOF'\n", np);
  788. return -ENODEV;
  789. }
  790. scpi_drvinfo->channels =
  791. devm_kcalloc(dev, count, sizeof(struct scpi_chan), GFP_KERNEL);
  792. if (!scpi_drvinfo->channels)
  793. return -ENOMEM;
  794. ret = devm_add_action(dev, scpi_free_channels, scpi_drvinfo);
  795. if (ret)
  796. return ret;
  797. for (; scpi_drvinfo->num_chans < count; scpi_drvinfo->num_chans++) {
  798. resource_size_t size;
  799. int idx = scpi_drvinfo->num_chans;
  800. struct scpi_chan *pchan = scpi_drvinfo->channels + idx;
  801. struct mbox_client *cl = &pchan->cl;
  802. struct device_node *shmem = of_parse_phandle(np, "shmem", idx);
  803. if (!of_match_node(shmem_of_match, shmem))
  804. return -ENXIO;
  805. ret = of_address_to_resource(shmem, 0, &res);
  806. of_node_put(shmem);
  807. if (ret) {
  808. dev_err(dev, "failed to get SCPI payload mem resource\n");
  809. return ret;
  810. }
  811. size = resource_size(&res);
  812. pchan->rx_payload = devm_ioremap(dev, res.start, size);
  813. if (!pchan->rx_payload) {
  814. dev_err(dev, "failed to ioremap SCPI payload\n");
  815. return -EADDRNOTAVAIL;
  816. }
  817. pchan->tx_payload = pchan->rx_payload + (size >> 1);
  818. cl->dev = dev;
  819. cl->rx_callback = scpi_handle_remote_msg;
  820. cl->tx_prepare = scpi_tx_prepare;
  821. cl->tx_block = true;
  822. cl->tx_tout = 20;
  823. cl->knows_txdone = false; /* controller can't ack */
  824. INIT_LIST_HEAD(&pchan->rx_pending);
  825. INIT_LIST_HEAD(&pchan->xfers_list);
  826. spin_lock_init(&pchan->rx_lock);
  827. mutex_init(&pchan->xfers_lock);
  828. ret = scpi_alloc_xfer_list(dev, pchan);
  829. if (!ret) {
  830. pchan->chan = mbox_request_channel(cl, idx);
  831. if (!IS_ERR(pchan->chan))
  832. continue;
  833. ret = PTR_ERR(pchan->chan);
  834. if (ret != -EPROBE_DEFER)
  835. dev_err(dev, "failed to get channel%d err %d\n",
  836. idx, ret);
  837. }
  838. return ret;
  839. }
  840. scpi_drvinfo->commands = scpi_std_commands;
  841. platform_set_drvdata(pdev, scpi_drvinfo);
  842. if (scpi_drvinfo->is_legacy) {
  843. /* Replace with legacy variants */
  844. scpi_ops.clk_set_val = legacy_scpi_clk_set_val;
  845. scpi_drvinfo->commands = scpi_legacy_commands;
  846. /* Fill priority bitmap */
  847. for (idx = 0; idx < ARRAY_SIZE(legacy_hpriority_cmds); idx++)
  848. set_bit(legacy_hpriority_cmds[idx],
  849. scpi_drvinfo->cmd_priority);
  850. }
  851. scpi_info = scpi_drvinfo;
  852. ret = scpi_init_versions(scpi_drvinfo);
  853. if (ret) {
  854. dev_err(dev, "incorrect or no SCP firmware found\n");
  855. scpi_info = NULL;
  856. return ret;
  857. }
  858. if (scpi_drvinfo->is_legacy && !scpi_drvinfo->protocol_version &&
  859. !scpi_drvinfo->firmware_version)
  860. dev_info(dev, "SCP Protocol legacy pre-1.0 firmware\n");
  861. else
  862. dev_info(dev, "SCP Protocol %lu.%lu Firmware %lu.%lu.%lu version\n",
  863. FIELD_GET(PROTO_REV_MAJOR_MASK,
  864. scpi_drvinfo->protocol_version),
  865. FIELD_GET(PROTO_REV_MINOR_MASK,
  866. scpi_drvinfo->protocol_version),
  867. FIELD_GET(FW_REV_MAJOR_MASK,
  868. scpi_drvinfo->firmware_version),
  869. FIELD_GET(FW_REV_MINOR_MASK,
  870. scpi_drvinfo->firmware_version),
  871. FIELD_GET(FW_REV_PATCH_MASK,
  872. scpi_drvinfo->firmware_version));
  873. scpi_drvinfo->scpi_ops = &scpi_ops;
  874. ret = devm_of_platform_populate(dev);
  875. if (ret)
  876. scpi_info = NULL;
  877. return ret;
  878. }
  879. static const struct of_device_id scpi_of_match[] = {
  880. {.compatible = "arm,scpi"},
  881. {.compatible = "arm,scpi-pre-1.0"},
  882. {},
  883. };
  884. MODULE_DEVICE_TABLE(of, scpi_of_match);
  885. static struct platform_driver scpi_driver = {
  886. .driver = {
  887. .name = "scpi_protocol",
  888. .of_match_table = scpi_of_match,
  889. .dev_groups = versions_groups,
  890. },
  891. .probe = scpi_probe,
  892. .remove = scpi_remove,
  893. };
  894. module_platform_driver(scpi_driver);
  895. MODULE_AUTHOR("Sudeep Holla <[email protected]>");
  896. MODULE_DESCRIPTION("ARM SCPI mailbox protocol driver");
  897. MODULE_LICENSE("GPL v2");