arm_scpi.c 27 KB

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