arm_scpi.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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 (scpi_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 int scpi_probe(struct platform_device *pdev)
  767. {
  768. int count, idx, ret;
  769. struct resource res;
  770. struct device *dev = &pdev->dev;
  771. struct device_node *np = dev->of_node;
  772. scpi_info = devm_kzalloc(dev, sizeof(*scpi_info), GFP_KERNEL);
  773. if (!scpi_info)
  774. return -ENOMEM;
  775. if (of_match_device(legacy_scpi_of_match, &pdev->dev))
  776. scpi_info->is_legacy = true;
  777. count = of_count_phandle_with_args(np, "mboxes", "#mbox-cells");
  778. if (count < 0) {
  779. dev_err(dev, "no mboxes property in '%pOF'\n", np);
  780. return -ENODEV;
  781. }
  782. scpi_info->channels = devm_kcalloc(dev, count, sizeof(struct scpi_chan),
  783. GFP_KERNEL);
  784. if (!scpi_info->channels)
  785. return -ENOMEM;
  786. ret = devm_add_action(dev, scpi_free_channels, scpi_info);
  787. if (ret)
  788. return ret;
  789. for (; scpi_info->num_chans < count; scpi_info->num_chans++) {
  790. resource_size_t size;
  791. int idx = scpi_info->num_chans;
  792. struct scpi_chan *pchan = scpi_info->channels + idx;
  793. struct mbox_client *cl = &pchan->cl;
  794. struct device_node *shmem = of_parse_phandle(np, "shmem", idx);
  795. ret = of_address_to_resource(shmem, 0, &res);
  796. of_node_put(shmem);
  797. if (ret) {
  798. dev_err(dev, "failed to get SCPI payload mem resource\n");
  799. return ret;
  800. }
  801. size = resource_size(&res);
  802. pchan->rx_payload = devm_ioremap(dev, res.start, size);
  803. if (!pchan->rx_payload) {
  804. dev_err(dev, "failed to ioremap SCPI payload\n");
  805. return -EADDRNOTAVAIL;
  806. }
  807. pchan->tx_payload = pchan->rx_payload + (size >> 1);
  808. cl->dev = dev;
  809. cl->rx_callback = scpi_handle_remote_msg;
  810. cl->tx_prepare = scpi_tx_prepare;
  811. cl->tx_block = true;
  812. cl->tx_tout = 20;
  813. cl->knows_txdone = false; /* controller can't ack */
  814. INIT_LIST_HEAD(&pchan->rx_pending);
  815. INIT_LIST_HEAD(&pchan->xfers_list);
  816. spin_lock_init(&pchan->rx_lock);
  817. mutex_init(&pchan->xfers_lock);
  818. ret = scpi_alloc_xfer_list(dev, pchan);
  819. if (!ret) {
  820. pchan->chan = mbox_request_channel(cl, idx);
  821. if (!IS_ERR(pchan->chan))
  822. continue;
  823. ret = PTR_ERR(pchan->chan);
  824. if (ret != -EPROBE_DEFER)
  825. dev_err(dev, "failed to get channel%d err %d\n",
  826. idx, ret);
  827. }
  828. return ret;
  829. }
  830. scpi_info->commands = scpi_std_commands;
  831. platform_set_drvdata(pdev, scpi_info);
  832. if (scpi_info->is_legacy) {
  833. /* Replace with legacy variants */
  834. scpi_ops.clk_set_val = legacy_scpi_clk_set_val;
  835. scpi_info->commands = scpi_legacy_commands;
  836. /* Fill priority bitmap */
  837. for (idx = 0; idx < ARRAY_SIZE(legacy_hpriority_cmds); idx++)
  838. set_bit(legacy_hpriority_cmds[idx],
  839. scpi_info->cmd_priority);
  840. }
  841. ret = scpi_init_versions(scpi_info);
  842. if (ret) {
  843. dev_err(dev, "incorrect or no SCP firmware found\n");
  844. return ret;
  845. }
  846. if (scpi_info->is_legacy && !scpi_info->protocol_version &&
  847. !scpi_info->firmware_version)
  848. dev_info(dev, "SCP Protocol legacy pre-1.0 firmware\n");
  849. else
  850. dev_info(dev, "SCP Protocol %lu.%lu Firmware %lu.%lu.%lu version\n",
  851. FIELD_GET(PROTO_REV_MAJOR_MASK,
  852. scpi_info->protocol_version),
  853. FIELD_GET(PROTO_REV_MINOR_MASK,
  854. scpi_info->protocol_version),
  855. FIELD_GET(FW_REV_MAJOR_MASK,
  856. scpi_info->firmware_version),
  857. FIELD_GET(FW_REV_MINOR_MASK,
  858. scpi_info->firmware_version),
  859. FIELD_GET(FW_REV_PATCH_MASK,
  860. scpi_info->firmware_version));
  861. scpi_info->scpi_ops = &scpi_ops;
  862. return devm_of_platform_populate(dev);
  863. }
  864. static const struct of_device_id scpi_of_match[] = {
  865. {.compatible = "arm,scpi"},
  866. {.compatible = "arm,scpi-pre-1.0"},
  867. {},
  868. };
  869. MODULE_DEVICE_TABLE(of, scpi_of_match);
  870. static struct platform_driver scpi_driver = {
  871. .driver = {
  872. .name = "scpi_protocol",
  873. .of_match_table = scpi_of_match,
  874. .dev_groups = versions_groups,
  875. },
  876. .probe = scpi_probe,
  877. .remove = scpi_remove,
  878. };
  879. module_platform_driver(scpi_driver);
  880. MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
  881. MODULE_DESCRIPTION("ARM SCPI mailbox protocol driver");
  882. MODULE_LICENSE("GPL v2");