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