1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Common library for PCI host controller drivers 4 * 5 * Copyright (C) 2014 ARM Limited 6 * 7 * Author: Will Deacon <will.deacon@arm.com> 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/module.h> 12 #include <linux/of.h> 13 #include <linux/of_address.h> 14 #include <linux/of_pci.h> 15 #include <linux/pci-ecam.h> 16 #include <linux/platform_device.h> 17 18 #include "pci-host-common.h" 19 20 static void gen_pci_unmap_cfg(void *ptr) 21 { 22 pci_ecam_free((struct pci_config_window *)ptr); 23 } 24 25 static struct pci_config_window *gen_pci_init(struct device *dev, 26 struct pci_host_bridge *bridge, const struct pci_ecam_ops *ops) 27 { 28 int err; 29 struct resource cfgres; 30 struct resource_entry *bus; 31 struct pci_config_window *cfg; 32 33 err = of_address_to_resource(dev->of_node, 0, &cfgres); 34 if (err) { 35 dev_err(dev, "missing \"reg\" property\n"); 36 return ERR_PTR(err); 37 } 38 39 bus = resource_list_first_type(&bridge->windows, IORESOURCE_BUS); 40 if (!bus) 41 return ERR_PTR(-ENODEV); 42 43 cfg = pci_ecam_create(dev, &cfgres, bus->res, ops); 44 if (IS_ERR(cfg)) 45 return cfg; 46 47 err = devm_add_action_or_reset(dev, gen_pci_unmap_cfg, cfg); 48 if (err) 49 return ERR_PTR(err); 50 51 return cfg; 52 } 53 54 int pci_host_common_init(struct platform_device *pdev, 55 const struct pci_ecam_ops *ops) 56 { 57 struct device *dev = &pdev->dev; 58 struct pci_host_bridge *bridge; 59 struct pci_config_window *cfg; 60 61 bridge = devm_pci_alloc_host_bridge(dev, 0); 62 if (!bridge) 63 return -ENOMEM; 64 65 of_pci_check_probe_only(); 66 67 /* Parse and map our Configuration Space windows */ 68 cfg = gen_pci_init(dev, bridge, ops); 69 if (IS_ERR(cfg)) 70 return PTR_ERR(cfg); 71 72 platform_set_drvdata(pdev, bridge); 73 74 bridge->sysdata = cfg; 75 bridge->ops = (struct pci_ops *)&ops->pci_ops; 76 bridge->enable_device = ops->enable_device; 77 bridge->disable_device = ops->disable_device; 78 bridge->msi_domain = true; 79 80 return pci_host_probe(bridge); 81 } 82 EXPORT_SYMBOL_GPL(pci_host_common_init); 83 84 int pci_host_common_probe(struct platform_device *pdev) 85 { 86 const struct pci_ecam_ops *ops; 87 88 ops = of_device_get_match_data(&pdev->dev); 89 if (!ops) 90 return -ENODEV; 91 92 return pci_host_common_init(pdev, ops); 93 } 94 EXPORT_SYMBOL_GPL(pci_host_common_probe); 95 96 void pci_host_common_remove(struct platform_device *pdev) 97 { 98 struct pci_host_bridge *bridge = platform_get_drvdata(pdev); 99 100 pci_lock_rescan_remove(); 101 pci_stop_root_bus(bridge->bus); 102 pci_remove_root_bus(bridge->bus); 103 pci_unlock_rescan_remove(); 104 } 105 EXPORT_SYMBOL_GPL(pci_host_common_remove); 106 107 MODULE_DESCRIPTION("Common library for PCI host controller drivers"); 108 MODULE_LICENSE("GPL v2"); 109