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 static const struct pci_device_id ish_pci_tbl[] = {
27 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, CHV_DEVICE_ID)},
28 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, BXT_Ax_DEVICE_ID)},
29 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, BXT_Bx_DEVICE_ID)},
30 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, APL_Ax_DEVICE_ID)},
31 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, SPT_Ax_DEVICE_ID)},
32 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, CNL_Ax_DEVICE_ID)},
33 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, GLK_Ax_DEVICE_ID)},
34 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, CNL_H_DEVICE_ID)},
35 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, ICL_MOBILE_DEVICE_ID)},
36 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, SPT_H_DEVICE_ID)},
37 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, CML_LP_DEVICE_ID)},
38 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, CMP_H_DEVICE_ID)},
39 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, EHL_Ax_DEVICE_ID)},
40 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, TGL_LP_DEVICE_ID)},
41 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, TGL_H_DEVICE_ID)},
42 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, ADL_S_DEVICE_ID)},
43 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, ADL_P_DEVICE_ID)},
44 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, ADL_N_DEVICE_ID)},
45 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, RPL_S_DEVICE_ID)},
46 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, MTL_P_DEVICE_ID)},
47 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, ARL_H_DEVICE_ID)},
48 	{0, }
49 };
50 MODULE_DEVICE_TABLE(pci, ish_pci_tbl);
51 
52 /**
53  * ish_event_tracer() - Callback function to dump trace messages
54  * @dev:	ishtp device
55  * @format:	printf style format
56  *
57  * Callback to direct log messages to Linux trace buffers
58  */
59 static __printf(2, 3)
ish_event_tracer(struct ishtp_device * dev,const char * format,...)60 void ish_event_tracer(struct ishtp_device *dev, const char *format, ...)
61 {
62 	if (trace_ishtp_dump_enabled()) {
63 		va_list args;
64 		char tmp_buf[100];
65 
66 		va_start(args, format);
67 		vsnprintf(tmp_buf, sizeof(tmp_buf), format, args);
68 		va_end(args);
69 
70 		trace_ishtp_dump(tmp_buf);
71 	}
72 }
73 
74 /**
75  * ish_init() - Init function
76  * @dev:	ishtp device
77  *
78  * This function initialize wait queues for suspend/resume and call
79  * calls hadware initialization function. This will initiate
80  * startup sequence
81  *
82  * Return: 0 for success or error code for failure
83  */
ish_init(struct ishtp_device * dev)84 static int ish_init(struct ishtp_device *dev)
85 {
86 	int ret;
87 
88 	/* Set the state of ISH HW to start */
89 	ret = ish_hw_start(dev);
90 	if (ret) {
91 		dev_err(dev->devc, "ISH: hw start failed.\n");
92 		return ret;
93 	}
94 
95 	/* Start the inter process communication to ISH processor */
96 	ret = ishtp_start(dev);
97 	if (ret) {
98 		dev_err(dev->devc, "ISHTP: Protocol init failed.\n");
99 		return ret;
100 	}
101 
102 	return 0;
103 }
104 
105 static const struct pci_device_id ish_invalid_pci_ids[] = {
106 	/* Mehlow platform special pci ids */
107 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0xA309)},
108 	{PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0xA30A)},
109 	{}
110 };
111 
ish_should_enter_d0i3(struct pci_dev * pdev)112 static inline bool ish_should_enter_d0i3(struct pci_dev *pdev)
113 {
114 	return !pm_suspend_via_firmware() || pdev->device == CHV_DEVICE_ID;
115 }
116 
ish_should_leave_d0i3(struct pci_dev * pdev)117 static inline bool ish_should_leave_d0i3(struct pci_dev *pdev)
118 {
119 	return !pm_resume_via_firmware() || pdev->device == CHV_DEVICE_ID;
120 }
121 
enable_gpe(struct device * dev)122 static int enable_gpe(struct device *dev)
123 {
124 #ifdef CONFIG_ACPI
125 	acpi_status acpi_sts;
126 	struct acpi_device *adev;
127 	struct acpi_device_wakeup *wakeup;
128 
129 	adev = ACPI_COMPANION(dev);
130 	if (!adev) {
131 		dev_err(dev, "get acpi handle failed\n");
132 		return -ENODEV;
133 	}
134 	wakeup = &adev->wakeup;
135 
136 	/*
137 	 * Call acpi_disable_gpe(), so that reference count
138 	 * gpe_event_info->runtime_count doesn't overflow.
139 	 * When gpe_event_info->runtime_count = 0, the call
140 	 * to acpi_disable_gpe() simply return.
141 	 */
142 	acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
143 
144 	acpi_sts = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
145 	if (ACPI_FAILURE(acpi_sts)) {
146 		dev_err(dev, "enable ose_gpe failed\n");
147 		return -EIO;
148 	}
149 
150 	return 0;
151 #else
152 	return -ENODEV;
153 #endif
154 }
155 
enable_pme_wake(struct pci_dev * pdev)156 static void enable_pme_wake(struct pci_dev *pdev)
157 {
158 	if ((pci_pme_capable(pdev, PCI_D0) ||
159 	     pci_pme_capable(pdev, PCI_D3hot) ||
160 	     pci_pme_capable(pdev, PCI_D3cold)) && !enable_gpe(&pdev->dev)) {
161 		pci_pme_active(pdev, true);
162 		dev_dbg(&pdev->dev, "ish ipc driver pme wake enabled\n");
163 	}
164 }
165 
166 /**
167  * ish_probe() - PCI driver probe callback
168  * @pdev:	pci device
169  * @ent:	pci device id
170  *
171  * Initialize PCI function, setup interrupt and call for ISH initialization
172  *
173  * Return: 0 for success or error code for failure
174  */
ish_probe(struct pci_dev * pdev,const struct pci_device_id * ent)175 static int ish_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
176 {
177 	int ret;
178 	struct ish_hw *hw;
179 	unsigned long irq_flag = 0;
180 	struct ishtp_device *ishtp;
181 	struct device *dev = &pdev->dev;
182 
183 	/* Check for invalid platforms for ISH support */
184 	if (pci_dev_present(ish_invalid_pci_ids))
185 		return -ENODEV;
186 
187 	/* enable pci dev */
188 	ret = pcim_enable_device(pdev);
189 	if (ret) {
190 		dev_err(dev, "ISH: Failed to enable PCI device\n");
191 		return ret;
192 	}
193 
194 	/* set PCI host mastering */
195 	pci_set_master(pdev);
196 
197 	/* pci request regions for ISH driver */
198 	ret = pcim_iomap_regions(pdev, 1 << 0, KBUILD_MODNAME);
199 	if (ret) {
200 		dev_err(dev, "ISH: Failed to get PCI regions\n");
201 		return ret;
202 	}
203 
204 	/* allocates and initializes the ISH dev structure */
205 	ishtp = ish_dev_init(pdev);
206 	if (!ishtp) {
207 		ret = -ENOMEM;
208 		return ret;
209 	}
210 	hw = to_ish_hw(ishtp);
211 	ishtp->print_log = ish_event_tracer;
212 
213 	/* mapping IO device memory */
214 	hw->mem_addr = pcim_iomap_table(pdev)[0];
215 	ishtp->pdev = pdev;
216 
217 	/* request and enable interrupt */
218 	ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
219 	if (ret < 0) {
220 		dev_err(dev, "ISH: Failed to allocate IRQ vectors\n");
221 		return ret;
222 	}
223 
224 	if (!pdev->msi_enabled && !pdev->msix_enabled)
225 		irq_flag = IRQF_SHARED;
226 
227 	ret = devm_request_irq(dev, pdev->irq, ish_irq_handler,
228 			       irq_flag, KBUILD_MODNAME, ishtp);
229 	if (ret) {
230 		dev_err(dev, "ISH: request IRQ %d failed\n", pdev->irq);
231 		return ret;
232 	}
233 
234 	dev_set_drvdata(ishtp->devc, ishtp);
235 
236 	init_waitqueue_head(&ishtp->suspend_wait);
237 	init_waitqueue_head(&ishtp->resume_wait);
238 
239 	/* Enable PME for EHL */
240 	if (pdev->device == EHL_Ax_DEVICE_ID)
241 		enable_pme_wake(pdev);
242 
243 	ret = ish_init(ishtp);
244 	if (ret)
245 		return ret;
246 
247 	return 0;
248 }
249 
250 /**
251  * ish_remove() - PCI driver remove callback
252  * @pdev:	pci device
253  *
254  * This function does cleanup of ISH on pci remove callback
255  */
ish_remove(struct pci_dev * pdev)256 static void ish_remove(struct pci_dev *pdev)
257 {
258 	struct ishtp_device *ishtp_dev = pci_get_drvdata(pdev);
259 
260 	ishtp_bus_remove_all_clients(ishtp_dev, false);
261 	ish_device_disable(ishtp_dev);
262 }
263 
264 static struct device __maybe_unused *ish_resume_device;
265 
266 /* 50ms to get resume response */
267 #define WAIT_FOR_RESUME_ACK_MS		50
268 
269 /**
270  * ish_resume_handler() - Work function to complete resume
271  * @work:	work struct
272  *
273  * The resume work function to complete resume function asynchronously.
274  * There are two resume paths, one where ISH is not powered off,
275  * in that case a simple resume message is enough, others we need
276  * a reset sequence.
277  */
ish_resume_handler(struct work_struct * work)278 static void __maybe_unused ish_resume_handler(struct work_struct *work)
279 {
280 	struct pci_dev *pdev = to_pci_dev(ish_resume_device);
281 	struct ishtp_device *dev = pci_get_drvdata(pdev);
282 	uint32_t fwsts = dev->ops->get_fw_status(dev);
283 
284 	if (ish_should_leave_d0i3(pdev) && !dev->suspend_flag
285 			&& IPC_IS_ISH_ILUP(fwsts)) {
286 		if (device_may_wakeup(&pdev->dev))
287 			disable_irq_wake(pdev->irq);
288 
289 		ish_set_host_ready(dev);
290 
291 		ishtp_send_resume(dev);
292 
293 		/* Waiting to get resume response */
294 		if (dev->resume_flag)
295 			wait_event_interruptible_timeout(dev->resume_wait,
296 				!dev->resume_flag,
297 				msecs_to_jiffies(WAIT_FOR_RESUME_ACK_MS));
298 
299 		/*
300 		 * If the flag is not cleared, something is wrong with ISH FW.
301 		 * So on resume, need to go through init sequence again.
302 		 */
303 		if (dev->resume_flag)
304 			ish_init(dev);
305 	} else {
306 		/*
307 		 * Resume from the D3, full reboot of ISH processor will happen,
308 		 * so need to go through init sequence again.
309 		 */
310 		ish_init(dev);
311 	}
312 }
313 
314 /**
315  * ish_suspend() - ISH suspend callback
316  * @device:	device pointer
317  *
318  * ISH suspend callback
319  *
320  * Return: 0 to the pm core
321  */
ish_suspend(struct device * device)322 static int __maybe_unused ish_suspend(struct device *device)
323 {
324 	struct pci_dev *pdev = to_pci_dev(device);
325 	struct ishtp_device *dev = pci_get_drvdata(pdev);
326 
327 	if (ish_should_enter_d0i3(pdev)) {
328 		/*
329 		 * If previous suspend hasn't been asnwered then ISH is likely
330 		 * dead, don't attempt nested notification
331 		 */
332 		if (dev->suspend_flag)
333 			return	0;
334 
335 		dev->resume_flag = 0;
336 		dev->suspend_flag = 1;
337 		ishtp_send_suspend(dev);
338 
339 		/* 25 ms should be enough for live ISH to flush all IPC buf */
340 		if (dev->suspend_flag)
341 			wait_event_interruptible_timeout(dev->suspend_wait,
342 					!dev->suspend_flag,
343 					msecs_to_jiffies(25));
344 
345 		if (dev->suspend_flag) {
346 			/*
347 			 * It looks like FW halt, clear the DMA bit, and put
348 			 * ISH into D3, and FW would reset on resume.
349 			 */
350 			ish_disable_dma(dev);
351 		} else {
352 			/*
353 			 * Save state so PCI core will keep the device at D0,
354 			 * the ISH would enter D0i3
355 			 */
356 			pci_save_state(pdev);
357 
358 			if (device_may_wakeup(&pdev->dev))
359 				enable_irq_wake(pdev->irq);
360 		}
361 	} else {
362 		/*
363 		 * Clear the DMA bit before putting ISH into D3,
364 		 * or ISH FW would reset automatically.
365 		 */
366 		ish_disable_dma(dev);
367 	}
368 
369 	return 0;
370 }
371 
372 static __maybe_unused DECLARE_WORK(resume_work, ish_resume_handler);
373 /**
374  * ish_resume() - ISH resume callback
375  * @device:	device pointer
376  *
377  * ISH resume callback
378  *
379  * Return: 0 to the pm core
380  */
ish_resume(struct device * device)381 static int __maybe_unused ish_resume(struct device *device)
382 {
383 	struct pci_dev *pdev = to_pci_dev(device);
384 	struct ishtp_device *dev = pci_get_drvdata(pdev);
385 
386 	/* add this to finish power flow for EHL */
387 	if (dev->pdev->device == EHL_Ax_DEVICE_ID) {
388 		pci_set_power_state(pdev, PCI_D0);
389 		enable_pme_wake(pdev);
390 		dev_dbg(dev->devc, "set power state to D0 for ehl\n");
391 	}
392 
393 	ish_resume_device = device;
394 	dev->resume_flag = 1;
395 
396 	schedule_work(&resume_work);
397 
398 	return 0;
399 }
400 
401 static SIMPLE_DEV_PM_OPS(ish_pm_ops, ish_suspend, ish_resume);
402 
403 static struct pci_driver ish_driver = {
404 	.name = KBUILD_MODNAME,
405 	.id_table = ish_pci_tbl,
406 	.probe = ish_probe,
407 	.remove = ish_remove,
408 	.driver.pm = &ish_pm_ops,
409 };
410 
411 module_pci_driver(ish_driver);
412 
413 /* Original author */
414 MODULE_AUTHOR("Daniel Drubin <daniel.drubin@intel.com>");
415 /* Adoption to upstream Linux kernel */
416 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
417 
418 MODULE_DESCRIPTION("Intel(R) Integrated Sensor Hub PCI Device Driver");
419 MODULE_LICENSE("GPL");
420