arm_scpi.c 26 KB

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