blob: 63c15c917ccbf2deb22b91321e31f21a06c7ef0f [file] [log] [blame]
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -07001/*
2 * PCI glue for ISHTP provider device (ISH) driver
3 *
4 * Copyright (c) 2014-2016, Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 */
15
16#include <linux/module.h>
17#include <linux/moduleparam.h>
18#include <linux/kernel.h>
19#include <linux/device.h>
20#include <linux/fs.h>
21#include <linux/errno.h>
22#include <linux/types.h>
23#include <linux/pci.h>
24#include <linux/sched.h>
25#include <linux/interrupt.h>
26#include <linux/workqueue.h>
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -070027#define CREATE_TRACE_POINTS
28#include <trace/events/intel_ish.h>
29#include "ishtp-dev.h"
30#include "hw-ish.h"
31
32static const struct pci_device_id ish_pci_tbl[] = {
33 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CHV_DEVICE_ID)},
34 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, BXT_Ax_DEVICE_ID)},
35 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, BXT_Bx_DEVICE_ID)},
36 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, APL_Ax_DEVICE_ID)},
37 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, SPT_Ax_DEVICE_ID)},
Song Hongyan1e3b74a2017-06-29 13:43:33 -070038 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CNL_Ax_DEVICE_ID)},
Song Hongyan16941302017-06-29 13:43:34 -070039 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, GLK_Ax_DEVICE_ID)},
Srinivas Pandruvada7103f6b2017-12-20 11:24:10 -080040 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CNL_H_DEVICE_ID)},
Andreas Bosche0ab8b22018-08-17 22:16:00 +020041 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, SPT_H_DEVICE_ID)},
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -070042 {0, }
43};
44MODULE_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 */
Nicolas Iooss0aae34f2016-12-22 11:09:09 +010053static __printf(2, 3)
54void ish_event_tracer(struct ishtp_device *dev, const char *format, ...)
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -070055{
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 */
78static 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
Srinivas Pandruvadaa1e9a9c2018-07-18 12:32:00 -070099static 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
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700106/**
107 * ish_probe() - PCI driver probe callback
108 * @pdev: pci device
109 * @ent: pci device id
110 *
111 * Initialize PCI function, setup interrupt and call for ISH initialization
112 *
113 * Return: 0 for success or error code for failure
114 */
115static int ish_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
116{
Hong Liu946a7772018-09-11 16:44:20 -0700117 int ret;
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700118 struct ish_hw *hw;
Hong Liu946a7772018-09-11 16:44:20 -0700119 struct ishtp_device *ishtp;
120 struct device *dev = &pdev->dev;
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700121
Srinivas Pandruvadaa1e9a9c2018-07-18 12:32:00 -0700122 /* Check for invalid platforms for ISH support */
123 if (pci_dev_present(ish_invalid_pci_ids))
124 return -ENODEV;
125
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700126 /* enable pci dev */
Hong Liu946a7772018-09-11 16:44:20 -0700127 ret = pcim_enable_device(pdev);
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700128 if (ret) {
Hong Liu946a7772018-09-11 16:44:20 -0700129 dev_err(dev, "ISH: Failed to enable PCI device\n");
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700130 return ret;
131 }
132
133 /* set PCI host mastering */
134 pci_set_master(pdev);
135
136 /* pci request regions for ISH driver */
Hong Liu946a7772018-09-11 16:44:20 -0700137 ret = pcim_iomap_regions(pdev, 1 << 0, KBUILD_MODNAME);
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700138 if (ret) {
Hong Liu946a7772018-09-11 16:44:20 -0700139 dev_err(dev, "ISH: Failed to get PCI regions\n");
140 return ret;
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700141 }
142
143 /* allocates and initializes the ISH dev structure */
Hong Liu946a7772018-09-11 16:44:20 -0700144 ishtp = ish_dev_init(pdev);
145 if (!ishtp) {
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700146 ret = -ENOMEM;
Hong Liu946a7772018-09-11 16:44:20 -0700147 return ret;
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700148 }
Hong Liu946a7772018-09-11 16:44:20 -0700149 hw = to_ish_hw(ishtp);
150 ishtp->print_log = ish_event_tracer;
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700151
152 /* mapping IO device memory */
Hong Liu946a7772018-09-11 16:44:20 -0700153 hw->mem_addr = pcim_iomap_table(pdev)[0];
154 ishtp->pdev = pdev;
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700155 pdev->dev_flags |= PCI_DEV_FLAGS_NO_D3;
156
157 /* request and enable interrupt */
Hong Liu946a7772018-09-11 16:44:20 -0700158 ret = devm_request_irq(dev, pdev->irq, ish_irq_handler,
159 IRQF_SHARED, KBUILD_MODNAME, ishtp);
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700160 if (ret) {
Hong Liu946a7772018-09-11 16:44:20 -0700161 dev_err(dev, "ISH: request IRQ %d failed\n", pdev->irq);
162 return ret;
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700163 }
164
Hong Liu946a7772018-09-11 16:44:20 -0700165 dev_set_drvdata(ishtp->devc, ishtp);
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700166
Hong Liu946a7772018-09-11 16:44:20 -0700167 init_waitqueue_head(&ishtp->suspend_wait);
168 init_waitqueue_head(&ishtp->resume_wait);
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700169
Hong Liu946a7772018-09-11 16:44:20 -0700170 ret = ish_init(ishtp);
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700171 if (ret)
Hong Liu946a7772018-09-11 16:44:20 -0700172 return ret;
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700173
174 return 0;
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700175}
176
177/**
178 * ish_remove() - PCI driver remove callback
179 * @pdev: pci device
180 *
181 * This function does cleanup of ISH on pci remove callback
182 */
183static void ish_remove(struct pci_dev *pdev)
184{
185 struct ishtp_device *ishtp_dev = pci_get_drvdata(pdev);
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700186
187 ishtp_bus_remove_all_clients(ishtp_dev, false);
188 ish_device_disable(ishtp_dev);
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700189}
190
Even Xuebeaa362016-02-12 04:11:34 +0800191static struct device __maybe_unused *ish_resume_device;
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700192
Even Xu291e9e3f2017-02-03 14:24:53 +0800193/* 50ms to get resume response */
194#define WAIT_FOR_RESUME_ACK_MS 50
195
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700196/**
197 * ish_resume_handler() - Work function to complete resume
198 * @work: work struct
199 *
200 * The resume work function to complete resume function asynchronously.
Even Xu291e9e3f2017-02-03 14:24:53 +0800201 * There are two resume paths, one where ISH is not powered off,
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700202 * in that case a simple resume message is enough, others we need
203 * a reset sequence.
204 */
Even Xuebeaa362016-02-12 04:11:34 +0800205static void __maybe_unused ish_resume_handler(struct work_struct *work)
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700206{
207 struct pci_dev *pdev = to_pci_dev(ish_resume_device);
208 struct ishtp_device *dev = pci_get_drvdata(pdev);
Even Xu291e9e3f2017-02-03 14:24:53 +0800209 uint32_t fwsts;
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700210 int ret;
211
Even Xu291e9e3f2017-02-03 14:24:53 +0800212 /* Get ISH FW status */
213 fwsts = IPC_GET_ISH_FWSTS(dev->ops->get_fw_status(dev));
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700214
215 /*
Even Xu291e9e3f2017-02-03 14:24:53 +0800216 * If currently, in ISH FW, sensor app is loaded or beyond that,
217 * it means ISH isn't powered off, in this case, send a resume message.
218 */
219 if (fwsts >= FWSTS_SENSOR_APP_LOADED) {
220 ishtp_send_resume(dev);
221
222 /* Waiting to get resume response */
223 if (dev->resume_flag)
224 ret = wait_event_interruptible_timeout(dev->resume_wait,
225 !dev->resume_flag,
226 msecs_to_jiffies(WAIT_FOR_RESUME_ACK_MS));
227 }
228
229 /*
230 * If in ISH FW, sensor app isn't loaded yet, or no resume response.
231 * That means this platform is not S0ix compatible, or something is
232 * wrong with ISH FW. So on resume, full reboot of ISH processor will
233 * happen, so need to go through init sequence again.
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700234 */
235 if (dev->resume_flag)
236 ish_init(dev);
237}
238
239/**
240 * ish_suspend() - ISH suspend callback
241 * @device: device pointer
242 *
243 * ISH suspend callback
244 *
245 * Return: 0 to the pm core
246 */
Even Xuebeaa362016-02-12 04:11:34 +0800247static int __maybe_unused ish_suspend(struct device *device)
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700248{
249 struct pci_dev *pdev = to_pci_dev(device);
250 struct ishtp_device *dev = pci_get_drvdata(pdev);
251
252 enable_irq_wake(pdev->irq);
253 /*
254 * If previous suspend hasn't been asnwered then ISH is likely dead,
255 * don't attempt nested notification
256 */
257 if (dev->suspend_flag)
258 return 0;
259
260 dev->resume_flag = 0;
261 dev->suspend_flag = 1;
262 ishtp_send_suspend(dev);
263
264 /* 25 ms should be enough for live ISH to flush all IPC buf */
265 if (dev->suspend_flag)
266 wait_event_interruptible_timeout(dev->suspend_wait,
267 !dev->suspend_flag,
268 msecs_to_jiffies(25));
269
270 return 0;
271}
272
Even Xuebeaa362016-02-12 04:11:34 +0800273static __maybe_unused DECLARE_WORK(resume_work, ish_resume_handler);
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700274/**
275 * ish_resume() - ISH resume callback
276 * @device: device pointer
277 *
278 * ISH resume callback
279 *
280 * Return: 0 to the pm core
281 */
Even Xuebeaa362016-02-12 04:11:34 +0800282static int __maybe_unused ish_resume(struct device *device)
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700283{
284 struct pci_dev *pdev = to_pci_dev(device);
285 struct ishtp_device *dev = pci_get_drvdata(pdev);
286
287 ish_resume_device = device;
288 dev->resume_flag = 1;
289
290 disable_irq_wake(pdev->irq);
291 schedule_work(&resume_work);
292
293 return 0;
294}
295
Even Xuebeaa362016-02-12 04:11:34 +0800296static SIMPLE_DEV_PM_OPS(ish_pm_ops, ish_suspend, ish_resume);
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700297
298static struct pci_driver ish_driver = {
299 .name = KBUILD_MODNAME,
300 .id_table = ish_pci_tbl,
301 .probe = ish_probe,
302 .remove = ish_remove,
Even Xuebeaa362016-02-12 04:11:34 +0800303 .driver.pm = &ish_pm_ops,
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700304};
305
Wei Yongjun37becf62016-08-21 15:28:36 +0000306module_pci_driver(ish_driver);
Srinivas Pandruvadaae02e5d2016-08-07 02:25:35 -0700307
308/* Original author */
309MODULE_AUTHOR("Daniel Drubin <daniel.drubin@intel.com>");
310/* Adoption to upstream Linux kernel */
311MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
312
313MODULE_DESCRIPTION("Intel(R) Integrated Sensor Hub PCI Device Driver");
314MODULE_LICENSE("GPL");