ppc_cbe_cpufreq_pmi.c 3.1 KB

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