<commit_before>/*
 * Copyright 2010 Red Hat Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: <NAME>
 */

#include "drmP.h"

#include "nouveau_drv.h"
#include "nouveau_pm.h"
#include "nouveau_gpio.h"

#ifdef CONFIG_ACPI
#include <linux/acpi.h>
#endif
#include <linux/power_supply.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>

static int
nouveau_pwmfan_get(struct drm_device *dev)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
	struct gpio_func gpio;
	u32 divs, duty;
	int ret;

	if (!pm->pwm_get)
		return -ENODEV;

	ret = nouveau_gpio_find(dev, 0, DCB_GPIO_PWM_FAN, 0xff, &gpio);
	if (ret == 0) {
		ret = pm->pwm_get(dev, gpio.line, &divs, &duty);
		if (ret == 0) {
			divs = max(divs, duty);
			if (dev_priv->card_type <= NV_40 || (gpio.log[0] & 1))
				duty = divs - duty;
			return (duty * 100) / divs;
		}

		return nouveau_gpio_func_get(dev, gpio.func) * 100;
	}

	return -ENODEV;
}

static int
nouveau_pwmfan_set(struct drm_device *dev, int percent)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
	struct gpio_func gpio;
	u32 divs, duty;
	int ret;

	if (!pm->pwm_set)
		return -ENODEV;

	ret = nouveau_gpio_find(dev, 0, DCB_GPIO_PWM_FAN, 0xff, &gpio);
	if (ret == 0) {
		divs = pm->fan.pwm_divisor;
		if (pm->fan.pwm_freq) {
			/*XXX: PNVIO clock more than likely... */
			divs = 135000 / pm->fan.pwm_freq;
			if (dev_priv->chipset < 0xa3)
				divs /= 4;
		}

		duty = ((divs * percent) + 99) / 100;
		if (dev_priv->card_type <= NV_40 || (gpio.log[0] & 1))
			duty = divs - duty;

		ret = pm->pwm_set(dev, gpio.line, divs, duty);
		if (!ret)
			pm->fan.percent = percent;
		return ret;
	}

	return -ENODEV;
}

static int
nouveau_pm_perflvl_aux(struct drm_device *dev, struct nouveau_pm_level *perflvl,
		       struct nouveau_pm_level *a, struct nouveau_pm_level *b)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
	int ret;

	/*XXX: not on all boards, we should control based on temperature
	 *     on recent boards..  or maybe on some other factor we don't
	 *     know about?
	 */
	if (a->fanspeed && b->fanspeed && b->fanspeed > a->fanspeed) {
		ret = nouveau_pwmfan_set(dev, perflvl->fanspeed);
		if (ret && ret != -ENODEV) {
			NV_ERROR(dev, "fanspeed set failed: %d\n", ret);
			return ret;
		}
	}

	if (pm->voltage.supported && pm->voltage_set) {
		if (perflvl->volt_min && b->volt_min > a->volt_min) {
			ret = pm->voltage_set(dev, perflvl->volt_min);
			if (ret) {
				NV_ERROR(dev, "voltage set failed: %d\n", ret);
				return ret;
			}
		}
	}

	return 0;
}

static int
nouveau_pm_perflvl_set(struct drm_device *dev, struct nouveau_pm_level *perflvl)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
	void *state;
	int ret;

	if (perflvl == pm->cur)
		return 0;

	ret = nouveau_pm_perflvl_aux(dev, perflvl, pm->cur, perflvl);
	if (ret)
		return ret;

	state = pm->clocks_pre(dev, perflvl);
	if (IS_ERR(state))
		return PTR_ERR(state);
	pm->clocks_set(dev, state);

	ret = nouveau_pm_perflvl_aux(dev, perflvl, perflvl, pm->cur);
	if (ret)
		return ret;

	pm->cur = perflvl;
	return 0;
}

static int
nouveau_pm_profile_set(struct drm_device *dev, const char *profile)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
	struct nouveau_pm_level *perflvl = NULL;
	long pl;

	/* safety precaution, for now */
	if (nouveau_perflvl_wr != 7777)
		return -EPERM;

	if (!strncmp(profile, "boot", 4))
		perflvl = &pm->boot;
	else {
		int i;
		if (kstrtol(profile, 10, &pl) == -EINVAL)
			return -EINVAL;

		for (i = 0; i < pm->nr_perflvl; i++) {
			if (pm->perflvl[i].id == pl) {
				perflvl = &pm->perflvl[i];
				break;
			}
		}

		if (!perflvl)
			return -EINVAL;
	}

	NV_INFO(dev, "setting performance level: %s\n", profile);
	return nouveau_pm_perflvl_set(dev, perflvl);
}

static int
nouveau_pm_perflvl_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
	int ret;

	memset(perflvl, 0, sizeof(*perflvl));

	ret = pm->clocks_get(dev, perflvl);<commit_msg>drm/nouveau/pm: improve the reclocking logs' readability<commit_after>/*
 * Copyright 2010 Red Hat Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: <NAME>
 */

#include "drmP.h"

#include "nouveau_drv.h"
#include "nouveau_pm.h"
#include "nouveau_gpio.h"

#ifdef CONFIG_ACPI
#include <linux/acpi.h>
#endif
#include <linux/power_supply.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>

