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/suspend.h> 18 #include <linux/interrupt.h> 19 #include <linux/workqueue.h> 20 #define CREATE_TRACE_POINTS 21 #include <trace/events/intel_ish.h> 22 #include "ishtp-dev.h" 23 #include "hw-ish.h" 24 25 static const struct pci_device_id ish_pci_tbl[] = { 26 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CHV_DEVICE_ID)}, 27 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, BXT_Ax_DEVICE_ID)}, 28 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, BXT_Bx_DEVICE_ID)}, 29 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, APL_Ax_DEVICE_ID)}, 30 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, SPT_Ax_DEVICE_ID)}, 31 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CNL_Ax_DEVICE_ID)}, 32 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, GLK_Ax_DEVICE_ID)}, 33 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CNL_H_DEVICE_ID)}, 34 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, ICL_MOBILE_DEVICE_ID)}, 35 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, SPT_H_DEVICE_ID)}, 36 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CML_LP_DEVICE_ID)}, 37 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, EHL_Ax_DEVICE_ID)}, 38 {0, } 39 }; 40 MODULE_DEVICE_TABLE(pci, ish_pci_tbl); 41 42 /** 43 * ish_event_tracer() - Callback function to dump trace messages 44 * @dev: ishtp device 45 * @format: printf style format 46 * 47 * Callback to direct log messages to Linux trace buffers 48 */ 49 static __printf(2, 3) 50 void ish_event_tracer(struct ishtp_device *dev, const char *format, ...) 51 { 52 if (trace_ishtp_dump_enabled()) { 53 va_list args; 54 char tmp_buf[100]; 55 56 va_start(args, format); 57 vsnprintf(tmp_buf, sizeof(tmp_buf), format, args); 58 va_end(args); 59 60 trace_ishtp_dump(tmp_buf); 61 } 62 } 63 64 /** 65 * ish_init() - Init function 66 * @dev: ishtp device 67 * 68 * This function initialize wait queues for suspend/resume and call 69 * calls hadware initialization function. This will initiate 70 * startup sequence 71 * 72 * Return: 0 for success or error code for failure 73 */ 74 static int ish_init(struct ishtp_device *dev) 75 { 76 int ret; 77 78 /* Set the state of ISH HW to start */ 79 ret = ish_hw_start(dev); 80 if (ret) { 81 dev_err(dev->devc, "ISH: hw start failed.\n"); 82 return ret; 83 } 84 85 /* Start the inter process communication to ISH processor */ 86 ret = ishtp_start(dev); 87 if (ret) { 88 dev_err(dev->devc, "ISHTP: Protocol init failed.\n"); 89 return ret; 90 } 91 92 return 0; 93 } 94 95 static const struct pci_device_id ish_invalid_pci_ids[] = { 96 /* Mehlow platform special pci ids */ 97 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0xA309)}, 98 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0xA30A)}, 99 {} 100 }; 101 102 static inline bool ish_should_enter_d0i3(struct pci_dev *pdev) 103 { 104 return !pm_suspend_via_firmware() || pdev->device == CHV_DEVICE_ID; 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 158 /* request and enable interrupt */ 159 ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES); 160 if (!pdev->msi_enabled && !pdev->msix_enabled) 161 irq_flag = IRQF_SHARED; 162 163 ret = devm_request_irq(dev, pdev->irq, ish_irq_handler, 164 irq_flag, KBUILD_MODNAME, ishtp); 165 if (ret) { 166 dev_err(dev, "ISH: request IRQ %d failed\n", pdev->irq); 167 return ret; 168 } 169 170 dev_set_drvdata(ishtp->devc, ishtp); 171 172 init_waitqueue_head(&ishtp->suspend_wait); 173 init_waitqueue_head(&ishtp->resume_wait); 174 175 ret = ish_init(ishtp); 176 if (ret) 177 return ret; 178 179 return 0; 180 } 181 182 /** 183 * ish_remove() - PCI driver remove callback 184 * @pdev: pci device 185 * 186 * This function does cleanup of ISH on pci remove callback 187 */ 188 static void ish_remove(struct pci_dev *pdev) 189 { 190 struct ishtp_device *ishtp_dev = pci_get_drvdata(pdev); 191 192 ishtp_bus_remove_all_clients(ishtp_dev, false); 193 ish_device_disable(ishtp_dev); 194 } 195 196 static struct device __maybe_unused *ish_resume_device; 197 198 /* 50ms to get resume response */ 199 #define WAIT_FOR_RESUME_ACK_MS 50 200 201 /** 202 * ish_resume_handler() - Work function to complete resume 203 * @work: work struct 204 * 205 * The resume work function to complete resume function asynchronously. 206 * There are two resume paths, one where ISH is not powered off, 207 * in that case a simple resume message is enough, others we need 208 * a reset sequence. 209 */ 210 static void __maybe_unused ish_resume_handler(struct work_struct *work) 211 { 212 struct pci_dev *pdev = to_pci_dev(ish_resume_device); 213 struct ishtp_device *dev = pci_get_drvdata(pdev); 214 int ret; 215 216 /* Check the NO_D3 flag to distinguish the resume paths */ 217 if (pdev->dev_flags & PCI_DEV_FLAGS_NO_D3) { 218 pdev->dev_flags &= ~PCI_DEV_FLAGS_NO_D3; 219 disable_irq_wake(pdev->irq); 220 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 * If the flag is not cleared, something is wrong with ISH FW. 231 * So on resume, need to go through init sequence again. 232 */ 233 if (dev->resume_flag) 234 ish_init(dev); 235 } else { 236 /* 237 * Resume from the D3, full reboot of ISH processor will happen, 238 * so need to go through init sequence again. 239 */ 240 ish_init(dev); 241 } 242 } 243 244 /** 245 * ish_suspend() - ISH suspend callback 246 * @device: device pointer 247 * 248 * ISH suspend callback 249 * 250 * Return: 0 to the pm core 251 */ 252 static int __maybe_unused ish_suspend(struct device *device) 253 { 254 struct pci_dev *pdev = to_pci_dev(device); 255 struct ishtp_device *dev = pci_get_drvdata(pdev); 256 257 if (ish_should_enter_d0i3(pdev)) { 258 /* 259 * If previous suspend hasn't been asnwered then ISH is likely 260 * dead, don't attempt nested notification 261 */ 262 if (dev->suspend_flag) 263 return 0; 264 265 dev->resume_flag = 0; 266 dev->suspend_flag = 1; 267 ishtp_send_suspend(dev); 268 269 /* 25 ms should be enough for live ISH to flush all IPC buf */ 270 if (dev->suspend_flag) 271 wait_event_interruptible_timeout(dev->suspend_wait, 272 !dev->suspend_flag, 273 msecs_to_jiffies(25)); 274 275 if (dev->suspend_flag) { 276 /* 277 * It looks like FW halt, clear the DMA bit, and put 278 * ISH into D3, and FW would reset on resume. 279 */ 280 ish_disable_dma(dev); 281 } else { 282 /* Set the NO_D3 flag, the ISH would enter D0i3 */ 283 pdev->dev_flags |= PCI_DEV_FLAGS_NO_D3; 284 285 enable_irq_wake(pdev->irq); 286 } 287 } else { 288 /* 289 * Clear the DMA bit before putting ISH into D3, 290 * or ISH FW would reset automatically. 291 */ 292 ish_disable_dma(dev); 293 } 294 295 return 0; 296 } 297 298 static __maybe_unused DECLARE_WORK(resume_work, ish_resume_handler); 299 /** 300 * ish_resume() - ISH resume callback 301 * @device: device pointer 302 * 303 * ISH resume callback 304 * 305 * Return: 0 to the pm core 306 */ 307 static int __maybe_unused ish_resume(struct device *device) 308 { 309 struct pci_dev *pdev = to_pci_dev(device); 310 struct ishtp_device *dev = pci_get_drvdata(pdev); 311 312 ish_resume_device = device; 313 dev->resume_flag = 1; 314 315 schedule_work(&resume_work); 316 317 return 0; 318 } 319 320 static SIMPLE_DEV_PM_OPS(ish_pm_ops, ish_suspend, ish_resume); 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 = &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