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>
16 #include <linux/kthread.h>
20 #include "hw-me-regs.h"
22 #include "mei-trace.h"
24 #define MEI_GSC_RPM_TIMEOUT 500
26 static int mei_gsc_read_hfs(const struct mei_device *dev, int where, u32 *val)
28 struct mei_me_hw *hw = to_me_hw(dev);
30 *val = ioread32(hw->mem_addr + where + 0xC00);
35 static void mei_gsc_set_ext_op_mem(const struct mei_me_hw *hw, struct resource *mem)
37 u32 low = lower_32_bits(mem->start);
38 u32 hi = upper_32_bits(mem->start);
39 u32 limit = (resource_size(mem) / SZ_4K) | GSC_EXT_OP_MEM_VALID;
41 iowrite32(low, hw->mem_addr + H_GSC_EXT_OP_MEM_BASE_ADDR_LO_REG);
42 iowrite32(hi, hw->mem_addr + H_GSC_EXT_OP_MEM_BASE_ADDR_HI_REG);
43 iowrite32(limit, hw->mem_addr + H_GSC_EXT_OP_MEM_LIMIT_REG);
46 static int mei_gsc_probe(struct auxiliary_device *aux_dev,
47 const struct auxiliary_device_id *aux_dev_id)
49 struct mei_aux_device *adev = auxiliary_dev_to_mei_aux_dev(aux_dev);
50 struct mei_device *dev;
52 struct device *device;
53 const struct mei_cfg *cfg;
56 cfg = mei_me_get_cfg(aux_dev_id->driver_data);
60 device = &aux_dev->dev;
62 dev = mei_me_dev_init(device, cfg, adev->slow_firmware);
69 hw->mem_addr = devm_ioremap_resource(device, &adev->bar);
70 if (IS_ERR(hw->mem_addr)) {
71 ret = PTR_ERR(hw->mem_addr);
76 hw->read_fws = mei_gsc_read_hfs;
78 dev_set_drvdata(device, dev);
80 if (adev->ext_op_mem.start) {
81 mei_gsc_set_ext_op_mem(hw, &adev->ext_op_mem);
82 dev->pxp_mode = MEI_DEV_PXP_INIT;
86 if (mei_me_hw_use_polling(hw)) {
87 mei_disable_interrupts(dev);
88 mei_clear_interrupts(dev);
89 init_waitqueue_head(&hw->wait_active);
90 hw->is_active = true; /* start in active mode for initialization */
91 hw->polling_thread = kthread_run(mei_me_polling_thread, dev,
92 "kmegscirqd/%s", dev_name(device));
93 if (IS_ERR(hw->polling_thread)) {
94 ret = PTR_ERR(hw->polling_thread);
95 dev_err(device, "unable to create kernel thread: %d\n", ret);
99 ret = devm_request_threaded_irq(device, hw->irq,
100 mei_me_irq_quick_handler,
101 mei_me_irq_thread_handler,
102 IRQF_ONESHOT, KBUILD_MODNAME, dev);
104 dev_err(device, "irq register failed %d\n", ret);
109 pm_runtime_get_noresume(device);
110 pm_runtime_set_active(device);
111 pm_runtime_enable(device);
113 /* Continue to char device setup in spite of firmware handshake failure.
114 * In order to provide access to the firmware status registers to the user
118 dev_warn(device, "init hw failure.\n");
120 pm_runtime_set_autosuspend_delay(device, MEI_GSC_RPM_TIMEOUT);
121 pm_runtime_use_autosuspend(device);
123 ret = mei_register(dev, device);
127 pm_runtime_put_noidle(device);
132 if (!mei_me_hw_use_polling(hw))
133 devm_free_irq(device, hw->irq, dev);
136 dev_err(device, "probe failed: %d\n", ret);
137 dev_set_drvdata(device, NULL);
141 static void mei_gsc_remove(struct auxiliary_device *aux_dev)
143 struct mei_device *dev;
144 struct mei_me_hw *hw;
146 dev = dev_get_drvdata(&aux_dev->dev);
155 if (mei_me_hw_use_polling(hw))
156 kthread_stop(hw->polling_thread);
160 pm_runtime_disable(&aux_dev->dev);
162 mei_disable_interrupts(dev);
163 if (!mei_me_hw_use_polling(hw))
164 devm_free_irq(&aux_dev->dev, hw->irq, dev);
167 static int __maybe_unused mei_gsc_pm_suspend(struct device *device)
169 struct mei_device *dev = dev_get_drvdata(device);
176 mei_disable_interrupts(dev);
181 static int __maybe_unused mei_gsc_pm_resume(struct device *device)
183 struct mei_device *dev = dev_get_drvdata(device);
184 struct auxiliary_device *aux_dev;
185 struct mei_aux_device *adev;
187 struct mei_me_hw *hw;
193 aux_dev = to_auxiliary_dev(device);
194 adev = auxiliary_dev_to_mei_aux_dev(aux_dev);
195 if (adev->ext_op_mem.start) {
196 mei_gsc_set_ext_op_mem(hw, &adev->ext_op_mem);
197 dev->pxp_mode = MEI_DEV_PXP_INIT;
200 err = mei_restart(dev);
204 /* Start timer if stopped in suspend */
205 schedule_delayed_work(&dev->timer_work, HZ);
210 static int __maybe_unused mei_gsc_pm_runtime_idle(struct device *device)
212 struct mei_device *dev = dev_get_drvdata(device);
216 if (mei_write_is_idle(dev))
217 pm_runtime_autosuspend(device);
222 static int __maybe_unused mei_gsc_pm_runtime_suspend(struct device *device)
224 struct mei_device *dev = dev_get_drvdata(device);
225 struct mei_me_hw *hw;
231 mutex_lock(&dev->device_lock);
233 if (mei_write_is_idle(dev)) {
235 hw->pg_state = MEI_PG_ON;
237 if (mei_me_hw_use_polling(hw))
238 hw->is_active = false;
244 mutex_unlock(&dev->device_lock);
249 static int __maybe_unused mei_gsc_pm_runtime_resume(struct device *device)
251 struct mei_device *dev = dev_get_drvdata(device);
252 struct mei_me_hw *hw;
258 mutex_lock(&dev->device_lock);
261 hw->pg_state = MEI_PG_OFF;
263 if (mei_me_hw_use_polling(hw)) {
264 hw->is_active = true;
265 wake_up(&hw->wait_active);
268 mutex_unlock(&dev->device_lock);
270 irq_ret = mei_me_irq_thread_handler(1, dev);
271 if (irq_ret != IRQ_HANDLED)
272 dev_err(dev->dev, "thread handler fail %d\n", irq_ret);
277 static const struct dev_pm_ops mei_gsc_pm_ops = {
278 SET_SYSTEM_SLEEP_PM_OPS(mei_gsc_pm_suspend,
280 SET_RUNTIME_PM_OPS(mei_gsc_pm_runtime_suspend,
281 mei_gsc_pm_runtime_resume,
282 mei_gsc_pm_runtime_idle)
285 static const struct auxiliary_device_id mei_gsc_id_table[] = {
287 .name = "i915.mei-gsc",
288 .driver_data = MEI_ME_GSC_CFG,
292 .name = "i915.mei-gscfi",
293 .driver_data = MEI_ME_GSCFI_CFG,
299 MODULE_DEVICE_TABLE(auxiliary, mei_gsc_id_table);
301 static struct auxiliary_driver mei_gsc_driver = {
302 .probe = mei_gsc_probe,
303 .remove = mei_gsc_remove,
305 /* auxiliary_driver_register() sets .name to be the modname */
306 .pm = &mei_gsc_pm_ops,
308 .id_table = mei_gsc_id_table
310 module_auxiliary_driver(mei_gsc_driver);
312 MODULE_AUTHOR("Intel Corporation");
313 MODULE_ALIAS("auxiliary:i915.mei-gsc");
314 MODULE_ALIAS("auxiliary:i915.mei-gscfi");
315 MODULE_LICENSE("GPL");