4141#define ASPM_STATE_ALL (ASPM_STATE_L0S | ASPM_STATE_L1 | \
4242 ASPM_STATE_L1SS)
4343
44- struct aspm_latency {
45- u32 l0s ; /* L0s latency (nsec) */
46- u32 l1 ; /* L1 latency (nsec) */
47- };
48-
4944struct pcie_link_state {
5045 struct pci_dev * pdev ; /* Upstream component of the Link */
5146 struct pci_dev * downstream ; /* Downstream component, function 0 */
@@ -65,15 +60,6 @@ struct pcie_link_state {
6560 u32 clkpm_enabled :1 ; /* Current Clock PM state */
6661 u32 clkpm_default :1 ; /* Default Clock PM state by BIOS */
6762 u32 clkpm_disable :1 ; /* Clock PM disabled */
68-
69- /* Exit latencies */
70- struct aspm_latency latency_up ; /* Upstream direction exit latency */
71- struct aspm_latency latency_dw ; /* Downstream direction exit latency */
72- /*
73- * Endpoint acceptable latencies. A pcie downstream port only
74- * has one slot under it, so at most there are 8 functions.
75- */
76- struct aspm_latency acceptable [8 ];
7763};
7864
7965static int aspm_disabled , aspm_force ;
@@ -105,6 +91,20 @@ static const char *policy_str[] = {
10591
10692#define LINK_RETRAIN_TIMEOUT HZ
10793
94+ /*
95+ * The L1 PM substate capability is only implemented in function 0 in a
96+ * multi function device.
97+ */
98+ static struct pci_dev * pci_function_0 (struct pci_bus * linkbus )
99+ {
100+ struct pci_dev * child ;
101+
102+ list_for_each_entry (child , & linkbus -> devices , bus_list )
103+ if (PCI_FUNC (child -> devfn ) == 0 )
104+ return child ;
105+ return NULL ;
106+ }
107+
108108static int policy_to_aspm_state (struct pcie_link_state * link )
109109{
110110 switch (aspm_policy ) {
@@ -378,8 +378,10 @@ static void encode_l12_threshold(u32 threshold_us, u32 *scale, u32 *value)
378378
379379static void pcie_aspm_check_latency (struct pci_dev * endpoint )
380380{
381- u32 latency , l1_switch_latency = 0 ;
382- struct aspm_latency * acceptable ;
381+ u32 latency , encoding , lnkcap_up , lnkcap_dw ;
382+ u32 l1_switch_latency = 0 , latency_up_l0s ;
383+ u32 latency_up_l1 , latency_dw_l0s , latency_dw_l1 ;
384+ u32 acceptable_l0s , acceptable_l1 ;
383385 struct pcie_link_state * link ;
384386
385387 /* Device not in D0 doesn't need latency check */
@@ -388,17 +390,36 @@ static void pcie_aspm_check_latency(struct pci_dev *endpoint)
388390 return ;
389391
390392 link = endpoint -> bus -> self -> link_state ;
391- acceptable = & link -> acceptable [PCI_FUNC (endpoint -> devfn )];
393+
394+ /* Calculate endpoint L0s acceptable latency */
395+ encoding = (endpoint -> devcap & PCI_EXP_DEVCAP_L0S ) >> 6 ;
396+ acceptable_l0s = calc_l0s_acceptable (encoding );
397+
398+ /* Calculate endpoint L1 acceptable latency */
399+ encoding = (endpoint -> devcap & PCI_EXP_DEVCAP_L1 ) >> 9 ;
400+ acceptable_l1 = calc_l1_acceptable (encoding );
392401
393402 while (link ) {
403+ struct pci_dev * dev = pci_function_0 (link -> pdev -> subordinate );
404+
405+ /* Read direction exit latencies */
406+ pcie_capability_read_dword (link -> pdev , PCI_EXP_LNKCAP ,
407+ & lnkcap_up );
408+ pcie_capability_read_dword (dev , PCI_EXP_LNKCAP ,
409+ & lnkcap_dw );
410+ latency_up_l0s = calc_l0s_latency (lnkcap_up );
411+ latency_up_l1 = calc_l1_latency (lnkcap_up );
412+ latency_dw_l0s = calc_l0s_latency (lnkcap_dw );
413+ latency_dw_l1 = calc_l1_latency (lnkcap_dw );
414+
394415 /* Check upstream direction L0s latency */
395416 if ((link -> aspm_capable & ASPM_STATE_L0S_UP ) &&
396- (link -> latency_up . l0s > acceptable -> l0s ))
417+ (latency_up_l0s > acceptable_l0s ))
397418 link -> aspm_capable &= ~ASPM_STATE_L0S_UP ;
398419
399420 /* Check downstream direction L0s latency */
400421 if ((link -> aspm_capable & ASPM_STATE_L0S_DW ) &&
401- (link -> latency_dw . l0s > acceptable -> l0s ))
422+ (latency_dw_l0s > acceptable_l0s ))
402423 link -> aspm_capable &= ~ASPM_STATE_L0S_DW ;
403424 /*
404425 * Check L1 latency.
@@ -413,30 +434,16 @@ static void pcie_aspm_check_latency(struct pci_dev *endpoint)
413434 * L1 exit latencies advertised by a device include L1
414435 * substate latencies (and hence do not do any check).
415436 */
416- latency = max_t (u32 , link -> latency_up . l1 , link -> latency_dw . l1 );
437+ latency = max_t (u32 , latency_up_l1 , latency_dw_l1 );
417438 if ((link -> aspm_capable & ASPM_STATE_L1 ) &&
418- (latency + l1_switch_latency > acceptable -> l1 ))
439+ (latency + l1_switch_latency > acceptable_l1 ))
419440 link -> aspm_capable &= ~ASPM_STATE_L1 ;
420441 l1_switch_latency += 1000 ;
421442
422443 link = link -> parent ;
423444 }
424445}
425446
426- /*
427- * The L1 PM substate capability is only implemented in function 0 in a
428- * multi function device.
429- */
430- static struct pci_dev * pci_function_0 (struct pci_bus * linkbus )
431- {
432- struct pci_dev * child ;
433-
434- list_for_each_entry (child , & linkbus -> devices , bus_list )
435- if (PCI_FUNC (child -> devfn ) == 0 )
436- return child ;
437- return NULL ;
438- }
439-
440447static void pci_clear_and_set_dword (struct pci_dev * pdev , int pos ,
441448 u32 clear , u32 set )
442449{
@@ -593,17 +600,13 @@ static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist)
593600 link -> aspm_enabled |= ASPM_STATE_L0S_UP ;
594601 if (parent_lnkctl & PCI_EXP_LNKCTL_ASPM_L0S )
595602 link -> aspm_enabled |= ASPM_STATE_L0S_DW ;
596- link -> latency_up .l0s = calc_l0s_latency (parent_lnkcap );
597- link -> latency_dw .l0s = calc_l0s_latency (child_lnkcap );
598603
599604 /* Setup L1 state */
600605 if (parent_lnkcap & child_lnkcap & PCI_EXP_LNKCAP_ASPM_L1 )
601606 link -> aspm_support |= ASPM_STATE_L1 ;
602607
603608 if (parent_lnkctl & child_lnkctl & PCI_EXP_LNKCTL_ASPM_L1 )
604609 link -> aspm_enabled |= ASPM_STATE_L1 ;
605- link -> latency_up .l1 = calc_l1_latency (parent_lnkcap );
606- link -> latency_dw .l1 = calc_l1_latency (child_lnkcap );
607610
608611 /* Setup L1 substate */
609612 pci_read_config_dword (parent , parent -> l1ss + PCI_L1SS_CAP ,
@@ -660,22 +663,10 @@ static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist)
660663
661664 /* Get and check endpoint acceptable latencies */
662665 list_for_each_entry (child , & linkbus -> devices , bus_list ) {
663- u32 reg32 , encoding ;
664- struct aspm_latency * acceptable =
665- & link -> acceptable [PCI_FUNC (child -> devfn )];
666-
667666 if (pci_pcie_type (child ) != PCI_EXP_TYPE_ENDPOINT &&
668667 pci_pcie_type (child ) != PCI_EXP_TYPE_LEG_END )
669668 continue ;
670669
671- pcie_capability_read_dword (child , PCI_EXP_DEVCAP , & reg32 );
672- /* Calculate endpoint L0s acceptable latency */
673- encoding = (reg32 & PCI_EXP_DEVCAP_L0S ) >> 6 ;
674- acceptable -> l0s = calc_l0s_acceptable (encoding );
675- /* Calculate endpoint L1 acceptable latency */
676- encoding = (reg32 & PCI_EXP_DEVCAP_L1 ) >> 9 ;
677- acceptable -> l1 = calc_l1_acceptable (encoding );
678-
679670 pcie_aspm_check_latency (child );
680671 }
681672}
0 commit comments