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
mei_gsc_read_hfs(const struct mei_device * dev,int where,u32 * val)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
mei_gsc_set_ext_op_mem(const struct mei_me_hw * hw,struct resource * mem)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
mei_gsc_probe(struct auxiliary_device * aux_dev,const struct auxiliary_device_id * aux_dev_id)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
mei_gsc_remove(struct auxiliary_device * aux_dev)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 hw = to_me_hw(dev);
148a98c30fdSTomas Winkler
149a98c30fdSTomas Winkler mei_stop(dev);
150a98c30fdSTomas Winkler
1515b063995STomas Winkler hw = to_me_hw(dev);
1525b063995STomas Winkler if (mei_me_hw_use_polling(hw))
1535b063995STomas Winkler kthread_stop(hw->polling_thread);
1545b063995STomas Winkler
155a98c30fdSTomas Winkler mei_deregister(dev);
156a98c30fdSTomas Winkler
157a98c30fdSTomas Winkler pm_runtime_disable(&aux_dev->dev);
158a98c30fdSTomas Winkler
159a98c30fdSTomas Winkler mei_disable_interrupts(dev);
1605b063995STomas Winkler if (!mei_me_hw_use_polling(hw))
161a98c30fdSTomas Winkler devm_free_irq(&aux_dev->dev, hw->irq, dev);
162a98c30fdSTomas Winkler }
163a98c30fdSTomas Winkler
mei_gsc_pm_suspend(struct device * device)164a98c30fdSTomas Winkler static int __maybe_unused mei_gsc_pm_suspend(struct device *device)
165a98c30fdSTomas Winkler {
166a98c30fdSTomas Winkler struct mei_device *dev = dev_get_drvdata(device);
167a98c30fdSTomas Winkler
168a98c30fdSTomas Winkler mei_stop(dev);
169a98c30fdSTomas Winkler
170a98c30fdSTomas Winkler mei_disable_interrupts(dev);
171a98c30fdSTomas Winkler
172a98c30fdSTomas Winkler return 0;
173a98c30fdSTomas Winkler }
174a98c30fdSTomas Winkler
mei_gsc_pm_resume(struct device * device)175a98c30fdSTomas Winkler static int __maybe_unused mei_gsc_pm_resume(struct device *device)
176a98c30fdSTomas Winkler {
177a98c30fdSTomas Winkler struct mei_device *dev = dev_get_drvdata(device);
178bc9abe0eSVitaly Lubart struct auxiliary_device *aux_dev;
179bc9abe0eSVitaly Lubart struct mei_aux_device *adev;
180a98c30fdSTomas Winkler int err;
181bc9abe0eSVitaly Lubart struct mei_me_hw *hw;
182a98c30fdSTomas Winkler
183bc9abe0eSVitaly Lubart hw = to_me_hw(dev);
184bc9abe0eSVitaly Lubart aux_dev = to_auxiliary_dev(device);
185bc9abe0eSVitaly Lubart adev = auxiliary_dev_to_mei_aux_dev(aux_dev);
186bc9abe0eSVitaly Lubart if (adev->ext_op_mem.start) {
187bc9abe0eSVitaly Lubart mei_gsc_set_ext_op_mem(hw, &adev->ext_op_mem);
188bc9abe0eSVitaly Lubart dev->pxp_mode = MEI_DEV_PXP_INIT;
189bc9abe0eSVitaly Lubart }
190bc9abe0eSVitaly Lubart
191a98c30fdSTomas Winkler err = mei_restart(dev);
192a98c30fdSTomas Winkler if (err)
193a98c30fdSTomas Winkler return err;
194a98c30fdSTomas Winkler
195a98c30fdSTomas Winkler /* Start timer if stopped in suspend */
196a98c30fdSTomas Winkler schedule_delayed_work(&dev->timer_work, HZ);
197a98c30fdSTomas Winkler
198a98c30fdSTomas Winkler return 0;
199a98c30fdSTomas Winkler }
200a98c30fdSTomas Winkler
mei_gsc_pm_runtime_idle(struct device * device)201ad10a354STomas Winkler static int __maybe_unused mei_gsc_pm_runtime_idle(struct device *device)
202ad10a354STomas Winkler {
203ad10a354STomas Winkler struct mei_device *dev = dev_get_drvdata(device);
204ad10a354STomas Winkler
205ad10a354STomas Winkler if (mei_write_is_idle(dev))
206ad10a354STomas Winkler pm_runtime_autosuspend(device);
207ad10a354STomas Winkler
208ad10a354STomas Winkler return -EBUSY;
209ad10a354STomas Winkler }
210ad10a354STomas Winkler
mei_gsc_pm_runtime_suspend(struct device * device)211ad10a354STomas Winkler static int __maybe_unused mei_gsc_pm_runtime_suspend(struct device *device)
212ad10a354STomas Winkler {
213ad10a354STomas Winkler struct mei_device *dev = dev_get_drvdata(device);
214ad10a354STomas Winkler struct mei_me_hw *hw;
215ad10a354STomas Winkler int ret;
216ad10a354STomas Winkler
217ad10a354STomas Winkler mutex_lock(&dev->device_lock);
218ad10a354STomas Winkler
219ad10a354STomas Winkler if (mei_write_is_idle(dev)) {
220ad10a354STomas Winkler hw = to_me_hw(dev);
221ad10a354STomas Winkler hw->pg_state = MEI_PG_ON;
2225b063995STomas Winkler
2235b063995STomas Winkler if (mei_me_hw_use_polling(hw))
2245b063995STomas Winkler hw->is_active = false;
225ad10a354STomas Winkler ret = 0;
226ad10a354STomas Winkler } else {
227ad10a354STomas Winkler ret = -EAGAIN;
228ad10a354STomas Winkler }
229ad10a354STomas Winkler
230ad10a354STomas Winkler mutex_unlock(&dev->device_lock);
231ad10a354STomas Winkler
232ad10a354STomas Winkler return ret;
233ad10a354STomas Winkler }
234ad10a354STomas Winkler
mei_gsc_pm_runtime_resume(struct device * device)235ad10a354STomas Winkler static int __maybe_unused mei_gsc_pm_runtime_resume(struct device *device)
236ad10a354STomas Winkler {
237ad10a354STomas Winkler struct mei_device *dev = dev_get_drvdata(device);
238ad10a354STomas Winkler struct mei_me_hw *hw;
239ad10a354STomas Winkler irqreturn_t irq_ret;
240ad10a354STomas Winkler
241ad10a354STomas Winkler mutex_lock(&dev->device_lock);
242ad10a354STomas Winkler
243ad10a354STomas Winkler hw = to_me_hw(dev);
244ad10a354STomas Winkler hw->pg_state = MEI_PG_OFF;
245ad10a354STomas Winkler
2465b063995STomas Winkler if (mei_me_hw_use_polling(hw)) {
2475b063995STomas Winkler hw->is_active = true;
2485b063995STomas Winkler wake_up(&hw->wait_active);
2495b063995STomas Winkler }
2505b063995STomas Winkler
251ad10a354STomas Winkler mutex_unlock(&dev->device_lock);
252ad10a354STomas Winkler
253ad10a354STomas Winkler irq_ret = mei_me_irq_thread_handler(1, dev);
254ad10a354STomas Winkler if (irq_ret != IRQ_HANDLED)
255ad10a354STomas Winkler dev_err(dev->dev, "thread handler fail %d\n", irq_ret);
256ad10a354STomas Winkler
257ad10a354STomas Winkler return 0;
258ad10a354STomas Winkler }
259ad10a354STomas Winkler
260ad10a354STomas Winkler static const struct dev_pm_ops mei_gsc_pm_ops = {
261ad10a354STomas Winkler SET_SYSTEM_SLEEP_PM_OPS(mei_gsc_pm_suspend,
262ad10a354STomas Winkler mei_gsc_pm_resume)
263ad10a354STomas Winkler SET_RUNTIME_PM_OPS(mei_gsc_pm_runtime_suspend,
264ad10a354STomas Winkler mei_gsc_pm_runtime_resume,
265ad10a354STomas Winkler mei_gsc_pm_runtime_idle)
266ad10a354STomas Winkler };
267a98c30fdSTomas Winkler
268a98c30fdSTomas Winkler static const struct auxiliary_device_id mei_gsc_id_table[] = {
269a98c30fdSTomas Winkler {
270a98c30fdSTomas Winkler .name = "i915.mei-gsc",
271a98c30fdSTomas Winkler .driver_data = MEI_ME_GSC_CFG,
272a98c30fdSTomas Winkler
273a98c30fdSTomas Winkler },
274a98c30fdSTomas Winkler {
275a98c30fdSTomas Winkler .name = "i915.mei-gscfi",
276a98c30fdSTomas Winkler .driver_data = MEI_ME_GSCFI_CFG,
277a98c30fdSTomas Winkler },
278a98c30fdSTomas Winkler {
279*0e59f01dSVitaly Lubart .name = "xe.mei-gscfi",
280*0e59f01dSVitaly Lubart .driver_data = MEI_ME_GSCFI_CFG,
281*0e59f01dSVitaly Lubart },
282*0e59f01dSVitaly Lubart {
283a98c30fdSTomas Winkler /* sentinel */
284a98c30fdSTomas Winkler }
285a98c30fdSTomas Winkler };
286a98c30fdSTomas Winkler MODULE_DEVICE_TABLE(auxiliary, mei_gsc_id_table);
287a98c30fdSTomas Winkler
288a98c30fdSTomas Winkler static struct auxiliary_driver mei_gsc_driver = {
289a98c30fdSTomas Winkler .probe = mei_gsc_probe,
290a98c30fdSTomas Winkler .remove = mei_gsc_remove,
291a98c30fdSTomas Winkler .driver = {
292a98c30fdSTomas Winkler /* auxiliary_driver_register() sets .name to be the modname */
293a98c30fdSTomas Winkler .pm = &mei_gsc_pm_ops,
294a98c30fdSTomas Winkler },
295a98c30fdSTomas Winkler .id_table = mei_gsc_id_table
296a98c30fdSTomas Winkler };
297a98c30fdSTomas Winkler module_auxiliary_driver(mei_gsc_driver);
298a98c30fdSTomas Winkler
299a98c30fdSTomas Winkler MODULE_AUTHOR("Intel Corporation");
300a98c30fdSTomas Winkler MODULE_ALIAS("auxiliary:i915.mei-gsc");
301a98c30fdSTomas Winkler MODULE_ALIAS("auxiliary:i915.mei-gscfi");
302*0e59f01dSVitaly Lubart MODULE_ALIAS("auxiliary:xe.mei-gscfi");
3030995c95bSTomas Winkler MODULE_DESCRIPTION("Intel(R) Graphics System Controller");
304a98c30fdSTomas Winkler MODULE_LICENSE("GPL");
305