nouveau_pci.c revision 1.35
1/*	$NetBSD: nouveau_pci.c,v 1.35 2021/12/19 12:45:35 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.35 2021/12/19 12:45:35 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/atomic.h>
43#include <sys/device.h>
44#include <sys/queue.h>
45#include <sys/workqueue.h>
46#include <sys/module.h>
47
48#ifdef FDT
49#include <dev/fdt/fdtvar.h>
50#endif
51
52#include <drm/drm_pci.h>
53
54#include <core/device.h>
55#include <core/pci.h>
56
57#include "nouveau_drv.h"
58#include "nouveau_pci.h"
59
60struct drm_device;
61
62MODULE(MODULE_CLASS_DRIVER, nouveau_pci, "nouveau,drmkms_pci");
63
64SIMPLEQ_HEAD(nouveau_pci_task_head, nouveau_pci_task);
65
66struct nouveau_pci_softc {
67	device_t		sc_dev;
68	struct pci_attach_args	sc_pa;
69	struct lwp		*sc_task_thread;
70	struct nouveau_pci_task_head sc_tasks;
71	struct workqueue	*sc_task_wq;
72	struct drm_device	*sc_drm_dev;
73	struct pci_dev		sc_pci_dev;
74	struct nvkm_device	*sc_nv_dev;
75	bool			sc_pci_attached;
76	bool			sc_nvdev_inited;
77	bool			sc_dev_registered;
78};
79
80static int	nouveau_pci_match(device_t, cfdata_t, void *);
81static void	nouveau_pci_attach(device_t, device_t, void *);
82static void	nouveau_pci_attach_real(device_t);
83static int	nouveau_pci_detach(device_t, int);
84
85static bool	nouveau_pci_suspend(device_t, const pmf_qual_t *);
86static bool	nouveau_pci_resume(device_t, const pmf_qual_t *);
87
88static void	nouveau_pci_task_work(struct work *, void *);
89
90CFATTACH_DECL_NEW(nouveau_pci, sizeof(struct nouveau_pci_softc),
91    nouveau_pci_match, nouveau_pci_attach, nouveau_pci_detach, NULL);
92
93/* Kludge to get this from nouveau_drm.c.  */
94extern struct drm_driver *const nouveau_drm_driver_pci;
95
96static int
97nouveau_pci_match(device_t parent, cfdata_t match, void *aux)
98{
99	const struct pci_attach_args *const pa = aux;
100	struct pci_dev pdev;
101	struct nvkm_device *device;
102	int ret;
103
104	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA &&
105	    PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA_SGS)
106		return 0;
107
108	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
109		return 0;
110
111	/*
112	 * NetBSD drm2/5.6 doesn't support Ampere (GTX 30 series) based cards:
113	 *   0x2080-0x20ff 	GA100
114	 *   0x2200-0x227f 	GA102
115	 *   0x2300-0x237f 	GA103
116	 *   0x2480-0x24ff 	GA104
117	 *   0x2500-0x257f 	GA106
118	 *   0x2580-0x25ff 	GA107
119	 *
120	 * TU116 (GTX 16xx) occupies the space from 0x2180-0x21ff.
121	 */
122	if (PCI_PRODUCT(pa->pa_id) >= 0x1fff && PCI_PRODUCT(pa->pa_id) < 0x2180)
123		return 0;
124	if (PCI_PRODUCT(pa->pa_id) >= 0x21ff)
125		return 0;
126
127	linux_pci_dev_init(&pdev, parent /* XXX bogus */, parent, pa, 0);
128	ret = nvkm_device_pci_new(&pdev, NULL, "error",
129	    /* detect */ true, /* mmio */ false, /* subdev_mask */ 0, &device);
130	if (ret == 0)		/* don't want to hang onto it */
131		nvkm_device_del(&device);
132	linux_pci_dev_destroy(&pdev);
133	if (ret)		/* failure */
134		return 0;
135
136	return 6;		/* XXX Beat genfb_pci...  */
137}
138
139extern char *nouveau_config;
140extern char *nouveau_debug;
141
142static void
143nouveau_pci_attach(device_t parent, device_t self, void *aux)
144{
145	struct nouveau_pci_softc *const sc = device_private(self);
146	const struct pci_attach_args *const pa = aux;
147	int error;
148
149	pci_aprint_devinfo(pa, NULL);
150
151	/* Initialize the Linux PCI device descriptor.  */
152	linux_pci_dev_init(&sc->sc_pci_dev, self, device_parent(self), pa, 0);
153
154	sc->sc_dev = self;
155	sc->sc_pa = *pa;
156	sc->sc_task_thread = NULL;
157	SIMPLEQ_INIT(&sc->sc_tasks);
158	error = workqueue_create(&sc->sc_task_wq, "nouveau_pci",
159	    &nouveau_pci_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE);
160	if (error) {
161		aprint_error_dev(self, "unable to create workqueue: %d\n",
162		    error);
163		sc->sc_task_wq = NULL;
164		return;
165	}
166
167#ifdef FDT
168	/*
169	 * XXX Remove the simple framebuffer, assuming that this device
170	 * will take over.
171	 */
172	const char *fb_compatible[] = { "simple-framebuffer", NULL };
173	fdt_remove_bycompat(fb_compatible);
174#endif
175
176	/*
177	 * Defer the remainder of initialization until we have mounted
178	 * the root file system and can load firmware images.
179	 */
180	config_mountroot(self, &nouveau_pci_attach_real);
181}
182
183static void
184nouveau_pci_attach_real(device_t self)
185{
186	struct nouveau_pci_softc *const sc = device_private(self);
187	int error;
188
189	/*
190	 * Cause any tasks issued synchronously during attach to be
191	 * processed at the end of this function.
192	 */
193	sc->sc_task_thread = curlwp;
194
195	/* XXX errno Linux->NetBSD */
196	error = -nvkm_device_pci_new(&sc->sc_pci_dev,
197	    nouveau_config, nouveau_debug,
198	    /* detect */ true, /* mmio */ true, /* subdev_mask */ ~0ULL,
199	    &sc->sc_nv_dev);
200	if (error) {
201		aprint_error_dev(self, "unable to create nouveau device: %d\n",
202		    error);
203		sc->sc_nv_dev = NULL;
204		goto out;
205	}
206
207	sc->sc_drm_dev = drm_dev_alloc(nouveau_drm_driver_pci, self);
208	if (IS_ERR(sc->sc_drm_dev)) {
209		aprint_error_dev(self, "unable to create drm device: %ld\n",
210		    PTR_ERR(sc->sc_drm_dev));
211		sc->sc_drm_dev = NULL;
212		goto out;
213	}
214
215	/* XXX errno Linux->NetBSD */
216	error = -drm_pci_attach(sc->sc_drm_dev, &sc->sc_pci_dev);
217	if (error) {
218		aprint_error_dev(self, "unable to attach drm: %d\n", error);
219		goto out;
220	}
221	sc->sc_pci_attached = true;
222
223	/* XXX errno Linux->NetBSD */
224	error = -nouveau_drm_device_init(sc->sc_drm_dev);
225	if (error) {
226		aprint_error_dev(self, "unable to init nouveau: %d\n", error);
227		goto out;
228	}
229	sc->sc_nvdev_inited = true;
230
231	/* XXX errno Linux->NetBSD */
232	error = -drm_dev_register(sc->sc_drm_dev, 0);
233	if (error) {
234		aprint_error_dev(self, "unable to register drm: %d\n", error);
235		goto out;
236	}
237	sc->sc_dev_registered = true;
238
239	if (!pmf_device_register(self, &nouveau_pci_suspend,
240		&nouveau_pci_resume))
241		aprint_error_dev(self, "unable to establish power handler\n");
242
243	while (!SIMPLEQ_EMPTY(&sc->sc_tasks)) {
244		struct nouveau_pci_task *const task =
245		    SIMPLEQ_FIRST(&sc->sc_tasks);
246
247		SIMPLEQ_REMOVE_HEAD(&sc->sc_tasks, nt_u.queue);
248		(*task->nt_fn)(task);
249	}
250
251out:	/* Cause any subesquent tasks to be processed by the workqueue.  */
252	atomic_store_relaxed(&sc->sc_task_thread, NULL);
253}
254
255static int
256nouveau_pci_detach(device_t self, int flags)
257{
258	struct nouveau_pci_softc *const sc = device_private(self);
259	int error;
260
261	/* XXX Check for in-use before tearing it all down...  */
262	error = config_detach_children(self, flags);
263	if (error)
264		return error;
265
266	pmf_device_deregister(self);
267	if (sc->sc_dev_registered)
268		drm_dev_unregister(sc->sc_drm_dev);
269	if (sc->sc_nvdev_inited)
270		nouveau_drm_device_fini(sc->sc_drm_dev);
271	if (sc->sc_pci_attached)
272		drm_pci_detach(sc->sc_drm_dev);
273	if (sc->sc_drm_dev) {
274		drm_dev_put(sc->sc_drm_dev);
275		sc->sc_drm_dev = NULL;
276	}
277	if (sc->sc_nv_dev)
278		nvkm_device_del(&sc->sc_nv_dev);
279	if (sc->sc_task_wq) {
280		workqueue_destroy(sc->sc_task_wq);
281		sc->sc_task_wq = NULL;
282	}
283	linux_pci_dev_destroy(&sc->sc_pci_dev);
284
285	return 0;
286}
287
288/*
289 * XXX Synchronize with nouveau_do_suspend in nouveau_drm.c.
290 */
291static bool
292nouveau_pci_suspend(device_t self, const pmf_qual_t *qual __unused)
293{
294	struct nouveau_pci_softc *const sc = device_private(self);
295
296	return nouveau_pmops_suspend(sc->sc_drm_dev) == 0;
297}
298
299static bool
300nouveau_pci_resume(device_t self, const pmf_qual_t *qual)
301{
302	struct nouveau_pci_softc *const sc = device_private(self);
303
304	return nouveau_pmops_resume(sc->sc_drm_dev) == 0;
305}
306
307static void
308nouveau_pci_task_work(struct work *work, void *cookie __unused)
309{
310	struct nouveau_pci_task *const task = container_of(work,
311	    struct nouveau_pci_task, nt_u.work);
312
313	(*task->nt_fn)(task);
314}
315
316int
317nouveau_pci_task_schedule(device_t self, struct nouveau_pci_task *task)
318{
319	struct nouveau_pci_softc *const sc = device_private(self);
320
321	if (atomic_load_relaxed(&sc->sc_task_thread) == curlwp)
322		SIMPLEQ_INSERT_TAIL(&sc->sc_tasks, task, nt_u.queue);
323	else
324		workqueue_enqueue(sc->sc_task_wq, &task->nt_u.work, NULL);
325
326	return 0;
327}
328
329extern struct drm_driver *const nouveau_drm_driver_stub; /* XXX */
330extern struct drm_driver *const nouveau_drm_driver_pci;	 /* XXX */
331
332static int
333nouveau_pci_modcmd(modcmd_t cmd, void *arg __unused)
334{
335
336	switch (cmd) {
337	case MODULE_CMD_INIT:
338		*nouveau_drm_driver_pci = *nouveau_drm_driver_stub;
339		nouveau_drm_driver_pci->request_irq = drm_pci_request_irq;
340		nouveau_drm_driver_pci->free_irq = drm_pci_free_irq;
341#if 0		/* XXX nouveau acpi */
342		nouveau_register_dsm_handler();
343#endif
344		break;
345	case MODULE_CMD_FINI:
346#if 0		/* XXX nouveau acpi */
347		nouveau_unregister_dsm_handler();
348#endif
349		break;
350	default:
351		return ENOTTY;
352	}
353
354	return 0;
355}
356