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