1
2
3
4
5
6
7
8#include <linux/acpi.h>
9#include <linux/module.h>
10#include <linux/moduleparam.h>
11#include <linux/kernel.h>
12#include <linux/device.h>
13#include <linux/fs.h>
14#include <linux/errno.h>
15#include <linux/types.h>
16#include <linux/pci.h>
17#include <linux/sched.h>
18#include <linux/suspend.h>
19#include <linux/interrupt.h>
20#include <linux/workqueue.h>
21#define CREATE_TRACE_POINTS
22#include <trace/events/intel_ish.h>
23#include "ishtp-dev.h"
24#include "hw-ish.h"
25
26static const struct pci_device_id ish_pci_tbl[] = {
27 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CHV_DEVICE_ID)},
28 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, BXT_Ax_DEVICE_ID)},
29 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, BXT_Bx_DEVICE_ID)},
30 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, APL_Ax_DEVICE_ID)},
31 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, SPT_Ax_DEVICE_ID)},
32 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CNL_Ax_DEVICE_ID)},
33 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, GLK_Ax_DEVICE_ID)},
34 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CNL_H_DEVICE_ID)},
35 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, ICL_MOBILE_DEVICE_ID)},
36 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, SPT_H_DEVICE_ID)},
37 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CML_LP_DEVICE_ID)},
38 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CMP_H_DEVICE_ID)},
39 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, EHL_Ax_DEVICE_ID)},
40 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, TGL_LP_DEVICE_ID)},
41 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, TGL_H_DEVICE_ID)},
42 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, ADL_S_DEVICE_ID)},
43 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, ADL_P_DEVICE_ID)},
44 {0, }
45};
46MODULE_DEVICE_TABLE(pci, ish_pci_tbl);
47
48
49
50
51
52
53
54
55static __printf(2, 3)
56void ish_event_tracer(struct ishtp_device *dev, const char *format, ...)
57{
58 if (trace_ishtp_dump_enabled()) {
59 va_list args;
60 char tmp_buf[100];
61
62 va_start(args, format);
63 vsnprintf(tmp_buf, sizeof(tmp_buf), format, args);
64 va_end(args);
65
66 trace_ishtp_dump(tmp_buf);
67 }
68}
69
70
71
72
73
74
75
76
77
78
79
80static int ish_init(struct ishtp_device *dev)
81{
82 int ret;
83
84
85 ret = ish_hw_start(dev);
86 if (ret) {
87 dev_err(dev->devc, "ISH: hw start failed.\n");
88 return ret;
89 }
90
91
92 ret = ishtp_start(dev);
93 if (ret) {
94 dev_err(dev->devc, "ISHTP: Protocol init failed.\n");
95 return ret;
96 }
97
98 return 0;
99}
100
101static const struct pci_device_id ish_invalid_pci_ids[] = {
102
103 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0xA309)},
104 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0xA30A)},
105 {}
106};
107
108static inline bool ish_should_enter_d0i3(struct pci_dev *pdev)
109{
110 return !pm_suspend_via_firmware() || pdev->device == CHV_DEVICE_ID;
111}
112
113static inline bool ish_should_leave_d0i3(struct pci_dev *pdev)
114{
115 return !pm_resume_via_firmware() || pdev->device == CHV_DEVICE_ID;
116}
117
118static int enable_gpe(struct device *dev)
119{
120#ifdef CONFIG_ACPI
121 acpi_status acpi_sts;
122 struct acpi_device *adev;
123 struct acpi_device_wakeup *wakeup;
124
125 adev = ACPI_COMPANION(dev);
126 if (!adev) {
127 dev_err(dev, "get acpi handle failed\n");
128 return -ENODEV;
129 }
130 wakeup = &adev->wakeup;
131
132 acpi_sts = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
133 if (ACPI_FAILURE(acpi_sts)) {
134 dev_err(dev, "enable ose_gpe failed\n");
135 return -EIO;
136 }
137
138 return 0;
139#else
140 return -ENODEV;
141#endif
142}
143
144static void enable_pme_wake(struct pci_dev *pdev)
145{
146 if ((pci_pme_capable(pdev, PCI_D0) ||
147 pci_pme_capable(pdev, PCI_D3hot) ||
148 pci_pme_capable(pdev, PCI_D3cold)) && !enable_gpe(&pdev->dev)) {
149 pci_pme_active(pdev, true);
150 dev_dbg(&pdev->dev, "ish ipc driver pme wake enabled\n");
151 }
152}
153
154
155
156
157
158
159
160
161
162
163static int ish_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
164{
165 int ret;
166 struct ish_hw *hw;
167 unsigned long irq_flag = 0;
168 struct ishtp_device *ishtp;
169 struct device *dev = &pdev->dev;
170
171
172 if (pci_dev_present(ish_invalid_pci_ids))
173 return -ENODEV;
174
175
176 ret = pcim_enable_device(pdev);
177 if (ret) {
178 dev_err(dev, "ISH: Failed to enable PCI device\n");
179 return ret;
180 }
181
182
183 pci_set_master(pdev);
184
185
186 ret = pcim_iomap_regions(pdev, 1 << 0, KBUILD_MODNAME);
187 if (ret) {
188 dev_err(dev, "ISH: Failed to get PCI regions\n");
189 return ret;
190 }
191
192
193 ishtp = ish_dev_init(pdev);
194 if (!ishtp) {
195 ret = -ENOMEM;
196 return ret;
197 }
198 hw = to_ish_hw(ishtp);
199 ishtp->print_log = ish_event_tracer;
200
201
202 hw->mem_addr = pcim_iomap_table(pdev)[0];
203 ishtp->pdev = pdev;
204
205
206 ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
207 if (!pdev->msi_enabled && !pdev->msix_enabled)
208 irq_flag = IRQF_SHARED;
209
210 ret = devm_request_irq(dev, pdev->irq, ish_irq_handler,
211 irq_flag, KBUILD_MODNAME, ishtp);
212 if (ret) {
213 dev_err(dev, "ISH: request IRQ %d failed\n", pdev->irq);
214 return ret;
215 }
216
217 dev_set_drvdata(ishtp->devc, ishtp);
218
219 init_waitqueue_head(&ishtp->suspend_wait);
220 init_waitqueue_head(&ishtp->resume_wait);
221
222
223 if (pdev->device == EHL_Ax_DEVICE_ID)
224 enable_pme_wake(pdev);
225
226 ret = ish_init(ishtp);
227 if (ret)
228 return ret;
229
230 return 0;
231}
232
233
234
235
236
237
238
239static void ish_remove(struct pci_dev *pdev)
240{
241 struct ishtp_device *ishtp_dev = pci_get_drvdata(pdev);
242
243 ishtp_bus_remove_all_clients(ishtp_dev, false);
244 ish_device_disable(ishtp_dev);
245}
246
247static struct device __maybe_unused *ish_resume_device;
248
249
250#define WAIT_FOR_RESUME_ACK_MS 50
251
252
253
254
255
256
257
258
259
260
261static void __maybe_unused ish_resume_handler(struct work_struct *work)
262{
263 struct pci_dev *pdev = to_pci_dev(ish_resume_device);
264 struct ishtp_device *dev = pci_get_drvdata(pdev);
265 uint32_t fwsts = dev->ops->get_fw_status(dev);
266
267 if (ish_should_leave_d0i3(pdev) && !dev->suspend_flag
268 && IPC_IS_ISH_ILUP(fwsts)) {
269 disable_irq_wake(pdev->irq);
270
271 ish_set_host_ready(dev);
272
273 ishtp_send_resume(dev);
274
275
276 if (dev->resume_flag)
277 wait_event_interruptible_timeout(dev->resume_wait,
278 !dev->resume_flag,
279 msecs_to_jiffies(WAIT_FOR_RESUME_ACK_MS));
280
281
282
283
284
285 if (dev->resume_flag)
286 ish_init(dev);
287 } else {
288
289
290
291
292 ish_init(dev);
293 }
294}
295
296
297
298
299
300
301
302
303
304static int __maybe_unused ish_suspend(struct device *device)
305{
306 struct pci_dev *pdev = to_pci_dev(device);
307 struct ishtp_device *dev = pci_get_drvdata(pdev);
308
309 if (ish_should_enter_d0i3(pdev)) {
310
311
312
313
314 if (dev->suspend_flag)
315 return 0;
316
317 dev->resume_flag = 0;
318 dev->suspend_flag = 1;
319 ishtp_send_suspend(dev);
320
321
322 if (dev->suspend_flag)
323 wait_event_interruptible_timeout(dev->suspend_wait,
324 !dev->suspend_flag,
325 msecs_to_jiffies(25));
326
327 if (dev->suspend_flag) {
328
329
330
331
332 ish_disable_dma(dev);
333 } else {
334
335
336
337
338 pci_save_state(pdev);
339
340 enable_irq_wake(pdev->irq);
341 }
342 } else {
343
344
345
346
347 ish_disable_dma(dev);
348 }
349
350 return 0;
351}
352
353static __maybe_unused DECLARE_WORK(resume_work, ish_resume_handler);
354
355
356
357
358
359
360
361
362static int __maybe_unused ish_resume(struct device *device)
363{
364 struct pci_dev *pdev = to_pci_dev(device);
365 struct ishtp_device *dev = pci_get_drvdata(pdev);
366
367
368 if (dev->pdev->device == EHL_Ax_DEVICE_ID) {
369 pci_set_power_state(pdev, PCI_D0);
370 enable_pme_wake(pdev);
371 dev_dbg(dev->devc, "set power state to D0 for ehl\n");
372 }
373
374 ish_resume_device = device;
375 dev->resume_flag = 1;
376
377 schedule_work(&resume_work);
378
379 return 0;
380}
381
382static SIMPLE_DEV_PM_OPS(ish_pm_ops, ish_suspend, ish_resume);
383
384static struct pci_driver ish_driver = {
385 .name = KBUILD_MODNAME,
386 .id_table = ish_pci_tbl,
387 .probe = ish_probe,
388 .remove = ish_remove,
389 .driver.pm = &ish_pm_ops,
390};
391
392module_pci_driver(ish_driver);
393
394
395MODULE_AUTHOR("Daniel Drubin <daniel.drubin@intel.com>");
396
397MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
398
399MODULE_DESCRIPTION("Intel(R) Integrated Sensor Hub PCI Device Driver");
400MODULE_LICENSE("GPL");
401