Bjorn Helgaas | 8cfab3c | 2018-01-26 12:50:27 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 2 | /* |
Paul Gortmaker | 4068bd1 | 2016-08-22 17:59:48 -0400 | [diff] [blame] | 3 | * Generic PCI host driver common code |
| 4 | * |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 5 | * Copyright (C) 2014 ARM Limited |
| 6 | * |
| 7 | * Author: Will Deacon <will.deacon@arm.com> |
| 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
Rob Herring | 0c59c06 | 2020-04-09 17:49:22 -0600 | [diff] [blame] | 11 | #include <linux/module.h> |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 12 | #include <linux/of_address.h> |
Rob Herring | b2f75a4 | 2020-04-09 17:49:23 -0600 | [diff] [blame] | 13 | #include <linux/of_device.h> |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 14 | #include <linux/of_pci.h> |
Jayachandran C | 80955f9 | 2016-06-10 21:55:09 +0200 | [diff] [blame] | 15 | #include <linux/pci-ecam.h> |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 16 | #include <linux/platform_device.h> |
| 17 | |
Jayachandran C | 1958e71 | 2016-05-11 17:34:46 -0500 | [diff] [blame] | 18 | static void gen_pci_unmap_cfg(void *ptr) |
| 19 | { |
| 20 | pci_ecam_free((struct pci_config_window *)ptr); |
| 21 | } |
| 22 | |
| 23 | static struct pci_config_window *gen_pci_init(struct device *dev, |
Rob Herring | e63434f | 2020-05-22 17:48:20 -0600 | [diff] [blame] | 24 | struct pci_host_bridge *bridge, const struct pci_ecam_ops *ops) |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 25 | { |
| 26 | int err; |
Jayachandran C | 1958e71 | 2016-05-11 17:34:46 -0500 | [diff] [blame] | 27 | struct resource cfgres; |
Rob Herring | 669cbc7 | 2020-07-21 20:25:13 -0600 | [diff] [blame] | 28 | struct resource_entry *bus; |
Jayachandran C | 1958e71 | 2016-05-11 17:34:46 -0500 | [diff] [blame] | 29 | struct pci_config_window *cfg; |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 30 | |
Jayachandran C | 1958e71 | 2016-05-11 17:34:46 -0500 | [diff] [blame] | 31 | err = of_address_to_resource(dev->of_node, 0, &cfgres); |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 32 | if (err) { |
| 33 | dev_err(dev, "missing \"reg\" property\n"); |
Rob Herring | e63434f | 2020-05-22 17:48:20 -0600 | [diff] [blame] | 34 | return ERR_PTR(err); |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 35 | } |
| 36 | |
Rob Herring | 669cbc7 | 2020-07-21 20:25:13 -0600 | [diff] [blame] | 37 | bus = resource_list_first_type(&bridge->windows, IORESOURCE_BUS); |
| 38 | if (!bus) |
| 39 | return ERR_PTR(-ENODEV); |
| 40 | |
| 41 | cfg = pci_ecam_create(dev, &cfgres, bus->res, ops); |
Rob Herring | e63434f | 2020-05-22 17:48:20 -0600 | [diff] [blame] | 42 | if (IS_ERR(cfg)) |
| 43 | return cfg; |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 44 | |
Fuqian Huang | befa45fb | 2019-07-08 20:33:54 +0800 | [diff] [blame] | 45 | err = devm_add_action_or_reset(dev, gen_pci_unmap_cfg, cfg); |
Rob Herring | e63434f | 2020-05-22 17:48:20 -0600 | [diff] [blame] | 46 | if (err) |
| 47 | return ERR_PTR(err); |
Jayachandran C | 1958e71 | 2016-05-11 17:34:46 -0500 | [diff] [blame] | 48 | |
Rob Herring | e63434f | 2020-05-22 17:48:20 -0600 | [diff] [blame] | 49 | return cfg; |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 50 | } |
| 51 | |
Rob Herring | b2f75a4 | 2020-04-09 17:49:23 -0600 | [diff] [blame] | 52 | int pci_host_common_probe(struct platform_device *pdev) |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 53 | { |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 54 | struct device *dev = &pdev->dev; |
Lorenzo Pieralisi | 4246a86 | 2017-06-28 15:14:00 -0500 | [diff] [blame] | 55 | struct pci_host_bridge *bridge; |
Jayachandran C | 1958e71 | 2016-05-11 17:34:46 -0500 | [diff] [blame] | 56 | struct pci_config_window *cfg; |
Rob Herring | b2f75a4 | 2020-04-09 17:49:23 -0600 | [diff] [blame] | 57 | const struct pci_ecam_ops *ops; |
Lorenzo Pieralisi | 4246a86 | 2017-06-28 15:14:00 -0500 | [diff] [blame] | 58 | |
Rob Herring | b2f75a4 | 2020-04-09 17:49:23 -0600 | [diff] [blame] | 59 | ops = of_device_get_match_data(&pdev->dev); |
| 60 | if (!ops) |
| 61 | return -ENODEV; |
| 62 | |
Lorenzo Pieralisi | 4246a86 | 2017-06-28 15:14:00 -0500 | [diff] [blame] | 63 | bridge = devm_pci_alloc_host_bridge(dev, 0); |
| 64 | if (!bridge) |
| 65 | return -ENOMEM; |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 66 | |
Daire McNamara | 791c9f1 | 2021-01-25 16:29:31 +0000 | [diff] [blame] | 67 | platform_set_drvdata(pdev, bridge); |
| 68 | |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 69 | of_pci_check_probe_only(); |
| 70 | |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 71 | /* Parse and map our Configuration Space windows */ |
Rob Herring | e63434f | 2020-05-22 17:48:20 -0600 | [diff] [blame] | 72 | cfg = gen_pci_init(dev, bridge, ops); |
Jayachandran C | 1958e71 | 2016-05-11 17:34:46 -0500 | [diff] [blame] | 73 | if (IS_ERR(cfg)) |
| 74 | return PTR_ERR(cfg); |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 75 | |
| 76 | /* Do not reassign resources if probe only */ |
| 77 | if (!pci_has_flag(PCI_PROBE_ONLY)) |
Bjorn Helgaas | 7153884 | 2017-11-30 11:21:57 -0600 | [diff] [blame] | 78 | pci_add_flags(PCI_REASSIGN_ALL_BUS); |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 79 | |
Lorenzo Pieralisi | 4246a86 | 2017-06-28 15:14:00 -0500 | [diff] [blame] | 80 | bridge->sysdata = cfg; |
Rob Herring | 0b10477 | 2020-04-09 17:49:21 -0600 | [diff] [blame] | 81 | bridge->ops = (struct pci_ops *)&ops->pci_ops; |
Marc Zyngier | 9ec37ef | 2021-03-30 16:11:42 +0100 | [diff] [blame] | 82 | bridge->msi_domain = true; |
Lorenzo Pieralisi | 4246a86 | 2017-06-28 15:14:00 -0500 | [diff] [blame] | 83 | |
Rob Herring | e63434f | 2020-05-22 17:48:20 -0600 | [diff] [blame] | 84 | return pci_host_probe(bridge); |
Jan Kiszka | 01fcb7f | 2018-05-15 11:07:06 +0200 | [diff] [blame] | 85 | } |
Rob Herring | 0c59c06 | 2020-04-09 17:49:22 -0600 | [diff] [blame] | 86 | EXPORT_SYMBOL_GPL(pci_host_common_probe); |
Jan Kiszka | 01fcb7f | 2018-05-15 11:07:06 +0200 | [diff] [blame] | 87 | |
| 88 | int pci_host_common_remove(struct platform_device *pdev) |
| 89 | { |
Rob Herring | e63434f | 2020-05-22 17:48:20 -0600 | [diff] [blame] | 90 | struct pci_host_bridge *bridge = platform_get_drvdata(pdev); |
Jan Kiszka | 01fcb7f | 2018-05-15 11:07:06 +0200 | [diff] [blame] | 91 | |
| 92 | pci_lock_rescan_remove(); |
Rob Herring | e63434f | 2020-05-22 17:48:20 -0600 | [diff] [blame] | 93 | pci_stop_root_bus(bridge->bus); |
| 94 | pci_remove_root_bus(bridge->bus); |
Jan Kiszka | 01fcb7f | 2018-05-15 11:07:06 +0200 | [diff] [blame] | 95 | pci_unlock_rescan_remove(); |
| 96 | |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 97 | return 0; |
| 98 | } |
Rob Herring | 0c59c06 | 2020-04-09 17:49:22 -0600 | [diff] [blame] | 99 | EXPORT_SYMBOL_GPL(pci_host_common_remove); |
| 100 | |
| 101 | MODULE_LICENSE("GPL v2"); |