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 enum ishtp_driver_data_index {
27 ISHTP_DRIVER_DATA_NONE,
28 ISHTP_DRIVER_DATA_LNL_M,
29 ISHTP_DRIVER_DATA_PTL,
30 ISHTP_DRIVER_DATA_WCL,
31 ISHTP_DRIVER_DATA_NVL_H,
32 ISHTP_DRIVER_DATA_NVL_S,
33 };
34
35 #define ISH_FW_GEN_LNL_M "lnlm"
36 #define ISH_FW_GEN_PTL "ptl"
37 #define ISH_FW_GEN_WCL "wcl"
38 #define ISH_FW_GEN_NVL_H "nvlh"
39 #define ISH_FW_GEN_NVL_S "nvls"
40
41 #define ISH_FIRMWARE_PATH(gen) "intel/ish/ish_" gen ".bin"
42 #define ISH_FIRMWARE_PATH_ALL "intel/ish/ish_*.bin"
43
44 static struct ishtp_driver_data ishtp_driver_data[] = {
45 [ISHTP_DRIVER_DATA_LNL_M] = {
46 .fw_generation = ISH_FW_GEN_LNL_M,
47 },
48 [ISHTP_DRIVER_DATA_PTL] = {
49 .fw_generation = ISH_FW_GEN_PTL,
50 },
51 [ISHTP_DRIVER_DATA_WCL] = {
52 .fw_generation = ISH_FW_GEN_WCL,
53 },
54 [ISHTP_DRIVER_DATA_NVL_H] = {
55 .fw_generation = ISH_FW_GEN_NVL_H,
56 },
57 [ISHTP_DRIVER_DATA_NVL_S] = {
58 .fw_generation = ISH_FW_GEN_NVL_S,
59 },
60 };
61
62 static const struct pci_device_id ish_pci_tbl[] = {
63 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_CHV)},
64 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_BXT_Ax)},
65 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_BXT_Bx)},
66 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_APL_Ax)},
67 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_SPT_Ax)},
68 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_CNL_Ax)},
69 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_GLK_Ax)},
70 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_CNL_H)},
71 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_ICL_MOBILE)},
72 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_SPT_H)},
73 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_CML_LP)},
74 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_CMP_H)},
75 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_EHL_Ax)},
76 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_TGL_LP)},
77 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_TGL_H)},
78 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_ADL_S)},
79 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_ADL_P)},
80 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_ADL_N)},
81 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_RPL_S)},
82 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_MTL_P)},
83 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_ARL_H)},
84 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_ARL_S)},
85 {PCI_DEVICE_DATA(INTEL, ISH_LNL_M, ISHTP_DRIVER_DATA_LNL_M)},
86 {PCI_DEVICE_DATA(INTEL, ISH_PTL_H, ISHTP_DRIVER_DATA_PTL)},
87 {PCI_DEVICE_DATA(INTEL, ISH_PTL_P, ISHTP_DRIVER_DATA_PTL)},
88 {PCI_DEVICE_DATA(INTEL, ISH_WCL, ISHTP_DRIVER_DATA_WCL)},
89 {PCI_DEVICE_DATA(INTEL, ISH_NVL_H, ISHTP_DRIVER_DATA_NVL_H)},
90 {PCI_DEVICE_DATA(INTEL, ISH_NVL_S, ISHTP_DRIVER_DATA_NVL_S)},
91 {}
92 };
93 MODULE_DEVICE_TABLE(pci, ish_pci_tbl);
94
95 /**
96 * ish_event_tracer() - Callback function to dump trace messages
97 * @dev: ishtp device
98 * @format: printf style format
99 *
100 * Callback to direct log messages to Linux trace buffers
101 */
102 static __printf(2, 3)
ish_event_tracer(struct ishtp_device * dev,const char * format,...)103 void ish_event_tracer(struct ishtp_device *dev, const char *format, ...)
104 {
105 if (trace_ishtp_dump_enabled()) {
106 va_list args;
107 char tmp_buf[100];
108
109 va_start(args, format);
110 vsnprintf(tmp_buf, sizeof(tmp_buf), format, args);
111 va_end(args);
112
113 trace_ishtp_dump(tmp_buf);
114 }
115 }
116
117 /**
118 * ish_init() - Init function
119 * @dev: ishtp device
120 *
121 * This function initialize wait queues for suspend/resume and call
122 * calls hadware initialization function. This will initiate
123 * startup sequence
124 *
125 * Return: 0 for success or error code for failure
126 */
ish_init(struct ishtp_device * dev)127 static int ish_init(struct ishtp_device *dev)
128 {
129 int ret;
130
131 /* Set the state of ISH HW to start */
132 ret = ish_hw_start(dev);
133 if (ret) {
134 dev_err(dev->devc, "ISH: hw start failed.\n");
135 return ret;
136 }
137
138 /* Start the inter process communication to ISH processor */
139 ret = ishtp_start(dev);
140 if (ret) {
141 dev_err(dev->devc, "ISHTP: Protocol init failed.\n");
142 return ret;
143 }
144
145 return 0;
146 }
147
148 static const struct pci_device_id ish_invalid_pci_ids[] = {
149 /* Mehlow platform special pci ids */
150 {PCI_VDEVICE(INTEL, 0xA309)},
151 {PCI_VDEVICE(INTEL, 0xA30A)},
152 {}
153 };
154
ish_should_enter_d0i3(struct pci_dev * pdev)155 static inline bool ish_should_enter_d0i3(struct pci_dev *pdev)
156 {
157 return !pm_suspend_via_firmware() || pdev->device == PCI_DEVICE_ID_INTEL_ISH_CHV;
158 }
159
ish_should_leave_d0i3(struct pci_dev * pdev)160 static inline bool ish_should_leave_d0i3(struct pci_dev *pdev)
161 {
162 struct ishtp_device *dev = pci_get_drvdata(pdev);
163 u32 fwsts = dev->ops->get_fw_status(dev);
164
165 if (dev->suspend_flag || !IPC_IS_ISH_ILUP(fwsts))
166 return false;
167
168 return !pm_resume_via_firmware() || pdev->device == PCI_DEVICE_ID_INTEL_ISH_CHV;
169 }
170
171 /**
172 * ish_probe() - PCI driver probe callback
173 * @pdev: pci device
174 * @ent: pci device id
175 *
176 * Initialize PCI function, setup interrupt and call for ISH initialization
177 *
178 * Return: 0 for success or error code for failure
179 */
ish_probe(struct pci_dev * pdev,const struct pci_device_id * ent)180 static int ish_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
181 {
182 int ret;
183 struct ish_hw *hw;
184 unsigned long irq_flag = 0;
185 struct ishtp_device *ishtp;
186 struct device *dev = &pdev->dev;
187
188 /* Check for invalid platforms for ISH support */
189 if (pci_dev_present(ish_invalid_pci_ids))
190 return -ENODEV;
191
192 /* enable pci dev */
193 ret = pcim_enable_device(pdev);
194 if (ret) {
195 dev_err(dev, "ISH: Failed to enable PCI device\n");
196 return ret;
197 }
198
199 /* set PCI host mastering */
200 pci_set_master(pdev);
201
202 /* pci request regions for ISH driver */
203 ret = pcim_iomap_regions(pdev, 1 << 0, KBUILD_MODNAME);
204 if (ret) {
205 dev_err(dev, "ISH: Failed to get PCI regions\n");
206 return ret;
207 }
208
209 /* allocates and initializes the ISH dev structure */
210 ishtp = ish_dev_init(pdev);
211 if (!ishtp) {
212 ret = -ENOMEM;
213 return ret;
214 }
215 hw = to_ish_hw(ishtp);
216 ishtp->print_log = ish_event_tracer;
217 ishtp->driver_data = &ishtp_driver_data[ent->driver_data];
218
219 /* mapping IO device memory */
220 hw->mem_addr = pcim_iomap_table(pdev)[0];
221 ishtp->pdev = pdev;
222
223 /* request and enable interrupt */
224 ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
225 if (ret < 0) {
226 dev_err(dev, "ISH: Failed to allocate IRQ vectors\n");
227 return ret;
228 }
229
230 if (!pdev->msi_enabled && !pdev->msix_enabled)
231 irq_flag = IRQF_SHARED;
232
233 ret = devm_request_irq(dev, pdev->irq, ish_irq_handler,
234 irq_flag, KBUILD_MODNAME, ishtp);
235 if (ret) {
236 dev_err(dev, "ISH: request IRQ %d failed\n", pdev->irq);
237 return ret;
238 }
239
240 dev_set_drvdata(ishtp->devc, ishtp);
241
242 init_waitqueue_head(&ishtp->suspend_wait);
243 init_waitqueue_head(&ishtp->resume_wait);
244
245 /* Enable PME for EHL */
246 if (pdev->device == PCI_DEVICE_ID_INTEL_ISH_EHL_Ax)
247 device_init_wakeup(dev, true);
248
249 ret = ish_init(ishtp);
250 if (ret)
251 return ret;
252
253 return 0;
254 }
255
256 /**
257 * ish_remove() - PCI driver remove callback
258 * @pdev: pci device
259 *
260 * This function does cleanup of ISH on pci remove callback
261 */
ish_remove(struct pci_dev * pdev)262 static void ish_remove(struct pci_dev *pdev)
263 {
264 struct ishtp_device *ishtp_dev = pci_get_drvdata(pdev);
265
266 ishtp_bus_remove_all_clients(ishtp_dev, false);
267 ish_device_disable(ishtp_dev);
268 }
269
270
271 /**
272 * ish_shutdown() - PCI driver shutdown callback
273 * @pdev: pci device
274 *
275 * This function sets up wakeup for S5
276 */
ish_shutdown(struct pci_dev * pdev)277 static void ish_shutdown(struct pci_dev *pdev)
278 {
279 if (pdev->device == PCI_DEVICE_ID_INTEL_ISH_EHL_Ax)
280 pci_prepare_to_sleep(pdev);
281 }
282
283 static struct device __maybe_unused *ish_resume_device;
284
285 /**
286 * ish_resume_handler() - Work function to complete resume
287 * @work: work struct
288 *
289 * The resume work function to complete resume function asynchronously.
290 * There are two resume paths, one where ISH is not powered off,
291 * in that case a simple resume message is enough, others we need
292 * a reset sequence.
293 */
ish_resume_handler(struct work_struct * work)294 static void __maybe_unused ish_resume_handler(struct work_struct *work)
295 {
296 struct pci_dev *pdev = to_pci_dev(ish_resume_device);
297 struct ishtp_device *dev = pci_get_drvdata(pdev);
298
299 if (ish_should_leave_d0i3(pdev)) {
300 if (device_may_wakeup(&pdev->dev))
301 disable_irq_wake(pdev->irq);
302
303 ish_set_host_ready(dev);
304
305 ishtp_send_resume(dev);
306
307 /* Waiting to get resume response */
308 if (dev->resume_flag)
309 wait_event_interruptible_timeout(dev->resume_wait,
310 !dev->resume_flag,
311 msecs_to_jiffies(WAIT_FOR_RESUME_ACK_MS));
312
313 /*
314 * If the flag is not cleared, something is wrong with ISH FW.
315 * So on resume, need to go through init sequence again.
316 */
317 if (dev->resume_flag)
318 ish_init(dev);
319 } else {
320 /*
321 * Resume from the D3, full reboot of ISH processor will happen,
322 * so need to go through init sequence again.
323 */
324 ish_init(dev);
325 }
326 }
327
328 /**
329 * ish_suspend() - ISH suspend callback
330 * @device: device pointer
331 *
332 * ISH suspend callback
333 *
334 * Return: 0 to the pm core
335 */
ish_suspend(struct device * device)336 static int __maybe_unused ish_suspend(struct device *device)
337 {
338 struct pci_dev *pdev = to_pci_dev(device);
339 struct ishtp_device *dev = pci_get_drvdata(pdev);
340
341 if (ish_should_enter_d0i3(pdev)) {
342 /*
343 * If previous suspend hasn't been asnwered then ISH is likely
344 * dead, don't attempt nested notification
345 */
346 if (dev->suspend_flag)
347 return 0;
348
349 dev->resume_flag = 0;
350 dev->suspend_flag = 1;
351 ishtp_send_suspend(dev);
352
353 /* 25 ms should be enough for live ISH to flush all IPC buf */
354 if (dev->suspend_flag)
355 wait_event_interruptible_timeout(dev->suspend_wait,
356 !dev->suspend_flag,
357 msecs_to_jiffies(25));
358
359 if (dev->suspend_flag) {
360 /*
361 * It looks like FW halt, clear the DMA bit, and put
362 * ISH into D3, and FW would reset on resume.
363 */
364 ish_disable_dma(dev);
365 } else {
366 /*
367 * Save state so PCI core will keep the device at D0,
368 * the ISH would enter D0i3
369 */
370 pci_save_state(pdev);
371
372 if (device_may_wakeup(&pdev->dev))
373 enable_irq_wake(pdev->irq);
374 }
375 } else {
376 /*
377 * Clear the DMA bit before putting ISH into D3,
378 * or ISH FW would reset automatically.
379 */
380 ish_disable_dma(dev);
381 }
382
383 return 0;
384 }
385
386 static __maybe_unused DECLARE_WORK(resume_work, ish_resume_handler);
387 /**
388 * ish_resume() - ISH resume callback
389 * @device: device pointer
390 *
391 * ISH resume callback
392 *
393 * Return: 0 to the pm core
394 */
ish_resume(struct device * device)395 static int __maybe_unused ish_resume(struct device *device)
396 {
397 struct pci_dev *pdev = to_pci_dev(device);
398 struct ishtp_device *dev = pci_get_drvdata(pdev);
399
400 ish_resume_device = device;
401 dev->resume_flag = 1;
402
403 /* If ISH resume from D3, reset ishtp clients before return */
404 if (!ish_should_leave_d0i3(pdev))
405 ishtp_reset_handler(dev);
406
407 queue_work(dev->unbound_wq, &resume_work);
408
409 return 0;
410 }
411
ish_freeze(struct device * device)412 static int __maybe_unused ish_freeze(struct device *device)
413 {
414 struct pci_dev *pdev = to_pci_dev(device);
415
416 return pci_save_state(pdev);
417 }
418
419 static const struct dev_pm_ops __maybe_unused ish_pm_ops = {
420 .suspend = pm_sleep_ptr(ish_suspend),
421 .resume = pm_sleep_ptr(ish_resume),
422 .freeze = pm_sleep_ptr(ish_freeze),
423 .restore = pm_sleep_ptr(ish_resume),
424 .poweroff = pm_sleep_ptr(ish_suspend),
425 };
426
base_version_show(struct device * cdev,struct device_attribute * attr,char * buf)427 static ssize_t base_version_show(struct device *cdev,
428 struct device_attribute *attr, char *buf)
429 {
430 struct ishtp_device *dev = dev_get_drvdata(cdev);
431
432 return sysfs_emit(buf, "%u.%u.%u.%u\n", dev->base_ver.major,
433 dev->base_ver.minor, dev->base_ver.hotfix,
434 dev->base_ver.build);
435 }
436 static DEVICE_ATTR_RO(base_version);
437
project_version_show(struct device * cdev,struct device_attribute * attr,char * buf)438 static ssize_t project_version_show(struct device *cdev,
439 struct device_attribute *attr, char *buf)
440 {
441 struct ishtp_device *dev = dev_get_drvdata(cdev);
442
443 return sysfs_emit(buf, "%u.%u.%u.%u\n", dev->prj_ver.major,
444 dev->prj_ver.minor, dev->prj_ver.hotfix,
445 dev->prj_ver.build);
446 }
447 static DEVICE_ATTR_RO(project_version);
448
449 static struct attribute *ish_firmware_attrs[] = {
450 &dev_attr_base_version.attr,
451 &dev_attr_project_version.attr,
452 NULL
453 };
454
firmware_is_visible(struct kobject * kobj,struct attribute * attr,int i)455 static umode_t firmware_is_visible(struct kobject *kobj, struct attribute *attr,
456 int i)
457 {
458 struct ishtp_device *dev = dev_get_drvdata(kobj_to_dev(kobj));
459
460 return dev->driver_data->fw_generation ? attr->mode : 0;
461 }
462
463 static const struct attribute_group ish_firmware_group = {
464 .name = "firmware",
465 .attrs = ish_firmware_attrs,
466 .is_visible = firmware_is_visible,
467 };
468
469 __ATTRIBUTE_GROUPS(ish_firmware);
470
471 static struct pci_driver ish_driver = {
472 .name = KBUILD_MODNAME,
473 .id_table = ish_pci_tbl,
474 .probe = ish_probe,
475 .remove = ish_remove,
476 .shutdown = ish_shutdown,
477 .driver.pm = &ish_pm_ops,
478 .dev_groups = ish_firmware_groups,
479 };
480
481 module_pci_driver(ish_driver);
482
483 /* Original author */
484 MODULE_AUTHOR("Daniel Drubin <daniel.drubin@intel.com>");
485 /* Adoption to upstream Linux kernel */
486 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
487
488 MODULE_DESCRIPTION("Intel(R) Integrated Sensor Hub PCI Device Driver");
489 MODULE_LICENSE("GPL");
490
491 MODULE_FIRMWARE(ISH_FIRMWARE_PATH(ISH_FW_GEN_LNL_M));
492 MODULE_FIRMWARE(ISH_FIRMWARE_PATH_ALL);
493