1/* $NetBSD: nouveau_engine_device_ctrl.c,v 1.1.1.1 2014/08/06 12:36:24 riastradh Exp $ */
2
3/*
4 * Copyright 2013 Red Hat Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Ben Skeggs <bskeggs@redhat.com>
25 */
26
27#include <sys/cdefs.h>
28__KERNEL_RCSID(0, "$NetBSD: nouveau_engine_device_ctrl.c,v 1.1.1.1 2014/08/06 12:36:24 riastradh Exp $");
29
30#include <core/object.h>
31#include <core/class.h>
32
33#include <subdev/clock.h>
34
35#include "priv.h"
36
37static int
38nouveau_control_mthd_pstate_info(struct nouveau_object *object, u32 mthd,
39 void *data, u32 size)
40{
41 struct nouveau_clock *clk = nouveau_clock(object);
42 struct nv_control_pstate_info *args = data;
43
44 if (size < sizeof(*args))
45 return -EINVAL;
46
47 if (clk) {
48 args->count = clk->state_nr;
49 args->ustate = clk->ustate;
50 args->pstate = clk->pstate;
51 } else {
52 args->count = 0;
53 args->ustate = NV_CONTROL_PSTATE_INFO_USTATE_DISABLE;
54 args->pstate = NV_CONTROL_PSTATE_INFO_PSTATE_UNKNOWN;
55 }
56
57 return 0;
58}
59
60static int
61nouveau_control_mthd_pstate_attr(struct nouveau_object *object, u32 mthd,
62 void *data, u32 size)
63{
64 struct nouveau_clock *clk = nouveau_clock(object);
65 struct nv_control_pstate_attr *args = data;
66 struct nouveau_clocks *domain;
67 struct nouveau_pstate *pstate;
68 struct nouveau_cstate *cstate;
69 int i = 0, j = -1;
70 u32 lo, hi;
71
72 if ((size < sizeof(*args)) || !clk ||
73 (args->state >= 0 && args->state >= clk->state_nr))
74 return -EINVAL;
75 domain = clk->domains;
76
77 while (domain->name != nv_clk_src_max) {
78 if (domain->mname && ++j == args->index)
79 break;
80 domain++;
81 }
82
83 if (domain->name == nv_clk_src_max)
84 return -EINVAL;
85
86 if (args->state != NV_CONTROL_PSTATE_ATTR_STATE_CURRENT) {
87 list_for_each_entry(pstate, &clk->states, head) {
88 if (i++ == args->state)
89 break;
90 }
91
92 lo = pstate->base.domain[domain->name];
93 hi = lo;
94 list_for_each_entry(cstate, &pstate->list, head) {
95 lo = min(lo, cstate->domain[domain->name]);
96 hi = max(hi, cstate->domain[domain->name]);
97 }
98
99 args->state = pstate->pstate;
100 } else {
101 lo = max(clk->read(clk, domain->name), 0);
102 hi = lo;
103 }
104
105 snprintf(args->name, sizeof(args->name), "%s", domain->mname);
106 snprintf(args->unit, sizeof(args->unit), "MHz");
107 args->min = lo / domain->mdiv;
108 args->max = hi / domain->mdiv;
109
110 args->index = 0;
111 while ((++domain)->name != nv_clk_src_max) {
112 if (domain->mname) {
113 args->index = ++j;
114 break;
115 }
116 }
117
118 return 0;
119}
120
121static int
122nouveau_control_mthd_pstate_user(struct nouveau_object *object, u32 mthd,
123 void *data, u32 size)
124{
125 struct nouveau_clock *clk = nouveau_clock(object);
126 struct nv_control_pstate_user *args = data;
127
128 if (size < sizeof(*args) || !clk)
129 return -EINVAL;
130
131 return nouveau_clock_ustate(clk, args->state);
132}
133
134struct nouveau_oclass
135nouveau_control_oclass[] = {
136 { .handle = NV_CONTROL_CLASS,
137 .ofuncs = &nouveau_object_ofuncs,
138 .omthds = (struct nouveau_omthds[]) {
139 { NV_CONTROL_PSTATE_INFO,
140 NV_CONTROL_PSTATE_INFO, nouveau_control_mthd_pstate_info },
141 { NV_CONTROL_PSTATE_ATTR,
142 NV_CONTROL_PSTATE_ATTR, nouveau_control_mthd_pstate_attr },
143 { NV_CONTROL_PSTATE_USER,
144 NV_CONTROL_PSTATE_USER, nouveau_control_mthd_pstate_user },
145 {},
146 },
147 },
148 {}
149};
150