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