Linux-libre 3.18.98-gnu
[librecmc/linux-libre.git] / drivers / gpu / drm / nouveau / core / subdev / therm / nv84.c
1 /*
2  * Copyright 2012 Red Hat Inc.
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: Ben Skeggs
23  *          Martin Peres
24  */
25
26 #include "priv.h"
27 #include <subdev/fuse.h>
28
29 struct nv84_therm_priv {
30         struct nouveau_therm_priv base;
31 };
32
33 int
34 nv84_temp_get(struct nouveau_therm *therm)
35 {
36         struct nouveau_fuse *fuse = nouveau_fuse(therm);
37
38         if (nv_ro32(fuse, 0x1a8) == 1)
39                 return nv_rd32(therm, 0x20400);
40         else
41                 return -ENODEV;
42 }
43
44 void
45 nv84_sensor_setup(struct nouveau_therm *therm)
46 {
47         struct nouveau_fuse *fuse = nouveau_fuse(therm);
48
49         /* enable temperature reading for cards with insane defaults */
50         if (nv_ro32(fuse, 0x1a8) == 1) {
51                 nv_mask(therm, 0x20008, 0x80008000, 0x80000000);
52                 nv_mask(therm, 0x2000c, 0x80000003, 0x00000000);
53                 mdelay(20); /* wait for the temperature to stabilize */
54         }
55 }
56
57 static void
58 nv84_therm_program_alarms(struct nouveau_therm *therm)
59 {
60         struct nouveau_therm_priv *priv = (void *)therm;
61         struct nvbios_therm_sensor *sensor = &priv->bios_sensor;
62         unsigned long flags;
63
64         spin_lock_irqsave(&priv->sensor.alarm_program_lock, flags);
65
66         /* enable RISING and FALLING IRQs for shutdown, THRS 0, 1, 2 and 4 */
67         nv_wr32(therm, 0x20000, 0x000003ff);
68
69         /* shutdown: The computer should be shutdown when reached */
70         nv_wr32(therm, 0x20484, sensor->thrs_shutdown.hysteresis);
71         nv_wr32(therm, 0x20480, sensor->thrs_shutdown.temp);
72
73         /* THRS_1 : fan boost*/
74         nv_wr32(therm, 0x204c4, sensor->thrs_fan_boost.temp);
75
76         /* THRS_2 : critical */
77         nv_wr32(therm, 0x204c0, sensor->thrs_critical.temp);
78
79         /* THRS_4 : down clock */
80         nv_wr32(therm, 0x20414, sensor->thrs_down_clock.temp);
81         spin_unlock_irqrestore(&priv->sensor.alarm_program_lock, flags);
82
83         nv_debug(therm,
84                  "Programmed thresholds [ %d(%d), %d(%d), %d(%d), %d(%d) ]\n",
85                  sensor->thrs_fan_boost.temp, sensor->thrs_fan_boost.hysteresis,
86                  sensor->thrs_down_clock.temp,
87                  sensor->thrs_down_clock.hysteresis,
88                  sensor->thrs_critical.temp, sensor->thrs_critical.hysteresis,
89                  sensor->thrs_shutdown.temp, sensor->thrs_shutdown.hysteresis);
90
91 }
92
93 /* must be called with alarm_program_lock taken ! */
94 static void
95 nv84_therm_threshold_hyst_emulation(struct nouveau_therm *therm,
96                                    uint32_t thrs_reg, u8 status_bit,
97                                    const struct nvbios_therm_threshold *thrs,
98                                    enum nouveau_therm_thrs thrs_name)
99 {
100         enum nouveau_therm_thrs_direction direction;
101         enum nouveau_therm_thrs_state prev_state, new_state;
102         int temp, cur;
103
104         prev_state = nouveau_therm_sensor_get_threshold_state(therm, thrs_name);
105         temp = nv_rd32(therm, thrs_reg);
106
107         /* program the next threshold */
108         if (temp == thrs->temp) {
109                 nv_wr32(therm, thrs_reg, thrs->temp - thrs->hysteresis);
110                 new_state = NOUVEAU_THERM_THRS_HIGHER;
111         } else {
112                 nv_wr32(therm, thrs_reg, thrs->temp);
113                 new_state = NOUVEAU_THERM_THRS_LOWER;
114         }
115
116         /* fix the state (in case someone reprogrammed the alarms) */
117         cur = therm->temp_get(therm);
118         if (new_state == NOUVEAU_THERM_THRS_LOWER && cur > thrs->temp)
119                 new_state = NOUVEAU_THERM_THRS_HIGHER;
120         else if (new_state == NOUVEAU_THERM_THRS_HIGHER &&
121                 cur < thrs->temp - thrs->hysteresis)
122                 new_state = NOUVEAU_THERM_THRS_LOWER;
123         nouveau_therm_sensor_set_threshold_state(therm, thrs_name, new_state);
124
125         /* find the direction */
126         if (prev_state < new_state)
127                 direction = NOUVEAU_THERM_THRS_RISING;
128         else if (prev_state > new_state)
129                 direction = NOUVEAU_THERM_THRS_FALLING;
130         else
131                 return;
132
133         /* advertise a change in direction */
134         nouveau_therm_sensor_event(therm, thrs_name, direction);
135 }
136
137 static void
138 nv84_therm_intr(struct nouveau_subdev *subdev)
139 {
140         struct nouveau_therm *therm = nouveau_therm(subdev);
141         struct nouveau_therm_priv *priv = (void *)therm;
142         struct nvbios_therm_sensor *sensor = &priv->bios_sensor;
143         unsigned long flags;
144         uint32_t intr;
145
146         spin_lock_irqsave(&priv->sensor.alarm_program_lock, flags);
147
148         intr = nv_rd32(therm, 0x20100) & 0x3ff;
149
150         /* THRS_4: downclock */
151         if (intr & 0x002) {
152                 nv84_therm_threshold_hyst_emulation(therm, 0x20414, 24,
153                                                   &sensor->thrs_down_clock,
154                                                   NOUVEAU_THERM_THRS_DOWNCLOCK);
155                 intr &= ~0x002;
156         }
157
158         /* shutdown */
159         if (intr & 0x004) {
160                 nv84_therm_threshold_hyst_emulation(therm, 0x20480, 20,
161                                                    &sensor->thrs_shutdown,
162                                                    NOUVEAU_THERM_THRS_SHUTDOWN);
163                 intr &= ~0x004;
164         }
165
166         /* THRS_1 : fan boost */
167         if (intr & 0x008) {
168                 nv84_therm_threshold_hyst_emulation(therm, 0x204c4, 21,
169                                                    &sensor->thrs_fan_boost,
170                                                    NOUVEAU_THERM_THRS_FANBOOST);
171                 intr &= ~0x008;
172         }
173
174         /* THRS_2 : critical */
175         if (intr & 0x010) {
176                 nv84_therm_threshold_hyst_emulation(therm, 0x204c0, 22,
177                                                    &sensor->thrs_critical,
178                                                    NOUVEAU_THERM_THRS_CRITICAL);
179                 intr &= ~0x010;
180         }
181
182         if (intr)
183                 nv_error(therm, "unhandled intr 0x%08x\n", intr);
184
185         /* ACK everything */
186         nv_wr32(therm, 0x20100, 0xffffffff);
187         nv_wr32(therm, 0x1100, 0x10000); /* PBUS */
188
189         spin_unlock_irqrestore(&priv->sensor.alarm_program_lock, flags);
190 }
191
192 static int
193 nv84_therm_init(struct nouveau_object *object)
194 {
195         struct nv84_therm_priv *priv = (void *)object;
196         int ret;
197
198         ret = nouveau_therm_init(&priv->base.base);
199         if (ret)
200                 return ret;
201
202         nv84_sensor_setup(&priv->base.base);
203
204         return 0;
205 }
206
207 static int
208 nv84_therm_ctor(struct nouveau_object *parent,
209                 struct nouveau_object *engine,
210                 struct nouveau_oclass *oclass, void *data, u32 size,
211                 struct nouveau_object **pobject)
212 {
213         struct nv84_therm_priv *priv;
214         int ret;
215
216         ret = nouveau_therm_create(parent, engine, oclass, &priv);
217         *pobject = nv_object(priv);
218         if (ret)
219                 return ret;
220
221         priv->base.base.pwm_ctrl = nv50_fan_pwm_ctrl;
222         priv->base.base.pwm_get = nv50_fan_pwm_get;
223         priv->base.base.pwm_set = nv50_fan_pwm_set;
224         priv->base.base.pwm_clock = nv50_fan_pwm_clock;
225         priv->base.base.temp_get = nv84_temp_get;
226         priv->base.sensor.program_alarms = nv84_therm_program_alarms;
227         nv_subdev(priv)->intr = nv84_therm_intr;
228
229         /* init the thresholds */
230         nouveau_therm_sensor_set_threshold_state(&priv->base.base,
231                                                  NOUVEAU_THERM_THRS_SHUTDOWN,
232                                                  NOUVEAU_THERM_THRS_LOWER);
233         nouveau_therm_sensor_set_threshold_state(&priv->base.base,
234                                                  NOUVEAU_THERM_THRS_FANBOOST,
235                                                  NOUVEAU_THERM_THRS_LOWER);
236         nouveau_therm_sensor_set_threshold_state(&priv->base.base,
237                                                  NOUVEAU_THERM_THRS_CRITICAL,
238                                                  NOUVEAU_THERM_THRS_LOWER);
239         nouveau_therm_sensor_set_threshold_state(&priv->base.base,
240                                                  NOUVEAU_THERM_THRS_DOWNCLOCK,
241                                                  NOUVEAU_THERM_THRS_LOWER);
242
243         return nouveau_therm_preinit(&priv->base.base);
244 }
245
246 int
247 nv84_therm_fini(struct nouveau_object *object, bool suspend)
248 {
249         /* Disable PTherm IRQs */
250         nv_wr32(object, 0x20000, 0x00000000);
251
252         /* ACK all PTherm IRQs */
253         nv_wr32(object, 0x20100, 0xffffffff);
254         nv_wr32(object, 0x1100, 0x10000); /* PBUS */
255
256         return _nouveau_therm_fini(object, suspend);
257 }
258
259 struct nouveau_oclass
260 nv84_therm_oclass = {
261         .handle = NV_SUBDEV(THERM, 0x84),
262         .ofuncs = &(struct nouveau_ofuncs) {
263                 .ctor = nv84_therm_ctor,
264                 .dtor = _nouveau_therm_dtor,
265                 .init = nv84_therm_init,
266                 .fini = nv84_therm_fini,
267         },
268 };