1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "priv.h"
25
26#include <core/option.h>
27#include <subdev/top.h>
28
29void
30nvkm_mc_unk260(struct nvkm_device *device, u32 data)
31{
32 struct nvkm_mc *mc = device->mc;
33 if (likely(mc) && mc->func->unk260)
34 mc->func->unk260(mc, data);
35}
36
37void
38nvkm_mc_intr_mask(struct nvkm_device *device, enum nvkm_devidx devidx, bool en)
39{
40 struct nvkm_mc *mc = device->mc;
41 const struct nvkm_mc_map *map;
42 if (likely(mc) && mc->func->intr_mask) {
43 u32 mask = nvkm_top_intr_mask(device, devidx);
44 for (map = mc->func->intr; !mask && map->stat; map++) {
45 if (map->unit == devidx)
46 mask = map->stat;
47 }
48 mc->func->intr_mask(mc, mask, en ? mask : 0);
49 }
50}
51
52void
53nvkm_mc_intr_unarm(struct nvkm_device *device)
54{
55 struct nvkm_mc *mc = device->mc;
56 if (likely(mc))
57 mc->func->intr_unarm(mc);
58}
59
60void
61nvkm_mc_intr_rearm(struct nvkm_device *device)
62{
63 struct nvkm_mc *mc = device->mc;
64 if (likely(mc))
65 mc->func->intr_rearm(mc);
66}
67
68static u32
69nvkm_mc_intr_stat(struct nvkm_mc *mc)
70{
71 u32 intr = mc->func->intr_stat(mc);
72 if (WARN_ON_ONCE(intr == 0xffffffff))
73 intr = 0;
74 return intr;
75}
76
77void
78nvkm_mc_intr(struct nvkm_device *device, bool *handled)
79{
80 struct nvkm_mc *mc = device->mc;
81 struct nvkm_subdev *subdev;
82 const struct nvkm_mc_map *map;
83 u32 stat, intr;
84 u64 subdevs;
85
86 if (unlikely(!mc))
87 return;
88
89 intr = nvkm_mc_intr_stat(mc);
90 stat = nvkm_top_intr(device, intr, &subdevs);
91 while (subdevs) {
92 enum nvkm_devidx subidx = __ffs64(subdevs);
93 subdev = nvkm_device_subdev(device, subidx);
94 if (subdev)
95 nvkm_subdev_intr(subdev);
96 subdevs &= ~BIT_ULL(subidx);
97 }
98
99 for (map = mc->func->intr; map->stat; map++) {
100 if (intr & map->stat) {
101 subdev = nvkm_device_subdev(device, map->unit);
102 if (subdev)
103 nvkm_subdev_intr(subdev);
104 stat &= ~map->stat;
105 }
106 }
107
108 if (stat)
109 nvkm_error(&mc->subdev, "intr %08x\n", stat);
110 *handled = intr != 0;
111
112 if (mc->func->intr_hack)
113 mc->func->intr_hack(mc, handled);
114}
115
116static u32
117nvkm_mc_reset_mask(struct nvkm_device *device, bool isauto,
118 enum nvkm_devidx devidx)
119{
120 struct nvkm_mc *mc = device->mc;
121 const struct nvkm_mc_map *map;
122 u64 pmc_enable = 0;
123 if (likely(mc)) {
124 if (!(pmc_enable = nvkm_top_reset(device, devidx))) {
125 for (map = mc->func->reset; map && map->stat; map++) {
126 if (!isauto || !map->noauto) {
127 if (map->unit == devidx) {
128 pmc_enable = map->stat;
129 break;
130 }
131 }
132 }
133 }
134 }
135 return pmc_enable;
136}
137
138void
139nvkm_mc_reset(struct nvkm_device *device, enum nvkm_devidx devidx)
140{
141 u64 pmc_enable = nvkm_mc_reset_mask(device, true, devidx);
142 if (pmc_enable) {
143 nvkm_mask(device, 0x000200, pmc_enable, 0x00000000);
144 nvkm_mask(device, 0x000200, pmc_enable, pmc_enable);
145 nvkm_rd32(device, 0x000200);
146 }
147}
148
149void
150nvkm_mc_disable(struct nvkm_device *device, enum nvkm_devidx devidx)
151{
152 u64 pmc_enable = nvkm_mc_reset_mask(device, false, devidx);
153 if (pmc_enable)
154 nvkm_mask(device, 0x000200, pmc_enable, 0x00000000);
155}
156
157void
158nvkm_mc_enable(struct nvkm_device *device, enum nvkm_devidx devidx)
159{
160 u64 pmc_enable = nvkm_mc_reset_mask(device, false, devidx);
161 if (pmc_enable) {
162 nvkm_mask(device, 0x000200, pmc_enable, pmc_enable);
163 nvkm_rd32(device, 0x000200);
164 }
165}
166
167bool
168nvkm_mc_enabled(struct nvkm_device *device, enum nvkm_devidx devidx)
169{
170 u64 pmc_enable = nvkm_mc_reset_mask(device, false, devidx);
171
172 return (pmc_enable != 0) &&
173 ((nvkm_rd32(device, 0x000200) & pmc_enable) == pmc_enable);
174}
175
176
177static int
178nvkm_mc_fini(struct nvkm_subdev *subdev, bool suspend)
179{
180 nvkm_mc_intr_unarm(subdev->device);
181 return 0;
182}
183
184static int
185nvkm_mc_init(struct nvkm_subdev *subdev)
186{
187 struct nvkm_mc *mc = nvkm_mc(subdev);
188 if (mc->func->init)
189 mc->func->init(mc);
190 nvkm_mc_intr_rearm(subdev->device);
191 return 0;
192}
193
194static void *
195nvkm_mc_dtor(struct nvkm_subdev *subdev)
196{
197 return nvkm_mc(subdev);
198}
199
200static const struct nvkm_subdev_func
201nvkm_mc = {
202 .dtor = nvkm_mc_dtor,
203 .init = nvkm_mc_init,
204 .fini = nvkm_mc_fini,
205};
206
207void
208nvkm_mc_ctor(const struct nvkm_mc_func *func, struct nvkm_device *device,
209 int index, struct nvkm_mc *mc)
210{
211 nvkm_subdev_ctor(&nvkm_mc, device, index, &mc->subdev);
212 mc->func = func;
213}
214
215int
216nvkm_mc_new_(const struct nvkm_mc_func *func, struct nvkm_device *device,
217 int index, struct nvkm_mc **pmc)
218{
219 struct nvkm_mc *mc;
220 if (!(mc = *pmc = kzalloc(sizeof(*mc), GFP_KERNEL)))
221 return -ENOMEM;
222 nvkm_mc_ctor(func, device, index, *pmc);
223 return 0;
224}
225