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