1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Intel Speed Select Interface: MMIO Interface
4 * Copyright (c) 2019, Intel Corporation.
5 * All rights reserved.
6 *
7 * Author: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
8 */
9
10 #include <linux/module.h>
11 #include <linux/pci.h>
12 #include <linux/sched/signal.h>
13 #include <linux/uaccess.h>
14 #include <uapi/linux/isst_if.h>
15
16 #include "isst_if_common.h"
17
18 struct isst_mmio_range {
19 int beg;
20 int end;
21 };
22
23 struct isst_mmio_range mmio_range[] = {
24 {0x04, 0x14},
25 {0x20, 0xD0},
26 };
27
28 struct isst_if_device {
29 void __iomem *punit_mmio;
30 u32 range_0[5];
31 u32 range_1[45];
32 struct mutex mutex;
33 };
34
isst_if_mmio_rd_wr(u8 * cmd_ptr,int * write_only,int resume)35 static long isst_if_mmio_rd_wr(u8 *cmd_ptr, int *write_only, int resume)
36 {
37 struct isst_if_device *punit_dev;
38 struct isst_if_io_reg *io_reg;
39 struct pci_dev *pdev;
40
41 io_reg = (struct isst_if_io_reg *)cmd_ptr;
42 if (io_reg->reg < 0x04 || io_reg->reg > 0xD0)
43 return -EINVAL;
44
45 if (io_reg->read_write && !capable(CAP_SYS_ADMIN))
46 return -EPERM;
47
48 pdev = isst_if_get_pci_dev(io_reg->logical_cpu, 0, 0, 1);
49 if (!pdev)
50 return -EINVAL;
51
52 punit_dev = pci_get_drvdata(pdev);
53 if (!punit_dev)
54 return -EINVAL;
55
56 /*
57 * Ensure that operation is complete on a PCI device to avoid read
58 * write race by using per PCI device mutex.
59 */
60 mutex_lock(&punit_dev->mutex);
61 if (io_reg->read_write) {
62 writel(io_reg->value, punit_dev->punit_mmio+io_reg->reg);
63 *write_only = 1;
64 } else {
65 io_reg->value = readl(punit_dev->punit_mmio+io_reg->reg);
66 *write_only = 0;
67 }
68 mutex_unlock(&punit_dev->mutex);
69
70 return 0;
71 }
72
73 static const struct pci_device_id isst_if_ids[] = {
74 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, INTEL_RAPL_PRIO_DEVID_0)},
75 { 0 },
76 };
77 MODULE_DEVICE_TABLE(pci, isst_if_ids);
78
isst_if_probe(struct pci_dev * pdev,const struct pci_device_id * ent)79 static int isst_if_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
80 {
81 struct isst_if_device *punit_dev;
82 struct isst_if_cmd_cb cb;
83 u32 mmio_base, pcu_base;
84 u64 base_addr;
85 int ret;
86
87 punit_dev = devm_kzalloc(&pdev->dev, sizeof(*punit_dev), GFP_KERNEL);
88 if (!punit_dev)
89 return -ENOMEM;
90
91 ret = pcim_enable_device(pdev);
92 if (ret)
93 return ret;
94
95 ret = pci_read_config_dword(pdev, 0xD0, &mmio_base);
96 if (ret)
97 return ret;
98
99 ret = pci_read_config_dword(pdev, 0xFC, &pcu_base);
100 if (ret)
101 return ret;
102
103 pcu_base &= GENMASK(10, 0);
104 base_addr = (u64)mmio_base << 23 | (u64) pcu_base << 12;
105 punit_dev->punit_mmio = devm_ioremap(&pdev->dev, base_addr, 256);
106 if (!punit_dev->punit_mmio)
107 return -ENOMEM;
108
109 mutex_init(&punit_dev->mutex);
110 pci_set_drvdata(pdev, punit_dev);
111
112 memset(&cb, 0, sizeof(cb));
113 cb.cmd_size = sizeof(struct isst_if_io_reg);
114 cb.offset = offsetof(struct isst_if_io_regs, io_reg);
115 cb.cmd_callback = isst_if_mmio_rd_wr;
116 cb.owner = THIS_MODULE;
117 ret = isst_if_cdev_register(ISST_IF_DEV_MMIO, &cb);
118 if (ret)
119 mutex_destroy(&punit_dev->mutex);
120
121 return ret;
122 }
123
isst_if_remove(struct pci_dev * pdev)124 static void isst_if_remove(struct pci_dev *pdev)
125 {
126 struct isst_if_device *punit_dev;
127
128 punit_dev = pci_get_drvdata(pdev);
129 isst_if_cdev_unregister(ISST_IF_DEV_MBOX);
130 mutex_destroy(&punit_dev->mutex);
131 }
132
isst_if_suspend(struct device * device)133 static int __maybe_unused isst_if_suspend(struct device *device)
134 {
135 struct isst_if_device *punit_dev = dev_get_drvdata(device);
136 int i;
137
138 for (i = 0; i < ARRAY_SIZE(punit_dev->range_0); ++i)
139 punit_dev->range_0[i] = readl(punit_dev->punit_mmio +
140 mmio_range[0].beg + 4 * i);
141 for (i = 0; i < ARRAY_SIZE(punit_dev->range_1); ++i)
142 punit_dev->range_1[i] = readl(punit_dev->punit_mmio +
143 mmio_range[1].beg + 4 * i);
144
145 return 0;
146 }
147
isst_if_resume(struct device * device)148 static int __maybe_unused isst_if_resume(struct device *device)
149 {
150 struct isst_if_device *punit_dev = dev_get_drvdata(device);
151 int i;
152
153 for (i = 0; i < ARRAY_SIZE(punit_dev->range_0); ++i)
154 writel(punit_dev->range_0[i], punit_dev->punit_mmio +
155 mmio_range[0].beg + 4 * i);
156 for (i = 0; i < ARRAY_SIZE(punit_dev->range_1); ++i)
157 writel(punit_dev->range_1[i], punit_dev->punit_mmio +
158 mmio_range[1].beg + 4 * i);
159
160 return 0;
161 }
162
163 static SIMPLE_DEV_PM_OPS(isst_if_pm_ops, isst_if_suspend, isst_if_resume);
164
165 static struct pci_driver isst_if_pci_driver = {
166 .name = "isst_if_pci",
167 .id_table = isst_if_ids,
168 .probe = isst_if_probe,
169 .remove = isst_if_remove,
170 .driver.pm = &isst_if_pm_ops,
171 };
172
173 module_pci_driver(isst_if_pci_driver);
174
175 MODULE_LICENSE("GPL v2");
176 MODULE_DESCRIPTION("Intel speed select interface mmio driver");
177