Skip to content

Commit d278b09

Browse files
ij-intelbjorn-helgaas
authored andcommitted
thermal: Add PCIe cooling driver
Add a thermal cooling driver to provide path to access PCIe bandwidth controller using the usual thermal interfaces. A cooling device is instantiated for controllable PCIe Ports from the bwctrl service driver. If registering the cooling device fails, allow bwctrl's probe to succeed regardless. As cdev in that case contains IS_ERR() pseudo "pointer", clean that up inside the probe function so the remove side doesn't need to suddenly make an odd looking IS_ERR() check. The thermal side state 0 means no throttling, i.e., maximum supported PCIe Link Speed. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Ilpo Järvinen <[email protected]> [bhelgaas: dropped data->cdev test per https://lore.kernel.org/r/[email protected]] Signed-off-by: Bjorn Helgaas <[email protected]> Reviewed-by: Jonathan Cameron <[email protected]> Acked-by: Rafael J. Wysocki <[email protected]> # From the cooling device interface perspective
1 parent de9a6c8 commit d278b09

File tree

6 files changed

+133
-0
lines changed

6 files changed

+133
-0
lines changed

MAINTAINERS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17938,6 +17938,8 @@ M: Ilpo Järvinen <[email protected]>
1793817938
1793917939
S: Supported
1794017940
F: drivers/pci/pcie/bwctrl.c
17941+
F: drivers/thermal/pcie_cooling.c
17942+
F: include/linux/pci-bwctrl.h
1794117943

1794217944
PCIE DRIVER FOR AMAZON ANNAPURNA LABS
1794317945
M: Jonathan Chocron <[email protected]>

drivers/pci/pcie/bwctrl.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <linux/interrupt.h>
2828
#include <linux/mutex.h>
2929
#include <linux/pci.h>
30+
#include <linux/pci-bwctrl.h>
3031
#include <linux/rwsem.h>
3132
#include <linux/slab.h>
3233
#include <linux/types.h>
@@ -38,10 +39,12 @@
3839
* struct pcie_bwctrl_data - PCIe bandwidth controller
3940
* @set_speed_mutex: Serializes link speed changes
4041
* @lbms_count: Count for LBMS (since last reset)
42+
* @cdev: Thermal cooling device associated with the port
4143
*/
4244
struct pcie_bwctrl_data {
4345
struct mutex set_speed_mutex;
4446
atomic_t lbms_count;
47+
struct thermal_cooling_device *cdev;
4548
};
4649

4750
/*
@@ -314,11 +317,20 @@ static int pcie_bwnotif_probe(struct pcie_device *srv)
314317

315318
pci_dbg(port, "enabled with IRQ %d\n", srv->irq);
316319

320+
/* Don't fail on errors. Don't leave IS_ERR() "pointer" into ->cdev */
321+
port->link_bwctrl->cdev = pcie_cooling_device_register(port);
322+
if (IS_ERR(port->link_bwctrl->cdev))
323+
port->link_bwctrl->cdev = NULL;
324+
317325
return 0;
318326
}
319327

