From 15e259bc5255e891f776a84b5f28a97ea0567178 Mon Sep 17 00:00:00 2001 From: Terje Bergstrom Date: Mon, 30 Oct 2017 12:52:52 -0700 Subject: gpu: nvgpu: Move gk20a_scale to be Linux only Move gk20a_scale.[ch] to be common/linux/scale.[ch]. The code is Linux specific, and only referred from Linux specific source files. Change the license back to GPL. JIRA NVGPU-259 Change-Id: I89fa905a1fea4f93c826ddfe2ffce34aefc1b0a2 Signed-off-by: Terje Bergstrom Reviewed-on: https://git-master.nvidia.com/r/1588650 Reviewed-by: mobile promotions Tested-by: mobile promotions --- drivers/gpu/nvgpu/Makefile | 2 +- drivers/gpu/nvgpu/common/linux/driver_common.c | 2 +- drivers/gpu/nvgpu/common/linux/module.c | 2 +- .../gpu/nvgpu/common/linux/platform_gk20a_tegra.c | 2 +- .../gpu/nvgpu/common/linux/platform_gp10b_tegra.c | 2 +- drivers/gpu/nvgpu/common/linux/scale.c | 428 ++++++++++++++++++++ drivers/gpu/nvgpu/common/linux/scale.h | 66 ++++ drivers/gpu/nvgpu/gk20a/gk20a_scale.c | 434 --------------------- drivers/gpu/nvgpu/gk20a/gk20a_scale.h | 72 ---- drivers/gpu/nvgpu/vgpu/vgpu.c | 2 +- 10 files changed, 500 insertions(+), 512 deletions(-) create mode 100644 drivers/gpu/nvgpu/common/linux/scale.c create mode 100644 drivers/gpu/nvgpu/common/linux/scale.h delete mode 100644 drivers/gpu/nvgpu/gk20a/gk20a_scale.c delete mode 100644 drivers/gpu/nvgpu/gk20a/gk20a_scale.h diff --git a/drivers/gpu/nvgpu/Makefile b/drivers/gpu/nvgpu/Makefile index f1a6f267..06d3dedb 100644 --- a/drivers/gpu/nvgpu/Makefile +++ b/drivers/gpu/nvgpu/Makefile @@ -180,7 +180,7 @@ nvgpu-$(CONFIG_COMMON_CLK) += \ common/linux/clk.o nvgpu-$(CONFIG_GK20A_DEVFREQ) += \ - gk20a/gk20a_scale.o + common/linux/scale.o nvgpu-$(CONFIG_GK20A_CYCLE_STATS) += \ gk20a/css_gr_gk20a.o diff --git a/drivers/gpu/nvgpu/common/linux/driver_common.c b/drivers/gpu/nvgpu/common/linux/driver_common.c index e4a65692..759607a2 100644 --- a/drivers/gpu/nvgpu/common/linux/driver_common.c +++ b/drivers/gpu/nvgpu/common/linux/driver_common.c @@ -23,7 +23,7 @@ #include #include -#include "gk20a/gk20a_scale.h" +#include "scale.h" #include "gk20a/gk20a.h" #include "gk20a/platform_gk20a.h" #include "module.h" diff --git a/drivers/gpu/nvgpu/common/linux/module.c b/drivers/gpu/nvgpu/common/linux/module.c index 4f66fc67..d0abc836 100644 --- a/drivers/gpu/nvgpu/common/linux/module.c +++ b/drivers/gpu/nvgpu/common/linux/module.c @@ -35,7 +35,7 @@ #include "gk20a/platform_gk20a.h" #include "sysfs.h" #include "vgpu/vgpu.h" -#include "gk20a/gk20a_scale.h" +#include "scale.h" #include "gk20a/ctxsw_trace_gk20a.h" #include "pci.h" #include "module.h" diff --git a/drivers/gpu/nvgpu/common/linux/platform_gk20a_tegra.c b/drivers/gpu/nvgpu/common/linux/platform_gk20a_tegra.c index 5786feab..2d6d156c 100644 --- a/drivers/gpu/nvgpu/common/linux/platform_gk20a_tegra.c +++ b/drivers/gpu/nvgpu/common/linux/platform_gk20a_tegra.c @@ -48,9 +48,9 @@ #include "gk20a/gk20a.h" #include "gk20a/platform_gk20a.h" -#include "gk20a/gk20a_scale.h" #include "gm20b/clk_gm20b.h" +#include "scale.h" #include "clk.h" #include "os_linux.h" diff --git a/drivers/gpu/nvgpu/common/linux/platform_gp10b_tegra.c b/drivers/gpu/nvgpu/common/linux/platform_gp10b_tegra.c index e5d19976..fb8686c2 100644 --- a/drivers/gpu/nvgpu/common/linux/platform_gp10b_tegra.c +++ b/drivers/gpu/nvgpu/common/linux/platform_gp10b_tegra.c @@ -35,11 +35,11 @@ #include "gk20a/platform_gk20a.h" #include "gk20a/gk20a.h" -#include "gk20a/gk20a_scale.h" #include "platform_gk20a_tegra.h" #include "gp10b/platform_gp10b.h" #include "platform_gp10b_tegra.h" +#include "scale.h" /* Select every GP10B_FREQ_SELECT_STEP'th frequency from h/w table */ #define GP10B_FREQ_SELECT_STEP 8 diff --git a/drivers/gpu/nvgpu/common/linux/scale.c b/drivers/gpu/nvgpu/common/linux/scale.c new file mode 100644 index 00000000..05f09dcc --- /dev/null +++ b/drivers/gpu/nvgpu/common/linux/scale.c @@ -0,0 +1,428 @@ +/* + * gk20a clock scaling profile + * + * Copyright (c) 2013-2017, NVIDIA Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include + +#include + +#include +#include + +#include "gk20a/gk20a.h" +#include "gk20a/platform_gk20a.h" +#include "scale.h" +#include "os_linux.h" + +/* + * gk20a_scale_qos_notify() + * + * This function is called when the minimum QoS requirement for the device + * has changed. The function calls postscaling callback if it is defined. + */ + +#if defined(CONFIG_COMMON_CLK) +int gk20a_scale_qos_notify(struct notifier_block *nb, + unsigned long n, void *p) +{ + struct gk20a_scale_profile *profile = + container_of(nb, struct gk20a_scale_profile, + qos_notify_block); + struct gk20a *g = get_gk20a(profile->dev); + struct nvgpu_os_linux *l = nvgpu_os_linux_from_gk20a(g); + struct devfreq *devfreq = l->devfreq; + + if (!devfreq) + return NOTIFY_OK; + + mutex_lock(&devfreq->lock); + /* check for pm_qos min and max frequency requirement */ + profile->qos_min_freq = + (unsigned long)pm_qos_read_min_bound(PM_QOS_GPU_FREQ_BOUNDS) * 1000UL; + profile->qos_max_freq = + (unsigned long)pm_qos_read_max_bound(PM_QOS_GPU_FREQ_BOUNDS) * 1000UL; + + if (profile->qos_min_freq > profile->qos_max_freq) { + nvgpu_err(g, + "QoS: setting invalid limit, min_freq=%lu max_freq=%lu", + profile->qos_min_freq, profile->qos_max_freq); + profile->qos_min_freq = profile->qos_max_freq; + } + + update_devfreq(devfreq); + mutex_unlock(&devfreq->lock); + + return NOTIFY_OK; +} +#else +int gk20a_scale_qos_notify(struct notifier_block *nb, + unsigned long n, void *p) +{ + struct gk20a_scale_profile *profile = + container_of(nb, struct gk20a_scale_profile, + qos_notify_block); + struct gk20a_platform *platform = dev_get_drvdata(profile->dev); + struct gk20a *g = get_gk20a(profile->dev); + struct nvgpu_os_linux *l = nvgpu_os_linux_from_gk20a(g); + unsigned long freq; + + if (!platform->postscale) + return NOTIFY_OK; + + /* get the frequency requirement. if devfreq is enabled, check if it + * has higher demand than qos */ + freq = platform->clk_round_rate(profile->dev, + (u32)pm_qos_read_min_bound(PM_QOS_GPU_FREQ_BOUNDS)); + if (l->devfreq) + freq = max(l->devfreq->previous_freq, freq); + + /* Update gpu load because we may scale the emc target + * if the gpu load changed. */ + nvgpu_pmu_load_update(g); + platform->postscale(profile->dev, freq); + + return NOTIFY_OK; +} +#endif + +/* + * gk20a_scale_make_freq_table(profile) + * + * This function initialises the frequency table for the given device profile + */ + +static int gk20a_scale_make_freq_table(struct gk20a_scale_profile *profile) +{ + struct gk20a_platform *platform = dev_get_drvdata(profile->dev); + int num_freqs, err; + unsigned long *freqs; + + if (platform->get_clk_freqs) { + /* get gpu frequency table */ + err = platform->get_clk_freqs(profile->dev, &freqs, + &num_freqs); + if (err) + return -ENOSYS; + } else + return -ENOSYS; + + profile->devfreq_profile.freq_table = (unsigned long *)freqs; + profile->devfreq_profile.max_state = num_freqs; + + return 0; +} + +/* + * gk20a_scale_target(dev, *freq, flags) + * + * This function scales the clock + */ + +static int gk20a_scale_target(struct device *dev, unsigned long *freq, + u32 flags) +{ + struct gk20a_platform *platform = dev_get_drvdata(dev); + struct gk20a *g = platform->g; + struct nvgpu_os_linux *l = nvgpu_os_linux_from_gk20a(g); + struct gk20a_scale_profile *profile = g->scale_profile; + struct devfreq *devfreq = l->devfreq; + unsigned long local_freq = *freq; + unsigned long rounded_rate; + unsigned long min_freq = 0, max_freq = 0; + + /* + * Calculate floor and cap frequency values + * + * Policy : + * We have two APIs to clip the frequency + * 1. devfreq + * 2. pm_qos + * + * To calculate floor (min) freq, we select MAX of floor frequencies + * requested from both APIs + * To get cap (max) freq, we select MIN of max frequencies + * + * In case we have conflict (min_freq > max_freq) after above + * steps, we ensure that max_freq wins over min_freq + */ + min_freq = max_t(u32, devfreq->min_freq, profile->qos_min_freq); + max_freq = min_t(u32, devfreq->max_freq, profile->qos_max_freq); + + if (min_freq > max_freq) + min_freq = max_freq; + + /* Clip requested frequency */ + if (local_freq < min_freq) + local_freq = min_freq; + + if (local_freq > max_freq) + local_freq = max_freq; + + /* set the final frequency */ + rounded_rate = platform->clk_round_rate(dev, local_freq); + + /* Check for duplicate request */ + if (rounded_rate == g->last_freq) + return 0; + + if (g->ops.clk.get_rate(g, CTRL_CLK_DOMAIN_GPCCLK) == rounded_rate) + *freq = rounded_rate; + else { + g->ops.clk.set_rate(g, CTRL_CLK_DOMAIN_GPCCLK, rounded_rate); + *freq = g->ops.clk.get_rate(g, CTRL_CLK_DOMAIN_GPCCLK); + } + + g->last_freq = *freq; + + /* postscale will only scale emc (dram clock) if evaluating + * gk20a_tegra_get_emc_rate() produces a new or different emc + * target because the load or_and gpufreq has changed */ + if (platform->postscale) + platform->postscale(dev, rounded_rate); + + return 0; +} + +/* + * update_load_estimate_gpmu(profile) + * + * Update load estimate using gpmu. The gpmu value is normalised + * based on the time it was asked last time. + */ + +static void update_load_estimate_gpmu(struct device *dev) +{ + struct gk20a *g = get_gk20a(dev); + struct gk20a_scale_profile *profile = g->scale_profile; + unsigned long dt; + u32 busy_time; + ktime_t t; + + t = ktime_get(); + dt = ktime_us_delta(t, profile->last_event_time); + + profile->dev_stat.total_time = dt; + profile->last_event_time = t; + nvgpu_pmu_load_norm(g, &busy_time); + profile->dev_stat.busy_time = (busy_time * dt) / 1000; +} + +/* + * gk20a_scale_suspend(dev) + * + * This function informs devfreq of suspend + */ + +void gk20a_scale_suspend(struct device *dev) +{ + struct gk20a *g = get_gk20a(dev); + struct nvgpu_os_linux *l = nvgpu_os_linux_from_gk20a(g); + struct devfreq *devfreq = l->devfreq; + + if (!devfreq) + return; + + devfreq_suspend_device(devfreq); +} + +/* + * gk20a_scale_resume(dev) + * + * This functions informs devfreq of resume + */ + +void gk20a_scale_resume(struct device *dev) +{ + struct gk20a *g = get_gk20a(dev); + struct nvgpu_os_linux *l = nvgpu_os_linux_from_gk20a(g); + struct devfreq *devfreq = l->devfreq; + + if (!devfreq) + return; + + g->last_freq = 0; + devfreq_resume_device(devfreq); +} + +/* + * gk20a_scale_get_dev_status(dev, *stat) + * + * This function queries the current device status. + */ + +static int gk20a_scale_get_dev_status(struct device *dev, + struct devfreq_dev_status *stat) +{ + struct gk20a *g = get_gk20a(dev); + struct gk20a_scale_profile *profile = g->scale_profile; + struct gk20a_platform *platform = dev_get_drvdata(dev); + + /* update the software shadow */ + nvgpu_pmu_load_update(g); + + /* inform edp about new constraint */ + if (platform->prescale) + platform->prescale(dev); + + /* Make sure there are correct values for the current frequency */ + profile->dev_stat.current_frequency = + g->ops.clk.get_rate(g, CTRL_CLK_DOMAIN_GPCCLK); + + /* Update load estimate */ + update_load_estimate_gpmu(dev); + + /* Copy the contents of the current device status */ + *stat = profile->dev_stat; + + /* Finally, clear out the local values */ + profile->dev_stat.total_time = 0; + profile->dev_stat.busy_time = 0; + + return 0; +} + +/* + * get_cur_freq(struct device *dev, unsigned long *freq) + * + * This function gets the current GPU clock rate. + */ + +static int get_cur_freq(struct device *dev, unsigned long *freq) +{ + struct gk20a *g = get_gk20a(dev); + *freq = g->ops.clk.get_rate(g, CTRL_CLK_DOMAIN_GPCCLK); + return 0; +} + + +/* + * gk20a_scale_init(dev) + */ + +void gk20a_scale_init(struct device *dev) +{ + struct gk20a_platform *platform = dev_get_drvdata(dev); + struct gk20a *g = platform->g; + struct nvgpu_os_linux *l = nvgpu_os_linux_from_gk20a(g); + struct gk20a_scale_profile *profile; + int err; + + if (g->scale_profile) + return; + + if (!platform->devfreq_governor && !platform->qos_notify) + return; + + profile = nvgpu_kzalloc(g, sizeof(*profile)); + + profile->dev = dev; + profile->dev_stat.busy = false; + + /* Create frequency table */ + err = gk20a_scale_make_freq_table(profile); + if (err || !profile->devfreq_profile.max_state) + goto err_get_freqs; + + profile->qos_min_freq = 0; + profile->qos_max_freq = UINT_MAX; + + /* Store device profile so we can access it if devfreq governor + * init needs that */ + g->scale_profile = profile; + + if (platform->devfreq_governor) { + struct devfreq *devfreq; + + profile->devfreq_profile.initial_freq = + profile->devfreq_profile.freq_table[0]; + profile->devfreq_profile.target = gk20a_scale_target; + profile->devfreq_profile.get_dev_status = + gk20a_scale_get_dev_status; + profile->devfreq_profile.get_cur_freq = get_cur_freq; + profile->devfreq_profile.polling_ms = 25; + + devfreq = devfreq_add_device(dev, + &profile->devfreq_profile, + platform->devfreq_governor, NULL); + + if (IS_ERR(devfreq)) + devfreq = NULL; + + l->devfreq = devfreq; + } + + /* Should we register QoS callback for this device? */ + if (platform->qos_notify) { + profile->qos_notify_block.notifier_call = + platform->qos_notify; + + pm_qos_add_min_notifier(PM_QOS_GPU_FREQ_BOUNDS, + &profile->qos_notify_block); + pm_qos_add_max_notifier(PM_QOS_GPU_FREQ_BOUNDS, + &profile->qos_notify_block); + } + + return; + +err_get_freqs: + nvgpu_kfree(g, profile); +} + +void gk20a_scale_exit(struct device *dev) +{ + struct gk20a_platform *platform = dev_get_drvdata(dev); + struct gk20a *g = platform->g; + struct nvgpu_os_linux *l = nvgpu_os_linux_from_gk20a(g); + int err; + + if (platform->qos_notify) { + pm_qos_remove_min_notifier(PM_QOS_GPU_FREQ_BOUNDS, + &g->scale_profile->qos_notify_block); + pm_qos_remove_max_notifier(PM_QOS_GPU_FREQ_BOUNDS, + &g->scale_profile->qos_notify_block); + } + + if (platform->devfreq_governor) { + err = devfreq_remove_device(l->devfreq); + l->devfreq = NULL; + } + + nvgpu_kfree(g, g->scale_profile); + g->scale_profile = NULL; +} + +/* + * gk20a_scale_hw_init(dev) + * + * Initialize hardware portion of the device + */ + +void gk20a_scale_hw_init(struct device *dev) +{ + struct gk20a_platform *platform = dev_get_drvdata(dev); + struct gk20a_scale_profile *profile = platform->g->scale_profile; + + /* make sure that scaling has bee initialised */ + if (!profile) + return; + + profile->dev_stat.total_time = 0; + profile->last_event_time = ktime_get(); +} diff --git a/drivers/gpu/nvgpu/common/linux/scale.h b/drivers/gpu/nvgpu/common/linux/scale.h new file mode 100644 index 00000000..c1e6fe86 --- /dev/null +++ b/drivers/gpu/nvgpu/common/linux/scale.h @@ -0,0 +1,66 @@ +/* + * gk20a clock scaling profile + * + * Copyright (c) 2013-2016, NVIDIA Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef GK20A_SCALE_H +#define GK20A_SCALE_H + +#include + +struct clk; + +struct gk20a_scale_profile { + struct device *dev; + ktime_t last_event_time; + struct devfreq_dev_profile devfreq_profile; + struct devfreq_dev_status dev_stat; + struct notifier_block qos_notify_block; + unsigned long qos_min_freq; + unsigned long qos_max_freq; + void *private_data; +}; + +/* Initialization and de-initialization for module */ +void gk20a_scale_init(struct device *); +void gk20a_scale_exit(struct device *); +void gk20a_scale_hw_init(struct device *dev); + +#if defined(CONFIG_GK20A_DEVFREQ) +/* + * call when performing submit to notify scaling mechanism that the module is + * in use + */ +void gk20a_scale_notify_busy(struct device *); +void gk20a_scale_notify_idle(struct device *); + +void gk20a_scale_suspend(struct device *); +void gk20a_scale_resume(struct device *); +int gk20a_scale_qos_notify(struct notifier_block *nb, + unsigned long n, void *p); +#else +static inline void gk20a_scale_notify_busy(struct device *dev) {} +static inline void gk20a_scale_notify_idle(struct device *dev) {} +static inline void gk20a_scale_suspend(struct device *dev) {} +static inline void gk20a_scale_resume(struct device *dev) {} +static inline int gk20a_scale_qos_notify(struct notifier_block *nb, + unsigned long n, void *p) +{ + return -ENOSYS; +} +#endif + +#endif diff --git a/drivers/gpu/nvgpu/gk20a/gk20a_scale.c b/drivers/gpu/nvgpu/gk20a/gk20a_scale.c deleted file mode 100644 index ae426eec..00000000 --- a/drivers/gpu/nvgpu/gk20a/gk20a_scale.c +++ /dev/null @@ -1,434 +0,0 @@ -/* - * gk20a clock scaling profile - * - * Copyright (c) 2013-2017, NVIDIA Corporation. All rights reserved. - * - * 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 AUTHORS OR COPYRIGHT HOLDERS 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. - */ - -#include -#include -#include -#include - -#include - -#include -#include - -#include "gk20a.h" -#include "platform_gk20a.h" -#include "gk20a_scale.h" -#include "common/linux/os_linux.h" - -/* - * gk20a_scale_qos_notify() - * - * This function is called when the minimum QoS requirement for the device - * has changed. The function calls postscaling callback if it is defined. - */ - -#if defined(CONFIG_COMMON_CLK) -int gk20a_scale_qos_notify(struct notifier_block *nb, - unsigned long n, void *p) -{ - struct gk20a_scale_profile *profile = - container_of(nb, struct gk20a_scale_profile, - qos_notify_block); - struct gk20a *g = get_gk20a(profile->dev); - struct nvgpu_os_linux *l = nvgpu_os_linux_from_gk20a(g); - struct devfreq *devfreq = l->devfreq; - - if (!devfreq) - return NOTIFY_OK; - - mutex_lock(&devfreq->lock); - /* check for pm_qos min and max frequency requirement */ - profile->qos_min_freq = - (unsigned long)pm_qos_read_min_bound(PM_QOS_GPU_FREQ_BOUNDS) * 1000UL; - profile->qos_max_freq = - (unsigned long)pm_qos_read_max_bound(PM_QOS_GPU_FREQ_BOUNDS) * 1000UL; - - if (profile->qos_min_freq > profile->qos_max_freq) { - nvgpu_err(g, - "QoS: setting invalid limit, min_freq=%lu max_freq=%lu", - profile->qos_min_freq, profile->qos_max_freq); - profile->qos_min_freq = profile->qos_max_freq; - } - - update_devfreq(devfreq); - mutex_unlock(&devfreq->lock); - - return NOTIFY_OK; -} -#else -int gk20a_scale_qos_notify(struct notifier_block *nb, - unsigned long n, void *p) -{ - struct gk20a_scale_profile *profile = - container_of(nb, struct gk20a_scale_profile, - qos_notify_block); - struct gk20a_platform *platform = dev_get_drvdata(profile->dev); - struct gk20a *g = get_gk20a(profile->dev); - struct nvgpu_os_linux *l = nvgpu_os_linux_from_gk20a(g); - unsigned long freq; - - if (!platform->postscale) - return NOTIFY_OK; - - /* get the frequency requirement. if devfreq is enabled, check if it - * has higher demand than qos */ - freq = platform->clk_round_rate(profile->dev, - (u32)pm_qos_read_min_bound(PM_QOS_GPU_FREQ_BOUNDS)); - if (l->devfreq) - freq = max(l->devfreq->previous_freq, freq); - - /* Update gpu load because we may scale the emc target - * if the gpu load changed. */ - nvgpu_pmu_load_update(g); - platform->postscale(profile->dev, freq); - - return NOTIFY_OK; -} -#endif - -/* - * gk20a_scale_make_freq_table(profile) - * - * This function initialises the frequency table for the given device profile - */ - -static int gk20a_scale_make_freq_table(struct gk20a_scale_profile *profile) -{ - struct gk20a_platform *platform = dev_get_drvdata(profile->dev); - int num_freqs, err; - unsigned long *freqs; - - if (platform->get_clk_freqs) { - /* get gpu frequency table */ - err = platform->get_clk_freqs(profile->dev, &freqs, - &num_freqs); - if (err) - return -ENOSYS; - } else - return -ENOSYS; - - profile->devfreq_profile.freq_table = (unsigned long *)freqs; - profile->devfreq_profile.max_state = num_freqs; - - return 0; -} - -/* - * gk20a_scale_target(dev, *freq, flags) - * - * This function scales the clock - */ - -static int gk20a_scale_target(struct device *dev, unsigned long *freq, - u32 flags) -{ - struct gk20a_platform *platform = dev_get_drvdata(dev); - struct gk20a *g = platform->g; - struct nvgpu_os_linux *l = nvgpu_os_linux_from_gk20a(g); - struct gk20a_scale_profile *profile = g->scale_profile; - struct devfreq *devfreq = l->devfreq; - unsigned long local_freq = *freq; - unsigned long rounded_rate; - unsigned long min_freq = 0, max_freq = 0; - - /* - * Calculate floor and cap frequency values - * - * Policy : - * We have two APIs to clip the frequency - * 1. devfreq - * 2. pm_qos - * - * To calculate floor (min) freq, we select MAX of floor frequencies - * requested from both APIs - * To get cap (max) freq, we select MIN of max frequencies - * - * In case we have conflict (min_freq > max_freq) after above - * steps, we ensure that max_freq wins over min_freq - */ - min_freq = max_t(u32, devfreq->min_freq, profile->qos_min_freq); - max_freq = min_t(u32, devfreq->max_freq, profile->qos_max_freq); - - if (min_freq > max_freq) - min_freq = max_freq; - - /* Clip requested frequency */ - if (local_freq < min_freq) - local_freq = min_freq; - - if (local_freq > max_freq) - local_freq = max_freq; - - /* set the final frequency */ - rounded_rate = platform->clk_round_rate(dev, local_freq); - - /* Check for duplicate request */ - if (rounded_rate == g->last_freq) - return 0; - - if (g->ops.clk.get_rate(g, CTRL_CLK_DOMAIN_GPCCLK) == rounded_rate) - *freq = rounded_rate; - else { - g->ops.clk.set_rate(g, CTRL_CLK_DOMAIN_GPCCLK, rounded_rate); - *freq = g->ops.clk.get_rate(g, CTRL_CLK_DOMAIN_GPCCLK); - } - - g->last_freq = *freq; - - /* postscale will only scale emc (dram clock) if evaluating - * gk20a_tegra_get_emc_rate() produces a new or different emc - * target because the load or_and gpufreq has changed */ - if (platform->postscale) - platform->postscale(dev, rounded_rate); - - return 0; -} - -/* - * update_load_estimate_gpmu(profile) - * - * Update load estimate using gpmu. The gpmu value is normalised - * based on the time it was asked last time. - */ - -static void update_load_estimate_gpmu(struct device *dev) -{ - struct gk20a *g = get_gk20a(dev); - struct gk20a_scale_profile *profile = g->scale_profile; - unsigned long dt; - u32 busy_time; - ktime_t t; - - t = ktime_get(); - dt = ktime_us_delta(t, profile->last_event_time); - - profile->dev_stat.total_time = dt; - profile->last_event_time = t; - nvgpu_pmu_load_norm(g, &busy_time); - profile->dev_stat.busy_time = (busy_time * dt) / 1000; -} - -/* - * gk20a_scale_suspend(dev) - * - * This function informs devfreq of suspend - */ - -void gk20a_scale_suspend(struct device *dev) -{ - struct gk20a *g = get_gk20a(dev); - struct nvgpu_os_linux *l = nvgpu_os_linux_from_gk20a(g); - struct devfreq *devfreq = l->devfreq; - - if (!devfreq) - return; - - devfreq_suspend_device(devfreq); -} - -/* - * gk20a_scale_resume(dev) - * - * This functions informs devfreq of resume - */ - -void gk20a_scale_resume(struct device *dev) -{ - struct gk20a *g = get_gk20a(dev); - struct nvgpu_os_linux *l = nvgpu_os_linux_from_gk20a(g); - struct devfreq *devfreq = l->devfreq; - - if (!devfreq) - return; - - g->last_freq = 0; - devfreq_resume_device(devfreq); -} - -/* - * gk20a_scale_get_dev_status(dev, *stat) - * - * This function queries the current device status. - */ - -static int gk20a_scale_get_dev_status(struct device *dev, - struct devfreq_dev_status *stat) -{ - struct gk20a *g = get_gk20a(dev); - struct gk20a_scale_profile *profile = g->scale_profile; - struct gk20a_platform *platform = dev_get_drvdata(dev); - - /* update the software shadow */ - nvgpu_pmu_load_update(g); - - /* inform edp about new constraint */ - if (platform->prescale) - platform->prescale(dev); - - /* Make sure there are correct values for the current frequency */ - profile->dev_stat.current_frequency = - g->ops.clk.get_rate(g, CTRL_CLK_DOMAIN_GPCCLK); - - /* Update load estimate */ - update_load_estimate_gpmu(dev); - - /* Copy the contents of the current device status */ - *stat = profile->dev_stat; - - /* Finally, clear out the local values */ - profile->dev_stat.total_time = 0; - profile->dev_stat.busy_time = 0; - - return 0; -} - -/* - * get_cur_freq(struct device *dev, unsigned long *freq) - * - * This function gets the current GPU clock rate. - */ - -static int get_cur_freq(struct device *dev, unsigned long *freq) -{ - struct gk20a *g = get_gk20a(dev); - *freq = g->ops.clk.get_rate(g, CTRL_CLK_DOMAIN_GPCCLK); - return 0; -} - - -/* - * gk20a_scale_init(dev) - */ - -void gk20a_scale_init(struct device *dev) -{ - struct gk20a_platform *platform = dev_get_drvdata(dev); - struct gk20a *g = platform->g; - struct nvgpu_os_linux *l = nvgpu_os_linux_from_gk20a(g); - struct gk20a_scale_profile *profile; - int err; - - if (g->scale_profile) - return; - - if (!platform->devfreq_governor && !platform->qos_notify) - return; - - profile = nvgpu_kzalloc(g, sizeof(*profile)); - - profile->dev = dev; - profile->dev_stat.busy = false; - - /* Create frequency table */ - err = gk20a_scale_make_freq_table(profile); - if (err || !profile->devfreq_profile.max_state) - goto err_get_freqs; - - profile->qos_min_freq = 0; - profile->qos_max_freq = UINT_MAX; - - /* Store device profile so we can access it if devfreq governor - * init needs that */ - g->scale_profile = profile; - - if (platform->devfreq_governor) { - struct devfreq *devfreq; - - profile->devfreq_profile.initial_freq = - profile->devfreq_profile.freq_table[0]; - profile->devfreq_profile.target = gk20a_scale_target; - profile->devfreq_profile.get_dev_status = - gk20a_scale_get_dev_status; - profile->devfreq_profile.get_cur_freq = get_cur_freq; - profile->devfreq_profile.polling_ms = 25; - - devfreq = devfreq_add_device(dev, - &profile->devfreq_profile, - platform->devfreq_governor, NULL); - - if (IS_ERR(devfreq)) - devfreq = NULL; - - l->devfreq = devfreq; - } - - /* Should we register QoS callback for this device? */ - if (platform->qos_notify) { - profile->qos_notify_block.notifier_call = - platform->qos_notify; - - pm_qos_add_min_notifier(PM_QOS_GPU_FREQ_BOUNDS, - &profile->qos_notify_block); - pm_qos_add_max_notifier(PM_QOS_GPU_FREQ_BOUNDS, - &profile->qos_notify_block); - } - - return; - -err_get_freqs: - nvgpu_kfree(g, profile); -} - -void gk20a_scale_exit(struct device *dev) -{ - struct gk20a_platform *platform = dev_get_drvdata(dev); - struct gk20a *g = platform->g; - struct nvgpu_os_linux *l = nvgpu_os_linux_from_gk20a(g); - int err; - - if (platform->qos_notify) { - pm_qos_remove_min_notifier(PM_QOS_GPU_FREQ_BOUNDS, - &g->scale_profile->qos_notify_block); - pm_qos_remove_max_notifier(PM_QOS_GPU_FREQ_BOUNDS, - &g->scale_profile->qos_notify_block); - } - - if (platform->devfreq_governor) { - err = devfreq_remove_device(l->devfreq); - l->devfreq = NULL; - } - - nvgpu_kfree(g, g->scale_profile); - g->scale_profile = NULL; -} - -/* - * gk20a_scale_hw_init(dev) - * - * Initialize hardware portion of the device - */ - -void gk20a_scale_hw_init(struct device *dev) -{ - struct gk20a_platform *platform = dev_get_drvdata(dev); - struct gk20a_scale_profile *profile = platform->g->scale_profile; - - /* make sure that scaling has bee initialised */ - if (!profile) - return; - - profile->dev_stat.total_time = 0; - profile->last_event_time = ktime_get(); -} diff --git a/drivers/gpu/nvgpu/gk20a/gk20a_scale.h b/drivers/gpu/nvgpu/gk20a/gk20a_scale.h deleted file mode 100644 index 05dc2e12..00000000 --- a/drivers/gpu/nvgpu/gk20a/gk20a_scale.h +++ /dev/null @@ -1,72 +0,0 @@ -/* - * gk20a clock scaling profile - * - * Copyright (c) 2013-2016, NVIDIA Corporation. All rights reserved. - * - * 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 AUTHORS OR COPYRIGHT HOLDERS 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. - */ - -#ifndef GK20A_SCALE_H -#define GK20A_SCALE_H - -#include - -struct clk; - -struct gk20a_scale_profile { - struct device *dev; - ktime_t last_event_time; - struct devfreq_dev_profile devfreq_profile; - struct devfreq_dev_status dev_stat; - struct notifier_block qos_notify_block; - unsigned long qos_min_freq; - unsigned long qos_max_freq; - void *private_data; -}; - -/* Initialization and de-initialization for module */ -void gk20a_scale_init(struct device *); -void gk20a_scale_exit(struct device *); -void gk20a_scale_hw_init(struct device *dev); - -#if defined(CONFIG_GK20A_DEVFREQ) -/* - * call when performing submit to notify scaling mechanism that the module is - * in use - */ -void gk20a_scale_notify_busy(struct device *); -void gk20a_scale_notify_idle(struct device *); - -void gk20a_scale_suspend(struct device *); -void gk20a_scale_resume(struct device *); -int gk20a_scale_qos_notify(struct notifier_block *nb, - unsigned long n, void *p); -#else -static inline void gk20a_scale_notify_busy(struct device *dev) {} -static inline void gk20a_scale_notify_idle(struct device *dev) {} -static inline void gk20a_scale_suspend(struct device *dev) {} -static inline void gk20a_scale_resume(struct device *dev) {} -static inline int gk20a_scale_qos_notify(struct notifier_block *nb, - unsigned long n, void *p) -{ - return -ENOSYS; -} -#endif - -#endif diff --git a/drivers/gpu/nvgpu/vgpu/vgpu.c b/drivers/gpu/nvgpu/vgpu/vgpu.c index 14a19f56..70214676 100644 --- a/drivers/gpu/nvgpu/vgpu/vgpu.c +++ b/drivers/gpu/nvgpu/vgpu/vgpu.c @@ -40,13 +40,13 @@ #include "vgpu/clk_vgpu.h" #include "gk20a/ctxsw_trace_gk20a.h" #include "gk20a/tsg_gk20a.h" -#include "gk20a/gk20a_scale.h" #include "gk20a/channel_gk20a.h" #include "gm20b/hal_gm20b.h" #include "common/linux/module.h" #include "common/linux/os_linux.h" #include "common/linux/ioctl.h" +#include "common/linux/scale.h" #ifdef CONFIG_TEGRA_19x_GPU #include -- cgit v1.2.2