mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v6] mfd: Add support for RTS5250S power saving
@ 2017-09-07  8:26 rui_feng
  2017-09-07 10:57 ` Lee Jones
  2017-12-14 22:25 ` Bjorn Helgaas
  0 siblings, 2 replies; 11+ messages in thread
From: rui_feng @ 2017-09-07  8:26 UTC (permalink / raw)
  To: lee.jones, linux-kernel; +Cc: Rui Feng

From: Rui Feng <rui_feng@realsil.com.cn>

Enable power saving for RTS5250S as following steps:
1.Set 0xFE58 to enable clock power management.
2.Check cfg space whether support L1SS or not.
3.If support L1SS, set 0xFF03 to free clkreq.
4.When entering idle status, enable aspm
  and set parameters for L1SS and LTR.
5.Wnen entering run status, disable aspm
  and set parameters for L1SS and LTR.
If entering L1SS mode successfully,
electric current will be below 2mA.

Signed-off-by: Rui Feng <rui_feng@realsil.com.cn>
---
 drivers/mfd/rts5249.c        | 155 ++++++++++++++++++++++++++++++++++++++++++-
 drivers/mfd/rtsx_pcr.c       | 142 +++++++++++++++++++++++++++++++++++++--
 drivers/mfd/rtsx_pcr.h       |  14 ++++
 include/linux/mfd/rtsx_pci.h |  84 +++++++++++++++++++++++
 4 files changed, 389 insertions(+), 6 deletions(-)

diff --git a/drivers/mfd/rts5249.c b/drivers/mfd/rts5249.c
index 40f8bb1..7fcf37b 100644
--- a/drivers/mfd/rts5249.c
+++ b/drivers/mfd/rts5249.c
@@ -103,8 +103,64 @@ static void rtsx_base_force_power_down(struct rtsx_pcr *pcr, u8 pm_state)
 	rtsx_pci_write_register(pcr, FPDCTL, 0x03, 0x03);
 }
 
+static void rts5249_init_from_cfg(struct rtsx_pcr *pcr)
+{
+	struct rtsx_cr_option *option = &(pcr->option);
+	u32 lval;
+
+	if (CHK_PCI_PID(pcr, PID_524A))
+		rtsx_pci_read_config_dword(pcr,
+			PCR_ASPM_SETTING_REG1, &lval);
+	else
+		rtsx_pci_read_config_dword(pcr,
+			PCR_ASPM_SETTING_REG2, &lval);
+
+	if (lval & ASPM_L1_1_EN_MASK)
+		rtsx_set_dev_flag(pcr, ASPM_L1_1_EN);
+
+	if (lval & ASPM_L1_2_EN_MASK)
+		rtsx_set_dev_flag(pcr, ASPM_L1_2_EN);
+
+	if (lval & PM_L1_1_EN_MASK)
+		rtsx_set_dev_flag(pcr, PM_L1_1_EN);
+
+	if (lval & PM_L1_2_EN_MASK)
+		rtsx_set_dev_flag(pcr, PM_L1_2_EN);
+
+	if (option->ltr_en) {
+		u16 val;
+
+		pcie_capability_read_word(pcr->pci, PCI_EXP_DEVCTL2, &val);
+		if (val & PCI_EXP_DEVCTL2_LTR_EN) {
+			option->ltr_enabled = true;
+			option->ltr_active = true;
+			rtsx_set_ltr_latency(pcr, option->ltr_active_latency);
+		} else {
+			option->ltr_enabled = false;
+		}
+	}
+}
+
+static int rts5249_init_from_hw(struct rtsx_pcr *pcr)
+{
+	struct rtsx_cr_option *option = &(pcr->option);
+
+	if (rtsx_check_dev_flag(pcr, ASPM_L1_1_EN | ASPM_L1_2_EN
+				| PM_L1_1_EN | PM_L1_2_EN))
+		option->force_clkreq_0 = false;
+	else
+		option->force_clkreq_0 = true;
+
+	return 0;
+}
+
 static int rts5249_extra_init_hw(struct rtsx_pcr *pcr)
 {
+	struct rtsx_cr_option *option = &(pcr->option);
+
+	rts5249_init_from_cfg(pcr);
+	rts5249_init_from_hw(pcr);
+
 	rtsx_pci_init_cmd(pcr);
 
 	/* Rest L1SUB Config */
@@ -125,7 +181,18 @@ static int rts5249_extra_init_hw(struct rtsx_pcr *pcr)
 	else
 		rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PETXCFG, 0xB0, 0x80);
 
-	return rtsx_pci_send_cmd(pcr, 100);
+	/*
+	 * If u_force_clkreq_0 is enabled, CLKREQ# PIN will be forced
+	 * to drive low, and we forcibly request clock.
+	 */
+	if (option->force_clkreq_0)
+		rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PETXCFG,
+			FORCE_CLKREQ_DELINK_MASK, FORCE_CLKREQ_LOW);
+	else
+		rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PETXCFG,
+			FORCE_CLKREQ_DELINK_MASK, FORCE_CLKREQ_HIGH);
+
+	return rtsx_pci_send_cmd(pcr, CMD_TIMEOUT_DEF);
 }
 
 static int rts5249_optimize_phy(struct rtsx_pcr *pcr)
@@ -285,6 +352,31 @@ static int rtsx_base_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
 	return rtsx_pci_send_cmd(pcr, 100);
 }
 
+static void rts5249_set_aspm(struct rtsx_pcr *pcr, bool enable)
+{
+	struct rtsx_cr_option *option = &pcr->option;
+	u8 val = 0;
+
+	if (pcr->aspm_enabled == enable)
+		return;
+
+	if (option->dev_aspm_mode == DEV_ASPM_DYNAMIC) {
+		if (enable)
+			val = pcr->aspm_en;
+		rtsx_pci_update_cfg_byte(pcr,
+			pcr->pcie_cap + PCI_EXP_LNKCTL,
+			ASPM_MASK_NEG, val);
+	} else if (option->dev_aspm_mode == DEV_ASPM_BACKDOOR) {
+		u8 mask = FORCE_ASPM_VAL_MASK | FORCE_ASPM_CTL0;
+
+		if (!enable)
+			val = FORCE_ASPM_CTL0;
+		rtsx_pci_write_register(pcr, ASPM_FORCE_CTL, mask, val);
+	}
+
+	pcr->aspm_enabled = enable;
+}
+
 static const struct pcr_ops rts5249_pcr_ops = {
 	.fetch_vendor_settings = rtsx_base_fetch_vendor_settings,
 	.extra_init_hw = rts5249_extra_init_hw,
@@ -297,6 +389,7 @@ static int rtsx_base_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
 	.card_power_off = rtsx_base_card_power_off,
 	.switch_output_voltage = rtsx_base_switch_output_voltage,
 	.force_power_down = rtsx_base_force_power_down,
+	.set_aspm = rts5249_set_aspm,
 };
 
 /* SD Pull Control Enable:
@@ -353,6 +446,8 @@ static int rtsx_base_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
 
 void rts5249_init_params(struct rtsx_pcr *pcr)
 {
+	struct rtsx_cr_option *option = &(pcr->option);
+
 	pcr->extra_caps = EXTRA_CAPS_SD_SDR50 | EXTRA_CAPS_SD_SDR104;
 	pcr->num_slots = 2;
 	pcr->ops = &rts5249_pcr_ops;
@@ -372,6 +467,20 @@ void rts5249_init_params(struct rtsx_pcr *pcr)
 	pcr->ms_pull_ctl_disable_tbl = rts5249_ms_pull_ctl_disable_tbl;
 
 	pcr->reg_pm_ctrl3 = PM_CTRL3;
+
+	option->dev_flags = (LTR_L1SS_PWR_GATE_CHECK_CARD_EN
+				| LTR_L1SS_PWR_GATE_EN);
+	option->ltr_en = true;
+
+	/* Init latency of active, idle, L1OFF to 60us, 300us, 3ms */
+	option->ltr_active_latency = LTR_ACTIVE_LATENCY_DEF;
+	option->ltr_idle_latency = LTR_IDLE_LATENCY_DEF;
+	option->ltr_l1off_latency = LTR_L1OFF_LATENCY_DEF;
+	option->dev_aspm_mode = DEV_ASPM_DYNAMIC;
+	option->l1_snooze_delay = L1_SNOOZE_DELAY_DEF;
+	option->ltr_l1off_sspwrgate = LTR_L1OFF_SSPWRGATE_5249_DEF;
+	option->ltr_l1off_snooze_sspwrgate =
+		LTR_L1OFF_SNOOZE_SSPWRGATE_5249_DEF;
 }
 
 static int rts524a_write_phy(struct rtsx_pcr *pcr, u8 addr, u16 val)
@@ -459,6 +568,40 @@ static int rts524a_extra_init_hw(struct rtsx_pcr *pcr)
 	return 0;
 }
 
+static void rts5250_set_l1off_cfg_sub_d0(struct rtsx_pcr *pcr, int active)
+{
+	struct rtsx_cr_option *option = &(pcr->option);
+
+	u32 interrupt = rtsx_pci_readl(pcr, RTSX_BIPR);
+	int card_exist = (interrupt & SD_EXIST) | (interrupt & MS_EXIST);
+	int aspm_L1_1, aspm_L1_2;
+	u8 val = 0;
+
+	aspm_L1_1 = rtsx_check_dev_flag(pcr, ASPM_L1_1_EN);
+	aspm_L1_2 = rtsx_check_dev_flag(pcr, ASPM_L1_2_EN);
+
+	if (active) {
+		/* Run, latency: 60us */
+		if (aspm_L1_1)
+			val = option->ltr_l1off_snooze_sspwrgate;
+	} else {
+		/* L1off, latency: 300us */
+		if (aspm_L1_2)
+			val = option->ltr_l1off_sspwrgate;
+	}
+
+	if (aspm_L1_1 || aspm_L1_2) {
+		if (rtsx_check_dev_flag(pcr,
+					LTR_L1SS_PWR_GATE_CHECK_CARD_EN)) {
+			if (card_exist)
+				val &= ~L1OFF_MBIAS2_EN_5250;
+			else
+				val |= L1OFF_MBIAS2_EN_5250;
+		}
+	}
+	rtsx_set_l1off_sub(pcr, val);
+}
+
 static const struct pcr_ops rts524a_pcr_ops = {
 	.write_phy = rts524a_write_phy,
 	.read_phy = rts524a_read_phy,
@@ -473,11 +616,16 @@ static int rts524a_extra_init_hw(struct rtsx_pcr *pcr)
 	.card_power_off = rtsx_base_card_power_off,
 	.switch_output_voltage = rtsx_base_switch_output_voltage,
 	.force_power_down = rtsx_base_force_power_down,
+	.set_l1off_cfg_sub_d0 = rts5250_set_l1off_cfg_sub_d0,
+	.set_aspm = rts5249_set_aspm,
 };
 
 void rts524a_init_params(struct rtsx_pcr *pcr)
 {
 	rts5249_init_params(pcr);
+	pcr->option.ltr_l1off_sspwrgate = LTR_L1OFF_SSPWRGATE_5250_DEF;
+	pcr->option.ltr_l1off_snooze_sspwrgate =
+		LTR_L1OFF_SNOOZE_SSPWRGATE_5250_DEF;
 
 	pcr->reg_pm_ctrl3 = RTS524A_PM_CTRL3;
 	pcr->ops = &rts524a_pcr_ops;
@@ -576,11 +724,16 @@ static int rts525a_extra_init_hw(struct rtsx_pcr *pcr)
 	.card_power_off = rtsx_base_card_power_off,
 	.switch_output_voltage = rts525a_switch_output_voltage,
 	.force_power_down = rtsx_base_force_power_down,
+	.set_l1off_cfg_sub_d0 = rts5250_set_l1off_cfg_sub_d0,
+	.set_aspm = rts5249_set_aspm,
 };
 
 void rts525a_init_params(struct rtsx_pcr *pcr)
 {
 	rts5249_init_params(pcr);
+	pcr->option.ltr_l1off_sspwrgate = LTR_L1OFF_SSPWRGATE_5250_DEF;
+	pcr->option.ltr_l1off_snooze_sspwrgate =
+		LTR_L1OFF_SNOOZE_SSPWRGATE_5250_DEF;
 
 	pcr->reg_pm_ctrl3 = RTS524A_PM_CTRL3;
 	pcr->ops = &rts525a_pcr_ops;
diff --git a/drivers/mfd/rtsx_pcr.c b/drivers/mfd/rtsx_pcr.c
index a0ac89d..7c87485 100644
--- a/drivers/mfd/rtsx_pcr.c
+++ b/drivers/mfd/rtsx_pcr.c
@@ -79,6 +79,96 @@ static inline void rtsx_pci_disable_aspm(struct rtsx_pcr *pcr)
 		0xFC, 0);
 }
 