320328
static void pcie_bwnotif_remove(struct pcie_device *srv)
321329
{
330+
struct pcie_bwctrl_data *data = srv->port->link_bwctrl;
331+
332+
pcie_cooling_device_unregister(data->cdev);
333+
322334
pcie_bwnotif_disable(srv->port);
323335

324336
scoped_guard(rwsem_write, &pcie_bwctrl_setspeed_rwsem)

drivers/thermal/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,15 @@ config DEVFREQ_THERMAL
220220

221221
If you want this support, you should say Y here.
222222

223+
config PCIE_THERMAL
224+
bool "PCIe cooling support"
225+
depends on PCIEPORTBUS
226+
help
227+
This implements PCIe cooling mechanism through bandwidth reduction
228+
for PCIe devices.
229+
230+
If you want this support, you should say Y here.
231+
223232
config THERMAL_EMULATION
224233
bool "Thermal emulation mode support"
225234
help

drivers/thermal/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ thermal_sys-$(CONFIG_CPU_IDLE_THERMAL) += cpuidle_cooling.o
3131
# devfreq cooling
3232
thermal_sys-$(CONFIG_DEVFREQ_THERMAL) += devfreq_cooling.o
3333

34+
thermal_sys-$(CONFIG_PCIE_THERMAL) += pcie_cooling.o
35+
3436
obj-$(CONFIG_K3_THERMAL) += k3_bandgap.o k3_j72xx_bandgap.o
3537
# platform thermal drivers
3638
obj-y += broadcom/

drivers/thermal/pcie_cooling.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* PCIe cooling device
4+
*
5+
* Copyright (C) 2023-2024 Intel Corporation
6+
*/
7+
8+
#include <linux/build_bug.h>
9+
#include <linux/cleanup.h>
10+
#include <linux/err.h>
11+
#include <linux/module.h>
12+
#include <linux/pci.h>
13+
#include <linux/pci-bwctrl.h>
14+
#include <linux/slab.h>
15+
#include <linux/sprintf.h>
16+
#include <linux/thermal.h>
17+
18+
#define COOLING_DEV_TYPE_PREFIX "PCIe_Port_Link_Speed_"
19+
20+
static int pcie_cooling_get_max_level(struct thermal_cooling_device *cdev, unsigned long *state)
21+
{
22+
struct pci_dev *port = cdev->devdata;
23+
24+
/* cooling state 0 is same as the maximum PCIe speed */
25+
*state = port->subordinate->max_bus_speed - PCIE_SPEED_2_5GT;
26+
27+
return 0;
28+
}
29+
30+
static int pcie_cooling_get_cur_level(struct thermal_cooling_device *cdev, unsigned long *state)
31+
{
32+
struct pci_dev *port = cdev->devdata;
33+
34+
/* cooling state 0 is same as the maximum PCIe speed */
35+
*state = cdev->max_state - (port->subordinate->cur_bus_speed - PCIE_SPEED_2_5GT);
36+
37+
return 0;
38+
}
39+
40+
static int pcie_cooling_set_cur_level(struct thermal_cooling_device *cdev, unsigned long state)
41+
{
42+
struct pci_dev *port = cdev->devdata;
43+
enum pci_bus_speed speed;
44+
45+
/* cooling state 0 is same as the maximum PCIe speed */
46+
speed = (cdev->max_state - state) + PCIE_SPEED_2_5GT;
47+
48+
return pcie_set_target_speed(port, speed, true);
49+
}
50+
51+
static struct thermal_cooling_device_ops pcie_cooling_ops = {
52+
.get_max_state = pcie_cooling_get_max_level,
53+
.get_cur_state = pcie_cooling_get_cur_level,
54+
.set_cur_state = pcie_cooling_set_cur_level,
55+
};
56+
57+
struct thermal_cooling_device *pcie_cooling_device_register(struct pci_dev *port)
58+
{
59+
char *name __free(kfree) =
60+
kasprintf(GFP_KERNEL, COOLING_DEV_TYPE_PREFIX "%s", pci_name(port));
61+
if (!name)
62+
return ERR_PTR(-ENOMEM);
63+
64+
return thermal_cooling_device_register(name, port, &pcie_cooling_ops);
65+
}
66+
67+
void pcie_cooling_device_unregister(struct thermal_cooling_device *cdev)
68+
{
69+
thermal_cooling_device_unregister(cdev);
70+
}
71+
72+
/* For bus_speed <-> state arithmetic */
73+
static_assert(PCIE_SPEED_2_5GT + 1 == PCIE_SPEED_5_0GT);
74+
static_assert(PCIE_SPEED_5_0GT + 1 == PCIE_SPEED_8_0GT);
75+
static_assert(PCIE_SPEED_8_0GT + 1 == PCIE_SPEED_16_0GT);
76+
static_assert(PCIE_SPEED_16_0GT + 1 == PCIE_SPEED_32_0GT);
77+
static_assert(PCIE_SPEED_32_0GT + 1 == PCIE_SPEED_64_0GT);
78+
79+
MODULE_AUTHOR("Ilpo Järvinen <[email protected]>");
80+
MODULE_DESCRIPTION("PCIe cooling driver");

include/linux/pci-bwctrl.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
/*
3+
* PCIe bandwidth controller
4+
*
5+
* Copyright (C) 2023-2024 Intel Corporation
6+
*/
7+
8+
#ifndef LINUX_PCI_BWCTRL_H
9+
#define LINUX_PCI_BWCTRL_H
10+
11+
#include <linux/pci.h>
12+
13+
struct thermal_cooling_device;
14+
15+
#ifdef CONFIG_PCIE_THERMAL
16+
struct thermal_cooling_device *pcie_cooling_device_register(struct pci_dev *port);
17+
void pcie_cooling_device_unregister(struct thermal_cooling_device *cdev);
18+
#else
19+
static inline struct thermal_cooling_device *pcie_cooling_device_register(struct pci_dev *port)
20+
{
21+
return NULL;
22+
}
23+
static inline void pcie_cooling_device_unregister(struct thermal_cooling_device *cdev)
24+
{
25+
}
26+
#endif
27+
28+
#endif

0 commit comments

Comments
 (0)