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)}, |
Andreas Bosch | e0ab8b2 | 2018-08-17 22:16:00 +0200 | [diff] [blame] | 41 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, SPT_H_DEVICE_ID)}, |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 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 | */ |
Nicolas Iooss | 0aae34f | 2016-12-22 11:09:09 +0100 | [diff] [blame] | 53 | static __printf(2, 3) |
| 54 | void ish_event_tracer(struct ishtp_device *dev, const char *format, ...) |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 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 | |
Srinivas Pandruvada | a1e9a9c | 2018-07-18 12:32:00 -0700 | [diff] [blame] | 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 | |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 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 | { |
Hong Liu | 946a777 | 2018-09-11 16:44:20 -0700 | [diff] [blame^] | 117 | int ret; |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 118 | struct ish_hw *hw; |
Hong Liu | 946a777 | 2018-09-11 16:44:20 -0700 | [diff] [blame^] | 119 | struct ishtp_device *ishtp; |
| 120 | struct device *dev = &pdev->dev; |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 121 | |
Srinivas Pandruvada | a1e9a9c | 2018-07-18 12:32:00 -0700 | [diff] [blame] | 122 | /* Check for invalid platforms for ISH support */ |
| 123 | if (pci_dev_present(ish_invalid_pci_ids)) |
| 124 | return -ENODEV; |
| 125 | |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 126 | /* enable pci dev */ |
Hong Liu | 946a777 | 2018-09-11 16:44:20 -0700 | [diff] [blame^] | 127 | ret = pcim_enable_device(pdev); |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 128 | if (ret) { |
Hong Liu | 946a777 | 2018-09-11 16:44:20 -0700 | [diff] [blame^] | 129 | dev_err(dev, "ISH: Failed to enable PCI device\n"); |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 130 | return ret; |
| 131 | } |
| 132 | |
| 133 | /* set PCI host mastering */ |
| 134 | pci_set_master(pdev); |
| 135 | |
| 136 | /* pci request regions for ISH driver */ |
Hong Liu | 946a777 | 2018-09-11 16:44:20 -0700 | [diff] [blame^] | 137 | ret = pcim_iomap_regions(pdev, 1 << 0, KBUILD_MODNAME); |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 138 | if (ret) { |
Hong Liu | 946a777 | 2018-09-11 16:44:20 -0700 | [diff] [blame^] | 139 | dev_err(dev, "ISH: Failed to get PCI regions\n"); |
| 140 | return ret; |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /* allocates and initializes the ISH dev structure */ |
Hong Liu | 946a777 | 2018-09-11 16:44:20 -0700 | [diff] [blame^] | 144 | ishtp = ish_dev_init(pdev); |
| 145 | if (!ishtp) { |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 146 | ret = -ENOMEM; |
Hong Liu | 946a777 | 2018-09-11 16:44:20 -0700 | [diff] [blame^] | 147 | return ret; |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 148 | } |
Hong Liu | 946a777 | 2018-09-11 16:44:20 -0700 | [diff] [blame^] | 149 | hw = to_ish_hw(ishtp); |
| 150 | ishtp->print_log = ish_event_tracer; |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 151 | |
| 152 | /* mapping IO device memory */ |
Hong Liu | 946a777 | 2018-09-11 16:44:20 -0700 | [diff] [blame^] | 153 | hw->mem_addr = pcim_iomap_table(pdev)[0]; |
| 154 | ishtp->pdev = pdev; |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 155 | pdev->dev_flags |= PCI_DEV_FLAGS_NO_D3; |
| 156 | |
| 157 | /* request and enable interrupt */ |
Hong Liu | 946a777 | 2018-09-11 16:44:20 -0700 | [diff] [blame^] | 158 | ret = devm_request_irq(dev, pdev->irq, ish_irq_handler, |
| 159 | IRQF_SHARED, KBUILD_MODNAME, ishtp); |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 160 | if (ret) { |
Hong Liu | 946a777 | 2018-09-11 16:44:20 -0700 | [diff] [blame^] | 161 | dev_err(dev, "ISH: request IRQ %d failed\n", pdev->irq); |
| 162 | return ret; |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Hong Liu | 946a777 | 2018-09-11 16:44:20 -0700 | [diff] [blame^] | 165 | dev_set_drvdata(ishtp->devc, ishtp); |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 166 | |
Hong Liu | 946a777 | 2018-09-11 16:44:20 -0700 | [diff] [blame^] | 167 | init_waitqueue_head(&ishtp->suspend_wait); |
| 168 | init_waitqueue_head(&ishtp->resume_wait); |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 169 | |
Hong Liu | 946a777 | 2018-09-11 16:44:20 -0700 | [diff] [blame^] | 170 | ret = ish_init(ishtp); |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 171 | if (ret) |
Hong Liu | 946a777 | 2018-09-11 16:44:20 -0700 | [diff] [blame^] | 172 | return ret; |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 173 | |
| 174 | return 0; |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 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); |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 186 | |
| 187 | ishtp_bus_remove_all_clients(ishtp_dev, false); |
| 188 | ish_device_disable(ishtp_dev); |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Even Xu | ebeaa36 | 2016-02-12 04:11:34 +0800 | [diff] [blame] | 191 | static struct device __maybe_unused *ish_resume_device; |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 192 | |
Even Xu | 291e9e3f | 2017-02-03 14:24:53 +0800 | [diff] [blame] | 193 | /* 50ms to get resume response */ |
| 194 | #define WAIT_FOR_RESUME_ACK_MS 50 |
| 195 | |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 196 | /** |
| 197 | * ish_resume_handler() - Work function to complete resume |
| 198 | * @work: work struct |
| 199 | * |
| 200 | * The resume work function to complete resume function asynchronously. |
Even Xu | 291e9e3f | 2017-02-03 14:24:53 +0800 | [diff] [blame] | 201 | * There are two resume paths, one where ISH is not powered off, |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 202 | * in that case a simple resume message is enough, others we need |
| 203 | * a reset sequence. |
| 204 | */ |
Even Xu | ebeaa36 | 2016-02-12 04:11:34 +0800 | [diff] [blame] | 205 | static void __maybe_unused ish_resume_handler(struct work_struct *work) |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 206 | { |
| 207 | struct pci_dev *pdev = to_pci_dev(ish_resume_device); |
| 208 | struct ishtp_device *dev = pci_get_drvdata(pdev); |
Even Xu | 291e9e3f | 2017-02-03 14:24:53 +0800 | [diff] [blame] | 209 | uint32_t fwsts; |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 210 | int ret; |
| 211 | |
Even Xu | 291e9e3f | 2017-02-03 14:24:53 +0800 | [diff] [blame] | 212 | /* Get ISH FW status */ |
| 213 | fwsts = IPC_GET_ISH_FWSTS(dev->ops->get_fw_status(dev)); |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 214 | |
| 215 | /* |
Even Xu | 291e9e3f | 2017-02-03 14:24:53 +0800 | [diff] [blame] | 216 | * If currently, in ISH FW, sensor app is loaded or beyond that, |
| 217 | * it means ISH isn't powered off, in this case, send a resume message. |
| 218 | */ |
| 219 | if (fwsts >= FWSTS_SENSOR_APP_LOADED) { |
| 220 | ishtp_send_resume(dev); |
| 221 | |
| 222 | /* Waiting to get resume response */ |
| 223 | if (dev->resume_flag) |
| 224 | ret = wait_event_interruptible_timeout(dev->resume_wait, |
| 225 | !dev->resume_flag, |
| 226 | msecs_to_jiffies(WAIT_FOR_RESUME_ACK_MS)); |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * If in ISH FW, sensor app isn't loaded yet, or no resume response. |
| 231 | * That means this platform is not S0ix compatible, or something is |
| 232 | * wrong with ISH FW. So on resume, full reboot of ISH processor will |
| 233 | * happen, so need to go through init sequence again. |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 234 | */ |
| 235 | if (dev->resume_flag) |
| 236 | ish_init(dev); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * ish_suspend() - ISH suspend callback |
| 241 | * @device: device pointer |
| 242 | * |
| 243 | * ISH suspend callback |
| 244 | * |
| 245 | * Return: 0 to the pm core |
| 246 | */ |
Even Xu | ebeaa36 | 2016-02-12 04:11:34 +0800 | [diff] [blame] | 247 | static int __maybe_unused ish_suspend(struct device *device) |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 248 | { |
| 249 | struct pci_dev *pdev = to_pci_dev(device); |
| 250 | struct ishtp_device *dev = pci_get_drvdata(pdev); |
| 251 | |
| 252 | enable_irq_wake(pdev->irq); |
| 253 | /* |
| 254 | * If previous suspend hasn't been asnwered then ISH is likely dead, |
| 255 | * don't attempt nested notification |
| 256 | */ |
| 257 | if (dev->suspend_flag) |
| 258 | return 0; |
| 259 | |
| 260 | dev->resume_flag = 0; |
| 261 | dev->suspend_flag = 1; |
| 262 | ishtp_send_suspend(dev); |
| 263 | |
| 264 | /* 25 ms should be enough for live ISH to flush all IPC buf */ |
| 265 | if (dev->suspend_flag) |
| 266 | wait_event_interruptible_timeout(dev->suspend_wait, |
| 267 | !dev->suspend_flag, |
| 268 | msecs_to_jiffies(25)); |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
Even Xu | ebeaa36 | 2016-02-12 04:11:34 +0800 | [diff] [blame] | 273 | static __maybe_unused DECLARE_WORK(resume_work, ish_resume_handler); |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 274 | /** |
| 275 | * ish_resume() - ISH resume callback |
| 276 | * @device: device pointer |
| 277 | * |
| 278 | * ISH resume callback |
| 279 | * |
| 280 | * Return: 0 to the pm core |
| 281 | */ |
Even Xu | ebeaa36 | 2016-02-12 04:11:34 +0800 | [diff] [blame] | 282 | static int __maybe_unused ish_resume(struct device *device) |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 283 | { |
| 284 | struct pci_dev *pdev = to_pci_dev(device); |
| 285 | struct ishtp_device *dev = pci_get_drvdata(pdev); |
| 286 | |
| 287 | ish_resume_device = device; |
| 288 | dev->resume_flag = 1; |
| 289 | |
| 290 | disable_irq_wake(pdev->irq); |
| 291 | schedule_work(&resume_work); |
| 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
Even Xu | ebeaa36 | 2016-02-12 04:11:34 +0800 | [diff] [blame] | 296 | static SIMPLE_DEV_PM_OPS(ish_pm_ops, ish_suspend, ish_resume); |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 297 | |
| 298 | static struct pci_driver ish_driver = { |
| 299 | .name = KBUILD_MODNAME, |
| 300 | .id_table = ish_pci_tbl, |
| 301 | .probe = ish_probe, |
| 302 | .remove = ish_remove, |
Even Xu | ebeaa36 | 2016-02-12 04:11:34 +0800 | [diff] [blame] | 303 | .driver.pm = &ish_pm_ops, |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 304 | }; |
| 305 | |
Wei Yongjun | 37becf6 | 2016-08-21 15:28:36 +0000 | [diff] [blame] | 306 | module_pci_driver(ish_driver); |
Srinivas Pandruvada | ae02e5d | 2016-08-07 02:25:35 -0700 | [diff] [blame] | 307 | |
| 308 | /* Original author */ |
| 309 | MODULE_AUTHOR("Daniel Drubin <daniel.drubin@intel.com>"); |
| 310 | /* Adoption to upstream Linux kernel */ |
| 311 | MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>"); |
| 312 | |
| 313 | MODULE_DESCRIPTION("Intel(R) Integrated Sensor Hub PCI Device Driver"); |
| 314 | MODULE_LICENSE("GPL"); |