7
7
* Copyright (C) Shaohua Li ([email protected] )
8
8
*/
9
9
10
+ #include <linux/bitfield.h>
10
11
#include <linux/kernel.h>
12
+ #include <linux/limits.h>
11
13
#include <linux/math.h>
12
14
#include <linux/module.h>
13
15
#include <linux/moduleparam.h>
16
18
#include <linux/errno.h>
17
19
#include <linux/pm.h>
18
20
#include <linux/init.h>
21
+ #include <linux/printk.h>
19
22
#include <linux/slab.h>
20
- #include <linux/jiffies .h>
21
- #include <linux/delay.h>
23
+ #include <linux/time .h>
24
+
22
25
#include "../pci.h"
23
26
24
27
#ifdef MODULE_PARAM_PREFIX
@@ -267,37 +270,37 @@ static void pcie_aspm_configure_common_clock(struct pcie_link_state *link)
267
270
/* Convert L0s latency encoding to ns */
268
271
static u32 calc_l0s_latency (u32 lnkcap )
269
272
{
270
- u32 encoding = ( lnkcap & PCI_EXP_LNKCAP_L0SEL ) >> 12 ;
273
+ u32 encoding = FIELD_GET ( PCI_EXP_LNKCAP_L0SEL , lnkcap ) ;
271
274
272
275
if (encoding == 0x7 )
273
- return ( 5 * 1000 ) ; /* > 4us */
276
+ return 5 * NSEC_PER_USEC ; /* > 4us */
274
277
return (64 << encoding );
275
278
}
276
279
277
280
/* Convert L0s acceptable latency encoding to ns */
278
281
static u32 calc_l0s_acceptable (u32 encoding )
279
282
{
280
283
if (encoding == 0x7 )
281
- return -1U ;
284
+ return U32_MAX ;
282
285
return (64 << encoding );
283
286
}
284
287
285
288
/* Convert L1 latency encoding to ns */
286
289
static u32 calc_l1_latency (u32 lnkcap )
287
290
{
288
- u32 encoding = ( lnkcap & PCI_EXP_LNKCAP_L1EL ) >> 15 ;
291
+ u32 encoding = FIELD_GET ( PCI_EXP_LNKCAP_L1EL , lnkcap ) ;
289
292
290
293
if (encoding == 0x7 )
291
- return ( 65 * 1000 ) ; /* > 64us */
292
- return ( 1000 << encoding ) ;
294
+ return 65 * NSEC_PER_USEC ; /* > 64us */
295
+ return NSEC_PER_USEC << encoding ;
293
296
}
294
297
295
298
/* Convert L1 acceptable latency encoding to ns */
296
299
static u32 calc_l1_acceptable (u32 encoding )
297
300
{
298
301
if (encoding == 0x7 )
299
- return -1U ;
300
- return ( 1000 << encoding ) ;
302
+ return U32_MAX ;
303
+ return NSEC_PER_USEC << encoding ;
301
304
}
302
305
303
306
/* Convert L1SS T_pwr encoding to usec */
@@ -325,33 +328,33 @@ static u32 calc_l12_pwron(struct pci_dev *pdev, u32 scale, u32 val)
325
328
*/
326
329
static void encode_l12_threshold (u32 threshold_us , u32 * scale , u32 * value )
327
330
{
328
- u64 threshold_ns = (u64 ) threshold_us * 1000 ;
331
+ u64 threshold_ns = (u64 )threshold_us * NSEC_PER_USEC ;
329
332
330
333
/*
331
334
* LTR_L1.2_THRESHOLD_Value ("value") is a 10-bit field with max
332
335
* value of 0x3ff.
333
336
*/
334
- if (threshold_ns <= 0x3ff * 1 ) {
337
+ if (threshold_ns <= 1 * FIELD_MAX ( PCI_L1SS_CTL1_LTR_L12_TH_VALUE ) ) {
335
338
* scale = 0 ; /* Value times 1ns */
336
339
* value = threshold_ns ;
337
- } else if (threshold_ns <= 0x3ff * 32 ) {
340
+ } else if (threshold_ns <= 32 * FIELD_MAX ( PCI_L1SS_CTL1_LTR_L12_TH_VALUE ) ) {
338
341
* scale = 1 ; /* Value times 32ns */
339
342
* value = roundup (threshold_ns , 32 ) / 32 ;
340
- } else if (threshold_ns <= 0x3ff * 1024 ) {
343
+ } else if (threshold_ns <= 1024 * FIELD_MAX ( PCI_L1SS_CTL1_LTR_L12_TH_VALUE ) ) {
341
344
* scale = 2 ; /* Value times 1024ns */
342
345
* value = roundup (threshold_ns , 1024 ) / 1024 ;
343
- } else if (threshold_ns <= 0x3ff * 32768 ) {
346
+ } else if (threshold_ns <= 32768 * FIELD_MAX ( PCI_L1SS_CTL1_LTR_L12_TH_VALUE ) ) {
344
347
* scale = 3 ; /* Value times 32768ns */
345
348
* value = roundup (threshold_ns , 32768 ) / 32768 ;
346
- } else if (threshold_ns <= 0x3ff * 1048576 ) {
349
+ } else if (threshold_ns <= 1048576 * FIELD_MAX ( PCI_L1SS_CTL1_LTR_L12_TH_VALUE ) ) {
347
350
* scale = 4 ; /* Value times 1048576ns */
348
351
* value = roundup (threshold_ns , 1048576 ) / 1048576 ;
349
- } else if (threshold_ns <= 0x3ff * ( u64 ) 33554432 ) {
352
+ } else if (threshold_ns <= ( u64 ) 33554432 * FIELD_MAX ( PCI_L1SS_CTL1_LTR_L12_TH_VALUE ) ) {
350
353
* scale = 5 ; /* Value times 33554432ns */
351
354
* value = roundup (threshold_ns , 33554432 ) / 33554432 ;
352
355
} else {
353
356
* scale = 5 ;
354
- * value = 0x3ff ; /* Max representable value */
357
+ * value = FIELD_MAX ( PCI_L1SS_CTL1_LTR_L12_TH_VALUE );
355
358
}
356
359
}
357
360
@@ -371,11 +374,11 @@ static void pcie_aspm_check_latency(struct pci_dev *endpoint)
371
374
link = endpoint -> bus -> self -> link_state ;
372
375
373
376
/* Calculate endpoint L0s acceptable latency */
374
- encoding = ( endpoint -> devcap & PCI_EXP_DEVCAP_L0S ) >> 6 ;
377
+ encoding = FIELD_GET ( PCI_EXP_DEVCAP_L0S , endpoint -> devcap ) ;
375
378
acceptable_l0s = calc_l0s_acceptable (encoding );
376
379
377
380
/* Calculate endpoint L1 acceptable latency */
378
- encoding = ( endpoint -> devcap & PCI_EXP_DEVCAP_L1 ) >> 9 ;
381
+ encoding = FIELD_GET ( PCI_EXP_DEVCAP_L1 , endpoint -> devcap ) ;
379
382
acceptable_l1 = calc_l1_acceptable (encoding );
380
383
381
384
while (link ) {
@@ -417,7 +420,7 @@ static void pcie_aspm_check_latency(struct pci_dev *endpoint)
417
420
if ((link -> aspm_capable & ASPM_STATE_L1 ) &&
418
421
(latency + l1_switch_latency > acceptable_l1 ))
419
422
link -> aspm_capable &= ~ASPM_STATE_L1 ;
420
- l1_switch_latency += 1000 ;
423
+ l1_switch_latency += NSEC_PER_USEC ;
421
424
422
425
link = link -> parent ;
423
426
}
@@ -446,22 +449,24 @@ static void aspm_calc_l12_info(struct pcie_link_state *link,
446
449
u32 pl1_2_enables , cl1_2_enables ;
447
450
448
451
/* Choose the greater of the two Port Common_Mode_Restore_Times */
449
- val1 = ( parent_l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME ) >> 8 ;
450
- val2 = ( child_l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME ) >> 8 ;
452
+ val1 = FIELD_GET ( PCI_L1SS_CAP_CM_RESTORE_TIME , parent_l1ss_cap ) ;
453
+ val2 = FIELD_GET ( PCI_L1SS_CAP_CM_RESTORE_TIME , child_l1ss_cap ) ;
451
454
t_common_mode = max (val1 , val2 );
452
455
453
456
/* Choose the greater of the two Port T_POWER_ON times */
454
- val1 = ( parent_l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_VALUE ) >> 19 ;
455
- scale1 = ( parent_l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_SCALE ) >> 16 ;
456
- val2 = ( child_l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_VALUE ) >> 19 ;
457
- scale2 = ( child_l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_SCALE ) >> 16 ;
457
+ val1 = FIELD_GET ( PCI_L1SS_CAP_P_PWR_ON_VALUE , parent_l1ss_cap ) ;
458
+ scale1 = FIELD_GET ( PCI_L1SS_CAP_P_PWR_ON_SCALE , parent_l1ss_cap ) ;
459
+ val2 = FIELD_GET ( PCI_L1SS_CAP_P_PWR_ON_VALUE , child_l1ss_cap ) ;
460
+ scale2 = FIELD_GET ( PCI_L1SS_CAP_P_PWR_ON_SCALE , child_l1ss_cap ) ;
458
461
459
462
if (calc_l12_pwron (parent , scale1 , val1 ) >
460
463
calc_l12_pwron (child , scale2 , val2 )) {
461
- ctl2 |= scale1 | (val1 << 3 );
464
+ ctl2 |= FIELD_PREP (PCI_L1SS_CTL2_T_PWR_ON_SCALE , scale1 ) |
465
+ FIELD_PREP (PCI_L1SS_CTL2_T_PWR_ON_VALUE , val1 );
462
466
t_power_on = calc_l12_pwron (parent , scale1 , val1 );
463
467
} else {
464
- ctl2 |= scale2 | (val2 << 3 );
468
+ ctl2 |= FIELD_PREP (PCI_L1SS_CTL2_T_PWR_ON_SCALE , scale2 ) |
469
+ FIELD_PREP (PCI_L1SS_CTL2_T_PWR_ON_VALUE , val2 );
465
470
t_power_on = calc_l12_pwron (child , scale2 , val2 );
466
471
}
467
472
@@ -477,7 +482,9 @@ static void aspm_calc_l12_info(struct pcie_link_state *link,
477
482
*/
478
483
l1_2_threshold = 2 + 4 + t_common_mode + t_power_on ;
479
484
encode_l12_threshold (l1_2_threshold , & scale , & value );
480
- ctl1 |= t_common_mode << 8 | scale << 29 | value << 16 ;
485
+ ctl1 |= FIELD_PREP (PCI_L1SS_CTL1_CM_RESTORE_TIME , t_common_mode ) |
486
+ FIELD_PREP (PCI_L1SS_CTL1_LTR_L12_TH_VALUE , value ) |
487
+ FIELD_PREP (PCI_L1SS_CTL1_LTR_L12_TH_SCALE , scale );
481
488
482
489
/* Some broken devices only support dword access to L1 SS */
483
490
pci_read_config_dword (parent , parent -> l1ss + PCI_L1SS_CTL1 , & pctl1 );
@@ -1059,7 +1066,8 @@ static int __pci_disable_link_state(struct pci_dev *pdev, int state, bool sem)
1059
1066
if (state & PCIE_LINK_STATE_L0S )
1060
1067
link -> aspm_disable |= ASPM_STATE_L0S ;
1061
1068
if (state & PCIE_LINK_STATE_L1 )
1062
- link -> aspm_disable |= ASPM_STATE_L1 ;
1069
+ /* L1 PM substates require L1 */
1070
+ link -> aspm_disable |= ASPM_STATE_L1 | ASPM_STATE_L1SS ;
1063
1071
if (state & PCIE_LINK_STATE_L1_1 )
1064
1072
link -> aspm_disable |= ASPM_STATE_L1_1 ;
1065
1073
if (state & PCIE_LINK_STATE_L1_2 )
@@ -1247,6 +1255,8 @@ static ssize_t aspm_attr_store_common(struct device *dev,
1247
1255
link -> aspm_disable &= ~ASPM_STATE_L1 ;
1248
1256
} else {
1249
1257
link -> aspm_disable |= state ;
1258
+ if (state & ASPM_STATE_L1 )
1259
+ link -> aspm_disable |= ASPM_STATE_L1SS ;
1250
1260
}
1251
1261
1252
1262
pcie_config_aspm_link (link , policy_to_aspm_state (link ));
@@ -1361,10 +1371,10 @@ static int __init pcie_aspm_disable(char *str)
1361
1371
aspm_policy = POLICY_DEFAULT ;
1362
1372
aspm_disabled = 1 ;
1363
1373
aspm_support_enabled = false;
1364
- printk ( KERN_INFO "PCIe ASPM is disabled\n" );
1374
+ pr_info ( "PCIe ASPM is disabled\n" );
1365
1375
} else if (!strcmp (str , "force" )) {
1366
1376
aspm_force = 1 ;
1367
- printk ( KERN_INFO "PCIe ASPM is forcibly enabled\n" );
1377
+ pr_info ( "PCIe ASPM is forcibly enabled\n" );
1368
1378
}
1369
1379
return 1 ;
1370
1380
}
0 commit comments