xref: /linux/drivers/misc/mei/gsc-me.c (revision 0995c95b0882ee0ed0ea1930c8918bb0899e924c)
1a98c30fdSTomas Winkler // SPDX-License-Identifier: GPL-2.0
2a98c30fdSTomas Winkler /*
3a98c30fdSTomas Winkler  * Copyright(c) 2019-2022, Intel Corporation. All rights reserved.
4a98c30fdSTomas Winkler  *
5a98c30fdSTomas Winkler  * Intel Management Engine Interface (Intel MEI) Linux driver
6a98c30fdSTomas Winkler  */
7a98c30fdSTomas Winkler 
8a98c30fdSTomas Winkler #include <linux/module.h>
9a98c30fdSTomas Winkler #include <linux/mei_aux.h>
10a98c30fdSTomas Winkler #include <linux/device.h>
11a98c30fdSTomas Winkler #include <linux/irqreturn.h>
12a98c30fdSTomas Winkler #include <linux/jiffies.h>
13a98c30fdSTomas Winkler #include <linux/ktime.h>
14a98c30fdSTomas Winkler #include <linux/delay.h>
15a98c30fdSTomas Winkler #include <linux/pm_runtime.h>
165b063995STomas Winkler #include <linux/kthread.h>
17a98c30fdSTomas Winkler 
18a98c30fdSTomas Winkler #include "mei_dev.h"
19a98c30fdSTomas Winkler #include "hw-me.h"
20a98c30fdSTomas Winkler #include "hw-me-regs.h"
21a98c30fdSTomas Winkler 
22a98c30fdSTomas Winkler #include "mei-trace.h"
23a98c30fdSTomas Winkler 
24a98c30fdSTomas Winkler #define MEI_GSC_RPM_TIMEOUT 500
25a98c30fdSTomas Winkler 
26a98c30fdSTomas Winkler static int mei_gsc_read_hfs(const struct mei_device *dev, int where, u32 *val)
27a98c30fdSTomas Winkler {
28a98c30fdSTomas Winkler 	struct mei_me_hw *hw = to_me_hw(dev);
29a98c30fdSTomas Winkler 
30a98c30fdSTomas Winkler 	*val = ioread32(hw->mem_addr + where + 0xC00);
31a98c30fdSTomas Winkler 
32a98c30fdSTomas Winkler 	return 0;
33a98c30fdSTomas Winkler }
34a98c30fdSTomas Winkler 
35342e4c7eSTomas Winkler static void mei_gsc_set_ext_op_mem(const struct mei_me_hw *hw, struct resource *mem)
36342e4c7eSTomas Winkler {
37342e4c7eSTomas Winkler 	u32 low = lower_32_bits(mem->start);
38342e4c7eSTomas Winkler 	u32 hi  = upper_32_bits(mem->start);
39342e4c7eSTomas Winkler 	u32 limit = (resource_size(mem) / SZ_4K) | GSC_EXT_OP_MEM_VALID;
40342e4c7eSTomas Winkler 
41342e4c7eSTomas Winkler 	iowrite32(low, hw->mem_addr + H_GSC_EXT_OP_MEM_BASE_ADDR_LO_REG);
42342e4c7eSTomas Winkler 	iowrite32(hi, hw->mem_addr + H_GSC_EXT_OP_MEM_BASE_ADDR_HI_REG);
43342e4c7eSTomas Winkler 	iowrite32(limit, hw->mem_addr + H_GSC_EXT_OP_MEM_LIMIT_REG);
44342e4c7eSTomas Winkler }
45342e4c7eSTomas Winkler 
46a98c30fdSTomas Winkler static int mei_gsc_probe(struct auxiliary_device *aux_dev,
47a98c30fdSTomas Winkler 			 const struct auxiliary_device_id *aux_dev_id)
48a98c30fdSTomas Winkler {
49a98c30fdSTomas Winkler 	struct mei_aux_device *adev = auxiliary_dev_to_mei_aux_dev(aux_dev);
50a98c30fdSTomas Winkler 	struct mei_device *dev;
51a98c30fdSTomas Winkler 	struct mei_me_hw *hw;
52a98c30fdSTomas Winkler 	struct device *device;
53a98c30fdSTomas Winkler 	const struct mei_cfg *cfg;
54a98c30fdSTomas Winkler 	int ret;
55a98c30fdSTomas Winkler 
56a98c30fdSTomas Winkler 	cfg = mei_me_get_cfg(aux_dev_id->driver_data);
57a98c30fdSTomas Winkler 	if (!cfg)
58a98c30fdSTomas Winkler 		return -ENODEV;
59a98c30fdSTomas Winkler 
60a98c30fdSTomas Winkler 	device = &aux_dev->dev;
61a98c30fdSTomas Winkler 
6295953618SAlexander Usyskin 	dev = mei_me_dev_init(device, cfg, adev->slow_firmware);
63a98c30fdSTomas Winkler 	if (!dev) {
64a98c30fdSTomas Winkler 		ret = -ENOMEM;
65a98c30fdSTomas Winkler 		goto err;
66a98c30fdSTomas Winkler 	}
67a98c30fdSTomas Winkler 
68a98c30fdSTomas Winkler 	hw = to_me_hw(dev);
69a98c30fdSTomas Winkler 	hw->mem_addr = devm_ioremap_resource(device, &adev->bar);
70a98c30fdSTomas Winkler 	if (IS_ERR(hw->mem_addr)) {
71a98c30fdSTomas Winkler 		ret = PTR_ERR(hw->mem_addr);
72a98c30fdSTomas Winkler 		goto err;
73a98c30fdSTomas Winkler 	}
74a98c30fdSTomas Winkler 
75a98c30fdSTomas Winkler 	hw->irq = adev->irq;
76a98c30fdSTomas Winkler 	hw->read_fws = mei_gsc_read_hfs;
77a98c30fdSTomas Winkler 
78a98c30fdSTomas Winkler 	dev_set_drvdata(device, dev);
79a98c30fdSTomas Winkler 
80342e4c7eSTomas Winkler 	if (adev->ext_op_mem.start) {
81342e4c7eSTomas Winkler 		mei_gsc_set_ext_op_mem(hw, &adev->ext_op_mem);
82342e4c7eSTomas Winkler 		dev->pxp_mode = MEI_DEV_PXP_INIT;
83342e4c7eSTomas Winkler 	}
84342e4c7eSTomas Winkler 
855b063995STomas Winkler 	/* use polling */
865b063995STomas Winkler 	if (mei_me_hw_use_polling(hw)) {
875b063995STomas Winkler 		mei_disable_interrupts(dev);
885b063995STomas Winkler 		mei_clear_interrupts(dev);
895b063995STomas Winkler 		init_waitqueue_head(&hw->wait_active);
905b063995STomas Winkler 		hw->is_active = true; /* start in active mode for initialization */
915b063995STomas Winkler 		hw->polling_thread = kthread_run(mei_me_polling_thread, dev,
925b063995STomas Winkler 						 "kmegscirqd/%s", dev_name(device));
935b063995STomas Winkler 		if (IS_ERR(hw->polling_thread)) {
945b063995STomas Winkler 			ret = PTR_ERR(hw->polling_thread);
955b063995STomas Winkler 			dev_err(device, "unable to create kernel thread: %d\n", ret);
965b063995STomas Winkler 			goto err;
975b063995STomas Winkler 		}
985b063995STomas Winkler 	} else {
99a98c30fdSTomas Winkler 		ret = devm_request_threaded_irq(device, hw->irq,
100a98c30fdSTomas Winkler 						mei_me_irq_quick_handler,
101a98c30fdSTomas Winkler 						mei_me_irq_thread_handler,
102a98c30fdSTomas Winkler 						IRQF_ONESHOT, KBUILD_MODNAME, dev);
103a98c30fdSTomas Winkler 		if (ret) {
104a98c30fdSTomas Winkler 			dev_err(device, "irq register failed %d\n", ret);
105a98c30fdSTomas Winkler 			goto err;
106a98c30fdSTomas Winkler 		}
1075b063995STomas Winkler 	}
108a98c30fdSTomas Winkler 
109a98c30fdSTomas Winkler 	pm_runtime_get_noresume(device);
110a98c30fdSTomas Winkler 	pm_runtime_set_active(device);
111a98c30fdSTomas Winkler 	pm_runtime_enable(device);
112a98c30fdSTomas Winkler 
113ce97126dSAlexander Usyskin 	/* Continue to char device setup in spite of firmware handshake failure.
114ce97126dSAlexander Usyskin 	 * In order to provide access to the firmware status registers to the user
115ce97126dSAlexander Usyskin 	 * space via sysfs.
116ce97126dSAlexander Usyskin 	 */
117ce97126dSAlexander Usyskin 	if (mei_start(dev))
118ce97126dSAlexander Usyskin 		dev_warn(device, "init hw failure.\n");
119a98c30fdSTomas Winkler 
120a98c30fdSTomas Winkler 	pm_runtime_set_autosuspend_delay(device, MEI_GSC_RPM_TIMEOUT);
121a98c30fdSTomas Winkler 	pm_runtime_use_autosuspend(device);
122a98c30fdSTomas Winkler 
123a98c30fdSTomas Winkler 	ret = mei_register(dev, device);
124a98c30fdSTomas Winkler 	if (ret)
125a98c30fdSTomas Winkler 		goto register_err;
126a98c30fdSTomas Winkler 
127a98c30fdSTomas Winkler 	pm_runtime_put_noidle(device);
128a98c30fdSTomas Winkler 	return 0;
129a98c30fdSTomas Winkler 
130a98c30fdSTomas Winkler register_err:
131a98c30fdSTomas Winkler 	mei_stop(dev);
1325b063995STomas Winkler 	if (!mei_me_hw_use_polling(hw))
133a98c30fdSTomas Winkler 		devm_free_irq(device, hw->irq, dev);
134a98c30fdSTomas Winkler 
135a98c30fdSTomas Winkler err:
136a98c30fdSTomas Winkler 	dev_err(device, "probe failed: %d\n", ret);
137a98c30fdSTomas Winkler 	dev_set_drvdata(device, NULL);
138a98c30fdSTomas Winkler 	return ret;
139a98c30fdSTomas Winkler }
140a98c30fdSTomas Winkler 
141a98c30fdSTomas Winkler static void mei_gsc_remove(struct auxiliary_device *aux_dev)
142a98c30fdSTomas Winkler {
143a98c30fdSTomas Winkler 	struct mei_device *dev;
144a98c30fdSTomas Winkler 	struct mei_me_hw *hw;
145a98c30fdSTomas Winkler 
146a98c30fdSTomas Winkler 	dev = dev_get_drvdata(&aux_dev->dev);
147a98c30fdSTomas Winkler 	if (!dev)
148a98c30fdSTomas Winkler 		return;
149a98c30fdSTomas Winkler 
150a98c30fdSTomas Winkler 	hw = to_me_hw(dev);
151a98c30fdSTomas Winkler 
152a98c30fdSTomas Winkler 	mei_stop(dev);
153a98c30fdSTomas Winkler 
1545b063995STomas Winkler 	hw = to_me_hw(dev);
1555b063995STomas Winkler 	if (mei_me_hw_use_polling(hw))
1565b063995STomas Winkler 		kthread_stop(hw->polling_thread);
1575b063995STomas Winkler 
158a98c30fdSTomas Winkler 	mei_deregister(dev);
159a98c30fdSTomas Winkler 
160a98c30fdSTomas Winkler 	pm_runtime_disable(&aux_dev->dev);
161a98c30fdSTomas Winkler 
162a98c30fdSTomas Winkler 	mei_disable_interrupts(dev);
1635b063995STomas Winkler 	if (!mei_me_hw_use_polling(hw))
164a98c30fdSTomas Winkler 		devm_free_irq(&aux_dev->dev, hw->irq, dev);
165a98c30fdSTomas Winkler }
166a98c30fdSTomas Winkler 
167a98c30fdSTomas Winkler static int __maybe_unused mei_gsc_pm_suspend(struct device *device)
168a98c30fdSTomas Winkler {
169a98c30fdSTomas Winkler 	struct mei_device *dev = dev_get_drvdata(device);
170a98c30fdSTomas Winkler 
171a98c30fdSTomas Winkler 	if (!dev)
172a98c30fdSTomas Winkler 		return -ENODEV;
173a98c30fdSTomas Winkler 
174a98c30fdSTomas Winkler 	mei_stop(dev);
175a98c30fdSTomas Winkler 
176a98c30fdSTomas Winkler 	mei_disable_interrupts(dev);
177a98c30fdSTomas Winkler 
178a98c30fdSTomas Winkler 	return 0;
179a98c30fdSTomas Winkler }
180a98c30fdSTomas Winkler 
181a98c30fdSTomas Winkler static int __maybe_unused mei_gsc_pm_resume(struct device *device)
182a98c30fdSTomas Winkler {
183a98c30fdSTomas Winkler 	struct mei_device *dev = dev_get_drvdata(device);
184bc9abe0eSVitaly Lubart 	struct auxiliary_device *aux_dev;
185bc9abe0eSVitaly Lubart 	struct mei_aux_device *adev;
186a98c30fdSTomas Winkler 	int err;
187bc9abe0eSVitaly Lubart 	struct mei_me_hw *hw;
188a98c30fdSTomas Winkler 
189a98c30fdSTomas Winkler 	if (!dev)
190a98c30fdSTomas Winkler 		return -ENODEV;
191a98c30fdSTomas Winkler 
192bc9abe0eSVitaly Lubart 	hw = to_me_hw(dev);
193bc9abe0eSVitaly Lubart 	aux_dev = to_auxiliary_dev(device);
194bc9abe0eSVitaly Lubart 	adev = auxiliary_dev_to_mei_aux_dev(aux_dev);
195bc9abe0eSVitaly Lubart 	if (adev->ext_op_mem.start) {
196bc9abe0eSVitaly Lubart 		mei_gsc_set_ext_op_mem(hw, &adev->ext_op_mem);
197bc9abe0eSVitaly Lubart 		dev->pxp_mode = MEI_DEV_PXP_INIT;
198bc9abe0eSVitaly Lubart 	}
199bc9abe0eSVitaly Lubart 
200a98c30fdSTomas Winkler 	err = mei_restart(dev);
201a98c30fdSTomas Winkler 	if (err)
202a98c30fdSTomas Winkler 		return err;
203a98c30fdSTomas Winkler 
204a98c30fdSTomas Winkler 	/* Start timer if stopped in suspend */
205a98c30fdSTomas Winkler 	schedule_delayed_work(&dev->timer_work, HZ);
206a98c30fdSTomas Winkler 
207a98c30fdSTomas Winkler 	return 0;
208a98c30fdSTomas Winkler }
209a98c30fdSTomas Winkler 
210ad10a354STomas Winkler static int __maybe_unused mei_gsc_pm_runtime_idle(struct device *device)
211ad10a354STomas Winkler {
212ad10a354STomas Winkler 	struct mei_device *dev = dev_get_drvdata(device);
213ad10a354STomas Winkler 
214ad10a354STomas Winkler 	if (!dev)
215ad10a354STomas Winkler 		return -ENODEV;
216ad10a354STomas Winkler 	if (mei_write_is_idle(dev))
217ad10a354STomas Winkler 		pm_runtime_autosuspend(device);
218ad10a354STomas Winkler 
219ad10a354STomas Winkler 	return -EBUSY;
220ad10a354STomas Winkler }
221ad10a354STomas Winkler 
222ad10a354STomas Winkler static int  __maybe_unused mei_gsc_pm_runtime_suspend(struct device *device)
223ad10a354STomas Winkler {
224ad10a354STomas Winkler 	struct mei_device *dev = dev_get_drvdata(device);
225ad10a354STomas Winkler 	struct mei_me_hw *hw;
226ad10a354STomas Winkler 	int ret;
227ad10a354STomas Winkler 
228ad10a354STomas Winkler 	if (!dev)
229ad10a354STomas Winkler 		return -ENODEV;
230ad10a354STomas Winkler 
231ad10a354STomas Winkler 	mutex_lock(&dev->device_lock);
232ad10a354STomas Winkler 
233ad10a354STomas Winkler 	if (mei_write_is_idle(dev)) {
234ad10a354STomas Winkler 		hw = to_me_hw(dev);
235ad10a354STomas Winkler 		hw->pg_state = MEI_PG_ON;
2365b063995STomas Winkler 
2375b063995STomas Winkler 		if (mei_me_hw_use_polling(hw))
2385b063995STomas Winkler 			hw->is_active = false;
239ad10a354STomas Winkler 		ret = 0;
240ad10a354STomas Winkler 	} else {
241ad10a354STomas Winkler 		ret = -EAGAIN;
242ad10a354STomas Winkler 	}
243ad10a354STomas Winkler 
244ad10a354STomas Winkler 	mutex_unlock(&dev->device_lock);
245ad10a354STomas Winkler 
246ad10a354STomas Winkler 	return ret;
247ad10a354STomas Winkler }
248ad10a354STomas Winkler 
249ad10a354STomas Winkler static int __maybe_unused mei_gsc_pm_runtime_resume(struct device *device)
250ad10a354STomas Winkler {
251ad10a354STomas Winkler 	struct mei_device *dev = dev_get_drvdata(device);
252ad10a354STomas Winkler 	struct mei_me_hw *hw;
253ad10a354STomas Winkler 	irqreturn_t irq_ret;
254ad10a354STomas Winkler 
255ad10a354STomas Winkler 	if (!dev)
256ad10a354STomas Winkler 		return -ENODEV;
257ad10a354STomas Winkler 
258ad10a354STomas Winkler 	mutex_lock(&dev->device_lock);
259ad10a354STomas Winkler 
260ad10a354STomas Winkler 	hw = to_me_hw(dev);
261ad10a354STomas Winkler 	hw->pg_state = MEI_PG_OFF;
262ad10a354STomas Winkler 
2635b063995STomas Winkler 	if (mei_me_hw_use_polling(hw)) {
2645b063995STomas Winkler 		hw->is_active = true;
2655b063995STomas Winkler 		wake_up(&hw->wait_active);
2665b063995STomas Winkler 	}
2675b063995STomas Winkler 
268ad10a354STomas Winkler 	mutex_unlock(&dev->device_lock);
269ad10a354STomas Winkler 
270ad10a354STomas Winkler 	irq_ret = mei_me_irq_thread_handler(1, dev);
271ad10a354STomas Winkler 	if (irq_ret != IRQ_HANDLED)
272ad10a354STomas Winkler 		dev_err(dev->dev, "thread handler fail %d\n", irq_ret);
273ad10a354STomas Winkler 
274ad10a354STomas Winkler 	return 0;
275ad10a354STomas Winkler }
276ad10a354STomas Winkler 
277ad10a354STomas Winkler static const struct dev_pm_ops mei_gsc_pm_ops = {
278ad10a354STomas Winkler 	SET_SYSTEM_SLEEP_PM_OPS(mei_gsc_pm_suspend,
279ad10a354STomas Winkler 				mei_gsc_pm_resume)
280ad10a354STomas Winkler 	SET_RUNTIME_PM_OPS(mei_gsc_pm_runtime_suspend,
281ad10a354STomas Winkler 			   mei_gsc_pm_runtime_resume,
282ad10a354STomas Winkler 			   mei_gsc_pm_runtime_idle)
283ad10a354STomas Winkler };
284a98c30fdSTomas Winkler 
285a98c30fdSTomas Winkler static const struct auxiliary_device_id mei_gsc_id_table[] = {
286a98c30fdSTomas Winkler 	{
287a98c30fdSTomas Winkler 		.name = "i915.mei-gsc",
288a98c30fdSTomas Winkler 		.driver_data = MEI_ME_GSC_CFG,
289a98c30fdSTomas Winkler 
290a98c30fdSTomas Winkler 	},
291a98c30fdSTomas Winkler 	{
292a98c30fdSTomas Winkler 		.name = "i915.mei-gscfi",
293a98c30fdSTomas Winkler 		.driver_data = MEI_ME_GSCFI_CFG,
294a98c30fdSTomas Winkler 	},
295a98c30fdSTomas Winkler 	{
296a98c30fdSTomas Winkler 		/* sentinel */
297a98c30fdSTomas Winkler 	}
298a98c30fdSTomas Winkler };
299a98c30fdSTomas Winkler MODULE_DEVICE_TABLE(auxiliary, mei_gsc_id_table);
300a98c30fdSTomas Winkler 
301a98c30fdSTomas Winkler static struct auxiliary_driver mei_gsc_driver = {
302a98c30fdSTomas Winkler 	.probe	= mei_gsc_probe,
303a98c30fdSTomas Winkler 	.remove = mei_gsc_remove,
304a98c30fdSTomas Winkler 	.driver = {
305a98c30fdSTomas Winkler 		/* auxiliary_driver_register() sets .name to be the modname */
306a98c30fdSTomas Winkler 		.pm = &mei_gsc_pm_ops,
307a98c30fdSTomas Winkler 	},
308a98c30fdSTomas Winkler 	.id_table = mei_gsc_id_table
309a98c30fdSTomas Winkler };
310a98c30fdSTomas Winkler module_auxiliary_driver(mei_gsc_driver);
311a98c30fdSTomas Winkler 
312a98c30fdSTomas Winkler MODULE_AUTHOR("Intel Corporation");
313a98c30fdSTomas Winkler MODULE_ALIAS("auxiliary:i915.mei-gsc");
314a98c30fdSTomas Winkler MODULE_ALIAS("auxiliary:i915.mei-gscfi");
315*0995c95bSTomas Winkler MODULE_DESCRIPTION("Intel(R) Graphics System Controller");
316a98c30fdSTomas Winkler MODULE_LICENSE("GPL");
317