1 /*
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>
27 #define CREATE_TRACE_POINTS
28 #include <trace/events/intel_ish.h>
29 #include "ishtp-dev.h"
30 #include "hw-ish.h"
31 
32 static 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)},
38 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, CNL_Ax_DEVICE_ID)},
39 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, GLK_Ax_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 /**
98  * ish_probe() - PCI driver probe callback
99  * @pdev:	pci device
100  * @ent:	pci device id
101  *
102  * Initialize PCI function, setup interrupt and call for ISH initialization
103  *
104  * Return: 0 for success or error code for failure
105  */
106 static int ish_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
107 {
108 	struct ishtp_device *dev;
109 	struct ish_hw *hw;
110 	int	ret;
111 
112 	/* enable pci dev */
113 	ret = pci_enable_device(pdev);
114 	if (ret) {
115 		dev_err(&pdev->dev, "ISH: Failed to enable PCI device\n");
116 		return ret;
117 	}
118 
119 	/* set PCI host mastering */
120 	pci_set_master(pdev);
121 
122 	/* pci request regions for ISH driver */
123 	ret = pci_request_regions(pdev, KBUILD_MODNAME);
124 	if (ret) {
125 		dev_err(&pdev->dev, "ISH: Failed to get PCI regions\n");
126 		goto disable_device;
127 	}
128 
129 	/* allocates and initializes the ISH dev structure */
130 	dev = ish_dev_init(pdev);
131 	if (!dev) {
132 		ret = -ENOMEM;
133 		goto release_regions;
134 	}
135 	hw = to_ish_hw(dev);
136 	dev->print_log = ish_event_tracer;
137 
138 	/* mapping IO device memory */
139 	hw->mem_addr = pci_iomap(pdev, 0, 0);
140 	if (!hw->mem_addr) {
141 		dev_err(&pdev->dev, "ISH: mapping I/O range failure\n");
142 		ret = -ENOMEM;
143 		goto free_device;
144 	}
145 
146 	dev->pdev = pdev;
147 
148 	pdev->dev_flags |= PCI_DEV_FLAGS_NO_D3;
149 
150 	/* request and enable interrupt */
151 	ret = request_irq(pdev->irq, ish_irq_handler, IRQF_SHARED,
152 			  KBUILD_MODNAME, dev);
153 	if (ret) {
154 		dev_err(&pdev->dev, "ISH: request IRQ failure (%d)\n",
155 			pdev->irq);
156 		goto free_device;
157 	}
158 
159 	dev_set_drvdata(dev->devc, dev);
160 
161 	init_waitqueue_head(&dev->suspend_wait);
162 	init_waitqueue_head(&dev->resume_wait);
163 
164 	ret = ish_init(dev);
165 	if (ret)
166 		goto free_irq;
167 
168 	return 0;
169 
170 free_irq:
171 	free_irq(pdev->irq, dev);
172 free_device:
173 	pci_iounmap(pdev, hw->mem_addr);
174 	kfree(dev);
175 release_regions:
176 	pci_release_regions(pdev);
177 disable_device:
178 	pci_clear_master(pdev);
179 	pci_disable_device(pdev);
180 	dev_err(&pdev->dev, "ISH: PCI driver initialization failed.\n");
181 
182 	return ret;
183 }
184 
185 /**
186  * ish_remove() - PCI driver remove callback
187  * @pdev:	pci device
188  *
189  * This function does cleanup of ISH on pci remove callback
190  */
191 static void ish_remove(struct pci_dev *pdev)
192 {
193 	struct ishtp_device *ishtp_dev = pci_get_drvdata(pdev);
194 	struct ish_hw *hw = to_ish_hw(ishtp_dev);
195 
196 	ishtp_bus_remove_all_clients(ishtp_dev, false);
197 	ish_device_disable(ishtp_dev);
198 
199 	free_irq(pdev->irq, ishtp_dev);
200 	pci_iounmap(pdev, hw->mem_addr);
201 	pci_release_regions(pdev);
202 	pci_clear_master(pdev);
203 	pci_disable_device(pdev);
204 	kfree(ishtp_dev);
205 }
206 
207 #ifdef CONFIG_PM
208 static struct device *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 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 	uint32_t fwsts;
227 	int ret;
228 
229 	/* Get ISH FW status */
230 	fwsts = IPC_GET_ISH_FWSTS(dev->ops->get_fw_status(dev));
231 
232 	/*
233 	 * If currently, in ISH FW, sensor app is loaded or beyond that,
234 	 * it means ISH isn't powered off, in this case, send a resume message.
235 	 */
236 	if (fwsts >= FWSTS_SENSOR_APP_LOADED) {
237 		ishtp_send_resume(dev);
238 
239 		/* Waiting to get resume response */
240 		if (dev->resume_flag)
241 			ret = wait_event_interruptible_timeout(dev->resume_wait,
242 				!dev->resume_flag,
243 				msecs_to_jiffies(WAIT_FOR_RESUME_ACK_MS));
244 	}
245 
246 	/*
247 	 * If in ISH FW, sensor app isn't loaded yet, or no resume response.
248 	 * That means this platform is not S0ix compatible, or something is
249 	 * wrong with ISH FW. So on resume, full reboot of ISH processor will
250 	 * happen, so need to go through init sequence again.
251 	 */
252 	if (dev->resume_flag)
253 		ish_init(dev);
254 }
255 
256 /**
257  * ish_suspend() - ISH suspend callback
258  * @device:	device pointer
259  *
260  * ISH suspend callback
261  *
262  * Return: 0 to the pm core
263  */
264 static int ish_suspend(struct device *device)
265 {
266 	struct pci_dev *pdev = to_pci_dev(device);
267 	struct ishtp_device *dev = pci_get_drvdata(pdev);
268 
269 	enable_irq_wake(pdev->irq);
270 	/*
271 	 * If previous suspend hasn't been asnwered then ISH is likely dead,
272 	 * don't attempt nested notification
273 	 */
274 	if (dev->suspend_flag)
275 		return	0;
276 
277 	dev->resume_flag = 0;
278 	dev->suspend_flag = 1;
279 	ishtp_send_suspend(dev);
280 
281 	/* 25 ms should be enough for live ISH to flush all IPC buf */
282 	if (dev->suspend_flag)
283 		wait_event_interruptible_timeout(dev->suspend_wait,
284 						 !dev->suspend_flag,
285 						  msecs_to_jiffies(25));
286 
287 	return 0;
288 }
289 
290 static DECLARE_WORK(resume_work, ish_resume_handler);
291 /**
292  * ish_resume() - ISH resume callback
293  * @device:	device pointer
294  *
295  * ISH resume callback
296  *
297  * Return: 0 to the pm core
298  */
299 static int ish_resume(struct device *device)
300 {
301 	struct pci_dev *pdev = to_pci_dev(device);
302 	struct ishtp_device *dev = pci_get_drvdata(pdev);
303 
304 	ish_resume_device = device;
305 	dev->resume_flag = 1;
306 
307 	disable_irq_wake(pdev->irq);
308 	schedule_work(&resume_work);
309 
310 	return 0;
311 }
312 
313 static const struct dev_pm_ops ish_pm_ops = {
314 	.suspend = ish_suspend,
315 	.resume = ish_resume,
316 };
317 #define ISHTP_ISH_PM_OPS	(&ish_pm_ops)
318 #else
319 #define ISHTP_ISH_PM_OPS	NULL
320 #endif /* CONFIG_PM */
321 
322 static struct pci_driver ish_driver = {
323 	.name = KBUILD_MODNAME,
324 	.id_table = ish_pci_tbl,
325 	.probe = ish_probe,
326 	.remove = ish_remove,
327 	.driver.pm = ISHTP_ISH_PM_OPS,
328 };
329 
330 module_pci_driver(ish_driver);
331 
332 /* Original author */
333 MODULE_AUTHOR("Daniel Drubin <daniel.drubin@intel.com>");
334 /* Adoption to upstream Linux kernel */
335 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
336 
337 MODULE_DESCRIPTION("Intel(R) Integrated Sensor Hub PCI Device Driver");
338 MODULE_LICENSE("GPL");
339