+int rtsx_comm_set_ltr_latency(struct rtsx_pcr *pcr, u32 latency)
+{
+	rtsx_pci_write_register(pcr, MSGTXDATA0,
+				MASK_8_BIT_DEF, (u8) (latency & 0xFF));
+	rtsx_pci_write_register(pcr, MSGTXDATA1,
+				MASK_8_BIT_DEF, (u8)((latency >> 8) & 0xFF));
+	rtsx_pci_write_register(pcr, MSGTXDATA2,
+				MASK_8_BIT_DEF, (u8)((latency >> 16) & 0xFF));
+	rtsx_pci_write_register(pcr, MSGTXDATA3,
+				MASK_8_BIT_DEF, (u8)((latency >> 24) & 0xFF));
+	rtsx_pci_write_register(pcr, LTR_CTL, LTR_TX_EN_MASK |
+		LTR_LATENCY_MODE_MASK, LTR_TX_EN_1 | LTR_LATENCY_MODE_SW);
+
+	return 0;
+}
+
+int rtsx_set_ltr_latency(struct rtsx_pcr *pcr, u32 latency)
+{
+	if (pcr->ops->set_ltr_latency)
+		return pcr->ops->set_ltr_latency(pcr, latency);
+	else
+		return rtsx_comm_set_ltr_latency(pcr, latency);
+}
+
+static void rtsx_comm_set_aspm(struct rtsx_pcr *pcr, bool enable)
+{
+	struct rtsx_cr_option *option = &pcr->option;
+
+	if (pcr->aspm_enabled == enable)
+		return;
+
+	if (option->dev_aspm_mode == DEV_ASPM_DYNAMIC) {
+		if (enable)
+			rtsx_pci_enable_aspm(pcr);
+		else
+			rtsx_pci_disable_aspm(pcr);
+	} else if (option->dev_aspm_mode == DEV_ASPM_BACKDOOR) {
+		u8 mask = FORCE_ASPM_VAL_MASK;
+		u8 val = 0;
+
+		if (enable)
+			val = pcr->aspm_en;
+		rtsx_pci_write_register(pcr, ASPM_FORCE_CTL,  mask, val);
+	}
+
+	pcr->aspm_enabled = enable;
+}
+
+static void rtsx_disable_aspm(struct rtsx_pcr *pcr)
+{
+	if (pcr->ops->set_aspm)
+		pcr->ops->set_aspm(pcr, false);
+	else
+		rtsx_comm_set_aspm(pcr, false);
+}
+
+int rtsx_set_l1off_sub(struct rtsx_pcr *pcr, u8 val)
+{
+	rtsx_pci_write_register(pcr, L1SUB_CONFIG3, 0xFF, val);
+
+	return 0;
+}
+
+void rtsx_set_l1off_sub_cfg_d0(struct rtsx_pcr *pcr, int active)
+{
+	if (pcr->ops->set_l1off_cfg_sub_d0)
+		pcr->ops->set_l1off_cfg_sub_d0(pcr, active);
+}
+
+static void rtsx_comm_pm_full_on(struct rtsx_pcr *pcr)
+{
+	struct rtsx_cr_option *option = &pcr->option;
+
+	rtsx_disable_aspm(pcr);
+
+	if (option->ltr_enabled)
+		rtsx_set_ltr_latency(pcr, option->ltr_active_latency);
+
+	if (rtsx_check_dev_flag(pcr, LTR_L1SS_PWR_GATE_EN))
+		rtsx_set_l1off_sub_cfg_d0(pcr, 1);
+}
+
+void rtsx_pm_full_on(struct rtsx_pcr *pcr)
+{
+	if (pcr->ops->full_on)
+		pcr->ops->full_on(pcr);
+	else
+		rtsx_comm_pm_full_on(pcr);
+}
+
 void rtsx_pci_start_run(struct rtsx_pcr *pcr)
 {
 	/* If pci device removed, don't queue idle work any more */
@@ -89,9 +179,7 @@ void rtsx_pci_start_run(struct rtsx_pcr *pcr)
 		pcr->state = PDEV_STAT_RUN;
 		if (pcr->ops->enable_auto_blink)
 			pcr->ops->enable_auto_blink(pcr);
-
-		if (pcr->aspm_en)
-			rtsx_pci_disable_aspm(pcr);
+		rtsx_pm_full_on(pcr);
 	}
 
 	mod_delayed_work(system_wq, &pcr->idle_work, msecs_to_jiffies(200));
@@ -958,6 +1046,41 @@ static int rtsx_pci_acquire_irq(struct rtsx_pcr *pcr)
 	return 0;
 }
 
+static void rtsx_enable_aspm(struct rtsx_pcr *pcr)
+{
+	if (pcr->ops->set_aspm)
+		pcr->ops->set_aspm(pcr, true);
+	else
+		rtsx_comm_set_aspm(pcr, true);
+}
+
+static void rtsx_comm_pm_power_saving(struct rtsx_pcr *pcr)
+{
+	struct rtsx_cr_option *option = &pcr->option;
+
+	if (option->ltr_enabled) {
+		u32 latency = option->ltr_l1off_latency;
+
+		if (rtsx_check_dev_flag(pcr, L1_SNOOZE_TEST_EN))
+			mdelay(option->l1_snooze_delay);
+
+		rtsx_set_ltr_latency(pcr, latency);
+	}
+
+	if (rtsx_check_dev_flag(pcr, LTR_L1SS_PWR_GATE_EN))
+		rtsx_set_l1off_sub_cfg_d0(pcr, 0);
+
+	rtsx_enable_aspm(pcr);
+}
+
+void rtsx_pm_power_saving(struct rtsx_pcr *pcr)
+{
+	if (pcr->ops->power_saving)
+		pcr->ops->power_saving(pcr);
+	else
+		rtsx_comm_pm_power_saving(pcr);
+}
+
 static void rtsx_pci_idle_work(struct work_struct *work)
 {
 	struct delayed_work *dwork = to_delayed_work(work);
@@ -974,8 +1097,7 @@ static void rtsx_pci_idle_work(struct work_struct *work)
 	if (pcr->ops->turn_off_led)
 		pcr->ops->turn_off_led(pcr);
 
-	if (pcr->aspm_en)
-		rtsx_pci_enable_aspm(pcr);
+	rtsx_pm_power_saving(pcr);
 
 	mutex_unlock(&pcr->pcr_mutex);
 }
@@ -1063,6 +1185,16 @@ static int rtsx_pci_init_hw(struct rtsx_pcr *pcr)
 	if (err < 0)
 		return err;
 
+	switch (PCI_PID(pcr)) {
+	case PID_5250:
+	case PID_524A:
+	case PID_525A:
+		rtsx_pci_write_register(pcr, PM_CLK_FORCE_CTL, 1, 1);
+		break;
+	default:
+		break;
+	}
+
 	/* Enable clk_request_n to enable clock power management */
 	rtsx_pci_write_config_byte(pcr, pcr->pcie_cap + PCI_EXP_LNKCTL + 1, 1);
 	/* Enter L1 when host tx idle */
diff --git a/drivers/mfd/rtsx_pcr.h b/drivers/mfd/rtsx_pcr.h
index 931d1ae..ec784e0 100644
--- a/drivers/mfd/rtsx_pcr.h
+++ b/drivers/mfd/rtsx_pcr.h
@@ -32,6 +32,18 @@
 #define RTS524A_PME_FORCE_CTL		0xFF78
 #define RTS524A_PM_CTRL3		0xFF7E
 
+#define LTR_ACTIVE_LATENCY_DEF		0x883C
+#define LTR_IDLE_LATENCY_DEF		0x892C
+#define LTR_L1OFF_LATENCY_DEF		0x9003
+#define L1_SNOOZE_DELAY_DEF		1
+#define LTR_L1OFF_SSPWRGATE_5249_DEF		0xAF
+#define LTR_L1OFF_SSPWRGATE_5250_DEF		0xFF
+#define LTR_L1OFF_SNOOZE_SSPWRGATE_5249_DEF	0xAC
+#define LTR_L1OFF_SNOOZE_SSPWRGATE_5250_DEF	0xF8
+#define CMD_TIMEOUT_DEF		100
+#define ASPM_MASK_NEG		0xFC
+#define MASK_8_BIT_DEF		0xFF
+
 int __rtsx_pci_write_phy_register(struct rtsx_pcr *pcr, u8 addr, u16 val);
 int __rtsx_pci_read_phy_register(struct rtsx_pcr *pcr, u8 addr, u16 *val);
 
@@ -85,5 +97,7 @@ static inline u8 map_sd_drive(int idx)
 
 /* generic operations */
 int rtsx_gops_pm_reset(struct rtsx_pcr *pcr);
+int rtsx_set_ltr_latency(struct rtsx_pcr *pcr, u32 latency);
+int rtsx_set_l1off_sub(struct rtsx_pcr *pcr, u8 val);
 
 #endif
