turbo_max_3.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Intel Turbo Boost Max Technology 3.0 legacy (non HWP) enumeration driver
  4. * Copyright (c) 2017, Intel Corporation.
  5. * All rights reserved.
  6. *
  7. * Author: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/cpufeature.h>
  11. #include <linux/cpuhotplug.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/topology.h>
  15. #include <linux/workqueue.h>
  16. #include <asm/cpu_device_id.h>
  17. #include <asm/intel-family.h>
  18. #include <asm/msr.h>
  19. #define MSR_OC_MAILBOX 0x150
  20. #define MSR_OC_MAILBOX_CMD_OFFSET 32
  21. #define MSR_OC_MAILBOX_RSP_OFFSET 32
  22. #define MSR_OC_MAILBOX_BUSY_BIT 63
  23. #define OC_MAILBOX_FC_CONTROL_CMD 0x1C
  24. /*
  25. * Typical latency to get mail box response is ~3us, It takes +3 us to
  26. * process reading mailbox after issuing mailbox write on a Broadwell 3.4 GHz
  27. * system. So for most of the time, the first mailbox read should have the
  28. * response, but to avoid some boundary cases retry twice.
  29. */
  30. #define OC_MAILBOX_RETRY_COUNT 2
  31. static int get_oc_core_priority(unsigned int cpu)
  32. {
  33. u64 value, cmd = OC_MAILBOX_FC_CONTROL_CMD;
  34. int ret, i;
  35. /* Issue favored core read command */
  36. value = cmd << MSR_OC_MAILBOX_CMD_OFFSET;
  37. /* Set the busy bit to indicate OS is trying to issue command */
  38. value |= BIT_ULL(MSR_OC_MAILBOX_BUSY_BIT);
  39. ret = wrmsrq_safe(MSR_OC_MAILBOX, value);
  40. if (ret) {
  41. pr_debug("cpu %d OC mailbox write failed\n", cpu);
  42. return ret;
  43. }
  44. for (i = 0; i < OC_MAILBOX_RETRY_COUNT; ++i) {
  45. ret = rdmsrq_safe(MSR_OC_MAILBOX, &value);
  46. if (ret) {
  47. pr_debug("cpu %d OC mailbox read failed\n", cpu);
  48. break;
  49. }
  50. if (value & BIT_ULL(MSR_OC_MAILBOX_BUSY_BIT)) {
  51. pr_debug("cpu %d OC mailbox still processing\n", cpu);
  52. ret = -EBUSY;
  53. continue;
  54. }
  55. if ((value >> MSR_OC_MAILBOX_RSP_OFFSET) & 0xff) {
  56. pr_debug("cpu %d OC mailbox cmd failed\n", cpu);
  57. ret = -ENXIO;
  58. break;
  59. }
  60. ret = value & 0xff;
  61. pr_debug("cpu %d max_ratio %d\n", cpu, ret);
  62. break;
  63. }
  64. return ret;
  65. }
  66. /*
  67. * The work item is needed to avoid CPU hotplug locking issues. The function
  68. * itmt_legacy_set_priority() is called from CPU online callback, so can't
  69. * call sched_set_itmt_support() from there as this function will aquire
  70. * hotplug locks in its path.
  71. */
  72. static void itmt_legacy_work_fn(struct work_struct *work)
  73. {
  74. sched_set_itmt_support();
  75. }
  76. static DECLARE_WORK(sched_itmt_work, itmt_legacy_work_fn);
  77. static int itmt_legacy_cpu_online(unsigned int cpu)
  78. {
  79. static u32 max_highest_perf = 0, min_highest_perf = U32_MAX;
  80. int priority;
  81. priority = get_oc_core_priority(cpu);
  82. if (priority < 0)
  83. return 0;
  84. sched_set_itmt_core_prio(priority, cpu);
  85. /* Enable ITMT feature when a core with different priority is found */
  86. if (max_highest_perf <= min_highest_perf) {
  87. if (priority > max_highest_perf)
  88. max_highest_perf = priority;
  89. if (priority < min_highest_perf)
  90. min_highest_perf = priority;
  91. if (max_highest_perf > min_highest_perf)
  92. schedule_work(&sched_itmt_work);
  93. }
  94. return 0;
  95. }
  96. static const struct x86_cpu_id itmt_legacy_cpu_ids[] = {
  97. X86_MATCH_VFM(INTEL_BROADWELL_X, NULL),
  98. X86_MATCH_VFM(INTEL_SKYLAKE_X, NULL),
  99. {}
  100. };
  101. static int __init itmt_legacy_init(void)
  102. {
  103. const struct x86_cpu_id *id;
  104. int ret;
  105. id = x86_match_cpu(itmt_legacy_cpu_ids);
  106. if (!id)
  107. return -ENODEV;
  108. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
  109. "platform/x86/turbo_max_3:online",
  110. itmt_legacy_cpu_online, NULL);
  111. if (ret < 0)
  112. return ret;
  113. return 0;
  114. }
  115. late_initcall(itmt_legacy_init)