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