2 * intel_pmc_ipc.c: Driver for the Intel PMC IPC mechanism
4 * (C) Copyright 2014-2015 Intel Corporation
6 * This driver is based on Intel SCU IPC driver(intel_scu_opc.c) by
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; version 2
14 * PMC running in ARC processor communicates with other entity running in IA
15 * core through IPC mechanism which in turn messaging between IA core ad PMC.
18 #include <linux/module.h>
19 #include <linux/delay.h>
20 #include <linux/errno.h>
21 #include <linux/init.h>
22 #include <linux/device.h>
24 #include <linux/pci.h>
25 #include <linux/platform_device.h>
26 #include <linux/interrupt.h>
27 #include <linux/pm_qos.h>
28 #include <linux/kernel.h>
29 #include <linux/bitops.h>
30 #include <linux/sched.h>
31 #include <linux/atomic.h>
32 #include <linux/notifier.h>
33 #include <linux/suspend.h>
34 #include <linux/acpi.h>
35 #include <asm/intel_pmc_ipc.h>
36 #include <linux/mfd/lpc_ich.h>
40 * The IA write to IPC_CMD command register triggers an interrupt to the ARC,
41 * The ARC handles the interrupt and services it, writing optional data to
42 * the IPC1 registers, updates the IPC_STS response register with the status.
45 #define IPC_CMD_MSI 0x100
46 #define IPC_CMD_SIZE 16
47 #define IPC_CMD_SUBCMD 12
48 #define IPC_STATUS 0x04
49 #define IPC_STATUS_IRQ 0x4
50 #define IPC_STATUS_ERR 0x2
51 #define IPC_STATUS_BUSY 0x1
54 #define IPC_WRITE_BUFFER 0x80
55 #define IPC_READ_BUFFER 0x90
58 * 16-byte buffer for sending data associated with IPC command.
60 #define IPC_DATA_BUFFER_SIZE 16
62 #define IPC_LOOP_CNT 3000000
65 #define IPC_TRIGGER_MODE_IRQ true
67 /* exported resources from IFWI */
68 #define PLAT_RESOURCE_IPC_INDEX 0
69 #define PLAT_RESOURCE_IPC_SIZE 0x1000
70 #define PLAT_RESOURCE_GCR_SIZE 0x1000
71 #define PLAT_RESOURCE_PUNIT_DATA_INDEX 1
72 #define PLAT_RESOURCE_PUNIT_INTER_INDEX 2
73 #define PLAT_RESOURCE_ACPI_IO_INDEX 0
76 * BIOS does not create an ACPI device for each PMC function,
77 * but exports multiple resources from one ACPI device(IPC) for
78 * multiple functions. This driver is responsible to create a
79 * platform device and to export resources for those functions.
81 #define TCO_DEVICE_NAME "iTCO_wdt"
82 #define SMI_EN_OFFSET 0x30
84 #define TCO_BASE_OFFSET 0x60
85 #define TCO_REGS_SIZE 16
86 #define PUNIT_DEVICE_NAME "intel_punit_ipc"
88 static const int iTCO_version = 3;
90 static struct intel_pmc_ipc_dev {
92 void __iomem *ipc_base;
96 struct completion cmd_complete;
98 /* The following PMC BARs share the same ACPI device with the IPC */
99 resource_size_t acpi_io_base;
101 struct platform_device *tco_dev;
104 resource_size_t gcr_base;
108 resource_size_t punit_base;
110 resource_size_t punit_base2;
112 struct platform_device *punit_dev;
115 static char *ipc_err_sources[] = {
118 [IPC_ERR_CMD_NOT_SUPPORTED] =
119 "command not supported",
120 [IPC_ERR_CMD_NOT_SERVICED] =
121 "command not serviced",
122 [IPC_ERR_UNABLE_TO_SERVICE] =
124 [IPC_ERR_CMD_INVALID] =
126 [IPC_ERR_CMD_FAILED] =
128 [IPC_ERR_EMSECURITY] =
130 [IPC_ERR_UNSIGNEDKERNEL] =
134 /* Prevent concurrent calls to the PMC */
135 static DEFINE_MUTEX(ipclock);
137 static inline void ipc_send_command(u32 cmd)
140 if (ipcdev.irq_mode) {
141 reinit_completion(&ipcdev.cmd_complete);
144 writel(cmd, ipcdev.ipc_base + IPC_CMD);
147 static inline u32 ipc_read_status(void)
149 return readl(ipcdev.ipc_base + IPC_STATUS);
152 static inline void ipc_data_writel(u32 data, u32 offset)
154 writel(data, ipcdev.ipc_base + IPC_WRITE_BUFFER + offset);
157 static inline u8 ipc_data_readb(u32 offset)
159 return readb(ipcdev.ipc_base + IPC_READ_BUFFER + offset);
162 static inline u32 ipc_data_readl(u32 offset)
164 return readl(ipcdev.ipc_base + IPC_READ_BUFFER + offset);
167 static int intel_pmc_ipc_check_status(void)
172 if (ipcdev.irq_mode) {
173 if (0 == wait_for_completion_timeout(
174 &ipcdev.cmd_complete, IPC_MAX_SEC * HZ))
177 int loop_count = IPC_LOOP_CNT;
179 while ((ipc_read_status() & IPC_STATUS_BUSY) && --loop_count)
185 status = ipc_read_status();
186 if (ret == -ETIMEDOUT) {
188 "IPC timed out, TS=0x%x, CMD=0x%x\n",
193 if (status & IPC_STATUS_ERR) {
197 i = (status >> IPC_CMD_SIZE) & 0xFF;
198 if (i < ARRAY_SIZE(ipc_err_sources))
200 "IPC failed: %s, STS=0x%x, CMD=0x%x\n",
201 ipc_err_sources[i], status, ipcdev.cmd);
204 "IPC failed: unknown, STS=0x%x, CMD=0x%x\n",
206 if ((i == IPC_ERR_UNSIGNEDKERNEL) || (i == IPC_ERR_EMSECURITY))
214 * intel_pmc_ipc_simple_command() - Simple IPC command
215 * @cmd: IPC command code.
216 * @sub: IPC command sub type.
218 * Send a simple IPC command to PMC when don't need to specify
219 * input/output data and source/dest pointers.
221 * Return: an IPC error code or 0 on success.
223 int intel_pmc_ipc_simple_command(int cmd, int sub)
227 mutex_lock(&ipclock);
228 if (ipcdev.dev == NULL) {
229 mutex_unlock(&ipclock);
232 ipc_send_command(sub << IPC_CMD_SUBCMD | cmd);
233 ret = intel_pmc_ipc_check_status();
234 mutex_unlock(&ipclock);
238 EXPORT_SYMBOL_GPL(intel_pmc_ipc_simple_command);
241 * intel_pmc_ipc_raw_cmd() - IPC command with data and pointers
242 * @cmd: IPC command code.
243 * @sub: IPC command sub type.
244 * @in: input data of this IPC command.
245 * @inlen: input data length in bytes.
246 * @out: output data of this IPC command.
247 * @outlen: output data length in dwords.
248 * @sptr: data writing to SPTR register.
249 * @dptr: data writing to DPTR register.
251 * Send an IPC command to PMC with input/output data and source/dest pointers.
253 * Return: an IPC error code or 0 on success.
255 int intel_pmc_ipc_raw_cmd(u32 cmd, u32 sub, u8 *in, u32 inlen, u32 *out,
256 u32 outlen, u32 dptr, u32 sptr)
262 if (inlen > IPC_DATA_BUFFER_SIZE || outlen > IPC_DATA_BUFFER_SIZE / 4)
265 mutex_lock(&ipclock);
266 if (ipcdev.dev == NULL) {
267 mutex_unlock(&ipclock);
270 memcpy(wbuf, in, inlen);
271 writel(dptr, ipcdev.ipc_base + IPC_DPTR);
272 writel(sptr, ipcdev.ipc_base + IPC_SPTR);
273 /* The input data register is 32bit register and inlen is in Byte */
274 for (i = 0; i < ((inlen + 3) / 4); i++)
275 ipc_data_writel(wbuf[i], 4 * i);
276 ipc_send_command((inlen << IPC_CMD_SIZE) |
277 (sub << IPC_CMD_SUBCMD) | cmd);
278 ret = intel_pmc_ipc_check_status();
280 /* out is read from 32bit register and outlen is in 32bit */
281 for (i = 0; i < outlen; i++)
282 *out++ = ipc_data_readl(4 * i);
284 mutex_unlock(&ipclock);
288 EXPORT_SYMBOL_GPL(intel_pmc_ipc_raw_cmd);
291 * intel_pmc_ipc_command() - IPC command with input/output data
292 * @cmd: IPC command code.
293 * @sub: IPC command sub type.
294 * @in: input data of this IPC command.
295 * @inlen: input data length in bytes.
296 * @out: output data of this IPC command.
297 * @outlen: output data length in dwords.
299 * Send an IPC command to PMC with input/output data.
301 * Return: an IPC error code or 0 on success.
303 int intel_pmc_ipc_command(u32 cmd, u32 sub, u8 *in, u32 inlen,
304 u32 *out, u32 outlen)
306 return intel_pmc_ipc_raw_cmd(cmd, sub, in, inlen, out, outlen, 0, 0);
308 EXPORT_SYMBOL_GPL(intel_pmc_ipc_command);
310 static irqreturn_t ioc(int irq, void *dev_id)
314 if (ipcdev.irq_mode) {
315 status = ipc_read_status();
316 writel(status | IPC_STATUS_IRQ, ipcdev.ipc_base + IPC_STATUS);
318 complete(&ipcdev.cmd_complete);
323 static int ipc_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
325 resource_size_t pci_resource;
329 ipcdev.dev = &pci_dev_get(pdev)->dev;
330 ipcdev.irq_mode = IPC_TRIGGER_MODE_IRQ;
332 ret = pci_enable_device(pdev);
336 ret = pci_request_regions(pdev, "intel_pmc_ipc");
340 pci_resource = pci_resource_start(pdev, 0);
341 len = pci_resource_len(pdev, 0);
342 if (!pci_resource || !len) {
343 dev_err(&pdev->dev, "Failed to get resource\n");
347 init_completion(&ipcdev.cmd_complete);
349 if (request_irq(pdev->irq, ioc, 0, "intel_pmc_ipc", &ipcdev)) {
350 dev_err(&pdev->dev, "Failed to request irq\n");
354 ipcdev.ipc_base = ioremap_nocache(pci_resource, len);
355 if (!ipcdev.ipc_base) {
356 dev_err(&pdev->dev, "Failed to ioremap ipc base\n");
357 free_irq(pdev->irq, &ipcdev);
364 static void ipc_pci_remove(struct pci_dev *pdev)
366 free_irq(pdev->irq, &ipcdev);
367 pci_release_regions(pdev);
369 iounmap(ipcdev.ipc_base);
373 static const struct pci_device_id ipc_pci_ids[] = {
374 {PCI_VDEVICE(INTEL, 0x0a94), 0},
375 {PCI_VDEVICE(INTEL, 0x1a94), 0},
378 MODULE_DEVICE_TABLE(pci, ipc_pci_ids);
380 static struct pci_driver ipc_pci_driver = {
381 .name = "intel_pmc_ipc",
382 .id_table = ipc_pci_ids,
383 .probe = ipc_pci_probe,
384 .remove = ipc_pci_remove,
387 static ssize_t intel_pmc_ipc_simple_cmd_store(struct device *dev,
388 struct device_attribute *attr,
389 const char *buf, size_t count)
395 ret = sscanf(buf, "%d %d", &cmd, &subcmd);
397 dev_err(dev, "Error args\n");
401 ret = intel_pmc_ipc_simple_command(cmd, subcmd);
403 dev_err(dev, "command %d error with %d\n", cmd, ret);
406 return (ssize_t)count;
409 static ssize_t intel_pmc_ipc_northpeak_store(struct device *dev,
410 struct device_attribute *attr,
411 const char *buf, size_t count)
417 if (kstrtoul(buf, 0, &val))
424 ret = intel_pmc_ipc_simple_command(PMC_IPC_NORTHPEAK_CTRL, subcmd);
426 dev_err(dev, "command north %d error with %d\n", subcmd, ret);
429 return (ssize_t)count;
432 static DEVICE_ATTR(simplecmd, S_IWUSR,
433 NULL, intel_pmc_ipc_simple_cmd_store);
434 static DEVICE_ATTR(northpeak, S_IWUSR,
435 NULL, intel_pmc_ipc_northpeak_store);
437 static struct attribute *intel_ipc_attrs[] = {
438 &dev_attr_northpeak.attr,
439 &dev_attr_simplecmd.attr,
443 static const struct attribute_group intel_ipc_group = {
444 .attrs = intel_ipc_attrs,
447 #define PUNIT_RESOURCE_INTER 1
448 static struct resource punit_res[] = {
451 .flags = IORESOURCE_MEM,
454 .flags = IORESOURCE_MEM,
458 #define TCO_RESOURCE_ACPI_IO 0
459 #define TCO_RESOURCE_SMI_EN_IO 1
460 #define TCO_RESOURCE_GCR_MEM 2
461 static struct resource tco_res[] = {
464 .flags = IORESOURCE_IO,
468 .flags = IORESOURCE_IO,
472 .flags = IORESOURCE_MEM,
476 static struct lpc_ich_info tco_info = {
477 .name = "Apollo Lake SoC",
481 static int ipc_create_punit_device(void)
483 struct platform_device *pdev;
484 struct resource *res;
487 pdev = platform_device_alloc(PUNIT_DEVICE_NAME, -1);
489 dev_err(ipcdev.dev, "Failed to alloc punit platform device\n");
493 pdev->dev.parent = ipcdev.dev;
496 res->start = ipcdev.punit_base;
497 res->end = res->start + ipcdev.punit_size - 1;
499 res = punit_res + PUNIT_RESOURCE_INTER;
500 res->start = ipcdev.punit_base2;
501 res->end = res->start + ipcdev.punit_size2 - 1;
503 ret = platform_device_add_resources(pdev, punit_res,
504 ARRAY_SIZE(punit_res));
506 dev_err(ipcdev.dev, "Failed to add platform punit resources\n");
510 ret = platform_device_add(pdev);
512 dev_err(ipcdev.dev, "Failed to add punit platform device\n");
515 ipcdev.punit_dev = pdev;
519 platform_device_put(pdev);
523 static int ipc_create_tco_device(void)
525 struct platform_device *pdev;
526 struct resource *res;
529 pdev = platform_device_alloc(TCO_DEVICE_NAME, -1);
531 dev_err(ipcdev.dev, "Failed to alloc tco platform device\n");
535 pdev->dev.parent = ipcdev.dev;
537 res = tco_res + TCO_RESOURCE_ACPI_IO;
538 res->start = ipcdev.acpi_io_base + TCO_BASE_OFFSET;
539 res->end = res->start + TCO_REGS_SIZE - 1;
541 res = tco_res + TCO_RESOURCE_SMI_EN_IO;
542 res->start = ipcdev.acpi_io_base + SMI_EN_OFFSET;
543 res->end = res->start + SMI_EN_SIZE - 1;
545 res = tco_res + TCO_RESOURCE_GCR_MEM;
546 res->start = ipcdev.gcr_base;
547 res->end = res->start + ipcdev.gcr_size - 1;
549 ret = platform_device_add_resources(pdev, tco_res, ARRAY_SIZE(tco_res));
551 dev_err(ipcdev.dev, "Failed to add tco platform resources\n");
555 ret = platform_device_add_data(pdev, &tco_info,
556 sizeof(struct lpc_ich_info));
558 dev_err(ipcdev.dev, "Failed to add tco platform data\n");
562 ret = platform_device_add(pdev);
564 dev_err(ipcdev.dev, "Failed to add tco platform device\n");
567 ipcdev.tco_dev = pdev;
571 platform_device_put(pdev);
575 static int ipc_create_pmc_devices(void)
579 ret = ipc_create_tco_device();
581 dev_err(ipcdev.dev, "Failed to add tco platform device\n");
584 ret = ipc_create_punit_device();
586 dev_err(ipcdev.dev, "Failed to add punit platform device\n");
587 platform_device_unregister(ipcdev.tco_dev);
592 static int ipc_plat_get_res(struct platform_device *pdev)
594 struct resource *res;
598 res = platform_get_resource(pdev, IORESOURCE_IO,
599 PLAT_RESOURCE_ACPI_IO_INDEX);
601 dev_err(&pdev->dev, "Failed to get io resource\n");
604 size = resource_size(res);
605 ipcdev.acpi_io_base = res->start;
606 ipcdev.acpi_io_size = size;
607 dev_info(&pdev->dev, "io res: %llx %x\n",
608 (long long)res->start, (int)resource_size(res));
610 res = platform_get_resource(pdev, IORESOURCE_MEM,
611 PLAT_RESOURCE_PUNIT_DATA_INDEX);
613 dev_err(&pdev->dev, "Failed to get punit resource\n");
616 size = resource_size(res);
617 ipcdev.punit_base = res->start;
618 ipcdev.punit_size = size;
619 dev_info(&pdev->dev, "punit data res: %llx %x\n",
620 (long long)res->start, (int)resource_size(res));
622 res = platform_get_resource(pdev, IORESOURCE_MEM,
623 PLAT_RESOURCE_PUNIT_INTER_INDEX);
625 dev_err(&pdev->dev, "Failed to get punit inter resource\n");
628 size = resource_size(res);
629 ipcdev.punit_base2 = res->start;
630 ipcdev.punit_size2 = size;
631 dev_info(&pdev->dev, "punit interface res: %llx %x\n",
632 (long long)res->start, (int)resource_size(res));
634 res = platform_get_resource(pdev, IORESOURCE_MEM,
635 PLAT_RESOURCE_IPC_INDEX);
637 dev_err(&pdev->dev, "Failed to get ipc resource\n");
640 size = PLAT_RESOURCE_IPC_SIZE;
641 if (!request_mem_region(res->start, size, pdev->name)) {
642 dev_err(&pdev->dev, "Failed to request ipc resource\n");
645 addr = ioremap_nocache(res->start, size);
647 dev_err(&pdev->dev, "I/O memory remapping failed\n");
648 release_mem_region(res->start, size);
651 ipcdev.ipc_base = addr;
653 ipcdev.gcr_base = res->start + size;
654 ipcdev.gcr_size = PLAT_RESOURCE_GCR_SIZE;
655 dev_info(&pdev->dev, "ipc res: %llx %x\n",
656 (long long)res->start, (int)resource_size(res));
662 static const struct acpi_device_id ipc_acpi_ids[] = {
666 MODULE_DEVICE_TABLE(acpi, ipc_acpi_ids);
669 static int ipc_plat_probe(struct platform_device *pdev)
671 struct resource *res;
674 ipcdev.dev = &pdev->dev;
675 ipcdev.irq_mode = IPC_TRIGGER_MODE_IRQ;
676 init_completion(&ipcdev.cmd_complete);
678 ipcdev.irq = platform_get_irq(pdev, 0);
679 if (ipcdev.irq < 0) {
680 dev_err(&pdev->dev, "Failed to get irq\n");
684 ret = ipc_plat_get_res(pdev);
686 dev_err(&pdev->dev, "Failed to request resource\n");
690 ret = ipc_create_pmc_devices();
692 dev_err(&pdev->dev, "Failed to create pmc devices\n");
696 if (request_irq(ipcdev.irq, ioc, 0, "intel_pmc_ipc", &ipcdev)) {
697 dev_err(&pdev->dev, "Failed to request irq\n");
702 ret = sysfs_create_group(&pdev->dev.kobj, &intel_ipc_group);
704 dev_err(&pdev->dev, "Failed to create sysfs group %d\n",
711 free_irq(ipcdev.irq, &ipcdev);
713 platform_device_unregister(ipcdev.tco_dev);
714 platform_device_unregister(ipcdev.punit_dev);
716 iounmap(ipcdev.ipc_base);
717 res = platform_get_resource(pdev, IORESOURCE_MEM,
718 PLAT_RESOURCE_IPC_INDEX);
720 release_mem_region(res->start, PLAT_RESOURCE_IPC_SIZE);
724 static int ipc_plat_remove(struct platform_device *pdev)
726 struct resource *res;
728 sysfs_remove_group(&pdev->dev.kobj, &intel_ipc_group);
729 free_irq(ipcdev.irq, &ipcdev);
730 platform_device_unregister(ipcdev.tco_dev);
731 platform_device_unregister(ipcdev.punit_dev);
732 iounmap(ipcdev.ipc_base);
733 res = platform_get_resource(pdev, IORESOURCE_MEM,
734 PLAT_RESOURCE_IPC_INDEX);
736 release_mem_region(res->start, PLAT_RESOURCE_IPC_SIZE);
741 static struct platform_driver ipc_plat_driver = {
742 .remove = ipc_plat_remove,
743 .probe = ipc_plat_probe,
745 .name = "pmc-ipc-plat",
746 .acpi_match_table = ACPI_PTR(ipc_acpi_ids),
750 static int __init intel_pmc_ipc_init(void)
754 ret = platform_driver_register(&ipc_plat_driver);
756 pr_err("Failed to register PMC ipc platform driver\n");
759 ret = pci_register_driver(&ipc_pci_driver);
761 pr_err("Failed to register PMC ipc pci driver\n");
762 platform_driver_unregister(&ipc_plat_driver);
768 static void __exit intel_pmc_ipc_exit(void)
770 pci_unregister_driver(&ipc_pci_driver);
771 platform_driver_unregister(&ipc_plat_driver);
775 MODULE_DESCRIPTION("Intel PMC IPC driver");
776 MODULE_LICENSE("GPL");
778 /* Some modules are dependent on this, so init earlier */
779 fs_initcall(intel_pmc_ipc_init);
780 module_exit(intel_pmc_ipc_exit);