1 // SPDX-License-Identifier: GPL-2.0
3 * Intel Speed Select Interface: Mbox via PCI Interface
4 * Copyright (c) 2019, Intel Corporation.
7 * Author: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
10 #include <linux/cpufeature.h>
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include <linux/sched/signal.h>
14 #include <linux/uaccess.h>
15 #include <uapi/linux/isst_if.h>
17 #include "isst_if_common.h"
19 #define PUNIT_MAILBOX_DATA 0xA0
20 #define PUNIT_MAILBOX_INTERFACE 0xA4
21 #define PUNIT_MAILBOX_BUSY_BIT 31
24 * Commands has variable amount of processing time. Most of the commands will
25 * be done in 0-3 tries, but some takes up to 50.
26 * The real processing time was observed as 25us for the most of the commands
27 * at 2GHz. It is possible to optimize this count taking samples on customer
30 #define OS_MAILBOX_RETRY_COUNT 50
32 struct isst_if_device {
36 static int isst_if_mbox_cmd(struct pci_dev *pdev,
37 struct isst_if_mbox_cmd *mbox_cmd)
42 /* Poll for rb bit == 0 */
43 retries = OS_MAILBOX_RETRY_COUNT;
45 ret = pci_read_config_dword(pdev, PUNIT_MAILBOX_INTERFACE,
50 if (data & BIT_ULL(PUNIT_MAILBOX_BUSY_BIT)) {
61 /* Write DATA register */
62 ret = pci_write_config_dword(pdev, PUNIT_MAILBOX_DATA,
67 /* Write command register */
68 data = BIT_ULL(PUNIT_MAILBOX_BUSY_BIT) |
69 (mbox_cmd->parameter & GENMASK_ULL(13, 0)) << 16 |
70 (mbox_cmd->sub_command << 8) |
73 ret = pci_write_config_dword(pdev, PUNIT_MAILBOX_INTERFACE, data);
77 /* Poll for rb bit == 0 */
78 retries = OS_MAILBOX_RETRY_COUNT;
80 ret = pci_read_config_dword(pdev, PUNIT_MAILBOX_INTERFACE,
85 if (data & BIT_ULL(PUNIT_MAILBOX_BUSY_BIT)) {
93 ret = pci_read_config_dword(pdev, PUNIT_MAILBOX_DATA, &data);
97 mbox_cmd->resp_data = data;
105 static long isst_if_mbox_proc_cmd(u8 *cmd_ptr, int *write_only, int resume)
107 struct isst_if_mbox_cmd *mbox_cmd;
108 struct isst_if_device *punit_dev;
109 struct pci_dev *pdev;
112 mbox_cmd = (struct isst_if_mbox_cmd *)cmd_ptr;
114 if (isst_if_mbox_cmd_invalid(mbox_cmd))
117 if (isst_if_mbox_cmd_set_req(mbox_cmd) && !capable(CAP_SYS_ADMIN))
120 pdev = isst_if_get_pci_dev(mbox_cmd->logical_cpu, 1, 30, 1);
124 punit_dev = pci_get_drvdata(pdev);
129 * Basically we are allowing one complete mailbox transaction on
130 * a mapped PCI device at a time.
132 mutex_lock(&punit_dev->mutex);
133 ret = isst_if_mbox_cmd(pdev, mbox_cmd);
134 if (!ret && !resume && isst_if_mbox_cmd_set_req(mbox_cmd))
135 ret = isst_store_cmd(mbox_cmd->command,
136 mbox_cmd->sub_command,
137 mbox_cmd->logical_cpu, 1,
140 mutex_unlock(&punit_dev->mutex);
149 static const struct pci_device_id isst_if_mbox_ids[] = {
150 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, INTEL_CFG_MBOX_DEVID_0)},
153 MODULE_DEVICE_TABLE(pci, isst_if_mbox_ids);
155 static int isst_if_mbox_probe(struct pci_dev *pdev,
156 const struct pci_device_id *ent)
158 struct isst_if_device *punit_dev;
159 struct isst_if_cmd_cb cb;
162 punit_dev = devm_kzalloc(&pdev->dev, sizeof(*punit_dev), GFP_KERNEL);
166 ret = pcim_enable_device(pdev);
170 mutex_init(&punit_dev->mutex);
171 pci_set_drvdata(pdev, punit_dev);
173 memset(&cb, 0, sizeof(cb));
174 cb.cmd_size = sizeof(struct isst_if_mbox_cmd);
175 cb.offset = offsetof(struct isst_if_mbox_cmds, mbox_cmd);
176 cb.cmd_callback = isst_if_mbox_proc_cmd;
177 cb.owner = THIS_MODULE;
178 ret = isst_if_cdev_register(ISST_IF_DEV_MBOX, &cb);
181 mutex_destroy(&punit_dev->mutex);
186 static void isst_if_mbox_remove(struct pci_dev *pdev)
188 struct isst_if_device *punit_dev;
190 punit_dev = pci_get_drvdata(pdev);
191 isst_if_cdev_unregister(ISST_IF_DEV_MBOX);
192 mutex_destroy(&punit_dev->mutex);
195 static int __maybe_unused isst_if_resume(struct device *device)
197 isst_resume_common();
201 static SIMPLE_DEV_PM_OPS(isst_if_pm_ops, NULL, isst_if_resume);
203 static struct pci_driver isst_if_pci_driver = {
204 .name = "isst_if_mbox_pci",
205 .id_table = isst_if_mbox_ids,
206 .probe = isst_if_mbox_probe,
207 .remove = isst_if_mbox_remove,
208 .driver.pm = &isst_if_pm_ops,
211 module_pci_driver(isst_if_pci_driver);
213 MODULE_LICENSE("GPL v2");
214 MODULE_DESCRIPTION("Intel speed select interface pci mailbox driver");