#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/param.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/workqueue.h>
#include <asm/interrupt.h>
#include <asm/io.h>
#include <asm/reg.h>
#include <asm/nvram.h>
#include <asm/cache.h>
#include <asm/8xx_immap.h>
#include <asm/machdep.h>
#include "setup.h"
static struct tau_temp
{
int interrupts;
unsigned char low;
unsigned char high;
unsigned char grew;
} tau[NR_CPUS];
static bool tau_int_enable;
#define step_size 2
#define window_expand 1
#define shrink_timer 2000
#define min_window 2
static void set_thresholds(unsigned long cpu)
{
u32 maybe_tie = tau_int_enable ? THRM1_TIE : 0;
mtspr(SPRN_THRM1, THRM1_THRES(tau[cpu].low) | THRM1_V | maybe_tie | THRM1_TID);
mtspr(SPRN_THRM2, THRM1_THRES(tau[cpu].high) | THRM1_V | maybe_tie);
}
static void TAUupdate(int cpu)
{
u32 thrm;
u32 bits = THRM1_TIV | THRM1_TIN | THRM1_V;
thrm = mfspr(SPRN_THRM1);
if ((thrm & bits) == bits) {
mtspr(SPRN_THRM1, 0);
if (tau[cpu].low >= step_size) {
tau[cpu].low -= step_size;
tau[cpu].high -= (step_size - window_expand);
}
tau[cpu].grew = 1;
pr_debug("%s: low threshold crossed\n", __func__);
}
thrm = mfspr(SPRN_THRM2);
if ((thrm & bits) == bits) {
mtspr(SPRN_THRM2, 0);
if (tau[cpu].high <= 127 - step_size) {
tau[cpu].low += (step_size - window_expand);
tau[cpu].high += step_size;
}
tau[cpu].grew = 1;
pr_debug("%s: high threshold crossed\n", __func__);
}
}
#ifdef CONFIG_TAU_INT
DEFINE_INTERRUPT_HANDLER_ASYNC(TAUException)
{
int cpu = smp_processor_id();
tau[cpu].interrupts++;
TAUupdate(cpu);
}
#endif
static void tau_timeout(void * info)
{
int cpu;
int size;
int shrink;
cpu = smp_processor_id();
if (!tau_int_enable)
TAUupdate(cpu);
mtspr(SPRN_THRM3, 0);
size = tau[cpu].high - tau[cpu].low;
if (size > min_window && ! tau[cpu].grew) {
shrink = (2 + size - min_window) / 4;
if (shrink) {
tau[cpu].low += shrink;
tau[cpu].high -= shrink;
} else {
tau[cpu].low += 1;
#if 1
if ((tau[cpu].high - tau[cpu].low) != min_window){
printk(KERN_ERR "temp.c: line %d, logic error\n", __LINE__);
}
#endif
}
}
tau[cpu].grew = 0;
set_thresholds(cpu);
mtspr(SPRN_THRM3, THRM3_SITV(0x1fff) | THRM3_E);
}
static struct workqueue_struct *tau_workq;
static void tau_work_func(struct work_struct *work)
{
msleep(shrink_timer);
on_each_cpu(tau_timeout, NULL, 0);
queue_work(tau_workq, work);
}
static DECLARE_WORK(tau_work, tau_work_func);
int tau_initialized = 0;
static void __init TAU_init_smp(void *info)
{
unsigned long cpu = smp_processor_id();
tau[cpu].low = 5;
tau[cpu].high = 120;
set_thresholds(cpu);
}
static int __init TAU_init(void)
{
if (!cpu_has_feature(CPU_FTR_TAU)) {
printk("Thermal assist unit not available\n");
tau_initialized = 0;
return 1;
}
tau_int_enable = IS_ENABLED(CONFIG_TAU_INT) &&
!strcmp(cur_cpu_spec->platform, "ppc750");
tau_workq = alloc_ordered_workqueue("tau", 0);
if (!tau_workq)
return -ENOMEM;
on_each_cpu(TAU_init_smp, NULL, 0);
queue_work(tau_workq, &tau_work);
pr_info("Thermal assist unit using %s, shrink_timer: %d ms\n",
tau_int_enable ? "interrupts" : "workqueue", shrink_timer);
tau_initialized = 1;
return 0;
}
__initcall(TAU_init);
u32 cpu_temp_both(unsigned long cpu)
{
return ((tau[cpu].high << 16) | tau[cpu].low);
}
u32 cpu_temp(unsigned long cpu)
{
return ((tau[cpu].high + tau[cpu].low) / 2);
}
u32 tau_interrupts(unsigned long cpu)
{
return (tau[cpu].interrupts);
}