diff --git a/include/linux/mfd/rtsx_pci.h b/include/linux/mfd/rtsx_pci.h
index 116816f..57e23ad 100644
--- a/include/linux/mfd/rtsx_pci.h
+++ b/include/linux/mfd/rtsx_pci.h
@@ -573,6 +573,12 @@
 #define MSGTXDATA3			0xFE47
 #define MSGTXCTL			0xFE48
 #define LTR_CTL				0xFE4A
+#define LTR_TX_EN_MASK		BIT(7)
+#define LTR_TX_EN_1			BIT(7)
+#define LTR_TX_EN_0			0
+#define LTR_LATENCY_MODE_MASK		BIT(6)
+#define LTR_LATENCY_MODE_HW		0
+#define LTR_LATENCY_MODE_SW		BIT(6)
 #define OBFF_CFG			0xFE4C
 
 #define CDRESUMECTL			0xFE52
@@ -616,11 +622,15 @@
 #define L1SUB_CONFIG2			0xFE8E
 #define   L1SUB_AUTO_CFG		0x02
 #define L1SUB_CONFIG3			0xFE8F
+#define   L1OFF_MBIAS2_EN_5250		BIT(7)
 
 #define DUMMY_REG_RESET_0		0xFE90
 
 #define AUTOLOAD_CFG_BASE		0xFF00
 #define PETXCFG				0xFF03
+#define FORCE_CLKREQ_DELINK_MASK	BIT(7)
+#define FORCE_CLKREQ_LOW	0x80
+#define FORCE_CLKREQ_HIGH	0x00
 
 #define PM_CTRL1			0xFF44
 #define   CD_RESUME_EN_MASK		0xF0
@@ -844,6 +854,9 @@
 #define   PHY_DIG1E_RX_EN_KEEP		0x0001
 #define PHY_DUM_REG			0x1F
 
+#define PCR_ASPM_SETTING_REG1		0x160
+#define PCR_ASPM_SETTING_REG2		0x168
+
 #define PCR_SETTING_REG1		0x724
 #define PCR_SETTING_REG2		0x814
 #define PCR_SETTING_REG3		0x747
@@ -876,14 +889,79 @@ struct pcr_ops {
 	int		(*conv_clk_and_div_n)(int clk, int dir);
 	void		(*fetch_vendor_settings)(struct rtsx_pcr *pcr);
 	void		(*force_power_down)(struct rtsx_pcr *pcr, u8 pm_state);
+
+	void (*set_aspm)(struct rtsx_pcr *pcr, bool enable);
+	int (*set_ltr_latency)(struct rtsx_pcr *pcr, u32 latency);
+	int (*set_l1off_sub)(struct rtsx_pcr *pcr, u8 val);
+	void (*set_l1off_cfg_sub_d0)(struct rtsx_pcr *pcr, int active);
+	void (*full_on)(struct rtsx_pcr *pcr);
+	void (*power_saving)(struct rtsx_pcr *pcr);
 };
 
 enum PDEV_STAT  {PDEV_STAT_IDLE, PDEV_STAT_RUN};
 
+#define ASPM_L1_1_EN_MASK		BIT(3)
+#define ASPM_L1_2_EN_MASK		BIT(2)
+#define PM_L1_1_EN_MASK		BIT(1)
+#define PM_L1_2_EN_MASK		BIT(0)
+
+#define ASPM_L1_1_EN			BIT(0)
+#define ASPM_L1_2_EN			BIT(1)
+#define PM_L1_1_EN				BIT(2)
+#define PM_L1_2_EN				BIT(3)
+#define LTR_L1SS_PWR_GATE_EN	BIT(4)
+#define L1_SNOOZE_TEST_EN		BIT(5)
+#define LTR_L1SS_PWR_GATE_CHECK_CARD_EN	BIT(6)
+
+enum dev_aspm_mode {
+	DEV_ASPM_DISABLE = 0,
+	DEV_ASPM_DYNAMIC,
+	DEV_ASPM_BACKDOOR,
+	DEV_ASPM_STATIC,
+};
+
+/*
+ * struct rtsx_cr_option  - card reader option
+ * @dev_flags: device flags
+ * @force_clkreq_0: force clock request
+ * @ltr_en: enable ltr mode flag
+ * @ltr_enabled: ltr mode in configure space flag
+ * @ltr_active: ltr mode status
+ * @ltr_active_latency: ltr mode active latency
+ * @ltr_idle_latency: ltr mode idle latency
+ * @ltr_l1off_latency: ltr mode l1off latency
+ * @dev_aspm_mode: device aspm mode
+ * @l1_snooze_delay: l1 snooze delay
+ * @ltr_l1off_sspwrgate: ltr l1off sspwrgate
+ * @ltr_l1off_snooze_sspwrgate: ltr l1off snooze sspwrgate
+ */
+struct rtsx_cr_option {
+	u32 dev_flags;
+	bool force_clkreq_0;
+	bool ltr_en;
+	bool ltr_enabled;
+	bool ltr_active;
+	u32 ltr_active_latency;
+	u32 ltr_idle_latency;
+	u32 ltr_l1off_latency;
+	enum dev_aspm_mode dev_aspm_mode;
+	u32 l1_snooze_delay;
+	u8 ltr_l1off_sspwrgate;
+	u8 ltr_l1off_snooze_sspwrgate;
+};
+
+#define rtsx_set_dev_flag(cr, flag) \
+	((cr)->option.dev_flags |= (flag))
+#define rtsx_clear_dev_flag(cr, flag) \
+	((cr)->option.dev_flags &= ~(flag))
+#define rtsx_check_dev_flag(cr, flag) \
+	((cr)->option.dev_flags & (flag))
+
 struct rtsx_pcr {
 	struct pci_dev			*pci;
 	unsigned int			id;
 	int				pcie_cap;
+	struct rtsx_cr_option	option;
 
 	/* pci resources */
 	unsigned long			addr;
@@ -940,6 +1018,7 @@ struct rtsx_pcr {
 	u8				card_drive_sel;
 #define ASPM_L1_EN			0x02
 	u8				aspm_en;
+	bool				aspm_enabled;
 
 #define PCR_MS_PMOS			(1 << 0)
 #define PCR_REVERSE_SOCKET		(1 << 1)
@@ -964,6 +1043,11 @@ struct rtsx_pcr {
 	u8				dma_error_count;
 };
 
+#define PID_524A	0x524A
+#define PID_5249		0x5249
+#define PID_5250		0x5250
+#define PID_525A	0x525A
+
 #define CHK_PCI_PID(pcr, pid)		((pcr)->pci->device == (pid))
 #define PCI_VID(pcr)			((pcr)->pci->vendor)
 #define PCI_PID(pcr)			((pcr)->pci->device)
-- 
1.9.1

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v6] mfd: Add support for RTS5250S power saving
  2017-09-07  8:26 [PATCH v6] mfd: Add support for RTS5250S power saving rui_feng
@ 2017-09-07 10:57 ` Lee Jones
  2017-12-14 22:25 ` Bjorn Helgaas
  1 sibling, 0 replies; 11+ messages in thread
From: Lee Jones @ 2017-09-07 10:57 UTC (permalink / raw)
  To: rui_feng; +Cc: linux-kernel

On Thu, 07 Sep 2017, rui_feng@realsil.com.cn wrote:

> From: Rui Feng <rui_feng@realsil.com.cn>
> 
> Enable power saving for RTS5250S as following steps:
> 1.Set 0xFE58 to enable clock power management.
> 2.Check cfg space whether support L1SS or not.
> 3.If support L1SS, set 0xFF03 to free clkreq.
> 4.When entering idle status, enable aspm
>   and set parameters for L1SS and LTR.
> 5.Wnen entering run status, disable aspm
>   and set parameters for L1SS and LTR.
> If entering L1SS mode successfully,
> electric current will be below 2mA.
> 
> Signed-off-by: Rui Feng <rui_feng@realsil.com.cn>
> ---
>  drivers/mfd/rts5249.c        | 155 ++++++++++++++++++++++++++++++++++++++++++-
>  drivers/mfd/rtsx_pcr.c       | 142 +++++++++++++++++++++++++++++++++++++--
>  drivers/mfd/rtsx_pcr.h       |  14 ++++
>  include/linux/mfd/rtsx_pci.h |  84 +++++++++++++++++++++++
>  4 files changed, 389 insertions(+), 6 deletions(-)

Applied for v4.15, thanks.

-- 
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v6] mfd: Add support for RTS5250S power saving
  2017-09-07  8:26 [PATCH v6] mfd: Add support for RTS5250S power saving rui_feng
  2017-09-07 10:57 ` Lee Jones
@ 2017-12-14 22:25 ` Bjorn Helgaas
  2017-12-15  7:41   ` Hans de Goede
       [not found]   ` <2A308283684ECD4B896628E09AF5361E01A8150B@RS-MBS01.realsil.com.cn>
  1 sibling, 2 replies; 11+ messages in thread
From: Bjorn Helgaas @ 2017-12-14 22:25 UTC (permalink / raw)
  To: rui_feng; +Cc: lee.jones, linux-kernel, Hans de Goede, Dave Jiang, linux-pci

[+cc Hans, Dave, linux-pci]

On Thu, Sep 07, 2017 at 04:26:39PM +0800, rui_feng@realsil.com.cn wrote:
> From: Rui Feng <rui_feng@realsil.com.cn>

I wish this had been posted to linux-pci before being merged.

I'm concerned because some of this appears to overlap and
conflict with PCI core management of ASPM.

I assume these devices advertise ASPM support in their Link
Capabilites registers, right?  If so, why isn't the existing
PCI core ASPM support sufficient?

> Enable power saving for RTS5250S as following steps:
> 1.Set 0xFE58 to enable clock power management.

Is this clock power management something specific to RTS5250S, or is
it standard PCIe architected stuff?

> 2.Check cfg space whether support L1SS or not.

This sounds like standard PCIe ASPM L1 Substates, right?

> 3.If support L1SS, set 0xFF03 to free clkreq.
> 4.When entering idle status, enable aspm
>   and set parameters for L1SS and LTR.
> 5.Wnen entering run status, disable aspm
>   and set parameters for L1SS and LTR.

In general, drivers should not configure ASPM, L1SS, and LTR
themselves; the PCI core should do that.

If a driver needs to tweak ASPM at run-time, it should use interfaces
exported by the PCI core to do so.

> If entering L1SS mode successfully,
> electric current will be below 2mA.

> Signed-off-by: Rui Feng <rui_feng@realsil.com.cn>
> ...

