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