pcc-cpufreq.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /*
  2. * pcc-cpufreq.c - Processor Clocking Control firmware cpufreq interface
  3. *
  4. * Copyright (C) 2009 Red Hat, Matthew Garrett <mjg@redhat.com>
  5. * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
  6. * Nagananda Chumbalkar <nagananda.chumbalkar@hp.com>
  7. *
  8. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; version 2 of the License.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or NON
  17. * INFRINGEMENT. See the GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/smp.h>
  29. #include <linux/sched.h>
  30. #include <linux/cpufreq.h>
  31. #include <linux/compiler.h>
  32. #include <linux/slab.h>
  33. #include <linux/acpi.h>
  34. #include <linux/io.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/uaccess.h>
  37. #include <acpi/processor.h>
  38. #define PCC_VERSION "1.10.00"
  39. #define POLL_LOOPS 300
  40. #define CMD_COMPLETE 0x1
  41. #define CMD_GET_FREQ 0x0
  42. #define CMD_SET_FREQ 0x1
  43. #define BUF_SZ 4
  44. struct pcc_register_resource {
  45. u8 descriptor;
  46. u16 length;
  47. u8 space_id;
  48. u8 bit_width;
  49. u8 bit_offset;
  50. u8 access_size;
  51. u64 address;
  52. } __attribute__ ((packed));
  53. struct pcc_memory_resource {
  54. u8 descriptor;
  55. u16 length;
  56. u8 space_id;
  57. u8 resource_usage;
  58. u8 type_specific;
  59. u64 granularity;
  60. u64 minimum;
  61. u64 maximum;
  62. u64 translation_offset;
  63. u64 address_length;
  64. } __attribute__ ((packed));
  65. static struct cpufreq_driver pcc_cpufreq_driver;
  66. struct pcc_header {
  67. u32 signature;
  68. u16 length;
  69. u8 major;
  70. u8 minor;
  71. u32 features;
  72. u16 command;
  73. u16 status;
  74. u32 latency;
  75. u32 minimum_time;
  76. u32 maximum_time;
  77. u32 nominal;
  78. u32 throttled_frequency;
  79. u32 minimum_frequency;
  80. };
  81. static void __iomem *pcch_virt_addr;
  82. static struct pcc_header __iomem *pcch_hdr;
  83. static DEFINE_SPINLOCK(pcc_lock);
  84. static struct acpi_generic_address doorbell;
  85. static u64 doorbell_preserve;
  86. static u64 doorbell_write;
  87. static u8 OSC_UUID[16] = {0x9F, 0x2C, 0x9B, 0x63, 0x91, 0x70, 0x1f, 0x49,
  88. 0xBB, 0x4F, 0xA5, 0x98, 0x2F, 0xA1, 0xB5, 0x46};
  89. struct pcc_cpu {
  90. u32 input_offset;
  91. u32 output_offset;
  92. };
  93. static struct pcc_cpu __percpu *pcc_cpu_info;
  94. static int pcc_cpufreq_verify(struct cpufreq_policy *policy)
  95. {
  96. cpufreq_verify_within_cpu_limits(policy);
  97. return 0;
  98. }
  99. static inline void pcc_cmd(void)
  100. {
  101. u64 doorbell_value;
  102. int i;
  103. acpi_read(&doorbell_value, &doorbell);
  104. acpi_write((doorbell_value & doorbell_preserve) | doorbell_write,
  105. &doorbell);
  106. for (i = 0; i < POLL_LOOPS; i++) {
  107. if (ioread16(&pcch_hdr->status) & CMD_COMPLETE)
  108. break;
  109. }
  110. }
  111. static inline void pcc_clear_mapping(void)
  112. {
  113. if (pcch_virt_addr)
  114. iounmap(pcch_virt_addr);
  115. pcch_virt_addr = NULL;
  116. }
  117. static unsigned int pcc_get_freq(unsigned int cpu)
  118. {
  119. struct pcc_cpu *pcc_cpu_data;
  120. unsigned int curr_freq;
  121. unsigned int freq_limit;
  122. u16 status;
  123. u32 input_buffer;
  124. u32 output_buffer;
  125. spin_lock(&pcc_lock);
  126. pr_debug("get: get_freq for CPU %d\n", cpu);
  127. pcc_cpu_data = per_cpu_ptr(pcc_cpu_info, cpu);
  128. input_buffer = 0x1;
  129. iowrite32(input_buffer,
  130. (pcch_virt_addr + pcc_cpu_data->input_offset));
  131. iowrite16(CMD_GET_FREQ, &pcch_hdr->command);
  132. pcc_cmd();
  133. output_buffer =
  134. ioread32(pcch_virt_addr + pcc_cpu_data->output_offset);
  135. /* Clear the input buffer - we are done with the current command */
  136. memset_io((pcch_virt_addr + pcc_cpu_data->input_offset), 0, BUF_SZ);
  137. status = ioread16(&pcch_hdr->status);
  138. if (status != CMD_COMPLETE) {
  139. pr_debug("get: FAILED: for CPU %d, status is %d\n",
  140. cpu, status);
  141. goto cmd_incomplete;
  142. }
  143. iowrite16(0, &pcch_hdr->status);
  144. curr_freq = (((ioread32(&pcch_hdr->nominal) * (output_buffer & 0xff))
  145. / 100) * 1000);
  146. pr_debug("get: SUCCESS: (virtual) output_offset for cpu %d is "
  147. "0x%p, contains a value of: 0x%x. Speed is: %d MHz\n",
  148. cpu, (pcch_virt_addr + pcc_cpu_data->output_offset),
  149. output_buffer, curr_freq);
  150. freq_limit = (output_buffer >> 8) & 0xff;
  151. if (freq_limit != 0xff) {
  152. pr_debug("get: frequency for cpu %d is being temporarily"
  153. " capped at %d\n", cpu, curr_freq);
  154. }
  155. spin_unlock(&pcc_lock);
  156. return curr_freq;
  157. cmd_incomplete:
  158. iowrite16(0, &pcch_hdr->status);
  159. spin_unlock(&pcc_lock);
  160. return 0;
  161. }
  162. static int pcc_cpufreq_target(struct cpufreq_policy *policy,
  163. unsigned int target_freq,
  164. unsigned int relation)
  165. {
  166. struct pcc_cpu *pcc_cpu_data;
  167. struct cpufreq_freqs freqs;
  168. u16 status;
  169. u32 input_buffer;
  170. int cpu;
  171. cpu = policy->cpu;
  172. pcc_cpu_data = per_cpu_ptr(pcc_cpu_info, cpu);
  173. pr_debug("target: CPU %d should go to target freq: %d "
  174. "(virtual) input_offset is 0x%p\n",
  175. cpu, target_freq,
  176. (pcch_virt_addr + pcc_cpu_data->input_offset));
  177. freqs.old = policy->cur;
  178. freqs.new = target_freq;
  179. cpufreq_freq_transition_begin(policy, &freqs);
  180. spin_lock(&pcc_lock);
  181. input_buffer = 0x1 | (((target_freq * 100)
  182. / (ioread32(&pcch_hdr->nominal) * 1000)) << 8);
  183. iowrite32(input_buffer,
  184. (pcch_virt_addr + pcc_cpu_data->input_offset));
  185. iowrite16(CMD_SET_FREQ, &pcch_hdr->command);
  186. pcc_cmd();
  187. /* Clear the input buffer - we are done with the current command */
  188. memset_io((pcch_virt_addr + pcc_cpu_data->input_offset), 0, BUF_SZ);
  189. status = ioread16(&pcch_hdr->status);
  190. iowrite16(0, &pcch_hdr->status);
  191. cpufreq_freq_transition_end(policy, &freqs, status != CMD_COMPLETE);
  192. spin_unlock(&pcc_lock);
  193. if (status != CMD_COMPLETE) {
  194. pr_debug("target: FAILED for cpu %d, with status: 0x%x\n",
  195. cpu, status);
  196. return -EINVAL;
  197. }
  198. pr_debug("target: was SUCCESSFUL for cpu %d\n", cpu);
  199. return 0;
  200. }
  201. static int pcc_get_offset(int cpu)
  202. {
  203. acpi_status status;
  204. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  205. union acpi_object *pccp, *offset;
  206. struct pcc_cpu *pcc_cpu_data;
  207. struct acpi_processor *pr;
  208. int ret = 0;
  209. pr = per_cpu(processors, cpu);
  210. pcc_cpu_data = per_cpu_ptr(pcc_cpu_info, cpu);
  211. if (!pr)
  212. return -ENODEV;
  213. status = acpi_evaluate_object(pr->handle, "PCCP", NULL, &buffer);
  214. if (ACPI_FAILURE(status))
  215. return -ENODEV;
  216. pccp = buffer.pointer;
  217. if (!pccp || pccp->type != ACPI_TYPE_PACKAGE) {
  218. ret = -ENODEV;
  219. goto out_free;
  220. };
  221. offset = &(pccp->package.elements[0]);
  222. if (!offset || offset->type != ACPI_TYPE_INTEGER) {
  223. ret = -ENODEV;
  224. goto out_free;
  225. }
  226. pcc_cpu_data->input_offset = offset->integer.value;
  227. offset = &(pccp->package.elements[1]);
  228. if (!offset || offset->type != ACPI_TYPE_INTEGER) {
  229. ret = -ENODEV;
  230. goto out_free;
  231. }
  232. pcc_cpu_data->output_offset = offset->integer.value;
  233. memset_io((pcch_virt_addr + pcc_cpu_data->input_offset), 0, BUF_SZ);
  234. memset_io((pcch_virt_addr + pcc_cpu_data->output_offset), 0, BUF_SZ);
  235. pr_debug("pcc_get_offset: for CPU %d: pcc_cpu_data "
  236. "input_offset: 0x%x, pcc_cpu_data output_offset: 0x%x\n",
  237. cpu, pcc_cpu_data->input_offset, pcc_cpu_data->output_offset);
  238. out_free:
  239. kfree(buffer.pointer);
  240. return ret;
  241. }
  242. static int __init pcc_cpufreq_do_osc(acpi_handle *handle)
  243. {
  244. acpi_status status;
  245. struct acpi_object_list input;
  246. struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
  247. union acpi_object in_params[4];
  248. union acpi_object *out_obj;
  249. u32 capabilities[2];
  250. u32 errors;
  251. u32 supported;
  252. int ret = 0;
  253. input.count = 4;
  254. input.pointer = in_params;
  255. in_params[0].type = ACPI_TYPE_BUFFER;
  256. in_params[0].buffer.length = 16;
  257. in_params[0].buffer.pointer = OSC_UUID;
  258. in_params[1].type = ACPI_TYPE_INTEGER;
  259. in_params[1].integer.value = 1;
  260. in_params[2].type = ACPI_TYPE_INTEGER;
  261. in_params[2].integer.value = 2;
  262. in_params[3].type = ACPI_TYPE_BUFFER;
  263. in_params[3].buffer.length = 8;
  264. in_params[3].buffer.pointer = (u8 *)&capabilities;
  265. capabilities[0] = OSC_QUERY_ENABLE;
  266. capabilities[1] = 0x1;
  267. status = acpi_evaluate_object(*handle, "_OSC", &input, &output);
  268. if (ACPI_FAILURE(status))
  269. return -ENODEV;
  270. if (!output.length)
  271. return -ENODEV;
  272. out_obj = output.pointer;
  273. if (out_obj->type != ACPI_TYPE_BUFFER) {
  274. ret = -ENODEV;
  275. goto out_free;
  276. }
  277. errors = *((u32 *)out_obj->buffer.pointer) & ~(1 << 0);
  278. if (errors) {
  279. ret = -ENODEV;
  280. goto out_free;
  281. }
  282. supported = *((u32 *)(out_obj->buffer.pointer + 4));
  283. if (!(supported & 0x1)) {
  284. ret = -ENODEV;
  285. goto out_free;
  286. }
  287. kfree(output.pointer);
  288. capabilities[0] = 0x0;
  289. capabilities[1] = 0x1;
  290. status = acpi_evaluate_object(*handle, "_OSC", &input, &output);
  291. if (ACPI_FAILURE(status))
  292. return -ENODEV;
  293. if (!output.length)
  294. return -ENODEV;
  295. out_obj = output.pointer;
  296. if (out_obj->type != ACPI_TYPE_BUFFER) {
  297. ret = -ENODEV;
  298. goto out_free;
  299. }
  300. errors = *((u32 *)out_obj->buffer.pointer) & ~(1 << 0);
  301. if (errors) {
  302. ret = -ENODEV;
  303. goto out_free;
  304. }
  305. supported = *((u32 *)(out_obj->buffer.pointer + 4));
  306. if (!(supported & 0x1)) {
  307. ret = -ENODEV;
  308. goto out_free;
  309. }
  310. out_free:
  311. kfree(output.pointer);
  312. return ret;
  313. }
  314. static int __init pcc_cpufreq_probe(void)
  315. {
  316. acpi_status status;
  317. struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
  318. struct pcc_memory_resource *mem_resource;
  319. struct pcc_register_resource *reg_resource;
  320. union acpi_object *out_obj, *member;
  321. acpi_handle handle, osc_handle;
  322. int ret = 0;
  323. status = acpi_get_handle(NULL, "\\_SB", &handle);
  324. if (ACPI_FAILURE(status))
  325. return -ENODEV;
  326. if (!acpi_has_method(handle, "PCCH"))
  327. return -ENODEV;
  328. status = acpi_get_handle(handle, "_OSC", &osc_handle);
  329. if (ACPI_SUCCESS(status)) {
  330. ret = pcc_cpufreq_do_osc(&osc_handle);
  331. if (ret)
  332. pr_debug("probe: _OSC evaluation did not succeed\n");
  333. /* Firmware's use of _OSC is optional */
  334. ret = 0;
  335. }
  336. status = acpi_evaluate_object(handle, "PCCH", NULL, &output);
  337. if (ACPI_FAILURE(status))
  338. return -ENODEV;
  339. out_obj = output.pointer;
  340. if (out_obj->type != ACPI_TYPE_PACKAGE) {
  341. ret = -ENODEV;
  342. goto out_free;
  343. }
  344. member = &out_obj->package.elements[0];
  345. if (member->type != ACPI_TYPE_BUFFER) {
  346. ret = -ENODEV;
  347. goto out_free;
  348. }
  349. mem_resource = (struct pcc_memory_resource *)member->buffer.pointer;
  350. pr_debug("probe: mem_resource descriptor: 0x%x,"
  351. " length: %d, space_id: %d, resource_usage: %d,"
  352. " type_specific: %d, granularity: 0x%llx,"
  353. " minimum: 0x%llx, maximum: 0x%llx,"
  354. " translation_offset: 0x%llx, address_length: 0x%llx\n",
  355. mem_resource->descriptor, mem_resource->length,
  356. mem_resource->space_id, mem_resource->resource_usage,
  357. mem_resource->type_specific, mem_resource->granularity,
  358. mem_resource->minimum, mem_resource->maximum,
  359. mem_resource->translation_offset,
  360. mem_resource->address_length);
  361. if (mem_resource->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY) {
  362. ret = -ENODEV;
  363. goto out_free;
  364. }
  365. pcch_virt_addr = ioremap_nocache(mem_resource->minimum,
  366. mem_resource->address_length);
  367. if (pcch_virt_addr == NULL) {
  368. pr_debug("probe: could not map shared mem region\n");
  369. ret = -ENOMEM;
  370. goto out_free;
  371. }
  372. pcch_hdr = pcch_virt_addr;
  373. pr_debug("probe: PCCH header (virtual) addr: 0x%p\n", pcch_hdr);
  374. pr_debug("probe: PCCH header is at physical address: 0x%llx,"
  375. " signature: 0x%x, length: %d bytes, major: %d, minor: %d,"
  376. " supported features: 0x%x, command field: 0x%x,"
  377. " status field: 0x%x, nominal latency: %d us\n",
  378. mem_resource->minimum, ioread32(&pcch_hdr->signature),
  379. ioread16(&pcch_hdr->length), ioread8(&pcch_hdr->major),
  380. ioread8(&pcch_hdr->minor), ioread32(&pcch_hdr->features),
  381. ioread16(&pcch_hdr->command), ioread16(&pcch_hdr->status),
  382. ioread32(&pcch_hdr->latency));
  383. pr_debug("probe: min time between commands: %d us,"
  384. " max time between commands: %d us,"
  385. " nominal CPU frequency: %d MHz,"
  386. " minimum CPU frequency: %d MHz,"
  387. " minimum CPU frequency without throttling: %d MHz\n",
  388. ioread32(&pcch_hdr->minimum_time),
  389. ioread32(&pcch_hdr->maximum_time),
  390. ioread32(&pcch_hdr->nominal),
  391. ioread32(&pcch_hdr->throttled_frequency),
  392. ioread32(&pcch_hdr->minimum_frequency));
  393. member = &out_obj->package.elements[1];
  394. if (member->type != ACPI_TYPE_BUFFER) {
  395. ret = -ENODEV;
  396. goto pcch_free;
  397. }
  398. reg_resource = (struct pcc_register_resource *)member->buffer.pointer;
  399. doorbell.space_id = reg_resource->space_id;
  400. doorbell.bit_width = reg_resource->bit_width;
  401. doorbell.bit_offset = reg_resource->bit_offset;
  402. doorbell.access_width = 64;
  403. doorbell.address = reg_resource->address;
  404. pr_debug("probe: doorbell: space_id is %d, bit_width is %d, "
  405. "bit_offset is %d, access_width is %d, address is 0x%llx\n",
  406. doorbell.space_id, doorbell.bit_width, doorbell.bit_offset,
  407. doorbell.access_width, reg_resource->address);
  408. member = &out_obj->package.elements[2];
  409. if (member->type != ACPI_TYPE_INTEGER) {
  410. ret = -ENODEV;
  411. goto pcch_free;
  412. }
  413. doorbell_preserve = member->integer.value;
  414. member = &out_obj->package.elements[3];
  415. if (member->type != ACPI_TYPE_INTEGER) {
  416. ret = -ENODEV;
  417. goto pcch_free;
  418. }
  419. doorbell_write = member->integer.value;
  420. pr_debug("probe: doorbell_preserve: 0x%llx,"
  421. " doorbell_write: 0x%llx\n",
  422. doorbell_preserve, doorbell_write);
  423. pcc_cpu_info = alloc_percpu(struct pcc_cpu);
  424. if (!pcc_cpu_info) {
  425. ret = -ENOMEM;
  426. goto pcch_free;
  427. }
  428. printk(KERN_DEBUG "pcc-cpufreq: (v%s) driver loaded with frequency"
  429. " limits: %d MHz, %d MHz\n", PCC_VERSION,
  430. ioread32(&pcch_hdr->minimum_frequency),
  431. ioread32(&pcch_hdr->nominal));
  432. kfree(output.pointer);
  433. return ret;
  434. pcch_free:
  435. pcc_clear_mapping();
  436. out_free:
  437. kfree(output.pointer);
  438. return ret;
  439. }
  440. static int pcc_cpufreq_cpu_init(struct cpufreq_policy *policy)
  441. {
  442. unsigned int cpu = policy->cpu;
  443. unsigned int result = 0;
  444. if (!pcch_virt_addr) {
  445. result = -1;
  446. goto out;
  447. }
  448. result = pcc_get_offset(cpu);
  449. if (result) {
  450. pr_debug("init: PCCP evaluation failed\n");
  451. goto out;
  452. }
  453. policy->max = policy->cpuinfo.max_freq =
  454. ioread32(&pcch_hdr->nominal) * 1000;
  455. policy->min = policy->cpuinfo.min_freq =
  456. ioread32(&pcch_hdr->minimum_frequency) * 1000;
  457. pr_debug("init: policy->max is %d, policy->min is %d\n",
  458. policy->max, policy->min);
  459. out:
  460. return result;
  461. }
  462. static int pcc_cpufreq_cpu_exit(struct cpufreq_policy *policy)
  463. {
  464. return 0;
  465. }
  466. static struct cpufreq_driver pcc_cpufreq_driver = {
  467. .flags = CPUFREQ_CONST_LOOPS,
  468. .get = pcc_get_freq,
  469. .verify = pcc_cpufreq_verify,
  470. .target = pcc_cpufreq_target,
  471. .init = pcc_cpufreq_cpu_init,
  472. .exit = pcc_cpufreq_cpu_exit,
  473. .name = "pcc-cpufreq",
  474. };
  475. static int __init pcc_cpufreq_init(void)
  476. {
  477. int ret;
  478. if (acpi_disabled)
  479. return 0;
  480. ret = pcc_cpufreq_probe();
  481. if (ret) {
  482. pr_debug("pcc_cpufreq_init: PCCH evaluation failed\n");
  483. return ret;
  484. }
  485. ret = cpufreq_register_driver(&pcc_cpufreq_driver);
  486. return ret;
  487. }
  488. static void __exit pcc_cpufreq_exit(void)
  489. {
  490. cpufreq_unregister_driver(&pcc_cpufreq_driver);
  491. pcc_clear_mapping();
  492. free_percpu(pcc_cpu_info);
  493. }
  494. static const struct acpi_device_id processor_device_ids[] = {
  495. {ACPI_PROCESSOR_OBJECT_HID, },
  496. {ACPI_PROCESSOR_DEVICE_HID, },
  497. {},
  498. };
  499. MODULE_DEVICE_TABLE(acpi, processor_device_ids);
  500. MODULE_AUTHOR("Matthew Garrett, Naga Chumbalkar");
  501. MODULE_VERSION(PCC_VERSION);
  502. MODULE_DESCRIPTION("Processor Clocking Control interface driver");
  503. MODULE_LICENSE("GPL");
  504. late_initcall(pcc_cpufreq_init);
  505. module_exit(pcc_cpufreq_exit);