> +static void rts5249_init_from_cfg(struct rtsx_pcr *pcr)
> +{
> +	struct rtsx_cr_option *option = &(pcr->option);
> +	u32 lval;
> +
> +	if (CHK_PCI_PID(pcr, PID_524A))
> +		rtsx_pci_read_config_dword(pcr,
> +			PCR_ASPM_SETTING_REG1, &lval);
> +	else
> +		rtsx_pci_read_config_dword(pcr,
> +			PCR_ASPM_SETTING_REG2, &lval);

This looks like you're reading the ASPM L1 Substates capability, i.e.,
PCI_L1SS_CAP, using hard-coded offsets based on the Device ID.  You
should be using pci_find_ext_capability() to locate it.

> +	if (lval & ASPM_L1_1_EN_MASK)
> +		rtsx_set_dev_flag(pcr, ASPM_L1_1_EN);
> +
> +	if (lval & ASPM_L1_2_EN_MASK)
> +		rtsx_set_dev_flag(pcr, ASPM_L1_2_EN);
> +
> +	if (lval & PM_L1_1_EN_MASK)
> +		rtsx_set_dev_flag(pcr, PM_L1_1_EN);
> +
> +	if (lval & PM_L1_2_EN_MASK)
> +		rtsx_set_dev_flag(pcr, PM_L1_2_EN);

You're adding these:

  #define ASPM_L1_1_EN_MASK              BIT(3)
  #define ASPM_L1_2_EN_MASK              BIT(2)
  #define PM_L1_1_EN_MASK                BIT(1)
  #define PM_L1_2_EN_MASK                BIT(0)

The PCI core already defines these and you should use them instead:

  #define  PCI_L1SS_CAP_PCIPM_L1_2        0x00000001  /* PCI-PM L1.2 Supported */
  #define  PCI_L1SS_CAP_PCIPM_L1_1        0x00000002  /* PCI-PM L1.1 Supported */
  #define  PCI_L1SS_CAP_ASPM_L1_2         0x00000004  /* ASPM L1.2 Supported */
  #define  PCI_L1SS_CAP_ASPM_L1_1         0x00000008  /* ASPM L1.1 Supported */

> +	if (option->ltr_en) {
> +		u16 val;
> +
> +		pcie_capability_read_word(pcr->pci, PCI_EXP_DEVCTL2, &val);
> +		if (val & PCI_EXP_DEVCTL2_LTR_EN) {
> +			option->ltr_enabled = true;
> +			option->ltr_active = true;

ltr_active is never actually used.

> +			rtsx_set_ltr_latency(pcr, option->ltr_active_latency);
> +		} else {
> +			option->ltr_enabled = false;
> +		}
> +	}
> +}
> ...

> +static void rts5249_set_aspm(struct rtsx_pcr *pcr, bool enable)
> +{
> +	struct rtsx_cr_option *option = &pcr->option;
> +	u8 val = 0;
> +
> +	if (pcr->aspm_enabled == enable)
> +		return;
> +
> +	if (option->dev_aspm_mode == DEV_ASPM_DYNAMIC) {
> +		if (enable)
> +			val = pcr->aspm_en;
> +		rtsx_pci_update_cfg_byte(pcr,
> +			pcr->pcie_cap + PCI_EXP_LNKCTL,
> +			ASPM_MASK_NEG, val);

This stomps on whatever ASPM configuration the PCI core did.

> +	} else if (option->dev_aspm_mode == DEV_ASPM_BACKDOOR) {

DEV_ASPM_BACKDOOR is never set, so this looks like dead code.

> +		u8 mask = FORCE_ASPM_VAL_MASK | FORCE_ASPM_CTL0;
> +		if (!enable)
> +			val = FORCE_ASPM_CTL0;
> +		rtsx_pci_write_register(pcr, ASPM_FORCE_CTL, mask, val);
> +	}
> +
> +	pcr->aspm_enabled = enable;
> +}

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v6] mfd: Add support for RTS5250S power saving
  2017-12-14 22:25 ` Bjorn Helgaas
@ 2017-12-15  7:41   ` Hans de Goede
       [not found]   ` <2A308283684ECD4B896628E09AF5361E01A8150B@RS-MBS01.realsil.com.cn>
  1 sibling, 0 replies; 11+ messages in thread
From: Hans de Goede @ 2017-12-15  7:41 UTC (permalink / raw)
  To: Bjorn Helgaas, rui_feng; +Cc: lee.jones, linux-kernel, Dave Jiang, linux-pci

Hi,

On 14-12-17 23:25, Bjorn Helgaas wrote:
> [+cc Hans, Dave, linux-pci]

I appreciate the Cc but I'm not really involved in this, other
then fixing the original commit up so that the T440s can reach
idle-state PC7 again, instead of only going to PC3.

With that said, for any fixes to this please Cc me so that I can
test that the T440s does not regress again.

Regards,

Hans


> 
> On Thu, Sep 07, 2017 at 04:26:39PM +0800, rui_feng@realsil.com.cn wrote:
>> From: Rui Feng <rui_feng@realsil.com.cn>
> 
> I wish this had been posted to linux-pci before being merged.
> 
> I'm concerned because some of this appears to overlap and
> conflict with PCI core management of ASPM.
> 
> I assume these devices advertise ASPM support in their Link
> Capabilites registers, right?  If so, why isn't the existing
> PCI core ASPM support sufficient?
> 
>> Enable power saving for RTS5250S as following steps:
>> 1.Set 0xFE58 to enable clock power management.
> 
> Is this clock power management something specific to RTS5250S, or is
> it standard PCIe architected stuff?
> 
>> 2.Check cfg space whether support L1SS or not.
> 
> This sounds like standard PCIe ASPM L1 Substates, right?
> 
>> 3.If support L1SS, set 0xFF03 to free clkreq.
>> 4.When entering idle status, enable aspm
>>    and set parameters for L1SS and LTR.
>> 5.Wnen entering run status, disable aspm
>>    and set parameters for L1SS and LTR.
> 
> In general, drivers should not configure ASPM, L1SS, and LTR
> themselves; the PCI core should do that.
> 
> If a driver needs to tweak ASPM at run-time, it should use interfaces
> exported by the PCI core to do so.
> 
>> If entering L1SS mode successfully,
>> electric current will be below 2mA.
> 
>> Signed-off-by: Rui Feng <rui_feng@realsil.com.cn>
>> ...
> 
>> +static void rts5249_init_from_cfg(struct rtsx_pcr *pcr)
>> +{
>> +	struct rtsx_cr_option *option = &(pcr->option);
>> +	u32 lval;
>> +
>> +	if (CHK_PCI_PID(pcr, PID_524A))
>> +		rtsx_pci_read_config_dword(pcr,
>> +			PCR_ASPM_SETTING_REG1, &lval);
>> +	else
>> +		rtsx_pci_read_config_dword(pcr,
>> +			PCR_ASPM_SETTING_REG2, &lval);
> 
> This looks like you're reading the ASPM L1 Substates capability, i.e.,
> PCI_L1SS_CAP, using hard-coded offsets based on the Device ID.  You
> should be using pci_find_ext_capability() to locate it.
> 
>> +	if (lval & ASPM_L1_1_EN_MASK)
>> +		rtsx_set_dev_flag(pcr, ASPM_L1_1_EN);
>> +
>> +	if (lval & ASPM_L1_2_EN_MASK)
>> +		rtsx_set_dev_flag(pcr, ASPM_L1_2_EN);
>> +
>> +	if (lval & PM_L1_1_EN_MASK)
>> +		rtsx_set_dev_flag(pcr, PM_L1_1_EN);
>> +
>> +	if (lval & PM_L1_2_EN_MASK)
>> +		rtsx_set_dev_flag(pcr, PM_L1_2_EN);
> 
> You're adding these:
> 
>    #define ASPM_L1_1_EN_MASK              BIT(3)
>    #define ASPM_L1_2_EN_MASK              BIT(2)
>    #define PM_L1_1_EN_MASK                BIT(1)
>    #define PM_L1_2_EN_MASK                BIT(0)
> 
> The PCI core already defines these and you should use them instead:
> 
>    #define  PCI_L1SS_CAP_PCIPM_L1_2        0x00000001  /* PCI-PM L1.2 Supported */
>    #define  PCI_L1SS_CAP_PCIPM_L1_1        0x00000002  /* PCI-PM L1.1 Supported */
>    #define  PCI_L1SS_CAP_ASPM_L1_2         0x00000004  /* ASPM L1.2 Supported */
>    #define  PCI_L1SS_CAP_ASPM_L1_1         0x00000008  /* ASPM L1.1 Supported */
> 
>> +	if (option->ltr_en) {
>> +		u16 val;
>> +
>> +		pcie_capability_read_word(pcr->pci, PCI_EXP_DEVCTL2, &val);
>> +		if (val & PCI_EXP_DEVCTL2_LTR_EN) {
>> +			option->ltr_enabled = true;
>> +			option->ltr_active = true;
> 
> ltr_active is never actually used.
> 
>> +			rtsx_set_ltr_latency(pcr, option->ltr_active_latency);
>> +		} else {
>> +			option->ltr_enabled = false;
>> +		}
>> +	}
>> +}
>> ...
> 
>> +static void rts5249_set_aspm(struct rtsx_pcr *pcr, bool enable)
>> +{
>> +	struct rtsx_cr_option *option = &pcr->option;
>> +	u8 val = 0;
>> +
>> +	if (pcr->aspm_enabled == enable)
>> +		return;
>> +
>> +	if (option->dev_aspm_mode == DEV_ASPM_DYNAMIC) {
>> +		if (enable)
>> +			val = pcr->aspm_en;
>> +		rtsx_pci_update_cfg_byte(pcr,
>> +			pcr->pcie_cap + PCI_EXP_LNKCTL,
>> +			ASPM_MASK_NEG, val);
> 
> This stomps on whatever ASPM configuration the PCI core did.
> 
>> +	} else if (option->dev_aspm_mode == DEV_ASPM_BACKDOOR) {
> 
> DEV_ASPM_BACKDOOR is never set, so this looks like dead code.
> 
>> +		u8 mask = FORCE_ASPM_VAL_MASK | FORCE_ASPM_CTL0;
>> +		if (!enable)
>> +			val = FORCE_ASPM_CTL0;
>> +		rtsx_pci_write_register(pcr, ASPM_FORCE_CTL, mask, val);
>> +	}
>> +
>> +	pcr->aspm_enabled = enable;
>> +}

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: 答复: [PATCH v6] mfd: Add support for RTS5250S power saving
       [not found]   ` <2A308283684ECD4B896628E09AF5361E01A8150B@RS-MBS01.realsil.com.cn>
@ 2017-12-15 15:15     ` Bjorn Helgaas
  2017-12-19  8:15       ` 答复: " 冯锐
  0 siblings, 1 reply; 11+ messages in thread
From: Bjorn Helgaas @ 2017-12-15 15:15 UTC (permalink / raw)
  To: 冯锐
  Cc: lee.jones, linux-kernel, Hans de Goede, Dave Jiang, linux-pci

On Fri, Dec 15, 2017 at 09:42:45AM +0000, 冯锐 wrote:
> > [+cc Hans, Dave, linux-pci]
> > 
> > On Thu, Sep 07, 2017 at 04:26:39PM +0800, rui_feng@realsil.com.cn wrote:
> > > From: Rui Feng <rui_feng@realsil.com.cn>
> > 
> > I wish this had been posted to linux-pci before being merged.
> > 
> > I'm concerned because some of this appears to overlap and conflict with PCI
> > core management of ASPM.
> > 
> > I assume these devices advertise ASPM support in their Link Capabilites
> > registers, right?  If so, why isn't the existing PCI core ASPM support
> > sufficient?
> > 
> When L1SS is configured, the device(hardware) can't enter L1SS status automatically,
> it need driver(software) to do some work to achieve the function.

