]> Git Repo - linux.git/blob - drivers/pci/pwrctl/core.c
x86/kaslr: Expose and use the end of the physical memory address space
[linux.git] / drivers / pci / pwrctl / core.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2024 Linaro Ltd.
4  */
5
6 #include <linux/device.h>
7 #include <linux/export.h>
8 #include <linux/kernel.h>
9 #include <linux/pci.h>
10 #include <linux/pci-pwrctl.h>
11 #include <linux/property.h>
12 #include <linux/slab.h>
13
14 static int pci_pwrctl_notify(struct notifier_block *nb, unsigned long action,
15                              void *data)
16 {
17         struct pci_pwrctl *pwrctl = container_of(nb, struct pci_pwrctl, nb);
18         struct device *dev = data;
19
20         if (dev_fwnode(dev) != dev_fwnode(pwrctl->dev))
21                 return NOTIFY_DONE;
22
23         switch (action) {
24         case BUS_NOTIFY_ADD_DEVICE:
25                 /*
26                  * We will have two struct device objects bound to two different
27                  * drivers on different buses but consuming the same DT node. We
28                  * must not bind the pins twice in this case but only once for
29                  * the first device to be added.
30                  *
31                  * If we got here then the PCI device is the second after the
32                  * power control platform device. Mark its OF node as reused.
33                  */
34                 dev->of_node_reused = true;
35                 break;
36         case BUS_NOTIFY_BOUND_DRIVER:
37                 pwrctl->link = device_link_add(dev, pwrctl->dev,
38                                                DL_FLAG_AUTOREMOVE_CONSUMER);
39                 if (!pwrctl->link)
40                         dev_err(pwrctl->dev, "Failed to add device link\n");
41                 break;
42         case BUS_NOTIFY_UNBOUND_DRIVER:
43                 if (pwrctl->link)
44                         device_link_remove(dev, pwrctl->dev);
45                 break;
46         }
47
48         return NOTIFY_DONE;
49 }
50
51 /**
52  * pci_pwrctl_device_set_ready() - Notify the pwrctl subsystem that the PCI
53  * device is powered-up and ready to be detected.
54  *
55  * @pwrctl: PCI power control data.
56  *
57  * Returns:
58  * 0 on success, negative error number on error.
59  *
60  * Note:
61  * This function returning 0 doesn't mean the device was detected. It means,
62  * that the bus rescan was successfully started. The device will get bound to
63  * its PCI driver asynchronously.
64  */
65 int pci_pwrctl_device_set_ready(struct pci_pwrctl *pwrctl)
66 {
67         int ret;
68
69         if (!pwrctl->dev)
70                 return -ENODEV;
71
72         pwrctl->nb.notifier_call = pci_pwrctl_notify;
73         ret = bus_register_notifier(&pci_bus_type, &pwrctl->nb);
74         if (ret)
75                 return ret;
76
77         pci_lock_rescan_remove();
78         pci_rescan_bus(to_pci_dev(pwrctl->dev->parent)->bus);
79         pci_unlock_rescan_remove();
80
81         return 0;
82 }
83 EXPORT_SYMBOL_GPL(pci_pwrctl_device_set_ready);
84
85 /**
86  * pci_pwrctl_device_unset_ready() - Notify the pwrctl subsystem that the PCI
87  * device is about to be powered-down.
88  *
89  * @pwrctl: PCI power control data.
90  */
91 void pci_pwrctl_device_unset_ready(struct pci_pwrctl *pwrctl)
92 {
93         /*
94          * We don't have to delete the link here. Typically, this function
95          * is only called when the power control device is being detached. If
96          * it is being detached then the child PCI device must have already
97          * been unbound too or the device core wouldn't let us unbind.
98          */
99         bus_unregister_notifier(&pci_bus_type, &pwrctl->nb);
100 }
101 EXPORT_SYMBOL_GPL(pci_pwrctl_device_unset_ready);
102
103 static void devm_pci_pwrctl_device_unset_ready(void *data)
104 {
105         struct pci_pwrctl *pwrctl = data;
106
107         pci_pwrctl_device_unset_ready(pwrctl);
108 }
109
110 /**
111  * devm_pci_pwrctl_device_set_ready - Managed variant of
112  * pci_pwrctl_device_set_ready().
113  *
114  * @dev: Device managing this pwrctl provider.
115  * @pwrctl: PCI power control data.
116  *
117  * Returns:
118  * 0 on success, negative error number on error.
119  */
120 int devm_pci_pwrctl_device_set_ready(struct device *dev,
121                                      struct pci_pwrctl *pwrctl)
122 {
123         int ret;
124
125         ret = pci_pwrctl_device_set_ready(pwrctl);
126         if (ret)
127                 return ret;
128
129         return devm_add_action_or_reset(dev,
130                                         devm_pci_pwrctl_device_unset_ready,
131                                         pwrctl);
132 }
133 EXPORT_SYMBOL_GPL(devm_pci_pwrctl_device_set_ready);
134
135 MODULE_AUTHOR("Bartosz Golaszewski <[email protected]>");
136 MODULE_DESCRIPTION("PCI Device Power Control core driver");
137 MODULE_LICENSE("GPL");
This page took 0.042044 seconds and 4 git commands to generate.