intel_punit_ipc.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for the Intel P-Unit Mailbox IPC mechanism
  4. *
  5. * (C) Copyright 2015 Intel Corporation
  6. *
  7. * The heart of the P-Unit is the Foxton microcontroller and its firmware,
  8. * which provide mailbox interface for power management usage.
  9. */
  10. #include <linux/acpi.h>
  11. #include <linux/bitops.h>
  12. #include <linux/delay.h>
  13. #include <linux/device.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/mod_devicetable.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <asm/intel_punit_ipc.h>
  20. /* IPC Mailbox registers */
  21. #define OFFSET_DATA_LOW 0x0
  22. #define OFFSET_DATA_HIGH 0x4
  23. /* bit field of interface register */
  24. #define CMD_RUN BIT(31)
  25. #define CMD_ERRCODE_MASK GENMASK(7, 0)
  26. #define CMD_PARA1_SHIFT 8
  27. #define CMD_PARA2_SHIFT 16
  28. #define CMD_TIMEOUT_SECONDS 1
  29. enum {
  30. BASE_DATA = 0,
  31. BASE_IFACE,
  32. BASE_MAX,
  33. };
  34. typedef struct {
  35. struct device *dev;
  36. struct mutex lock;
  37. int irq;
  38. struct completion cmd_complete;
  39. /* base of interface and data registers */
  40. void __iomem *base[RESERVED_IPC][BASE_MAX];
  41. IPC_TYPE type;
  42. } IPC_DEV;
  43. static IPC_DEV *punit_ipcdev;
  44. static inline u32 ipc_read_status(IPC_DEV *ipcdev, IPC_TYPE type)
  45. {
  46. return readl(ipcdev->base[type][BASE_IFACE]);
  47. }
  48. static inline void ipc_write_cmd(IPC_DEV *ipcdev, IPC_TYPE type, u32 cmd)
  49. {
  50. writel(cmd, ipcdev->base[type][BASE_IFACE]);
  51. }
  52. static inline u32 ipc_read_data_low(IPC_DEV *ipcdev, IPC_TYPE type)
  53. {
  54. return readl(ipcdev->base[type][BASE_DATA] + OFFSET_DATA_LOW);
  55. }
  56. static inline u32 ipc_read_data_high(IPC_DEV *ipcdev, IPC_TYPE type)
  57. {
  58. return readl(ipcdev->base[type][BASE_DATA] + OFFSET_DATA_HIGH);
  59. }
  60. static inline void ipc_write_data_low(IPC_DEV *ipcdev, IPC_TYPE type, u32 data)
  61. {
  62. writel(data, ipcdev->base[type][BASE_DATA] + OFFSET_DATA_LOW);
  63. }
  64. static inline void ipc_write_data_high(IPC_DEV *ipcdev, IPC_TYPE type, u32 data)
  65. {
  66. writel(data, ipcdev->base[type][BASE_DATA] + OFFSET_DATA_HIGH);
  67. }
  68. static const char *ipc_err_string(int error)
  69. {
  70. if (error == IPC_PUNIT_ERR_SUCCESS)
  71. return "no error";
  72. else if (error == IPC_PUNIT_ERR_INVALID_CMD)
  73. return "invalid command";
  74. else if (error == IPC_PUNIT_ERR_INVALID_PARAMETER)
  75. return "invalid parameter";
  76. else if (error == IPC_PUNIT_ERR_CMD_TIMEOUT)
  77. return "command timeout";
  78. else if (error == IPC_PUNIT_ERR_CMD_LOCKED)
  79. return "command locked";
  80. else if (error == IPC_PUNIT_ERR_INVALID_VR_ID)
  81. return "invalid vr id";
  82. else if (error == IPC_PUNIT_ERR_VR_ERR)
  83. return "vr error";
  84. else
  85. return "unknown error";
  86. }
  87. static int intel_punit_ipc_check_status(IPC_DEV *ipcdev, IPC_TYPE type)
  88. {
  89. int loops = CMD_TIMEOUT_SECONDS * USEC_PER_SEC;
  90. int errcode;
  91. int status;
  92. if (ipcdev->irq) {
  93. if (!wait_for_completion_timeout(&ipcdev->cmd_complete,
  94. CMD_TIMEOUT_SECONDS * HZ)) {
  95. dev_err(ipcdev->dev, "IPC timed out\n");
  96. return -ETIMEDOUT;
  97. }
  98. } else {
  99. while ((ipc_read_status(ipcdev, type) & CMD_RUN) && --loops)
  100. udelay(1);
  101. if (!loops) {
  102. dev_err(ipcdev->dev, "IPC timed out\n");
  103. return -ETIMEDOUT;
  104. }
  105. }
  106. status = ipc_read_status(ipcdev, type);
  107. errcode = status & CMD_ERRCODE_MASK;
  108. if (errcode) {
  109. dev_err(ipcdev->dev, "IPC failed: %s, IPC_STS=0x%x\n",
  110. ipc_err_string(errcode), status);
  111. return -EIO;
  112. }
  113. return 0;
  114. }
  115. /**
  116. * intel_punit_ipc_simple_command() - Simple IPC command
  117. * @cmd: IPC command code.
  118. * @para1: First 8bit parameter, set 0 if not used.
  119. * @para2: Second 8bit parameter, set 0 if not used.
  120. *
  121. * Send a IPC command to P-Unit when there is no data transaction
  122. *
  123. * Return: IPC error code or 0 on success.
  124. */
  125. int intel_punit_ipc_simple_command(int cmd, int para1, int para2)
  126. {
  127. IPC_DEV *ipcdev = punit_ipcdev;
  128. IPC_TYPE type;
  129. u32 val;
  130. int ret;
  131. mutex_lock(&ipcdev->lock);
  132. reinit_completion(&ipcdev->cmd_complete);
  133. type = (cmd & IPC_PUNIT_CMD_TYPE_MASK) >> IPC_TYPE_OFFSET;
  134. val = cmd & ~IPC_PUNIT_CMD_TYPE_MASK;
  135. val |= CMD_RUN | para2 << CMD_PARA2_SHIFT | para1 << CMD_PARA1_SHIFT;
  136. ipc_write_cmd(ipcdev, type, val);
  137. ret = intel_punit_ipc_check_status(ipcdev, type);
  138. mutex_unlock(&ipcdev->lock);
  139. return ret;
  140. }
  141. EXPORT_SYMBOL(intel_punit_ipc_simple_command);
  142. /**
  143. * intel_punit_ipc_command() - IPC command with data and pointers
  144. * @cmd: IPC command code.
  145. * @para1: First 8bit parameter, set 0 if not used.
  146. * @para2: Second 8bit parameter, set 0 if not used.
  147. * @in: Input data, 32bit for BIOS cmd, two 32bit for GTD and ISPD.
  148. * @out: Output data.
  149. *
  150. * Send a IPC command to P-Unit with data transaction
  151. *
  152. * Return: IPC error code or 0 on success.
  153. */
  154. int intel_punit_ipc_command(u32 cmd, u32 para1, u32 para2, u32 *in, u32 *out)
  155. {
  156. IPC_DEV *ipcdev = punit_ipcdev;
  157. IPC_TYPE type;
  158. u32 val;
  159. int ret;
  160. mutex_lock(&ipcdev->lock);
  161. reinit_completion(&ipcdev->cmd_complete);
  162. type = (cmd & IPC_PUNIT_CMD_TYPE_MASK) >> IPC_TYPE_OFFSET;
  163. if (in) {
  164. ipc_write_data_low(ipcdev, type, *in);
  165. if (type == GTDRIVER_IPC || type == ISPDRIVER_IPC)
  166. ipc_write_data_high(ipcdev, type, *++in);
  167. }
  168. val = cmd & ~IPC_PUNIT_CMD_TYPE_MASK;
  169. val |= CMD_RUN | para2 << CMD_PARA2_SHIFT | para1 << CMD_PARA1_SHIFT;
  170. ipc_write_cmd(ipcdev, type, val);
  171. ret = intel_punit_ipc_check_status(ipcdev, type);
  172. if (ret)
  173. goto out;
  174. if (out) {
  175. *out = ipc_read_data_low(ipcdev, type);
  176. if (type == GTDRIVER_IPC || type == ISPDRIVER_IPC)
  177. *++out = ipc_read_data_high(ipcdev, type);
  178. }
  179. out:
  180. mutex_unlock(&ipcdev->lock);
  181. return ret;
  182. }
  183. EXPORT_SYMBOL_GPL(intel_punit_ipc_command);
  184. static irqreturn_t intel_punit_ioc(int irq, void *dev_id)
  185. {
  186. IPC_DEV *ipcdev = dev_id;
  187. complete(&ipcdev->cmd_complete);
  188. return IRQ_HANDLED;
  189. }
  190. static int intel_punit_get_bars(struct platform_device *pdev)
  191. {
  192. struct resource *res;
  193. void __iomem *addr;
  194. /*
  195. * The following resources are required
  196. * - BIOS_IPC BASE_DATA
  197. * - BIOS_IPC BASE_IFACE
  198. */
  199. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  200. addr = devm_ioremap_resource(&pdev->dev, res);
  201. if (IS_ERR(addr))
  202. return PTR_ERR(addr);
  203. punit_ipcdev->base[BIOS_IPC][BASE_DATA] = addr;
  204. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  205. addr = devm_ioremap_resource(&pdev->dev, res);
  206. if (IS_ERR(addr))
  207. return PTR_ERR(addr);
  208. punit_ipcdev->base[BIOS_IPC][BASE_IFACE] = addr;
  209. /*
  210. * The following resources are optional
  211. * - ISPDRIVER_IPC BASE_DATA
  212. * - ISPDRIVER_IPC BASE_IFACE
  213. * - GTDRIVER_IPC BASE_DATA
  214. * - GTDRIVER_IPC BASE_IFACE
  215. */
  216. res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  217. if (res && resource_size(res) > 1) {
  218. addr = devm_ioremap_resource(&pdev->dev, res);
  219. if (!IS_ERR(addr))
  220. punit_ipcdev->base[ISPDRIVER_IPC][BASE_DATA] = addr;
  221. }
  222. res = platform_get_resource(pdev, IORESOURCE_MEM, 3);
  223. if (res && resource_size(res) > 1) {
  224. addr = devm_ioremap_resource(&pdev->dev, res);
  225. if (!IS_ERR(addr))
  226. punit_ipcdev->base[ISPDRIVER_IPC][BASE_IFACE] = addr;
  227. }
  228. res = platform_get_resource(pdev, IORESOURCE_MEM, 4);
  229. if (res && resource_size(res) > 1) {
  230. addr = devm_ioremap_resource(&pdev->dev, res);
  231. if (!IS_ERR(addr))
  232. punit_ipcdev->base[GTDRIVER_IPC][BASE_DATA] = addr;
  233. }
  234. res = platform_get_resource(pdev, IORESOURCE_MEM, 5);
  235. if (res && resource_size(res) > 1) {
  236. addr = devm_ioremap_resource(&pdev->dev, res);
  237. if (!IS_ERR(addr))
  238. punit_ipcdev->base[GTDRIVER_IPC][BASE_IFACE] = addr;
  239. }
  240. return 0;
  241. }
  242. static int intel_punit_ipc_probe(struct platform_device *pdev)
  243. {
  244. int irq, ret;
  245. punit_ipcdev = devm_kzalloc(&pdev->dev,
  246. sizeof(*punit_ipcdev), GFP_KERNEL);
  247. if (!punit_ipcdev)
  248. return -ENOMEM;
  249. platform_set_drvdata(pdev, punit_ipcdev);
  250. irq = platform_get_irq(pdev, 0);
  251. if (irq < 0) {
  252. punit_ipcdev->irq = 0;
  253. dev_warn(&pdev->dev, "Invalid IRQ, using polling mode\n");
  254. } else {
  255. ret = devm_request_irq(&pdev->dev, irq, intel_punit_ioc,
  256. IRQF_NO_SUSPEND, "intel_punit_ipc",
  257. &punit_ipcdev);
  258. if (ret) {
  259. dev_err(&pdev->dev, "Failed to request irq: %d\n", irq);
  260. return ret;
  261. }
  262. punit_ipcdev->irq = irq;
  263. }
  264. ret = intel_punit_get_bars(pdev);
  265. if (ret)
  266. goto out;
  267. punit_ipcdev->dev = &pdev->dev;
  268. mutex_init(&punit_ipcdev->lock);
  269. init_completion(&punit_ipcdev->cmd_complete);
  270. out:
  271. return ret;
  272. }
  273. static int intel_punit_ipc_remove(struct platform_device *pdev)
  274. {
  275. return 0;
  276. }
  277. static const struct acpi_device_id punit_ipc_acpi_ids[] = {
  278. { "INT34D4", 0 },
  279. { }
  280. };
  281. static struct platform_driver intel_punit_ipc_driver = {
  282. .probe = intel_punit_ipc_probe,
  283. .remove = intel_punit_ipc_remove,
  284. .driver = {
  285. .name = "intel_punit_ipc",
  286. .acpi_match_table = ACPI_PTR(punit_ipc_acpi_ids),
  287. },
  288. };
  289. static int __init intel_punit_ipc_init(void)
  290. {
  291. return platform_driver_register(&intel_punit_ipc_driver);
  292. }
  293. static void __exit intel_punit_ipc_exit(void)
  294. {
  295. platform_driver_unregister(&intel_punit_ipc_driver);
  296. }
  297. MODULE_AUTHOR("Zha Qipeng <qipeng.zha@intel.com>");
  298. MODULE_DESCRIPTION("Intel P-Unit IPC driver");
  299. MODULE_LICENSE("GPL v2");
  300. /* Some modules are dependent on this, so init earlier */
  301. fs_initcall(intel_punit_ipc_init);
  302. module_exit(intel_punit_ipc_exit);