nouveau_pci.c revision 1.30 1 /* $NetBSD: nouveau_pci.c,v 1.30 2021/12/19 11:05:13 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.30 2021/12/19 11:05:13 riastradh Exp $");
34
35 #ifdef _KERNEL_OPT
36 #if defined(__arm__) || defined(__aarch64__)
37 #include "opt_fdt.h"
38 #endif
39 #endif
40
41 #include <sys/types.h>
42 #include <sys/device.h>
43 #include <sys/queue.h>
44 #include <sys/workqueue.h>
45 #include <sys/module.h>
46
47 #ifdef FDT
48 #include <dev/fdt/fdtvar.h>
49 #endif
50
51 #include <drm/drm_pci.h>
52
53 #include <core/device.h>
54 #include <core/pci.h>
55
56 #include "nouveau_drv.h"
57 #include "nouveau_pci.h"
58
59 struct drm_device;
60
61 MODULE(MODULE_CLASS_DRIVER, nouveau_pci, "nouveau,drmkms_pci");
62
63 SIMPLEQ_HEAD(nouveau_pci_task_head, nouveau_pci_task);
64
65 struct nouveau_pci_softc {
66 device_t sc_dev;
67 struct pci_attach_args sc_pa;
68 enum {
69 NOUVEAU_TASK_ATTACH,
70 NOUVEAU_TASK_WORKQUEUE,
71 } sc_task_state;
72 union {
73 struct workqueue *workqueue;
74 struct nouveau_pci_task_head attach;
75 } sc_task_u;
76 struct drm_device *sc_drm_dev;
77 struct pci_dev sc_pci_dev;
78 struct nvkm_device *sc_nv_dev;
79 bool sc_pci_attached;
80 bool sc_dev_registered;
81 };
82
83 static int nouveau_pci_match(device_t, cfdata_t, void *);
84 static void nouveau_pci_attach(device_t, device_t, void *);
85 static void nouveau_pci_attach_real(device_t);
86 static int nouveau_pci_detach(device_t, int);
87
88 static bool nouveau_pci_suspend(device_t, const pmf_qual_t *);
89 static bool nouveau_pci_resume(device_t, const pmf_qual_t *);
90
91 static void nouveau_pci_task_work(struct work *, void *);
92
93 CFATTACH_DECL_NEW(nouveau_pci, sizeof(struct nouveau_pci_softc),
94 nouveau_pci_match, nouveau_pci_attach, nouveau_pci_detach, NULL);
95
96 /* Kludge to get this from nouveau_drm.c. */
97 extern struct drm_driver *const nouveau_drm_driver_pci;
98
99 static int
100 nouveau_pci_match(device_t parent, cfdata_t match, void *aux)
101 {
102 const struct pci_attach_args *const pa = aux;
103 struct pci_dev pdev;
104 struct nvkm_device *device;
105 int ret;
106
107 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA &&
108 PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA_SGS)
109 return 0;
110
111 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
112 return 0;
113
114 /*
115 * NetBSD drm2 doesn't support Pascal, Volta or Turing based cards:
116 * 0x1580-0x15ff GP100
117 * 0x1b00-0x1b7f GP102
118 * 0x1b80-0x1bff GP104
119 * 0x1c00-0x1c7f GP106
120 * 0x1c80-0x1cff GP107
121 * 0x1d00-0x1d7f GP108
122 * 0x1d80-0x1dff GV100
123 * 0x1e00-0x1e7f TU102
124 * 0x1e80-0x1eff TU104
125 * 0x1f00-0x1f7f TU106
126 * 0x1f80-0x1fff TU117
127 * 0x2180-0x21ff TU116
128 *
129 * reduce this to >= 1580, so that new chipsets not explictly
130 * listed above will be picked up.
131 *
132 * XXX perhaps switch this to explicitly match known list.
133 */
134 if (PCI_PRODUCT(pa->pa_id) >= 0x1580)
135 return 0;
136
137 linux_pci_dev_init(&pdev, parent /* XXX bogus */, parent, pa, 0);
138 ret = nvkm_device_pci_new(&pdev, NULL, "error",
139 /* detect */ true, /* mmio */ false, /* subdev_mask */ 0, &device);
140 if (ret == 0) /* don't want to hang onto it */
141 nvkm_device_del(&device);
142 linux_pci_dev_destroy(&pdev);
143 if (ret) /* failure */
144 return 0;
145
146 return 6; /* XXX Beat genfb_pci... */
147 }
148
149 extern char *nouveau_config;
150 extern char *nouveau_debug;
151
152 static void
153 nouveau_pci_attach(device_t parent, device_t self, void *aux)
154 {
155 struct nouveau_pci_softc *const sc = device_private(self);
156 const struct pci_attach_args *const pa = aux;
157
158 pci_aprint_devinfo(pa, NULL);
159
160 if (!pmf_device_register(self, &nouveau_pci_suspend, &nouveau_pci_resume))
161 aprint_error_dev(self, "unable to establish power handler\n");
162
163 /*
164 * Trivial initialization first; the rest will come after we
165 * have mounted the root file system and can load firmware
166 * images.
167 */
168 sc->sc_dev = NULL;
169 sc->sc_pa = *pa;
170
171 #ifdef FDT
172 /*
173 * XXX Remove the simple framebuffer, assuming that this device
174 * will take over.
175 */
176 const char *fb_compatible[] = { "simple-framebuffer", NULL };
177 fdt_remove_bycompat(fb_compatible);
178 #endif
179
180 config_mountroot(self, &nouveau_pci_attach_real);
181 }
182
183 static void
184 nouveau_pci_attach_real(device_t self)
185 {
186 struct nouveau_pci_softc *const sc = device_private(self);
187 const struct pci_attach_args *const pa = &sc->sc_pa;
188 int error;
189
190 sc->sc_dev = self;
191 sc->sc_task_state = NOUVEAU_TASK_ATTACH;
192 SIMPLEQ_INIT(&sc->sc_task_u.attach);
193
194 /* Initialize the Linux PCI device descriptor. */
195 linux_pci_dev_init(&sc->sc_pci_dev, self, device_parent(self), pa, 0);
196
197 /* XXX errno Linux->NetBSD */
198 error = -nvkm_device_pci_new(&sc->sc_pci_dev,
199 nouveau_config, nouveau_debug,
200 /* detect */ true, /* mmio */ true, /* subdev_mask */ ~0ULL,
201 &sc->sc_nv_dev);
202 if (error) {
203 aprint_error_dev(self, "unable to create nouveau device: %d\n",
204 error);
205 sc->sc_nv_dev = NULL;
206 return;
207 }
208
209 sc->sc_drm_dev = drm_dev_alloc(nouveau_drm_driver_pci, self);
210 if (IS_ERR(sc->sc_drm_dev)) {
211 aprint_error_dev(self, "unable to create drm device: %ld\n",
212 PTR_ERR(sc->sc_drm_dev));
213 sc->sc_drm_dev = NULL;
214 return;
215 }
216
217 /* XXX errno Linux->NetBSD */
218 error = -drm_pci_attach(sc->sc_drm_dev, pa, &sc->sc_pci_dev);
219 if (error) {
220 aprint_error_dev(self, "unable to attach drm: %d\n", error);
221 return;
222 }
223 sc->sc_pci_attached = true;
224
225 /* XXX errno Linux->NetBSD */
226 error = -drm_dev_register(sc->sc_drm_dev, 0);
227 if (error) {
228 aprint_error_dev(self, "unable to register drm: %d\n", error);
229 return;
230 }
231 sc->sc_dev_registered = true;
232
233 while (!SIMPLEQ_EMPTY(&sc->sc_task_u.attach)) {
234 struct nouveau_pci_task *const task =
235 SIMPLEQ_FIRST(&sc->sc_task_u.attach);
236
237 SIMPLEQ_REMOVE_HEAD(&sc->sc_task_u.attach, nt_u.queue);
238 (*task->nt_fn)(task);
239 }
240
241 sc->sc_task_state = NOUVEAU_TASK_WORKQUEUE;
242 error = workqueue_create(&sc->sc_task_u.workqueue, "nouveau_pci",
243 &nouveau_pci_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE);
244 if (error) {
245 aprint_error_dev(self, "unable to create workqueue: %d\n",
246 error);
247 sc->sc_task_u.workqueue = NULL;
248 return;
249 }
250 }
251
252 static int
253 nouveau_pci_detach(device_t self, int flags)
254 {
255 struct nouveau_pci_softc *const sc = device_private(self);
256 int error;
257
258 if (sc->sc_dev == NULL)
259 /* Not done attaching. */
260 return EBUSY;
261
262 /* XXX Check for in-use before tearing it all down... */
263 error = config_detach_children(self, flags);
264 if (error)
265 return error;
266
267 if (sc->sc_task_state == NOUVEAU_TASK_ATTACH)
268 goto out0;
269 if (sc->sc_task_u.workqueue != NULL) {
270 workqueue_destroy(sc->sc_task_u.workqueue);
271 sc->sc_task_u.workqueue = NULL;
272 }
273
274 if (sc->sc_nv_dev == NULL)
275 goto out0;
276 if (sc->sc_drm_dev == NULL)
277 goto out1;
278 if (!sc->sc_pci_attached)
279 goto out2;
280 if (!sc->sc_dev_registered)
281 goto out3;
282
283 drm_dev_unregister(sc->sc_drm_dev);
284 out3: drm_pci_detach(sc->sc_drm_dev);
285 out2: drm_dev_put(sc->sc_drm_dev);
286 sc->sc_drm_dev = NULL;
287 out1: nvkm_device_del(&sc->sc_nv_dev);
288 out0: linux_pci_dev_destroy(&sc->sc_pci_dev);
289 pmf_device_deregister(self);
290 return 0;
291 }
292
293 /*
294 * XXX Synchronize with nouveau_do_suspend in nouveau_drm.c.
295 */
296 static bool
297 nouveau_pci_suspend(device_t self, const pmf_qual_t *qual __unused)
298 {
299 struct nouveau_pci_softc *const sc = device_private(self);
300
301 return nouveau_pmops_suspend(sc->sc_drm_dev) == 0;
302 }
303
304 static bool
305 nouveau_pci_resume(device_t self, const pmf_qual_t *qual)
306 {
307 struct nouveau_pci_softc *const sc = device_private(self);
308
309 return nouveau_pmops_resume(sc->sc_drm_dev) == 0;
310 }
311
312 static void
313 nouveau_pci_task_work(struct work *work, void *cookie __unused)
314 {
315 struct nouveau_pci_task *const task = container_of(work,
316 struct nouveau_pci_task, nt_u.work);
317
318 (*task->nt_fn)(task);
319 }
320
321 int
322 nouveau_pci_task_schedule(device_t self, struct nouveau_pci_task *task)
323 {
324 struct nouveau_pci_softc *const sc = device_private(self);
325
326 switch (sc->sc_task_state) {
327 case NOUVEAU_TASK_ATTACH:
328 SIMPLEQ_INSERT_TAIL(&sc->sc_task_u.attach, task, nt_u.queue);
329 return 0;
330 case NOUVEAU_TASK_WORKQUEUE:
331 if (sc->sc_task_u.workqueue == NULL) {
332 aprint_error_dev(self, "unable to schedule task\n");
333 return EIO;
334 }
335 workqueue_enqueue(sc->sc_task_u.workqueue, &task->nt_u.work,
336 NULL);
337 return 0;
338 default:
339 panic("nouveau in invalid task state: %d\n",
340 (int)sc->sc_task_state);
341 }
342 }
343
344 extern struct drm_driver *const nouveau_drm_driver_stub; /* XXX */
345 extern struct drm_driver *const nouveau_drm_driver_pci; /* XXX */
346
347 static int
348 nouveau_pci_modcmd(modcmd_t cmd, void *arg __unused)
349 {
350
351 switch (cmd) {
352 case MODULE_CMD_INIT:
353 *nouveau_drm_driver_pci = *nouveau_drm_driver_stub;
354 nouveau_drm_driver_pci->set_busid = drm_pci_set_busid;
355 nouveau_drm_driver_pci->request_irq = drm_pci_request_irq;
356 nouveau_drm_driver_pci->free_irq = drm_pci_free_irq;
357 #if 0 /* XXX nouveau acpi */
358 nouveau_register_dsm_handler();
359 #endif
360 break;
361 case MODULE_CMD_FINI:
362 #if 0 /* XXX nouveau acpi */
363 nouveau_unregister_dsm_handler();
364 #endif
365 break;
366 default:
367 return ENOTTY;
368 }
369
370 return 0;
371 }
372