Merge remote-tracking branch 'iwlwifi-fixes/master' into NEXT
[sfrench/cifs-2.6.git] / drivers / gpu / drm / nouveau / core / engine / device / ctrl.c
1 /*
2  * Copyright 2013 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 <bskeggs@redhat.com>
23  */
24
25 #include <core/client.h>
26 #include <core/object.h>
27 #include <nvif/unpack.h>
28 #include <nvif/class.h>
29 #include <nvif/ioctl.h>
30
31 #include <subdev/clock.h>
32
33 #include "priv.h"
34
35 static int
36 nouveau_control_mthd_pstate_info(struct nouveau_object *object,
37                                  void *data, u32 size)
38 {
39         union {
40                 struct nvif_control_pstate_info_v0 v0;
41         } *args = data;
42         struct nouveau_clock *clk = nouveau_clock(object);
43         int ret;
44
45         nv_ioctl(object, "control pstate info size %d\n", size);
46         if (nvif_unpack(args->v0, 0, 0, false)) {
47                 nv_ioctl(object, "control pstate info vers %d\n",
48                          args->v0.version);
49         } else
50                 return ret;
51
52         if (clk) {
53                 args->v0.count = clk->state_nr;
54                 args->v0.ustate_ac = clk->ustate_ac;
55                 args->v0.ustate_dc = clk->ustate_dc;
56                 args->v0.pwrsrc = clk->pwrsrc;
57                 args->v0.pstate = clk->pstate;
58         } else {
59                 args->v0.count = 0;
60                 args->v0.ustate_ac = NVIF_CONTROL_PSTATE_INFO_V0_USTATE_DISABLE;
61                 args->v0.ustate_dc = NVIF_CONTROL_PSTATE_INFO_V0_USTATE_DISABLE;
62                 args->v0.pwrsrc = -ENOSYS;
63                 args->v0.pstate = NVIF_CONTROL_PSTATE_INFO_V0_PSTATE_UNKNOWN;
64         }
65
66         return 0;
67 }
68
69 static int
70 nouveau_control_mthd_pstate_attr(struct nouveau_object *object,
71                                  void *data, u32 size)
72 {
73         union {
74                 struct nvif_control_pstate_attr_v0 v0;
75         } *args = data;
76         struct nouveau_clock *clk = nouveau_clock(object);
77         struct nouveau_clocks *domain;
78         struct nouveau_pstate *pstate;
79         struct nouveau_cstate *cstate;
80         int i = 0, j = -1;
81         u32 lo, hi;
82         int ret;
83
84         nv_ioctl(object, "control pstate attr size %d\n", size);
85         if (nvif_unpack(args->v0, 0, 0, false)) {
86                 nv_ioctl(object, "control pstate attr vers %d state %d "
87                                  "index %d\n",
88                          args->v0.version, args->v0.state, args->v0.index);
89                 if (!clk)
90                         return -ENODEV;
91                 if (args->v0.state < NVIF_CONTROL_PSTATE_ATTR_V0_STATE_CURRENT)
92                         return -EINVAL;
93                 if (args->v0.state >= clk->state_nr)
94                         return -EINVAL;
95         } else
96                 return ret;
97         domain = clk->domains;
98
99         while (domain->name != nv_clk_src_max) {
100                 if (domain->mname && ++j == args->v0.index)
101                         break;
102                 domain++;
103         }
104
105         if (domain->name == nv_clk_src_max)
106                 return -EINVAL;
107
108         if (args->v0.state != NVIF_CONTROL_PSTATE_ATTR_V0_STATE_CURRENT) {
109                 list_for_each_entry(pstate, &clk->states, head) {
110                         if (i++ == args->v0.state)
111                                 break;
112                 }
113
114                 lo = pstate->base.domain[domain->name];
115                 hi = lo;
116                 list_for_each_entry(cstate, &pstate->list, head) {
117                         lo = min(lo, cstate->domain[domain->name]);
118                         hi = max(hi, cstate->domain[domain->name]);
119                 }
120
121                 args->v0.state = pstate->pstate;
122         } else {
123                 lo = max(clk->read(clk, domain->name), 0);
124                 hi = lo;
125         }
126
127         snprintf(args->v0.name, sizeof(args->v0.name), "%s", domain->mname);
128         snprintf(args->v0.unit, sizeof(args->v0.unit), "MHz");
129         args->v0.min = lo / domain->mdiv;
130         args->v0.max = hi / domain->mdiv;
131
132         args->v0.index = 0;
133         while ((++domain)->name != nv_clk_src_max) {
134                 if (domain->mname) {
135                         args->v0.index = ++j;
136                         break;
137                 }
138         }
139
140         return 0;
141 }
142
143 static int
144 nouveau_control_mthd_pstate_user(struct nouveau_object *object,
145                                  void *data, u32 size)
146 {
147         union {
148                 struct nvif_control_pstate_user_v0 v0;
149         } *args = data;
150         struct nouveau_clock *clk = nouveau_clock(object);
151         int ret;
152
153         nv_ioctl(object, "control pstate user size %d\n", size);
154         if (nvif_unpack(args->v0, 0, 0, false)) {
155                 nv_ioctl(object, "control pstate user vers %d ustate %d "
156                                  "pwrsrc %d\n", args->v0.version,
157                          args->v0.ustate, args->v0.pwrsrc);
158                 if (!clk)
159                         return -ENODEV;
160         } else
161                 return ret;
162
163         if (args->v0.pwrsrc >= 0) {
164                 ret |= nouveau_clock_ustate(clk, args->v0.ustate, args->v0.pwrsrc);
165         } else {
166                 ret |= nouveau_clock_ustate(clk, args->v0.ustate, 0);
167                 ret |= nouveau_clock_ustate(clk, args->v0.ustate, 1);
168         }
169
170         return ret;
171 }
172
173 static int
174 nouveau_control_mthd(struct nouveau_object *object, u32 mthd,
175                      void *data, u32 size)
176 {
177         switch (mthd) {
178         case NVIF_CONTROL_PSTATE_INFO:
179                 return nouveau_control_mthd_pstate_info(object, data, size);
180         case NVIF_CONTROL_PSTATE_ATTR:
181                 return nouveau_control_mthd_pstate_attr(object, data, size);
182         case NVIF_CONTROL_PSTATE_USER:
183                 return nouveau_control_mthd_pstate_user(object, data, size);
184         default:
185                 break;
186         }
187         return -EINVAL;
188 }
189
190 static struct nouveau_ofuncs
191 nouveau_control_ofuncs = {
192         .ctor = _nouveau_object_ctor,
193         .dtor = nouveau_object_destroy,
194         .init = nouveau_object_init,
195         .fini = nouveau_object_fini,
196         .mthd = nouveau_control_mthd,
197 };
198
199 struct nouveau_oclass
200 nouveau_control_oclass[] = {
201         { .handle = NVIF_IOCTL_NEW_V0_CONTROL,
202           .ofuncs = &nouveau_control_ofuncs
203         },
204         {}
205 };