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 struct pci_config_window *pci_host_common_ecam_create(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 EXPORT_SYMBOL_GPL(pci_host_common_ecam_create); 54 55 int pci_host_common_init(struct platform_device *pdev, 56 struct pci_host_bridge *bridge, 57 const struct pci_ecam_ops *ops) 58 { 59 struct device *dev = &pdev->dev; 60 struct pci_config_window *cfg; 61 62 of_pci_check_probe_only(); 63 64 platform_set_drvdata(pdev, bridge); 65 66 /* Parse and map our Configuration Space windows */ 67 cfg = pci_host_common_ecam_create(dev, bridge, ops); 68 if (IS_ERR(cfg)) 69 return PTR_ERR(cfg); 70 71 bridge->sysdata = cfg; 72 bridge->ops = (struct pci_ops *)&ops->pci_ops; 73 bridge->enable_device = ops->enable_device; 74 bridge->disable_device = ops->disable_device; 75 bridge->msi_domain = true; 76 77 return pci_host_probe(bridge); 78 } 79 EXPORT_SYMBOL_GPL(pci_host_common_init); 80 81 int pci_host_common_probe(struct platform_device *pdev) 82 { 83 const struct pci_ecam_ops *ops; 84 struct pci_host_bridge *bridge; 85 86 ops = of_device_get_match_data(&pdev->dev); 87 if (!ops) 88 return -ENODEV; 89 90 bridge = devm_pci_alloc_host_bridge(&pdev->dev, 0); 91 if (!bridge) 92 return -ENOMEM; 93 94 return pci_host_common_init(pdev, bridge, ops); 95 } 96 EXPORT_SYMBOL_GPL(pci_host_common_probe); 97 98 void pci_host_common_remove(struct platform_device *pdev) 99 { 100 struct pci_host_bridge *bridge = platform_get_drvdata(pdev); 101 102 pci_lock_rescan_remove(); 103 pci_stop_root_bus(bridge->bus); 104 pci_remove_root_bus(bridge->bus); 105 pci_unlock_rescan_remove(); 106 } 107 EXPORT_SYMBOL_GPL(pci_host_common_remove); 108 109 MODULE_DESCRIPTION("Common library for PCI host controller drivers"); 110 MODULE_LICENSE("GPL v2"); 111