nouveau_pci.c revision 1.31 1 1.31 riastrad /* $NetBSD: nouveau_pci.c,v 1.31 2021/12/19 11:05:20 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2015 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 riastrad * by Taylor R. Campbell.
9 1.1 riastrad *
10 1.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1 riastrad * modification, are permitted provided that the following conditions
12 1.1 riastrad * are met:
13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1 riastrad *
19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 riastrad */
31 1.1 riastrad
32 1.1 riastrad #include <sys/cdefs.h>
33 1.31 riastrad __KERNEL_RCSID(0, "$NetBSD: nouveau_pci.c,v 1.31 2021/12/19 11:05:20 riastradh Exp $");
34 1.26 jmcneill
35 1.26 jmcneill #ifdef _KERNEL_OPT
36 1.26 jmcneill #if defined(__arm__) || defined(__aarch64__)
37 1.26 jmcneill #include "opt_fdt.h"
38 1.26 jmcneill #endif
39 1.26 jmcneill #endif
40 1.1 riastrad
41 1.1 riastrad #include <sys/types.h>
42 1.1 riastrad #include <sys/device.h>
43 1.1 riastrad #include <sys/queue.h>
44 1.1 riastrad #include <sys/workqueue.h>
45 1.4 jmcneill #include <sys/module.h>
46 1.1 riastrad
47 1.26 jmcneill #ifdef FDT
48 1.26 jmcneill #include <dev/fdt/fdtvar.h>
49 1.26 jmcneill #endif
50 1.26 jmcneill
51 1.29 riastrad #include <drm/drm_pci.h>
52 1.29 riastrad
53 1.29 riastrad #include <core/device.h>
54 1.29 riastrad #include <core/pci.h>
55 1.29 riastrad
56 1.27 riastrad #include "nouveau_drv.h"
57 1.1 riastrad #include "nouveau_pci.h"
58 1.1 riastrad
59 1.28 riastrad struct drm_device;
60 1.28 riastrad
61 1.4 jmcneill MODULE(MODULE_CLASS_DRIVER, nouveau_pci, "nouveau,drmkms_pci");
62 1.1 riastrad
63 1.4 jmcneill SIMPLEQ_HEAD(nouveau_pci_task_head, nouveau_pci_task);
64 1.4 jmcneill
65 1.4 jmcneill struct nouveau_pci_softc {
66 1.1 riastrad device_t sc_dev;
67 1.24 wiz struct pci_attach_args sc_pa;
68 1.1 riastrad enum {
69 1.1 riastrad NOUVEAU_TASK_ATTACH,
70 1.1 riastrad NOUVEAU_TASK_WORKQUEUE,
71 1.1 riastrad } sc_task_state;
72 1.1 riastrad union {
73 1.1 riastrad struct workqueue *workqueue;
74 1.4 jmcneill struct nouveau_pci_task_head attach;
75 1.1 riastrad } sc_task_u;
76 1.1 riastrad struct drm_device *sc_drm_dev;
77 1.1 riastrad struct pci_dev sc_pci_dev;
78 1.12 riastrad struct nvkm_device *sc_nv_dev;
79 1.30 riastrad bool sc_pci_attached;
80 1.30 riastrad bool sc_dev_registered;
81 1.1 riastrad };
82 1.1 riastrad
83 1.4 jmcneill static int nouveau_pci_match(device_t, cfdata_t, void *);
84 1.4 jmcneill static void nouveau_pci_attach(device_t, device_t, void *);
85 1.24 wiz static void nouveau_pci_attach_real(device_t);
86 1.4 jmcneill static int nouveau_pci_detach(device_t, int);
87 1.1 riastrad
88 1.4 jmcneill static bool nouveau_pci_suspend(device_t, const pmf_qual_t *);
89 1.4 jmcneill static bool nouveau_pci_resume(device_t, const pmf_qual_t *);
90 1.1 riastrad
91 1.4 jmcneill static void nouveau_pci_task_work(struct work *, void *);
92 1.1 riastrad
93 1.4 jmcneill CFATTACH_DECL_NEW(nouveau_pci, sizeof(struct nouveau_pci_softc),
94 1.4 jmcneill nouveau_pci_match, nouveau_pci_attach, nouveau_pci_detach, NULL);
95 1.1 riastrad
96 1.1 riastrad /* Kludge to get this from nouveau_drm.c. */
97 1.14 riastrad extern struct drm_driver *const nouveau_drm_driver_pci;
98 1.1 riastrad
99 1.1 riastrad static int
100 1.4 jmcneill nouveau_pci_match(device_t parent, cfdata_t match, void *aux)
101 1.1 riastrad {
102 1.1 riastrad const struct pci_attach_args *const pa = aux;
103 1.19 riastrad struct pci_dev pdev;
104 1.19 riastrad struct nvkm_device *device;
105 1.19 riastrad int ret;
106 1.1 riastrad
107 1.1 riastrad if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA &&
108 1.1 riastrad PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA_SGS)
109 1.1 riastrad return 0;
110 1.1 riastrad
111 1.1 riastrad if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
112 1.1 riastrad return 0;
113 1.1 riastrad
114 1.10 mrg /*
115 1.23 mrg * NetBSD drm2 doesn't support Pascal, Volta or Turing based cards:
116 1.10 mrg * 0x1580-0x15ff GP100
117 1.10 mrg * 0x1b00-0x1b7f GP102
118 1.10 mrg * 0x1b80-0x1bff GP104
119 1.10 mrg * 0x1c00-0x1c7f GP106
120 1.10 mrg * 0x1c80-0x1cff GP107
121 1.10 mrg * 0x1d00-0x1d7f GP108
122 1.10 mrg * 0x1d80-0x1dff GV100
123 1.23 mrg * 0x1e00-0x1e7f TU102
124 1.23 mrg * 0x1e80-0x1eff TU104
125 1.23 mrg * 0x1f00-0x1f7f TU106
126 1.25 mrg * 0x1f80-0x1fff TU117
127 1.25 mrg * 0x2180-0x21ff TU116
128 1.25 mrg *
129 1.25 mrg * reduce this to >= 1580, so that new chipsets not explictly
130 1.25 mrg * listed above will be picked up.
131 1.25 mrg *
132 1.25 mrg * XXX perhaps switch this to explicitly match known list.
133 1.10 mrg */
134 1.25 mrg if (PCI_PRODUCT(pa->pa_id) >= 0x1580)
135 1.10 mrg return 0;
136 1.10 mrg
137 1.20 riastrad linux_pci_dev_init(&pdev, parent /* XXX bogus */, parent, pa, 0);
138 1.20 riastrad ret = nvkm_device_pci_new(&pdev, NULL, "error",
139 1.20 riastrad /* detect */ true, /* mmio */ false, /* subdev_mask */ 0, &device);
140 1.19 riastrad if (ret == 0) /* don't want to hang onto it */
141 1.19 riastrad nvkm_device_del(&device);
142 1.19 riastrad linux_pci_dev_destroy(&pdev);
143 1.19 riastrad if (ret) /* failure */
144 1.19 riastrad return 0;
145 1.19 riastrad
146 1.1 riastrad return 6; /* XXX Beat genfb_pci... */
147 1.1 riastrad }
148 1.1 riastrad
149 1.3 riastrad extern char *nouveau_config;
150 1.3 riastrad extern char *nouveau_debug;
151 1.3 riastrad
152 1.1 riastrad static void
153 1.4 jmcneill nouveau_pci_attach(device_t parent, device_t self, void *aux)
154 1.1 riastrad {
155 1.4 jmcneill struct nouveau_pci_softc *const sc = device_private(self);
156 1.1 riastrad const struct pci_attach_args *const pa = aux;
157 1.1 riastrad
158 1.1 riastrad pci_aprint_devinfo(pa, NULL);
159 1.1 riastrad
160 1.24 wiz if (!pmf_device_register(self, &nouveau_pci_suspend, &nouveau_pci_resume))
161 1.24 wiz aprint_error_dev(self, "unable to establish power handler\n");
162 1.24 wiz
163 1.24 wiz /*
164 1.24 wiz * Trivial initialization first; the rest will come after we
165 1.24 wiz * have mounted the root file system and can load firmware
166 1.24 wiz * images.
167 1.24 wiz */
168 1.24 wiz sc->sc_dev = NULL;
169 1.24 wiz sc->sc_pa = *pa;
170 1.1 riastrad
171 1.26 jmcneill #ifdef FDT
172 1.26 jmcneill /*
173 1.26 jmcneill * XXX Remove the simple framebuffer, assuming that this device
174 1.26 jmcneill * will take over.
175 1.26 jmcneill */
176 1.26 jmcneill const char *fb_compatible[] = { "simple-framebuffer", NULL };
177 1.26 jmcneill fdt_remove_bycompat(fb_compatible);
178 1.26 jmcneill #endif
179 1.26 jmcneill
180 1.24 wiz config_mountroot(self, &nouveau_pci_attach_real);
181 1.24 wiz }
182 1.16 riastrad
183 1.24 wiz static void
184 1.24 wiz nouveau_pci_attach_real(device_t self)
185 1.24 wiz {
186 1.24 wiz struct nouveau_pci_softc *const sc = device_private(self);
187 1.24 wiz const struct pci_attach_args *const pa = &sc->sc_pa;
188 1.24 wiz int error;
189 1.1 riastrad
190 1.24 wiz sc->sc_dev = self;
191 1.1 riastrad sc->sc_task_state = NOUVEAU_TASK_ATTACH;
192 1.1 riastrad SIMPLEQ_INIT(&sc->sc_task_u.attach);
193 1.1 riastrad
194 1.24 wiz /* Initialize the Linux PCI device descriptor. */
195 1.24 wiz linux_pci_dev_init(&sc->sc_pci_dev, self, device_parent(self), pa, 0);
196 1.24 wiz
197 1.3 riastrad /* XXX errno Linux->NetBSD */
198 1.12 riastrad error = -nvkm_device_pci_new(&sc->sc_pci_dev,
199 1.20 riastrad nouveau_config, nouveau_debug,
200 1.20 riastrad /* detect */ true, /* mmio */ true, /* subdev_mask */ ~0ULL,
201 1.3 riastrad &sc->sc_nv_dev);
202 1.3 riastrad if (error) {
203 1.3 riastrad aprint_error_dev(self, "unable to create nouveau device: %d\n",
204 1.3 riastrad error);
205 1.30 riastrad sc->sc_nv_dev = NULL;
206 1.30 riastrad return;
207 1.30 riastrad }
208 1.30 riastrad
209 1.30 riastrad sc->sc_drm_dev = drm_dev_alloc(nouveau_drm_driver_pci, self);
210 1.30 riastrad if (IS_ERR(sc->sc_drm_dev)) {
211 1.30 riastrad aprint_error_dev(self, "unable to create drm device: %ld\n",
212 1.30 riastrad PTR_ERR(sc->sc_drm_dev));
213 1.30 riastrad sc->sc_drm_dev = NULL;
214 1.3 riastrad return;
215 1.3 riastrad }
216 1.3 riastrad
217 1.1 riastrad /* XXX errno Linux->NetBSD */
218 1.30 riastrad error = -drm_pci_attach(sc->sc_drm_dev, pa, &sc->sc_pci_dev);
219 1.1 riastrad if (error) {
220 1.1 riastrad aprint_error_dev(self, "unable to attach drm: %d\n", error);
221 1.1 riastrad return;
222 1.1 riastrad }
223 1.30 riastrad sc->sc_pci_attached = true;
224 1.30 riastrad
225 1.30 riastrad /* XXX errno Linux->NetBSD */
226 1.31 riastrad error = -nouveau_drm_device_init(sc->sc_drm_dev);
227 1.31 riastrad if (error) {
228 1.31 riastrad aprint_error_dev(self, "unable to init nouveau: %d\n", error);
229 1.31 riastrad return;
230 1.31 riastrad }
231 1.31 riastrad
232 1.31 riastrad /* XXX errno Linux->NetBSD */
233 1.30 riastrad error = -drm_dev_register(sc->sc_drm_dev, 0);
234 1.30 riastrad if (error) {
235 1.30 riastrad aprint_error_dev(self, "unable to register drm: %d\n", error);
236 1.30 riastrad return;
237 1.30 riastrad }
238 1.30 riastrad sc->sc_dev_registered = true;
239 1.1 riastrad
240 1.1 riastrad while (!SIMPLEQ_EMPTY(&sc->sc_task_u.attach)) {
241 1.4 jmcneill struct nouveau_pci_task *const task =
242 1.1 riastrad SIMPLEQ_FIRST(&sc->sc_task_u.attach);
243 1.1 riastrad
244 1.1 riastrad SIMPLEQ_REMOVE_HEAD(&sc->sc_task_u.attach, nt_u.queue);
245 1.1 riastrad (*task->nt_fn)(task);
246 1.1 riastrad }
247 1.1 riastrad
248 1.1 riastrad sc->sc_task_state = NOUVEAU_TASK_WORKQUEUE;
249 1.4 jmcneill error = workqueue_create(&sc->sc_task_u.workqueue, "nouveau_pci",
250 1.4 jmcneill &nouveau_pci_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE);
251 1.1 riastrad if (error) {
252 1.1 riastrad aprint_error_dev(self, "unable to create workqueue: %d\n",
253 1.1 riastrad error);
254 1.1 riastrad sc->sc_task_u.workqueue = NULL;
255 1.1 riastrad return;
256 1.1 riastrad }
257 1.1 riastrad }
258 1.1 riastrad
259 1.1 riastrad static int
260 1.4 jmcneill nouveau_pci_detach(device_t self, int flags)
261 1.1 riastrad {
262 1.4 jmcneill struct nouveau_pci_softc *const sc = device_private(self);
263 1.1 riastrad int error;
264 1.1 riastrad
265 1.24 wiz if (sc->sc_dev == NULL)
266 1.24 wiz /* Not done attaching. */
267 1.24 wiz return EBUSY;
268 1.24 wiz
269 1.1 riastrad /* XXX Check for in-use before tearing it all down... */
270 1.1 riastrad error = config_detach_children(self, flags);
271 1.1 riastrad if (error)
272 1.1 riastrad return error;
273 1.1 riastrad
274 1.1 riastrad if (sc->sc_task_state == NOUVEAU_TASK_ATTACH)
275 1.3 riastrad goto out0;
276 1.1 riastrad if (sc->sc_task_u.workqueue != NULL) {
277 1.1 riastrad workqueue_destroy(sc->sc_task_u.workqueue);
278 1.1 riastrad sc->sc_task_u.workqueue = NULL;
279 1.1 riastrad }
280 1.1 riastrad
281 1.3 riastrad if (sc->sc_nv_dev == NULL)
282 1.3 riastrad goto out0;
283 1.1 riastrad if (sc->sc_drm_dev == NULL)
284 1.3 riastrad goto out1;
285 1.30 riastrad if (!sc->sc_pci_attached)
286 1.30 riastrad goto out2;
287 1.30 riastrad if (!sc->sc_dev_registered)
288 1.30 riastrad goto out3;
289 1.30 riastrad
290 1.30 riastrad drm_dev_unregister(sc->sc_drm_dev);
291 1.30 riastrad out3: drm_pci_detach(sc->sc_drm_dev);
292 1.30 riastrad out2: drm_dev_put(sc->sc_drm_dev);
293 1.1 riastrad sc->sc_drm_dev = NULL;
294 1.13 riastrad out1: nvkm_device_del(&sc->sc_nv_dev);
295 1.18 riastrad out0: linux_pci_dev_destroy(&sc->sc_pci_dev);
296 1.18 riastrad pmf_device_deregister(self);
297 1.1 riastrad return 0;
298 1.1 riastrad }
299 1.1 riastrad
300 1.1 riastrad /*
301 1.1 riastrad * XXX Synchronize with nouveau_do_suspend in nouveau_drm.c.
302 1.1 riastrad */
303 1.1 riastrad static bool
304 1.4 jmcneill nouveau_pci_suspend(device_t self, const pmf_qual_t *qual __unused)
305 1.1 riastrad {
306 1.7 riastrad struct nouveau_pci_softc *const sc = device_private(self);
307 1.1 riastrad
308 1.7 riastrad return nouveau_pmops_suspend(sc->sc_drm_dev) == 0;
309 1.1 riastrad }
310 1.1 riastrad
311 1.1 riastrad static bool
312 1.4 jmcneill nouveau_pci_resume(device_t self, const pmf_qual_t *qual)
313 1.1 riastrad {
314 1.7 riastrad struct nouveau_pci_softc *const sc = device_private(self);
315 1.1 riastrad
316 1.7 riastrad return nouveau_pmops_resume(sc->sc_drm_dev) == 0;
317 1.1 riastrad }
318 1.1 riastrad
319 1.1 riastrad static void
320 1.4 jmcneill nouveau_pci_task_work(struct work *work, void *cookie __unused)
321 1.1 riastrad {
322 1.4 jmcneill struct nouveau_pci_task *const task = container_of(work,
323 1.4 jmcneill struct nouveau_pci_task, nt_u.work);
324 1.1 riastrad
325 1.1 riastrad (*task->nt_fn)(task);
326 1.1 riastrad }
327 1.1 riastrad
328 1.1 riastrad int
329 1.4 jmcneill nouveau_pci_task_schedule(device_t self, struct nouveau_pci_task *task)
330 1.1 riastrad {
331 1.4 jmcneill struct nouveau_pci_softc *const sc = device_private(self);
332 1.1 riastrad
333 1.1 riastrad switch (sc->sc_task_state) {
334 1.1 riastrad case NOUVEAU_TASK_ATTACH:
335 1.1 riastrad SIMPLEQ_INSERT_TAIL(&sc->sc_task_u.attach, task, nt_u.queue);
336 1.1 riastrad return 0;
337 1.1 riastrad case NOUVEAU_TASK_WORKQUEUE:
338 1.1 riastrad if (sc->sc_task_u.workqueue == NULL) {
339 1.1 riastrad aprint_error_dev(self, "unable to schedule task\n");
340 1.1 riastrad return EIO;
341 1.1 riastrad }
342 1.1 riastrad workqueue_enqueue(sc->sc_task_u.workqueue, &task->nt_u.work,
343 1.1 riastrad NULL);
344 1.1 riastrad return 0;
345 1.1 riastrad default:
346 1.1 riastrad panic("nouveau in invalid task state: %d\n",
347 1.1 riastrad (int)sc->sc_task_state);
348 1.1 riastrad }
349 1.1 riastrad }
350 1.4 jmcneill
351 1.21 riastrad extern struct drm_driver *const nouveau_drm_driver_stub; /* XXX */
352 1.21 riastrad extern struct drm_driver *const nouveau_drm_driver_pci; /* XXX */
353 1.21 riastrad
354 1.4 jmcneill static int
355 1.4 jmcneill nouveau_pci_modcmd(modcmd_t cmd, void *arg __unused)
356 1.4 jmcneill {
357 1.4 jmcneill
358 1.4 jmcneill switch (cmd) {
359 1.4 jmcneill case MODULE_CMD_INIT:
360 1.21 riastrad *nouveau_drm_driver_pci = *nouveau_drm_driver_stub;
361 1.21 riastrad nouveau_drm_driver_pci->set_busid = drm_pci_set_busid;
362 1.21 riastrad nouveau_drm_driver_pci->request_irq = drm_pci_request_irq;
363 1.21 riastrad nouveau_drm_driver_pci->free_irq = drm_pci_free_irq;
364 1.4 jmcneill #if 0 /* XXX nouveau acpi */
365 1.4 jmcneill nouveau_register_dsm_handler();
366 1.4 jmcneill #endif
367 1.4 jmcneill break;
368 1.4 jmcneill case MODULE_CMD_FINI:
369 1.4 jmcneill #if 0 /* XXX nouveau acpi */
370 1.4 jmcneill nouveau_unregister_dsm_handler();
371 1.4 jmcneill #endif
372 1.4 jmcneill break;
373 1.4 jmcneill default:
374 1.4 jmcneill return ENOTTY;
375 1.4 jmcneill }
376 1.4 jmcneill
377 1.4 jmcneill return 0;
378 1.4 jmcneill }
379