1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2025-2026, Intel Corporation. All rights reserved.
* Intel Management Engine Interface (Intel MEI) Linux driver
* for CSC platforms.
*/
#include <linux/cleanup.h>
#include <linux/device.h>
#include <linux/dma-mapping.h>
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/pci.h>
#include <linux/pm_runtime.h>
#include <linux/sched.h>
#include <linux/types.h>
#include "client.h"
#include "hw-me-regs.h"
#include "hw-me.h"
#include "mei_dev.h"
#include "mei-trace.h"
#define MEI_CSC_HECI2_OFFSET 0x1000
static int mei_csc_read_fws(const struct mei_device *mdev, int where, const char *name, u32 *val)
{
struct mei_me_hw *hw = to_me_hw(mdev);
*val = ioread32(hw->mem_addr + where + 0xC00);
trace_mei_reg_read(&mdev->dev, name, where, *val);
return 0;
}
static int mei_csc_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
{
struct device *dev = &pdev->dev;
const struct mei_cfg *cfg;
char __iomem *registers;
struct mei_device *mdev;
struct mei_me_hw *hw;
int err;
cfg = mei_me_get_cfg(ent->driver_data);
if (!cfg)
return -ENODEV;
err = pcim_enable_device(pdev);
if (err)
return dev_err_probe(dev, err, "Failed to enable PCI device.\n");
pci_set_master(pdev);
registers = pcim_iomap_region(pdev, 0, KBUILD_MODNAME);
if (IS_ERR(registers))
return dev_err_probe(dev, PTR_ERR(registers), "Failed to get PCI region.\n");
err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
if (err)
return dev_err_probe(dev, err, "No usable DMA configuration.\n");
/* allocates and initializes the mei dev structure */
mdev = mei_me_dev_init(dev, cfg, false);
if (!mdev)
return -ENOMEM;
mdev->read_fws_need_resume = true;
hw = to_me_hw(mdev);
/*
* Both HECI1 and HECI2 are on this device, but only HECI2 is supported.
*/
hw->mem_addr = registers + MEI_CSC_HECI2_OFFSET;
hw->read_fws = mei_csc_read_fws;
/*
* mei_register() assumes ownership of mdev.
* No need to release it explicitly in error path.
*/
err = mei_register(mdev, dev);
if (err)
return err;
pci_set_drvdata(pdev, mdev);
err = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_INTX | PCI_IRQ_MSI);
if (err < 0) {
dev_err_probe(dev, err, "Failed to allocate IRQ.\n");
goto err_mei_unreg;
}
hw->irq = pci_irq_vector(pdev, 0);
/* request and enable interrupt */
err = request_threaded_irq(hw->irq,
mei_me_irq_quick_handler, mei_me_irq_thread_handler,
IRQF_SHARED | IRQF_ONESHOT, KBUILD_MODNAME, mdev);
if (err)
goto err_free_irq_vectors;
/*
* Continue to char device setup in spite of firmware handshake failure.
* In order to provide access to the firmware status registers to the user
* space via sysfs. The firmware status registers required to understand
* firmware error state and possible recovery flow.
*/
if (mei_start(mdev))
dev_warn(dev, "Failed to initialize HECI hardware.\n");
pm_runtime_set_autosuspend_delay(dev, MEI_ME_RPM_TIMEOUT);
pm_runtime_use_autosuspend(dev);
/*
* MEI requires to resume from runtime suspend mode
* in order to perform link reset flow upon system suspend.
*/
dev_pm_set_driver_flags(dev, DPM_FLAG_NO_DIRECT_COMPLETE);
pm_runtime_allow(dev);
pm_runtime_put_noidle(dev);
return 0;
err_free_irq_vectors:
pci_free_irq_vectors(pdev);
err_mei_unreg:
mei_deregister(mdev);
return err;
}
static void mei_csc_shutdown(struct pci_dev *pdev)
{
struct mei_device *mdev = pci_get_drvdata(pdev);
struct mei_me_hw *hw = to_me_hw(mdev);
pm_runtime_get_noresume(&pdev->dev);
mei_stop(mdev);
mei_disable_interrupts(mdev);
free_irq(hw->irq, mdev);
pci_free_irq_vectors(pdev);
}
static void mei_csc_remove(struct pci_dev *pdev)
{
struct mei_device *mdev = pci_get_drvdata(pdev);
mei_csc_shutdown(pdev);
mei_deregister(mdev);
}
static int mei_csc_pci_prepare(struct device *dev)
{
pm_runtime_resume(dev);
return 0;
}
static int mei_csc_pci_suspend(struct device *dev)
{
struct mei_device *mdev = dev_get_drvdata(dev);
mei_stop(mdev);
mei_disable_interrupts(mdev);
return 0;
}
static int mei_csc_pci_resume(struct device *dev)
{
struct mei_device *mdev = dev_get_drvdata(dev);
int err;
err = mei_restart(mdev);
if (err)
return err;
/* Start timer if stopped in suspend */
schedule_delayed_work(&mdev->timer_work, HZ);
return 0;
}
static void mei_csc_pci_complete(struct device *dev)
{
pm_runtime_suspend(dev);
}
static int mei_csc_pm_runtime_idle(struct device *dev)
{
struct mei_device *mdev = dev_get_drvdata(dev);
return mei_write_is_idle(mdev) ? 0 : -EBUSY;
}
static int mei_csc_pm_runtime_suspend(struct device *dev)
{
struct mei_device *mdev = dev_get_drvdata(dev);
struct mei_me_hw *hw = to_me_hw(mdev);
guard(mutex)(&mdev->device_lock);
if (!mei_write_is_idle(mdev))
return -EAGAIN;
hw->pg_state = MEI_PG_ON;
return 0;
}
static int mei_csc_pm_runtime_resume(struct device *dev)
{
struct mei_device *mdev = dev_get_drvdata(dev);
struct mei_me_hw *hw = to_me_hw(mdev);
irqreturn_t irq_ret;
scoped_guard(mutex, &mdev->device_lock)
hw->pg_state = MEI_PG_OFF;
/* Process all queues that wait for resume */
irq_ret = mei_me_irq_thread_handler(1, mdev);
if (irq_ret != IRQ_HANDLED)
dev_err(dev, "thread handler fail %d\n", irq_ret);
return 0;
}
static const struct dev_pm_ops mei_csc_pm_ops = {
.prepare = pm_sleep_ptr(mei_csc_pci_prepare),
.complete = pm_sleep_ptr(mei_csc_pci_complete),
SYSTEM_SLEEP_PM_OPS(mei_csc_pci_suspend, mei_csc_pci_resume)
RUNTIME_PM_OPS(mei_csc_pm_runtime_suspend,
mei_csc_pm_runtime_resume, mei_csc_pm_runtime_idle)
};
static const struct pci_device_id mei_csc_pci_tbl[] = {
{ PCI_DEVICE_DATA(INTEL, MEI_CRI, MEI_ME_CSC_CFG) },
{}
};
MODULE_DEVICE_TABLE(pci, mei_csc_pci_tbl);
static struct pci_driver mei_csc_driver = {
.name = KBUILD_MODNAME,
.id_table = mei_csc_pci_tbl,
.probe = mei_csc_probe,
.remove = mei_csc_remove,
.shutdown = mei_csc_shutdown,
.driver = {
.pm = &mei_csc_pm_ops,
.probe_type = PROBE_PREFER_ASYNCHRONOUS,
}
};
module_pci_driver(mei_csc_driver);
MODULE_DESCRIPTION("Intel(R) Management Engine Interface for discrete graphics (CSC)");
MODULE_LICENSE("GPL");
|