So this is a hardware defect in the device?  As far as I know, ASPM
and L1SS are specified such that they should work without special
driver support.

> > > Enable power saving for RTS5250S as following steps:
> > > 1.Set 0xFE58 to enable clock power management.
> > 
> > Is this clock power management something specific to RTS5250S, or is it
> > standard PCIe architected stuff?
> > 
> 0xFE58 is specific register to RTS5250S not standard PCIe architected stuff.

OK.  I asked because devices often mirror architected PCIe config
things in device-specific MMIO space, and if I squint just right, I
can sort of match up the register bits you used with things in the
PCIe spec.

> > > 2.Check cfg space whether support L1SS or not.
> > 
> > This sounds like standard PCIe ASPM L1 Substates, right?
> > 
> Yes.
> 
> > > 3.If support L1SS, set 0xFF03 to free clkreq.
> > > 4.When entering idle status, enable aspm
> > >   and set parameters for L1SS and LTR.
> > > 5.Wnen entering run status, disable aspm
> > >   and set parameters for L1SS and LTR.
> > 
> > In general, drivers should not configure ASPM, L1SS, and LTR themselves; the
> > PCI core should do that.
> > 
> > If a driver needs to tweak ASPM at run-time, it should use interfaces exported
> > by the PCI core to do so.
> > 
> Which interface I can use to set ASPM? I use "pci_write_config_byte" now.

What do you need to do?  include/linux/pci-aspm.h exports
pci_disable_link_state(), which is mainly used to avoid ASPM states
that have hardware errata.

If you need to do something beyond that, we can talk about adding
something new.

There are quite a few other things in include/linux/pci-aspm.h, but
they're all for internal use in the PCI core.  I'll move them to
drivers/pci/pci.h to avoid confusion.

