xref: /openbmc/linux/drivers/misc/mei/gsc-me.c (revision a98c30fd)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright(c) 2019-2022, Intel Corporation. All rights reserved.
4  *
5  * Intel Management Engine Interface (Intel MEI) Linux driver
6  */
7 
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 
17 #include "mei_dev.h"
18 #include "hw-me.h"
19 #include "hw-me-regs.h"
20 
21 #include "mei-trace.h"
22 
23 #define MEI_GSC_RPM_TIMEOUT 500
24 
25 static int mei_gsc_read_hfs(const struct mei_device *dev, int where, u32 *val)
26 {
27 	struct mei_me_hw *hw = to_me_hw(dev);
28 
29 	*val = ioread32(hw->mem_addr + where + 0xC00);
30 
31 	return 0;
32 }
33 
34 static int mei_gsc_probe(struct auxiliary_device *aux_dev,
35 			 const struct auxiliary_device_id *aux_dev_id)
36 {
37 	struct mei_aux_device *adev = auxiliary_dev_to_mei_aux_dev(aux_dev);
38 	struct mei_device *dev;
39 	struct mei_me_hw *hw;
40 	struct device *device;
41 	const struct mei_cfg *cfg;
42 	int ret;
43 
44 	cfg = mei_me_get_cfg(aux_dev_id->driver_data);
45 	if (!cfg)
46 		return -ENODEV;
47 
48 	device = &aux_dev->dev;
49 
50 	dev = mei_me_dev_init(device, cfg);
51 	if (!dev) {
52 		ret = -ENOMEM;
53 		goto err;
54 	}
55 
56 	hw = to_me_hw(dev);
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);
61 		goto err;
62 	}
63 
64 	hw->irq = adev->irq;
65 	hw->read_fws = mei_gsc_read_hfs;
66 
67 	dev_set_drvdata(device, dev);
68 
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);
73 	if (ret) {
74 		dev_err(device, "irq register failed %d\n", ret);
75 		goto err;
76 	}
77 
78 	pm_runtime_get_noresume(device);
79 	pm_runtime_set_active(device);
80 	pm_runtime_enable(device);
81 
82 	if (mei_start(dev)) {
83 		dev_err(device, "init hw failure.\n");
84 		ret = -ENODEV;
85 		goto irq_err;
86 	}
87 
88 	pm_runtime_set_autosuspend_delay(device, MEI_GSC_RPM_TIMEOUT);
89 	pm_runtime_use_autosuspend(device);
90 
91 	ret = mei_register(dev, device);
92 	if (ret)
93 		goto register_err;
94 
95 	pm_runtime_put_noidle(device);
96 	return 0;
97 
98 register_err:
99 	mei_stop(dev);
100 irq_err:
101 	devm_free_irq(device, hw->irq, dev);
102 
103 err:
104 	dev_err(device, "probe failed: %d\n", ret);
105 	dev_set_drvdata(device, NULL);
106 	return ret;
107 }
108 
109 static void mei_gsc_remove(struct auxiliary_device *aux_dev)
110 {
111 	struct mei_device *dev;
112 	struct mei_me_hw *hw;
113 
114 	dev = dev_get_drvdata(&aux_dev->dev);
115 	if (!dev)
116 		return;
117 
118 	hw = to_me_hw(dev);
119 
120 	mei_stop(dev);
121 
122 	mei_deregister(dev);
123 
124 	pm_runtime_disable(&aux_dev->dev);
125 
126 	mei_disable_interrupts(dev);
127 	devm_free_irq(&aux_dev->dev, hw->irq, dev);
128 }
129 
130 static int __maybe_unused mei_gsc_pm_suspend(struct device *device)
131 {
132 	struct mei_device *dev = dev_get_drvdata(device);
133 
134 	if (!dev)
135 		return -ENODEV;
136 
137 	mei_stop(dev);
138 
139 	mei_disable_interrupts(dev);
140 
141 	return 0;
142 }
143 
144 static int __maybe_unused mei_gsc_pm_resume(struct device *device)
145 {
146 	struct mei_device *dev = dev_get_drvdata(device);
147 	int err;
148 
149 	if (!dev)
150 		return -ENODEV;
151 
152 	err = mei_restart(dev);
153 	if (err)
154 		return err;
155 
156 	/* Start timer if stopped in suspend */
157 	schedule_delayed_work(&dev->timer_work, HZ);
158 
159 	return 0;
160 }
161 
162 static SIMPLE_DEV_PM_OPS(mei_gsc_pm_ops, mei_gsc_pm_suspend, mei_gsc_pm_resume);
163 
164 static const struct auxiliary_device_id mei_gsc_id_table[] = {
165 	{
166 		.name = "i915.mei-gsc",
167 		.driver_data = MEI_ME_GSC_CFG,
168 
169 	},
170 	{
171 		.name = "i915.mei-gscfi",
172 		.driver_data = MEI_ME_GSCFI_CFG,
173 	},
174 	{
175 		/* sentinel */
176 	}
177 };
178 MODULE_DEVICE_TABLE(auxiliary, mei_gsc_id_table);
179 
180 static struct auxiliary_driver mei_gsc_driver = {
181 	.probe	= mei_gsc_probe,
182 	.remove = mei_gsc_remove,
183 	.driver = {
184 		/* auxiliary_driver_register() sets .name to be the modname */
185 		.pm = &mei_gsc_pm_ops,
186 	},
187 	.id_table = mei_gsc_id_table
188 };
189 module_auxiliary_driver(mei_gsc_driver);
190 
191 MODULE_AUTHOR("Intel Corporation");
192 MODULE_ALIAS("auxiliary:i915.mei-gsc");
193 MODULE_ALIAS("auxiliary:i915.mei-gscfi");
194 MODULE_LICENSE("GPL");
195