1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright(c) 2019-2022, Intel Corporation. All rights reserved.
5 * Intel Management Engine Interface (Intel MEI) Linux driver
8 #include <linux/module.h>
9 #include <linux/mei_aux.h>
10 #include <linux/device.h>
11 #include <linux/irqreturn.h>
12 #include <linux/jiffies.h>
13 #include <linux/ktime.h>
14 #include <linux/delay.h>
15 #include <linux/pm_runtime.h>
19 #include "hw-me-regs.h"
21 #include "mei-trace.h"
23 #define MEI_GSC_RPM_TIMEOUT 500
25 static int mei_gsc_read_hfs(const struct mei_device *dev, int where, u32 *val)
27 struct mei_me_hw *hw = to_me_hw(dev);
29 *val = ioread32(hw->mem_addr + where + 0xC00);
34 static int mei_gsc_probe(struct auxiliary_device *aux_dev,
35 const struct auxiliary_device_id *aux_dev_id)
37 struct mei_aux_device *adev = auxiliary_dev_to_mei_aux_dev(aux_dev);
38 struct mei_device *dev;
40 struct device *device;
41 const struct mei_cfg *cfg;
44 cfg = mei_me_get_cfg(aux_dev_id->driver_data);
48 device = &aux_dev->dev;
50 dev = mei_me_dev_init(device, cfg);
57 hw->mem_addr = devm_ioremap_resource(device, &adev->bar);
58 if (IS_ERR(hw->mem_addr)) {
59 dev_err(device, "mmio not mapped\n");
60 ret = PTR_ERR(hw->mem_addr);
65 hw->read_fws = mei_gsc_read_hfs;
67 dev_set_drvdata(device, dev);
69 ret = devm_request_threaded_irq(device, hw->irq,
70 mei_me_irq_quick_handler,
71 mei_me_irq_thread_handler,
72 IRQF_ONESHOT, KBUILD_MODNAME, dev);
74 dev_err(device, "irq register failed %d\n", ret);
78 pm_runtime_get_noresume(device);
79 pm_runtime_set_active(device);
80 pm_runtime_enable(device);
82 /* Continue to char device setup in spite of firmware handshake failure.
83 * In order to provide access to the firmware status registers to the user
87 dev_warn(device, "init hw failure.\n");
89 pm_runtime_set_autosuspend_delay(device, MEI_GSC_RPM_TIMEOUT);
90 pm_runtime_use_autosuspend(device);
92 ret = mei_register(dev, device);
96 pm_runtime_put_noidle(device);
101 devm_free_irq(device, hw->irq, dev);
104 dev_err(device, "probe failed: %d\n", ret);
105 dev_set_drvdata(device, NULL);
109 static void mei_gsc_remove(struct auxiliary_device *aux_dev)
111 struct mei_device *dev;
112 struct mei_me_hw *hw;
114 dev = dev_get_drvdata(&aux_dev->dev);
124 pm_runtime_disable(&aux_dev->dev);
126 mei_disable_interrupts(dev);
127 devm_free_irq(&aux_dev->dev, hw->irq, dev);
130 static int __maybe_unused mei_gsc_pm_suspend(struct device *device)
132 struct mei_device *dev = dev_get_drvdata(device);
139 mei_disable_interrupts(dev);
144 static int __maybe_unused mei_gsc_pm_resume(struct device *device)
146 struct mei_device *dev = dev_get_drvdata(device);
152 err = mei_restart(dev);
156 /* Start timer if stopped in suspend */
157 schedule_delayed_work(&dev->timer_work, HZ);
162 static int __maybe_unused mei_gsc_pm_runtime_idle(struct device *device)
164 struct mei_device *dev = dev_get_drvdata(device);
168 if (mei_write_is_idle(dev))
169 pm_runtime_autosuspend(device);
174 static int __maybe_unused mei_gsc_pm_runtime_suspend(struct device *device)
176 struct mei_device *dev = dev_get_drvdata(device);
177 struct mei_me_hw *hw;
183 mutex_lock(&dev->device_lock);
185 if (mei_write_is_idle(dev)) {
187 hw->pg_state = MEI_PG_ON;
193 mutex_unlock(&dev->device_lock);
198 static int __maybe_unused mei_gsc_pm_runtime_resume(struct device *device)
200 struct mei_device *dev = dev_get_drvdata(device);
201 struct mei_me_hw *hw;
207 mutex_lock(&dev->device_lock);
210 hw->pg_state = MEI_PG_OFF;
212 mutex_unlock(&dev->device_lock);
214 irq_ret = mei_me_irq_thread_handler(1, dev);
215 if (irq_ret != IRQ_HANDLED)
216 dev_err(dev->dev, "thread handler fail %d\n", irq_ret);
221 static const struct dev_pm_ops mei_gsc_pm_ops = {
222 SET_SYSTEM_SLEEP_PM_OPS(mei_gsc_pm_suspend,
224 SET_RUNTIME_PM_OPS(mei_gsc_pm_runtime_suspend,
225 mei_gsc_pm_runtime_resume,
226 mei_gsc_pm_runtime_idle)
229 static const struct auxiliary_device_id mei_gsc_id_table[] = {
231 .name = "i915.mei-gsc",
232 .driver_data = MEI_ME_GSC_CFG,
236 .name = "i915.mei-gscfi",
237 .driver_data = MEI_ME_GSCFI_CFG,
243 MODULE_DEVICE_TABLE(auxiliary, mei_gsc_id_table);
245 static struct auxiliary_driver mei_gsc_driver = {
246 .probe = mei_gsc_probe,
247 .remove = mei_gsc_remove,
249 /* auxiliary_driver_register() sets .name to be the modname */
250 .pm = &mei_gsc_pm_ops,
252 .id_table = mei_gsc_id_table
254 module_auxiliary_driver(mei_gsc_driver);
256 MODULE_AUTHOR("Intel Corporation");
257 MODULE_ALIAS("auxiliary:i915.mei-gsc");
258 MODULE_ALIAS("auxiliary:i915.mei-gscfi");
259 MODULE_LICENSE("GPL");