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