forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/drivers/pci/hotplug/pciehp_core.c
....@@ -17,19 +17,19 @@
1717 * Dely Sy <dely.l.sy@intel.com>"
1818 */
1919
20
+#define pr_fmt(fmt) "pciehp: " fmt
21
+#define dev_fmt pr_fmt
22
+
2023 #include <linux/moduleparam.h>
2124 #include <linux/kernel.h>
2225 #include <linux/slab.h>
2326 #include <linux/types.h>
2427 #include <linux/pci.h>
2528 #include "pciehp.h"
26
-#include <linux/interrupt.h>
27
-#include <linux/time.h>
2829
2930 #include "../pci.h"
3031
3132 /* Global variables */
32
-bool pciehp_debug;
3333 bool pciehp_poll_mode;
3434 int pciehp_poll_time;
3535
....@@ -37,55 +37,36 @@
3737 * not really modular, but the easiest way to keep compat with existing
3838 * bootargs behaviour is to continue using module_param here.
3939 */
40
-module_param(pciehp_debug, bool, 0644);
4140 module_param(pciehp_poll_mode, bool, 0644);
4241 module_param(pciehp_poll_time, int, 0644);
43
-MODULE_PARM_DESC(pciehp_debug, "Debugging mode enabled or not");
4442 MODULE_PARM_DESC(pciehp_poll_mode, "Using polling mechanism for hot-plug events or not");
4543 MODULE_PARM_DESC(pciehp_poll_time, "Polling mechanism frequency, in seconds");
4644
47
-#define PCIE_MODULE_NAME "pciehp"
48
-
4945 static int set_attention_status(struct hotplug_slot *slot, u8 value);
50
-static int enable_slot(struct hotplug_slot *slot);
51
-static int disable_slot(struct hotplug_slot *slot);
5246 static int get_power_status(struct hotplug_slot *slot, u8 *value);
53
-static int get_attention_status(struct hotplug_slot *slot, u8 *value);
5447 static int get_latch_status(struct hotplug_slot *slot, u8 *value);
5548 static int get_adapter_status(struct hotplug_slot *slot, u8 *value);
56
-static int reset_slot(struct hotplug_slot *slot, int probe);
5749
5850 static int init_slot(struct controller *ctrl)
5951 {
60
- struct slot *slot = ctrl->slot;
61
- struct hotplug_slot *hotplug = NULL;
62
- struct hotplug_slot_info *info = NULL;
63
- struct hotplug_slot_ops *ops = NULL;
52
+ struct hotplug_slot_ops *ops;
6453 char name[SLOT_NAME_SIZE];
65
- int retval = -ENOMEM;
66
-
67
- hotplug = kzalloc(sizeof(*hotplug), GFP_KERNEL);
68
- if (!hotplug)
69
- goto out;
70
-
71
- info = kzalloc(sizeof(*info), GFP_KERNEL);
72
- if (!info)
73
- goto out;
54
+ int retval;
7455
7556 /* Setup hotplug slot ops */
7657 ops = kzalloc(sizeof(*ops), GFP_KERNEL);
7758 if (!ops)
78
- goto out;
59
+ return -ENOMEM;
7960
80
- ops->enable_slot = enable_slot;
81
- ops->disable_slot = disable_slot;
61
+ ops->enable_slot = pciehp_sysfs_enable_slot;
62
+ ops->disable_slot = pciehp_sysfs_disable_slot;
8263 ops->get_power_status = get_power_status;
8364 ops->get_adapter_status = get_adapter_status;
84
- ops->reset_slot = reset_slot;
65
+ ops->reset_slot = pciehp_reset_slot;
8566 if (MRL_SENS(ctrl))
8667 ops->get_latch_status = get_latch_status;
8768 if (ATTN_LED(ctrl)) {
88
- ops->get_attention_status = get_attention_status;
69
+ ops->get_attention_status = pciehp_get_attention_status;
8970 ops->set_attention_status = set_attention_status;
9071 } else if (ctrl->pcie->port->hotplug_user_indicators) {
9172 ops->get_attention_status = pciehp_get_raw_indicator_status;
....@@ -93,115 +74,86 @@
9374 }
9475
9576 /* register this slot with the hotplug pci core */
96
- hotplug->info = info;
97
- hotplug->private = slot;
98
- hotplug->ops = ops;
99
- slot->hotplug_slot = hotplug;
77
+ ctrl->hotplug_slot.ops = ops;
10078 snprintf(name, SLOT_NAME_SIZE, "%u", PSN(ctrl));
10179
102
- retval = pci_hp_initialize(hotplug,
80
+ retval = pci_hp_initialize(&ctrl->hotplug_slot,
10381 ctrl->pcie->port->subordinate, 0, name);
104
- if (retval)
105
- ctrl_err(ctrl, "pci_hp_initialize failed: error %d\n", retval);
106
-out:
10782 if (retval) {
83
+ ctrl_err(ctrl, "pci_hp_initialize failed: error %d\n", retval);
10884 kfree(ops);
109
- kfree(info);
110
- kfree(hotplug);
11185 }
11286 return retval;
11387 }
11488
11589 static void cleanup_slot(struct controller *ctrl)
11690 {
117
- struct hotplug_slot *hotplug_slot = ctrl->slot->hotplug_slot;
91
+ struct hotplug_slot *hotplug_slot = &ctrl->hotplug_slot;
11892
11993 pci_hp_destroy(hotplug_slot);
12094 kfree(hotplug_slot->ops);
121
- kfree(hotplug_slot->info);
122
- kfree(hotplug_slot);
12395 }
12496
12597 /*
126
- * set_attention_status - Turns the Amber LED for a slot on, off or blink
98
+ * set_attention_status - Turns the Attention Indicator on, off or blinking
12799 */
128100 static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
129101 {
130
- struct slot *slot = hotplug_slot->private;
131
- struct pci_dev *pdev = slot->ctrl->pcie->port;
102
+ struct controller *ctrl = to_ctrl(hotplug_slot);
103
+ struct pci_dev *pdev = ctrl->pcie->port;
104
+
105
+ if (status)
106
+ status <<= PCI_EXP_SLTCTL_ATTN_IND_SHIFT;
107
+ else
108
+ status = PCI_EXP_SLTCTL_ATTN_IND_OFF;
132109
133110 pci_config_pm_runtime_get(pdev);
134
- pciehp_set_attention_status(slot, status);
111
+ pciehp_set_indicators(ctrl, INDICATOR_NOOP, status);
135112 pci_config_pm_runtime_put(pdev);
136113 return 0;
137
-}
138
-
139
-
140
-static int enable_slot(struct hotplug_slot *hotplug_slot)
141
-{
142
- struct slot *slot = hotplug_slot->private;
143
-
144
- return pciehp_sysfs_enable_slot(slot);
145
-}
146
-
147
-
148
-static int disable_slot(struct hotplug_slot *hotplug_slot)
149
-{
150
- struct slot *slot = hotplug_slot->private;
151
-
152
- return pciehp_sysfs_disable_slot(slot);
153114 }
154115
155116 static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
156117 {
157
- struct slot *slot = hotplug_slot->private;
158
- struct pci_dev *pdev = slot->ctrl->pcie->port;
118
+ struct controller *ctrl = to_ctrl(hotplug_slot);
119
+ struct pci_dev *pdev = ctrl->pcie->port;
159120
160121 pci_config_pm_runtime_get(pdev);
161
- pciehp_get_power_status(slot, value);
122
+ pciehp_get_power_status(ctrl, value);
162123 pci_config_pm_runtime_put(pdev);
163
- return 0;
164
-}
165
-
166
-static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
167
-{
168
- struct slot *slot = hotplug_slot->private;
169
-
170
- pciehp_get_attention_status(slot, value);
171124 return 0;
172125 }
173126
174127 static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
175128 {
176
- struct slot *slot = hotplug_slot->private;
177
- struct pci_dev *pdev = slot->ctrl->pcie->port;
129
+ struct controller *ctrl = to_ctrl(hotplug_slot);
130
+ struct pci_dev *pdev = ctrl->pcie->port;
178131
179132 pci_config_pm_runtime_get(pdev);
180
- pciehp_get_latch_status(slot, value);
133
+ pciehp_get_latch_status(ctrl, value);
181134 pci_config_pm_runtime_put(pdev);
182135 return 0;
183136 }
184137
185138 static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
186139 {
187
- struct slot *slot = hotplug_slot->private;
188
- struct pci_dev *pdev = slot->ctrl->pcie->port;
140
+ struct controller *ctrl = to_ctrl(hotplug_slot);
141
+ struct pci_dev *pdev = ctrl->pcie->port;
142
+ int ret;
189143
190144 pci_config_pm_runtime_get(pdev);
191
- pciehp_get_adapter_status(slot, value);
145
+ ret = pciehp_card_present_or_link_active(ctrl);
192146 pci_config_pm_runtime_put(pdev);
147
+ if (ret < 0)
148
+ return ret;
149
+
150
+ *value = ret;
193151 return 0;
194
-}
195
-
196
-static int reset_slot(struct hotplug_slot *hotplug_slot, int probe)
197
-{
198
- struct slot *slot = hotplug_slot->private;
199
-
200
- return pciehp_reset_slot(slot, probe);
201152 }
202153
203154 /**
204155 * pciehp_check_presence() - synthesize event if presence has changed
156
+ * @ctrl: controller to check
205157 *
206158 * On probe and resume, an explicit presence check is necessary to bring up an
207159 * occupied slot or bring down an unoccupied slot. This can't be triggered by
....@@ -212,20 +164,19 @@
212164 */
213165 static void pciehp_check_presence(struct controller *ctrl)
214166 {
215
- struct slot *slot = ctrl->slot;
216
- u8 occupied;
167
+ int occupied;
217168
218
- down_read(&ctrl->reset_lock);
219
- mutex_lock(&slot->lock);
169
+ down_read_nested(&ctrl->reset_lock, ctrl->depth);
170
+ mutex_lock(&ctrl->state_lock);
220171
221
- pciehp_get_adapter_status(slot, &occupied);
222
- if ((occupied && (slot->state == OFF_STATE ||
223
- slot->state == BLINKINGON_STATE)) ||
224
- (!occupied && (slot->state == ON_STATE ||
225
- slot->state == BLINKINGOFF_STATE)))
172
+ occupied = pciehp_card_present_or_link_active(ctrl);
173
+ if ((occupied > 0 && (ctrl->state == OFF_STATE ||
174
+ ctrl->state == BLINKINGON_STATE)) ||
175
+ (!occupied && (ctrl->state == ON_STATE ||
176
+ ctrl->state == BLINKINGOFF_STATE)))
226177 pciehp_request(ctrl, PCI_EXP_SLTSTA_PDC);
227178
228
- mutex_unlock(&slot->lock);
179
+ mutex_unlock(&ctrl->state_lock);
229180 up_read(&ctrl->reset_lock);
230181 }
231182
....@@ -233,7 +184,6 @@
233184 {
234185 int rc;
235186 struct controller *ctrl;
236
- struct slot *slot;
237187
238188 /* If this is not a "hotplug" service, we have no business here. */
239189 if (dev->service != PCIE_PORT_SERVICE_HP)
....@@ -241,14 +191,14 @@
241191
242192 if (!dev->port->subordinate) {
243193 /* Can happen if we run out of bus numbers during probe */
244
- dev_err(&dev->device,
194
+ pci_err(dev->port,
245195 "Hotplug bridge without secondary bus, ignoring\n");
246196 return -ENODEV;
247197 }
248198
249199 ctrl = pcie_init(dev);
250200 if (!ctrl) {
251
- dev_err(&dev->device, "Controller initialization failed\n");
201
+ pci_err(dev->port, "Controller initialization failed\n");
252202 return -ENODEV;
253203 }
254204 set_service_data(dev, ctrl);
....@@ -271,8 +221,7 @@
271221 }
272222
273223 /* Publish to user space */
274
- slot = ctrl->slot;
275
- rc = pci_hp_add(slot->hotplug_slot);
224
+ rc = pci_hp_add(&ctrl->hotplug_slot);
276225 if (rc) {
277226 ctrl_err(ctrl, "Publication to user space failed (%d)\n", rc);
278227 goto err_out_shutdown_notification;
....@@ -295,46 +244,98 @@
295244 {
296245 struct controller *ctrl = get_service_data(dev);
297246
298
- pci_hp_del(ctrl->slot->hotplug_slot);
247
+ pci_hp_del(&ctrl->hotplug_slot);
299248 pcie_shutdown_notification(ctrl);
300249 cleanup_slot(ctrl);
301250 pciehp_release_ctrl(ctrl);
302251 }
303252
304253 #ifdef CONFIG_PM
254
+static bool pme_is_native(struct pcie_device *dev)
255
+{
256
+ const struct pci_host_bridge *host;
257
+
258
+ host = pci_find_host_bridge(dev->port->bus);
259
+ return pcie_ports_native || host->native_pme;
260
+}
261
+
262
+static void pciehp_disable_interrupt(struct pcie_device *dev)
263
+{
264
+ /*
265
+ * Disable hotplug interrupt so that it does not trigger
266
+ * immediately when the downstream link goes down.
267
+ */
268
+ if (pme_is_native(dev))
269
+ pcie_disable_interrupt(get_service_data(dev));
270
+}
271
+
272
+#ifdef CONFIG_PM_SLEEP
305273 static int pciehp_suspend(struct pcie_device *dev)
306274 {
275
+ /*
276
+ * If the port is already runtime suspended we can keep it that
277
+ * way.
278
+ */
279
+ if (dev_pm_skip_suspend(&dev->port->dev))
280
+ return 0;
281
+
282
+ pciehp_disable_interrupt(dev);
307283 return 0;
308284 }
309285
310286 static int pciehp_resume_noirq(struct pcie_device *dev)
311287 {
312288 struct controller *ctrl = get_service_data(dev);
313
- struct slot *slot = ctrl->slot;
314289
315290 /* pci_restore_state() just wrote to the Slot Control register */
316291 ctrl->cmd_started = jiffies;
317292 ctrl->cmd_busy = true;
318293
319294 /* clear spurious events from rediscovery of inserted card */
320
- if (slot->state == ON_STATE || slot->state == BLINKINGOFF_STATE)
295
+ if (ctrl->state == ON_STATE || ctrl->state == BLINKINGOFF_STATE)
321296 pcie_clear_hotplug_events(ctrl);
322297
323298 return 0;
324299 }
300
+#endif
325301
326302 static int pciehp_resume(struct pcie_device *dev)
327303 {
328304 struct controller *ctrl = get_service_data(dev);
329305
306
+ if (pme_is_native(dev))
307
+ pcie_enable_interrupt(ctrl);
308
+
330309 pciehp_check_presence(ctrl);
331310
332311 return 0;
333312 }
313
+
314
+static int pciehp_runtime_suspend(struct pcie_device *dev)
315
+{
316
+ pciehp_disable_interrupt(dev);
317
+ return 0;
318
+}
319
+
320
+static int pciehp_runtime_resume(struct pcie_device *dev)
321
+{
322
+ struct controller *ctrl = get_service_data(dev);
323
+
324
+ /* pci_restore_state() just wrote to the Slot Control register */
325
+ ctrl->cmd_started = jiffies;
326
+ ctrl->cmd_busy = true;
327
+
328
+ /* clear spurious events from rediscovery of inserted card */
329
+ if ((ctrl->state == ON_STATE || ctrl->state == BLINKINGOFF_STATE) &&
330
+ pme_is_native(dev))
331
+ pcie_clear_hotplug_events(ctrl);
332
+
333
+ return pciehp_resume(dev);
334
+}
334335 #endif /* PM */
335336
336337 static struct pcie_port_service_driver hpdriver_portdrv = {
337
- .name = PCIE_MODULE_NAME,
338
+ .name = "pciehp",
338339 .port_type = PCIE_ANY_PORT,
339340 .service = PCIE_PORT_SERVICE_HP,
340341
....@@ -342,9 +343,13 @@
342343 .remove = pciehp_remove,
343344
344345 #ifdef CONFIG_PM
346
+#ifdef CONFIG_PM_SLEEP
345347 .suspend = pciehp_suspend,
346348 .resume_noirq = pciehp_resume_noirq,
347349 .resume = pciehp_resume,
350
+#endif
351
+ .runtime_suspend = pciehp_runtime_suspend,
352
+ .runtime_resume = pciehp_runtime_resume,
348353 #endif /* PM */
349354 };
350355
....@@ -353,9 +358,9 @@
353358 int retval = 0;
354359
355360 retval = pcie_port_service_register(&hpdriver_portdrv);
356
- dbg("pcie_port_service_register = %d\n", retval);
361
+ pr_debug("pcie_port_service_register = %d\n", retval);
357362 if (retval)
358
- dbg("Failure to register service\n");
363
+ pr_debug("Failure to register service\n");
359364
360365 return retval;
361366 }