@@ -193,12 +193,39 @@ static void pcie_clkpm_cap_init(struct pcie_link_state *link, int blacklist)
193
193
link -> clkpm_disable = blacklist ? 1 : 0 ;
194
194
}
195
195
196
- static bool pcie_retrain_link (struct pcie_link_state * link )
196
+ static int pcie_wait_for_retrain (struct pci_dev * pdev )
197
197
{
198
- struct pci_dev * parent = link -> pdev ;
199
198
unsigned long end_jiffies ;
200
199
u16 reg16 ;
201
200
201
+ /* Wait for Link Training to be cleared by hardware */
202
+ end_jiffies = jiffies + LINK_RETRAIN_TIMEOUT ;
203
+ do {
204
+ pcie_capability_read_word (pdev , PCI_EXP_LNKSTA , & reg16 );
205
+ if (!(reg16 & PCI_EXP_LNKSTA_LT ))
206
+ return 0 ;
207
+ msleep (1 );
208
+ } while (time_before (jiffies , end_jiffies ));
209
+
210
+ return - ETIMEDOUT ;
211
+ }
212
+
213
+ static int pcie_retrain_link (struct pcie_link_state * link )
214
+ {
215
+ struct pci_dev * parent = link -> pdev ;
216
+ int rc ;
217
+ u16 reg16 ;
218
+
219
+ /*
220
+ * Ensure the updated LNKCTL parameters are used during link
221
+ * training by checking that there is no ongoing link training to
222
+ * avoid LTSSM race as recommended in Implementation Note at the
223
+ * end of PCIe r6.0.1 sec 7.5.3.7.
224
+ */
225
+ rc = pcie_wait_for_retrain (parent );
226
+ if (rc )
227
+ return rc ;
228
+
202
229
pcie_capability_read_word (parent , PCI_EXP_LNKCTL , & reg16 );
203
230
reg16 |= PCI_EXP_LNKCTL_RL ;
204
231
pcie_capability_write_word (parent , PCI_EXP_LNKCTL , reg16 );
@@ -212,15 +239,7 @@ static bool pcie_retrain_link(struct pcie_link_state *link)
212
239
pcie_capability_write_word (parent , PCI_EXP_LNKCTL , reg16 );
213
240
}
214
241
215
- /* Wait for link training end. Break out after waiting for timeout */
216
- end_jiffies = jiffies + LINK_RETRAIN_TIMEOUT ;
217
- do {
218
- pcie_capability_read_word (parent , PCI_EXP_LNKSTA , & reg16 );
219
- if (!(reg16 & PCI_EXP_LNKSTA_LT ))
220
- break ;
221
- msleep (1 );
222
- } while (time_before (jiffies , end_jiffies ));
223
- return !(reg16 & PCI_EXP_LNKSTA_LT );
242
+ return pcie_wait_for_retrain (parent );
224
243
}
225
244
226
245
/*
@@ -289,15 +308,15 @@ static void pcie_aspm_configure_common_clock(struct pcie_link_state *link)
289
308
reg16 &= ~PCI_EXP_LNKCTL_CCC ;
290
309
pcie_capability_write_word (parent , PCI_EXP_LNKCTL , reg16 );
291
310
292
- if (pcie_retrain_link (link ))
293
- return ;
311
+ if (pcie_retrain_link (link )) {
294
312
295
- /* Training failed. Restore common clock configurations */
296
- pci_err (parent , "ASPM: Could not configure common clock\n" );
297
- list_for_each_entry (child , & linkbus -> devices , bus_list )
298
- pcie_capability_write_word (child , PCI_EXP_LNKCTL ,
313
+ /* Training failed. Restore common clock configurations */
314
+ pci_err (parent , "ASPM: Could not configure common clock\n" );
315
+ list_for_each_entry (child , & linkbus -> devices , bus_list )
316
+ pcie_capability_write_word (child , PCI_EXP_LNKCTL ,
299
317
child_reg [PCI_FUNC (child -> devfn )]);
300
- pcie_capability_write_word (parent , PCI_EXP_LNKCTL , parent_reg );
318
+ pcie_capability_write_word (parent , PCI_EXP_LNKCTL , parent_reg );
319
+ }
301
320
}
302
321
303
322
/* Convert L0s latency encoding to ns */
@@ -337,7 +356,7 @@ static u32 calc_l1_acceptable(u32 encoding)
337
356
}
338
357
339
358
/* Convert L1SS T_pwr encoding to usec */
340
- static u32 calc_l1ss_pwron (struct pci_dev * pdev , u32 scale , u32 val )
359
+ static u32 calc_l12_pwron (struct pci_dev * pdev , u32 scale , u32 val )
341
360
{
342
361
switch (scale ) {
343
362
case 0 :
@@ -471,7 +490,7 @@ static void pci_clear_and_set_dword(struct pci_dev *pdev, int pos,
471
490
}
472
491
473
492
/* Calculate L1.2 PM substate timing parameters */
474
- static void aspm_calc_l1ss_info (struct pcie_link_state * link ,
493
+ static void aspm_calc_l12_info (struct pcie_link_state * link ,
475
494
u32 parent_l1ss_cap , u32 child_l1ss_cap )
476
495
{
477
496
struct pci_dev * child = link -> downstream , * parent = link -> pdev ;
@@ -481,9 +500,6 @@ static void aspm_calc_l1ss_info(struct pcie_link_state *link,
481
500
u32 pctl1 , pctl2 , cctl1 , cctl2 ;
482
501
u32 pl1_2_enables , cl1_2_enables ;
483
502
484
- if (!(link -> aspm_support & ASPM_STATE_L1_2_MASK ))
485
- return ;
486
-
487
503
/* Choose the greater of the two Port Common_Mode_Restore_Times */
488
504
val1 = (parent_l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME ) >> 8 ;
489
505
val2 = (child_l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME ) >> 8 ;
@@ -495,13 +511,13 @@ static void aspm_calc_l1ss_info(struct pcie_link_state *link,
495
511
val2 = (child_l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_VALUE ) >> 19 ;
496
512
scale2 = (child_l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_SCALE ) >> 16 ;
497
513
498
- if (calc_l1ss_pwron (parent , scale1 , val1 ) >
499
- calc_l1ss_pwron (child , scale2 , val2 )) {
514
+ if (calc_l12_pwron (parent , scale1 , val1 ) >
515
+ calc_l12_pwron (child , scale2 , val2 )) {
500
516
ctl2 |= scale1 | (val1 << 3 );
501
- t_power_on = calc_l1ss_pwron (parent , scale1 , val1 );
517
+ t_power_on = calc_l12_pwron (parent , scale1 , val1 );
502
518
} else {
503
519
ctl2 |= scale2 | (val2 << 3 );
504
- t_power_on = calc_l1ss_pwron (child , scale2 , val2 );
520
+ t_power_on = calc_l12_pwron (child , scale2 , val2 );
505
521
}
506
522
507
523
/*
@@ -616,8 +632,8 @@ static void aspm_l1ss_init(struct pcie_link_state *link)
616
632
if (parent_l1ss_ctl1 & child_l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_2 )
617
633
link -> aspm_enabled |= ASPM_STATE_L1_2_PCIPM ;
618
634
619
- if (link -> aspm_support & ASPM_STATE_L1SS )
620
- aspm_calc_l1ss_info (link , parent_l1ss_cap , child_l1ss_cap );
635
+ if (link -> aspm_support & ASPM_STATE_L1_2_MASK )
636
+ aspm_calc_l12_info (link , parent_l1ss_cap , child_l1ss_cap );
621
637
}
622
638
623
639
static void pcie_aspm_cap_init (struct pcie_link_state * link , int blacklist )
@@ -1010,29 +1026,32 @@ void pcie_aspm_exit_link_state(struct pci_dev *pdev)
1010
1026
1011
1027
down_read (& pci_bus_sem );
1012
1028
mutex_lock (& aspm_lock );
1013
- /*
1014
- * All PCIe functions are in one slot, remove one function will remove
1015
- * the whole slot, so just wait until we are the last function left.
1016
- */
1017
- if (!list_empty (& parent -> subordinate -> devices ))
1018
- goto out ;
1019
1029
1020
1030
link = parent -> link_state ;
1021
1031
root = link -> root ;
1022
1032
parent_link = link -> parent ;
1023
1033
1024
- /* All functions are removed, so just disable ASPM for the link */
1034
+ /*
1035
+ * link->downstream is a pointer to the pci_dev of function 0. If
1036
+ * we remove that function, the pci_dev is about to be deallocated,
1037
+ * so we can't use link->downstream again. Free the link state to
1038
+ * avoid this.
1039
+ *
1040
+ * If we're removing a non-0 function, it's possible we could
1041
+ * retain the link state, but PCIe r6.0, sec 7.5.3.7, recommends
1042
+ * programming the same ASPM Control value for all functions of
1043
+ * multi-function devices, so disable ASPM for all of them.
1044
+ */
1025
1045
pcie_config_aspm_link (link , 0 );
1026
1046
list_del (& link -> sibling );
1027
- /* Clock PM is for endpoint device */
1028
1047
free_link_state (link );
1029
1048
1030
1049
/* Recheck latencies and configure upstream links */
1031
1050
if (parent_link ) {
1032
1051
pcie_update_aspm_capable (root );
1033
1052
pcie_config_aspm_path (parent_link );
1034
1053
}
1035
- out :
1054
+
1036
1055
mutex_unlock (& aspm_lock );
1037
1056
up_read (& pci_bus_sem );
1038
1057
}
@@ -1095,8 +1114,7 @@ static int __pci_disable_link_state(struct pci_dev *pdev, int state, bool sem)
1095
1114
if (state & PCIE_LINK_STATE_L0S )
1096
1115
link -> aspm_disable |= ASPM_STATE_L0S ;
1097
1116
if (state & PCIE_LINK_STATE_L1 )
1098
- /* L1 PM substates require L1 */
1099
- link -> aspm_disable |= ASPM_STATE_L1 | ASPM_STATE_L1SS ;
1117
+ link -> aspm_disable |= ASPM_STATE_L1 ;
1100
1118
if (state & PCIE_LINK_STATE_L1_1 )
1101
1119
link -> aspm_disable |= ASPM_STATE_L1_1 ;
1102
1120
if (state & PCIE_LINK_STATE_L1_2 )
@@ -1171,16 +1189,16 @@ int pci_enable_link_state(struct pci_dev *pdev, int state)
1171
1189
if (state & PCIE_LINK_STATE_L0S )
1172
1190
link -> aspm_default |= ASPM_STATE_L0S ;
1173
1191
if (state & PCIE_LINK_STATE_L1 )
1174
- /* L1 PM substates require L1 */
1175
- link -> aspm_default |= ASPM_STATE_L1 | ASPM_STATE_L1SS ;
1192
+ link -> aspm_default |= ASPM_STATE_L1 ;
1193
+ /* L1 PM substates require L1 */
1176
1194
if (state & PCIE_LINK_STATE_L1_1 )
1177
- link -> aspm_default |= ASPM_STATE_L1_1 ;
1195
+ link -> aspm_default |= ASPM_STATE_L1_1 | ASPM_STATE_L1 ;
1178
1196
if (state & PCIE_LINK_STATE_L1_2 )
1179
- link -> aspm_default |= ASPM_STATE_L1_2 ;
1197
+ link -> aspm_default |= ASPM_STATE_L1_2 | ASPM_STATE_L1 ;
1180
1198
if (state & PCIE_LINK_STATE_L1_1_PCIPM )
1181
- link -> aspm_default |= ASPM_STATE_L1_1_PCIPM ;
1199
+ link -> aspm_default |= ASPM_STATE_L1_1_PCIPM | ASPM_STATE_L1 ;
1182
1200
if (state & PCIE_LINK_STATE_L1_2_PCIPM )
1183
- link -> aspm_default |= ASPM_STATE_L1_2_PCIPM ;
1201
+ link -> aspm_default |= ASPM_STATE_L1_2_PCIPM | ASPM_STATE_L1 ;
1184
1202
pcie_config_aspm_link (link , policy_to_aspm_state (link ));
1185
1203
1186
1204
link -> clkpm_default = (state & PCIE_LINK_STATE_CLKPM ) ? 1 : 0 ;
0 commit comments