mei: me: add gemini lake devices id
[linux-2.6/btrfs-unstable.git] / drivers / misc / mei / pci-me.c
blob78b3172c8e6e298fa8f74c93db2850c4f96b95a8
1 /*
3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2012, Intel Corporation.
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.
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.
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/fcntl.h>
24 #include <linux/pci.h>
25 #include <linux/poll.h>
26 #include <linux/ioctl.h>
27 #include <linux/cdev.h>
28 #include <linux/sched.h>
29 #include <linux/uuid.h>
30 #include <linux/compat.h>
31 #include <linux/jiffies.h>
32 #include <linux/interrupt.h>
34 #include <linux/pm_domain.h>
35 #include <linux/pm_runtime.h>
37 #include <linux/mei.h>
39 #include "mei_dev.h"
40 #include "client.h"
41 #include "hw-me-regs.h"
42 #include "hw-me.h"
44 /* mei_pci_tbl - PCI Device ID Table */
45 static const struct pci_device_id mei_me_pci_tbl[] = {
46 {MEI_PCI_DEVICE(MEI_DEV_ID_82946GZ, MEI_ME_ICH_CFG)},
47 {MEI_PCI_DEVICE(MEI_DEV_ID_82G35, MEI_ME_ICH_CFG)},
48 {MEI_PCI_DEVICE(MEI_DEV_ID_82Q965, MEI_ME_ICH_CFG)},
49 {MEI_PCI_DEVICE(MEI_DEV_ID_82G965, MEI_ME_ICH_CFG)},
50 {MEI_PCI_DEVICE(MEI_DEV_ID_82GM965, MEI_ME_ICH_CFG)},
51 {MEI_PCI_DEVICE(MEI_DEV_ID_82GME965, MEI_ME_ICH_CFG)},
52 {MEI_P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ID_GLK, MEI_ME_PCH8_CFG)},
98 {MEI_PCI_DEVICE(MEI_DEV_ID_KBP, MEI_ME_PCH8_CFG)},
99 {MEI_PCI_DEVICE(MEI_DEV_ID_KBP_2, MEI_ME_PCH8_CFG)},
101 /* required last entry */
102 {0, }
105 MODULE_DEVICE_TABLE(pci, mei_me_pci_tbl);
107 #ifdef CONFIG_PM
108 static inline void mei_me_set_pm_domain(struct mei_device *dev);
109 static inline void mei_me_unset_pm_domain(struct mei_device *dev);
110 #else
111 static inline void mei_me_set_pm_domain(struct mei_device *dev) {}
112 static inline void mei_me_unset_pm_domain(struct mei_device *dev) {}
113 #endif /* CONFIG_PM */
116 * mei_me_quirk_probe - probe for devices that doesn't valid ME interface
118 * @pdev: PCI device structure
119 * @cfg: per generation config
121 * Return: true if ME Interface is valid, false otherwise
123 static bool mei_me_quirk_probe(struct pci_dev *pdev,
124 const struct mei_cfg *cfg)
126 if (cfg->quirk_probe && cfg->quirk_probe(pdev)) {
127 dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n");
128 return false;
131 return true;
135 * mei_me_probe - Device Initialization Routine
137 * @pdev: PCI device structure
138 * @ent: entry in kcs_pci_tbl
140 * Return: 0 on success, <0 on failure.
142 static int mei_me_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
144 const struct mei_cfg *cfg;
145 struct mei_device *dev;
146 struct mei_me_hw *hw;
147 unsigned int irqflags;
148 int err;
150 cfg = mei_me_get_cfg(ent->driver_data);
151 if (!cfg)
152 return -ENODEV;
154 if (!mei_me_quirk_probe(pdev, cfg))
155 return -ENODEV;
157 /* enable pci dev */
158 err = pcim_enable_device(pdev);
159 if (err) {
160 dev_err(&pdev->dev, "failed to enable pci device.\n");
161 goto end;
163 /* set PCI host mastering */
164 pci_set_master(pdev);
165 /* pci request regions and mapping IO device memory for mei driver */
166 err = pcim_iomap_regions(pdev, BIT(0), KBUILD_MODNAME);
167 if (err) {
168 dev_err(&pdev->dev, "failed to get pci regions.\n");
169 goto end;
172 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)) ||
173 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64))) {
175 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
176 if (err)
177 err = dma_set_coherent_mask(&pdev->dev,
178 DMA_BIT_MASK(32));
180 if (err) {
181 dev_err(&pdev->dev, "No usable DMA configuration, aborting\n");
182 goto end;
185 /* allocates and initializes the mei dev structure */
186 dev = mei_me_dev_init(pdev, cfg);
187 if (!dev) {
188 err = -ENOMEM;
189 goto end;
191 hw = to_me_hw(dev);
192 hw->mem_addr = pcim_iomap_table(pdev)[0];
194 pci_enable_msi(pdev);
196 /* request and enable interrupt */
197 irqflags = pci_dev_msi_enabled(pdev) ? IRQF_ONESHOT : IRQF_SHARED;
199 err = request_threaded_irq(pdev->irq,
200 mei_me_irq_quick_handler,
201 mei_me_irq_thread_handler,
202 irqflags, KBUILD_MODNAME, dev);
203 if (err) {
204 dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n",
205 pdev->irq);
206 goto end;
209 if (mei_start(dev)) {
210 dev_err(&pdev->dev, "init hw failure.\n");
211 err = -ENODEV;
212 goto release_irq;
215 pm_runtime_set_autosuspend_delay(&pdev->dev, MEI_ME_RPM_TIMEOUT);
216 pm_runtime_use_autosuspend(&pdev->dev);
218 err = mei_register(dev, &pdev->dev);
219 if (err)
220 goto stop;
222 pci_set_drvdata(pdev, dev);
225 * MEI requires to resume from runtime suspend mode
226 * in order to perform link reset flow upon system suspend.
228 pdev->dev_flags |= PCI_DEV_FLAGS_NEEDS_RESUME;
231 * ME maps runtime suspend/resume to D0i states,
232 * hence we need to go around native PCI runtime service which
233 * eventually brings the device into D3cold/hot state,
234 * but the mei device cannot wake up from D3 unlike from D0i3.
235 * To get around the PCI device native runtime pm,
236 * ME uses runtime pm domain handlers which take precedence
237 * over the driver's pm handlers.
239 mei_me_set_pm_domain(dev);
241 if (mei_pg_is_enabled(dev))
242 pm_runtime_put_noidle(&pdev->dev);
244 dev_dbg(&pdev->dev, "initialization successful.\n");
246 return 0;
248 stop:
249 mei_stop(dev);
250 release_irq:
251 mei_cancel_work(dev);
252 mei_disable_interrupts(dev);
253 free_irq(pdev->irq, dev);
254 end:
255 dev_err(&pdev->dev, "initialization failed.\n");
256 return err;
260 * mei_me_shutdown - Device Removal Routine
262 * @pdev: PCI device structure
264 * mei_me_shutdown is called from the reboot notifier
265 * it's a simplified version of remove so we go down
266 * faster.
268 static void mei_me_shutdown(struct pci_dev *pdev)
270 struct mei_device *dev;
272 dev = pci_get_drvdata(pdev);
273 if (!dev)
274 return;
276 dev_dbg(&pdev->dev, "shutdown\n");
277 mei_stop(dev);
279 mei_me_unset_pm_domain(dev);
281 mei_disable_interrupts(dev);
282 free_irq(pdev->irq, dev);
286 * mei_me_remove - Device Removal Routine
288 * @pdev: PCI device structure
290 * mei_me_remove is called by the PCI subsystem to alert the driver
291 * that it should release a PCI device.
293 static void mei_me_remove(struct pci_dev *pdev)
295 struct mei_device *dev;
297 dev = pci_get_drvdata(pdev);
298 if (!dev)
299 return;
301 if (mei_pg_is_enabled(dev))
302 pm_runtime_get_noresume(&pdev->dev);
304 dev_dbg(&pdev->dev, "stop\n");
305 mei_stop(dev);
307 mei_me_unset_pm_domain(dev);
309 mei_disable_interrupts(dev);
311 free_irq(pdev->irq, dev);
313 mei_deregister(dev);
316 #ifdef CONFIG_PM_SLEEP
317 static int mei_me_pci_suspend(struct device *device)
319 struct pci_dev *pdev = to_pci_dev(device);
320 struct mei_device *dev = pci_get_drvdata(pdev);
322 if (!dev)
323 return -ENODEV;
325 dev_dbg(&pdev->dev, "suspend\n");
327 mei_stop(dev);
329 mei_disable_interrupts(dev);
331 free_irq(pdev->irq, dev);
332 pci_disable_msi(pdev);
334 return 0;
337 static int mei_me_pci_resume(struct device *device)
339 struct pci_dev *pdev = to_pci_dev(device);
340 struct mei_device *dev;
341 unsigned int irqflags;
342 int err;
344 dev = pci_get_drvdata(pdev);
345 if (!dev)
346 return -ENODEV;
348 pci_enable_msi(pdev);
350 irqflags = pci_dev_msi_enabled(pdev) ? IRQF_ONESHOT : IRQF_SHARED;
352 /* request and enable interrupt */
353 err = request_threaded_irq(pdev->irq,
354 mei_me_irq_quick_handler,
355 mei_me_irq_thread_handler,
356 irqflags, KBUILD_MODNAME, dev);
358 if (err) {
359 dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
360 pdev->irq);
361 return err;
364 err = mei_restart(dev);
365 if (err)
366 return err;
368 /* Start timer if stopped in suspend */
369 schedule_delayed_work(&dev->timer_work, HZ);
371 return 0;
373 #endif /* CONFIG_PM_SLEEP */
375 #ifdef CONFIG_PM
376 static int mei_me_pm_runtime_idle(struct device *device)
378 struct pci_dev *pdev = to_pci_dev(device);
379 struct mei_device *dev;
381 dev_dbg(&pdev->dev, "rpm: me: runtime_idle\n");
383 dev = pci_get_drvdata(pdev);
384 if (!dev)
385 return -ENODEV;
386 if (mei_write_is_idle(dev))
387 pm_runtime_autosuspend(device);
389 return -EBUSY;
392 static int mei_me_pm_runtime_suspend(struct device *device)
394 struct pci_dev *pdev = to_pci_dev(device);
395 struct mei_device *dev;
396 int ret;
398 dev_dbg(&pdev->dev, "rpm: me: runtime suspend\n");
400 dev = pci_get_drvdata(pdev);
401 if (!dev)
402 return -ENODEV;
404 mutex_lock(&dev->device_lock);
406 if (mei_write_is_idle(dev))
407 ret = mei_me_pg_enter_sync(dev);
408 else
409 ret = -EAGAIN;
411 mutex_unlock(&dev->device_lock);
413 dev_dbg(&pdev->dev, "rpm: me: runtime suspend ret=%d\n", ret);
415 if (ret && ret != -EAGAIN)
416 schedule_work(&dev->reset_work);
418 return ret;
421 static int mei_me_pm_runtime_resume(struct device *device)
423 struct pci_dev *pdev = to_pci_dev(device);
424 struct mei_device *dev;
425 int ret;
427 dev_dbg(&pdev->dev, "rpm: me: runtime resume\n");
429 dev = pci_get_drvdata(pdev);
430 if (!dev)
431 return -ENODEV;
433 mutex_lock(&dev->device_lock);
435 ret = mei_me_pg_exit_sync(dev);
437 mutex_unlock(&dev->device_lock);
439 dev_dbg(&pdev->dev, "rpm: me: runtime resume ret = %d\n", ret);
441 if (ret)
442 schedule_work(&dev->reset_work);
444 return ret;
448 * mei_me_set_pm_domain - fill and set pm domain structure for device
450 * @dev: mei_device
452 static inline void mei_me_set_pm_domain(struct mei_device *dev)
454 struct pci_dev *pdev = to_pci_dev(dev->dev);
456 if (pdev->dev.bus && pdev->dev.bus->pm) {
457 dev->pg_domain.ops = *pdev->dev.bus->pm;
459 dev->pg_domain.ops.runtime_suspend = mei_me_pm_runtime_suspend;
460 dev->pg_domain.ops.runtime_resume = mei_me_pm_runtime_resume;
461 dev->pg_domain.ops.runtime_idle = mei_me_pm_runtime_idle;
463 dev_pm_domain_set(&pdev->dev, &dev->pg_domain);
468 * mei_me_unset_pm_domain - clean pm domain structure for device
470 * @dev: mei_device
472 static inline void mei_me_unset_pm_domain(struct mei_device *dev)
474 /* stop using pm callbacks if any */
475 dev_pm_domain_set(dev->dev, NULL);
478 static const struct dev_pm_ops mei_me_pm_ops = {
479 SET_SYSTEM_SLEEP_PM_OPS(mei_me_pci_suspend,
480 mei_me_pci_resume)
481 SET_RUNTIME_PM_OPS(
482 mei_me_pm_runtime_suspend,
483 mei_me_pm_runtime_resume,
484 mei_me_pm_runtime_idle)
487 #define MEI_ME_PM_OPS (&mei_me_pm_ops)
488 #else
489 #define MEI_ME_PM_OPS NULL
490 #endif /* CONFIG_PM */
492 * PCI driver structure
494 static struct pci_driver mei_me_driver = {
495 .name = KBUILD_MODNAME,
496 .id_table = mei_me_pci_tbl,
497 .probe = mei_me_probe,
498 .remove = mei_me_remove,
499 .shutdown = mei_me_shutdown,
500 .driver.pm = MEI_ME_PM_OPS,
501 .driver.probe_type = PROBE_PREFER_ASYNCHRONOUS,
504 module_pci_driver(mei_me_driver);
506 MODULE_AUTHOR("Intel Corporation");
507 MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
508 MODULE_LICENSE("GPL v2");