Skip to content

Commit

Permalink
mt76: mt7921: introduce Runtime PM support
Browse files Browse the repository at this point in the history
Introduce runtime PM to mt7921 driver

Co-developed-by: Lorenzo Bianconi <lorenzo@kernel.org>
Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
Signed-off-by: Sean Wang <sean.wang@mediatek.com>
Signed-off-by: Felix Fietkau <nbd@nbd.name>
  • Loading branch information
Sean Wang authored and Felix Fietkau committed Jan 29, 2021
1 parent 022159b commit 1d8efc7
Show file tree
Hide file tree
Showing 8 changed files with 372 additions and 54 deletions.
57 changes: 57 additions & 0 deletions drivers/net/wireless/mediatek/mt76/mt7921/debugfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,60 @@ mt7921_queues_read(struct seq_file *s, void *data)
return 0;
}

static int
mt7921_pm_set(void *data, u64 val)
{
struct mt7921_dev *dev = data;
struct mt76_phy *mphy = dev->phy.mt76;
int ret = 0;

mt7921_mutex_acquire(dev);

dev->pm.enable = val;

ieee80211_iterate_active_interfaces(mphy->hw,
IEEE80211_IFACE_ITER_RESUME_ALL,
mt7921_pm_interface_iter, mphy->priv);
mt7921_mutex_release(dev);

return ret;
}

static int
mt7921_pm_get(void *data, u64 *val)
{
struct mt7921_dev *dev = data;

*val = dev->pm.enable;

return 0;
}

DEFINE_DEBUGFS_ATTRIBUTE(fops_pm, mt7921_pm_get, mt7921_pm_set, "%lld\n");

static int
mt7921_pm_idle_timeout_set(void *data, u64 val)
{
struct mt7921_dev *dev = data;

dev->pm.idle_timeout = msecs_to_jiffies(val);

return 0;
}

static int
mt7921_pm_idle_timeout_get(void *data, u64 *val)
{
struct mt7921_dev *dev = data;

*val = jiffies_to_msecs(dev->pm.idle_timeout);

return 0;
}

DEFINE_DEBUGFS_ATTRIBUTE(fops_pm_idle_timeout, mt7921_pm_idle_timeout_get,
mt7921_pm_idle_timeout_set, "%lld\n");

int mt7921_init_debugfs(struct mt7921_dev *dev)
{
struct dentry *dir;
Expand All @@ -173,6 +227,9 @@ int mt7921_init_debugfs(struct mt7921_dev *dev)
mt7921_queues_acq);
debugfs_create_file("tx_stats", 0400, dir, dev, &fops_tx_stats);
debugfs_create_file("fw_debug", 0600, dir, dev, &fops_fw_debug);
debugfs_create_file("runtime-pm", 0600, dir, dev, &fops_pm);
debugfs_create_file("idle-timeout", 0600, dir, dev,
&fops_pm_idle_timeout);

return 0;
}
7 changes: 7 additions & 0 deletions drivers/net/wireless/mediatek/mt76/mt7921/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ int mt7921_register_device(struct mt7921_dev *dev)
dev->phy.dev = dev;
dev->phy.mt76 = &dev->mt76.phy;
dev->mt76.phy.priv = &dev->phy;

INIT_DELAYED_WORK(&dev->pm.ps_work, mt7921_pm_power_save_work);
INIT_WORK(&dev->pm.wake_work, mt7921_pm_wake_work);
init_completion(&dev->pm.wake_cmpl);
spin_lock_init(&dev->pm.txq_lock);
set_bit(MT76_STATE_PM, &dev->mphy.state);
INIT_LIST_HEAD(&dev->phy.stats_list);
INIT_DELAYED_WORK(&dev->mphy.mac_work, mt7921_mac_work);
INIT_DELAYED_WORK(&dev->phy.scan_work, mt7921_scan_work);
Expand All @@ -216,6 +222,7 @@ int mt7921_register_device(struct mt7921_dev *dev)
return ret;

mt7921_init_wiphy(hw);
dev->pm.idle_timeout = MT7921_PM_TIMEOUT;
dev->mphy.sband_2g.sband.ht_cap.cap |=
IEEE80211_HT_CAP_LDPC_CODING |
IEEE80211_HT_CAP_MAX_AMSDU;
Expand Down
98 changes: 89 additions & 9 deletions drivers/net/wireless/mediatek/mt76/mt7921/mac.c
Original file line number Diff line number Diff line change
Expand Up @@ -1001,22 +1001,27 @@ void mt7921_mac_tx_free(struct mt7921_dev *dev, struct sk_buff *skb)
mt76_put_txwi(mdev, txwi);
}

mt7921_mac_sta_poll(dev);

if (wake) {
spin_lock_bh(&dev->token_lock);
mt7921_set_tx_blocked(dev, false);
spin_unlock_bh(&dev->token_lock);
}

mt76_worker_schedule(&dev->mt76.tx_worker);

napi_consume_skb(skb, 1);

list_for_each_entry_safe(skb, tmp, &free_list, list) {
skb_list_del_init(skb);
napi_consume_skb(skb, 1);
}

