ppc_cbe_cpufreq_pmi.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * pmi backend for the cbe_cpufreq driver
  4. *
  5. * (C) Copyright IBM Deutschland Entwicklung GmbH 2005-2007
  6. *
  7. * Author: Christian Krafft <krafft@de.ibm.com>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/types.h>
  11. #include <linux/timer.h>
  12. #include <linux/init.h>
  13. #include <linux/of_platform.h>
  14. #include <linux/pm_qos.h>
  15. #include <asm/processor.h>
  16. #include <asm/prom.h>
  17. #include <asm/pmi.h>
  18. #include <asm/cell-regs.h>
  19. #ifdef DEBUG
  20. #include <asm/time.h>
  21. #endif
  22. #include "ppc_cbe_cpufreq.h"
  23. bool cbe_cpufreq_has_pmi = false;
  24. EXPORT_SYMBOL_GPL(cbe_cpufreq_has_pmi);
  25. /*
  26. * hardware specific functions
  27. */
  28. int cbe_cpufreq_set_pmode_pmi(int cpu, unsigned int pmode)
  29. {
  30. int ret;
  31. pmi_message_t pmi_msg;
  32. #ifdef DEBUG
  33. long time;
  34. #endif
  35. pmi_msg.type = PMI_TYPE_FREQ_CHANGE;
  36. pmi_msg.data1 = cbe_cpu_to_node(cpu);
  37. pmi_msg.data2 = pmode;
  38. #ifdef DEBUG
  39. time = jiffies;
  40. #endif
  41. pmi_send_message(pmi_msg);
  42. #ifdef DEBUG
  43. time = jiffies - time;
  44. time = jiffies_to_msecs(time);
  45. pr_debug("had to wait %lu ms for a transition using " \
  46. "PMI\n", time);
  47. #endif
  48. ret = pmi_msg.data2;
  49. pr_debug("PMI returned slow mode %d\n", ret);
  50. return ret;
  51. }
  52. EXPORT_SYMBOL_GPL(cbe_cpufreq_set_pmode_pmi);
  53. static void cbe_cpufreq_handle_pmi(pmi_message_t pmi_msg)
  54. {
  55. struct cpufreq_policy *policy;
  56. struct freq_qos_request *req;
  57. u8 node, slow_mode;
  58. int cpu, ret;
  59. BUG_ON(pmi_msg.type != PMI_TYPE_FREQ_CHANGE);
  60. node = pmi_msg.data1;
  61. slow_mode = pmi_msg.data2;
  62. cpu = cbe_node_to_cpu(node);
  63. pr_debug("cbe_handle_pmi: node: %d max_freq: %d\n", node, slow_mode);
  64. policy = cpufreq_cpu_get(cpu);
  65. if (!policy) {
  66. pr_warn("cpufreq policy not found cpu%d\n", cpu);
  67. return;
  68. }
  69. req = policy->driver_data;
  70. ret = freq_qos_update_request(req,
  71. policy->freq_table[slow_mode].frequency);
  72. if (ret < 0)
  73. pr_warn("Failed to update freq constraint: %d\n", ret);
  74. else
  75. pr_debug("limiting node %d to slow mode %d\n", node, slow_mode);
  76. cpufreq_cpu_put(policy);
  77. }
  78. static struct pmi_handler cbe_pmi_handler = {
  79. .type = PMI_TYPE_FREQ_CHANGE,
  80. .handle_pmi_message = cbe_cpufreq_handle_pmi,
  81. };
  82. void cbe_cpufreq_pmi_policy_init(struct cpufreq_policy *policy)
  83. {
  84. struct freq_qos_request *req;
  85. int ret;
  86. if (!cbe_cpufreq_has_pmi)
  87. return;
  88. req = kzalloc(sizeof(*req), GFP_KERNEL);
  89. if (!req)
  90. return;
  91. ret = freq_qos_add_request(&policy->constraints, req, FREQ_QOS_MAX,
  92. policy->freq_table[0].frequency);
  93. if (ret < 0) {
  94. pr_err("Failed to add freq constraint (%d)\n", ret);
  95. kfree(req);
  96. return;
  97. }
  98. policy->driver_data = req;
  99. }
  100. EXPORT_SYMBOL_GPL(cbe_cpufreq_pmi_policy_init);
  101. void cbe_cpufreq_pmi_policy_exit(struct cpufreq_policy *policy)
  102. {
  103. struct freq_qos_request *req = policy->driver_data;
  104. if (cbe_cpufreq_has_pmi) {
  105. freq_qos_remove_request(req);
  106. kfree(req);
  107. }
  108. }
  109. EXPORT_SYMBOL_GPL(cbe_cpufreq_pmi_policy_exit);
  110. void cbe_cpufreq_pmi_init(void)
  111. {
  112. if (!pmi_register_handler(&cbe_pmi_handler))
  113. cbe_cpufreq_has_pmi = true;
  114. }
  115. EXPORT_SYMBOL_GPL(cbe_cpufreq_pmi_init);
  116. void cbe_cpufreq_pmi_exit(void)
  117. {
  118. pmi_unregister_handler(&cbe_pmi_handler);
  119. cbe_cpufreq_has_pmi = false;
  120. }
  121. EXPORT_SYMBOL_GPL(cbe_cpufreq_pmi_exit);