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