arm_scpi.c 26 KB

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