thunderbolt: Changes for v4.19 merge window
cover thunderbolt: Changes for v4.19 merge window
Hi Greg,
Please pull Thunderbolt changes for v4.19 merge window.
This adds runtime PM support for the Thunderbolt driver to save energy in recent systems where the controller is present all the time. In addition there is a fix for a typo in variable name and a removal of an unused variable.
I've included individual patches as well.
The following changes since commit 7daf201d7fe8334e2d2364d4e8ed3394ec9af819:
Linux 4.18-rc2 (2018-06-24 20:54:29 +0800)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/westeri/thunderbolt.git tags/thunderbolt-for-v4.19
for you to fetch changes up to 33a7231d8ebbc4a4e05adcbc7ac053ad20dbc054:
thunderbolt: Remove redundant variable 'approved' (2018-07-15 09:40:25 +0300)
Colin Ian King (1):
thunderbolt: Remove redundant variable 'approved'
Mika Westerberg (5):
thunderbolt: Use 64-bit DMA mask if supported by the platform
thunderbolt: Do not unnecessarily call ICM get route
thunderbolt: No need to take tb->lock in domain suspend/complete
thunderbolt: Use correct ICM commands in system suspend
thunderbolt: Add support for runtime PM
Nathan Ciobanu (1):
thunderbolt: Fix small typo in variable name
drivers/thunderbolt/domain.c | 55 ++++++++--- drivers/thunderbolt/icm.c | 174 ++++++++++++++++++++++++++++------ drivers/thunderbolt/nhi.c | 46 ++++++++- drivers/thunderbolt/switch.c | 65 ++++++++++++- drivers/thunderbolt/tb.h | 10 ++ drivers/thunderbolt/tb_msgs.h | 4 + drivers/thunderbolt/tb_regs.h | 2 +- drivers/thunderbolt/xdomain.c | 18 ++++ 8 files changed, 324 insertions(+), 50 deletions(-)
Signature · 1 line
2.18.0
PATCH 1/7 thunderbolt: Fix small typo in variable name
From: Nathan Ciobanu <[email protected]>
Fixes small variable name typo and the associated
checkpatch spelling warning.
--- drivers/thunderbolt/tb_regs.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/thunderbolt/tb_regs.h +1 -1
@@ -202,7 +202,7 @@ struct tb_regs_port_header {/* DWORD 5 */ u32 max_in_hop_id:11; u32 max_out_hop_id:11; - u32 __unkown4:10; + u32 __unknown4:10;/* DWORD 6 */ u32 __unknown5;/* DWORD 7 */
Signature · 1 line
2.18.0
PATCH 2/7 thunderbolt: Use 64-bit DMA mask if supported by the platform
In reply to Mika WesterbergPCI defaults to 32-bit DMA mask but this device is capable of full 64-bit addressing, so make sure we first try 64-bit DMA mask before falling back to the default 32-bit.
--- drivers/thunderbolt/nhi.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/thunderbolt/nhi.c +8 -0
@@ -1015,6 +1015,14 @@ static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)spin_lock_init (&nhi->lock); + res =dma_set_mask_and_coherent (&pdev->dev,DMA_BIT_MASK (64 )); +if (res) + res =dma_set_mask_and_coherent (&pdev->dev,DMA_BIT_MASK (32 )); +if (res) { +dev_err (&pdev->dev,"failed to set DMA mask\n" ); +return res; + } +pci_set_master (pdev); tb =icm_probe (nhi);
Signature · 1 line
2.18.0
PATCH 3/7 thunderbolt: Do not unnecessarily call ICM get route
In reply to Mika WesterbergThis command is not really fast and can make resume time slower. We only need to get route again if the link was changed and during initial device connected message.
--- drivers/thunderbolt/icm.c | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-)
drivers/thunderbolt/icm.c +26 -9
@@ -534,20 +534,13 @@ icm_fr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)return ; } - ret = icm->get_route (tb, link, depth, &route); -if (ret) { -tb_err (tb,"failed to find route string for switch at %u.%u\n" , - link, depth); -return ; - } - sw =tb_switch_find_by_uuid (tb, &pkg->ep_uuid);if (sw) {u8 phy_port, sw_phy_port; parent_sw =tb_to_switch (sw->dev.parent); - sw_phy_port =phy_port_from_route (tb_route (sw), sw->depth); - phy_port =phy_port_from_route (route, depth); + sw_phy_port =tb_phy_port_from_link (sw->link); + phy_port =tb_phy_port_from_link (link);/* * On resume ICM will send us connected events for the
@@ -559,6 +552,22 @@ icm_fr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)*/ if (sw->depth == depth && sw_phy_port == phy_port && !!sw->authorized == authorized) { +/* +* It was enumerated through another link so update +* route string accordingly. +*/ +if (sw->link != link) { + ret = icm->get_route (tb, link, depth, &route); +if (ret) { +tb_err (tb,"failed to update route string for switch at %u.%u\n" , + link, depth); +tb_switch_put (sw); +return ; + } + }else { + route =tb_route (sw); + } +update_switch (parent_sw, sw, route, pkg->connection_id, pkg->connection_key, link, depth, boot);tb_switch_put (sw);
@@ -607,6 +616,14 @@ icm_fr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)return ; } + ret = icm->get_route (tb, link, depth, &route); +if (ret) { +tb_err (tb,"failed to find route string for switch at %u.%u\n" , + link, depth); +tb_switch_put (parent_sw); +return ; + } +add_switch (parent_sw, route, &pkg->ep_uuid, pkg->connection_id, pkg->connection_key, link, depth, security_level, authorized, boot);
Signature · 1 line
2.18.0
PATCH 4/7 thunderbolt: No need to take tb->lock in domain suspend/complete
In reply to Mika WesterbergIf the connection manager implementation needs to touch the domain structures it ought to take the lock itself. Currently only ICM implements these hooks and it does not need the lock because we there will be no notifications before driver ready message is sent to it.
--- drivers/thunderbolt/domain.c | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-)
drivers/thunderbolt/domain.c +1 -14
@@ -505,26 +505,13 @@ int tb_domain_resume_noirq(struct tb *tb)int tb_domain_suspend (struct tb *tb) { -int ret; - -mutex_lock (&tb->lock); -if (tb->cm_ops->suspend) { - ret = tb->cm_ops->suspend (tb); -if (ret) { -mutex_unlock (&tb->lock); -return ret; - } - } -mutex_unlock (&tb->lock); -return 0 ; +return tb->cm_ops->suspend ? tb->cm_ops->suspend (tb) :0 ; }void tb_domain_complete (struct tb *tb) { -mutex_lock (&tb->lock);if (tb->cm_ops->complete) tb->cm_ops->complete (tb); -mutex_unlock (&tb->lock); }/**
Signature · 1 line
2.18.0
PATCH 5/7 thunderbolt: Use correct ICM commands in system suspend
In reply to Mika WesterbergThe correct way to put the ICM into suspend state is to send it NHI_MAILBOX_DRV_UNLOADS mailbox command. NHI_MAILBOX_SAVE_DEVS is not needed on Intel Titan Ridge so we can skip it.
--- drivers/thunderbolt/icm.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-)
drivers/thunderbolt/icm.c +13 -5
@@ -60,6 +60,7 @@* @is_supported: Checks if we can support ICM on this controller * @get_mode: Read and return the ICM firmware mode (optional) * @get_route: Find a route string for given switch +* @save_devices: Ask ICM to save devices to ACL when suspending (optional) * @driver_ready: Send driver ready message to ICM * @device_connected: Handle device connected ICM message * @device_disconnected: Handle device disconnected ICM message
@@ -76,6 +77,7 @@ struct icm {bool (*is_supported)(struct tb *tb);int (*get_mode)(struct tb *tb);int (*get_route)(struct tb *tb,u8 link,u8 depth,u64 *route); +void (*save_devices)(struct tb *tb);int (*driver_ready)(struct tb *tb,enum tb_security_level *security_level,size_t *nboot_acl);
@@ -258,6 +260,11 @@ static int icm_fr_get_route(struct tb *tb, u8 link, u8 depth, u64 *route)return ret; } +static void icm_fr_save_devices (struct tb *tb) +{ +nhi_mailbox_cmd (tb->nhi,NHI_MAILBOX_SAVE_DEVS ,0 ); +} +static int icm_fr_driver_ready (struct tb *tb,enum tb_security_level *security_level,size_t *nboot_acl)
@@ -1665,13 +1672,12 @@ static int icm_driver_ready(struct tb *tb)static int icm_suspend (struct tb *tb) { -int ret; +struct icm *icm =tb_priv (tb); - ret =nhi_mailbox_cmd (tb->nhi,NHI_MAILBOX_SAVE_DEVS ,0 ); -if (ret) -tb_info (tb,"Ignoring mailbox command error (%d) in %s\n" , - ret, __func__); +if (icm->save_devices) + icm->save_devices (tb); +nhi_mailbox_cmd (tb->nhi,NHI_MAILBOX_DRV_UNLOADS ,0 );return 0 ; }
@@ -1879,6 +1885,7 @@ struct tb *icm_probe(struct tb_nhi *nhi)case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI : icm->is_supported = icm_fr_is_supported; icm->get_route = icm_fr_get_route; + icm->save_devices = icm_fr_save_devices; icm->driver_ready = icm_fr_driver_ready; icm->device_connected = icm_fr_device_connected; icm->device_disconnected = icm_fr_device_disconnected;
@@ -1896,6 +1903,7 @@ struct tb *icm_probe(struct tb_nhi *nhi) icm->is_supported = icm_ar_is_supported; icm->get_mode = icm_ar_get_mode; icm->get_route = icm_ar_get_route; + icm->save_devices = icm_fr_save_devices; icm->driver_ready = icm_ar_driver_ready; icm->device_connected = icm_fr_device_connected; icm->device_disconnected = icm_fr_device_disconnected;
Signature · 1 line
2.18.0
PATCH 6/7 thunderbolt: Add support for runtime PM
In reply to Mika WesterbergWhen Thunderbolt host controller is set to RTD3 mode (Runtime D3) it is present all the time. Because of this it is important to runtime suspend the controller whenever possible. In case of ICM we have following rules which all needs to be true before the host controller can be put to D3:
- The controller firmware reports to support RTD3
- All the connected devices announce support for RTD3
- There is no active XDomain connection
Implement this using standard Linux runtime PM APIs so that when all the children devices are runtime suspended, the Thunderbolt host controller PCI device is runtime suspended as well. The ICM firmware then starts powering down power domains towards RTD3 but it can prevent this if it detects that there is an active Display Port stream (this is not visible to the software, though).
The Thunderbolt host controller will be runtime resumed either when there is a remote wake event (device is connected or disconnected), or when there is access from userspace that requires hardware access.
--- drivers/thunderbolt/domain.c | 42 +++++++++++- drivers/thunderbolt/icm.c | 119 ++++++++++++++++++++++++++++++---- drivers/thunderbolt/nhi.c | 38 ++++++++++- drivers/thunderbolt/switch.c | 65 +++++++++++++++++-- drivers/thunderbolt/tb.h | 10 +++ drivers/thunderbolt/tb_msgs.h | 4 ++ drivers/thunderbolt/xdomain.c | 18 +++++ 7 files changed, 276 insertions(+), 20 deletions(-)
drivers/thunderbolt/domain.c +41 -1
@@ -12,6 +12,7 @@#include <linux/device.h> #include <linux/idr.h> #include <linux/module.h> +#include <linux/pm_runtime.h> #include <linux/slab.h> #include <linux/random.h> #include <crypto/hash.h>
@@ -132,6 +133,8 @@ static ssize_t boot_acl_show(struct device *dev, struct device_attribute *attr,if (!uuids)return -ENOMEM ; +pm_runtime_get_sync (&tb->dev); +if (mutex_lock_interruptible (&tb->lock)) { ret = -ERESTARTSYS ; goto out;
@@ -153,7 +156,10 @@ static ssize_t boot_acl_show(struct device *dev, struct device_attribute *attr, } out: +pm_runtime_mark_last_busy (&tb->dev); +pm_runtime_put_autosuspend (&tb->dev);kfree (uuids); +return ret; }
@@ -208,13 +214,18 @@ static ssize_t boot_acl_store(struct device *dev, struct device_attribute *attr, goto err_free_acl; } +pm_runtime_get_sync (&tb->dev); +if (mutex_lock_interruptible (&tb->lock)) { ret = -ERESTARTSYS ; - goto err_free_acl; + goto err_rpm_put; } ret = tb->cm_ops->set_boot_acl (tb, acl, tb->nboot_acl);mutex_unlock (&tb->lock); +err_rpm_put: +pm_runtime_mark_last_busy (&tb->dev); +pm_runtime_put_autosuspend (&tb->dev); err_free_acl:kfree (acl); err_free_str:
@@ -426,6 +437,13 @@ int tb_domain_add(struct tb *tb)/* This starts event processing */ mutex_unlock (&tb->lock); +pm_runtime_no_callbacks (&tb->dev); +pm_runtime_set_active (&tb->dev); +pm_runtime_enable (&tb->dev); +pm_runtime_set_autosuspend_delay (&tb->dev,TB_AUTOSUSPEND_DELAY ); +pm_runtime_mark_last_busy (&tb->dev); +pm_runtime_use_autosuspend (&tb->dev); +return 0 ; err_domain_del:
@@ -514,6 +532,28 @@ void tb_domain_complete(struct tb *tb) tb->cm_ops->complete (tb); } +int tb_domain_runtime_suspend (struct tb *tb) +{ +if (tb->cm_ops->runtime_suspend) { +int ret = tb->cm_ops->runtime_suspend (tb); +if (ret) +return ret; + } +tb_ctl_stop (tb->ctl); +return 0 ; +} + +int tb_domain_runtime_resume (struct tb *tb) +{ +tb_ctl_start (tb->ctl); +if (tb->cm_ops->runtime_resume) { +int ret = tb->cm_ops->runtime_resume (tb); +if (ret) +return ret; + } +return 0 ; +} +/** * tb_domain_approve_switch() - Approve switch * @tb: Domain the switch belongs to
drivers/thunderbolt/icm.c +105 -14
278 lines not on this page · raw message
drivers/thunderbolt/nhi.c +37 -1
@@ -900,7 +900,32 @@ static void nhi_complete(struct device *dev)struct pci_dev *pdev =to_pci_dev (dev);struct tb *tb =pci_get_drvdata (pdev); -tb_domain_complete (tb); +/* +* If we were runtime suspended when system suspend started, +* schedule runtime resume now. It should bring the domain back +* to functional state. +*/ +if (pm_runtime_suspended (&pdev->dev)) +pm_runtime_resume (&pdev->dev); +else +tb_domain_complete (tb); +} + +static int nhi_runtime_suspend (struct device *dev) +{ +struct pci_dev *pdev =to_pci_dev (dev); +struct tb *tb =pci_get_drvdata (pdev); + +return tb_domain_runtime_suspend (tb); +} + +static int nhi_runtime_resume (struct device *dev) +{ +struct pci_dev *pdev =to_pci_dev (dev); +struct tb *tb =pci_get_drvdata (pdev); + +nhi_enable_int_throttling (tb->nhi); +return tb_domain_runtime_resume (tb); }static void nhi_shutdown (struct tb_nhi *nhi)
@@ -1048,6 +1073,11 @@ static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id) }pci_set_drvdata (pdev, tb); +pm_runtime_allow (&pdev->dev); +pm_runtime_set_autosuspend_delay (&pdev->dev,TB_AUTOSUSPEND_DELAY ); +pm_runtime_use_autosuspend (&pdev->dev); +pm_runtime_put_autosuspend (&pdev->dev); +return 0 ; }
@@ -1056,6 +1086,10 @@ static void nhi_remove(struct pci_dev *pdev)struct tb *tb =pci_get_drvdata (pdev);struct tb_nhi *nhi = tb->nhi; +pm_runtime_get_sync (&pdev->dev); +pm_runtime_dont_use_autosuspend (&pdev->dev); +pm_runtime_forbid (&pdev->dev); +tb_domain_remove (tb);nhi_shutdown (nhi); }
@@ -1078,6 +1112,8 @@ static const struct dev_pm_ops nhi_pm_ops = { .freeze = nhi_suspend, .poweroff = nhi_suspend, .complete = nhi_complete, + .runtime_suspend = nhi_runtime_suspend, + .runtime_resume = nhi_runtime_resume, };static struct pci_device_id nhi_ids[] = {
drivers/thunderbolt/switch.c +61 -4
127 lines not on this page · raw message
drivers/thunderbolt/tb.h +10 -0
@@ -67,6 +67,7 @@ struct tb_switch_nvm {* @no_nvm_upgrade: Prevent NVM upgrade of this switch * @safe_mode: The switch is in safe-mode * @boot: Whether the switch was already authorized on boot or not +* @rpm: The switch supports runtime PM * @authorized: Whether the switch is authorized by user or policy * @work: Work used to automatically authorize a switch * @security_level: Switch supported security level
@@ -101,6 +102,7 @@ struct tb_switch {bool no_nvm_upgrade;bool safe_mode;bool boot; +bool rpm;unsigned int authorized;struct work_struct work;enum tb_security_level security_level;
@@ -199,6 +201,8 @@ struct tb_path {* @resume_noirq: Connection manager specific resume_noirq * @suspend: Connection manager specific suspend * @complete: Connection manager specific complete +* @runtime_suspend: Connection manager specific runtime_suspend +* @runtime_resume: Connection manager specific runtime_resume * @handle_event: Handle thunderbolt event * @get_boot_acl: Get boot ACL list * @set_boot_acl: Set boot ACL list
@@ -217,6 +221,8 @@ struct tb_cm_ops {int (*resume_noirq)(struct tb *tb);int (*suspend)(struct tb *tb);void (*complete)(struct tb *tb); +int (*runtime_suspend)(struct tb *tb); +int (*runtime_resume)(struct tb *tb);void (*handle_event)(struct tb *tb,enum tb_cfg_pkg_type ,const void *buf,size_t size);int (*get_boot_acl)(struct tb *tb,uuid_t *uuids,size_t nuuids);
@@ -235,6 +241,8 @@ static inline void *tb_priv(struct tb *tb)return (void *)tb->privdata; } +#define TB_AUTOSUSPEND_DELAY 15000/* ms */ +/* helper functions & macros */ /**
@@ -364,6 +372,8 @@ int tb_domain_suspend_noirq(struct tb *tb);int tb_domain_resume_noirq (struct tb *tb);int tb_domain_suspend (struct tb *tb);void tb_domain_complete (struct tb *tb); +int tb_domain_runtime_suspend (struct tb *tb); +int tb_domain_runtime_resume (struct tb *tb);int tb_domain_approve_switch (struct tb *tb,struct tb_switch *sw);int tb_domain_approve_switch_key (struct tb *tb,struct tb_switch *sw);int tb_domain_challenge_switch_key (struct tb *tb,struct tb_switch *sw);
drivers/thunderbolt/tb_msgs.h +4 -0
@@ -286,6 +286,8 @@ struct icm_ar_pkg_driver_ready_response {u16 info; }; +#define ICM_AR_FLAGS_RTD3 BIT(6) +#define ICM_AR_INFO_SLEVEL_MASK GENMASK(3, 0)#define ICM_AR_INFO_BOOT_ACL_SHIFT 7#define ICM_AR_INFO_BOOT_ACL_MASK GENMASK(11, 7)
@@ -333,6 +335,8 @@ struct icm_tr_pkg_driver_ready_response {u16 reserved2; }; +#define ICM_TR_FLAGS_RTD3 BIT(6) +#define ICM_TR_INFO_SLEVEL_MASK GENMASK(2, 0)#define ICM_TR_INFO_BOOT_ACL_SHIFT 7#define ICM_TR_INFO_BOOT_ACL_MASK GENMASK(12, 7)
drivers/thunderbolt/xdomain.c +18 -0
@@ -13,6 +13,7 @@#include <linux/device.h> #include <linux/kmod.h> #include <linux/module.h> +#include <linux/pm_runtime.h> #include <linux/utsname.h> #include <linux/uuid.h> #include <linux/workqueue.h>
@@ -1129,6 +1130,14 @@ struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent, xd->dev.groups = xdomain_attr_groups;dev_set_name (&xd->dev,"%u-%llx" , tb->index, route); +/* +* This keeps the DMA powered on as long as we have active +* connection to another host. +*/ +pm_runtime_set_active (&xd->dev); +pm_runtime_get_noresume (&xd->dev); +pm_runtime_enable (&xd->dev); +return xd; err_free_local_uuid:
@@ -1174,6 +1183,15 @@ void tb_xdomain_remove(struct tb_xdomain *xd)device_for_each_child_reverse (&xd->dev, xd, unregister_service); +/* +* Undo runtime PM here explicitly because it is possible that +* the XDomain was never added to the bus and thus device_del() +* is not called for it (device_del() would handle this otherwise). +*/ +pm_runtime_disable (&xd->dev); +pm_runtime_put_noidle (&xd->dev); +pm_runtime_set_suspended (&xd->dev); +if (!device_is_registered (&xd->dev))put_device (&xd->dev);else
Signature · 1 line
2.18.0
Greg Kroah-Hartman
AppliedRe: [PATCH 6/7] thunderbolt: Add support for runtime PM
Quoted 21 lines · Mika Westerberg
When Thunderbolt host controller is set to RTD3 mode (Runtime D3) it is present all the time. Because of this it is important to runtime suspend the controller whenever possible. In case of ICM we have following rules which all needs to be true before the host controller can be put to D3:
- The controller firmware reports to support RTD3
- All the connected devices announce support for RTD3
- There is no active XDomain connectionImplement this using standard Linux runtime PM APIs so that when all the children devices are runtime suspended, the Thunderbolt host controller PCI device is runtime suspended as well. The ICM firmware then starts powering down power domains towards RTD3 but it can prevent this if it detects that there is an active Display Port stream (this is not visible to the software, though).
The Thunderbolt host controller will be runtime resumed either when there is a remote wake event (device is connected or disconnected), or when there is access from userspace that requires hardware access.
Signed-off-by Mika Westerberg <[email protected]>
Quoted 9 lines
--- drivers/thunderbolt/domain.c | 42 +++++++++++- drivers/thunderbolt/icm.c | 119 ++++++++++++++++++++++++++++++---- drivers/thunderbolt/nhi.c | 38 ++++++++++- drivers/thunderbolt/switch.c | 65 +++++++++++++++++-- drivers/thunderbolt/tb.h | 10 +++ drivers/thunderbolt/tb_msgs.h | 4 ++ drivers/thunderbolt/xdomain.c | 18 +++++ 7 files changed, 276 insertions(+), 20 deletions(-)
This patch did not apply to my tree at all, what branch did you make it against?
I've applied the others here, thanks.
greg k-h
Re: [PATCH 6/7] thunderbolt: Add support for runtime PM
Quoted 33 lines · Greg Kroah-Hartman
Quoted 21 lines · Mika Westerberg
When Thunderbolt host controller is set to RTD3 mode (Runtime D3) it is present all the time. Because of this it is important to runtime suspend the controller whenever possible. In case of ICM we have following rules which all needs to be true before the host controller can be put to D3:
- The controller firmware reports to support RTD3
- All the connected devices announce support for RTD3
- There is no active XDomain connectionImplement this using standard Linux runtime PM APIs so that when all the children devices are runtime suspended, the Thunderbolt host controller PCI device is runtime suspended as well. The ICM firmware then starts powering down power domains towards RTD3 but it can prevent this if it detects that there is an active Display Port stream (this is not visible to the software, though).
The Thunderbolt host controller will be runtime resumed either when there is a remote wake event (device is connected or disconnected), or when there is access from userspace that requires hardware access.
Signed-off-by Mika Westerberg <[email protected]>Quoted 9 lines
--- drivers/thunderbolt/domain.c | 42 +++++++++++- drivers/thunderbolt/icm.c | 119 ++++++++++++++++++++++++++++++---- drivers/thunderbolt/nhi.c | 38 ++++++++++- drivers/thunderbolt/switch.c | 65 +++++++++++++++++-- drivers/thunderbolt/tb.h | 10 +++ drivers/thunderbolt/tb_msgs.h | 4 ++ drivers/thunderbolt/xdomain.c | 18 +++++ 7 files changed, 276 insertions(+), 20 deletions(-)
Quoted 2 lines
This patch did not apply to my tree at all, what branch did you make it against?
It is based on v4.18-rc2 but I missed the fact that 007a74907dee ("thunderbolt: Notify userspace when boot_acl is changed") touches domain.c as well. I've just rebased this patch on top of your current char-misc-testing and will be sending it to your direction in a moment. Sorry about that.
Quoted 1 line
I've applied the others here, thanks.
Thanks!
PATCH v2 6/7 thunderbolt: Add support for runtime PM
In reply to Mika WesterbergWhen Thunderbolt host controller is set to RTD3 mode (Runtime D3) it is present all the time. Because of this it is important to runtime suspend the controller whenever possible. In case of ICM we have following rules which all needs to be true before the host controller can be put to D3:
- The controller firmware reports to support RTD3
- All the connected devices announce support for RTD3
- There is no active XDomain connection
Implement this using standard Linux runtime PM APIs so that when all the children devices are runtime suspended, the Thunderbolt host controller PCI device is runtime suspended as well. The ICM firmware then starts powering down power domains towards RTD3 but it can prevent this if it detects that there is an active Display Port stream (this is not visible to the software, though).
The Thunderbolt host controller will be runtime resumed either when there is a remote wake event (device is connected or disconnected), or when there is access from userspace that requires hardware access.
---
Rebased on top of char-misc-testing which includes 007a74907dee ("thunderbolt: Notify userspace when boot_acl is changed")
drivers/thunderbolt/domain.c | 42 +++++++++++- drivers/thunderbolt/icm.c | 119 ++++++++++++++++++++++++++++++---- drivers/thunderbolt/nhi.c | 38 ++++++++++- drivers/thunderbolt/switch.c | 65 +++++++++++++++++-- drivers/thunderbolt/tb.h | 10 +++ drivers/thunderbolt/tb_msgs.h | 4 ++ drivers/thunderbolt/xdomain.c | 18 +++++ 7 files changed, 276 insertions(+), 20 deletions(-)
drivers/thunderbolt/domain.c +41 -1
@@ -12,6 +12,7 @@#include <linux/device.h> #include <linux/idr.h> #include <linux/module.h> +#include <linux/pm_runtime.h> #include <linux/slab.h> #include <linux/random.h> #include <crypto/hash.h>
@@ -132,6 +133,8 @@ static ssize_t boot_acl_show(struct device *dev, struct device_attribute *attr,if (!uuids)return -ENOMEM ; +pm_runtime_get_sync (&tb->dev); +if (mutex_lock_interruptible (&tb->lock)) { ret = -ERESTARTSYS ; goto out;
@@ -153,7 +156,10 @@ static ssize_t boot_acl_show(struct device *dev, struct device_attribute *attr, } out: +pm_runtime_mark_last_busy (&tb->dev); +pm_runtime_put_autosuspend (&tb->dev);kfree (uuids); +return ret; }
@@ -208,9 +214,11 @@ static ssize_t boot_acl_store(struct device *dev, struct device_attribute *attr, goto err_free_acl; } +pm_runtime_get_sync (&tb->dev); +if (mutex_lock_interruptible (&tb->lock)) { ret = -ERESTARTSYS ; - goto err_free_acl; + goto err_rpm_put; } ret = tb->cm_ops->set_boot_acl (tb, acl, tb->nboot_acl);if (!ret) {
@@ -219,6 +227,9 @@ static ssize_t boot_acl_store(struct device *dev, struct device_attribute *attr, }mutex_unlock (&tb->lock); +err_rpm_put: +pm_runtime_mark_last_busy (&tb->dev); +pm_runtime_put_autosuspend (&tb->dev); err_free_acl:kfree (acl); err_free_str:
@@ -430,6 +441,13 @@ int tb_domain_add(struct tb *tb)/* This starts event processing */ mutex_unlock (&tb->lock); +pm_runtime_no_callbacks (&tb->dev); +pm_runtime_set_active (&tb->dev); +pm_runtime_enable (&tb->dev); +pm_runtime_set_autosuspend_delay (&tb->dev,TB_AUTOSUSPEND_DELAY ); +pm_runtime_mark_last_busy (&tb->dev); +pm_runtime_use_autosuspend (&tb->dev); +return 0 ; err_domain_del:
@@ -518,6 +536,28 @@ void tb_domain_complete(struct tb *tb) tb->cm_ops->complete (tb); } +int tb_domain_runtime_suspend (struct tb *tb) +{ +if (tb->cm_ops->runtime_suspend) { +int ret = tb->cm_ops->runtime_suspend (tb); +if (ret) +return ret; + } +tb_ctl_stop (tb->ctl); +return 0 ; +} + +int tb_domain_runtime_resume (struct tb *tb) +{ +tb_ctl_start (tb->ctl); +if (tb->cm_ops->runtime_resume) { +int ret = tb->cm_ops->runtime_resume (tb); +if (ret) +return ret; + } +return 0 ; +} +/** * tb_domain_approve_switch() - Approve switch * @tb: Domain the switch belongs to
drivers/thunderbolt/icm.c +105 -14
278 lines not on this page · raw message
drivers/thunderbolt/nhi.c +37 -1
@@ -900,7 +900,32 @@ static void nhi_complete(struct device *dev)struct pci_dev *pdev =to_pci_dev (dev);struct tb *tb =pci_get_drvdata (pdev); -tb_domain_complete (tb); +/* +* If we were runtime suspended when system suspend started, +* schedule runtime resume now. It should bring the domain back +* to functional state. +*/ +if (pm_runtime_suspended (&pdev->dev)) +pm_runtime_resume (&pdev->dev); +else +tb_domain_complete (tb); +} + +static int nhi_runtime_suspend (struct device *dev) +{ +struct pci_dev *pdev =to_pci_dev (dev); +struct tb *tb =pci_get_drvdata (pdev); + +return tb_domain_runtime_suspend (tb); +} + +static int nhi_runtime_resume (struct device *dev) +{ +struct pci_dev *pdev =to_pci_dev (dev); +struct tb *tb =pci_get_drvdata (pdev); + +nhi_enable_int_throttling (tb->nhi); +return tb_domain_runtime_resume (tb); }static void nhi_shutdown (struct tb_nhi *nhi)
@@ -1048,6 +1073,11 @@ static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id) }pci_set_drvdata (pdev, tb); +pm_runtime_allow (&pdev->dev); +pm_runtime_set_autosuspend_delay (&pdev->dev,TB_AUTOSUSPEND_DELAY ); +pm_runtime_use_autosuspend (&pdev->dev); +pm_runtime_put_autosuspend (&pdev->dev); +return 0 ; }
@@ -1056,6 +1086,10 @@ static void nhi_remove(struct pci_dev *pdev)struct tb *tb =pci_get_drvdata (pdev);struct tb_nhi *nhi = tb->nhi; +pm_runtime_get_sync (&pdev->dev); +pm_runtime_dont_use_autosuspend (&pdev->dev); +pm_runtime_forbid (&pdev->dev); +tb_domain_remove (tb);nhi_shutdown (nhi); }
@@ -1078,6 +1112,8 @@ static const struct dev_pm_ops nhi_pm_ops = { .freeze = nhi_suspend, .poweroff = nhi_suspend, .complete = nhi_complete, + .runtime_suspend = nhi_runtime_suspend, + .runtime_resume = nhi_runtime_resume, };static struct pci_device_id nhi_ids[] = {
drivers/thunderbolt/switch.c +61 -4
127 lines not on this page · raw message
drivers/thunderbolt/tb.h +10 -0
@@ -67,6 +67,7 @@ struct tb_switch_nvm {* @no_nvm_upgrade: Prevent NVM upgrade of this switch * @safe_mode: The switch is in safe-mode * @boot: Whether the switch was already authorized on boot or not +* @rpm: The switch supports runtime PM * @authorized: Whether the switch is authorized by user or policy * @work: Work used to automatically authorize a switch * @security_level: Switch supported security level
@@ -101,6 +102,7 @@ struct tb_switch {bool no_nvm_upgrade;bool safe_mode;bool boot; +bool rpm;unsigned int authorized;struct work_struct work;enum tb_security_level security_level;
@@ -199,6 +201,8 @@ struct tb_path {* @resume_noirq: Connection manager specific resume_noirq * @suspend: Connection manager specific suspend * @complete: Connection manager specific complete +* @runtime_suspend: Connection manager specific runtime_suspend +* @runtime_resume: Connection manager specific runtime_resume * @handle_event: Handle thunderbolt event * @get_boot_acl: Get boot ACL list * @set_boot_acl: Set boot ACL list
@@ -217,6 +221,8 @@ struct tb_cm_ops {int (*resume_noirq)(struct tb *tb);int (*suspend)(struct tb *tb);void (*complete)(struct tb *tb); +int (*runtime_suspend)(struct tb *tb); +int (*runtime_resume)(struct tb *tb);void (*handle_event)(struct tb *tb,enum tb_cfg_pkg_type ,const void *buf,size_t size);int (*get_boot_acl)(struct tb *tb,uuid_t *uuids,size_t nuuids);
@@ -235,6 +241,8 @@ static inline void *tb_priv(struct tb *tb)return (void *)tb->privdata; } +#define TB_AUTOSUSPEND_DELAY 15000/* ms */ +/* helper functions & macros */ /**
@@ -364,6 +372,8 @@ int tb_domain_suspend_noirq(struct tb *tb);int tb_domain_resume_noirq (struct tb *tb);int tb_domain_suspend (struct tb *tb);void tb_domain_complete (struct tb *tb); +int tb_domain_runtime_suspend (struct tb *tb); +int tb_domain_runtime_resume (struct tb *tb);int tb_domain_approve_switch (struct tb *tb,struct tb_switch *sw);int tb_domain_approve_switch_key (struct tb *tb,struct tb_switch *sw);int tb_domain_challenge_switch_key (struct tb *tb,struct tb_switch *sw);
drivers/thunderbolt/tb_msgs.h +4 -0
@@ -286,6 +286,8 @@ struct icm_ar_pkg_driver_ready_response {u16 info; }; +#define ICM_AR_FLAGS_RTD3 BIT(6) +#define ICM_AR_INFO_SLEVEL_MASK GENMASK(3, 0)#define ICM_AR_INFO_BOOT_ACL_SHIFT 7#define ICM_AR_INFO_BOOT_ACL_MASK GENMASK(11, 7)
@@ -333,6 +335,8 @@ struct icm_tr_pkg_driver_ready_response {u16 reserved2; }; +#define ICM_TR_FLAGS_RTD3 BIT(6) +#define ICM_TR_INFO_SLEVEL_MASK GENMASK(2, 0)#define ICM_TR_INFO_BOOT_ACL_SHIFT 7#define ICM_TR_INFO_BOOT_ACL_MASK GENMASK(12, 7)
drivers/thunderbolt/xdomain.c +18 -0
@@ -13,6 +13,7 @@#include <linux/device.h> #include <linux/kmod.h> #include <linux/module.h> +#include <linux/pm_runtime.h> #include <linux/utsname.h> #include <linux/uuid.h> #include <linux/workqueue.h>
@@ -1129,6 +1130,14 @@ struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent, xd->dev.groups = xdomain_attr_groups;dev_set_name (&xd->dev,"%u-%llx" , tb->index, route); +/* +* This keeps the DMA powered on as long as we have active +* connection to another host. +*/ +pm_runtime_set_active (&xd->dev); +pm_runtime_get_noresume (&xd->dev); +pm_runtime_enable (&xd->dev); +return xd; err_free_local_uuid:
@@ -1174,6 +1183,15 @@ void tb_xdomain_remove(struct tb_xdomain *xd)device_for_each_child_reverse (&xd->dev, xd, unregister_service); +/* +* Undo runtime PM here explicitly because it is possible that +* the XDomain was never added to the bus and thus device_del() +* is not called for it (device_del() would handle this otherwise). +*/ +pm_runtime_disable (&xd->dev); +pm_runtime_put_noidle (&xd->dev); +pm_runtime_set_suspended (&xd->dev); +if (!device_is_registered (&xd->dev))put_device (&xd->dev);else
Signature · 1 line
2.18.0
PATCH 7/7 thunderbolt: Remove redundant variable 'approved'
In reply to Mika WesterbergFrom: Colin Ian King <[email protected]>
Variable 'approved' is being assigned but is never used hence it is redundant and can be removed.
Cleans up clang warning:
warning: variable 'approved' set but not used [-Wunused-but-set-variable]
--- drivers/thunderbolt/icm.c | 2 -- 1 file changed, 2 deletions(-)
drivers/thunderbolt/icm.c +0 -2
@@ -736,7 +736,6 @@ icm_fr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr)struct tb_xdomain *xd;struct tb_switch *sw;u8 link, depth; -bool approved;u64 route;/*
@@ -750,7 +749,6 @@ icm_fr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr) link = pkg->link_info &ICM_LINK_INFO_LINK_MASK ; depth = (pkg->link_info &ICM_LINK_INFO_DEPTH_MASK ) >>ICM_LINK_INFO_DEPTH_SHIFT ; - approved = pkg->link_info &ICM_LINK_INFO_APPROVED ;if (link >ICM_MAX_LINK || depth >ICM_MAX_DEPTH ) {tb_warn (tb,"invalid topology %u.%u, ignoring\n" , link, depth);
Signature · 1 line
2.18.0