arm_scpi.c 28 KB

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