]> Git Repo - linux.git/blob - drivers/platform/x86/intel_speed_select_if/isst_if_mmio.c
drm/nouveau/kms: Don't change EDID when it hasn't actually changed
[linux.git] / drivers / platform / x86 / intel_speed_select_if / isst_if_mmio.c
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 <[email protected]>
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
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         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, INTEL_RAPL_PRIO_DEVID_1)},
76         { 0 },
77 };
78 MODULE_DEVICE_TABLE(pci, isst_if_ids);
79
80 static int isst_if_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
81 {
82         struct isst_if_device *punit_dev;
83         struct isst_if_cmd_cb cb;
84         u32 mmio_base, pcu_base;
85         u64 base_addr;
86         int ret;
87
88         punit_dev = devm_kzalloc(&pdev->dev, sizeof(*punit_dev), GFP_KERNEL);
89         if (!punit_dev)
90                 return -ENOMEM;
91
92         ret = pcim_enable_device(pdev);
93         if (ret)
94                 return ret;
95
96         ret = pci_read_config_dword(pdev, 0xD0, &mmio_base);
97         if (ret)
98                 return ret;
99
100         ret = pci_read_config_dword(pdev, 0xFC, &pcu_base);
101         if (ret)
102                 return ret;
103
104         pcu_base &= GENMASK(10, 0);
105         base_addr = (u64)mmio_base << 23 | (u64) pcu_base << 12;
106         punit_dev->punit_mmio = devm_ioremap(&pdev->dev, base_addr, 256);
107         if (!punit_dev->punit_mmio)
108                 return -ENOMEM;
109
110         mutex_init(&punit_dev->mutex);
111         pci_set_drvdata(pdev, punit_dev);
112
113         memset(&cb, 0, sizeof(cb));
114         cb.cmd_size = sizeof(struct isst_if_io_reg);
115         cb.offset = offsetof(struct isst_if_io_regs, io_reg);
116         cb.cmd_callback = isst_if_mmio_rd_wr;
117         cb.owner = THIS_MODULE;
118         ret = isst_if_cdev_register(ISST_IF_DEV_MMIO, &cb);
119         if (ret)
120                 mutex_destroy(&punit_dev->mutex);
121
122         return ret;
123 }
124
125 static void isst_if_remove(struct pci_dev *pdev)
126 {
127         struct isst_if_device *punit_dev;
128
129         punit_dev = pci_get_drvdata(pdev);
130         isst_if_cdev_unregister(ISST_IF_DEV_MMIO);
131         mutex_destroy(&punit_dev->mutex);
132 }
133
134 static int __maybe_unused isst_if_suspend(struct device *device)
135 {
136         struct isst_if_device *punit_dev = dev_get_drvdata(device);
137         int i;
138
139         for (i = 0; i < ARRAY_SIZE(punit_dev->range_0); ++i)
140                 punit_dev->range_0[i] = readl(punit_dev->punit_mmio +
141                                                 mmio_range[0].beg + 4 * i);
142         for (i = 0; i < ARRAY_SIZE(punit_dev->range_1); ++i)
143                 punit_dev->range_1[i] = readl(punit_dev->punit_mmio +
144                                                 mmio_range[1].beg + 4 * i);
145
146         return 0;
147 }
148
149 static int __maybe_unused isst_if_resume(struct device *device)
150 {
151         struct isst_if_device *punit_dev = dev_get_drvdata(device);
152         int i;
153
154         for (i = 0; i < ARRAY_SIZE(punit_dev->range_0); ++i)
155                 writel(punit_dev->range_0[i], punit_dev->punit_mmio +
156                                                 mmio_range[0].beg + 4 * i);
157         for (i = 0; i < ARRAY_SIZE(punit_dev->range_1); ++i)
158                 writel(punit_dev->range_1[i], punit_dev->punit_mmio +
159                                                 mmio_range[1].beg + 4 * i);
160
161         return 0;
162 }
163
164 static SIMPLE_DEV_PM_OPS(isst_if_pm_ops, isst_if_suspend, isst_if_resume);
165
166 static struct pci_driver isst_if_pci_driver = {
167         .name                   = "isst_if_pci",
168         .id_table               = isst_if_ids,
169         .probe                  = isst_if_probe,
170         .remove                 = isst_if_remove,
171         .driver.pm              = &isst_if_pm_ops,
172 };
173
174 module_pci_driver(isst_if_pci_driver);
175
176 MODULE_LICENSE("GPL v2");
177 MODULE_DESCRIPTION("Intel speed select interface mmio driver");
This page took 0.037476 seconds and 4 git commands to generate.