Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / thermal / pcie_cooling.c
bloba876d64f15827a555f08fba70b405023985285bf
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * PCIe cooling device
5 * Copyright (C) 2023-2024 Intel Corporation
6 */
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>
18 #define COOLING_DEV_TYPE_PREFIX "PCIe_Port_Link_Speed_"
20 static int pcie_cooling_get_max_level(struct thermal_cooling_device *cdev, unsigned long *state)
22 struct pci_dev *port = cdev->devdata;
24 /* cooling state 0 is same as the maximum PCIe speed */
25 *state = port->subordinate->max_bus_speed - PCIE_SPEED_2_5GT;
27 return 0;
30 static int pcie_cooling_get_cur_level(struct thermal_cooling_device *cdev, unsigned long *state)
32 struct pci_dev *port = cdev->devdata;
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);
37 return 0;
40 static int pcie_cooling_set_cur_level(struct thermal_cooling_device *cdev, unsigned long state)
42 struct pci_dev *port = cdev->devdata;
43 enum pci_bus_speed speed;
45 /* cooling state 0 is same as the maximum PCIe speed */
46 speed = (cdev->max_state - state) + PCIE_SPEED_2_5GT;
48 return pcie_set_target_speed(port, speed, true);
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,
57 struct thermal_cooling_device *pcie_cooling_device_register(struct pci_dev *port)
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);
64 return thermal_cooling_device_register(name, port, &pcie_cooling_ops);
67 void pcie_cooling_device_unregister(struct thermal_cooling_device *cdev)
69 thermal_cooling_device_unregister(cdev);
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);
79 MODULE_AUTHOR("Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>");
80 MODULE_DESCRIPTION("PCIe cooling driver");