linux/drivers/gpu/drm/nouveau/core/subdev/therm/base.c
<<
>>
Prefs
   1/*
   2 * Copyright 2012 The Nouveau community
   3 *
   4 * Permission is hereby granted, free of charge, to any person obtaining a
   5 * copy of this software and associated documentation files (the "Software"),
   6 * to deal in the Software without restriction, including without limitation
   7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
   8 * and/or sell copies of the Software, and to permit persons to whom the
   9 * Software is furnished to do so, subject to the following conditions:
  10 *
  11 * The above copyright notice and this permission notice shall be included in
  12 * all copies or substantial portions of the Software.
  13 *
  14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20 * OTHER DEALINGS IN THE SOFTWARE.
  21 *
  22 * Authors: Martin Peres
  23 */
  24
  25#include <core/object.h>
  26#include <core/device.h>
  27
  28#include <subdev/bios.h>
  29
  30#include "priv.h"
  31
  32static int
  33nouveau_therm_update_trip(struct nouveau_therm *therm)
  34{
  35        struct nouveau_therm_priv *priv = (void *)therm;
  36        struct nouveau_therm_trip_point *trip = priv->fan->bios.trip,
  37                                        *cur_trip = NULL,
  38                                        *last_trip = priv->last_trip;
  39        u8  temp = therm->temp_get(therm);
  40        u16 duty, i;
  41
  42        /* look for the trip point corresponding to the current temperature */
  43        cur_trip = NULL;
  44        for (i = 0; i < priv->fan->bios.nr_fan_trip; i++) {
  45                if (temp >= trip[i].temp)
  46                        cur_trip = &trip[i];
  47        }
  48
  49        /* account for the hysteresis cycle */
  50        if (last_trip && temp <= (last_trip->temp) &&
  51            temp > (last_trip->temp - last_trip->hysteresis))
  52                cur_trip = last_trip;
  53
  54        if (cur_trip) {
  55                duty = cur_trip->fan_duty;
  56                priv->last_trip = cur_trip;
  57        } else {
  58                duty = 0;
  59                priv->last_trip = NULL;
  60        }
  61
  62        return duty;
  63}
  64
  65static int
  66nouveau_therm_update_linear(struct nouveau_therm *therm)
  67{
  68        struct nouveau_therm_priv *priv = (void *)therm;
  69        u8  linear_min_temp = priv->fan->bios.linear_min_temp;
  70        u8  linear_max_temp = priv->fan->bios.linear_max_temp;
  71        u8  temp = therm->temp_get(therm);
  72        u16 duty;
  73
  74        /* handle the non-linear part first */
  75        if (temp < linear_min_temp)
  76                return priv->fan->bios.min_duty;
  77        else if (temp > linear_max_temp)
  78                return priv->fan->bios.max_duty;
  79
  80        /* we are in the linear zone */
  81        duty  = (temp - linear_min_temp);
  82        duty *= (priv->fan->bios.max_duty - priv->fan->bios.min_duty);
  83        duty /= (linear_max_temp - linear_min_temp);
  84        duty += priv->fan->bios.min_duty;
  85
  86        return duty;
  87}
  88
  89static void
  90nouveau_therm_update(struct nouveau_therm *therm, int mode)
  91{
  92        struct nouveau_timer *ptimer = nouveau_timer(therm);
  93        struct nouveau_therm_priv *priv = (void *)therm;
  94        unsigned long flags;
  95        bool immd = true;
  96        bool poll = true;
  97        int duty = -1;
  98
  99        spin_lock_irqsave(&priv->lock, flags);
 100        if (mode < 0)
 101                mode = priv->mode;
 102        priv->mode = mode;
 103
 104        switch (mode) {
 105        case NOUVEAU_THERM_CTRL_MANUAL:
 106                ptimer->alarm_cancel(ptimer, &priv->alarm);
 107                duty = nouveau_therm_fan_get(therm);
 108                if (duty < 0)
 109                        duty = 100;
 110                poll = false;
 111                break;
 112        case NOUVEAU_THERM_CTRL_AUTO:
 113                switch(priv->fan->bios.fan_mode) {
 114                case NVBIOS_THERM_FAN_TRIP:
 115                        duty = nouveau_therm_update_trip(therm);
 116                        break;
 117                case NVBIOS_THERM_FAN_LINEAR:
 118                        duty = nouveau_therm_update_linear(therm);
 119                        break;
 120                case NVBIOS_THERM_FAN_OTHER:
 121                        if (priv->cstate)
 122                                duty = priv->cstate;
 123                        poll = false;
 124                        break;
 125                }
 126                immd = false;
 127                break;
 128        case NOUVEAU_THERM_CTRL_NONE:
 129        default:
 130                ptimer->alarm_cancel(ptimer, &priv->alarm);
 131                poll = false;
 132        }
 133
 134        if (list_empty(&priv->alarm.head) && poll)
 135                ptimer->alarm(ptimer, 1000000000ULL, &priv->alarm);
 136        spin_unlock_irqrestore(&priv->lock, flags);
 137
 138        if (duty >= 0) {
 139                nv_debug(therm, "FAN target request: %d%%\n", duty);
 140                nouveau_therm_fan_set(therm, immd, duty);
 141        }
 142}
 143
 144int
 145nouveau_therm_cstate(struct nouveau_therm *ptherm, int fan, int dir)
 146{
 147        struct nouveau_therm_priv *priv = (void *)ptherm;
 148        if (!dir || (dir < 0 && fan < priv->cstate) ||
 149                    (dir > 0 && fan > priv->cstate)) {
 150                nv_debug(ptherm, "default fan speed -> %d%%\n", fan);
 151                priv->cstate = fan;
 152                nouveau_therm_update(ptherm, -1);
 153        }
 154        return 0;
 155}
 156
 157static void
 158nouveau_therm_alarm(struct nouveau_alarm *alarm)
 159{
 160        struct nouveau_therm_priv *priv =
 161               container_of(alarm, struct nouveau_therm_priv, alarm);
 162        nouveau_therm_update(&priv->base, -1);
 163}
 164
 165int
 166nouveau_therm_fan_mode(struct nouveau_therm *therm, int mode)
 167{
 168        struct nouveau_therm_priv *priv = (void *)therm;
 169        struct nouveau_device *device = nv_device(therm);
 170        static const char *name[] = {
 171                "disabled",
 172                "manual",
 173                "automatic"
 174        };
 175
 176        /* The default PPWR ucode on fermi interferes with fan management */
 177        if ((mode >= ARRAY_SIZE(name)) ||
 178            (mode != NOUVEAU_THERM_CTRL_NONE && device->card_type >= NV_C0 &&
 179             !nouveau_subdev(device, NVDEV_SUBDEV_PWR)))
 180                return -EINVAL;
 181
 182        /* do not allow automatic fan management if the thermal sensor is
 183         * not available */
 184        if (mode == NOUVEAU_THERM_CTRL_AUTO && therm->temp_get(therm) < 0)
 185                return -EINVAL;
 186
 187        if (priv->mode == mode)
 188                return 0;
 189
 190        nv_info(therm, "fan management: %s\n", name[mode]);
 191        nouveau_therm_update(therm, mode);
 192        return 0;
 193}
 194
 195int
 196nouveau_therm_attr_get(struct nouveau_therm *therm,
 197                       enum nouveau_therm_attr_type type)
 198{
 199        struct nouveau_therm_priv *priv = (void *)therm;
 200
 201        switch (type) {
 202        case NOUVEAU_THERM_ATTR_FAN_MIN_DUTY:
 203                return priv->fan->bios.min_duty;
 204        case NOUVEAU_THERM_ATTR_FAN_MAX_DUTY:
 205                return priv->fan->bios.max_duty;
 206        case NOUVEAU_THERM_ATTR_FAN_MODE:
 207                return priv->mode;
 208        case NOUVEAU_THERM_ATTR_THRS_FAN_BOOST:
 209                return priv->bios_sensor.thrs_fan_boost.temp;
 210        case NOUVEAU_THERM_ATTR_THRS_FAN_BOOST_HYST:
 211                return priv->bios_sensor.thrs_fan_boost.hysteresis;
 212        case NOUVEAU_THERM_ATTR_THRS_DOWN_CLK:
 213                return priv->bios_sensor.thrs_down_clock.temp;
 214        case NOUVEAU_THERM_ATTR_THRS_DOWN_CLK_HYST:
 215                return priv->bios_sensor.thrs_down_clock.hysteresis;
 216        case NOUVEAU_THERM_ATTR_THRS_CRITICAL:
 217                return priv->bios_sensor.thrs_critical.temp;
 218        case NOUVEAU_THERM_ATTR_THRS_CRITICAL_HYST:
 219                return priv->bios_sensor.thrs_critical.hysteresis;
 220        case NOUVEAU_THERM_ATTR_THRS_SHUTDOWN:
 221                return priv->bios_sensor.thrs_shutdown.temp;
 222        case NOUVEAU_THERM_ATTR_THRS_SHUTDOWN_HYST:
 223                return priv->bios_sensor.thrs_shutdown.hysteresis;
 224        }
 225
 226        return -EINVAL;
 227}
 228
 229int
 230nouveau_therm_attr_set(struct nouveau_therm *therm,
 231                       enum nouveau_therm_attr_type type, int value)
 232{
 233        struct nouveau_therm_priv *priv = (void *)therm;
 234
 235        switch (type) {
 236        case NOUVEAU_THERM_ATTR_FAN_MIN_DUTY:
 237                if (value < 0)
 238                        value = 0;
 239                if (value > priv->fan->bios.max_duty)
 240                        value = priv->fan->bios.max_duty;
 241                priv->fan->bios.min_duty = value;
 242                return 0;
 243        case NOUVEAU_THERM_ATTR_FAN_MAX_DUTY:
 244                if (value < 0)
 245                        value = 0;
 246                if (value < priv->fan->bios.min_duty)
 247                        value = priv->fan->bios.min_duty;
 248                priv->fan->bios.max_duty = value;
 249                return 0;
 250        case NOUVEAU_THERM_ATTR_FAN_MODE:
 251                return nouveau_therm_fan_mode(therm, value);
 252        case NOUVEAU_THERM_ATTR_THRS_FAN_BOOST:
 253                priv->bios_sensor.thrs_fan_boost.temp = value;
 254                priv->sensor.program_alarms(therm);
 255                return 0;
 256        case NOUVEAU_THERM_ATTR_THRS_FAN_BOOST_HYST:
 257                priv->bios_sensor.thrs_fan_boost.hysteresis = value;
 258                priv->sensor.program_alarms(therm);
 259                return 0;
 260        case NOUVEAU_THERM_ATTR_THRS_DOWN_CLK:
 261                priv->bios_sensor.thrs_down_clock.temp = value;
 262                priv->sensor.program_alarms(therm);
 263                return 0;
 264        case NOUVEAU_THERM_ATTR_THRS_DOWN_CLK_HYST:
 265                priv->bios_sensor.thrs_down_clock.hysteresis = value;
 266                priv->sensor.program_alarms(therm);
 267                return 0;
 268        case NOUVEAU_THERM_ATTR_THRS_CRITICAL:
 269                priv->bios_sensor.thrs_critical.temp = value;
 270                priv->sensor.program_alarms(therm);
 271                return 0;
 272        case NOUVEAU_THERM_ATTR_THRS_CRITICAL_HYST:
 273                priv->bios_sensor.thrs_critical.hysteresis = value;
 274                priv->sensor.program_alarms(therm);
 275                return 0;
 276        case NOUVEAU_THERM_ATTR_THRS_SHUTDOWN:
 277                priv->bios_sensor.thrs_shutdown.temp = value;
 278                priv->sensor.program_alarms(therm);
 279                return 0;
 280        case NOUVEAU_THERM_ATTR_THRS_SHUTDOWN_HYST:
 281                priv->bios_sensor.thrs_shutdown.hysteresis = value;
 282                priv->sensor.program_alarms(therm);
 283                return 0;
 284        }
 285
 286        return -EINVAL;
 287}
 288
 289int
 290_nouveau_therm_init(struct nouveau_object *object)
 291{
 292        struct nouveau_therm *therm = (void *)object;
 293        struct nouveau_therm_priv *priv = (void *)therm;
 294        int ret;
 295
 296        ret = nouveau_subdev_init(&therm->base);
 297        if (ret)
 298                return ret;
 299
 300        if (priv->suspend >= 0) {
 301                /* restore the pwm value only when on manual or auto mode */
 302                if (priv->suspend > 0)
 303                        nouveau_therm_fan_set(therm, true, priv->fan->percent);
 304
 305                nouveau_therm_fan_mode(therm, priv->suspend);
 306        }
 307        nouveau_therm_sensor_init(therm);
 308        nouveau_therm_fan_init(therm);
 309        return 0;
 310}
 311
 312int
 313_nouveau_therm_fini(struct nouveau_object *object, bool suspend)
 314{
 315        struct nouveau_therm *therm = (void *)object;
 316        struct nouveau_therm_priv *priv = (void *)therm;
 317
 318        nouveau_therm_fan_fini(therm, suspend);
 319        nouveau_therm_sensor_fini(therm, suspend);
 320        if (suspend) {
 321                priv->suspend = priv->mode;
 322                priv->mode = NOUVEAU_THERM_CTRL_NONE;
 323        }
 324
 325        return nouveau_subdev_fini(&therm->base, suspend);
 326}
 327
 328int
 329nouveau_therm_create_(struct nouveau_object *parent,
 330                      struct nouveau_object *engine,
 331                      struct nouveau_oclass *oclass,
 332                      int length, void **pobject)
 333{
 334        struct nouveau_therm_priv *priv;
 335        int ret;
 336
 337        ret = nouveau_subdev_create_(parent, engine, oclass, 0, "PTHERM",
 338                                     "therm", length, pobject);
 339        priv = *pobject;
 340        if (ret)
 341                return ret;
 342
 343        nouveau_alarm_init(&priv->alarm, nouveau_therm_alarm);
 344        spin_lock_init(&priv->lock);
 345        spin_lock_init(&priv->sensor.alarm_program_lock);
 346
 347        priv->base.fan_get = nouveau_therm_fan_user_get;
 348        priv->base.fan_set = nouveau_therm_fan_user_set;
 349        priv->base.fan_sense = nouveau_therm_fan_sense;
 350        priv->base.attr_get = nouveau_therm_attr_get;
 351        priv->base.attr_set = nouveau_therm_attr_set;
 352        priv->mode = priv->suspend = -1; /* undefined */
 353        return 0;
 354}
 355
 356int
 357nouveau_therm_preinit(struct nouveau_therm *therm)
 358{
 359        nouveau_therm_sensor_ctor(therm);
 360        nouveau_therm_ic_ctor(therm);
 361        nouveau_therm_fan_ctor(therm);
 362
 363        nouveau_therm_fan_mode(therm, NOUVEAU_THERM_CTRL_AUTO);
 364        nouveau_therm_sensor_preinit(therm);
 365        return 0;
 366}
 367
 368void
 369_nouveau_therm_dtor(struct nouveau_object *object)
 370{
 371        struct nouveau_therm_priv *priv = (void *)object;
 372        kfree(priv->fan);
 373        nouveau_subdev_destroy(&priv->base.base);
 374}
 375