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 enum ishtp_driver_data_index { 27 ISHTP_DRIVER_DATA_NONE, 28 ISHTP_DRIVER_DATA_LNL_M, 29 ISHTP_DRIVER_DATA_PTL, 30 ISHTP_DRIVER_DATA_WCL, 31 }; 32 33 #define ISH_FW_GEN_LNL_M "lnlm" 34 #define ISH_FW_GEN_PTL "ptl" 35 #define ISH_FW_GEN_WCL "wcl" 36 37 #define ISH_FIRMWARE_PATH(gen) "intel/ish/ish_" gen ".bin" 38 #define ISH_FIRMWARE_PATH_ALL "intel/ish/ish_*.bin" 39 40 static struct ishtp_driver_data ishtp_driver_data[] = { 41 [ISHTP_DRIVER_DATA_LNL_M] = { 42 .fw_generation = ISH_FW_GEN_LNL_M, 43 }, 44 [ISHTP_DRIVER_DATA_PTL] = { 45 .fw_generation = ISH_FW_GEN_PTL, 46 }, 47 [ISHTP_DRIVER_DATA_WCL] = { 48 .fw_generation = ISH_FW_GEN_WCL, 49 }, 50 }; 51 52 static const struct pci_device_id ish_pci_tbl[] = { 53 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_CHV)}, 54 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_BXT_Ax)}, 55 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_BXT_Bx)}, 56 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_APL_Ax)}, 57 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_SPT_Ax)}, 58 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_CNL_Ax)}, 59 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_GLK_Ax)}, 60 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_CNL_H)}, 61 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_ICL_MOBILE)}, 62 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_SPT_H)}, 63 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_CML_LP)}, 64 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_CMP_H)}, 65 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_EHL_Ax)}, 66 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_TGL_LP)}, 67 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_TGL_H)}, 68 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_ADL_S)}, 69 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_ADL_P)}, 70 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_ADL_N)}, 71 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_RPL_S)}, 72 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_MTL_P)}, 73 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_ARL_H)}, 74 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_ARL_S)}, 75 {PCI_DEVICE_DATA(INTEL, ISH_LNL_M, ISHTP_DRIVER_DATA_LNL_M)}, 76 {PCI_DEVICE_DATA(INTEL, ISH_PTL_H, ISHTP_DRIVER_DATA_PTL)}, 77 {PCI_DEVICE_DATA(INTEL, ISH_PTL_P, ISHTP_DRIVER_DATA_PTL)}, 78 {PCI_DEVICE_DATA(INTEL, ISH_WCL, ISHTP_DRIVER_DATA_WCL)}, 79 {} 80 }; 81 MODULE_DEVICE_TABLE(pci, ish_pci_tbl); 82 83 /** 84 * ish_event_tracer() - Callback function to dump trace messages 85 * @dev: ishtp device 86 * @format: printf style format 87 * 88 * Callback to direct log messages to Linux trace buffers 89 */ 90 static __printf(2, 3) 91 void ish_event_tracer(struct ishtp_device *dev, const char *format, ...) 92 { 93 if (trace_ishtp_dump_enabled()) { 94 va_list args; 95 char tmp_buf[100]; 96 97 va_start(args, format); 98 vsnprintf(tmp_buf, sizeof(tmp_buf), format, args); 99 va_end(args); 100 101 trace_ishtp_dump(tmp_buf); 102 } 103 } 104 105 /** 106 * ish_init() - Init function 107 * @dev: ishtp device 108 * 109 * This function initialize wait queues for suspend/resume and call 110 * calls hadware initialization function. This will initiate 111 * startup sequence 112 * 113 * Return: 0 for success or error code for failure 114 */ 115 static int ish_init(struct ishtp_device *dev) 116 { 117 int ret; 118 119 /* Set the state of ISH HW to start */ 120 ret = ish_hw_start(dev); 121 if (ret) { 122 dev_err(dev->devc, "ISH: hw start failed.\n"); 123 return ret; 124 } 125 126 /* Start the inter process communication to ISH processor */ 127 ret = ishtp_start(dev); 128 if (ret) { 129 dev_err(dev->devc, "ISHTP: Protocol init failed.\n"); 130 return ret; 131 } 132 133 return 0; 134 } 135 136 static const struct pci_device_id ish_invalid_pci_ids[] = { 137 /* Mehlow platform special pci ids */ 138 {PCI_VDEVICE(INTEL, 0xA309)}, 139 {PCI_VDEVICE(INTEL, 0xA30A)}, 140 {} 141 }; 142 143 static inline bool ish_should_enter_d0i3(struct pci_dev *pdev) 144 { 145 return !pm_suspend_via_firmware() || pdev->device == PCI_DEVICE_ID_INTEL_ISH_CHV; 146 } 147 148 static inline bool ish_should_leave_d0i3(struct pci_dev *pdev) 149 { 150 struct ishtp_device *dev = pci_get_drvdata(pdev); 151 u32 fwsts = dev->ops->get_fw_status(dev); 152 153 if (dev->suspend_flag || !IPC_IS_ISH_ILUP(fwsts)) 154 return false; 155 156 return !pm_resume_via_firmware() || pdev->device == PCI_DEVICE_ID_INTEL_ISH_CHV; 157 } 158 159 /** 160 * ish_probe() - PCI driver probe callback 161 * @pdev: pci device 162 * @ent: pci device id 163 * 164 * Initialize PCI function, setup interrupt and call for ISH initialization 165 * 166 * Return: 0 for success or error code for failure 167 */ 168 static int ish_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 169 { 170 int ret; 171 struct ish_hw *hw; 172 unsigned long irq_flag = 0; 173 struct ishtp_device *ishtp; 174 struct device *dev = &pdev->dev; 175 176 /* Check for invalid platforms for ISH support */ 177 if (pci_dev_present(ish_invalid_pci_ids)) 178 return -ENODEV; 179 180 /* enable pci dev */ 181 ret = pcim_enable_device(pdev); 182 if (ret) { 183 dev_err(dev, "ISH: Failed to enable PCI device\n"); 184 return ret; 185 } 186 187 /* set PCI host mastering */ 188 pci_set_master(pdev); 189 190 /* pci request regions for ISH driver */ 191 ret = pcim_iomap_regions(pdev, 1 << 0, KBUILD_MODNAME); 192 if (ret) { 193 dev_err(dev, "ISH: Failed to get PCI regions\n"); 194 return ret; 195 } 196 197 /* allocates and initializes the ISH dev structure */ 198 ishtp = ish_dev_init(pdev); 199 if (!ishtp) { 200 ret = -ENOMEM; 201 return ret; 202 } 203 hw = to_ish_hw(ishtp); 204 ishtp->print_log = ish_event_tracer; 205 ishtp->driver_data = &ishtp_driver_data[ent->driver_data]; 206 207 /* mapping IO device memory */ 208 hw->mem_addr = pcim_iomap_table(pdev)[0]; 209 ishtp->pdev = pdev; 210 211 /* request and enable interrupt */ 212 ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES); 213 if (ret < 0) { 214 dev_err(dev, "ISH: Failed to allocate IRQ vectors\n"); 215 return ret; 216 } 217 218 if (!pdev->msi_enabled && !pdev->msix_enabled) 219 irq_flag = IRQF_SHARED; 220 221 ret = devm_request_irq(dev, pdev->irq, ish_irq_handler, 222 irq_flag, KBUILD_MODNAME, ishtp); 223 if (ret) { 224 dev_err(dev, "ISH: request IRQ %d failed\n", pdev->irq); 225 return ret; 226 } 227 228 dev_set_drvdata(ishtp->devc, ishtp); 229 230 init_waitqueue_head(&ishtp->suspend_wait); 231 init_waitqueue_head(&ishtp->resume_wait); 232 233 /* Enable PME for EHL */ 234 if (pdev->device == PCI_DEVICE_ID_INTEL_ISH_EHL_Ax) 235 device_init_wakeup(dev, true); 236 237 ret = ish_init(ishtp); 238 if (ret) 239 return ret; 240 241 return 0; 242 } 243 244 /** 245 * ish_remove() - PCI driver remove callback 246 * @pdev: pci device 247 * 248 * This function does cleanup of ISH on pci remove callback 249 */ 250 static void ish_remove(struct pci_dev *pdev) 251 { 252 struct ishtp_device *ishtp_dev = pci_get_drvdata(pdev); 253 254 ishtp_bus_remove_all_clients(ishtp_dev, false); 255 ish_device_disable(ishtp_dev); 256 } 257 258 259 /** 260 * ish_shutdown() - PCI driver shutdown callback 261 * @pdev: pci device 262 * 263 * This function sets up wakeup for S5 264 */ 265 static void ish_shutdown(struct pci_dev *pdev) 266 { 267 if (pdev->device == PCI_DEVICE_ID_INTEL_ISH_EHL_Ax) 268 pci_prepare_to_sleep(pdev); 269 } 270 271 static struct device __maybe_unused *ish_resume_device; 272 273 /** 274 * ish_resume_handler() - Work function to complete resume 275 * @work: work struct 276 * 277 * The resume work function to complete resume function asynchronously. 278 * There are two resume paths, one where ISH is not powered off, 279 * in that case a simple resume message is enough, others we need 280 * a reset sequence. 281 */ 282 static void __maybe_unused ish_resume_handler(struct work_struct *work) 283 { 284 struct pci_dev *pdev = to_pci_dev(ish_resume_device); 285 struct ishtp_device *dev = pci_get_drvdata(pdev); 286 287 if (ish_should_leave_d0i3(pdev)) { 288 if (device_may_wakeup(&pdev->dev)) 289 disable_irq_wake(pdev->irq); 290 291 ish_set_host_ready(dev); 292 293 ishtp_send_resume(dev); 294 295 /* Waiting to get resume response */ 296 if (dev->resume_flag) 297 wait_event_interruptible_timeout(dev->resume_wait, 298 !dev->resume_flag, 299 msecs_to_jiffies(WAIT_FOR_RESUME_ACK_MS)); 300 301 /* 302 * If the flag is not cleared, something is wrong with ISH FW. 303 * So on resume, need to go through init sequence again. 304 */ 305 if (dev->resume_flag) 306 ish_init(dev); 307 } else { 308 /* 309 * Resume from the D3, full reboot of ISH processor will happen, 310 * so need to go through init sequence again. 311 */ 312 ish_init(dev); 313 } 314 } 315 316 /** 317 * ish_suspend() - ISH suspend callback 318 * @device: device pointer 319 * 320 * ISH suspend callback 321 * 322 * Return: 0 to the pm core 323 */ 324 static int __maybe_unused ish_suspend(struct device *device) 325 { 326 struct pci_dev *pdev = to_pci_dev(device); 327 struct ishtp_device *dev = pci_get_drvdata(pdev); 328 329 if (ish_should_enter_d0i3(pdev)) { 330 /* 331 * If previous suspend hasn't been asnwered then ISH is likely 332 * dead, don't attempt nested notification 333 */ 334 if (dev->suspend_flag) 335 return 0; 336 337 dev->resume_flag = 0; 338 dev->suspend_flag = 1; 339 ishtp_send_suspend(dev); 340 341 /* 25 ms should be enough for live ISH to flush all IPC buf */ 342 if (dev->suspend_flag) 343 wait_event_interruptible_timeout(dev->suspend_wait, 344 !dev->suspend_flag, 345 msecs_to_jiffies(25)); 346 347 if (dev->suspend_flag) { 348 /* 349 * It looks like FW halt, clear the DMA bit, and put 350 * ISH into D3, and FW would reset on resume. 351 */ 352 ish_disable_dma(dev); 353 } else { 354 /* 355 * Save state so PCI core will keep the device at D0, 356 * the ISH would enter D0i3 357 */ 358 pci_save_state(pdev); 359 360 if (device_may_wakeup(&pdev->dev)) 361 enable_irq_wake(pdev->irq); 362 } 363 } else { 364 /* 365 * Clear the DMA bit before putting ISH into D3, 366 * or ISH FW would reset automatically. 367 */ 368 ish_disable_dma(dev); 369 } 370 371 return 0; 372 } 373 374 static __maybe_unused DECLARE_WORK(resume_work, ish_resume_handler); 375 /** 376 * ish_resume() - ISH resume callback 377 * @device: device pointer 378 * 379 * ISH resume callback 380 * 381 * Return: 0 to the pm core 382 */ 383 static int __maybe_unused ish_resume(struct device *device) 384 { 385 struct pci_dev *pdev = to_pci_dev(device); 386 struct ishtp_device *dev = pci_get_drvdata(pdev); 387 388 ish_resume_device = device; 389 dev->resume_flag = 1; 390 391 /* If ISH resume from D3, reset ishtp clients before return */ 392 if (!ish_should_leave_d0i3(pdev)) 393 ishtp_reset_handler(dev); 394 395 queue_work(dev->unbound_wq, &resume_work); 396 397 return 0; 398 } 399 400 static int __maybe_unused ish_freeze(struct device *device) 401 { 402 struct pci_dev *pdev = to_pci_dev(device); 403 404 return pci_save_state(pdev); 405 } 406 407 static const struct dev_pm_ops __maybe_unused ish_pm_ops = { 408 .suspend = pm_sleep_ptr(ish_suspend), 409 .resume = pm_sleep_ptr(ish_resume), 410 .freeze = pm_sleep_ptr(ish_freeze), 411 .restore = pm_sleep_ptr(ish_resume), 412 .poweroff = pm_sleep_ptr(ish_suspend), 413 }; 414 415 static ssize_t base_version_show(struct device *cdev, 416 struct device_attribute *attr, char *buf) 417 { 418 struct ishtp_device *dev = dev_get_drvdata(cdev); 419 420 return sysfs_emit(buf, "%u.%u.%u.%u\n", dev->base_ver.major, 421 dev->base_ver.minor, dev->base_ver.hotfix, 422 dev->base_ver.build); 423 } 424 static DEVICE_ATTR_RO(base_version); 425 426 static ssize_t project_version_show(struct device *cdev, 427 struct device_attribute *attr, char *buf) 428 { 429 struct ishtp_device *dev = dev_get_drvdata(cdev); 430 431 return sysfs_emit(buf, "%u.%u.%u.%u\n", dev->prj_ver.major, 432 dev->prj_ver.minor, dev->prj_ver.hotfix, 433 dev->prj_ver.build); 434 } 435 static DEVICE_ATTR_RO(project_version); 436 437 static struct attribute *ish_firmware_attrs[] = { 438 &dev_attr_base_version.attr, 439 &dev_attr_project_version.attr, 440 NULL 441 }; 442 443 static umode_t firmware_is_visible(struct kobject *kobj, struct attribute *attr, 444 int i) 445 { 446 struct ishtp_device *dev = dev_get_drvdata(kobj_to_dev(kobj)); 447 448 return dev->driver_data->fw_generation ? attr->mode : 0; 449 } 450 451 static const struct attribute_group ish_firmware_group = { 452 .name = "firmware", 453 .attrs = ish_firmware_attrs, 454 .is_visible = firmware_is_visible, 455 }; 456 457 __ATTRIBUTE_GROUPS(ish_firmware); 458 459 static struct pci_driver ish_driver = { 460 .name = KBUILD_MODNAME, 461 .id_table = ish_pci_tbl, 462 .probe = ish_probe, 463 .remove = ish_remove, 464 .shutdown = ish_shutdown, 465 .driver.pm = &ish_pm_ops, 466 .dev_groups = ish_firmware_groups, 467 }; 468 469 module_pci_driver(ish_driver); 470 471 /* Original author */ 472 MODULE_AUTHOR("Daniel Drubin <daniel.drubin@intel.com>"); 473 /* Adoption to upstream Linux kernel */ 474 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>"); 475 476 MODULE_DESCRIPTION("Intel(R) Integrated Sensor Hub PCI Device Driver"); 477 MODULE_LICENSE("GPL"); 478 479 MODULE_FIRMWARE(ISH_FIRMWARE_PATH(ISH_FW_GEN_LNL_M)); 480 MODULE_FIRMWARE(ISH_FIRMWARE_PATH_ALL); 481