xref: /linux/drivers/misc/mei/gsc-me.c (revision cb4eb6771c0f8fd1c52a8f6fdec7762fb087380a)
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 #include <linux/kthread.h>
17 
18 #include "mei_dev.h"
19 #include "hw-me.h"
20 #include "hw-me-regs.h"
21 
22 #include "mei-trace.h"
23 
24 #define MEI_GSC_RPM_TIMEOUT 500
25 
mei_gsc_read_hfs(const struct mei_device * dev,int where,const char * name,u32 * val)26 static int mei_gsc_read_hfs(const struct mei_device *dev, int where, const char *name, u32 *val)
27 {
28 	struct mei_me_hw *hw = to_me_hw(dev);
29 
30 	*val = ioread32(hw->mem_addr + where + 0xC00);
31 	trace_mei_reg_read(&dev->dev, name, where, *val);
32 
33 	return 0;
34 }
35 
mei_gsc_set_ext_op_mem(const struct mei_me_hw * hw,struct resource * mem)36 static void mei_gsc_set_ext_op_mem(const struct mei_me_hw *hw, struct resource *mem)
37 {
38 	u32 low = lower_32_bits(mem->start);
39 	u32 hi  = upper_32_bits(mem->start);
40 	u32 limit = (resource_size(mem) / SZ_4K) | GSC_EXT_OP_MEM_VALID;
41 
42 	iowrite32(low, hw->mem_addr + H_GSC_EXT_OP_MEM_BASE_ADDR_LO_REG);
43 	iowrite32(hi, hw->mem_addr + H_GSC_EXT_OP_MEM_BASE_ADDR_HI_REG);
44 	iowrite32(limit, hw->mem_addr + H_GSC_EXT_OP_MEM_LIMIT_REG);
45 }
46 
mei_gsc_probe(struct auxiliary_device * aux_dev,const struct auxiliary_device_id * aux_dev_id)47 static int mei_gsc_probe(struct auxiliary_device *aux_dev,
48 			 const struct auxiliary_device_id *aux_dev_id)
49 {
50 	struct mei_aux_device *adev = auxiliary_dev_to_mei_aux_dev(aux_dev);
51 	struct mei_device *dev;
52 	struct mei_me_hw *hw;
53 	struct device *device;
54 	const struct mei_cfg *cfg;
55 	int ret;
56 
57 	cfg = mei_me_get_cfg(aux_dev_id->driver_data);
58 	if (!cfg)
59 		return -ENODEV;
60 
61 	device = &aux_dev->dev;
62 
63 	dev = mei_me_dev_init(device, cfg, adev->slow_firmware);
64 	if (!dev) {
65 		ret = -ENOMEM;
66 		goto err;
67 	}
68 
69 	hw = to_me_hw(dev);
70 	hw->mem_addr = devm_ioremap_resource(device, &adev->bar);
71 	if (IS_ERR(hw->mem_addr)) {
72 		ret = PTR_ERR(hw->mem_addr);
73 		goto err;
74 	}
75 
76 	hw->irq = adev->irq;
77 	hw->read_fws = mei_gsc_read_hfs;
78 
79 	dev_set_drvdata(device, dev);
80 
81 	if (adev->ext_op_mem.start) {
82 		mei_gsc_set_ext_op_mem(hw, &adev->ext_op_mem);
83 		dev->pxp_mode = MEI_DEV_PXP_INIT;
84 	}
85 
86 	/* use polling */
87 	if (mei_me_hw_use_polling(hw)) {
88 		mei_disable_interrupts(dev);
89 		mei_clear_interrupts(dev);
90 		init_waitqueue_head(&hw->wait_active);
91 		hw->is_active = true; /* start in active mode for initialization */
92 		hw->polling_thread = kthread_run(mei_me_polling_thread, dev,
93 						 "kmegscirqd/%s", dev_name(device));
94 		if (IS_ERR(hw->polling_thread)) {
95 			ret = PTR_ERR(hw->polling_thread);
96 			dev_err(device, "unable to create kernel thread: %d\n", ret);
97 			goto err;
98 		}
99 	} else {
100 		ret = devm_request_threaded_irq(device, hw->irq,
101 						mei_me_irq_quick_handler,
102 						mei_me_irq_thread_handler,
103 						IRQF_ONESHOT, KBUILD_MODNAME, dev);
104 		if (ret) {
105 			dev_err(device, "irq register failed %d\n", ret);
106 			goto err;
107 		}
108 	}
109 
110 	ret = mei_register(dev, device);
111 	if (ret)
112 		goto deinterrupt;
113 
114 	pm_runtime_get_noresume(device);
115 	pm_runtime_set_active(device);
116 	pm_runtime_enable(device);
117 
118 	/* Continue in spite of firmware handshake failure.
119 	 * In order to provide access to the firmware status registers to the user
120 	 * space via sysfs.
121 	 */
122 	if (mei_start(dev))
123 		dev_warn(device, "init hw failure.\n");
124 
125 	pm_runtime_set_autosuspend_delay(device, MEI_GSC_RPM_TIMEOUT);
126 	pm_runtime_use_autosuspend(device);
127 
128 	pm_runtime_put_noidle(device);
129 	return 0;
130 
131 deinterrupt:
132 	if (!mei_me_hw_use_polling(hw))
133 		devm_free_irq(device, hw->irq, dev);
134 err:
135 	dev_err(device, "probe failed: %d\n", ret);
136 	dev_set_drvdata(device, NULL);
137 	return ret;
138 }
139 
mei_gsc_remove(struct auxiliary_device * aux_dev)140 static void mei_gsc_remove(struct auxiliary_device *aux_dev)
141 {
142 	struct mei_device *dev;
143 	struct mei_me_hw *hw;
144 
145 	dev = dev_get_drvdata(&aux_dev->dev);
146 	hw = to_me_hw(dev);
147 
148 	mei_stop(dev);
149 
150 	hw = to_me_hw(dev);
151 	if (mei_me_hw_use_polling(hw))
152 		kthread_stop(hw->polling_thread);
153 
154 	pm_runtime_disable(&aux_dev->dev);
155 
156 	mei_disable_interrupts(dev);
157 	if (!mei_me_hw_use_polling(hw))
158 		devm_free_irq(&aux_dev->dev, hw->irq, dev);
159 
160 	mei_deregister(dev);
161 }
162 
mei_gsc_pm_suspend(struct device * device)163 static int __maybe_unused mei_gsc_pm_suspend(struct device *device)
164 {
165 	struct mei_device *dev = dev_get_drvdata(device);
166 
167 	mei_stop(dev);
168 
169 	mei_disable_interrupts(dev);
170 
171 	return 0;
172 }
173 
mei_gsc_pm_resume(struct device * device)174 static int __maybe_unused mei_gsc_pm_resume(struct device *device)
175 {
176 	struct mei_device *dev = dev_get_drvdata(device);
177 	struct auxiliary_device *aux_dev;
178 	struct mei_aux_device *adev;
179 	int err;
180 	struct mei_me_hw *hw;
181 
182 	hw = to_me_hw(dev);
183 	aux_dev = to_auxiliary_dev(device);
184 	adev = auxiliary_dev_to_mei_aux_dev(aux_dev);
185 	if (adev->ext_op_mem.start) {
186 		mei_gsc_set_ext_op_mem(hw, &adev->ext_op_mem);
187 		dev->pxp_mode = MEI_DEV_PXP_INIT;
188 	}
189 
190 	err = mei_restart(dev);
191 	if (err)
192 		return err;
193 
194 	/* Start timer if stopped in suspend */
195 	schedule_delayed_work(&dev->timer_work, HZ);
196 
197 	return 0;
198 }
199 
mei_gsc_pm_runtime_idle(struct device * device)200 static int __maybe_unused mei_gsc_pm_runtime_idle(struct device *device)
201 {
202 	struct mei_device *dev = dev_get_drvdata(device);
203 
204 	if (mei_write_is_idle(dev))
205 		pm_runtime_autosuspend(device);
206 
207 	return -EBUSY;
208 }
209 
mei_gsc_pm_runtime_suspend(struct device * device)210 static int  __maybe_unused mei_gsc_pm_runtime_suspend(struct device *device)
211 {
212 	struct mei_device *dev = dev_get_drvdata(device);
213 	struct mei_me_hw *hw;
214 	int ret;
215 
216 	mutex_lock(&dev->device_lock);
217 
218 	if (mei_write_is_idle(dev)) {
219 		hw = to_me_hw(dev);
220 		hw->pg_state = MEI_PG_ON;
221 
222 		if (mei_me_hw_use_polling(hw))
223 			hw->is_active = false;
224 		ret = 0;
225 	} else {
226 		ret = -EAGAIN;
227 	}
228 
229 	mutex_unlock(&dev->device_lock);
230 
231 	return ret;
232 }
233 
mei_gsc_pm_runtime_resume(struct device * device)234 static int __maybe_unused mei_gsc_pm_runtime_resume(struct device *device)
235 {
236 	struct mei_device *dev = dev_get_drvdata(device);
237 	struct mei_me_hw *hw;
238 	irqreturn_t irq_ret;
239 
240 	mutex_lock(&dev->device_lock);
241 
242 	hw = to_me_hw(dev);
243 	hw->pg_state = MEI_PG_OFF;
244 
245 	if (mei_me_hw_use_polling(hw)) {
246 		hw->is_active = true;
247 		wake_up(&hw->wait_active);
248 	}
249 
250 	mutex_unlock(&dev->device_lock);
251 
252 	irq_ret = mei_me_irq_thread_handler(1, dev);
253 	if (irq_ret != IRQ_HANDLED)
254 		dev_err(&dev->dev, "thread handler fail %d\n", irq_ret);
255 
256 	return 0;
257 }
258 
259 static const struct dev_pm_ops mei_gsc_pm_ops = {
260 	SET_SYSTEM_SLEEP_PM_OPS(mei_gsc_pm_suspend,
261 				mei_gsc_pm_resume)
262 	SET_RUNTIME_PM_OPS(mei_gsc_pm_runtime_suspend,
263 			   mei_gsc_pm_runtime_resume,
264 			   mei_gsc_pm_runtime_idle)
265 };
266 
267 static const struct auxiliary_device_id mei_gsc_id_table[] = {
268 	{
269 		.name = "i915.mei-gsc",
270 		.driver_data = MEI_ME_GSC_CFG,
271 
272 	},
273 	{
274 		.name = "i915.mei-gscfi",
275 		.driver_data = MEI_ME_GSCFI_CFG,
276 	},
277 	{
278 		.name = "xe.mei-gscfi",
279 		.driver_data = MEI_ME_GSCFI_CFG,
280 	},
281 	{
282 		/* sentinel */
283 	}
284 };
285 MODULE_DEVICE_TABLE(auxiliary, mei_gsc_id_table);
286 
287 static struct auxiliary_driver mei_gsc_driver = {
288 	.probe	= mei_gsc_probe,
289 	.remove = mei_gsc_remove,
290 	.driver = {
291 		/* auxiliary_driver_register() sets .name to be the modname */
292 		.pm = &mei_gsc_pm_ops,
293 	},
294 	.id_table = mei_gsc_id_table
295 };
296 module_auxiliary_driver(mei_gsc_driver);
297 
298 MODULE_AUTHOR("Intel Corporation");
299 MODULE_ALIAS("auxiliary:i915.mei-gsc");
300 MODULE_ALIAS("auxiliary:i915.mei-gscfi");
301 MODULE_ALIAS("auxiliary:xe.mei-gscfi");
302 MODULE_DESCRIPTION("Intel(R) Graphics System Controller");
303 MODULE_LICENSE("GPL");
304