1 /* $NetBSD: nouveau_nvkm_subdev_gpio_base.c,v 1.4 2021/12/18 23:45:39 riastradh Exp $ */
2
3 /*
4 * Copyright 2011 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
25 */
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_gpio_base.c,v 1.4 2021/12/18 23:45:39 riastradh Exp $");
28
29 #include "priv.h"
30
31 #include <core/option.h>
32 #include <core/notify.h>
33
34 static int
nvkm_gpio_drive(struct nvkm_gpio * gpio,int idx,int line,int dir,int out)35 nvkm_gpio_drive(struct nvkm_gpio *gpio, int idx, int line, int dir, int out)
36 {
37 return gpio->func->drive(gpio, line, dir, out);
38 }
39
40 static int
nvkm_gpio_sense(struct nvkm_gpio * gpio,int idx,int line)41 nvkm_gpio_sense(struct nvkm_gpio *gpio, int idx, int line)
42 {
43 return gpio->func->sense(gpio, line);
44 }
45
46 void
nvkm_gpio_reset(struct nvkm_gpio * gpio,u8 func)47 nvkm_gpio_reset(struct nvkm_gpio *gpio, u8 func)
48 {
49 if (gpio->func->reset)
50 gpio->func->reset(gpio, func);
51 }
52
53 int
nvkm_gpio_find(struct nvkm_gpio * gpio,int idx,u8 tag,u8 line,struct dcb_gpio_func * func)54 nvkm_gpio_find(struct nvkm_gpio *gpio, int idx, u8 tag, u8 line,
55 struct dcb_gpio_func *func)
56 {
57 struct nvkm_device *device = gpio->subdev.device;
58 struct nvkm_bios *bios = device->bios;
59 u8 ver, len;
60 u16 data;
61
62 if (line == 0xff && tag == 0xff)
63 return -EINVAL;
64
65 data = dcb_gpio_match(bios, idx, tag, line, &ver, &len, func);
66 if (data)
67 return 0;
68
69 /* Apple iMac G4 NV18 */
70 if (device->quirk && device->quirk->tv_gpio) {
71 if (tag == DCB_GPIO_TVDAC0) {
72 *func = (struct dcb_gpio_func) {
73 .func = DCB_GPIO_TVDAC0,
74 .line = device->quirk->tv_gpio,
75 .log[0] = 0,
76 .log[1] = 1,
77 };
78 return 0;
79 }
80 }
81
82 return -ENOENT;
83 }
84
85 int
nvkm_gpio_set(struct nvkm_gpio * gpio,int idx,u8 tag,u8 line,int state)86 nvkm_gpio_set(struct nvkm_gpio *gpio, int idx, u8 tag, u8 line, int state)
87 {
88 struct dcb_gpio_func func;
89 int ret;
90
91 ret = nvkm_gpio_find(gpio, idx, tag, line, &func);
92 if (ret == 0) {
93 int dir = !!(func.log[state] & 0x02);
94 int out = !!(func.log[state] & 0x01);
95 ret = nvkm_gpio_drive(gpio, idx, func.line, dir, out);
96 }
97
98 return ret;
99 }
100
101 int
nvkm_gpio_get(struct nvkm_gpio * gpio,int idx,u8 tag,u8 line)102 nvkm_gpio_get(struct nvkm_gpio *gpio, int idx, u8 tag, u8 line)
103 {
104 struct dcb_gpio_func func;
105 int ret;
106
107 ret = nvkm_gpio_find(gpio, idx, tag, line, &func);
108 if (ret == 0) {
109 ret = nvkm_gpio_sense(gpio, idx, func.line);
110 if (ret >= 0)
111 ret = (ret == (func.log[1] & 1));
112 }
113
114 return ret;
115 }
116
117 static void
nvkm_gpio_intr_fini(struct nvkm_event * event,int type,int index)118 nvkm_gpio_intr_fini(struct nvkm_event *event, int type, int index)
119 {
120 struct nvkm_gpio *gpio = container_of(event, typeof(*gpio), event);
121 gpio->func->intr_mask(gpio, type, 1 << index, 0);
122 }
123
124 static void
nvkm_gpio_intr_init(struct nvkm_event * event,int type,int index)125 nvkm_gpio_intr_init(struct nvkm_event *event, int type, int index)
126 {
127 struct nvkm_gpio *gpio = container_of(event, typeof(*gpio), event);
128 gpio->func->intr_mask(gpio, type, 1 << index, 1 << index);
129 }
130
131 static int
nvkm_gpio_intr_ctor(struct nvkm_object * object,void * data,u32 size,struct nvkm_notify * notify)132 nvkm_gpio_intr_ctor(struct nvkm_object *object, void *data, u32 size,
133 struct nvkm_notify *notify)
134 {
135 struct nvkm_gpio_ntfy_req *req = data;
136 if (!WARN_ON(size != sizeof(*req))) {
137 notify->size = sizeof(struct nvkm_gpio_ntfy_rep);
138 notify->types = req->mask;
139 notify->index = req->line;
140 return 0;
141 }
142 return -EINVAL;
143 }
144
145 static const struct nvkm_event_func
146 nvkm_gpio_intr_func = {
147 .ctor = nvkm_gpio_intr_ctor,
148 .init = nvkm_gpio_intr_init,
149 .fini = nvkm_gpio_intr_fini,
150 };
151
152 static void
nvkm_gpio_intr(struct nvkm_subdev * subdev)153 nvkm_gpio_intr(struct nvkm_subdev *subdev)
154 {
155 struct nvkm_gpio *gpio = nvkm_gpio(subdev);
156 u32 hi, lo, i;
157
158 gpio->func->intr_stat(gpio, &hi, &lo);
159
160 for (i = 0; (hi | lo) && i < gpio->func->lines; i++) {
161 struct nvkm_gpio_ntfy_rep rep = {
162 .mask = (NVKM_GPIO_HI * !!(hi & (1 << i))) |
163 (NVKM_GPIO_LO * !!(lo & (1 << i))),
164 };
165 nvkm_event_send(&gpio->event, rep.mask, i, &rep, sizeof(rep));
166 }
167 }
168
169 static int
nvkm_gpio_fini(struct nvkm_subdev * subdev,bool suspend)170 nvkm_gpio_fini(struct nvkm_subdev *subdev, bool suspend)
171 {
172 struct nvkm_gpio *gpio = nvkm_gpio(subdev);
173 u32 mask = (1ULL << gpio->func->lines) - 1;
174
175 gpio->func->intr_mask(gpio, NVKM_GPIO_TOGGLED, mask, 0);
176 gpio->func->intr_stat(gpio, &mask, &mask);
177 return 0;
178 }
179
180 static const struct dmi_system_id gpio_reset_ids[] = {
181 {
182 .ident = "Apple Macbook 10,1",
183 .matches = {
184 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
185 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro10,1"),
186 }
187 },
188 { }
189 };
190
191 static enum dcb_gpio_func_name power_checks[] = {
192 DCB_GPIO_THERM_EXT_POWER_EVENT,
193 DCB_GPIO_POWER_ALERT,
194 DCB_GPIO_EXT_POWER_LOW,
195 };
196
197 static int
nvkm_gpio_init(struct nvkm_subdev * subdev)198 nvkm_gpio_init(struct nvkm_subdev *subdev)
199 {
200 struct nvkm_gpio *gpio = nvkm_gpio(subdev);
201 struct dcb_gpio_func func;
202 int ret;
203 int i;
204
205 if (dmi_check_system(gpio_reset_ids))
206 nvkm_gpio_reset(gpio, DCB_GPIO_UNUSED);
207
208 if (nvkm_boolopt(subdev->device->cfgopt, "NvPowerChecks", true)) {
209 for (i = 0; i < ARRAY_SIZE(power_checks); ++i) {
210 ret = nvkm_gpio_find(gpio, 0, power_checks[i],
211 DCB_GPIO_UNUSED, &func);
212 if (ret)
213 continue;
214
215 ret = nvkm_gpio_get(gpio, 0, func.func, func.line);
216 if (!ret)
217 continue;
218
219 nvkm_error(&gpio->subdev,
220 "GPU is missing power, check its power "
221 "cables. Boot with "
222 "nouveau.config=NvPowerChecks=0 to "
223 "disable.\n");
224 return -EINVAL;
225 }
226 }
227
228 return 0;
229 }
230
231 static void *
nvkm_gpio_dtor(struct nvkm_subdev * subdev)232 nvkm_gpio_dtor(struct nvkm_subdev *subdev)
233 {
234 struct nvkm_gpio *gpio = nvkm_gpio(subdev);
235 nvkm_event_fini(&gpio->event);
236 return gpio;
237 }
238
239 static const struct nvkm_subdev_func
240 nvkm_gpio = {
241 .dtor = nvkm_gpio_dtor,
242 .init = nvkm_gpio_init,
243 .fini = nvkm_gpio_fini,
244 .intr = nvkm_gpio_intr,
245 };
246
247 int
nvkm_gpio_new_(const struct nvkm_gpio_func * func,struct nvkm_device * device,int index,struct nvkm_gpio ** pgpio)248 nvkm_gpio_new_(const struct nvkm_gpio_func *func, struct nvkm_device *device,
249 int index, struct nvkm_gpio **pgpio)
250 {
251 struct nvkm_gpio *gpio;
252
253 if (!(gpio = *pgpio = kzalloc(sizeof(*gpio), GFP_KERNEL)))
254 return -ENOMEM;
255
256 nvkm_subdev_ctor(&nvkm_gpio, device, index, &gpio->subdev);
257 gpio->func = func;
258
259 return nvkm_event_init(&nvkm_gpio_intr_func, 2, func->lines,
260 &gpio->event);
261 }
262