jbd: use kmem_cache_zalloc for allocating journal head
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / nouveau / core / subdev / therm / temp.c
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 "priv.h"
26
27 #include <core/object.h>
28 #include <core/device.h>
29
30 #include <subdev/bios.h>
31
32 static void
33 nouveau_therm_temp_set_defaults(struct nouveau_therm *therm)
34 {
35         struct nouveau_therm_priv *priv = (void *)therm;
36
37         priv->bios_sensor.slope_mult = 1;
38         priv->bios_sensor.slope_div = 1;
39         priv->bios_sensor.offset_num = 0;
40         priv->bios_sensor.offset_den = 1;
41         priv->bios_sensor.offset_constant = 0;
42
43         priv->bios_sensor.thrs_fan_boost.temp = 90;
44         priv->bios_sensor.thrs_fan_boost.hysteresis = 3;
45
46         priv->bios_sensor.thrs_down_clock.temp = 95;
47         priv->bios_sensor.thrs_down_clock.hysteresis = 3;
48
49         priv->bios_sensor.thrs_critical.temp = 105;
50         priv->bios_sensor.thrs_critical.hysteresis = 5;
51
52         priv->bios_sensor.thrs_shutdown.temp = 135;
53         priv->bios_sensor.thrs_shutdown.hysteresis = 5; /*not that it matters */
54 }
55
56
57 static void
58 nouveau_therm_temp_safety_checks(struct nouveau_therm *therm)
59 {
60         struct nouveau_therm_priv *priv = (void *)therm;
61         struct nvbios_therm_sensor *s = &priv->bios_sensor;
62
63         if (!priv->bios_sensor.slope_div)
64                 priv->bios_sensor.slope_div = 1;
65         if (!priv->bios_sensor.offset_den)
66                 priv->bios_sensor.offset_den = 1;
67
68         /* enforce a minimum hysteresis on thresholds */
69         s->thrs_fan_boost.hysteresis = max_t(u8, s->thrs_fan_boost.hysteresis, 2);
70         s->thrs_down_clock.hysteresis = max_t(u8, s->thrs_down_clock.hysteresis, 2);
71         s->thrs_critical.hysteresis = max_t(u8, s->thrs_critical.hysteresis, 2);
72         s->thrs_shutdown.hysteresis = max_t(u8, s->thrs_shutdown.hysteresis, 2);
73 }
74
75 /* must be called with alarm_program_lock taken ! */
76 void nouveau_therm_sensor_set_threshold_state(struct nouveau_therm *therm,
77                                              enum nouveau_therm_thrs thrs,
78                                              enum nouveau_therm_thrs_state st)
79 {
80         struct nouveau_therm_priv *priv = (void *)therm;
81         priv->sensor.alarm_state[thrs] = st;
82 }
83
84 /* must be called with alarm_program_lock taken ! */
85 enum nouveau_therm_thrs_state
86 nouveau_therm_sensor_get_threshold_state(struct nouveau_therm *therm,
87                                          enum nouveau_therm_thrs thrs)
88 {
89         struct nouveau_therm_priv *priv = (void *)therm;
90         return priv->sensor.alarm_state[thrs];
91 }
92
93 static void
94 nv_poweroff_work(struct work_struct *work)
95 {
96         orderly_poweroff(true);
97         kfree(work);
98 }
99
100 void nouveau_therm_sensor_event(struct nouveau_therm *therm,
101                                 enum nouveau_therm_thrs thrs,
102                                 enum nouveau_therm_thrs_direction dir)
103 {
104         struct nouveau_therm_priv *priv = (void *)therm;
105         bool active;
106         const char *thresolds[] = {
107                 "fanboost", "downclock", "critical", "shutdown"
108         };
109         uint8_t temperature = therm->temp_get(therm);
110
111         if (thrs < 0 || thrs > 3)
112                 return;
113
114         if (dir == NOUVEAU_THERM_THRS_FALLING)
115                 nv_info(therm, "temperature (%u C) went below the '%s' threshold\n",
116                         temperature, thresolds[thrs]);
117         else
118                 nv_info(therm, "temperature (%u C) hit the '%s' threshold\n",
119                         temperature, thresolds[thrs]);
120
121         active = (dir == NOUVEAU_THERM_THRS_RISING);
122         switch (thrs) {
123         case NOUVEAU_THERM_THRS_FANBOOST:
124                 if (active) {
125                         nouveau_therm_fan_set(therm, true, 100);
126                         nouveau_therm_mode(therm, NOUVEAU_THERM_CTRL_AUTO);
127                 }
128                 break;
129         case NOUVEAU_THERM_THRS_DOWNCLOCK:
130                 if (priv->emergency.downclock)
131                         priv->emergency.downclock(therm, active);
132                 break;
133         case NOUVEAU_THERM_THRS_CRITICAL:
134                 if (priv->emergency.pause)
135                         priv->emergency.pause(therm, active);
136                 break;
137         case NOUVEAU_THERM_THRS_SHUTDOWN:
138                 if (active) {
139                         struct work_struct *work;
140
141                         work = kmalloc(sizeof(*work), GFP_ATOMIC);
142                         if (work) {
143                                 INIT_WORK(work, nv_poweroff_work);
144                                 schedule_work(work);
145                         }
146                 }
147                 break;
148         case NOUVEAU_THERM_THRS_NR:
149                 break;
150         }
151
152 }
153
154 /* must be called with alarm_program_lock taken ! */
155 static void
156 nouveau_therm_threshold_hyst_polling(struct nouveau_therm *therm,
157                                    const struct nvbios_therm_threshold *thrs,
158                                    enum nouveau_therm_thrs thrs_name)
159 {
160         enum nouveau_therm_thrs_direction direction;
161         enum nouveau_therm_thrs_state prev_state, new_state;
162         int temp = therm->temp_get(therm);
163
164         prev_state = nouveau_therm_sensor_get_threshold_state(therm, thrs_name);
165
166         if (temp >= thrs->temp && prev_state == NOUVEAU_THERM_THRS_LOWER) {
167                 direction = NOUVEAU_THERM_THRS_RISING;
168                 new_state = NOUVEAU_THERM_THRS_HIGHER;
169         } else if (temp <= thrs->temp - thrs->hysteresis &&
170                         prev_state == NOUVEAU_THERM_THRS_HIGHER) {
171                 direction = NOUVEAU_THERM_THRS_FALLING;
172                 new_state = NOUVEAU_THERM_THRS_LOWER;
173         } else
174                 return; /* nothing to do */
175
176         nouveau_therm_sensor_set_threshold_state(therm, thrs_name, new_state);
177         nouveau_therm_sensor_event(therm, thrs_name, direction);
178 }
179
180 static void
181 alarm_timer_callback(struct nouveau_alarm *alarm)
182 {
183         struct nouveau_therm_priv *priv =
184         container_of(alarm, struct nouveau_therm_priv, sensor.therm_poll_alarm);
185         struct nvbios_therm_sensor *sensor = &priv->bios_sensor;
186         struct nouveau_timer *ptimer = nouveau_timer(priv);
187         struct nouveau_therm *therm = &priv->base;
188         unsigned long flags;
189
190         spin_lock_irqsave(&priv->sensor.alarm_program_lock, flags);
191
192         nouveau_therm_threshold_hyst_polling(therm, &sensor->thrs_fan_boost,
193                                              NOUVEAU_THERM_THRS_FANBOOST);
194
195         nouveau_therm_threshold_hyst_polling(therm, &sensor->thrs_down_clock,
196                                              NOUVEAU_THERM_THRS_DOWNCLOCK);
197
198         nouveau_therm_threshold_hyst_polling(therm, &sensor->thrs_critical,
199                                              NOUVEAU_THERM_THRS_CRITICAL);
200
201         nouveau_therm_threshold_hyst_polling(therm, &sensor->thrs_shutdown,
202                                              NOUVEAU_THERM_THRS_SHUTDOWN);
203
204         /* schedule the next poll in one second */
205         if (list_empty(&alarm->head))
206                 ptimer->alarm(ptimer, 1000 * 1000 * 1000, alarm);
207
208         spin_unlock_irqrestore(&priv->sensor.alarm_program_lock, flags);
209 }
210
211 void
212 nouveau_therm_program_alarms_polling(struct nouveau_therm *therm)
213 {
214         struct nouveau_therm_priv *priv = (void *)therm;
215         struct nvbios_therm_sensor *sensor = &priv->bios_sensor;
216
217         nv_info(therm,
218                 "programmed thresholds [ %d(%d), %d(%d), %d(%d), %d(%d) ]\n",
219                 sensor->thrs_fan_boost.temp, sensor->thrs_fan_boost.hysteresis,
220                 sensor->thrs_down_clock.temp,
221                 sensor->thrs_down_clock.hysteresis,
222                 sensor->thrs_critical.temp, sensor->thrs_critical.hysteresis,
223                 sensor->thrs_shutdown.temp, sensor->thrs_shutdown.hysteresis);
224
225         alarm_timer_callback(&priv->sensor.therm_poll_alarm);
226 }
227
228 int
229 nouveau_therm_sensor_ctor(struct nouveau_therm *therm)
230 {
231         struct nouveau_therm_priv *priv = (void *)therm;
232         struct nouveau_bios *bios = nouveau_bios(therm);
233
234         nouveau_alarm_init(&priv->sensor.therm_poll_alarm, alarm_timer_callback);
235
236         nouveau_therm_temp_set_defaults(therm);
237         if (nvbios_therm_sensor_parse(bios, NVBIOS_THERM_DOMAIN_CORE,
238                                       &priv->bios_sensor))
239                 nv_error(therm, "nvbios_therm_sensor_parse failed\n");
240         nouveau_therm_temp_safety_checks(therm);
241
242         return 0;
243 }