nouveau_pci.c revision 1.19 1 /* $NetBSD: nouveau_pci.c,v 1.19 2018/08/27 14:12:29 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2015 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: nouveau_pci.c,v 1.19 2018/08/27 14:12:29 riastradh Exp $");
34
35 #include <sys/types.h>
36 #include <sys/device.h>
37 #include <sys/queue.h>
38 #include <sys/workqueue.h>
39 #include <sys/module.h>
40
41 #include <drm/drmP.h>
42
43 #include <core/device.h>
44 #include <core/pci.h>
45
46 #include "nouveau_drm.h"
47 #include "nouveau_pci.h"
48
49 MODULE(MODULE_CLASS_DRIVER, nouveau_pci, "nouveau,drmkms_pci");
50
51 SIMPLEQ_HEAD(nouveau_pci_task_head, nouveau_pci_task);
52
53 struct nouveau_pci_softc {
54 device_t sc_dev;
55 enum {
56 NOUVEAU_TASK_ATTACH,
57 NOUVEAU_TASK_WORKQUEUE,
58 } sc_task_state;
59 union {
60 struct workqueue *workqueue;
61 struct nouveau_pci_task_head attach;
62 } sc_task_u;
63 struct drm_device *sc_drm_dev;
64 struct pci_dev sc_pci_dev;
65 struct nvkm_device *sc_nv_dev;
66 };
67
68 static int nouveau_pci_match(device_t, cfdata_t, void *);
69 static void nouveau_pci_attach(device_t, device_t, void *);
70 static int nouveau_pci_detach(device_t, int);
71
72 static bool nouveau_pci_suspend(device_t, const pmf_qual_t *);
73 static bool nouveau_pci_resume(device_t, const pmf_qual_t *);
74
75 static void nouveau_pci_task_work(struct work *, void *);
76
77 CFATTACH_DECL_NEW(nouveau_pci, sizeof(struct nouveau_pci_softc),
78 nouveau_pci_match, nouveau_pci_attach, nouveau_pci_detach, NULL);
79
80 /* Kludge to get this from nouveau_drm.c. */
81 extern struct drm_driver *const nouveau_drm_driver_pci;
82
83 static int
84 nouveau_pci_match(device_t parent, cfdata_t match, void *aux)
85 {
86 const struct pci_attach_args *const pa = aux;
87 struct pci_dev pdev;
88 struct nvkm_device *device;
89 int ret;
90
91 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA &&
92 PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA_SGS)
93 return 0;
94
95 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
96 return 0;
97
98 #define IS_BETWEEN(x,y) \
99 (PCI_PRODUCT(pa->pa_id) >= (x) && PCI_PRODUCT(pa->pa_id) <= (y))
100 /*
101 * NetBSD drm2 needs missing-so-far firmware for Maxwell-based cards:
102 * 0x1380-0x13bf GM107
103 */
104 if (IS_BETWEEN(0x1380, 0x13bf))
105 return 0;
106
107 /*
108 * NetBSD drm2 doesn't support Pascal-based cards:
109 * 0x1580-0x15ff GP100
110 * 0x1b00-0x1b7f GP102
111 * 0x1b80-0x1bff GP104
112 * 0x1c00-0x1c7f GP106
113 * 0x1c80-0x1cff GP107
114 * 0x1d00-0x1d7f GP108
115 * 0x1d80-0x1dff GV100
116 */
117
118 if (IS_BETWEEN(0x1580, 0x15ff) ||
119 IS_BETWEEN(0x1b00, 0x1b7f) ||
120 IS_BETWEEN(0x1b80, 0x1bff) ||
121 IS_BETWEEN(0x1c00, 0x1c7f) ||
122 IS_BETWEEN(0x1c80, 0x1cff) ||
123 IS_BETWEEN(0x1d00, 0x1d7f) ||
124 IS_BETWEEN(0x1d80, 0x1dff))
125 return 0;
126 #undef IS_BETWEEN
127
128 linux_pci_dev_init(&pdev, NULL, parent, pa, 0);
129 ret = nvkm_device_pci_new(&pdev, NULL, "error", true, false, 0,
130 &device);
131 if (ret == 0) /* don't want to hang onto it */
132 nvkm_device_del(&device);
133 linux_pci_dev_destroy(&pdev);
134 if (ret) /* failure */
135 return 0;
136
137 return 6; /* XXX Beat genfb_pci... */
138 }
139
140 extern char *nouveau_config;
141 extern char *nouveau_debug;
142
143 static void
144 nouveau_pci_attach(device_t parent, device_t self, void *aux)
145 {
146 struct nouveau_pci_softc *const sc = device_private(self);
147 const struct pci_attach_args *const pa = aux;
148 int error;
149
150 pci_aprint_devinfo(pa, NULL);
151
152 sc->sc_dev = self;
153
154 /* Initialize the Linux PCI device descriptor. */
155 linux_pci_dev_init(&sc->sc_pci_dev, self, device_parent(self), pa, 0);
156
157 if (!pmf_device_register(self, &nouveau_pci_suspend,
158 &nouveau_pci_resume))
159 aprint_error_dev(self, "unable to establish power handler\n");
160
161 sc->sc_task_state = NOUVEAU_TASK_ATTACH;
162 SIMPLEQ_INIT(&sc->sc_task_u.attach);
163
164 /* XXX errno Linux->NetBSD */
165 error = -nvkm_device_pci_new(&sc->sc_pci_dev,
166 nouveau_config, nouveau_debug, true, true, ~0ULL,
167 &sc->sc_nv_dev);
168 if (error) {
169 aprint_error_dev(self, "unable to create nouveau device: %d\n",
170 error);
171 return;
172 }
173
174 /* XXX errno Linux->NetBSD */
175 error = -drm_pci_attach(self, pa, &sc->sc_pci_dev,
176 nouveau_drm_driver_pci, 0, &sc->sc_drm_dev);
177 if (error) {
178 aprint_error_dev(self, "unable to attach drm: %d\n", error);
179 return;
180 }
181
182 while (!SIMPLEQ_EMPTY(&sc->sc_task_u.attach)) {
183 struct nouveau_pci_task *const task =
184 SIMPLEQ_FIRST(&sc->sc_task_u.attach);
185
186 SIMPLEQ_REMOVE_HEAD(&sc->sc_task_u.attach, nt_u.queue);
187 (*task->nt_fn)(task);
188 }
189
190 sc->sc_task_state = NOUVEAU_TASK_WORKQUEUE;
191 error = workqueue_create(&sc->sc_task_u.workqueue, "nouveau_pci",
192 &nouveau_pci_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE);
193 if (error) {
194 aprint_error_dev(self, "unable to create workqueue: %d\n",
195 error);
196 sc->sc_task_u.workqueue = NULL;
197 return;
198 }
199 }
200
201 static int
202 nouveau_pci_detach(device_t self, int flags)
203 {
204 struct nouveau_pci_softc *const sc = device_private(self);
205 int error;
206
207 /* XXX Check for in-use before tearing it all down... */
208 error = config_detach_children(self, flags);
209 if (error)
210 return error;
211
212 if (sc->sc_task_state == NOUVEAU_TASK_ATTACH)
213 goto out0;
214 if (sc->sc_task_u.workqueue != NULL) {
215 workqueue_destroy(sc->sc_task_u.workqueue);
216 sc->sc_task_u.workqueue = NULL;
217 }
218
219 if (sc->sc_nv_dev == NULL)
220 goto out0;
221
222 if (sc->sc_drm_dev == NULL)
223 goto out1;
224 /* XXX errno Linux->NetBSD */
225 error = -drm_pci_detach(sc->sc_drm_dev, flags);
226 if (error)
227 /* XXX Kinda too late to fail now... */
228 return error;
229 sc->sc_drm_dev = NULL;
230
231 out1: nvkm_device_del(&sc->sc_nv_dev);
232 out0: linux_pci_dev_destroy(&sc->sc_pci_dev);
233 pmf_device_deregister(self);
234 return 0;
235 }
236
237 /*
238 * XXX Synchronize with nouveau_do_suspend in nouveau_drm.c.
239 */
240 static bool
241 nouveau_pci_suspend(device_t self, const pmf_qual_t *qual __unused)
242 {
243 struct nouveau_pci_softc *const sc = device_private(self);
244
245 return nouveau_pmops_suspend(sc->sc_drm_dev) == 0;
246 }
247
248 static bool
249 nouveau_pci_resume(device_t self, const pmf_qual_t *qual)
250 {
251 struct nouveau_pci_softc *const sc = device_private(self);
252
253 return nouveau_pmops_resume(sc->sc_drm_dev) == 0;
254 }
255
256 static void
257 nouveau_pci_task_work(struct work *work, void *cookie __unused)
258 {
259 struct nouveau_pci_task *const task = container_of(work,
260 struct nouveau_pci_task, nt_u.work);
261
262 (*task->nt_fn)(task);
263 }
264
265 int
266 nouveau_pci_task_schedule(device_t self, struct nouveau_pci_task *task)
267 {
268 struct nouveau_pci_softc *const sc = device_private(self);
269
270 switch (sc->sc_task_state) {
271 case NOUVEAU_TASK_ATTACH:
272 SIMPLEQ_INSERT_TAIL(&sc->sc_task_u.attach, task, nt_u.queue);
273 return 0;
274 case NOUVEAU_TASK_WORKQUEUE:
275 if (sc->sc_task_u.workqueue == NULL) {
276 aprint_error_dev(self, "unable to schedule task\n");
277 return EIO;
278 }
279 workqueue_enqueue(sc->sc_task_u.workqueue, &task->nt_u.work,
280 NULL);
281 return 0;
282 default:
283 panic("nouveau in invalid task state: %d\n",
284 (int)sc->sc_task_state);
285 }
286 }
287
288 static int
289 nouveau_pci_modcmd(modcmd_t cmd, void *arg __unused)
290 {
291 int error;
292
293 switch (cmd) {
294 case MODULE_CMD_INIT:
295 error = drm_pci_init(nouveau_drm_driver_pci, NULL);
296 if (error) {
297 aprint_error("nouveau_pci: failed to init: %d\n",
298 error);
299 return error;
300 }
301 #if 0 /* XXX nouveau acpi */
302 nouveau_register_dsm_handler();
303 #endif
304 break;
305 case MODULE_CMD_FINI:
306 #if 0 /* XXX nouveau acpi */
307 nouveau_unregister_dsm_handler();
308 #endif
309 drm_pci_exit(nouveau_drm_driver_pci, NULL);
310 break;
311 default:
312 return ENOTTY;
313 }
314
315 return 0;
316 }
317