pci.c
来自「linux 内核源代码」· C语言 代码 · 共 1,653 行 · 第 1/3 页
C
1,653 行
struct pci_cap_saved_state *save_state; u16 *cap; pos = pci_find_capability(dev, PCI_CAP_ID_EXP); if (pos <= 0) return 0; save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP); if (!save_state) save_state = kzalloc(sizeof(*save_state) + sizeof(u16) * 4, GFP_KERNEL); if (!save_state) { dev_err(&dev->dev, "Out of memory in pci_save_pcie_state\n"); return -ENOMEM; } cap = (u16 *)&save_state->data[0]; pci_read_config_word(dev, pos + PCI_EXP_DEVCTL, &cap[i++]); pci_read_config_word(dev, pos + PCI_EXP_LNKCTL, &cap[i++]); pci_read_config_word(dev, pos + PCI_EXP_SLTCTL, &cap[i++]); pci_read_config_word(dev, pos + PCI_EXP_RTCTL, &cap[i++]); pci_add_saved_cap(dev, save_state); return 0;}static void pci_restore_pcie_state(struct pci_dev *dev){ int i = 0, pos; struct pci_cap_saved_state *save_state; u16 *cap; save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP); pos = pci_find_capability(dev, PCI_CAP_ID_EXP); if (!save_state || pos <= 0) return; cap = (u16 *)&save_state->data[0]; pci_write_config_word(dev, pos + PCI_EXP_DEVCTL, cap[i++]); pci_write_config_word(dev, pos + PCI_EXP_LNKCTL, cap[i++]); pci_write_config_word(dev, pos + PCI_EXP_SLTCTL, cap[i++]); pci_write_config_word(dev, pos + PCI_EXP_RTCTL, cap[i++]);}static int pci_save_pcix_state(struct pci_dev *dev){ int pos, i = 0; struct pci_cap_saved_state *save_state; u16 *cap; pos = pci_find_capability(dev, PCI_CAP_ID_PCIX); if (pos <= 0) return 0; save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP); if (!save_state) save_state = kzalloc(sizeof(*save_state) + sizeof(u16), GFP_KERNEL); if (!save_state) { dev_err(&dev->dev, "Out of memory in pci_save_pcie_state\n"); return -ENOMEM; } cap = (u16 *)&save_state->data[0]; pci_read_config_word(dev, pos + PCI_X_CMD, &cap[i++]); pci_add_saved_cap(dev, save_state); return 0;}static void pci_restore_pcix_state(struct pci_dev *dev){ int i = 0, pos; struct pci_cap_saved_state *save_state; u16 *cap; save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX); pos = pci_find_capability(dev, PCI_CAP_ID_PCIX); if (!save_state || pos <= 0) return; cap = (u16 *)&save_state->data[0]; pci_write_config_word(dev, pos + PCI_X_CMD, cap[i++]);}/** * pci_save_state - save the PCI configuration space of a device before suspending * @dev: - PCI device that we're dealing with */intpci_save_state(struct pci_dev *dev){ int i; /* XXX: 100% dword access ok here? */ for (i = 0; i < 16; i++) pci_read_config_dword(dev, i * 4,&dev->saved_config_space[i]); if ((i = pci_save_pcie_state(dev)) != 0) return i; if ((i = pci_save_pcix_state(dev)) != 0) return i; return 0;}/** * pci_restore_state - Restore the saved state of a PCI device * @dev: - PCI device that we're dealing with */int pci_restore_state(struct pci_dev *dev){ int i; u32 val; /* PCI Express register must be restored first */ pci_restore_pcie_state(dev); /* * The Base Address register should be programmed before the command * register(s) */ for (i = 15; i >= 0; i--) { pci_read_config_dword(dev, i * 4, &val); if (val != dev->saved_config_space[i]) { printk(KERN_DEBUG "PM: Writing back config space on " "device %s at offset %x (was %x, writing %x)\n", pci_name(dev), i, val, (int)dev->saved_config_space[i]); pci_write_config_dword(dev,i * 4, dev->saved_config_space[i]); } } pci_restore_pcix_state(dev); pci_restore_msi_state(dev); return 0;}static int do_pci_enable_device(struct pci_dev *dev, int bars){ int err; err = pci_set_power_state(dev, PCI_D0); if (err < 0 && err != -EIO) return err; err = pcibios_enable_device(dev, bars); if (err < 0) return err; pci_fixup_device(pci_fixup_enable, dev); return 0;}/** * pci_reenable_device - Resume abandoned device * @dev: PCI device to be resumed * * Note this function is a backend of pci_default_resume and is not supposed * to be called by normal code, write proper resume handler and use it instead. */int pci_reenable_device(struct pci_dev *dev){ if (atomic_read(&dev->enable_cnt)) return do_pci_enable_device(dev, (1 << PCI_NUM_RESOURCES) - 1); return 0;}/** * pci_enable_device_bars - Initialize some of a device for use * @dev: PCI device to be initialized * @bars: bitmask of BAR's that must be configured * * Initialize device before it's used by a driver. Ask low-level code * to enable selected I/O and memory resources. Wake up the device if it * was suspended. Beware, this function can fail. */intpci_enable_device_bars(struct pci_dev *dev, int bars){ int err; if (atomic_add_return(1, &dev->enable_cnt) > 1) return 0; /* already enabled */ err = do_pci_enable_device(dev, bars); if (err < 0) atomic_dec(&dev->enable_cnt); return err;}/** * pci_enable_device - Initialize device before it's used by a driver. * @dev: PCI device to be initialized * * Initialize device before it's used by a driver. Ask low-level code * to enable I/O and memory. Wake up the device if it was suspended. * Beware, this function can fail. * * Note we don't actually enable the device many times if we call * this function repeatedly (we just increment the count). */int pci_enable_device(struct pci_dev *dev){ return pci_enable_device_bars(dev, (1 << PCI_NUM_RESOURCES) - 1);}/* * Managed PCI resources. This manages device on/off, intx/msi/msix * on/off and BAR regions. pci_dev itself records msi/msix status, so * there's no need to track it separately. pci_devres is initialized * when a device is enabled using managed PCI device enable interface. */struct pci_devres { unsigned int enabled:1; unsigned int pinned:1; unsigned int orig_intx:1; unsigned int restore_intx:1; u32 region_mask;};static void pcim_release(struct device *gendev, void *res){ struct pci_dev *dev = container_of(gendev, struct pci_dev, dev); struct pci_devres *this = res; int i; if (dev->msi_enabled) pci_disable_msi(dev); if (dev->msix_enabled) pci_disable_msix(dev); for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) if (this->region_mask & (1 << i)) pci_release_region(dev, i); if (this->restore_intx) pci_intx(dev, this->orig_intx); if (this->enabled && !this->pinned) pci_disable_device(dev);}static struct pci_devres * get_pci_dr(struct pci_dev *pdev){ struct pci_devres *dr, *new_dr; dr = devres_find(&pdev->dev, pcim_release, NULL, NULL); if (dr) return dr; new_dr = devres_alloc(pcim_release, sizeof(*new_dr), GFP_KERNEL); if (!new_dr) return NULL; return devres_get(&pdev->dev, new_dr, NULL, NULL);}static struct pci_devres * find_pci_dr(struct pci_dev *pdev){ if (pci_is_managed(pdev)) return devres_find(&pdev->dev, pcim_release, NULL, NULL); return NULL;}/** * pcim_enable_device - Managed pci_enable_device() * @pdev: PCI device to be initialized * * Managed pci_enable_device(). */int pcim_enable_device(struct pci_dev *pdev){ struct pci_devres *dr; int rc; dr = get_pci_dr(pdev); if (unlikely(!dr)) return -ENOMEM; WARN_ON(!!dr->enabled); rc = pci_enable_device(pdev); if (!rc) { pdev->is_managed = 1; dr->enabled = 1; } return rc;}/** * pcim_pin_device - Pin managed PCI device * @pdev: PCI device to pin * * Pin managed PCI device @pdev. Pinned device won't be disabled on * driver detach. @pdev must have been enabled with * pcim_enable_device(). */void pcim_pin_device(struct pci_dev *pdev){ struct pci_devres *dr; dr = find_pci_dr(pdev); WARN_ON(!dr || !dr->enabled); if (dr) dr->pinned = 1;}/** * pcibios_disable_device - disable arch specific PCI resources for device dev * @dev: the PCI device to disable * * Disables architecture specific PCI resources for the device. This * is the default implementation. Architecture implementations can * override this. */void __attribute__ ((weak)) pcibios_disable_device (struct pci_dev *dev) {}/** * pci_disable_device - Disable PCI device after use * @dev: PCI device to be disabled * * Signal to the system that the PCI device is not in use by the system * anymore. This only involves disabling PCI bus-mastering, if active. * * Note we don't actually disable the device until all callers of * pci_device_enable() have called pci_device_disable(). */voidpci_disable_device(struct pci_dev *dev){ struct pci_devres *dr; u16 pci_command; dr = find_pci_dr(dev); if (dr) dr->enabled = 0; if (atomic_sub_return(1, &dev->enable_cnt) != 0) return; pci_read_config_word(dev, PCI_COMMAND, &pci_command); if (pci_command & PCI_COMMAND_MASTER) { pci_command &= ~PCI_COMMAND_MASTER; pci_write_config_word(dev, PCI_COMMAND, pci_command); } dev->is_busmaster = 0; pcibios_disable_device(dev);}/** * pcibios_set_pcie_reset_state - set reset state for device dev * @dev: the PCI-E device reset * @state: Reset state to enter into * * * Sets the PCI-E reset state for the device. This is the default * implementation. Architecture implementations can override this. */int __attribute__ ((weak)) pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state){ return -EINVAL;}/** * pci_set_pcie_reset_state - set reset state for device dev * @dev: the PCI-E device reset * @state: Reset state to enter into * * * Sets the PCI reset state for the device. */int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state){ return pcibios_set_pcie_reset_state(dev, state);}/** * pci_enable_wake - enable PCI device as wakeup event source * @dev: PCI device affected * @state: PCI state from which device will issue wakeup events * @enable: True to enable event generation; false to disable * * This enables the device as a wakeup event source, or disables it. * When such events involves platform-specific hooks, those hooks are * called automatically by this routine. * * Devices with legacy power management (no standard PCI PM capabilities) * always require such platform hooks. Depending on the platform, devices * supporting the standard PCI PME# signal may require such platform hooks; * they always update bits in config space to allow PME# generation. * * -EIO is returned if the device can't ever be a wakeup event source. * -EINVAL is returned if the device can't generate wakeup events from * the specified PCI state. Returns zero if the operation is successful. */int pci_enable_wake(struct pci_dev *dev, pci_power_t state, int enable){ int pm; int status; u16 value; /* Note that drivers should verify device_may_wakeup(&dev->dev) * before calling this function. Platform code should report * errors when drivers try to enable wakeup on devices that * can't issue wakeups, or on which wakeups were disabled by * userspace updating the /sys/devices.../power/wakeup file. */ status = call_platform_enable_wakeup(&dev->dev, enable); /* find PCI PM capability in list */ pm = pci_find_capability(dev, PCI_CAP_ID_PM); /* If device doesn't support PM Capabilities, but caller wants to * disable wake events, it's a NOP. Otherwise fail unless the * platform hooks handled this legacy device already. */ if (!pm) return enable ? status : 0; /* Check device's ability to generate PME# */ pci_read_config_word(dev,pm+PCI_PM_PMC,&value); value &= PCI_PM_CAP_PME_MASK; value >>= ffs(PCI_PM_CAP_PME_MASK) - 1; /* First bit of mask */ /* Check if it can generate PME# from requested state. */ if (!value || !(value & (1 << state))) { /* if it can't, revert what the platform hook changed, * always reporting the base "EINVAL, can't PME#" error */ if (enable) call_platform_enable_wakeup(&dev->dev, 0); return enable ? -EINVAL : 0; } pci_read_config_word(dev, pm + PCI_PM_CTRL, &value); /* Clear PME_Status by writing 1 to it and enable PME# */ value |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE; if (!enable) value &= ~PCI_PM_CTRL_PME_ENABLE; pci_write_config_word(dev, pm + PCI_PM_CTRL, value); return 0;}intpci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge){ u8 pin; pin = dev->pin; if (!pin) return -1; pin--; while (dev->bus->self) { pin = (pin + PCI_SLOT(dev->devfn)) % 4; dev = dev->bus->self; } *bridge = dev; return pin;}/** * pci_release_region - Release a PCI bar * @pdev: PCI device whose resources were previously reserved by pci_request_region * @bar: BAR to release * * Releases the PCI I/O and memory resources previously reserved by a * successful call to pci_request_region. Call this function only * after all use of the PCI regions has ceased. */void pci_release_region(struct pci_dev *pdev, int bar){ struct pci_devres *dr; if (pci_resource_len(pdev, bar) == 0) return; if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) release_region(pci_resource_start(pdev, bar), pci_resource_len(pdev, bar)); else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) release_mem_region(pci_resource_start(pdev, bar), pci_resource_len(pdev, bar)); dr = find_pci_dr(pdev); if (dr) dr->region_mask &= ~(1 << bar);}/** * pci_request_region - Reserved PCI I/O and memory resource * @pdev: PCI device whose resources are to be reserved * @bar: BAR to be reserved * @res_name: Name to be associated with resource. * * Mark the PCI region associated with PCI device @pdev BR @bar as * being reserved by owner @res_name. Do not access any * address inside the PCI regions unless this call returns * successfully. * * Returns 0 on success, or %EBUSY on error. A warning * message is also printed on failure. */int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name){ struct pci_devres *dr; if (pci_resource_len(pdev, bar) == 0) return 0; if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) { if (!request_region(pci_resource_start(pdev, bar), pci_resource_len(pdev, bar), res_name)) goto err_out; } else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) { if (!request_mem_region(pci_resource_start(pdev, bar), pci_resource_len(pdev, bar), res_name)) goto err_out; } dr = find_pci_dr(pdev); if (dr) dr->region_mask |= 1 << bar; return 0;err_out: printk (KERN_WARNING "PCI: Unable to reserve %s region #%d:%llx@%llx " "for device %s\n", pci_resource_flags(pdev, bar) & IORESOURCE_IO ? "I/O" : "mem", bar + 1, /* PCI BAR # */ (unsigned long long)pci_resource_len(pdev, bar), (unsigned long long)pci_resource_start(pdev, bar), pci_name(pdev)); return -EBUSY;}/** * pci_release_selected_regions - Release selected PCI I/O and memory resources * @pdev: PCI device whose resources were previously reserved * @bars: Bitmask of BARs to be released * * Release selected PCI I/O and memory resources previously reserved. * Call this function only after all use of the PCI regions has ceased. */void pci_release_selected_regions(struct pci_dev *pdev, int bars){ int i;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?