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> 165b063995STomas Winkler #include <linux/kthread.h> 17a98c30fdSTomas Winkler 18a98c30fdSTomas Winkler #include "mei_dev.h" 19a98c30fdSTomas Winkler #include "hw-me.h" 20a98c30fdSTomas Winkler #include "hw-me-regs.h" 21a98c30fdSTomas Winkler 22a98c30fdSTomas Winkler #include "mei-trace.h" 23a98c30fdSTomas Winkler 24a98c30fdSTomas Winkler #define MEI_GSC_RPM_TIMEOUT 500 25a98c30fdSTomas Winkler 26a98c30fdSTomas Winkler static int mei_gsc_read_hfs(const struct mei_device *dev, int where, u32 *val) 27a98c30fdSTomas Winkler { 28a98c30fdSTomas Winkler struct mei_me_hw *hw = to_me_hw(dev); 29a98c30fdSTomas Winkler 30a98c30fdSTomas Winkler *val = ioread32(hw->mem_addr + where + 0xC00); 31a98c30fdSTomas Winkler 32a98c30fdSTomas Winkler return 0; 33a98c30fdSTomas Winkler } 34a98c30fdSTomas Winkler 35*342e4c7eSTomas Winkler static void mei_gsc_set_ext_op_mem(const struct mei_me_hw *hw, struct resource *mem) 36*342e4c7eSTomas Winkler { 37*342e4c7eSTomas Winkler u32 low = lower_32_bits(mem->start); 38*342e4c7eSTomas Winkler u32 hi = upper_32_bits(mem->start); 39*342e4c7eSTomas Winkler u32 limit = (resource_size(mem) / SZ_4K) | GSC_EXT_OP_MEM_VALID; 40*342e4c7eSTomas Winkler 41*342e4c7eSTomas Winkler iowrite32(low, hw->mem_addr + H_GSC_EXT_OP_MEM_BASE_ADDR_LO_REG); 42*342e4c7eSTomas Winkler iowrite32(hi, hw->mem_addr + H_GSC_EXT_OP_MEM_BASE_ADDR_HI_REG); 43*342e4c7eSTomas Winkler iowrite32(limit, hw->mem_addr + H_GSC_EXT_OP_MEM_LIMIT_REG); 44*342e4c7eSTomas Winkler } 45*342e4c7eSTomas Winkler 46a98c30fdSTomas Winkler static int mei_gsc_probe(struct auxiliary_device *aux_dev, 47a98c30fdSTomas Winkler const struct auxiliary_device_id *aux_dev_id) 48a98c30fdSTomas Winkler { 49a98c30fdSTomas Winkler struct mei_aux_device *adev = auxiliary_dev_to_mei_aux_dev(aux_dev); 50a98c30fdSTomas Winkler struct mei_device *dev; 51a98c30fdSTomas Winkler struct mei_me_hw *hw; 52a98c30fdSTomas Winkler struct device *device; 53a98c30fdSTomas Winkler const struct mei_cfg *cfg; 54a98c30fdSTomas Winkler int ret; 55a98c30fdSTomas Winkler 56a98c30fdSTomas Winkler cfg = mei_me_get_cfg(aux_dev_id->driver_data); 57a98c30fdSTomas Winkler if (!cfg) 58a98c30fdSTomas Winkler return -ENODEV; 59a98c30fdSTomas Winkler 60a98c30fdSTomas Winkler device = &aux_dev->dev; 61a98c30fdSTomas Winkler 6295953618SAlexander Usyskin dev = mei_me_dev_init(device, cfg, adev->slow_firmware); 63a98c30fdSTomas Winkler if (!dev) { 64a98c30fdSTomas Winkler ret = -ENOMEM; 65a98c30fdSTomas Winkler goto err; 66a98c30fdSTomas Winkler } 67a98c30fdSTomas Winkler 68a98c30fdSTomas Winkler hw = to_me_hw(dev); 69a98c30fdSTomas Winkler hw->mem_addr = devm_ioremap_resource(device, &adev->bar); 70a98c30fdSTomas Winkler if (IS_ERR(hw->mem_addr)) { 71a98c30fdSTomas Winkler dev_err(device, "mmio not mapped\n"); 72a98c30fdSTomas Winkler ret = PTR_ERR(hw->mem_addr); 73a98c30fdSTomas Winkler goto err; 74a98c30fdSTomas Winkler } 75a98c30fdSTomas Winkler 76a98c30fdSTomas Winkler hw->irq = adev->irq; 77a98c30fdSTomas Winkler hw->read_fws = mei_gsc_read_hfs; 78a98c30fdSTomas Winkler 79a98c30fdSTomas Winkler dev_set_drvdata(device, dev); 80a98c30fdSTomas Winkler 81*342e4c7eSTomas Winkler if (adev->ext_op_mem.start) { 82*342e4c7eSTomas Winkler mei_gsc_set_ext_op_mem(hw, &adev->ext_op_mem); 83*342e4c7eSTomas Winkler dev->pxp_mode = MEI_DEV_PXP_INIT; 84*342e4c7eSTomas Winkler } 85*342e4c7eSTomas Winkler 865b063995STomas Winkler /* use polling */ 875b063995STomas Winkler if (mei_me_hw_use_polling(hw)) { 885b063995STomas Winkler mei_disable_interrupts(dev); 895b063995STomas Winkler mei_clear_interrupts(dev); 905b063995STomas Winkler init_waitqueue_head(&hw->wait_active); 915b063995STomas Winkler hw->is_active = true; /* start in active mode for initialization */ 925b063995STomas Winkler hw->polling_thread = kthread_run(mei_me_polling_thread, dev, 935b063995STomas Winkler "kmegscirqd/%s", dev_name(device)); 945b063995STomas Winkler if (IS_ERR(hw->polling_thread)) { 955b063995STomas Winkler ret = PTR_ERR(hw->polling_thread); 965b063995STomas Winkler dev_err(device, "unable to create kernel thread: %d\n", ret); 975b063995STomas Winkler goto err; 985b063995STomas Winkler } 995b063995STomas Winkler } else { 100a98c30fdSTomas Winkler ret = devm_request_threaded_irq(device, hw->irq, 101a98c30fdSTomas Winkler mei_me_irq_quick_handler, 102a98c30fdSTomas Winkler mei_me_irq_thread_handler, 103a98c30fdSTomas Winkler IRQF_ONESHOT, KBUILD_MODNAME, dev); 104a98c30fdSTomas Winkler if (ret) { 105a98c30fdSTomas Winkler dev_err(device, "irq register failed %d\n", ret); 106a98c30fdSTomas Winkler goto err; 107a98c30fdSTomas Winkler } 1085b063995STomas Winkler } 109a98c30fdSTomas Winkler 110a98c30fdSTomas Winkler pm_runtime_get_noresume(device); 111a98c30fdSTomas Winkler pm_runtime_set_active(device); 112a98c30fdSTomas Winkler pm_runtime_enable(device); 113a98c30fdSTomas Winkler 114ce97126dSAlexander Usyskin /* Continue to char device setup in spite of firmware handshake failure. 115ce97126dSAlexander Usyskin * In order to provide access to the firmware status registers to the user 116ce97126dSAlexander Usyskin * space via sysfs. 117ce97126dSAlexander Usyskin */ 118ce97126dSAlexander Usyskin if (mei_start(dev)) 119ce97126dSAlexander Usyskin dev_warn(device, "init hw failure.\n"); 120a98c30fdSTomas Winkler 121a98c30fdSTomas Winkler pm_runtime_set_autosuspend_delay(device, MEI_GSC_RPM_TIMEOUT); 122a98c30fdSTomas Winkler pm_runtime_use_autosuspend(device); 123a98c30fdSTomas Winkler 124a98c30fdSTomas Winkler ret = mei_register(dev, device); 125a98c30fdSTomas Winkler if (ret) 126a98c30fdSTomas Winkler goto register_err; 127a98c30fdSTomas Winkler 128a98c30fdSTomas Winkler pm_runtime_put_noidle(device); 129a98c30fdSTomas Winkler return 0; 130a98c30fdSTomas Winkler 131a98c30fdSTomas Winkler register_err: 132a98c30fdSTomas Winkler mei_stop(dev); 1335b063995STomas Winkler if (!mei_me_hw_use_polling(hw)) 134a98c30fdSTomas Winkler devm_free_irq(device, hw->irq, dev); 135a98c30fdSTomas Winkler 136a98c30fdSTomas Winkler err: 137a98c30fdSTomas Winkler dev_err(device, "probe failed: %d\n", ret); 138a98c30fdSTomas Winkler dev_set_drvdata(device, NULL); 139a98c30fdSTomas Winkler return ret; 140a98c30fdSTomas Winkler } 141a98c30fdSTomas Winkler 142a98c30fdSTomas Winkler static void mei_gsc_remove(struct auxiliary_device *aux_dev) 143a98c30fdSTomas Winkler { 144a98c30fdSTomas Winkler struct mei_device *dev; 145a98c30fdSTomas Winkler struct mei_me_hw *hw; 146a98c30fdSTomas Winkler 147a98c30fdSTomas Winkler dev = dev_get_drvdata(&aux_dev->dev); 148a98c30fdSTomas Winkler if (!dev) 149a98c30fdSTomas Winkler return; 150a98c30fdSTomas Winkler 151a98c30fdSTomas Winkler hw = to_me_hw(dev); 152a98c30fdSTomas Winkler 153a98c30fdSTomas Winkler mei_stop(dev); 154a98c30fdSTomas Winkler 1555b063995STomas Winkler hw = to_me_hw(dev); 1565b063995STomas Winkler if (mei_me_hw_use_polling(hw)) 1575b063995STomas Winkler kthread_stop(hw->polling_thread); 1585b063995STomas Winkler 159a98c30fdSTomas Winkler mei_deregister(dev); 160a98c30fdSTomas Winkler 161a98c30fdSTomas Winkler pm_runtime_disable(&aux_dev->dev); 162a98c30fdSTomas Winkler 163a98c30fdSTomas Winkler mei_disable_interrupts(dev); 1645b063995STomas Winkler if (!mei_me_hw_use_polling(hw)) 165a98c30fdSTomas Winkler devm_free_irq(&aux_dev->dev, hw->irq, dev); 166a98c30fdSTomas Winkler } 167a98c30fdSTomas Winkler 168a98c30fdSTomas Winkler static int __maybe_unused mei_gsc_pm_suspend(struct device *device) 169a98c30fdSTomas Winkler { 170a98c30fdSTomas Winkler struct mei_device *dev = dev_get_drvdata(device); 171a98c30fdSTomas Winkler 172a98c30fdSTomas Winkler if (!dev) 173a98c30fdSTomas Winkler return -ENODEV; 174a98c30fdSTomas Winkler 175a98c30fdSTomas Winkler mei_stop(dev); 176a98c30fdSTomas Winkler 177a98c30fdSTomas Winkler mei_disable_interrupts(dev); 178a98c30fdSTomas Winkler 179a98c30fdSTomas Winkler return 0; 180a98c30fdSTomas Winkler } 181a98c30fdSTomas Winkler 182a98c30fdSTomas Winkler static int __maybe_unused mei_gsc_pm_resume(struct device *device) 183a98c30fdSTomas Winkler { 184a98c30fdSTomas Winkler struct mei_device *dev = dev_get_drvdata(device); 185a98c30fdSTomas Winkler int err; 186a98c30fdSTomas Winkler 187a98c30fdSTomas Winkler if (!dev) 188a98c30fdSTomas Winkler return -ENODEV; 189a98c30fdSTomas Winkler 190a98c30fdSTomas Winkler err = mei_restart(dev); 191a98c30fdSTomas Winkler if (err) 192a98c30fdSTomas Winkler return err; 193a98c30fdSTomas Winkler 194a98c30fdSTomas Winkler /* Start timer if stopped in suspend */ 195a98c30fdSTomas Winkler schedule_delayed_work(&dev->timer_work, HZ); 196a98c30fdSTomas Winkler 197a98c30fdSTomas Winkler return 0; 198a98c30fdSTomas Winkler } 199a98c30fdSTomas Winkler 200ad10a354STomas Winkler static int __maybe_unused mei_gsc_pm_runtime_idle(struct device *device) 201ad10a354STomas Winkler { 202ad10a354STomas Winkler struct mei_device *dev = dev_get_drvdata(device); 203ad10a354STomas Winkler 204ad10a354STomas Winkler if (!dev) 205ad10a354STomas Winkler return -ENODEV; 206ad10a354STomas Winkler if (mei_write_is_idle(dev)) 207ad10a354STomas Winkler pm_runtime_autosuspend(device); 208ad10a354STomas Winkler 209ad10a354STomas Winkler return -EBUSY; 210ad10a354STomas Winkler } 211ad10a354STomas Winkler 212ad10a354STomas Winkler static int __maybe_unused mei_gsc_pm_runtime_suspend(struct device *device) 213ad10a354STomas Winkler { 214ad10a354STomas Winkler struct mei_device *dev = dev_get_drvdata(device); 215ad10a354STomas Winkler struct mei_me_hw *hw; 216ad10a354STomas Winkler int ret; 217ad10a354STomas Winkler 218ad10a354STomas Winkler if (!dev) 219ad10a354STomas Winkler return -ENODEV; 220ad10a354STomas Winkler 221ad10a354STomas Winkler mutex_lock(&dev->device_lock); 222ad10a354STomas Winkler 223ad10a354STomas Winkler if (mei_write_is_idle(dev)) { 224ad10a354STomas Winkler hw = to_me_hw(dev); 225ad10a354STomas Winkler hw->pg_state = MEI_PG_ON; 2265b063995STomas Winkler 2275b063995STomas Winkler if (mei_me_hw_use_polling(hw)) 2285b063995STomas Winkler hw->is_active = false; 229ad10a354STomas Winkler ret = 0; 230ad10a354STomas Winkler } else { 231ad10a354STomas Winkler ret = -EAGAIN; 232ad10a354STomas Winkler } 233ad10a354STomas Winkler 234ad10a354STomas Winkler mutex_unlock(&dev->device_lock); 235ad10a354STomas Winkler 236ad10a354STomas Winkler return ret; 237ad10a354STomas Winkler } 238ad10a354STomas Winkler 239ad10a354STomas Winkler static int __maybe_unused mei_gsc_pm_runtime_resume(struct device *device) 240ad10a354STomas Winkler { 241ad10a354STomas Winkler struct mei_device *dev = dev_get_drvdata(device); 242ad10a354STomas Winkler struct mei_me_hw *hw; 243ad10a354STomas Winkler irqreturn_t irq_ret; 244ad10a354STomas Winkler 245ad10a354STomas Winkler if (!dev) 246ad10a354STomas Winkler return -ENODEV; 247ad10a354STomas Winkler 248ad10a354STomas Winkler mutex_lock(&dev->device_lock); 249ad10a354STomas Winkler 250ad10a354STomas Winkler hw = to_me_hw(dev); 251ad10a354STomas Winkler hw->pg_state = MEI_PG_OFF; 252ad10a354STomas Winkler 2535b063995STomas Winkler if (mei_me_hw_use_polling(hw)) { 2545b063995STomas Winkler hw->is_active = true; 2555b063995STomas Winkler wake_up(&hw->wait_active); 2565b063995STomas Winkler } 2575b063995STomas Winkler 258ad10a354STomas Winkler mutex_unlock(&dev->device_lock); 259ad10a354STomas Winkler 260ad10a354STomas Winkler irq_ret = mei_me_irq_thread_handler(1, dev); 261ad10a354STomas Winkler if (irq_ret != IRQ_HANDLED) 262ad10a354STomas Winkler dev_err(dev->dev, "thread handler fail %d\n", irq_ret); 263ad10a354STomas Winkler 264ad10a354STomas Winkler return 0; 265ad10a354STomas Winkler } 266ad10a354STomas Winkler 267ad10a354STomas Winkler static const struct dev_pm_ops mei_gsc_pm_ops = { 268ad10a354STomas Winkler SET_SYSTEM_SLEEP_PM_OPS(mei_gsc_pm_suspend, 269ad10a354STomas Winkler mei_gsc_pm_resume) 270ad10a354STomas Winkler SET_RUNTIME_PM_OPS(mei_gsc_pm_runtime_suspend, 271ad10a354STomas Winkler mei_gsc_pm_runtime_resume, 272ad10a354STomas Winkler mei_gsc_pm_runtime_idle) 273ad10a354STomas Winkler }; 274a98c30fdSTomas Winkler 275a98c30fdSTomas Winkler static const struct auxiliary_device_id mei_gsc_id_table[] = { 276a98c30fdSTomas Winkler { 277a98c30fdSTomas Winkler .name = "i915.mei-gsc", 278a98c30fdSTomas Winkler .driver_data = MEI_ME_GSC_CFG, 279a98c30fdSTomas Winkler 280a98c30fdSTomas Winkler }, 281a98c30fdSTomas Winkler { 282a98c30fdSTomas Winkler .name = "i915.mei-gscfi", 283a98c30fdSTomas Winkler .driver_data = MEI_ME_GSCFI_CFG, 284a98c30fdSTomas Winkler }, 285a98c30fdSTomas Winkler { 286a98c30fdSTomas Winkler /* sentinel */ 287a98c30fdSTomas Winkler } 288a98c30fdSTomas Winkler }; 289a98c30fdSTomas Winkler MODULE_DEVICE_TABLE(auxiliary, mei_gsc_id_table); 290a98c30fdSTomas Winkler 291a98c30fdSTomas Winkler static struct auxiliary_driver mei_gsc_driver = { 292a98c30fdSTomas Winkler .probe = mei_gsc_probe, 293a98c30fdSTomas Winkler .remove = mei_gsc_remove, 294a98c30fdSTomas Winkler .driver = { 295a98c30fdSTomas Winkler /* auxiliary_driver_register() sets .name to be the modname */ 296a98c30fdSTomas Winkler .pm = &mei_gsc_pm_ops, 297a98c30fdSTomas Winkler }, 298a98c30fdSTomas Winkler .id_table = mei_gsc_id_table 299a98c30fdSTomas Winkler }; 300a98c30fdSTomas Winkler module_auxiliary_driver(mei_gsc_driver); 301a98c30fdSTomas Winkler 302a98c30fdSTomas Winkler MODULE_AUTHOR("Intel Corporation"); 303a98c30fdSTomas Winkler MODULE_ALIAS("auxiliary:i915.mei-gsc"); 304a98c30fdSTomas Winkler MODULE_ALIAS("auxiliary:i915.mei-gscfi"); 305a98c30fdSTomas Winkler MODULE_LICENSE("GPL"); 306