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