]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/platform/x86/intel_pmc_ipc.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux.git] / drivers / platform / x86 / intel_pmc_ipc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Driver for the Intel PMC IPC mechanism
4  *
5  * (C) Copyright 2014-2015 Intel Corporation
6  *
7  * This driver is based on Intel SCU IPC driver(intel_scu_ipc.c) by
8  *     Sreedhara DS <sreedhara.ds@intel.com>
9  *
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.
12  */
13
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>
22
23 #include <asm/intel_pmc_ipc.h>
24
25 #include <linux/platform_data/itco_wdt.h>
26
27 /*
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.
32  */
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
45
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;                                 \
52 })
53
54 /*
55  * 16-byte buffer for sending data associated with IPC command.
56  */
57 #define IPC_DATA_BUFFER_SIZE    16
58
59 #define IPC_LOOP_CNT            3000000
60 #define IPC_MAX_SEC             3
61
62 #define IPC_TRIGGER_MODE_IRQ            true
63
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
77
78 /*
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.
83  */
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
96
97 /* PMC register bit definitions */
98
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)
103
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;
111
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;
116
117         /* gcr */
118         void __iomem *gcr_mem_base;
119         bool has_gcr_regs;
120         spinlock_t gcr_lock;
121
122         /* punit */
123         struct platform_device *punit_dev;
124         unsigned int punit_res_count;
125
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;
134
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",
152 };
153
154 /* Prevent concurrent calls to the PMC */
155 static DEFINE_MUTEX(ipclock);
156
157 static inline void ipc_send_command(u32 cmd)
158 {
159         ipcdev.cmd = cmd;
160         if (ipcdev.irq_mode) {
161                 reinit_completion(&ipcdev.cmd_complete);
162                 cmd |= IPC_CMD_MSI;
163         }
164         writel(cmd, ipcdev.ipc_base + IPC_CMD);
165 }
166
167 static inline u32 ipc_read_status(void)
168 {
169         return readl(ipcdev.ipc_base + IPC_STATUS);
170 }
171
172 static inline void ipc_data_writel(u32 data, u32 offset)
173 {
174         writel(data, ipcdev.ipc_base + IPC_WRITE_BUFFER + offset);
175 }
176
177 static inline u32 ipc_data_readl(u32 offset)
178 {
179         return readl(ipcdev.ipc_base + IPC_READ_BUFFER + offset);
180 }
181
182 static inline u64 gcr_data_readq(u32 offset)
183 {
184         return readq(ipcdev.gcr_mem_base + offset);
185 }
186
187 static inline int is_gcr_valid(u32 offset)
188 {
189         if (!ipcdev.has_gcr_regs)
190                 return -EACCES;
191
192         if (offset > PLAT_RESOURCE_GCR_SIZE)
193                 return -EINVAL;
194
195         return 0;
196 }
197
198 /**
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
202  *
203  * Reads the 64-bit PMC GCR register at given offset.
204  *
205  * Return:      negative value on error or 0 on success.
206  */
207 int intel_pmc_gcr_read64(u32 offset, u64 *data)
208 {
209         int ret;
210
211         spin_lock(&ipcdev.gcr_lock);
212
213         ret = is_gcr_valid(offset);
214         if (ret < 0) {
215                 spin_unlock(&ipcdev.gcr_lock);
216                 return ret;
217         }
218
219         *data = readq(ipcdev.gcr_mem_base + offset);
220
221         spin_unlock(&ipcdev.gcr_lock);
222
223         return 0;
224 }
225 EXPORT_SYMBOL_GPL(intel_pmc_gcr_read64);
226
227 /**
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
232  *
233  * Updates the bits of given GCR register as specified by
234  * @mask and @val.
235  *
236  * Return:      negative value on error or 0 on success.
237  */
238 static int intel_pmc_gcr_update(u32 offset, u32 mask, u32 val)
239 {
240         u32 new_val;
241         int ret = 0;
242
243         spin_lock(&ipcdev.gcr_lock);
244
245         ret = is_gcr_valid(offset);
246         if (ret < 0)
247                 goto gcr_ipc_unlock;
248
249         new_val = readl(ipcdev.gcr_mem_base + offset);
250
251         new_val &= ~mask;
252         new_val |= val & mask;
253
254         writel(new_val, ipcdev.gcr_mem_base + offset);
255
256         new_val = readl(ipcdev.gcr_mem_base + offset);
257
258         /* check whether the bit update is successful */
259         if ((new_val & mask) != (val & mask)) {
260                 ret = -EIO;
261                 goto gcr_ipc_unlock;
262         }
263
264 gcr_ipc_unlock:
265         spin_unlock(&ipcdev.gcr_lock);
266         return ret;
267 }
268
269 static int update_no_reboot_bit(void *priv, bool set)
270 {
271         u32 value = set ? PMC_CFG_NO_REBOOT_EN : PMC_CFG_NO_REBOOT_DIS;
272
273         return intel_pmc_gcr_update(PMC_GCR_PMC_CFG_REG,
274                                     PMC_CFG_NO_REBOOT_MASK, value);
275 }
276
277 static int intel_pmc_ipc_check_status(void)
278 {
279         int status;
280         int ret = 0;
281
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;
288
289                 while ((ipc_read_status() & IPC_STATUS_BUSY) && --loop_count)
290                         udelay(1);
291                 if (loop_count == 0)
292                         ret = -ETIMEDOUT;
293         }
294
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;
301         }
302
303         if (status & IPC_STATUS_ERR) {
304                 int i;
305
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;
318         }
319
320         return ret;
321 }
322
323 /**
324  * intel_pmc_ipc_simple_command() - Simple IPC command
325  * @cmd:        IPC command code.
326  * @sub:        IPC command sub type.
327  *
328  * Send a simple IPC command to PMC when don't need to specify
329  * input/output data and source/dest pointers.
330  *
331  * Return:      an IPC error code or 0 on success.
332  */
333 static int intel_pmc_ipc_simple_command(int cmd, int sub)
334 {
335         int ret;
336
337         mutex_lock(&ipclock);
338         if (ipcdev.dev == NULL) {
339                 mutex_unlock(&ipclock);
340                 return -ENODEV;
341         }
342         ipc_send_command(sub << IPC_CMD_SUBCMD | cmd);
343         ret = intel_pmc_ipc_check_status();
344         mutex_unlock(&ipclock);
345
346         return ret;
347 }
348
349 /**
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.
359  *
360  * Send an IPC command to PMC with input/output data and source/dest pointers.
361  *
362  * Return:      an IPC error code or 0 on success.
363  */
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)
366 {
367         u32 wbuf[4] = { 0 };
368         int ret;
369         int i;
370
371         if (inlen > IPC_DATA_BUFFER_SIZE || outlen > IPC_DATA_BUFFER_SIZE / 4)
372                 return -EINVAL;
373
374         mutex_lock(&ipclock);
375         if (ipcdev.dev == NULL) {
376                 mutex_unlock(&ipclock);
377                 return -ENODEV;
378         }
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);
392         }
393         mutex_unlock(&ipclock);
394
395         return ret;
396 }
397
398 /**
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.
406  *
407  * Send an IPC command to PMC with input/output data.
408  *
409  * Return:      an IPC error code or 0 on success.
410  */
411 int intel_pmc_ipc_command(u32 cmd, u32 sub, u8 *in, u32 inlen,
412                           u32 *out, u32 outlen)
413 {
414         return intel_pmc_ipc_raw_cmd(cmd, sub, in, inlen, out, outlen, 0, 0);
415 }
416 EXPORT_SYMBOL_GPL(intel_pmc_ipc_command);
417
418 static irqreturn_t ioc(int irq, void *dev_id)
419 {
420         int status;
421
422         if (ipcdev.irq_mode) {
423                 status = ipc_read_status();
424                 writel(status | IPC_STATUS_IRQ, ipcdev.ipc_base + IPC_STATUS);
425         }
426         complete(&ipcdev.cmd_complete);
427
428         return IRQ_HANDLED;
429 }
430
431 static int ipc_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
432 {
433         struct intel_pmc_ipc_dev *pmc = &ipcdev;
434         int ret;
435
436         /* Only one PMC is supported */
437         if (pmc->dev)
438                 return -EBUSY;
439
440         pmc->irq_mode = IPC_TRIGGER_MODE_IRQ;
441
442         spin_lock_init(&ipcdev.gcr_lock);
443
444         ret = pcim_enable_device(pdev);
445         if (ret)
446                 return ret;
447
448         ret = pcim_iomap_regions(pdev, 1 << 0, pci_name(pdev));
449         if (ret)
450                 return ret;
451
452         init_completion(&pmc->cmd_complete);
453
454         pmc->ipc_base = pcim_iomap_table(pdev)[0];
455
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;
461         }
462
463         pmc->dev = &pdev->dev;
464
465         pci_set_drvdata(pdev, pmc);
466
467         return 0;
468 }
469
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,}
475 };
476 MODULE_DEVICE_TABLE(pci, ipc_pci_ids);
477
478 static struct pci_driver ipc_pci_driver = {
479         .name = "intel_pmc_ipc",
480         .id_table = ipc_pci_ids,
481         .probe = ipc_pci_probe,
482 };
483
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)
487 {
488         int subcmd;
489         int cmd;
490         int ret;
491
492         ret = sscanf(buf, "%d %d", &cmd, &subcmd);
493         if (ret != 2) {
494                 dev_err(dev, "Error args\n");
495                 return -EINVAL;
496         }
497
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;
502         }
503         return (ssize_t)count;
504 }
505 static DEVICE_ATTR(simplecmd, 0200, NULL, intel_pmc_ipc_simple_cmd_store);
506
507 static ssize_t intel_pmc_ipc_northpeak_store(struct device *dev,
508                                              struct device_attribute *attr,
509                                              const char *buf, size_t count)
510 {
511         unsigned long val;
512         int subcmd;
513         int ret;
514
515         ret = kstrtoul(buf, 0, &val);
516         if (ret)
517                 return ret;
518
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;
527         }
528         return (ssize_t)count;
529 }
530 static DEVICE_ATTR(northpeak, 0200, NULL, intel_pmc_ipc_northpeak_store);
531
532 static struct attribute *intel_ipc_attrs[] = {
533         &dev_attr_northpeak.attr,
534         &dev_attr_simplecmd.attr,
535         NULL
536 };
537
538 static const struct attribute_group intel_ipc_group = {
539         .attrs = intel_ipc_attrs,
540 };
541
542 static const struct attribute_group *intel_ipc_groups[] = {
543         &intel_ipc_group,
544         NULL
545 };
546
547 static struct resource punit_res_array[] = {
548         /* Punit BIOS */
549         {
550                 .flags = IORESOURCE_MEM,
551         },
552         {
553                 .flags = IORESOURCE_MEM,
554         },
555         /* Punit ISP */
556         {
557                 .flags = IORESOURCE_MEM,
558         },
559         {
560                 .flags = IORESOURCE_MEM,
561         },
562         /* Punit GTD */
563         {
564                 .flags = IORESOURCE_MEM,
565         },
566         {
567                 .flags = IORESOURCE_MEM,
568         },
569 };
570
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 */
576         {
577                 .flags = IORESOURCE_IO,
578         },
579         /* ACPI - SMI */
580         {
581                 .flags = IORESOURCE_IO,
582         },
583 };
584
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,
590 };
591
592 #define TELEMETRY_RESOURCE_PUNIT_SSRAM  0
593 #define TELEMETRY_RESOURCE_PMC_SSRAM    1
594 static struct resource telemetry_res[] = {
595         /*Telemetry*/
596         {
597                 .flags = IORESOURCE_MEM,
598         },
599         {
600                 .flags = IORESOURCE_MEM,
601         },
602 };
603
604 static int ipc_create_punit_device(void)
605 {
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,
613                 };
614
615         pdev = platform_device_register_full(&pdevinfo);
616         if (IS_ERR(pdev))
617                 return PTR_ERR(pdev);
618
619         ipcdev.punit_dev = pdev;
620
621         return 0;
622 }
623
624 static int ipc_create_tco_device(void)
625 {
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),
636                 };
637
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;
641
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;
645
646         pdev = platform_device_register_full(&pdevinfo);
647         if (IS_ERR(pdev))
648                 return PTR_ERR(pdev);
649
650         ipcdev.tco_dev = pdev;
651
652         return 0;
653 }
654
655 static int ipc_create_telemetry_device(void)
656 {
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),
665                 };
666
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;
670
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;
674
675         pdev = platform_device_register_full(&pdevinfo);
676         if (IS_ERR(pdev))
677                 return PTR_ERR(pdev);
678
679         ipcdev.telemetry_dev = pdev;
680
681         return 0;
682 }
683
684 static int ipc_create_pmc_devices(void)
685 {
686         int ret;
687
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;
694                 }
695         }
696
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;
702         }
703
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);
711                 }
712         }
713
714         return ret;
715 }
716
717 static int ipc_plat_get_res(struct platform_device *pdev)
718 {
719         struct resource *res, *punit_res = punit_res_array;
720         void __iomem *addr;
721         int size;
722
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;
728         }
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);
733
734         ipcdev.punit_res_count = 0;
735
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;
742         }
743         punit_res[ipcdev.punit_res_count++] = *res;
744         dev_info(&pdev->dev, "punit BIOS data res: %pR\n", res);
745
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;
752         }
753         punit_res[ipcdev.punit_res_count++] = *res;
754         dev_info(&pdev->dev, "punit BIOS interface res: %pR\n", res);
755
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);
762         }
763
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);
770         }
771
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);
778         }
779
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);
786         }
787
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;
793         }
794         size = PLAT_RESOURCE_IPC_SIZE + PLAT_RESOURCE_GCR_SIZE;
795         res->end = res->start + size - 1;
796
797         addr = devm_ioremap_resource(&pdev->dev, res);
798         if (IS_ERR(addr))
799                 return PTR_ERR(addr);
800
801         ipcdev.ipc_base = addr;
802
803         ipcdev.gcr_mem_base = addr + PLAT_RESOURCE_GCR_OFFSET;
804         dev_info(&pdev->dev, "ipc res: %pR\n", res);
805
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);
820         }
821
822         return 0;
823 }
824
825 /**
826  * intel_pmc_s0ix_counter_read() - Read S0ix residency.
827  * @data: Out param that contains current S0ix residency count.
828  *
829  * Return: an error code or 0 on success.
830  */
831 int intel_pmc_s0ix_counter_read(u64 *data)
832 {
833         u64 deep, shlw;
834
835         if (!ipcdev.has_gcr_regs)
836                 return -EACCES;
837
838         deep = gcr_data_readq(PMC_GCR_TELEM_DEEP_S0IX_REG);
839         shlw = gcr_data_readq(PMC_GCR_TELEM_SHLW_S0IX_REG);
840
841         *data = S0IX_RESIDENCY_IN_USECS(deep, shlw);
842
843         return 0;
844 }
845 EXPORT_SYMBOL_GPL(intel_pmc_s0ix_counter_read);
846
847 #ifdef CONFIG_ACPI
848 static const struct acpi_device_id ipc_acpi_ids[] = {
849         { "INT34D2", 0},
850         { }
851 };
852 MODULE_DEVICE_TABLE(acpi, ipc_acpi_ids);
853 #endif
854
855 static int ipc_plat_probe(struct platform_device *pdev)
856 {
857         int ret;
858
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);
863
864         ipcdev.irq = platform_get_irq(pdev, 0);
865         if (ipcdev.irq < 0)
866                 return -EINVAL;
867
868         ret = ipc_plat_get_res(pdev);
869         if (ret) {
870                 dev_err(&pdev->dev, "Failed to request resource\n");
871                 return ret;
872         }
873
874         ret = ipc_create_pmc_devices();
875         if (ret) {
876                 dev_err(&pdev->dev, "Failed to create pmc devices\n");
877                 return ret;
878         }
879
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;
885         }
886
887         ipcdev.has_gcr_regs = true;
888
889         return 0;
890
891 err_irq:
892         platform_device_unregister(ipcdev.tco_dev);
893         platform_device_unregister(ipcdev.punit_dev);
894         platform_device_unregister(ipcdev.telemetry_dev);
895
896         return ret;
897 }
898
899 static int ipc_plat_remove(struct platform_device *pdev)
900 {
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;
907 }
908
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,
916         },
917 };
918
919 static int __init intel_pmc_ipc_init(void)
920 {
921         int ret;
922
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;
927         }
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;
933         }
934         return ret;
935 }
936
937 static void __exit intel_pmc_ipc_exit(void)
938 {
939         pci_unregister_driver(&ipc_pci_driver);
940         platform_driver_unregister(&ipc_plat_driver);
941 }
942
943 MODULE_AUTHOR("Zha Qipeng <qipeng.zha@intel.com>");
944 MODULE_DESCRIPTION("Intel PMC IPC driver");
945 MODULE_LICENSE("GPL v2");
946
947 /* Some modules are dependent on this, so init earlier */
948 fs_initcall(intel_pmc_ipc_init);
949 module_exit(intel_pmc_ipc_exit);