Merge tag 'armsoc-fixes-nc' of git://git.kernel.org/pub/scm/linux/kernel/git/arm...
[linux-2.6-block.git] / drivers / platform / x86 / intel_pmc_ipc.c
1 /*
2  * intel_pmc_ipc.c: Driver for the Intel PMC IPC mechanism
3  *
4  * (C) Copyright 2014-2015 Intel Corporation
5  *
6  * This driver is based on Intel SCU IPC driver(intel_scu_opc.c) by
7  *     Sreedhara DS <sreedhara.ds@intel.com>
8  *
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
12  * of the License.
13  *
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.
16  */
17
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>
23 #include <linux/pm.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/platform_data/itco_wdt.h>
37
38 /*
39  * IPC registers
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.
43  */
44 #define IPC_CMD                 0x0
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
52 #define IPC_SPTR                0x08
53 #define IPC_DPTR                0x0C
54 #define IPC_WRITE_BUFFER        0x80
55 #define IPC_READ_BUFFER         0x90
56
57 /*
58  * 16-byte buffer for sending data associated with IPC command.
59  */
60 #define IPC_DATA_BUFFER_SIZE    16
61
62 #define IPC_LOOP_CNT            3000000
63 #define IPC_MAX_SEC             3
64
65 #define IPC_TRIGGER_MODE_IRQ            true
66
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_BIOS_DATA_INDEX   1
72 #define PLAT_RESOURCE_BIOS_IFACE_INDEX  2
73 #define PLAT_RESOURCE_TELEM_SSRAM_INDEX 3
74 #define PLAT_RESOURCE_ISP_DATA_INDEX    4
75 #define PLAT_RESOURCE_ISP_IFACE_INDEX   5
76 #define PLAT_RESOURCE_GTD_DATA_INDEX    6
77 #define PLAT_RESOURCE_GTD_IFACE_INDEX   7
78 #define PLAT_RESOURCE_ACPI_IO_INDEX     0
79
80 /*
81  * BIOS does not create an ACPI device for each PMC function,
82  * but exports multiple resources from one ACPI device(IPC) for
83  * multiple functions. This driver is responsible to create a
84  * platform device and to export resources for those functions.
85  */
86 #define TCO_DEVICE_NAME                 "iTCO_wdt"
87 #define SMI_EN_OFFSET                   0x30
88 #define SMI_EN_SIZE                     4
89 #define TCO_BASE_OFFSET                 0x60
90 #define TCO_REGS_SIZE                   16
91 #define PUNIT_DEVICE_NAME               "intel_punit_ipc"
92 #define TELEMETRY_DEVICE_NAME           "intel_telemetry"
93 #define TELEM_SSRAM_SIZE                240
94 #define TELEM_PMC_SSRAM_OFFSET          0x1B00
95 #define TELEM_PUNIT_SSRAM_OFFSET        0x1A00
96
97 static const int iTCO_version = 3;
98
99 static struct intel_pmc_ipc_dev {
100         struct device *dev;
101         void __iomem *ipc_base;
102         bool irq_mode;
103         int irq;
104         int cmd;
105         struct completion cmd_complete;
106
107         /* The following PMC BARs share the same ACPI device with the IPC */
108         resource_size_t acpi_io_base;
109         int acpi_io_size;
110         struct platform_device *tco_dev;
111
112         /* gcr */
113         resource_size_t gcr_base;
114         int gcr_size;
115
116         /* punit */
117         struct platform_device *punit_dev;
118
119         /* Telemetry */
120         resource_size_t telem_pmc_ssram_base;
121         resource_size_t telem_punit_ssram_base;
122         int telem_pmc_ssram_size;
123         int telem_punit_ssram_size;
124         u8 telem_res_inval;
125         struct platform_device *telemetry_dev;
126 } ipcdev;
127
128 static char *ipc_err_sources[] = {
129         [IPC_ERR_NONE] =
130                 "no error",
131         [IPC_ERR_CMD_NOT_SUPPORTED] =
132                 "command not supported",
133         [IPC_ERR_CMD_NOT_SERVICED] =
134                 "command not serviced",
135         [IPC_ERR_UNABLE_TO_SERVICE] =
136                 "unable to service",
137         [IPC_ERR_CMD_INVALID] =
138                 "command invalid",
139         [IPC_ERR_CMD_FAILED] =
140                 "command failed",
141         [IPC_ERR_EMSECURITY] =
142                 "Invalid Battery",
143         [IPC_ERR_UNSIGNEDKERNEL] =
144                 "Unsigned kernel",
145 };
146
147 /* Prevent concurrent calls to the PMC */
148 static DEFINE_MUTEX(ipclock);
149
150 static inline void ipc_send_command(u32 cmd)
151 {
152         ipcdev.cmd = cmd;
153         if (ipcdev.irq_mode) {
154                 reinit_completion(&ipcdev.cmd_complete);
155                 cmd |= IPC_CMD_MSI;
156         }
157         writel(cmd, ipcdev.ipc_base + IPC_CMD);
158 }
159
160 static inline u32 ipc_read_status(void)
161 {
162         return readl(ipcdev.ipc_base + IPC_STATUS);
163 }
164
165 static inline void ipc_data_writel(u32 data, u32 offset)
166 {
167         writel(data, ipcdev.ipc_base + IPC_WRITE_BUFFER + offset);
168 }
169
170 static inline u8 ipc_data_readb(u32 offset)
171 {
172         return readb(ipcdev.ipc_base + IPC_READ_BUFFER + offset);
173 }
174
175 static inline u32 ipc_data_readl(u32 offset)
176 {
177         return readl(ipcdev.ipc_base + IPC_READ_BUFFER + offset);
178 }
179
180 static int intel_pmc_ipc_check_status(void)
181 {
182         int status;
183         int ret = 0;
184
185         if (ipcdev.irq_mode) {
186                 if (0 == wait_for_completion_timeout(
187                                 &ipcdev.cmd_complete, IPC_MAX_SEC * HZ))
188                         ret = -ETIMEDOUT;
189         } else {
190                 int loop_count = IPC_LOOP_CNT;
191
192                 while ((ipc_read_status() & IPC_STATUS_BUSY) && --loop_count)
193                         udelay(1);
194                 if (loop_count == 0)
195                         ret = -ETIMEDOUT;
196         }
197
198         status = ipc_read_status();
199         if (ret == -ETIMEDOUT) {
200                 dev_err(ipcdev.dev,
201                         "IPC timed out, TS=0x%x, CMD=0x%x\n",
202                         status, ipcdev.cmd);
203                 return ret;
204         }
205
206         if (status & IPC_STATUS_ERR) {
207                 int i;
208
209                 ret = -EIO;
210                 i = (status >> IPC_CMD_SIZE) & 0xFF;
211                 if (i < ARRAY_SIZE(ipc_err_sources))
212                         dev_err(ipcdev.dev,
213                                 "IPC failed: %s, STS=0x%x, CMD=0x%x\n",
214                                 ipc_err_sources[i], status, ipcdev.cmd);
215                 else
216                         dev_err(ipcdev.dev,
217                                 "IPC failed: unknown, STS=0x%x, CMD=0x%x\n",
218                                 status, ipcdev.cmd);
219                 if ((i == IPC_ERR_UNSIGNEDKERNEL) || (i == IPC_ERR_EMSECURITY))
220                         ret = -EACCES;
221         }
222
223         return ret;
224 }
225
226 /**
227  * intel_pmc_ipc_simple_command() - Simple IPC command
228  * @cmd:        IPC command code.
229  * @sub:        IPC command sub type.
230  *
231  * Send a simple IPC command to PMC when don't need to specify
232  * input/output data and source/dest pointers.
233  *
234  * Return:      an IPC error code or 0 on success.
235  */
236 int intel_pmc_ipc_simple_command(int cmd, int sub)
237 {
238         int ret;
239
240         mutex_lock(&ipclock);
241         if (ipcdev.dev == NULL) {
242                 mutex_unlock(&ipclock);
243                 return -ENODEV;
244         }
245         ipc_send_command(sub << IPC_CMD_SUBCMD | cmd);
246         ret = intel_pmc_ipc_check_status();
247         mutex_unlock(&ipclock);
248
249         return ret;
250 }
251 EXPORT_SYMBOL_GPL(intel_pmc_ipc_simple_command);
252
253 /**
254  * intel_pmc_ipc_raw_cmd() - IPC command with data and pointers
255  * @cmd:        IPC command code.
256  * @sub:        IPC command sub type.
257  * @in:         input data of this IPC command.
258  * @inlen:      input data length in bytes.
259  * @out:        output data of this IPC command.
260  * @outlen:     output data length in dwords.
261  * @sptr:       data writing to SPTR register.
262  * @dptr:       data writing to DPTR register.
263  *
264  * Send an IPC command to PMC with input/output data and source/dest pointers.
265  *
266  * Return:      an IPC error code or 0 on success.
267  */
268 int intel_pmc_ipc_raw_cmd(u32 cmd, u32 sub, u8 *in, u32 inlen, u32 *out,
269                           u32 outlen, u32 dptr, u32 sptr)
270 {
271         u32 wbuf[4] = { 0 };
272         int ret;
273         int i;
274
275         if (inlen > IPC_DATA_BUFFER_SIZE || outlen > IPC_DATA_BUFFER_SIZE / 4)
276                 return -EINVAL;
277
278         mutex_lock(&ipclock);
279         if (ipcdev.dev == NULL) {
280                 mutex_unlock(&ipclock);
281                 return -ENODEV;
282         }
283         memcpy(wbuf, in, inlen);
284         writel(dptr, ipcdev.ipc_base + IPC_DPTR);
285         writel(sptr, ipcdev.ipc_base + IPC_SPTR);
286         /* The input data register is 32bit register and inlen is in Byte */
287         for (i = 0; i < ((inlen + 3) / 4); i++)
288                 ipc_data_writel(wbuf[i], 4 * i);
289         ipc_send_command((inlen << IPC_CMD_SIZE) |
290                         (sub << IPC_CMD_SUBCMD) | cmd);
291         ret = intel_pmc_ipc_check_status();
292         if (!ret) {
293                 /* out is read from 32bit register and outlen is in 32bit */
294                 for (i = 0; i < outlen; i++)
295                         *out++ = ipc_data_readl(4 * i);
296         }
297         mutex_unlock(&ipclock);
298
299         return ret;
300 }
301 EXPORT_SYMBOL_GPL(intel_pmc_ipc_raw_cmd);
302
303 /**
304  * intel_pmc_ipc_command() -  IPC command with input/output data
305  * @cmd:        IPC command code.
306  * @sub:        IPC command sub type.
307  * @in:         input data of this IPC command.
308  * @inlen:      input data length in bytes.
309  * @out:        output data of this IPC command.
310  * @outlen:     output data length in dwords.
311  *
312  * Send an IPC command to PMC with input/output data.
313  *
314  * Return:      an IPC error code or 0 on success.
315  */
316 int intel_pmc_ipc_command(u32 cmd, u32 sub, u8 *in, u32 inlen,
317                           u32 *out, u32 outlen)
318 {
319         return intel_pmc_ipc_raw_cmd(cmd, sub, in, inlen, out, outlen, 0, 0);
320 }
321 EXPORT_SYMBOL_GPL(intel_pmc_ipc_command);
322
323 static irqreturn_t ioc(int irq, void *dev_id)
324 {
325         int status;
326
327         if (ipcdev.irq_mode) {
328                 status = ipc_read_status();
329                 writel(status | IPC_STATUS_IRQ, ipcdev.ipc_base + IPC_STATUS);
330         }
331         complete(&ipcdev.cmd_complete);
332
333         return IRQ_HANDLED;
334 }
335
336 static int ipc_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
337 {
338         resource_size_t pci_resource;
339         int ret;
340         int len;
341
342         ipcdev.dev = &pci_dev_get(pdev)->dev;
343         ipcdev.irq_mode = IPC_TRIGGER_MODE_IRQ;
344
345         ret = pci_enable_device(pdev);
346         if (ret)
347                 return ret;
348
349         ret = pci_request_regions(pdev, "intel_pmc_ipc");
350         if (ret)
351                 return ret;
352
353         pci_resource = pci_resource_start(pdev, 0);
354         len = pci_resource_len(pdev, 0);
355         if (!pci_resource || !len) {
356                 dev_err(&pdev->dev, "Failed to get resource\n");
357                 return -ENOMEM;
358         }
359
360         init_completion(&ipcdev.cmd_complete);
361
362         if (request_irq(pdev->irq, ioc, 0, "intel_pmc_ipc", &ipcdev)) {
363                 dev_err(&pdev->dev, "Failed to request irq\n");
364                 return -EBUSY;
365         }
366
367         ipcdev.ipc_base = ioremap_nocache(pci_resource, len);
368         if (!ipcdev.ipc_base) {
369                 dev_err(&pdev->dev, "Failed to ioremap ipc base\n");
370                 free_irq(pdev->irq, &ipcdev);
371                 ret = -ENOMEM;
372         }
373
374         return ret;
375 }
376
377 static void ipc_pci_remove(struct pci_dev *pdev)
378 {
379         free_irq(pdev->irq, &ipcdev);
380         pci_release_regions(pdev);
381         pci_dev_put(pdev);
382         iounmap(ipcdev.ipc_base);
383         ipcdev.dev = NULL;
384 }
385
386 static const struct pci_device_id ipc_pci_ids[] = {
387         {PCI_VDEVICE(INTEL, 0x0a94), 0},
388         {PCI_VDEVICE(INTEL, 0x1a94), 0},
389         { 0,}
390 };
391 MODULE_DEVICE_TABLE(pci, ipc_pci_ids);
392
393 static struct pci_driver ipc_pci_driver = {
394         .name = "intel_pmc_ipc",
395         .id_table = ipc_pci_ids,
396         .probe = ipc_pci_probe,
397         .remove = ipc_pci_remove,
398 };
399
400 static ssize_t intel_pmc_ipc_simple_cmd_store(struct device *dev,
401                                               struct device_attribute *attr,
402                                               const char *buf, size_t count)
403 {
404         int subcmd;
405         int cmd;
406         int ret;
407
408         ret = sscanf(buf, "%d %d", &cmd, &subcmd);
409         if (ret != 2) {
410                 dev_err(dev, "Error args\n");
411                 return -EINVAL;
412         }
413
414         ret = intel_pmc_ipc_simple_command(cmd, subcmd);
415         if (ret) {
416                 dev_err(dev, "command %d error with %d\n", cmd, ret);
417                 return ret;
418         }
419         return (ssize_t)count;
420 }
421
422 static ssize_t intel_pmc_ipc_northpeak_store(struct device *dev,
423                                              struct device_attribute *attr,
424                                              const char *buf, size_t count)
425 {
426         unsigned long val;
427         int subcmd;
428         int ret;
429
430         if (kstrtoul(buf, 0, &val))
431                 return -EINVAL;
432
433         if (val)
434                 subcmd = 1;
435         else
436                 subcmd = 0;
437         ret = intel_pmc_ipc_simple_command(PMC_IPC_NORTHPEAK_CTRL, subcmd);
438         if (ret) {
439                 dev_err(dev, "command north %d error with %d\n", subcmd, ret);
440                 return ret;
441         }
442         return (ssize_t)count;
443 }
444
445 static DEVICE_ATTR(simplecmd, S_IWUSR,
446                    NULL, intel_pmc_ipc_simple_cmd_store);
447 static DEVICE_ATTR(northpeak, S_IWUSR,
448                    NULL, intel_pmc_ipc_northpeak_store);
449
450 static struct attribute *intel_ipc_attrs[] = {
451         &dev_attr_northpeak.attr,
452         &dev_attr_simplecmd.attr,
453         NULL
454 };
455
456 static const struct attribute_group intel_ipc_group = {
457         .attrs = intel_ipc_attrs,
458 };
459
460 static struct resource punit_res_array[] = {
461         /* Punit BIOS */
462         {
463                 .flags = IORESOURCE_MEM,
464         },
465         {
466                 .flags = IORESOURCE_MEM,
467         },
468         /* Punit ISP */
469         {
470                 .flags = IORESOURCE_MEM,
471         },
472         {
473                 .flags = IORESOURCE_MEM,
474         },
475         /* Punit GTD */
476         {
477                 .flags = IORESOURCE_MEM,
478         },
479         {
480                 .flags = IORESOURCE_MEM,
481         },
482 };
483
484 #define TCO_RESOURCE_ACPI_IO            0
485 #define TCO_RESOURCE_SMI_EN_IO          1
486 #define TCO_RESOURCE_GCR_MEM            2
487 static struct resource tco_res[] = {
488         /* ACPI - TCO */
489         {
490                 .flags = IORESOURCE_IO,
491         },
492         /* ACPI - SMI */
493         {
494                 .flags = IORESOURCE_IO,
495         },
496         /* GCS */
497         {
498                 .flags = IORESOURCE_MEM,
499         },
500 };
501
502 static struct itco_wdt_platform_data tco_info = {
503         .name = "Apollo Lake SoC",
504         .version = 3,
505 };
506
507 #define TELEMETRY_RESOURCE_PUNIT_SSRAM  0
508 #define TELEMETRY_RESOURCE_PMC_SSRAM    1
509 static struct resource telemetry_res[] = {
510         /*Telemetry*/
511         {
512                 .flags = IORESOURCE_MEM,
513         },
514         {
515                 .flags = IORESOURCE_MEM,
516         },
517 };
518
519 static int ipc_create_punit_device(void)
520 {
521         struct platform_device *pdev;
522         int ret;
523
524         pdev = platform_device_alloc(PUNIT_DEVICE_NAME, -1);
525         if (!pdev) {
526                 dev_err(ipcdev.dev, "Failed to alloc punit platform device\n");
527                 return -ENOMEM;
528         }
529
530         pdev->dev.parent = ipcdev.dev;
531         ret = platform_device_add_resources(pdev, punit_res_array,
532                                             ARRAY_SIZE(punit_res_array));
533         if (ret) {
534                 dev_err(ipcdev.dev, "Failed to add platform punit resources\n");
535                 goto err;
536         }
537
538         ret = platform_device_add(pdev);
539         if (ret) {
540                 dev_err(ipcdev.dev, "Failed to add punit platform device\n");
541                 goto err;
542         }
543         ipcdev.punit_dev = pdev;
544
545         return 0;
546 err:
547         platform_device_put(pdev);
548         return ret;
549 }
550
551 static int ipc_create_tco_device(void)
552 {
553         struct platform_device *pdev;
554         struct resource *res;
555         int ret;
556
557         pdev = platform_device_alloc(TCO_DEVICE_NAME, -1);
558         if (!pdev) {
559                 dev_err(ipcdev.dev, "Failed to alloc tco platform device\n");
560                 return -ENOMEM;
561         }
562
563         pdev->dev.parent = ipcdev.dev;
564
565         res = tco_res + TCO_RESOURCE_ACPI_IO;
566         res->start = ipcdev.acpi_io_base + TCO_BASE_OFFSET;
567         res->end = res->start + TCO_REGS_SIZE - 1;
568
569         res = tco_res + TCO_RESOURCE_SMI_EN_IO;
570         res->start = ipcdev.acpi_io_base + SMI_EN_OFFSET;
571         res->end = res->start + SMI_EN_SIZE - 1;
572
573         res = tco_res + TCO_RESOURCE_GCR_MEM;
574         res->start = ipcdev.gcr_base;
575         res->end = res->start + ipcdev.gcr_size - 1;
576
577         ret = platform_device_add_resources(pdev, tco_res, ARRAY_SIZE(tco_res));
578         if (ret) {
579                 dev_err(ipcdev.dev, "Failed to add tco platform resources\n");
580                 goto err;
581         }
582
583         ret = platform_device_add_data(pdev, &tco_info, sizeof(tco_info));
584         if (ret) {
585                 dev_err(ipcdev.dev, "Failed to add tco platform data\n");
586                 goto err;
587         }
588
589         ret = platform_device_add(pdev);
590         if (ret) {
591                 dev_err(ipcdev.dev, "Failed to add tco platform device\n");
592                 goto err;
593         }
594         ipcdev.tco_dev = pdev;
595
596         return 0;
597 err:
598         platform_device_put(pdev);
599         return ret;
600 }
601
602 static int ipc_create_telemetry_device(void)
603 {
604         struct platform_device *pdev;
605         struct resource *res;
606         int ret;
607
608         pdev = platform_device_alloc(TELEMETRY_DEVICE_NAME, -1);
609         if (!pdev) {
610                 dev_err(ipcdev.dev,
611                         "Failed to allocate telemetry platform device\n");
612                 return -ENOMEM;
613         }
614
615         pdev->dev.parent = ipcdev.dev;
616
617         res = telemetry_res + TELEMETRY_RESOURCE_PUNIT_SSRAM;
618         res->start = ipcdev.telem_punit_ssram_base;
619         res->end = res->start + ipcdev.telem_punit_ssram_size - 1;
620
621         res = telemetry_res + TELEMETRY_RESOURCE_PMC_SSRAM;
622         res->start = ipcdev.telem_pmc_ssram_base;
623         res->end = res->start + ipcdev.telem_pmc_ssram_size - 1;
624
625         ret = platform_device_add_resources(pdev, telemetry_res,
626                                             ARRAY_SIZE(telemetry_res));
627         if (ret) {
628                 dev_err(ipcdev.dev,
629                         "Failed to add telemetry platform resources\n");
630                 goto err;
631         }
632
633         ret = platform_device_add(pdev);
634         if (ret) {
635                 dev_err(ipcdev.dev,
636                         "Failed to add telemetry platform device\n");
637                 goto err;
638         }
639         ipcdev.telemetry_dev = pdev;
640
641         return 0;
642 err:
643         platform_device_put(pdev);
644         return ret;
645 }
646
647 static int ipc_create_pmc_devices(void)
648 {
649         int ret;
650
651         ret = ipc_create_tco_device();
652         if (ret) {
653                 dev_err(ipcdev.dev, "Failed to add tco platform device\n");
654                 return ret;
655         }
656         ret = ipc_create_punit_device();
657         if (ret) {
658                 dev_err(ipcdev.dev, "Failed to add punit platform device\n");
659                 platform_device_unregister(ipcdev.tco_dev);
660         }
661
662         if (!ipcdev.telem_res_inval) {
663                 ret = ipc_create_telemetry_device();
664                 if (ret)
665                         dev_warn(ipcdev.dev,
666                                 "Failed to add telemetry platform device\n");
667         }
668
669         return ret;
670 }
671
672 static int ipc_plat_get_res(struct platform_device *pdev)
673 {
674         struct resource *res, *punit_res;
675         void __iomem *addr;
676         int size;
677
678         res = platform_get_resource(pdev, IORESOURCE_IO,
679                                     PLAT_RESOURCE_ACPI_IO_INDEX);
680         if (!res) {
681                 dev_err(&pdev->dev, "Failed to get io resource\n");
682                 return -ENXIO;
683         }
684         size = resource_size(res);
685         ipcdev.acpi_io_base = res->start;
686         ipcdev.acpi_io_size = size;
687         dev_info(&pdev->dev, "io res: %pR\n", res);
688
689         /* This is index 0 to cover BIOS data register */
690         punit_res = punit_res_array;
691         res = platform_get_resource(pdev, IORESOURCE_MEM,
692                                     PLAT_RESOURCE_BIOS_DATA_INDEX);
693         if (!res) {
694                 dev_err(&pdev->dev, "Failed to get res of punit BIOS data\n");
695                 return -ENXIO;
696         }
697         *punit_res = *res;
698         dev_info(&pdev->dev, "punit BIOS data res: %pR\n", res);
699
700         res = platform_get_resource(pdev, IORESOURCE_MEM,
701                                     PLAT_RESOURCE_BIOS_IFACE_INDEX);
702         if (!res) {
703                 dev_err(&pdev->dev, "Failed to get res of punit BIOS iface\n");
704                 return -ENXIO;
705         }
706         /* This is index 1 to cover BIOS interface register */
707         *++punit_res = *res;
708         dev_info(&pdev->dev, "punit BIOS interface res: %pR\n", res);
709
710         res = platform_get_resource(pdev, IORESOURCE_MEM,
711                                     PLAT_RESOURCE_ISP_DATA_INDEX);
712         if (!res) {
713                 dev_err(&pdev->dev, "Failed to get res of punit ISP data\n");
714                 return -ENXIO;
715         }
716         /* This is index 2 to cover ISP data register */
717         *++punit_res = *res;
718         dev_info(&pdev->dev, "punit ISP data res: %pR\n", res);
719
720         res = platform_get_resource(pdev, IORESOURCE_MEM,
721                                     PLAT_RESOURCE_ISP_IFACE_INDEX);
722         if (!res) {
723                 dev_err(&pdev->dev, "Failed to get res of punit ISP iface\n");
724                 return -ENXIO;
725         }
726         /* This is index 3 to cover ISP interface register */
727         *++punit_res = *res;
728         dev_info(&pdev->dev, "punit ISP interface res: %pR\n", res);
729
730         res = platform_get_resource(pdev, IORESOURCE_MEM,
731                                     PLAT_RESOURCE_GTD_DATA_INDEX);
732         if (!res) {
733                 dev_err(&pdev->dev, "Failed to get res of punit GTD data\n");
734                 return -ENXIO;
735         }
736         /* This is index 4 to cover GTD data register */
737         *++punit_res = *res;
738         dev_info(&pdev->dev, "punit GTD data res: %pR\n", res);
739
740         res = platform_get_resource(pdev, IORESOURCE_MEM,
741                                     PLAT_RESOURCE_GTD_IFACE_INDEX);
742         if (!res) {
743                 dev_err(&pdev->dev, "Failed to get res of punit GTD iface\n");
744                 return -ENXIO;
745         }
746         /* This is index 5 to cover GTD interface register */
747         *++punit_res = *res;
748         dev_info(&pdev->dev, "punit GTD interface res: %pR\n", res);
749
750         res = platform_get_resource(pdev, IORESOURCE_MEM,
751                                     PLAT_RESOURCE_IPC_INDEX);
752         if (!res) {
753                 dev_err(&pdev->dev, "Failed to get ipc resource\n");
754                 return -ENXIO;
755         }
756         size = PLAT_RESOURCE_IPC_SIZE;
757         if (!request_mem_region(res->start, size, pdev->name)) {
758                 dev_err(&pdev->dev, "Failed to request ipc resource\n");
759                 return -EBUSY;
760         }
761         addr = ioremap_nocache(res->start, size);
762         if (!addr) {
763                 dev_err(&pdev->dev, "I/O memory remapping failed\n");
764                 release_mem_region(res->start, size);
765                 return -ENOMEM;
766         }
767         ipcdev.ipc_base = addr;
768
769         ipcdev.gcr_base = res->start + size;
770         ipcdev.gcr_size = PLAT_RESOURCE_GCR_SIZE;
771         dev_info(&pdev->dev, "ipc res: %pR\n", res);
772
773         ipcdev.telem_res_inval = 0;
774         res = platform_get_resource(pdev, IORESOURCE_MEM,
775                                     PLAT_RESOURCE_TELEM_SSRAM_INDEX);
776         if (!res) {
777                 dev_err(&pdev->dev, "Failed to get telemetry ssram resource\n");
778                 ipcdev.telem_res_inval = 1;
779         } else {
780                 ipcdev.telem_punit_ssram_base = res->start +
781                                                 TELEM_PUNIT_SSRAM_OFFSET;
782                 ipcdev.telem_punit_ssram_size = TELEM_SSRAM_SIZE;
783                 ipcdev.telem_pmc_ssram_base = res->start +
784                                                 TELEM_PMC_SSRAM_OFFSET;
785                 ipcdev.telem_pmc_ssram_size = TELEM_SSRAM_SIZE;
786                 dev_info(&pdev->dev, "telemetry ssram res: %pR\n", res);
787         }
788
789         return 0;
790 }
791
792 #ifdef CONFIG_ACPI
793 static const struct acpi_device_id ipc_acpi_ids[] = {
794         { "INT34D2", 0},
795         { }
796 };
797 MODULE_DEVICE_TABLE(acpi, ipc_acpi_ids);
798 #endif
799
800 static int ipc_plat_probe(struct platform_device *pdev)
801 {
802         struct resource *res;
803         int ret;
804
805         ipcdev.dev = &pdev->dev;
806         ipcdev.irq_mode = IPC_TRIGGER_MODE_IRQ;
807         init_completion(&ipcdev.cmd_complete);
808
809         ipcdev.irq = platform_get_irq(pdev, 0);
810         if (ipcdev.irq < 0) {
811                 dev_err(&pdev->dev, "Failed to get irq\n");
812                 return -EINVAL;
813         }
814
815         ret = ipc_plat_get_res(pdev);
816         if (ret) {
817                 dev_err(&pdev->dev, "Failed to request resource\n");
818                 return ret;
819         }
820
821         ret = ipc_create_pmc_devices();
822         if (ret) {
823                 dev_err(&pdev->dev, "Failed to create pmc devices\n");
824                 goto err_device;
825         }
826
827         if (request_irq(ipcdev.irq, ioc, 0, "intel_pmc_ipc", &ipcdev)) {
828                 dev_err(&pdev->dev, "Failed to request irq\n");
829                 ret = -EBUSY;
830                 goto err_irq;
831         }
832
833         ret = sysfs_create_group(&pdev->dev.kobj, &intel_ipc_group);
834         if (ret) {
835                 dev_err(&pdev->dev, "Failed to create sysfs group %d\n",
836                         ret);
837                 goto err_sys;
838         }
839
840         return 0;
841 err_sys:
842         free_irq(ipcdev.irq, &ipcdev);
843 err_irq:
844         platform_device_unregister(ipcdev.tco_dev);
845         platform_device_unregister(ipcdev.punit_dev);
846         platform_device_unregister(ipcdev.telemetry_dev);
847 err_device:
848         iounmap(ipcdev.ipc_base);
849         res = platform_get_resource(pdev, IORESOURCE_MEM,
850                                     PLAT_RESOURCE_IPC_INDEX);
851         if (res)
852                 release_mem_region(res->start, PLAT_RESOURCE_IPC_SIZE);
853         return ret;
854 }
855
856 static int ipc_plat_remove(struct platform_device *pdev)
857 {
858         struct resource *res;
859
860         sysfs_remove_group(&pdev->dev.kobj, &intel_ipc_group);
861         free_irq(ipcdev.irq, &ipcdev);
862         platform_device_unregister(ipcdev.tco_dev);
863         platform_device_unregister(ipcdev.punit_dev);
864         platform_device_unregister(ipcdev.telemetry_dev);
865         iounmap(ipcdev.ipc_base);
866         res = platform_get_resource(pdev, IORESOURCE_MEM,
867                                     PLAT_RESOURCE_IPC_INDEX);
868         if (res)
869                 release_mem_region(res->start, PLAT_RESOURCE_IPC_SIZE);
870         ipcdev.dev = NULL;
871         return 0;
872 }
873
874 static struct platform_driver ipc_plat_driver = {
875         .remove = ipc_plat_remove,
876         .probe = ipc_plat_probe,
877         .driver = {
878                 .name = "pmc-ipc-plat",
879                 .acpi_match_table = ACPI_PTR(ipc_acpi_ids),
880         },
881 };
882
883 static int __init intel_pmc_ipc_init(void)
884 {
885         int ret;
886
887         ret = platform_driver_register(&ipc_plat_driver);
888         if (ret) {
889                 pr_err("Failed to register PMC ipc platform driver\n");
890                 return ret;
891         }
892         ret = pci_register_driver(&ipc_pci_driver);
893         if (ret) {
894                 pr_err("Failed to register PMC ipc pci driver\n");
895                 platform_driver_unregister(&ipc_plat_driver);
896                 return ret;
897         }
898         return ret;
899 }
900
901 static void __exit intel_pmc_ipc_exit(void)
902 {
903         pci_unregister_driver(&ipc_pci_driver);
904         platform_driver_unregister(&ipc_plat_driver);
905 }
906
907 MODULE_AUTHOR("Zha Qipeng <qipeng.zha@intel.com>");
908 MODULE_DESCRIPTION("Intel PMC IPC driver");
909 MODULE_LICENSE("GPL");
910
911 /* Some modules are dependent on this, so init earlier */
912 fs_initcall(intel_pmc_ipc_init);
913 module_exit(intel_pmc_ipc_exit);