pcie_cooling.c 2.3 KB

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