1a98c30fdSTomas Winkler // SPDX-License-Identifier: GPL-2.0 2a98c30fdSTomas Winkler /* 3a98c30fdSTomas Winkler * Copyright(c) 2019-2022, Intel Corporation. All rights reserved. 4a98c30fdSTomas Winkler * 5a98c30fdSTomas Winkler * Intel Management Engine Interface (Intel MEI) Linux driver 6a98c30fdSTomas Winkler */ 7a98c30fdSTomas Winkler 8a98c30fdSTomas Winkler #include <linux/module.h> 9a98c30fdSTomas Winkler #include <linux/mei_aux.h> 10a98c30fdSTomas Winkler #include <linux/device.h> 11a98c30fdSTomas Winkler #include <linux/irqreturn.h> 12a98c30fdSTomas Winkler #include <linux/jiffies.h> 13a98c30fdSTomas Winkler #include <linux/ktime.h> 14a98c30fdSTomas Winkler #include <linux/delay.h> 15a98c30fdSTomas Winkler #include <linux/pm_runtime.h> 16a98c30fdSTomas Winkler 17a98c30fdSTomas Winkler #include "mei_dev.h" 18a98c30fdSTomas Winkler #include "hw-me.h" 19a98c30fdSTomas Winkler #include "hw-me-regs.h" 20a98c30fdSTomas Winkler 21a98c30fdSTomas Winkler #include "mei-trace.h" 22a98c30fdSTomas Winkler 23a98c30fdSTomas Winkler #define MEI_GSC_RPM_TIMEOUT 500 24a98c30fdSTomas Winkler 25a98c30fdSTomas Winkler static int mei_gsc_read_hfs(const struct mei_device *dev, int where, u32 *val) 26a98c30fdSTomas Winkler { 27a98c30fdSTomas Winkler struct mei_me_hw *hw = to_me_hw(dev); 28a98c30fdSTomas Winkler 29a98c30fdSTomas Winkler *val = ioread32(hw->mem_addr + where + 0xC00); 30a98c30fdSTomas Winkler 31a98c30fdSTomas Winkler return 0; 32a98c30fdSTomas Winkler } 33a98c30fdSTomas Winkler 34a98c30fdSTomas Winkler static int mei_gsc_probe(struct auxiliary_device *aux_dev, 35a98c30fdSTomas Winkler const struct auxiliary_device_id *aux_dev_id) 36a98c30fdSTomas Winkler { 37a98c30fdSTomas Winkler struct mei_aux_device *adev = auxiliary_dev_to_mei_aux_dev(aux_dev); 38a98c30fdSTomas Winkler struct mei_device *dev; 39a98c30fdSTomas Winkler struct mei_me_hw *hw; 40a98c30fdSTomas Winkler struct device *device; 41a98c30fdSTomas Winkler const struct mei_cfg *cfg; 42a98c30fdSTomas Winkler int ret; 43a98c30fdSTomas Winkler 44a98c30fdSTomas Winkler cfg = mei_me_get_cfg(aux_dev_id->driver_data); 45a98c30fdSTomas Winkler if (!cfg) 46a98c30fdSTomas Winkler return -ENODEV; 47a98c30fdSTomas Winkler 48a98c30fdSTomas Winkler device = &aux_dev->dev; 49a98c30fdSTomas Winkler 50a98c30fdSTomas Winkler dev = mei_me_dev_init(device, cfg); 51a98c30fdSTomas Winkler if (!dev) { 52a98c30fdSTomas Winkler ret = -ENOMEM; 53a98c30fdSTomas Winkler goto err; 54a98c30fdSTomas Winkler } 55a98c30fdSTomas Winkler 56a98c30fdSTomas Winkler hw = to_me_hw(dev); 57a98c30fdSTomas Winkler hw->mem_addr = devm_ioremap_resource(device, &adev->bar); 58a98c30fdSTomas Winkler if (IS_ERR(hw->mem_addr)) { 59a98c30fdSTomas Winkler dev_err(device, "mmio not mapped\n"); 60a98c30fdSTomas Winkler ret = PTR_ERR(hw->mem_addr); 61a98c30fdSTomas Winkler goto err; 62a98c30fdSTomas Winkler } 63a98c30fdSTomas Winkler 64a98c30fdSTomas Winkler hw->irq = adev->irq; 65a98c30fdSTomas Winkler hw->read_fws = mei_gsc_read_hfs; 66a98c30fdSTomas Winkler 67a98c30fdSTomas Winkler dev_set_drvdata(device, dev); 68a98c30fdSTomas Winkler 69a98c30fdSTomas Winkler ret = devm_request_threaded_irq(device, hw->irq, 70a98c30fdSTomas Winkler mei_me_irq_quick_handler, 71a98c30fdSTomas Winkler mei_me_irq_thread_handler, 72a98c30fdSTomas Winkler IRQF_ONESHOT, KBUILD_MODNAME, dev); 73a98c30fdSTomas Winkler if (ret) { 74a98c30fdSTomas Winkler dev_err(device, "irq register failed %d\n", ret); 75a98c30fdSTomas Winkler goto err; 76a98c30fdSTomas Winkler } 77a98c30fdSTomas Winkler 78a98c30fdSTomas Winkler pm_runtime_get_noresume(device); 79a98c30fdSTomas Winkler pm_runtime_set_active(device); 80a98c30fdSTomas Winkler pm_runtime_enable(device); 81a98c30fdSTomas Winkler 82*ce97126dSAlexander Usyskin /* Continue to char device setup in spite of firmware handshake failure. 83*ce97126dSAlexander Usyskin * In order to provide access to the firmware status registers to the user 84*ce97126dSAlexander Usyskin * space via sysfs. 85*ce97126dSAlexander Usyskin */ 86*ce97126dSAlexander Usyskin if (mei_start(dev)) 87*ce97126dSAlexander Usyskin dev_warn(device, "init hw failure.\n"); 88a98c30fdSTomas Winkler 89a98c30fdSTomas Winkler pm_runtime_set_autosuspend_delay(device, MEI_GSC_RPM_TIMEOUT); 90a98c30fdSTomas Winkler pm_runtime_use_autosuspend(device); 91a98c30fdSTomas Winkler 92a98c30fdSTomas Winkler ret = mei_register(dev, device); 93a98c30fdSTomas Winkler if (ret) 94a98c30fdSTomas Winkler goto register_err; 95a98c30fdSTomas Winkler 96a98c30fdSTomas Winkler pm_runtime_put_noidle(device); 97a98c30fdSTomas Winkler return 0; 98a98c30fdSTomas Winkler 99a98c30fdSTomas Winkler register_err: 100a98c30fdSTomas Winkler mei_stop(dev); 101a98c30fdSTomas Winkler devm_free_irq(device, hw->irq, dev); 102a98c30fdSTomas Winkler 103a98c30fdSTomas Winkler err: 104a98c30fdSTomas Winkler dev_err(device, "probe failed: %d\n", ret); 105a98c30fdSTomas Winkler dev_set_drvdata(device, NULL); 106a98c30fdSTomas Winkler return ret; 107a98c30fdSTomas Winkler } 108a98c30fdSTomas Winkler 109a98c30fdSTomas Winkler static void mei_gsc_remove(struct auxiliary_device *aux_dev) 110a98c30fdSTomas Winkler { 111a98c30fdSTomas Winkler struct mei_device *dev; 112a98c30fdSTomas Winkler struct mei_me_hw *hw; 113a98c30fdSTomas Winkler 114a98c30fdSTomas Winkler dev = dev_get_drvdata(&aux_dev->dev); 115a98c30fdSTomas Winkler if (!dev) 116a98c30fdSTomas Winkler return; 117a98c30fdSTomas Winkler 118a98c30fdSTomas Winkler hw = to_me_hw(dev); 119a98c30fdSTomas Winkler 120a98c30fdSTomas Winkler mei_stop(dev); 121a98c30fdSTomas Winkler 122a98c30fdSTomas Winkler mei_deregister(dev); 123a98c30fdSTomas Winkler 124a98c30fdSTomas Winkler pm_runtime_disable(&aux_dev->dev); 125a98c30fdSTomas Winkler 126a98c30fdSTomas Winkler mei_disable_interrupts(dev); 127a98c30fdSTomas Winkler devm_free_irq(&aux_dev->dev, hw->irq, dev); 128a98c30fdSTomas Winkler } 129a98c30fdSTomas Winkler 130a98c30fdSTomas Winkler static int __maybe_unused mei_gsc_pm_suspend(struct device *device) 131a98c30fdSTomas Winkler { 132a98c30fdSTomas Winkler struct mei_device *dev = dev_get_drvdata(device); 133a98c30fdSTomas Winkler 134a98c30fdSTomas Winkler if (!dev) 135a98c30fdSTomas Winkler return -ENODEV; 136a98c30fdSTomas Winkler 137a98c30fdSTomas Winkler mei_stop(dev); 138a98c30fdSTomas Winkler 139a98c30fdSTomas Winkler mei_disable_interrupts(dev); 140a98c30fdSTomas Winkler 141a98c30fdSTomas Winkler return 0; 142a98c30fdSTomas Winkler } 143a98c30fdSTomas Winkler 144a98c30fdSTomas Winkler static int __maybe_unused mei_gsc_pm_resume(struct device *device) 145a98c30fdSTomas Winkler { 146a98c30fdSTomas Winkler struct mei_device *dev = dev_get_drvdata(device); 147a98c30fdSTomas Winkler int err; 148a98c30fdSTomas Winkler 149a98c30fdSTomas Winkler if (!dev) 150a98c30fdSTomas Winkler return -ENODEV; 151a98c30fdSTomas Winkler 152a98c30fdSTomas Winkler err = mei_restart(dev); 153a98c30fdSTomas Winkler if (err) 154a98c30fdSTomas Winkler return err; 155a98c30fdSTomas Winkler 156a98c30fdSTomas Winkler /* Start timer if stopped in suspend */ 157a98c30fdSTomas Winkler schedule_delayed_work(&dev->timer_work, HZ); 158a98c30fdSTomas Winkler 159a98c30fdSTomas Winkler return 0; 160a98c30fdSTomas Winkler } 161a98c30fdSTomas Winkler 162a98c30fdSTomas Winkler static SIMPLE_DEV_PM_OPS(mei_gsc_pm_ops, mei_gsc_pm_suspend, mei_gsc_pm_resume); 163a98c30fdSTomas Winkler 164a98c30fdSTomas Winkler static const struct auxiliary_device_id mei_gsc_id_table[] = { 165a98c30fdSTomas Winkler { 166a98c30fdSTomas Winkler .name = "i915.mei-gsc", 167a98c30fdSTomas Winkler .driver_data = MEI_ME_GSC_CFG, 168a98c30fdSTomas Winkler 169a98c30fdSTomas Winkler }, 170a98c30fdSTomas Winkler { 171a98c30fdSTomas Winkler .name = "i915.mei-gscfi", 172a98c30fdSTomas Winkler .driver_data = MEI_ME_GSCFI_CFG, 173a98c30fdSTomas Winkler }, 174a98c30fdSTomas Winkler { 175a98c30fdSTomas Winkler /* sentinel */ 176a98c30fdSTomas Winkler } 177a98c30fdSTomas Winkler }; 178a98c30fdSTomas Winkler MODULE_DEVICE_TABLE(auxiliary, mei_gsc_id_table); 179a98c30fdSTomas Winkler 180a98c30fdSTomas Winkler static struct auxiliary_driver mei_gsc_driver = { 181a98c30fdSTomas Winkler .probe = mei_gsc_probe, 182a98c30fdSTomas Winkler .remove = mei_gsc_remove, 183a98c30fdSTomas Winkler .driver = { 184a98c30fdSTomas Winkler /* auxiliary_driver_register() sets .name to be the modname */ 185a98c30fdSTomas Winkler .pm = &mei_gsc_pm_ops, 186a98c30fdSTomas Winkler }, 187a98c30fdSTomas Winkler .id_table = mei_gsc_id_table 188a98c30fdSTomas Winkler }; 189a98c30fdSTomas Winkler module_auxiliary_driver(mei_gsc_driver); 190a98c30fdSTomas Winkler 191a98c30fdSTomas Winkler MODULE_AUTHOR("Intel Corporation"); 192a98c30fdSTomas Winkler MODULE_ALIAS("auxiliary:i915.mei-gsc"); 193a98c30fdSTomas Winkler MODULE_ALIAS("auxiliary:i915.mei-gscfi"); 194a98c30fdSTomas Winkler MODULE_LICENSE("GPL"); 195