windfarm_pm81.c

来自「linux 内核源代码」· C语言 代码 · 共 814 行 · 第 1/2 页

C
814
字号
	}	piddata = (struct smu_sdbp_cpupiddata *)&hdr[1];	/* Get the FVT params for operating point 0 (the only supported one	 * for now) in order to get tmax	 */	hdr = smu_get_sdb_partition(SMU_SDB_FVT_ID, NULL);	if (hdr) {		fvt = (struct smu_sdbp_fvt *)&hdr[1];		tmax = ((s32)fvt->maxtemp) << 16;	} else		tmax = 0x5e0000; /* 94 degree default */	/* Alloc & initialize state */	wf_smu_cpu_fans = kmalloc(sizeof(struct wf_smu_cpu_fans_state),				  GFP_KERNEL);	if (wf_smu_cpu_fans == NULL)		goto fail;       	wf_smu_cpu_fans->ticks = 1;	wf_smu_cpu_fans->scale = WF_SMU_CPU_FANS_SIBLING_SCALE;	wf_smu_cpu_fans->offset = WF_SMU_CPU_FANS_SIBLING_OFFSET;	/* Fill PID params */	pid_param.interval = WF_SMU_CPU_FANS_INTERVAL;	pid_param.history_len = piddata->history_len;	if (pid_param.history_len > WF_CPU_PID_MAX_HISTORY) {		printk(KERN_WARNING "windfarm: History size overflow on "		       "CPU control loop (%d)\n", piddata->history_len);		pid_param.history_len = WF_CPU_PID_MAX_HISTORY;	}	pid_param.gd = piddata->gd;	pid_param.gp = piddata->gp;	pid_param.gr = piddata->gr / pid_param.history_len;	tdelta = ((s32)piddata->target_temp_delta) << 16;	maxpow = ((s32)piddata->max_power) << 16;	powadj = ((s32)piddata->power_adj) << 16;	pid_param.tmax = tmax;	pid_param.ttarget = tmax - tdelta;	pid_param.pmaxadj = maxpow - powadj;	pid_param.min = fan_cpu_main->ops->get_min(fan_cpu_main);	pid_param.max = fan_cpu_main->ops->get_max(fan_cpu_main);	wf_cpu_pid_init(&wf_smu_cpu_fans->pid, &pid_param);	DBG("wf: CPU Fan control initialized.\n");	DBG("    ttarged=%d.%03d, tmax=%d.%03d, min=%d RPM, max=%d RPM\n",	    FIX32TOPRINT(pid_param.ttarget), FIX32TOPRINT(pid_param.tmax),	    pid_param.min, pid_param.max);	return; fail:	printk(KERN_WARNING "windfarm: CPU fan config not found\n"	       "for this machine model, max fan speed\n");	if (cpufreq_clamp)		wf_control_set_max(cpufreq_clamp);	if (fan_cpu_main)		wf_control_set_max(fan_cpu_main);}static void wf_smu_cpu_fans_tick(struct wf_smu_cpu_fans_state *st){	s32 new_setpoint, temp, power, systarget;	int rc;	if (--st->ticks != 0) {		if (wf_smu_readjust)			goto readjust;		return;	}	st->ticks = WF_SMU_CPU_FANS_INTERVAL;	rc = sensor_cpu_temp->ops->get_value(sensor_cpu_temp, &temp);	if (rc) {		printk(KERN_WARNING "windfarm: CPU temp sensor error %d\n",		       rc);		wf_smu_failure_state |= FAILURE_SENSOR;		return;	}	rc = sensor_cpu_power->ops->get_value(sensor_cpu_power, &power);	if (rc) {		printk(KERN_WARNING "windfarm: CPU power sensor error %d\n",		       rc);		wf_smu_failure_state |= FAILURE_SENSOR;		return;	}	DBG("wf_smu: CPU Fans tick ! CPU temp: %d.%03d, power: %d.%03d\n",	    FIX32TOPRINT(temp), FIX32TOPRINT(power));#ifdef HACKED_OVERTEMP	if (temp > 0x4a0000)		wf_smu_failure_state |= FAILURE_OVERTEMP;#else	if (temp > st->pid.param.tmax)		wf_smu_failure_state |= FAILURE_OVERTEMP;#endif	new_setpoint = wf_cpu_pid_run(&st->pid, power, temp);	DBG("wf_smu: new_setpoint: %d RPM\n", (int)new_setpoint);	systarget = wf_smu_sys_fans ? wf_smu_sys_fans->pid.target : 0;	systarget = ((((s64)systarget) * (s64)st->scale) >> 12)		+ st->offset;	new_setpoint = max(new_setpoint, systarget);	new_setpoint = max(new_setpoint, st->pid.param.min);	new_setpoint = min(new_setpoint, st->pid.param.max);	DBG("wf_smu: adjusted setpoint: %d RPM\n", (int)new_setpoint);	if (st->cpu_setpoint == new_setpoint)		return;	st->cpu_setpoint = new_setpoint; readjust:	if (fan_cpu_main && wf_smu_failure_state == 0) {		rc = fan_cpu_main->ops->set_value(fan_cpu_main,						  st->cpu_setpoint);		if (rc) {			printk(KERN_WARNING "windfarm: CPU main fan"			       " error %d\n", rc);			wf_smu_failure_state |= FAILURE_FAN;		}	}}/* * ****** Setup / Init / Misc ... ****** * */static void wf_smu_tick(void){	unsigned int last_failure = wf_smu_failure_state;	unsigned int new_failure;	if (!wf_smu_started) {		DBG("wf: creating control loops !\n");		wf_smu_create_sys_fans();		wf_smu_create_cpu_fans();		wf_smu_started = 1;	}	/* Skipping ticks */	if (wf_smu_skipping && --wf_smu_skipping)		return;	wf_smu_failure_state = 0;	if (wf_smu_sys_fans)		wf_smu_sys_fans_tick(wf_smu_sys_fans);	if (wf_smu_cpu_fans)		wf_smu_cpu_fans_tick(wf_smu_cpu_fans);	wf_smu_readjust = 0;	new_failure = wf_smu_failure_state & ~last_failure;	/* If entering failure mode, clamp cpufreq and ramp all	 * fans to full speed.	 */	if (wf_smu_failure_state && !last_failure) {		if (cpufreq_clamp)			wf_control_set_max(cpufreq_clamp);		if (fan_system)			wf_control_set_max(fan_system);		if (fan_cpu_main)			wf_control_set_max(fan_cpu_main);		if (fan_hd)			wf_control_set_max(fan_hd);	}	/* If leaving failure mode, unclamp cpufreq and readjust	 * all fans on next iteration	 */	if (!wf_smu_failure_state && last_failure) {		if (cpufreq_clamp)			wf_control_set_min(cpufreq_clamp);		wf_smu_readjust = 1;	}	/* Overtemp condition detected, notify and start skipping a couple	 * ticks to let the temperature go down	 */	if (new_failure & FAILURE_OVERTEMP) {		wf_set_overtemp();		wf_smu_skipping = 2;	}	/* We only clear the overtemp condition if overtemp is cleared	 * _and_ no other failure is present. Since a sensor error will	 * clear the overtemp condition (can't measure temperature) at	 * the control loop levels, but we don't want to keep it clear	 * here in this case	 */	if (new_failure == 0 && last_failure & FAILURE_OVERTEMP)		wf_clear_overtemp();}static void wf_smu_new_control(struct wf_control *ct){	if (wf_smu_all_controls_ok)		return;	if (fan_cpu_main == NULL && !strcmp(ct->name, "cpu-fan")) {		if (wf_get_control(ct) == 0)			fan_cpu_main = ct;	}	if (fan_system == NULL && !strcmp(ct->name, "system-fan")) {		if (wf_get_control(ct) == 0)			fan_system = ct;	}	if (cpufreq_clamp == NULL && !strcmp(ct->name, "cpufreq-clamp")) {		if (wf_get_control(ct) == 0)			cpufreq_clamp = ct;	}	/* Darwin property list says the HD fan is only for model ID	 * 0, 1, 2 and 3	 */	if (wf_smu_mach_model > 3) {		if (fan_system && fan_cpu_main && cpufreq_clamp)			wf_smu_all_controls_ok = 1;		return;	}	if (fan_hd == NULL && !strcmp(ct->name, "drive-bay-fan")) {		if (wf_get_control(ct) == 0)			fan_hd = ct;	}	if (fan_system && fan_hd && fan_cpu_main && cpufreq_clamp)		wf_smu_all_controls_ok = 1;}static void wf_smu_new_sensor(struct wf_sensor *sr){	if (wf_smu_all_sensors_ok)		return;	if (sensor_cpu_power == NULL && !strcmp(sr->name, "cpu-power")) {		if (wf_get_sensor(sr) == 0)			sensor_cpu_power = sr;	}	if (sensor_cpu_temp == NULL && !strcmp(sr->name, "cpu-temp")) {		if (wf_get_sensor(sr) == 0)			sensor_cpu_temp = sr;	}	if (sensor_hd_temp == NULL && !strcmp(sr->name, "hd-temp")) {		if (wf_get_sensor(sr) == 0)			sensor_hd_temp = sr;	}	if (sensor_cpu_power && sensor_cpu_temp && sensor_hd_temp)		wf_smu_all_sensors_ok = 1;}static int wf_smu_notify(struct notifier_block *self,			       unsigned long event, void *data){	switch(event) {	case WF_EVENT_NEW_CONTROL:		DBG("wf: new control %s detected\n",		    ((struct wf_control *)data)->name);		wf_smu_new_control(data);		wf_smu_readjust = 1;		break;	case WF_EVENT_NEW_SENSOR:		DBG("wf: new sensor %s detected\n",		    ((struct wf_sensor *)data)->name);		wf_smu_new_sensor(data);		break;	case WF_EVENT_TICK:		if (wf_smu_all_controls_ok && wf_smu_all_sensors_ok)			wf_smu_tick();	}	return 0;}static struct notifier_block wf_smu_events = {	.notifier_call	= wf_smu_notify,};static int wf_init_pm(void){	const struct smu_sdbp_header *hdr;	hdr = smu_get_sdb_partition(SMU_SDB_SENSORTREE_ID, NULL);	if (hdr != 0) {		struct smu_sdbp_sensortree *st =			(struct smu_sdbp_sensortree *)&hdr[1];		wf_smu_mach_model = st->model_id;	}	printk(KERN_INFO "windfarm: Initializing for iMacG5 model ID %d\n",	       wf_smu_mach_model);	return 0;}static int wf_smu_probe(struct platform_device *ddev){	wf_register_client(&wf_smu_events);	return 0;}static int __devexit wf_smu_remove(struct platform_device *ddev){	wf_unregister_client(&wf_smu_events);	/* XXX We don't have yet a guarantee that our callback isn't	 * in progress when returning from wf_unregister_client, so	 * we add an arbitrary delay. I'll have to fix that in the core	 */	msleep(1000);	/* Release all sensors */	/* One more crappy race: I don't think we have any guarantee here	 * that the attribute callback won't race with the sensor beeing	 * disposed of, and I'm not 100% certain what best way to deal	 * with that except by adding locks all over... I'll do that	 * eventually but heh, who ever rmmod this module anyway ?	 */	if (sensor_cpu_power)		wf_put_sensor(sensor_cpu_power);	if (sensor_cpu_temp)		wf_put_sensor(sensor_cpu_temp);	if (sensor_hd_temp)		wf_put_sensor(sensor_hd_temp);	/* Release all controls */	if (fan_cpu_main)		wf_put_control(fan_cpu_main);	if (fan_hd)		wf_put_control(fan_hd);	if (fan_system)		wf_put_control(fan_system);	if (cpufreq_clamp)		wf_put_control(cpufreq_clamp);	/* Destroy control loops state structures */	if (wf_smu_sys_fans)		kfree(wf_smu_sys_fans);	if (wf_smu_cpu_fans)		kfree(wf_smu_cpu_fans);	return 0;}static struct platform_driver wf_smu_driver = {        .probe = wf_smu_probe,        .remove = __devexit_p(wf_smu_remove),	.driver = {		.name = "windfarm",		.bus = &platform_bus_type,	},};static int __init wf_smu_init(void){	int rc = -ENODEV;	if (machine_is_compatible("PowerMac8,1") ||	    machine_is_compatible("PowerMac8,2"))		rc = wf_init_pm();	if (rc == 0) {#ifdef MODULE		request_module("windfarm_smu_controls");		request_module("windfarm_smu_sensors");		request_module("windfarm_lm75_sensor");		request_module("windfarm_cpufreq_clamp");#endif /* MODULE */		platform_driver_register(&wf_smu_driver);	}	return rc;}static void __exit wf_smu_exit(void){	platform_driver_unregister(&wf_smu_driver);}module_init(wf_smu_init);module_exit(wf_smu_exit);MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");MODULE_DESCRIPTION("Thermal control logic for iMac G5");MODULE_LICENSE("GPL");

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?