if (test_bit(MT76_STATE_PM, &dev->phy.mt76->state))
return;

mt7921_mac_sta_poll(dev);

mt76_connac_power_save_sched(&dev->mphy, &dev->pm);

mt76_worker_schedule(&dev->mt76.tx_worker);
}

void mt7921_tx_complete_skb(struct mt76_dev *mdev, struct mt76_queue_entry *e)
Expand Down Expand Up @@ -1166,9 +1171,14 @@ void mt7921_update_channel(struct mt76_dev *mdev)
{
struct mt7921_dev *dev = container_of(mdev, struct mt7921_dev, mt76);

if (mt76_connac_pm_wake(&dev->mphy, &dev->pm))
return;

mt7921_phy_update_channel(&mdev->phy, 0);
/* reset obss airtime */
mt76_set(dev, MT_WF_RMAC_MIB_TIME0(0), MT_WF_RMAC_MIB_RXTIME_CLR);

mt76_connac_power_save_sched(&dev->mphy, &dev->pm);
}

static bool
Expand Down Expand Up @@ -1257,7 +1267,7 @@ void mt7921_mac_reset_work(struct work_struct *work)
napi_disable(&dev->mt76.napi[2]);
napi_disable(&dev->mt76.tx_napi);

mutex_lock(&dev->mt76.mutex);
mt7921_mutex_acquire(dev);

mt76_wr(dev, MT_MCU_INT_EVENT, MT_MCU_INT_EVENT_DMA_STOPPED);

Expand Down Expand Up @@ -1292,7 +1302,7 @@ void mt7921_mac_reset_work(struct work_struct *work)
mt76_wr(dev, MT_MCU_INT_EVENT, MT_MCU_INT_EVENT_RESET_DONE);
mt7921_wait_reset_state(dev, MT_MCU_CMD_NORMAL_STATE);

mutex_unlock(&dev->mt76.mutex);
mt7921_mutex_release(dev);

ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mphy.mac_work,
MT7921_WATCHDOG_TIME);
Expand Down Expand Up @@ -1373,7 +1383,10 @@ void mt7921_mac_work(struct work_struct *work)
mac_work.work);
phy = mphy->priv;

mutex_lock(&mphy->dev->mutex);
if (test_bit(MT76_STATE_PM, &mphy->state))
goto out;

mt7921_mutex_acquire(phy->dev);

mt76_update_survey(mphy->dev);
if (++mphy->mac_work_count == 5) {
Expand All @@ -1386,8 +1399,75 @@ void mt7921_mac_work(struct work_struct *work)
mt7921_mac_sta_stats_work(phy);
};

mutex_unlock(&mphy->dev->mutex);
mt7921_mutex_release(phy->dev);

ieee80211_queue_delayed_work(mphy->hw, &mphy->mac_work,
out:
ieee80211_queue_delayed_work(phy->mt76->hw, &mphy->mac_work,
MT7921_WATCHDOG_TIME);
}

void mt7921_pm_wake_work(struct work_struct *work)
{
struct mt7921_dev *dev;
struct mt76_phy *mphy;

dev = (struct mt7921_dev *)container_of(work, struct mt7921_dev,
pm.wake_work);
mphy = dev->phy.mt76;

if (!mt7921_mcu_drv_pmctrl(dev))
mt76_connac_pm_dequeue_skbs(mphy, &dev->pm);
else
dev_err(mphy->dev->dev, "failed to wake device\n");

ieee80211_wake_queues(mphy->hw);
complete_all(&dev->pm.wake_cmpl);
}

void mt7921_pm_power_save_work(struct work_struct *work)
{
struct mt7921_dev *dev;
unsigned long delta;

dev = (struct mt7921_dev *)container_of(work, struct mt7921_dev,
pm.ps_work.work);

delta = dev->pm.idle_timeout;
if (time_is_after_jiffies(dev->pm.last_activity + delta)) {
delta = dev->pm.last_activity + delta - jiffies;
goto out;
}

if (!mt7921_mcu_fw_pmctrl(dev))
return;
out:
queue_delayed_work(dev->mt76.wq, &dev->pm.ps_work, delta);
}

int mt7921_mac_set_beacon_filter(struct mt7921_phy *phy,
struct ieee80211_vif *vif,
bool enable)
{
struct mt7921_dev *dev = phy->dev;
bool ext_phy = phy != &dev->phy;
int err;

if (!dev->pm.enable)
return -EOPNOTSUPP;

err = mt7921_mcu_set_bss_pm(dev, vif, enable);
if (err)
return err;

if (enable) {
vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
mt76_set(dev, MT_WF_RFCR(ext_phy),
MT_WF_RFCR_DROP_OTHER_BEACON);
} else {
vif->driver_flags &= ~IEEE80211_VIF_BEACON_FILTER;
mt76_clear(dev, MT_WF_RFCR(ext_phy),
MT_WF_RFCR_DROP_OTHER_BEACON);
}

return 0;
}
Loading

0 comments on commit 1d8efc7

Please sign in to comment.