> > > +static void rts5249_set_aspm(struct rtsx_pcr *pcr, bool enable) {
> > > +	struct rtsx_cr_option *option = &pcr->option;
> > > +	u8 val = 0;
> > > +
> > > +	if (pcr->aspm_enabled == enable)
> > > +		return;
> > > +
> > > +	if (option->dev_aspm_mode == DEV_ASPM_DYNAMIC) {
> > > +		if (enable)
> > > +			val = pcr->aspm_en;
> > > +		rtsx_pci_update_cfg_byte(pcr,
> > > +			pcr->pcie_cap + PCI_EXP_LNKCTL,
> > > +			ASPM_MASK_NEG, val);
> > 
> > This stomps on whatever ASPM configuration the PCI core did.
> We disable/enable aspm dynamic in order to improve read/write performance and more stable,
> so we don't allow PCI core do it.

This is pretty vague.  Can you be any more specific?  If there are
performance or stability problems in the PCI core ASPM support, I'd
prefer to fix those instead of working around them in every driver.

Bjorn

^ permalink raw reply	[flat|nested] 11+ messages in thread

* 答复: 答复: [PATCH v6] mfd: Add support for RTS5250S power saving
  2017-12-15 15:15     ` 答复: " Bjorn Helgaas
@ 2017-12-19  8:15       ` 冯锐
  2017-12-27 23:37         ` Bjorn Helgaas
  0 siblings, 1 reply; 11+ messages in thread
From: 冯锐 @ 2017-12-19  8:15 UTC (permalink / raw)
  To: Bjorn Helgaas
  Cc: lee.jones, linux-kernel, Hans de Goede, Dave Jiang, linux-pci

> On Fri, Dec 15, 2017 at 09:42:45AM +0000, 冯锐 wrote:
> > > [+cc Hans, Dave, linux-pci]
> > >
> > > On Thu, Sep 07, 2017 at 04:26:39PM +0800, rui_feng@realsil.com.cn
> wrote:
> > > > From: Rui Feng <rui_feng@realsil.com.cn>
> > >
> > > I wish this had been posted to linux-pci before being merged.
> > >
> > > I'm concerned because some of this appears to overlap and conflict
> > > with PCI core management of ASPM.
> > >
> > > I assume these devices advertise ASPM support in their Link
> > > Capabilites registers, right?  If so, why isn't the existing PCI
> > > core ASPM support sufficient?
> > >
> > When L1SS is configured, the device(hardware) can't enter L1SS status
> > automatically, it need driver(software) to do some work to achieve the
> function.
> 
> So this is a hardware defect in the device?  As far as I know, ASPM and L1SS
> are specified such that they should work without special driver support.
> 
Yes, you can say that.

> > > > Enable power saving for RTS5250S as following steps:
> > > > 1.Set 0xFE58 to enable clock power management.
> > >
> > > Is this clock power management something specific to RTS5250S, or is
> > > it standard PCIe architected stuff?
> > >
> > 0xFE58 is specific register to RTS5250S not standard PCIe architected stuff.
> 
> OK.  I asked because devices often mirror architected PCIe config things in
> device-specific MMIO space, and if I squint just right, I can sort of match up the
> register bits you used with things in the PCIe spec.
> 
> > > > 2.Check cfg space whether support L1SS or not.
> > >
> > > This sounds like standard PCIe ASPM L1 Substates, right?
> > >
> > Yes.
> >
> > > > 3.If support L1SS, set 0xFF03 to free clkreq.
> > > > 4.When entering idle status, enable aspm
> > > >   and set parameters for L1SS and LTR.
> > > > 5.Wnen entering run status, disable aspm
> > > >   and set parameters for L1SS and LTR.
> > >
> > > In general, drivers should not configure ASPM, L1SS, and LTR
> > > themselves; the PCI core should do that.
> > >
> > > If a driver needs to tweak ASPM at run-time, it should use
> > > interfaces exported by the PCI core to do so.
> > >
> > Which interface I can use to set ASPM? I use "pci_write_config_byte" now.
> 
> What do you need to do?  include/linux/pci-aspm.h exports
> pci_disable_link_state(), which is mainly used to avoid ASPM states that have
> hardware errata.
> 
I want to enable ASPM(L0 -> L1) and disable ASPM(L1 -> L0), which interface can I use?

> If you need to do something beyond that, we can talk about adding something
> new.
> 
> There are quite a few other things in include/linux/pci-aspm.h, but they're all
> for internal use in the PCI core.  I'll move them to drivers/pci/pci.h to avoid
> confusion.
> 
> > > > +static void rts5249_set_aspm(struct rtsx_pcr *pcr, bool enable) {
> > > > +	struct rtsx_cr_option *option = &pcr->option;
> > > > +	u8 val = 0;
> > > > +
> > > > +	if (pcr->aspm_enabled == enable)
> > > > +		return;
> > > > +
> > > > +	if (option->dev_aspm_mode == DEV_ASPM_DYNAMIC) {
> > > > +		if (enable)
> > > > +			val = pcr->aspm_en;
> > > > +		rtsx_pci_update_cfg_byte(pcr,
> > > > +			pcr->pcie_cap + PCI_EXP_LNKCTL,
> > > > +			ASPM_MASK_NEG, val);
> > >
> > > This stomps on whatever ASPM configuration the PCI core did.
> > We disable/enable aspm dynamic in order to improve read/write
> > performance and more stable, so we don't allow PCI core do it.
> 
> This is pretty vague.  Can you be any more specific?  If there are
> performance or stability problems in the PCI core ASPM support, I'd prefer to
> fix those instead of working around them in every driver.
> 
It's device's defect not PCI core, so no need to fix PCI core.

> Bjorn
> 
> ------Please consider the environment before printing this e-mail.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: 答复: 答复: [PATCH v6] mfd: Add support for RTS5250S power saving
  2017-12-19  8:15       ` 答复: " 冯锐
@ 2017-12-27 23:37         ` Bjorn Helgaas
  2018-01-17 21:01           ` Bjorn Helgaas
  0 siblings, 1 reply; 11+ messages in thread
From: Bjorn Helgaas @ 2017-12-27 23:37 UTC (permalink / raw)
  To: 冯锐
  Cc: lee.jones, linux-kernel, Hans de Goede, Dave Jiang, linux-pci

On Tue, Dec 19, 2017 at 08:15:24AM +0000, 冯锐 wrote:
> > On Fri, Dec 15, 2017 at 09:42:45AM +0000, 冯锐 wrote:
> > > > [+cc Hans, Dave, linux-pci]
> > > >
> > > > On Thu, Sep 07, 2017 at 04:26:39PM +0800, rui_feng@realsil.com.cn
> > wrote:
> > > > > From: Rui Feng <rui_feng@realsil.com.cn>
> > > >
> > > > I wish this had been posted to linux-pci before being merged.
> > > >
> > > > I'm concerned because some of this appears to overlap and conflict
> > > > with PCI core management of ASPM.
> > > >
> > > > I assume these devices advertise ASPM support in their Link
> > > > Capabilites registers, right?  If so, why isn't the existing PCI
> > > > core ASPM support sufficient?
> > > >
> > > When L1SS is configured, the device(hardware) can't enter L1SS status
> > > automatically, it need driver(software) to do some work to achieve the
> > function.
> > 
> > So this is a hardware defect in the device?  As far as I know, ASPM and L1SS
> > are specified such that they should work without special driver support.
> > 
> Yes, you can say that.
> 
> > > > > Enable power saving for RTS5250S as following steps:
> > > > > 1.Set 0xFE58 to enable clock power management.
> > > >
> > > > Is this clock power management something specific to RTS5250S, or is
> > > > it standard PCIe architected stuff?
> > > >
> > > 0xFE58 is specific register to RTS5250S not standard PCIe architected stuff.
> > 
> > OK.  I asked because devices often mirror architected PCIe config things in
> > device-specific MMIO space, and if I squint just right, I can sort of match up the
> > register bits you used with things in the PCIe spec.
> > 
> > > > > 2.Check cfg space whether support L1SS or not.
> > > >
> > > > This sounds like standard PCIe ASPM L1 Substates, right?
> > > >
> > > Yes.
> > >
> > > > > 3.If support L1SS, set 0xFF03 to free clkreq.
> > > > > 4.When entering idle status, enable aspm
> > > > >   and set parameters for L1SS and LTR.
> > > > > 5.Wnen entering run status, disable aspm
> > > > >   and set parameters for L1SS and LTR.
> > > >
> > > > In general, drivers should not configure ASPM, L1SS, and LTR
> > > > themselves; the PCI core should do that.
> > > >
> > > > If a driver needs to tweak ASPM at run-time, it should use
> > > > interfaces exported by the PCI core to do so.
> > > >
> > > Which interface I can use to set ASPM? I use "pci_write_config_byte" now.
> > 
> > What do you need to do?  include/linux/pci-aspm.h exports
> > pci_disable_link_state(), which is mainly used to avoid ASPM
> > states that have hardware errata.
> > 
> I want to enable ASPM(L0 -> L1) and disable ASPM(L1 -> L0), which
> interface can I use?

You can use pci_disable_link_state() to disable usage of L1.

Currently there is no corresponding pci_enable_link_state().  What if
we added something like the following (untested)?  Would that work for
you?


commit 209930d809fa602b8aafdd171b26719cee6c6649
Author: Bjorn Helgaas <bhelgaas@google.com>
Date:   Wed Dec 27 16:56:26 2017 -0600

    PCI/ASPM: Add pci_enable_link_state()
    
    Some drivers want control over the ASPM states their device is allowed to
    use.  We already have a pci_disable_link_state(), and drivers can use that
    to prevent the device from entering L0 or L1s.
    
    Add a corresponding pci_enable_link_state() so a driver can enable use of
    L0 or L1s again.

diff --git a/drivers/pci/pcie/aspm.c b/drivers/pci/pcie/aspm.c
index 3b9b4d50cd98..ca217195f800 100644
--- a/drivers/pci/pcie/aspm.c
+++ b/drivers/pci/pcie/aspm.c
@@ -1028,6 +1028,67 @@ void pcie_aspm_powersave_config_link(struct pci_dev *pdev)
 	up_read(&pci_bus_sem);
 }
 
+/**
+ * pci_enable_link_state - Enable device's link state, so the link may
+ * enter specific states.  Note that if the BIOS didn't grant ASPM
+ * control to the OS, this does nothing because we can't touch the LNKCTL
+ * register.
+ *
+ * @pdev: PCI device
+ * @state: ASPM link state to enable
+ */
+void pci_enable_link_state(struct pci_dev *pdev, int state)
+{
+	struct pci_dev *parent = pdev->bus->self;
+	struct pcie_link_state *link;
+	u32 lnkcap;
+
+	if (!pci_is_pcie(pdev))
+		return;
+
+	if (pdev->has_secondary_link)
+		parent = pdev;
+	if (!parent || !parent->link_state)
+		return;
+
+	/*
+	 * A driver requested that ASPM be enabled on this device, but
+	 * if we don't have permission to manage ASPM (e.g., on ACPI
+	 * systems we have to observe the FADT ACPI_FADT_NO_ASPM bit and
+	 * the _OSC method), we can't honor that request.  Windows has
+	 * a similar mechanism using "PciASPMOptOut", which is also
+	 * ignored in this situation.
+	 */
+	if (aspm_disabled) {
+		dev_warn(&pdev->dev, "can't enable ASPM; OS doesn't have ASPM control\n");
+		return;
+	}
+
+	down_read(&pci_bus_sem);
+	mutex_lock(&aspm_lock);
+	link = parent->link_state;
+	if (state & PCIE_LINK_STATE_L0S)
+		link->aspm_disable &= ~ASPM_STATE_L0S;
+	if (state & PCIE_LINK_STATE_L1)
+		link->aspm_disable &= ~ASPM_STATE_L1;
+	pcie_config_aspm_link(link, policy_to_aspm_state(link));
+
+	/*
+	 * Enable Clock Power Management if requested by the driver,
+	 * supported by the device, and allowed by the current policy.
+	 */
+	if (state & PCIE_LINK_STATE_CLKPM) {
+		pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, &lnkcap);
+		if (lnkcap & PCI_EXP_LNKCAP_CLKPM) {
+			link->clkpm_capable = 1;
+			pcie_set_clkpm(link, policy_to_clkpm_state(link));
+		}
+	}
+	mutex_unlock(&aspm_lock);
+	up_read(&pci_bus_sem);
+}
+EXPORT_SYMBOL(pci_enable_link_state);
+
 static void __pci_disable_link_state(struct pci_dev *pdev, int state, bool sem)
 {
 	struct pci_dev *parent = pdev->bus->self;
diff --git a/include/linux/pci-aspm.h b/include/linux/pci-aspm.h
index df28af5cef21..cd0736b384ae 100644
--- a/include/linux/pci-aspm.h
+++ b/include/linux/pci-aspm.h
@@ -24,10 +24,12 @@
 #define PCIE_LINK_STATE_CLKPM	4
 
 #ifdef CONFIG_PCIEASPM
+void pci_enable_link_state(struct pci_dev *pdev, int state);
 void pci_disable_link_state(struct pci_dev *pdev, int state);
 void pci_disable_link_state_locked(struct pci_dev *pdev, int state);
 void pcie_no_aspm(void);
 #else
+static inline void pci_enable_link_state(struct pci_dev *pdev, int state) { }
 static inline void pci_disable_link_state(struct pci_dev *pdev, int state) { }
 static inline void pcie_no_aspm(void) { }
 #endif

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: 答复: 答复: [PATCH v6] mfd: Add support for RTS5250S power saving
  2017-12-27 23:37         ` Bjorn Helgaas
@ 2018-01-17 21:01           ` Bjorn Helgaas
  2018-01-19  7:38             ` 答复: " 冯锐
  0 siblings, 1 reply; 11+ messages in thread
From: Bjorn Helgaas @ 2018-01-17 21:01 UTC (permalink / raw)
  To: 冯锐
  Cc: lee.jones, linux-kernel, Hans de Goede, Dave Jiang, linux-pci

On Wed, Dec 27, 2017 at 05:37:50PM -0600, Bjorn Helgaas wrote:
> On Tue, Dec 19, 2017 at 08:15:24AM +0000, 冯锐 wrote:
> > > On Fri, Dec 15, 2017 at 09:42:45AM +0000, 冯锐 wrote:
> > > > > [+cc Hans, Dave, linux-pci]
> > > > >
> > > > > On Thu, Sep 07, 2017 at 04:26:39PM +0800, rui_feng@realsil.com.cn
> > > wrote:
> > > > > > From: Rui Feng <rui_feng@realsil.com.cn>
> > > > >
> > > > > I wish this had been posted to linux-pci before being merged.
> > > > >
> > > > > I'm concerned because some of this appears to overlap and conflict
> > > > > with PCI core management of ASPM.
> > > > >
> > > > > I assume these devices advertise ASPM support in their Link
> > > > > Capabilites registers, right?  If so, why isn't the existing PCI
> > > > > core ASPM support sufficient?
> > > > >
> > > > When L1SS is configured, the device(hardware) can't enter L1SS status
> > > > automatically, it need driver(software) to do some work to achieve the
> > > function.
> > > 
> > > So this is a hardware defect in the device?  As far as I know, ASPM and L1SS
> > > are specified such that they should work without special driver support.
> > > 
> > Yes, you can say that.
> > 
> > > > > > Enable power saving for RTS5250S as following steps:
> > > > > > 1.Set 0xFE58 to enable clock power management.
> > > > >
> > > > > Is this clock power management something specific to RTS5250S, or is
> > > > > it standard PCIe architected stuff?
> > > > >
> > > > 0xFE58 is specific register to RTS5250S not standard PCIe architected stuff.
> > > 
> > > OK.  I asked because devices often mirror architected PCIe config things in
> > > device-specific MMIO space, and if I squint just right, I can sort of match up the
> > > register bits you used with things in the PCIe spec.
> > > 
> > > > > > 2.Check cfg space whether support L1SS or not.
> > > > >
> > > > > This sounds like standard PCIe ASPM L1 Substates, right?
> > > > >
> > > > Yes.
> > > >
> > > > > > 3.If support L1SS, set 0xFF03 to free clkreq.
> > > > > > 4.When entering idle status, enable aspm
> > > > > >   and set parameters for L1SS and LTR.
> > > > > > 5.Wnen entering run status, disable aspm
> > > > > >   and set parameters for L1SS and LTR.
> > > > >
> > > > > In general, drivers should not configure ASPM, L1SS, and LTR
> > > > > themselves; the PCI core should do that.
> > > > >
> > > > > If a driver needs to tweak ASPM at run-time, it should use
> > > > > interfaces exported by the PCI core to do so.
> > > > >
> > > > Which interface I can use to set ASPM? I use "pci_write_config_byte" now.
> > > 
> > > What do you need to do?  include/linux/pci-aspm.h exports
> > > pci_disable_link_state(), which is mainly used to avoid ASPM
> > > states that have hardware errata.
> > > 
> > I want to enable ASPM(L0 -> L1) and disable ASPM(L1 -> L0), which
> > interface can I use?
> 
> You can use pci_disable_link_state() to disable usage of L1.
> 
> Currently there is no corresponding pci_enable_link_state().  What if
> we added something like the following (untested)?  Would that work for
> you?

Hi Rui,

Any thoughts on the patch below?

> commit 209930d809fa602b8aafdd171b26719cee6c6649
> Author: Bjorn Helgaas <bhelgaas@google.com>
> Date:   Wed Dec 27 16:56:26 2017 -0600
> 
>     PCI/ASPM: Add pci_enable_link_state()
>     
>     Some drivers want control over the ASPM states their device is allowed to
>     use.  We already have a pci_disable_link_state(), and drivers can use that
>     to prevent the device from entering L0 or L1s.
>     
>     Add a corresponding pci_enable_link_state() so a driver can enable use of
>     L0 or L1s again.
> 
> diff --git a/drivers/pci/pcie/aspm.c b/drivers/pci/pcie/aspm.c
> index 3b9b4d50cd98..ca217195f800 100644
> --- a/drivers/pci/pcie/aspm.c
> +++ b/drivers/pci/pcie/aspm.c
> @@ -1028,6 +1028,67 @@ void pcie_aspm_powersave_config_link(struct pci_dev *pdev)
>  	up_read(&pci_bus_sem);
>  }
>  
> +/**
> + * pci_enable_link_state - Enable device's link state, so the link may
> + * enter specific states.  Note that if the BIOS didn't grant ASPM
> + * control to the OS, this does nothing because we can't touch the LNKCTL
> + * register.
> + *
> + * @pdev: PCI device
> + * @state: ASPM link state to enable
> + */
> +void pci_enable_link_state(struct pci_dev *pdev, int state)
> +{
> +	struct pci_dev *parent = pdev->bus->self;
> +	struct pcie_link_state *link;
> +	u32 lnkcap;
> +
> +	if (!pci_is_pcie(pdev))
> +		return;
> +
> +	if (pdev->has_secondary_link)
> +		parent = pdev;
> +	if (!parent || !parent->link_state)
> +		return;
> +
> +	/*
> +	 * A driver requested that ASPM be enabled on this device, but
> +	 * if we don't have permission to manage ASPM (e.g., on ACPI
> +	 * systems we have to observe the FADT ACPI_FADT_NO_ASPM bit and
> +	 * the _OSC method), we can't honor that request.  Windows has
> +	 * a similar mechanism using "PciASPMOptOut", which is also
> +	 * ignored in this situation.
> +	 */
> +	if (aspm_disabled) {
> +		dev_warn(&pdev->dev, "can't enable ASPM; OS doesn't have ASPM control\n");
> +		return;
> +	}
> +
> +	down_read(&pci_bus_sem);
> +	mutex_lock(&aspm_lock);
> +	link = parent->link_state;
> +	if (state & PCIE_LINK_STATE_L0S)
> +		link->aspm_disable &= ~ASPM_STATE_L0S;
> +	if (state & PCIE_LINK_STATE_L1)
> +		link->aspm_disable &= ~ASPM_STATE_L1;
> +	pcie_config_aspm_link(link, policy_to_aspm_state(link));
> +
> +	/*
> +	 * Enable Clock Power Management if requested by the driver,
> +	 * supported by the device, and allowed by the current policy.
> +	 */
> +	if (state & PCIE_LINK_STATE_CLKPM) {
> +		pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, &lnkcap);
> +		if (lnkcap & PCI_EXP_LNKCAP_CLKPM) {
> +			link->clkpm_capable = 1;
> +			pcie_set_clkpm(link, policy_to_clkpm_state(link));
> +		}
> +	}
> +	mutex_unlock(&aspm_lock);
> +	up_read(&pci_bus_sem);
> +}
> +EXPORT_SYMBOL(pci_enable_link_state);
> +
>  static void __pci_disable_link_state(struct pci_dev *pdev, int state, bool sem)
>  {
>  	struct pci_dev *parent = pdev->bus->self;
> diff --git a/include/linux/pci-aspm.h b/include/linux/pci-aspm.h
> index df28af5cef21..cd0736b384ae 100644
> --- a/include/linux/pci-aspm.h
> +++ b/include/linux/pci-aspm.h
> @@ -24,10 +24,12 @@
>  #define PCIE_LINK_STATE_CLKPM	4
>  
>  #ifdef CONFIG_PCIEASPM
> +void pci_enable_link_state(struct pci_dev *pdev, int state);
>  void pci_disable_link_state(struct pci_dev *pdev, int state);
>  void pci_disable_link_state_locked(struct pci_dev *pdev, int state);
>  void pcie_no_aspm(void);
>  #else
> +static inline void pci_enable_link_state(struct pci_dev *pdev, int state) { }
>  static inline void pci_disable_link_state(struct pci_dev *pdev, int state) { }
>  static inline void pcie_no_aspm(void) { }
>  #endif

^ permalink raw reply	[flat|nested] 11+ messages in thread

* 答复: 答复: 答复: [PATCH v6] mfd: Add support for RTS5250S power saving
  2018-01-17 21:01           ` Bjorn Helgaas
@ 2018-01-19  7:38             ` 冯锐
  2018-01-30 19:18               ` Bjorn Helgaas
  0 siblings, 1 reply; 11+ messages in thread
From: 冯锐 @ 2018-01-19  7:38 UTC (permalink / raw)
  To: Bjorn Helgaas
  Cc: lee.jones, linux-kernel, Hans de Goede, Dave Jiang, linux-pci

> On Wed, Dec 27, 2017 at 05:37:50PM -0600, Bjorn Helgaas wrote:
> > On Tue, Dec 19, 2017 at 08:15:24AM +0000, 冯锐 wrote:
> > > > On Fri, Dec 15, 2017 at 09:42:45AM +0000, 冯锐 wrote:
> > > > > > [+cc Hans, Dave, linux-pci]
> > > > > >
> > > > > > On Thu, Sep 07, 2017 at 04:26:39PM +0800,
> > > > > > rui_feng@realsil.com.cn
> > > > wrote:
> > > > > > > From: Rui Feng <rui_feng@realsil.com.cn>
> > > > > >
> > > > > > I wish this had been posted to linux-pci before being merged.
> > > > > >
> > > > > > I'm concerned because some of this appears to overlap and
> > > > > > conflict with PCI core management of ASPM.
> > > > > >
> > > > > > I assume these devices advertise ASPM support in their Link
> > > > > > Capabilites registers, right?  If so, why isn't the existing
> > > > > > PCI core ASPM support sufficient?
> > > > > >
> > > > > When L1SS is configured, the device(hardware) can't enter L1SS
> > > > > status automatically, it need driver(software) to do some work
> > > > > to achieve the
> > > > function.
> > > >
> > > > So this is a hardware defect in the device?  As far as I know,
> > > > ASPM and L1SS are specified such that they should work without special
> driver support.
> > > >
> > > Yes, you can say that.
> > >
> > > > > > > Enable power saving for RTS5250S as following steps:
> > > > > > > 1.Set 0xFE58 to enable clock power management.
> > > > > >
> > > > > > Is this clock power management something specific to RTS5250S,
> > > > > > or is it standard PCIe architected stuff?
> > > > > >
> > > > > 0xFE58 is specific register to RTS5250S not standard PCIe architected
> stuff.
> > > >
> > > > OK.  I asked because devices often mirror architected PCIe config
> > > > things in device-specific MMIO space, and if I squint just right,
> > > > I can sort of match up the register bits you used with things in the PCIe
> spec.
> > > >
> > > > > > > 2.Check cfg space whether support L1SS or not.
> > > > > >
> > > > > > This sounds like standard PCIe ASPM L1 Substates, right?
> > > > > >
> > > > > Yes.
> > > > >
> > > > > > > 3.If support L1SS, set 0xFF03 to free clkreq.
> > > > > > > 4.When entering idle status, enable aspm
> > > > > > >   and set parameters for L1SS and LTR.
> > > > > > > 5.Wnen entering run status, disable aspm
> > > > > > >   and set parameters for L1SS and LTR.
> > > > > >
> > > > > > In general, drivers should not configure ASPM, L1SS, and LTR
> > > > > > themselves; the PCI core should do that.
> > > > > >
> > > > > > If a driver needs to tweak ASPM at run-time, it should use
> > > > > > interfaces exported by the PCI core to do so.
> > > > > >
> > > > > Which interface I can use to set ASPM? I use "pci_write_config_byte"
> now.
> > > >
> > > > What do you need to do?  include/linux/pci-aspm.h exports
> > > > pci_disable_link_state(), which is mainly used to avoid ASPM
> > > > states that have hardware errata.
> > > >
> > > I want to enable ASPM(L0 -> L1) and disable ASPM(L1 -> L0), which
> > > interface can I use?
> >
> > You can use pci_disable_link_state() to disable usage of L1.
> >
> > Currently there is no corresponding pci_enable_link_state().  What if
> > we added something like the following (untested)?  Would that work for
> > you?
> 
> Hi Rui,
> 
> Any thoughts on the patch below?

I'm busy with other work, the patch seems ok, I will test it later.
> 
> > commit 209930d809fa602b8aafdd171b26719cee6c6649
> > Author: Bjorn Helgaas <bhelgaas@google.com>
> > Date:   Wed Dec 27 16:56:26 2017 -0600
> >
> >     PCI/ASPM: Add pci_enable_link_state()
> >
> >     Some drivers want control over the ASPM states their device is allowed
> to
> >     use.  We already have a pci_disable_link_state(), and drivers can use
> that
> >     to prevent the device from entering L0 or L1s.
> >
> >     Add a corresponding pci_enable_link_state() so a driver can enable use
> of
> >     L0 or L1s again.
> >
> > diff --git a/drivers/pci/pcie/aspm.c b/drivers/pci/pcie/aspm.c index
> > 3b9b4d50cd98..ca217195f800 100644
> > --- a/drivers/pci/pcie/aspm.c
> > +++ b/drivers/pci/pcie/aspm.c
> > @@ -1028,6 +1028,67 @@ void pcie_aspm_powersave_config_link(struct
> pci_dev *pdev)
> >  	up_read(&pci_bus_sem);
> >  }
> >
> > +/**
> > + * pci_enable_link_state - Enable device's link state, so the link
> > +may
> > + * enter specific states.  Note that if the BIOS didn't grant ASPM
> > + * control to the OS, this does nothing because we can't touch the
> > +LNKCTL
> > + * register.
> > + *
> > + * @pdev: PCI device
> > + * @state: ASPM link state to enable
> > + */
> > +void pci_enable_link_state(struct pci_dev *pdev, int state) {
> > +	struct pci_dev *parent = pdev->bus->self;
> > +	struct pcie_link_state *link;
> > +	u32 lnkcap;
> > +
> > +	if (!pci_is_pcie(pdev))
> > +		return;
> > +
> > +	if (pdev->has_secondary_link)
> > +		parent = pdev;
> > +	if (!parent || !parent->link_state)
> > +		return;
> > +
> > +	/*
> > +	 * A driver requested that ASPM be enabled on this device, but
> > +	 * if we don't have permission to manage ASPM (e.g., on ACPI
> > +	 * systems we have to observe the FADT ACPI_FADT_NO_ASPM bit and
> > +	 * the _OSC method), we can't honor that request.  Windows has
> > +	 * a similar mechanism using "PciASPMOptOut", which is also
> > +	 * ignored in this situation.
> > +	 */
> > +	if (aspm_disabled) {
> > +		dev_warn(&pdev->dev, "can't enable ASPM; OS doesn't have ASPM
> control\n");
> > +		return;
> > +	}
> > +
> > +	down_read(&pci_bus_sem);
> > +	mutex_lock(&aspm_lock);
> > +	link = parent->link_state;
> > +	if (state & PCIE_LINK_STATE_L0S)
> > +		link->aspm_disable &= ~ASPM_STATE_L0S;
> > +	if (state & PCIE_LINK_STATE_L1)
> > +		link->aspm_disable &= ~ASPM_STATE_L1;
> > +	pcie_config_aspm_link(link, policy_to_aspm_state(link));
> > +
> > +	/*
> > +	 * Enable Clock Power Management if requested by the driver,
> > +	 * supported by the device, and allowed by the current policy.
> > +	 */
> > +	if (state & PCIE_LINK_STATE_CLKPM) {
> > +		pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, &lnkcap);
> > +		if (lnkcap & PCI_EXP_LNKCAP_CLKPM) {
> > +			link->clkpm_capable = 1;
> > +			pcie_set_clkpm(link, policy_to_clkpm_state(link));
> > +		}
> > +	}
> > +	mutex_unlock(&aspm_lock);
> > +	up_read(&pci_bus_sem);
> > +}
> > +EXPORT_SYMBOL(pci_enable_link_state);
> > +
> >  static void __pci_disable_link_state(struct pci_dev *pdev, int state,
> > bool sem)  {
> >  	struct pci_dev *parent = pdev->bus->self; diff --git
> > a/include/linux/pci-aspm.h b/include/linux/pci-aspm.h index
> > df28af5cef21..cd0736b384ae 100644
> > --- a/include/linux/pci-aspm.h
> > +++ b/include/linux/pci-aspm.h
> > @@ -24,10 +24,12 @@
> >  #define PCIE_LINK_STATE_CLKPM	4
> >
> >  #ifdef CONFIG_PCIEASPM
> > +void pci_enable_link_state(struct pci_dev *pdev, int state);
> >  void pci_disable_link_state(struct pci_dev *pdev, int state);  void
> > pci_disable_link_state_locked(struct pci_dev *pdev, int state);  void
> > pcie_no_aspm(void);  #else
> > +static inline void pci_enable_link_state(struct pci_dev *pdev, int
> > +state) { }
> >  static inline void pci_disable_link_state(struct pci_dev *pdev, int
> > state) { }  static inline void pcie_no_aspm(void) { }  #endif
> 
> ------Please consider the environment before printing this e-mail.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: 答复: 答复: 答复: [PATCH v6] mfd: Add support for RTS5250S power saving
  2018-01-19  7:38             ` 答复: " 冯锐
@ 2018-01-30 19:18               ` Bjorn Helgaas
  2018-02-01  8:45                 ` 答复: " 冯锐
  0 siblings, 1 reply; 11+ messages in thread
From: Bjorn Helgaas @ 2018-01-30 19:18 UTC (permalink / raw)
  To: 冯锐
  Cc: lee.jones, linux-kernel, Hans de Goede, Dave Jiang, linux-pci

On Fri, Jan 19, 2018 at 07:38:22AM +0000, 冯锐 wrote:
> > On Wed, Dec 27, 2017 at 05:37:50PM -0600, Bjorn Helgaas wrote:
> > > On Tue, Dec 19, 2017 at 08:15:24AM +0000, 冯锐 wrote:
> > > > > On Fri, Dec 15, 2017 at 09:42:45AM +0000, 冯锐 wrote:
> > > > > > > [+cc Hans, Dave, linux-pci]
> > > > > > >
> > > > > > > On Thu, Sep 07, 2017 at 04:26:39PM +0800,
> > > > > > > rui_feng@realsil.com.cn
> > > > > wrote:
> > > > > > > > From: Rui Feng <rui_feng@realsil.com.cn>
> > > > > > >
> > > > > > > I wish this had been posted to linux-pci before being merged.
> > > > > > >
> > > > > > > I'm concerned because some of this appears to overlap and
> > > > > > > conflict with PCI core management of ASPM.
> > > > > > >
> > > > > > > I assume these devices advertise ASPM support in their Link
> > > > > > > Capabilites registers, right?  If so, why isn't the existing
> > > > > > > PCI core ASPM support sufficient?
> > > > > > >
> > > > > > When L1SS is configured, the device(hardware) can't enter L1SS
> > > > > > status automatically, it need driver(software) to do some work
> > > > > > to achieve the
> > > > > function.
> > > > >
> > > > > So this is a hardware defect in the device?  As far as I know,
> > > > > ASPM and L1SS are specified such that they should work without special
> > driver support.
> > > > >
> > > > Yes, you can say that.
> > > >
> > > > > > > > Enable power saving for RTS5250S as following steps:
> > > > > > > > 1.Set 0xFE58 to enable clock power management.
> > > > > > >
> > > > > > > Is this clock power management something specific to RTS5250S,
> > > > > > > or is it standard PCIe architected stuff?
> > > > > > >
> > > > > > 0xFE58 is specific register to RTS5250S not standard PCIe architected
> > stuff.
> > > > >
> > > > > OK.  I asked because devices often mirror architected PCIe config
> > > > > things in device-specific MMIO space, and if I squint just right,
> > > > > I can sort of match up the register bits you used with things in the PCIe
> > spec.
> > > > >
> > > > > > > > 2.Check cfg space whether support L1SS or not.
> > > > > > >
> > > > > > > This sounds like standard PCIe ASPM L1 Substates, right?
> > > > > > >
> > > > > > Yes.
> > > > > >
> > > > > > > > 3.If support L1SS, set 0xFF03 to free clkreq.
> > > > > > > > 4.When entering idle status, enable aspm
> > > > > > > >   and set parameters for L1SS and LTR.
> > > > > > > > 5.Wnen entering run status, disable aspm
> > > > > > > >   and set parameters for L1SS and LTR.
> > > > > > >
> > > > > > > In general, drivers should not configure ASPM, L1SS, and LTR
> > > > > > > themselves; the PCI core should do that.
> > > > > > >
> > > > > > > If a driver needs to tweak ASPM at run-time, it should use
> > > > > > > interfaces exported by the PCI core to do so.
> > > > > > >
> > > > > > Which interface I can use to set ASPM? I use "pci_write_config_byte"
> > now.
> > > > >
> > > > > What do you need to do?  include/linux/pci-aspm.h exports
> > > > > pci_disable_link_state(), which is mainly used to avoid ASPM
> > > > > states that have hardware errata.
> > > > >
> > > > I want to enable ASPM(L0 -> L1) and disable ASPM(L1 -> L0), which
> > > > interface can I use?
> > >
> > > You can use pci_disable_link_state() to disable usage of L1.
> > >
> > > Currently there is no corresponding pci_enable_link_state().  What if
> > > we added something like the following (untested)?  Would that work for
> > > you?
> > 
> > Hi Rui,
> > 
> > Any thoughts on the patch below?
> 
> I'm busy with other work, the patch seems ok, I will test it later.

Any idea when you might get back to this?

If you can't do it, I can try doing it myself, but of course, I don't
know the details about the device errata and I wouldn't be able to
test it.

Bjorn

^ permalink raw reply	[flat|nested] 11+ messages in thread

* 答复: 答复: 答复: 答复: [PATCH v6] mfd: Add support for RTS5250S power saving
  2018-01-30 19:18               ` Bjorn Helgaas
@ 2018-02-01  8:45                 ` 冯锐
  0 siblings, 0 replies; 11+ messages in thread
From: 冯锐 @ 2018-02-01  8:45 UTC (permalink / raw)
  To: Bjorn Helgaas
  Cc: lee.jones, linux-kernel, Hans de Goede, Dave Jiang, linux-pci

> 
> On Fri, Jan 19, 2018 at 07:38:22AM +0000, 冯锐 wrote:
> > > On Wed, Dec 27, 2017 at 05:37:50PM -0600, Bjorn Helgaas wrote:
> > > > On Tue, Dec 19, 2017 at 08:15:24AM +0000, 冯锐 wrote:
> > > > > > On Fri, Dec 15, 2017 at 09:42:45AM +0000, 冯锐 wrote:
> > > > > > > > [+cc Hans, Dave, linux-pci]
> > > > > > > >
> > > > > > > > On Thu, Sep 07, 2017 at 04:26:39PM +0800,
> > > > > > > > rui_feng@realsil.com.cn
> > > > > > wrote:
> > > > > > > > > From: Rui Feng <rui_feng@realsil.com.cn>
> > > > > > > >
> > > > > > > > I wish this had been posted to linux-pci before being merged.
> > > > > > > >
> > > > > > > > I'm concerned because some of this appears to overlap and
> > > > > > > > conflict with PCI core management of ASPM.
> > > > > > > >
> > > > > > > > I assume these devices advertise ASPM support in their
> > > > > > > > Link Capabilites registers, right?  If so, why isn't the
> > > > > > > > existing PCI core ASPM support sufficient?
> > > > > > > >
> > > > > > > When L1SS is configured, the device(hardware) can't enter
> > > > > > > L1SS status automatically, it need driver(software) to do
> > > > > > > some work to achieve the
> > > > > > function.
> > > > > >
> > > > > > So this is a hardware defect in the device?  As far as I know,
> > > > > > ASPM and L1SS are specified such that they should work without
> > > > > > special
> > > driver support.
> > > > > >
> > > > > Yes, you can say that.
> > > > >
> > > > > > > > > Enable power saving for RTS5250S as following steps:
> > > > > > > > > 1.Set 0xFE58 to enable clock power management.
> > > > > > > >
> > > > > > > > Is this clock power management something specific to
> > > > > > > > RTS5250S, or is it standard PCIe architected stuff?
> > > > > > > >
> > > > > > > 0xFE58 is specific register to RTS5250S not standard PCIe
> > > > > > > architected
> > > stuff.
> > > > > >
> > > > > > OK.  I asked because devices often mirror architected PCIe
> > > > > > config things in device-specific MMIO space, and if I squint
> > > > > > just right, I can sort of match up the register bits you used
> > > > > > with things in the PCIe
> > > spec.
> > > > > >
> > > > > > > > > 2.Check cfg space whether support L1SS or not.
> > > > > > > >
> > > > > > > > This sounds like standard PCIe ASPM L1 Substates, right?
> > > > > > > >
> > > > > > > Yes.
> > > > > > >
> > > > > > > > > 3.If support L1SS, set 0xFF03 to free clkreq.
> > > > > > > > > 4.When entering idle status, enable aspm
> > > > > > > > >   and set parameters for L1SS and LTR.
> > > > > > > > > 5.Wnen entering run status, disable aspm
> > > > > > > > >   and set parameters for L1SS and LTR.
> > > > > > > >
> > > > > > > > In general, drivers should not configure ASPM, L1SS, and
> > > > > > > > LTR themselves; the PCI core should do that.
> > > > > > > >
> > > > > > > > If a driver needs to tweak ASPM at run-time, it should use
> > > > > > > > interfaces exported by the PCI core to do so.
> > > > > > > >
> > > > > > > Which interface I can use to set ASPM? I use
> "pci_write_config_byte"
> > > now.
> > > > > >
> > > > > > What do you need to do?  include/linux/pci-aspm.h exports
> > > > > > pci_disable_link_state(), which is mainly used to avoid ASPM
> > > > > > states that have hardware errata.
> > > > > >
> > > > > I want to enable ASPM(L0 -> L1) and disable ASPM(L1 -> L0),
> > > > > which interface can I use?
> > > >
> > > > You can use pci_disable_link_state() to disable usage of L1.
> > > >
> > > > Currently there is no corresponding pci_enable_link_state().  What
> > > > if we added something like the following (untested)?  Would that
> > > > work for you?
> > >
> > > Hi Rui,
> > >
> > > Any thoughts on the patch below?
> >
> > I'm busy with other work, the patch seems ok, I will test it later.
> 
> Any idea when you might get back to this?
> 
> If you can't do it, I can try doing it myself, but of course, I don't know the details
> about the device errata and I wouldn't be able to test it.
> 
Of course, you can do it first.

> Bjorn
> 
> ------Please consider the environment before printing this e-mail.

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2018-02-01  8:46 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-07  8:26 [PATCH v6] mfd: Add support for RTS5250S power saving rui_feng
2017-09-07 10:57 ` Lee Jones
2017-12-14 22:25 ` Bjorn Helgaas
2017-12-15  7:41   ` Hans de Goede
     [not found]   ` <2A308283684ECD4B896628E09AF5361E01A8150B@RS-MBS01.realsil.com.cn>
2017-12-15 15:15     ` 答复: " Bjorn Helgaas
2017-12-19  8:15       ` 答复: " 冯锐
2017-12-27 23:37         ` Bjorn Helgaas
2018-01-17 21:01           ` Bjorn Helgaas
2018-01-19  7:38             ` 答复: " 冯锐
2018-01-30 19:18               ` Bjorn Helgaas
2018-02-01  8:45                 ` 答复: " 冯锐

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®