HID: intel-ish-hid: ipc: check the NO_D3 flag to distinguish resume paths
[sfrench/cifs-2.6.git] / drivers / hid / intel-ish-hid / ipc / pci-ish.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * PCI glue for ISHTP provider device (ISH) driver
4  *
5  * Copyright (c) 2014-2016, Intel Corporation.
6  */
7
8 #include <linux/module.h>
9 #include <linux/moduleparam.h>
10 #include <linux/kernel.h>
11 #include <linux/device.h>
12 #include <linux/fs.h>
13 #include <linux/errno.h>
14 #include <linux/types.h>
15 #include <linux/pci.h>
16 #include <linux/sched.h>
17 #include <linux/suspend.h>
18 #include <linux/interrupt.h>
19 #include <linux/workqueue.h>
20 #define CREATE_TRACE_POINTS
21 #include <trace/events/intel_ish.h>
22 #include "ishtp-dev.h"
23 #include "hw-ish.h"
24
25 static const struct pci_device_id ish_pci_tbl[] = {
26         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CHV_DEVICE_ID)},
27         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, BXT_Ax_DEVICE_ID)},
28         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, BXT_Bx_DEVICE_ID)},
29         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, APL_Ax_DEVICE_ID)},
30         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, SPT_Ax_DEVICE_ID)},
31         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CNL_Ax_DEVICE_ID)},
32         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, GLK_Ax_DEVICE_ID)},
33         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CNL_H_DEVICE_ID)},
34         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, ICL_MOBILE_DEVICE_ID)},
35         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, SPT_H_DEVICE_ID)},
36         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CML_LP_DEVICE_ID)},
37         {0, }
38 };
39 MODULE_DEVICE_TABLE(pci, ish_pci_tbl);
40
41 /**
42  * ish_event_tracer() - Callback function to dump trace messages
43  * @dev:        ishtp device
44  * @format:     printf style format
45  *
46  * Callback to direct log messages to Linux trace buffers
47  */
48 static __printf(2, 3)
49 void ish_event_tracer(struct ishtp_device *dev, const char *format, ...)
50 {
51         if (trace_ishtp_dump_enabled()) {
52                 va_list args;
53                 char tmp_buf[100];
54
55                 va_start(args, format);
56                 vsnprintf(tmp_buf, sizeof(tmp_buf), format, args);
57                 va_end(args);
58
59                 trace_ishtp_dump(tmp_buf);
60         }
61 }
62
63 /**
64  * ish_init() - Init function
65  * @dev:        ishtp device
66  *
67  * This function initialize wait queues for suspend/resume and call
68  * calls hadware initialization function. This will initiate
69  * startup sequence
70  *
71  * Return: 0 for success or error code for failure
72  */
73 static int ish_init(struct ishtp_device *dev)
74 {
75         int ret;
76
77         /* Set the state of ISH HW to start */
78         ret = ish_hw_start(dev);
79         if (ret) {
80                 dev_err(dev->devc, "ISH: hw start failed.\n");
81                 return ret;
82         }
83
84         /* Start the inter process communication to ISH processor */
85         ret = ishtp_start(dev);
86         if (ret) {
87                 dev_err(dev->devc, "ISHTP: Protocol init failed.\n");
88                 return ret;
89         }
90
91         return 0;
92 }
93
94 static const struct pci_device_id ish_invalid_pci_ids[] = {
95         /* Mehlow platform special pci ids */
96         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0xA309)},
97         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0xA30A)},
98         {}
99 };
100
101 static inline bool ish_should_enter_d0i3(struct pci_dev *pdev)
102 {
103         return !pm_suspend_via_firmware() || pdev->device == CHV_DEVICE_ID;
104 }
105
106 /**
107  * ish_probe() - PCI driver probe callback
108  * @pdev:       pci device
109  * @ent:        pci device id
110  *
111  * Initialize PCI function, setup interrupt and call for ISH initialization
112  *
113  * Return: 0 for success or error code for failure
114  */
115 static int ish_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
116 {
117         int ret;
118         struct ish_hw *hw;
119         unsigned long irq_flag = 0;
120         struct ishtp_device *ishtp;
121         struct device *dev = &pdev->dev;
122
123         /* Check for invalid platforms for ISH support */
124         if (pci_dev_present(ish_invalid_pci_ids))
125                 return -ENODEV;
126
127         /* enable pci dev */
128         ret = pcim_enable_device(pdev);
129         if (ret) {
130                 dev_err(dev, "ISH: Failed to enable PCI device\n");
131                 return ret;
132         }
133
134         /* set PCI host mastering */
135         pci_set_master(pdev);
136
137         /* pci request regions for ISH driver */
138         ret = pcim_iomap_regions(pdev, 1 << 0, KBUILD_MODNAME);
139         if (ret) {
140                 dev_err(dev, "ISH: Failed to get PCI regions\n");
141                 return ret;
142         }
143
144         /* allocates and initializes the ISH dev structure */
145         ishtp = ish_dev_init(pdev);
146         if (!ishtp) {
147                 ret = -ENOMEM;
148                 return ret;
149         }
150         hw = to_ish_hw(ishtp);
151         ishtp->print_log = ish_event_tracer;
152
153         /* mapping IO device memory */
154         hw->mem_addr = pcim_iomap_table(pdev)[0];
155         ishtp->pdev = pdev;
156
157         /* request and enable interrupt */
158         ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
159         if (!pdev->msi_enabled && !pdev->msix_enabled)
160                 irq_flag = IRQF_SHARED;
161
162         ret = devm_request_irq(dev, pdev->irq, ish_irq_handler,
163                                irq_flag, KBUILD_MODNAME, ishtp);
164         if (ret) {
165                 dev_err(dev, "ISH: request IRQ %d failed\n", pdev->irq);
166                 return ret;
167         }
168
169         dev_set_drvdata(ishtp->devc, ishtp);
170
171         init_waitqueue_head(&ishtp->suspend_wait);
172         init_waitqueue_head(&ishtp->resume_wait);
173
174         ret = ish_init(ishtp);
175         if (ret)
176                 return ret;
177
178         return 0;
179 }
180
181 /**
182  * ish_remove() - PCI driver remove callback
183  * @pdev:       pci device
184  *
185  * This function does cleanup of ISH on pci remove callback
186  */
187 static void ish_remove(struct pci_dev *pdev)
188 {
189         struct ishtp_device *ishtp_dev = pci_get_drvdata(pdev);
190
191         ishtp_bus_remove_all_clients(ishtp_dev, false);
192         ish_device_disable(ishtp_dev);
193 }
194
195 static struct device __maybe_unused *ish_resume_device;
196
197 /* 50ms to get resume response */
198 #define WAIT_FOR_RESUME_ACK_MS          50
199
200 /**
201  * ish_resume_handler() - Work function to complete resume
202  * @work:       work struct
203  *
204  * The resume work function to complete resume function asynchronously.
205  * There are two resume paths, one where ISH is not powered off,
206  * in that case a simple resume message is enough, others we need
207  * a reset sequence.
208  */
209 static void __maybe_unused ish_resume_handler(struct work_struct *work)
210 {
211         struct pci_dev *pdev = to_pci_dev(ish_resume_device);
212         struct ishtp_device *dev = pci_get_drvdata(pdev);
213         int ret;
214
215         /* Check the NO_D3 flag to distinguish the resume paths */
216         if (pdev->dev_flags & PCI_DEV_FLAGS_NO_D3) {
217                 pdev->dev_flags &= ~PCI_DEV_FLAGS_NO_D3;
218                 disable_irq_wake(pdev->irq);
219
220                 ishtp_send_resume(dev);
221
222                 /* Waiting to get resume response */
223                 if (dev->resume_flag)
224                         ret = wait_event_interruptible_timeout(dev->resume_wait,
225                                 !dev->resume_flag,
226                                 msecs_to_jiffies(WAIT_FOR_RESUME_ACK_MS));
227
228                 /*
229                  * If the flag is not cleared, something is wrong with ISH FW.
230                  * So on resume, need to go through init sequence again.
231                  */
232                 if (dev->resume_flag)
233                         ish_init(dev);
234         } else {
235                 /*
236                  * Resume from the D3, full reboot of ISH processor will happen,
237                  * so need to go through init sequence again.
238                  */
239                 ish_init(dev);
240         }
241 }
242
243 /**
244  * ish_suspend() - ISH suspend callback
245  * @device:     device pointer
246  *
247  * ISH suspend callback
248  *
249  * Return: 0 to the pm core
250  */
251 static int __maybe_unused ish_suspend(struct device *device)
252 {
253         struct pci_dev *pdev = to_pci_dev(device);
254         struct ishtp_device *dev = pci_get_drvdata(pdev);
255
256         if (ish_should_enter_d0i3(pdev)) {
257                 /*
258                  * If previous suspend hasn't been asnwered then ISH is likely
259                  * dead, don't attempt nested notification
260                  */
261                 if (dev->suspend_flag)
262                         return  0;
263
264                 dev->resume_flag = 0;
265                 dev->suspend_flag = 1;
266                 ishtp_send_suspend(dev);
267
268                 /* 25 ms should be enough for live ISH to flush all IPC buf */
269                 if (dev->suspend_flag)
270                         wait_event_interruptible_timeout(dev->suspend_wait,
271                                         !dev->suspend_flag,
272                                         msecs_to_jiffies(25));
273
274                 if (dev->suspend_flag) {
275                         /*
276                          * It looks like FW halt, clear the DMA bit, and put
277                          * ISH into D3, and FW would reset on resume.
278                          */
279                         ish_disable_dma(dev);
280                 } else {
281                         /* Set the NO_D3 flag, the ISH would enter D0i3 */
282                         pdev->dev_flags |= PCI_DEV_FLAGS_NO_D3;
283
284                         enable_irq_wake(pdev->irq);
285                 }
286         } else {
287                 /*
288                  * Clear the DMA bit before putting ISH into D3,
289                  * or ISH FW would reset automatically.
290                  */
291                 ish_disable_dma(dev);
292         }
293
294         return 0;
295 }
296
297 static __maybe_unused DECLARE_WORK(resume_work, ish_resume_handler);
298 /**
299  * ish_resume() - ISH resume callback
300  * @device:     device pointer
301  *
302  * ISH resume callback
303  *
304  * Return: 0 to the pm core
305  */
306 static int __maybe_unused ish_resume(struct device *device)
307 {
308         struct pci_dev *pdev = to_pci_dev(device);
309         struct ishtp_device *dev = pci_get_drvdata(pdev);
310
311         ish_resume_device = device;
312         dev->resume_flag = 1;
313
314         schedule_work(&resume_work);
315
316         return 0;
317 }
318
319 static SIMPLE_DEV_PM_OPS(ish_pm_ops, ish_suspend, ish_resume);
320
321 static struct pci_driver ish_driver = {
322         .name = KBUILD_MODNAME,
323         .id_table = ish_pci_tbl,
324         .probe = ish_probe,
325         .remove = ish_remove,
326         .driver.pm = &ish_pm_ops,
327 };
328
329 module_pci_driver(ish_driver);
330
331 /* Original author */
332 MODULE_AUTHOR("Daniel Drubin <daniel.drubin@intel.com>");
333 /* Adoption to upstream Linux kernel */
334 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
335
336 MODULE_DESCRIPTION("Intel(R) Integrated Sensor Hub PCI Device Driver");
337 MODULE_LICENSE("GPL");