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