Merge tag 'v5.12-rc7' into ecryptfs/next
[linux-2.6-microblaze.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/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
26 static 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         {0, }
43 };
44 MODULE_DEVICE_TABLE(pci, ish_pci_tbl);
45
46 /**
47  * ish_event_tracer() - Callback function to dump trace messages
48  * @dev:        ishtp device
49  * @format:     printf style format
50  *
51  * Callback to direct log messages to Linux trace buffers
52  */
53 static __printf(2, 3)
54 void ish_event_tracer(struct ishtp_device *dev, const char *format, ...)
55 {
56         if (trace_ishtp_dump_enabled()) {
57                 va_list args;
58                 char tmp_buf[100];
59
60                 va_start(args, format);
61                 vsnprintf(tmp_buf, sizeof(tmp_buf), format, args);
62                 va_end(args);
63
64                 trace_ishtp_dump(tmp_buf);
65         }
66 }
67
68 /**
69  * ish_init() - Init function
70  * @dev:        ishtp device
71  *
72  * This function initialize wait queues for suspend/resume and call
73  * calls hadware initialization function. This will initiate
74  * startup sequence
75  *
76  * Return: 0 for success or error code for failure
77  */
78 static int ish_init(struct ishtp_device *dev)
79 {
80         int ret;
81
82         /* Set the state of ISH HW to start */
83         ret = ish_hw_start(dev);
84         if (ret) {
85                 dev_err(dev->devc, "ISH: hw start failed.\n");
86                 return ret;
87         }
88
89         /* Start the inter process communication to ISH processor */
90         ret = ishtp_start(dev);
91         if (ret) {
92                 dev_err(dev->devc, "ISHTP: Protocol init failed.\n");
93                 return ret;
94         }
95
96         return 0;
97 }
98
99 static const struct pci_device_id ish_invalid_pci_ids[] = {
100         /* Mehlow platform special pci ids */
101         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0xA309)},
102         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0xA30A)},
103         {}
104 };
105
106 static inline bool ish_should_enter_d0i3(struct pci_dev *pdev)
107 {
108         return !pm_suspend_via_firmware() || pdev->device == CHV_DEVICE_ID;
109 }
110
111 static inline bool ish_should_leave_d0i3(struct pci_dev *pdev)
112 {
113         return !pm_resume_via_firmware() || pdev->device == CHV_DEVICE_ID;
114 }
115
116 static int enable_gpe(struct device *dev)
117 {
118 #ifdef CONFIG_ACPI
119         acpi_status acpi_sts;
120         struct acpi_device *adev;
121         struct acpi_device_wakeup *wakeup;
122
123         adev = ACPI_COMPANION(dev);
124         if (!adev) {
125                 dev_err(dev, "get acpi handle failed\n");
126                 return -ENODEV;
127         }
128         wakeup = &adev->wakeup;
129
130         acpi_sts = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
131         if (ACPI_FAILURE(acpi_sts)) {
132                 dev_err(dev, "enable ose_gpe failed\n");
133                 return -EIO;
134         }
135
136         return 0;
137 #else
138         return -ENODEV;
139 #endif
140 }
141
142 static void enable_pme_wake(struct pci_dev *pdev)
143 {
144         if ((pci_pme_capable(pdev, PCI_D0) ||
145              pci_pme_capable(pdev, PCI_D3hot) ||
146              pci_pme_capable(pdev, PCI_D3cold)) && !enable_gpe(&pdev->dev)) {
147                 pci_pme_active(pdev, true);
148                 dev_dbg(&pdev->dev, "ish ipc driver pme wake enabled\n");
149         }
150 }
151
152 /**
153  * ish_probe() - PCI driver probe callback
154  * @pdev:       pci device
155  * @ent:        pci device id
156  *
157  * Initialize PCI function, setup interrupt and call for ISH initialization
158  *
159  * Return: 0 for success or error code for failure
160  */
161 static int ish_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
162 {
163         int ret;
164         struct ish_hw *hw;
165         unsigned long irq_flag = 0;
166         struct ishtp_device *ishtp;
167         struct device *dev = &pdev->dev;
168
169         /* Check for invalid platforms for ISH support */
170         if (pci_dev_present(ish_invalid_pci_ids))
171                 return -ENODEV;
172
173         /* enable pci dev */
174         ret = pcim_enable_device(pdev);
175         if (ret) {
176                 dev_err(dev, "ISH: Failed to enable PCI device\n");
177                 return ret;
178         }
179
180         /* set PCI host mastering */
181         pci_set_master(pdev);
182
183         /* pci request regions for ISH driver */
184         ret = pcim_iomap_regions(pdev, 1 << 0, KBUILD_MODNAME);
185         if (ret) {
186                 dev_err(dev, "ISH: Failed to get PCI regions\n");
187                 return ret;
188         }
189
190         /* allocates and initializes the ISH dev structure */
191         ishtp = ish_dev_init(pdev);
192         if (!ishtp) {
193                 ret = -ENOMEM;
194                 return ret;
195         }
196         hw = to_ish_hw(ishtp);
197         ishtp->print_log = ish_event_tracer;
198
199         /* mapping IO device memory */
200         hw->mem_addr = pcim_iomap_table(pdev)[0];
201         ishtp->pdev = pdev;
202
203         /* request and enable interrupt */
204         ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
205         if (!pdev->msi_enabled && !pdev->msix_enabled)
206                 irq_flag = IRQF_SHARED;
207
208         ret = devm_request_irq(dev, pdev->irq, ish_irq_handler,
209                                irq_flag, KBUILD_MODNAME, ishtp);
210         if (ret) {
211                 dev_err(dev, "ISH: request IRQ %d failed\n", pdev->irq);
212                 return ret;
213         }
214
215         dev_set_drvdata(ishtp->devc, ishtp);
216
217         init_waitqueue_head(&ishtp->suspend_wait);
218         init_waitqueue_head(&ishtp->resume_wait);
219
220         /* Enable PME for EHL */
221         if (pdev->device == EHL_Ax_DEVICE_ID)
222                 enable_pme_wake(pdev);
223
224         ret = ish_init(ishtp);
225         if (ret)
226                 return ret;
227
228         return 0;
229 }
230
231 /**
232  * ish_remove() - PCI driver remove callback
233  * @pdev:       pci device
234  *
235  * This function does cleanup of ISH on pci remove callback
236  */
237 static void ish_remove(struct pci_dev *pdev)
238 {
239         struct ishtp_device *ishtp_dev = pci_get_drvdata(pdev);
240
241         ishtp_bus_remove_all_clients(ishtp_dev, false);
242         ish_device_disable(ishtp_dev);
243 }
244
245 static struct device __maybe_unused *ish_resume_device;
246
247 /* 50ms to get resume response */
248 #define WAIT_FOR_RESUME_ACK_MS          50
249
250 /**
251  * ish_resume_handler() - Work function to complete resume
252  * @work:       work struct
253  *
254  * The resume work function to complete resume function asynchronously.
255  * There are two resume paths, one where ISH is not powered off,
256  * in that case a simple resume message is enough, others we need
257  * a reset sequence.
258  */
259 static void __maybe_unused ish_resume_handler(struct work_struct *work)
260 {
261         struct pci_dev *pdev = to_pci_dev(ish_resume_device);
262         struct ishtp_device *dev = pci_get_drvdata(pdev);
263         uint32_t fwsts = dev->ops->get_fw_status(dev);
264         int ret;
265
266         if (ish_should_leave_d0i3(pdev) && !dev->suspend_flag
267                         && IPC_IS_ISH_ILUP(fwsts)) {
268                 disable_irq_wake(pdev->irq);
269
270                 ish_set_host_ready(dev);
271
272                 ishtp_send_resume(dev);
273
274                 /* Waiting to get resume response */
275                 if (dev->resume_flag)
276                         ret = wait_event_interruptible_timeout(dev->resume_wait,
277                                 !dev->resume_flag,
278                                 msecs_to_jiffies(WAIT_FOR_RESUME_ACK_MS));
279
280                 /*
281                  * If the flag is not cleared, something is wrong with ISH FW.
282                  * So on resume, need to go through init sequence again.
283                  */
284                 if (dev->resume_flag)
285                         ish_init(dev);
286         } else {
287                 /*
288                  * Resume from the D3, full reboot of ISH processor will happen,
289                  * so need to go through init sequence again.
290                  */
291                 ish_init(dev);
292         }
293 }
294
295 /**
296  * ish_suspend() - ISH suspend callback
297  * @device:     device pointer
298  *
299  * ISH suspend callback
300  *
301  * Return: 0 to the pm core
302  */
303 static int __maybe_unused ish_suspend(struct device *device)
304 {
305         struct pci_dev *pdev = to_pci_dev(device);
306         struct ishtp_device *dev = pci_get_drvdata(pdev);
307
308         if (ish_should_enter_d0i3(pdev)) {
309                 /*
310                  * If previous suspend hasn't been asnwered then ISH is likely
311                  * dead, don't attempt nested notification
312                  */
313                 if (dev->suspend_flag)
314                         return  0;
315
316                 dev->resume_flag = 0;
317                 dev->suspend_flag = 1;
318                 ishtp_send_suspend(dev);
319
320                 /* 25 ms should be enough for live ISH to flush all IPC buf */
321                 if (dev->suspend_flag)
322                         wait_event_interruptible_timeout(dev->suspend_wait,
323                                         !dev->suspend_flag,
324                                         msecs_to_jiffies(25));
325
326                 if (dev->suspend_flag) {
327                         /*
328                          * It looks like FW halt, clear the DMA bit, and put
329                          * ISH into D3, and FW would reset on resume.
330                          */
331                         ish_disable_dma(dev);
332                 } else {
333                         /*
334                          * Save state so PCI core will keep the device at D0,
335                          * the ISH would enter D0i3
336                          */
337                         pci_save_state(pdev);
338
339                         enable_irq_wake(pdev->irq);
340                 }
341         } else {
342                 /*
343                  * Clear the DMA bit before putting ISH into D3,
344                  * or ISH FW would reset automatically.
345                  */
346                 ish_disable_dma(dev);
347         }
348
349         return 0;
350 }
351
352 static __maybe_unused DECLARE_WORK(resume_work, ish_resume_handler);
353 /**
354  * ish_resume() - ISH resume callback
355  * @device:     device pointer
356  *
357  * ISH resume callback
358  *
359  * Return: 0 to the pm core
360  */
361 static int __maybe_unused ish_resume(struct device *device)
362 {
363         struct pci_dev *pdev = to_pci_dev(device);
364         struct ishtp_device *dev = pci_get_drvdata(pdev);
365
366         /* add this to finish power flow for EHL */
367         if (dev->pdev->device == EHL_Ax_DEVICE_ID) {
368                 pci_set_power_state(pdev, PCI_D0);
369                 enable_pme_wake(pdev);
370                 dev_dbg(dev->devc, "set power state to D0 for ehl\n");
371         }
372
373         ish_resume_device = device;
374         dev->resume_flag = 1;
375
376         schedule_work(&resume_work);
377
378         return 0;
379 }
380
381 static SIMPLE_DEV_PM_OPS(ish_pm_ops, ish_suspend, ish_resume);
382
383 static struct pci_driver ish_driver = {
384         .name = KBUILD_MODNAME,
385         .id_table = ish_pci_tbl,
386         .probe = ish_probe,
387         .remove = ish_remove,
388         .driver.pm = &ish_pm_ops,
389 };
390
391 module_pci_driver(ish_driver);
392
393 /* Original author */
394 MODULE_AUTHOR("Daniel Drubin <daniel.drubin@intel.com>");
395 /* Adoption to upstream Linux kernel */
396 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
397
398 MODULE_DESCRIPTION("Intel(R) Integrated Sensor Hub PCI Device Driver");
399 MODULE_LICENSE("GPL");