arm_scpi.c 27 KB

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