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