treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / platform / x86 / intel_pmc_ipc.c
blob2433bf73f1eda943c73f7b4dc6e6b04293dceabc
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Driver for the Intel PMC IPC mechanism
5 * (C) Copyright 2014-2015 Intel Corporation
7 * This driver is based on Intel SCU IPC driver(intel_scu_ipc.c) by
8 * Sreedhara DS <sreedhara.ds@intel.com>
10 * PMC running in ARC processor communicates with other entity running in IA
11 * core through IPC mechanism which in turn messaging between IA core ad PMC.
14 #include <linux/acpi.h>
15 #include <linux/delay.h>
16 #include <linux/errno.h>
17 #include <linux/interrupt.h>
18 #include <linux/io-64-nonatomic-lo-hi.h>
19 #include <linux/module.h>
20 #include <linux/pci.h>
21 #include <linux/platform_device.h>
23 #include <asm/intel_pmc_ipc.h>
25 #include <linux/platform_data/itco_wdt.h>
28 * IPC registers
29 * The IA write to IPC_CMD command register triggers an interrupt to the ARC,
30 * The ARC handles the interrupt and services it, writing optional data to
31 * the IPC1 registers, updates the IPC_STS response register with the status.
33 #define IPC_CMD 0x00
34 #define IPC_CMD_MSI BIT(8)
35 #define IPC_CMD_SIZE 16
36 #define IPC_CMD_SUBCMD 12
37 #define IPC_STATUS 0x04
38 #define IPC_STATUS_IRQ BIT(2)
39 #define IPC_STATUS_ERR BIT(1)
40 #define IPC_STATUS_BUSY BIT(0)
41 #define IPC_SPTR 0x08
42 #define IPC_DPTR 0x0C
43 #define IPC_WRITE_BUFFER 0x80
44 #define IPC_READ_BUFFER 0x90
46 /* Residency with clock rate at 19.2MHz to usecs */
47 #define S0IX_RESIDENCY_IN_USECS(d, s) \
48 ({ \
49 u64 result = 10ull * ((d) + (s)); \
50 do_div(result, 192); \
51 result; \
55 * 16-byte buffer for sending data associated with IPC command.
57 #define IPC_DATA_BUFFER_SIZE 16
59 #define IPC_LOOP_CNT 3000000
60 #define IPC_MAX_SEC 3
62 #define IPC_TRIGGER_MODE_IRQ true
64 /* exported resources from IFWI */
65 #define PLAT_RESOURCE_IPC_INDEX 0
66 #define PLAT_RESOURCE_IPC_SIZE 0x1000
67 #define PLAT_RESOURCE_GCR_OFFSET 0x1000
68 #define PLAT_RESOURCE_GCR_SIZE 0x1000
69 #define PLAT_RESOURCE_BIOS_DATA_INDEX 1
70 #define PLAT_RESOURCE_BIOS_IFACE_INDEX 2
71 #define PLAT_RESOURCE_TELEM_SSRAM_INDEX 3
72 #define PLAT_RESOURCE_ISP_DATA_INDEX 4
73 #define PLAT_RESOURCE_ISP_IFACE_INDEX 5
74 #define PLAT_RESOURCE_GTD_DATA_INDEX 6
75 #define PLAT_RESOURCE_GTD_IFACE_INDEX 7
76 #define PLAT_RESOURCE_ACPI_IO_INDEX 0
79 * BIOS does not create an ACPI device for each PMC function,
80 * but exports multiple resources from one ACPI device(IPC) for
81 * multiple functions. This driver is responsible to create a
82 * platform device and to export resources for those functions.
84 #define TCO_DEVICE_NAME "iTCO_wdt"
85 #define SMI_EN_OFFSET 0x40
86 #define SMI_EN_SIZE 4
87 #define TCO_BASE_OFFSET 0x60
88 #define TCO_REGS_SIZE 16
89 #define PUNIT_DEVICE_NAME "intel_punit_ipc"
90 #define TELEMETRY_DEVICE_NAME "intel_telemetry"
91 #define TELEM_SSRAM_SIZE 240
92 #define TELEM_PMC_SSRAM_OFFSET 0x1B00
93 #define TELEM_PUNIT_SSRAM_OFFSET 0x1A00
94 #define TCO_PMC_OFFSET 0x08
95 #define TCO_PMC_SIZE 0x04
97 /* PMC register bit definitions */
99 /* PMC_CFG_REG bit masks */
100 #define PMC_CFG_NO_REBOOT_MASK BIT_MASK(4)
101 #define PMC_CFG_NO_REBOOT_EN (1 << 4)
102 #define PMC_CFG_NO_REBOOT_DIS (0 << 4)
104 static struct intel_pmc_ipc_dev {
105 struct device *dev;
106 void __iomem *ipc_base;
107 bool irq_mode;
108 int irq;
109 int cmd;
110 struct completion cmd_complete;
112 /* The following PMC BARs share the same ACPI device with the IPC */
113 resource_size_t acpi_io_base;
114 int acpi_io_size;
115 struct platform_device *tco_dev;
117 /* gcr */
118 void __iomem *gcr_mem_base;
119 bool has_gcr_regs;
120 spinlock_t gcr_lock;
122 /* punit */
123 struct platform_device *punit_dev;
124 unsigned int punit_res_count;
126 /* Telemetry */
127 resource_size_t telem_pmc_ssram_base;
128 resource_size_t telem_punit_ssram_base;
129 int telem_pmc_ssram_size;
130 int telem_punit_ssram_size;
131 u8 telem_res_inval;
132 struct platform_device *telemetry_dev;
133 } ipcdev;
135 static char *ipc_err_sources[] = {
136 [IPC_ERR_NONE] =
137 "no error",
138 [IPC_ERR_CMD_NOT_SUPPORTED] =
139 "command not supported",
140 [IPC_ERR_CMD_NOT_SERVICED] =
141 "command not serviced",
142 [IPC_ERR_UNABLE_TO_SERVICE] =
143 "unable to service",
144 [IPC_ERR_CMD_INVALID] =
145 "command invalid",
146 [IPC_ERR_CMD_FAILED] =
147 "command failed",
148 [IPC_ERR_EMSECURITY] =
149 "Invalid Battery",
150 [IPC_ERR_UNSIGNEDKERNEL] =
151 "Unsigned kernel",
154 /* Prevent concurrent calls to the PMC */
155 static DEFINE_MUTEX(ipclock);
157 static inline void ipc_send_command(u32 cmd)
159 ipcdev.cmd = cmd;
160 if (ipcdev.irq_mode) {
161 reinit_completion(&ipcdev.cmd_complete);
162 cmd |= IPC_CMD_MSI;
164 writel(cmd, ipcdev.ipc_base + IPC_CMD);
167 static inline u32 ipc_read_status(void)
169 return readl(ipcdev.ipc_base + IPC_STATUS);
172 static inline void ipc_data_writel(u32 data, u32 offset)
174 writel(data, ipcdev.ipc_base + IPC_WRITE_BUFFER + offset);
177 static inline u32 ipc_data_readl(u32 offset)
179 return readl(ipcdev.ipc_base + IPC_READ_BUFFER + offset);
182 static inline u64 gcr_data_readq(u32 offset)
184 return readq(ipcdev.gcr_mem_base + offset);
187 static inline int is_gcr_valid(u32 offset)
189 if (!ipcdev.has_gcr_regs)
190 return -EACCES;
192 if (offset > PLAT_RESOURCE_GCR_SIZE)
193 return -EINVAL;
195 return 0;
199 * intel_pmc_gcr_read64() - Read a 64-bit PMC GCR register
200 * @offset: offset of GCR register from GCR address base
201 * @data: data pointer for storing the register output
203 * Reads the 64-bit PMC GCR register at given offset.
205 * Return: negative value on error or 0 on success.
207 int intel_pmc_gcr_read64(u32 offset, u64 *data)
209 int ret;
211 spin_lock(&ipcdev.gcr_lock);
213 ret = is_gcr_valid(offset);
214 if (ret < 0) {
215 spin_unlock(&ipcdev.gcr_lock);
216 return ret;
219 *data = readq(ipcdev.gcr_mem_base + offset);
221 spin_unlock(&ipcdev.gcr_lock);
223 return 0;
225 EXPORT_SYMBOL_GPL(intel_pmc_gcr_read64);
228 * intel_pmc_gcr_update() - Update PMC GCR register bits
229 * @offset: offset of GCR register from GCR address base
230 * @mask: bit mask for update operation
231 * @val: update value
233 * Updates the bits of given GCR register as specified by
234 * @mask and @val.
236 * Return: negative value on error or 0 on success.
238 static int intel_pmc_gcr_update(u32 offset, u32 mask, u32 val)
240 u32 new_val;
241 int ret = 0;
243 spin_lock(&ipcdev.gcr_lock);
245 ret = is_gcr_valid(offset);
246 if (ret < 0)
247 goto gcr_ipc_unlock;
249 new_val = readl(ipcdev.gcr_mem_base + offset);
251 new_val &= ~mask;
252 new_val |= val & mask;
254 writel(new_val, ipcdev.gcr_mem_base + offset);
256 new_val = readl(ipcdev.gcr_mem_base + offset);
258 /* check whether the bit update is successful */
259 if ((new_val & mask) != (val & mask)) {
260 ret = -EIO;
261 goto gcr_ipc_unlock;
264 gcr_ipc_unlock:
265 spin_unlock(&ipcdev.gcr_lock);
266 return ret;
269 static int update_no_reboot_bit(void *priv, bool set)
271 u32 value = set ? PMC_CFG_NO_REBOOT_EN : PMC_CFG_NO_REBOOT_DIS;
273 return intel_pmc_gcr_update(PMC_GCR_PMC_CFG_REG,
274 PMC_CFG_NO_REBOOT_MASK, value);
277 static int intel_pmc_ipc_check_status(void)
279 int status;
280 int ret = 0;
282 if (ipcdev.irq_mode) {
283 if (0 == wait_for_completion_timeout(
284 &ipcdev.cmd_complete, IPC_MAX_SEC * HZ))
285 ret = -ETIMEDOUT;
286 } else {
287 int loop_count = IPC_LOOP_CNT;
289 while ((ipc_read_status() & IPC_STATUS_BUSY) && --loop_count)
290 udelay(1);
291 if (loop_count == 0)
292 ret = -ETIMEDOUT;
295 status = ipc_read_status();
296 if (ret == -ETIMEDOUT) {
297 dev_err(ipcdev.dev,
298 "IPC timed out, TS=0x%x, CMD=0x%x\n",
299 status, ipcdev.cmd);
300 return ret;
303 if (status & IPC_STATUS_ERR) {
304 int i;
306 ret = -EIO;
307 i = (status >> IPC_CMD_SIZE) & 0xFF;
308 if (i < ARRAY_SIZE(ipc_err_sources))
309 dev_err(ipcdev.dev,
310 "IPC failed: %s, STS=0x%x, CMD=0x%x\n",
311 ipc_err_sources[i], status, ipcdev.cmd);
312 else
313 dev_err(ipcdev.dev,
314 "IPC failed: unknown, STS=0x%x, CMD=0x%x\n",
315 status, ipcdev.cmd);
316 if ((i == IPC_ERR_UNSIGNEDKERNEL) || (i == IPC_ERR_EMSECURITY))
317 ret = -EACCES;
320 return ret;
324 * intel_pmc_ipc_simple_command() - Simple IPC command
325 * @cmd: IPC command code.
326 * @sub: IPC command sub type.
328 * Send a simple IPC command to PMC when don't need to specify
329 * input/output data and source/dest pointers.
331 * Return: an IPC error code or 0 on success.
333 static int intel_pmc_ipc_simple_command(int cmd, int sub)
335 int ret;
337 mutex_lock(&ipclock);
338 if (ipcdev.dev == NULL) {
339 mutex_unlock(&ipclock);
340 return -ENODEV;
342 ipc_send_command(sub << IPC_CMD_SUBCMD | cmd);
343 ret = intel_pmc_ipc_check_status();
344 mutex_unlock(&ipclock);
346 return ret;
350 * intel_pmc_ipc_raw_cmd() - IPC command with data and pointers
351 * @cmd: IPC command code.
352 * @sub: IPC command sub type.
353 * @in: input data of this IPC command.
354 * @inlen: input data length in bytes.
355 * @out: output data of this IPC command.
356 * @outlen: output data length in dwords.
357 * @sptr: data writing to SPTR register.
358 * @dptr: data writing to DPTR register.
360 * Send an IPC command to PMC with input/output data and source/dest pointers.
362 * Return: an IPC error code or 0 on success.
364 static int intel_pmc_ipc_raw_cmd(u32 cmd, u32 sub, u8 *in, u32 inlen, u32 *out,
365 u32 outlen, u32 dptr, u32 sptr)
367 u32 wbuf[4] = { 0 };
368 int ret;
369 int i;
371 if (inlen > IPC_DATA_BUFFER_SIZE || outlen > IPC_DATA_BUFFER_SIZE / 4)
372 return -EINVAL;
374 mutex_lock(&ipclock);
375 if (ipcdev.dev == NULL) {
376 mutex_unlock(&ipclock);
377 return -ENODEV;
379 memcpy(wbuf, in, inlen);
380 writel(dptr, ipcdev.ipc_base + IPC_DPTR);
381 writel(sptr, ipcdev.ipc_base + IPC_SPTR);
382 /* The input data register is 32bit register and inlen is in Byte */
383 for (i = 0; i < ((inlen + 3) / 4); i++)
384 ipc_data_writel(wbuf[i], 4 * i);
385 ipc_send_command((inlen << IPC_CMD_SIZE) |
386 (sub << IPC_CMD_SUBCMD) | cmd);
387 ret = intel_pmc_ipc_check_status();
388 if (!ret) {
389 /* out is read from 32bit register and outlen is in 32bit */
390 for (i = 0; i < outlen; i++)
391 *out++ = ipc_data_readl(4 * i);
393 mutex_unlock(&ipclock);
395 return ret;
399 * intel_pmc_ipc_command() - IPC command with input/output data
400 * @cmd: IPC command code.
401 * @sub: IPC command sub type.
402 * @in: input data of this IPC command.
403 * @inlen: input data length in bytes.
404 * @out: output data of this IPC command.
405 * @outlen: output data length in dwords.
407 * Send an IPC command to PMC with input/output data.
409 * Return: an IPC error code or 0 on success.
411 int intel_pmc_ipc_command(u32 cmd, u32 sub, u8 *in, u32 inlen,
412 u32 *out, u32 outlen)
414 return intel_pmc_ipc_raw_cmd(cmd, sub, in, inlen, out, outlen, 0, 0);
416 EXPORT_SYMBOL_GPL(intel_pmc_ipc_command);
418 static irqreturn_t ioc(int irq, void *dev_id)
420 int status;
422 if (ipcdev.irq_mode) {
423 status = ipc_read_status();
424 writel(status | IPC_STATUS_IRQ, ipcdev.ipc_base + IPC_STATUS);
426 complete(&ipcdev.cmd_complete);
428 return IRQ_HANDLED;
431 static int ipc_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
433 struct intel_pmc_ipc_dev *pmc = &ipcdev;
434 int ret;
436 /* Only one PMC is supported */
437 if (pmc->dev)
438 return -EBUSY;
440 pmc->irq_mode = IPC_TRIGGER_MODE_IRQ;
442 spin_lock_init(&ipcdev.gcr_lock);
444 ret = pcim_enable_device(pdev);
445 if (ret)
446 return ret;
448 ret = pcim_iomap_regions(pdev, 1 << 0, pci_name(pdev));
449 if (ret)
450 return ret;
452 init_completion(&pmc->cmd_complete);
454 pmc->ipc_base = pcim_iomap_table(pdev)[0];
456 ret = devm_request_irq(&pdev->dev, pdev->irq, ioc, 0, "intel_pmc_ipc",
457 pmc);
458 if (ret) {
459 dev_err(&pdev->dev, "Failed to request irq\n");
460 return ret;
463 pmc->dev = &pdev->dev;
465 pci_set_drvdata(pdev, pmc);
467 return 0;
470 static const struct pci_device_id ipc_pci_ids[] = {
471 {PCI_VDEVICE(INTEL, 0x0a94), 0},
472 {PCI_VDEVICE(INTEL, 0x1a94), 0},
473 {PCI_VDEVICE(INTEL, 0x5a94), 0},
474 { 0,}
476 MODULE_DEVICE_TABLE(pci, ipc_pci_ids);
478 static struct pci_driver ipc_pci_driver = {
479 .name = "intel_pmc_ipc",
480 .id_table = ipc_pci_ids,
481 .probe = ipc_pci_probe,
484 static ssize_t intel_pmc_ipc_simple_cmd_store(struct device *dev,
485 struct device_attribute *attr,
486 const char *buf, size_t count)
488 int subcmd;
489 int cmd;
490 int ret;
492 ret = sscanf(buf, "%d %d", &cmd, &subcmd);
493 if (ret != 2) {
494 dev_err(dev, "Error args\n");
495 return -EINVAL;
498 ret = intel_pmc_ipc_simple_command(cmd, subcmd);
499 if (ret) {
500 dev_err(dev, "command %d error with %d\n", cmd, ret);
501 return ret;
503 return (ssize_t)count;
505 static DEVICE_ATTR(simplecmd, 0200, NULL, intel_pmc_ipc_simple_cmd_store);
507 static ssize_t intel_pmc_ipc_northpeak_store(struct device *dev,
508 struct device_attribute *attr,
509 const char *buf, size_t count)
511 unsigned long val;
512 int subcmd;
513 int ret;
515 ret = kstrtoul(buf, 0, &val);
516 if (ret)
517 return ret;
519 if (val)
520 subcmd = 1;
521 else
522 subcmd = 0;
523 ret = intel_pmc_ipc_simple_command(PMC_IPC_NORTHPEAK_CTRL, subcmd);
524 if (ret) {
525 dev_err(dev, "command north %d error with %d\n", subcmd, ret);
526 return ret;
528 return (ssize_t)count;
530 static DEVICE_ATTR(northpeak, 0200, NULL, intel_pmc_ipc_northpeak_store);
532 static struct attribute *intel_ipc_attrs[] = {
533 &dev_attr_northpeak.attr,
534 &dev_attr_simplecmd.attr,
535 NULL
538 static const struct attribute_group intel_ipc_group = {
539 .attrs = intel_ipc_attrs,
542 static const struct attribute_group *intel_ipc_groups[] = {
543 &intel_ipc_group,
544 NULL
547 static struct resource punit_res_array[] = {
548 /* Punit BIOS */
550 .flags = IORESOURCE_MEM,
553 .flags = IORESOURCE_MEM,
555 /* Punit ISP */
557 .flags = IORESOURCE_MEM,
560 .flags = IORESOURCE_MEM,
562 /* Punit GTD */
564 .flags = IORESOURCE_MEM,
567 .flags = IORESOURCE_MEM,
571 #define TCO_RESOURCE_ACPI_IO 0
572 #define TCO_RESOURCE_SMI_EN_IO 1
573 #define TCO_RESOURCE_GCR_MEM 2
574 static struct resource tco_res[] = {
575 /* ACPI - TCO */
577 .flags = IORESOURCE_IO,
579 /* ACPI - SMI */
581 .flags = IORESOURCE_IO,
585 static struct itco_wdt_platform_data tco_info = {
586 .name = "Apollo Lake SoC",
587 .version = 5,
588 .no_reboot_priv = &ipcdev,
589 .update_no_reboot_bit = update_no_reboot_bit,
592 #define TELEMETRY_RESOURCE_PUNIT_SSRAM 0
593 #define TELEMETRY_RESOURCE_PMC_SSRAM 1
594 static struct resource telemetry_res[] = {
595 /*Telemetry*/
597 .flags = IORESOURCE_MEM,
600 .flags = IORESOURCE_MEM,
604 static int ipc_create_punit_device(void)
606 struct platform_device *pdev;
607 const struct platform_device_info pdevinfo = {
608 .parent = ipcdev.dev,
609 .name = PUNIT_DEVICE_NAME,
610 .id = -1,
611 .res = punit_res_array,
612 .num_res = ipcdev.punit_res_count,
615 pdev = platform_device_register_full(&pdevinfo);
616 if (IS_ERR(pdev))
617 return PTR_ERR(pdev);
619 ipcdev.punit_dev = pdev;
621 return 0;
624 static int ipc_create_tco_device(void)
626 struct platform_device *pdev;
627 struct resource *res;
628 const struct platform_device_info pdevinfo = {
629 .parent = ipcdev.dev,
630 .name = TCO_DEVICE_NAME,
631 .id = -1,
632 .res = tco_res,
633 .num_res = ARRAY_SIZE(tco_res),
634 .data = &tco_info,
635 .size_data = sizeof(tco_info),
638 res = tco_res + TCO_RESOURCE_ACPI_IO;
639 res->start = ipcdev.acpi_io_base + TCO_BASE_OFFSET;
640 res->end = res->start + TCO_REGS_SIZE - 1;
642 res = tco_res + TCO_RESOURCE_SMI_EN_IO;
643 res->start = ipcdev.acpi_io_base + SMI_EN_OFFSET;
644 res->end = res->start + SMI_EN_SIZE - 1;
646 pdev = platform_device_register_full(&pdevinfo);
647 if (IS_ERR(pdev))
648 return PTR_ERR(pdev);
650 ipcdev.tco_dev = pdev;
652 return 0;
655 static int ipc_create_telemetry_device(void)
657 struct platform_device *pdev;
658 struct resource *res;
659 const struct platform_device_info pdevinfo = {
660 .parent = ipcdev.dev,
661 .name = TELEMETRY_DEVICE_NAME,
662 .id = -1,
663 .res = telemetry_res,
664 .num_res = ARRAY_SIZE(telemetry_res),
667 res = telemetry_res + TELEMETRY_RESOURCE_PUNIT_SSRAM;
668 res->start = ipcdev.telem_punit_ssram_base;
669 res->end = res->start + ipcdev.telem_punit_ssram_size - 1;
671 res = telemetry_res + TELEMETRY_RESOURCE_PMC_SSRAM;
672 res->start = ipcdev.telem_pmc_ssram_base;
673 res->end = res->start + ipcdev.telem_pmc_ssram_size - 1;
675 pdev = platform_device_register_full(&pdevinfo);
676 if (IS_ERR(pdev))
677 return PTR_ERR(pdev);
679 ipcdev.telemetry_dev = pdev;
681 return 0;
684 static int ipc_create_pmc_devices(void)
686 int ret;
688 /* If we have ACPI based watchdog use that instead */
689 if (!acpi_has_watchdog()) {
690 ret = ipc_create_tco_device();
691 if (ret) {
692 dev_err(ipcdev.dev, "Failed to add tco platform device\n");
693 return ret;
697 ret = ipc_create_punit_device();
698 if (ret) {
699 dev_err(ipcdev.dev, "Failed to add punit platform device\n");
700 platform_device_unregister(ipcdev.tco_dev);
701 return ret;
704 if (!ipcdev.telem_res_inval) {
705 ret = ipc_create_telemetry_device();
706 if (ret) {
707 dev_warn(ipcdev.dev,
708 "Failed to add telemetry platform device\n");
709 platform_device_unregister(ipcdev.punit_dev);
710 platform_device_unregister(ipcdev.tco_dev);
714 return ret;
717 static int ipc_plat_get_res(struct platform_device *pdev)
719 struct resource *res, *punit_res = punit_res_array;
720 void __iomem *addr;
721 int size;
723 res = platform_get_resource(pdev, IORESOURCE_IO,
724 PLAT_RESOURCE_ACPI_IO_INDEX);
725 if (!res) {
726 dev_err(&pdev->dev, "Failed to get io resource\n");
727 return -ENXIO;
729 size = resource_size(res);
730 ipcdev.acpi_io_base = res->start;
731 ipcdev.acpi_io_size = size;
732 dev_info(&pdev->dev, "io res: %pR\n", res);
734 ipcdev.punit_res_count = 0;
736 /* This is index 0 to cover BIOS data register */
737 res = platform_get_resource(pdev, IORESOURCE_MEM,
738 PLAT_RESOURCE_BIOS_DATA_INDEX);
739 if (!res) {
740 dev_err(&pdev->dev, "Failed to get res of punit BIOS data\n");
741 return -ENXIO;
743 punit_res[ipcdev.punit_res_count++] = *res;
744 dev_info(&pdev->dev, "punit BIOS data res: %pR\n", res);
746 /* This is index 1 to cover BIOS interface register */
747 res = platform_get_resource(pdev, IORESOURCE_MEM,
748 PLAT_RESOURCE_BIOS_IFACE_INDEX);
749 if (!res) {
750 dev_err(&pdev->dev, "Failed to get res of punit BIOS iface\n");
751 return -ENXIO;
753 punit_res[ipcdev.punit_res_count++] = *res;
754 dev_info(&pdev->dev, "punit BIOS interface res: %pR\n", res);
756 /* This is index 2 to cover ISP data register, optional */
757 res = platform_get_resource(pdev, IORESOURCE_MEM,
758 PLAT_RESOURCE_ISP_DATA_INDEX);
759 if (res) {
760 punit_res[ipcdev.punit_res_count++] = *res;
761 dev_info(&pdev->dev, "punit ISP data res: %pR\n", res);
764 /* This is index 3 to cover ISP interface register, optional */
765 res = platform_get_resource(pdev, IORESOURCE_MEM,
766 PLAT_RESOURCE_ISP_IFACE_INDEX);
767 if (res) {
768 punit_res[ipcdev.punit_res_count++] = *res;
769 dev_info(&pdev->dev, "punit ISP interface res: %pR\n", res);
772 /* This is index 4 to cover GTD data register, optional */
773 res = platform_get_resource(pdev, IORESOURCE_MEM,
774 PLAT_RESOURCE_GTD_DATA_INDEX);
775 if (res) {
776 punit_res[ipcdev.punit_res_count++] = *res;
777 dev_info(&pdev->dev, "punit GTD data res: %pR\n", res);
780 /* This is index 5 to cover GTD interface register, optional */
781 res = platform_get_resource(pdev, IORESOURCE_MEM,
782 PLAT_RESOURCE_GTD_IFACE_INDEX);
783 if (res) {
784 punit_res[ipcdev.punit_res_count++] = *res;
785 dev_info(&pdev->dev, "punit GTD interface res: %pR\n", res);
788 res = platform_get_resource(pdev, IORESOURCE_MEM,
789 PLAT_RESOURCE_IPC_INDEX);
790 if (!res) {
791 dev_err(&pdev->dev, "Failed to get ipc resource\n");
792 return -ENXIO;
794 size = PLAT_RESOURCE_IPC_SIZE + PLAT_RESOURCE_GCR_SIZE;
795 res->end = res->start + size - 1;
797 addr = devm_ioremap_resource(&pdev->dev, res);
798 if (IS_ERR(addr))
799 return PTR_ERR(addr);
801 ipcdev.ipc_base = addr;
803 ipcdev.gcr_mem_base = addr + PLAT_RESOURCE_GCR_OFFSET;
804 dev_info(&pdev->dev, "ipc res: %pR\n", res);
806 ipcdev.telem_res_inval = 0;
807 res = platform_get_resource(pdev, IORESOURCE_MEM,
808 PLAT_RESOURCE_TELEM_SSRAM_INDEX);
809 if (!res) {
810 dev_err(&pdev->dev, "Failed to get telemetry ssram resource\n");
811 ipcdev.telem_res_inval = 1;
812 } else {
813 ipcdev.telem_punit_ssram_base = res->start +
814 TELEM_PUNIT_SSRAM_OFFSET;
815 ipcdev.telem_punit_ssram_size = TELEM_SSRAM_SIZE;
816 ipcdev.telem_pmc_ssram_base = res->start +
817 TELEM_PMC_SSRAM_OFFSET;
818 ipcdev.telem_pmc_ssram_size = TELEM_SSRAM_SIZE;
819 dev_info(&pdev->dev, "telemetry ssram res: %pR\n", res);
822 return 0;
826 * intel_pmc_s0ix_counter_read() - Read S0ix residency.
827 * @data: Out param that contains current S0ix residency count.
829 * Return: an error code or 0 on success.
831 int intel_pmc_s0ix_counter_read(u64 *data)
833 u64 deep, shlw;
835 if (!ipcdev.has_gcr_regs)
836 return -EACCES;
838 deep = gcr_data_readq(PMC_GCR_TELEM_DEEP_S0IX_REG);
839 shlw = gcr_data_readq(PMC_GCR_TELEM_SHLW_S0IX_REG);
841 *data = S0IX_RESIDENCY_IN_USECS(deep, shlw);
843 return 0;
845 EXPORT_SYMBOL_GPL(intel_pmc_s0ix_counter_read);
847 #ifdef CONFIG_ACPI
848 static const struct acpi_device_id ipc_acpi_ids[] = {
849 { "INT34D2", 0},
852 MODULE_DEVICE_TABLE(acpi, ipc_acpi_ids);
853 #endif
855 static int ipc_plat_probe(struct platform_device *pdev)
857 int ret;
859 ipcdev.dev = &pdev->dev;
860 ipcdev.irq_mode = IPC_TRIGGER_MODE_IRQ;
861 init_completion(&ipcdev.cmd_complete);
862 spin_lock_init(&ipcdev.gcr_lock);
864 ipcdev.irq = platform_get_irq(pdev, 0);
865 if (ipcdev.irq < 0)
866 return -EINVAL;
868 ret = ipc_plat_get_res(pdev);
869 if (ret) {
870 dev_err(&pdev->dev, "Failed to request resource\n");
871 return ret;
874 ret = ipc_create_pmc_devices();
875 if (ret) {
876 dev_err(&pdev->dev, "Failed to create pmc devices\n");
877 return ret;
880 if (devm_request_irq(&pdev->dev, ipcdev.irq, ioc, IRQF_NO_SUSPEND,
881 "intel_pmc_ipc", &ipcdev)) {
882 dev_err(&pdev->dev, "Failed to request irq\n");
883 ret = -EBUSY;
884 goto err_irq;
887 ipcdev.has_gcr_regs = true;
889 return 0;
891 err_irq:
892 platform_device_unregister(ipcdev.tco_dev);
893 platform_device_unregister(ipcdev.punit_dev);
894 platform_device_unregister(ipcdev.telemetry_dev);
896 return ret;
899 static int ipc_plat_remove(struct platform_device *pdev)
901 devm_free_irq(&pdev->dev, ipcdev.irq, &ipcdev);
902 platform_device_unregister(ipcdev.tco_dev);
903 platform_device_unregister(ipcdev.punit_dev);
904 platform_device_unregister(ipcdev.telemetry_dev);
905 ipcdev.dev = NULL;
906 return 0;
909 static struct platform_driver ipc_plat_driver = {
910 .remove = ipc_plat_remove,
911 .probe = ipc_plat_probe,
912 .driver = {
913 .name = "pmc-ipc-plat",
914 .acpi_match_table = ACPI_PTR(ipc_acpi_ids),
915 .dev_groups = intel_ipc_groups,
919 static int __init intel_pmc_ipc_init(void)
921 int ret;
923 ret = platform_driver_register(&ipc_plat_driver);
924 if (ret) {
925 pr_err("Failed to register PMC ipc platform driver\n");
926 return ret;
928 ret = pci_register_driver(&ipc_pci_driver);
929 if (ret) {
930 pr_err("Failed to register PMC ipc pci driver\n");
931 platform_driver_unregister(&ipc_plat_driver);
932 return ret;
934 return ret;
937 static void __exit intel_pmc_ipc_exit(void)
939 pci_unregister_driver(&ipc_pci_driver);
940 platform_driver_unregister(&ipc_plat_driver);
943 MODULE_AUTHOR("Zha Qipeng <qipeng.zha@intel.com>");
944 MODULE_DESCRIPTION("Intel PMC IPC driver");
945 MODULE_LICENSE("GPL v2");
947 /* Some modules are dependent on this, so init earlier */
948 fs_initcall(intel_pmc_ipc_init);
949 module_exit(intel_pmc_ipc_exit);