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