static int
nouveau_pwmfan_get(struct drm_device *dev)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
	struct gpio_func gpio;
	u32 divs, duty;
	int ret;

	if (!pm->pwm_get)
		return -ENODEV;

	ret = nouveau_gpio_find(dev, 0, DCB_GPIO_PWM_FAN, 0xff, &gpio);
	if (ret == 0) {
		ret = pm->pwm_get(dev, gpio.line, &divs, &duty);
		if (ret == 0) {
			divs = max(divs, duty);
			if (dev_priv->card_type <= NV_40 || (gpio.log[0] & 1))
				duty = divs - duty;
			return (duty * 100) / divs;
		}

		return nouveau_gpio_func_get(dev, gpio.func) * 100;
	}

	return -ENODEV;
}

static int
nouveau_pwmfan_set(struct drm_device *dev, int percent)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
	struct gpio_func gpio;
	u32 divs, duty;
	int ret;

	if (!pm->pwm_set)
		return -ENODEV;

	ret = nouveau_gpio_find(dev, 0, DCB_GPIO_PWM_FAN, 0xff, &gpio);
	if (ret == 0) {
		divs = pm->fan.pwm_divisor;
		if (pm->fan.pwm_freq) {
			/*XXX: PNVIO clock more than likely... */
			divs = 135000 / pm->fan.pwm_freq;
			if (dev_priv->chipset < 0xa3)
				divs /= 4;
		}

		duty = ((divs * percent) + 99) / 100;
		if (dev_priv->card_type <= NV_40 || (gpio.log[0] & 1))
			duty = divs - duty;

		ret = pm->pwm_set(dev, gpio.line, divs, duty);
		if (!ret)
			pm->fan.percent = percent;
		return ret;
	}

	return -ENODEV;
}

static int
nouveau_pm_perflvl_aux(struct drm_device *dev, struct nouveau_pm_level *perflvl,
		       struct nouveau_pm_level *a, struct nouveau_pm_level *b)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
	int ret;

	/*XXX: not on all boards, we should control based on temperature
	 *     on recent boards..  or maybe on some other factor we don't
	 *     know about?
	 */
	if (a->fanspeed && b->fanspeed && b->fanspeed > a->fanspeed) {
		ret = nouveau_pwmfan_set(dev, perflvl->fanspeed);
		if (ret && ret != -ENODEV) {
			NV_ERROR(dev, "fanspeed set failed: %d\n", ret);
			return ret;
		}
	}

	if (pm->voltage.supported && pm->voltage_set) {
		if (perflvl->volt_min && b->volt_min > a->volt_min) {
			ret = pm->voltage_set(dev, perflvl->volt_min);
			if (ret) {
				NV_ERROR(dev, "voltage set failed: %d\n", ret);
				return ret;
			}
		}
	}

	return 0;
}

static int
nouveau_pm_perflvl_set(struct drm_device *dev, struct nouveau_pm_level *perflvl)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
	void *state;
	int ret;

	if (perflvl == pm->cur)
		return 0;

	ret = nouveau_pm_perflvl_aux(dev, perflvl, pm->cur, perflvl);
	if (ret)
		return ret;

	state = pm->clocks_pre(dev, perflvl);
	if (IS_ERR(state)) {
		ret = PTR_ERR(state);
		goto error;
	}
	ret = pm->clocks_set(dev, state);
	if (ret)
		goto error;

	ret = nouveau_pm_perflvl_aux(dev, perflvl, perflvl, pm->cur);
	if (ret)
		return ret;

	pm->cur = perflvl;
	return 0;

error:
	/* restore the fan speed and voltage before leaving */
	nouveau_pm_perflvl_aux(dev, perflvl, perflvl, pm->cur);
	return ret;
}

static int
nouveau_pm_profile_set(struct drm_device *dev, const char *profile)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
	struct nouveau_pm_level *perflvl = NULL;
	u64 start_time;
	int ret = 0;
	long pl;

	/* safety precaution, for now */
	if (nouveau_perflvl_wr != 7777)
		return -EPERM;

	if (!strncmp(profile, "boot", 4))
		perflvl = &pm->boot;
	else {
		int i;
		if (kstrtol(profile, 10, &pl) == -EINVAL)
			return -EINVAL;

		for (i = 0; i < pm->nr_perflvl; i++) {
			if (pm->perflvl[i].id == pl) {
				perflvl = &pm->perflvl[i];
				break;
			}
		}

		if (!perflvl)
			return -EINVAL;
	}

	NV_INFO(dev, "setting performance level: %s", profile);
	start_time = nv04_timer_read(dev);
	ret = nouveau_pm_perflvl_set(dev, perflvl);
	if (!ret) {
		NV_INFO(dev, "> reclocking took %lluns\n\n",
			(nv04_timer_read(dev) - start_time));
	} else {
		NV_INFO(dev, "> reclocking failed\n\n");
	}

	return ret;
}

static int
nouveau_pm_perflvl_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
	int ret;

	memset(perflvl, 0, sizeof(*perflvl));

	ret = pm->clocks_get(dev, perflvl);
{'fraction_non_alphanumeric': 0.12753829711435696, 'fraction_numerical': 0.01042037762736017, 'mean_word_length': 3.9314888010540185, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 112, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4806245', 'n_tokens_mistral': 4662, 'n_tokens_neox': 4251, 'n_words': 1195}