nouveau_pci.c revision 1.7
1/*	$NetBSD: nouveau_pci.c,v 1.7 2016/02/11 04:51:44 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.7 2016/02/11 04:51:44 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 <engine/device.h>
44
45#include "nouveau_drm.h"
46#include "nouveau_pci.h"
47
48MODULE(MODULE_CLASS_DRIVER, nouveau_pci, "nouveau,drmkms_pci");
49
50SIMPLEQ_HEAD(nouveau_pci_task_head, nouveau_pci_task);
51
52struct nouveau_pci_softc {
53	device_t		sc_dev;
54	struct pci_attach_args	sc_pa;
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 nouveau_device	*sc_nv_dev;
66};
67
68static int	nouveau_pci_match(device_t, cfdata_t, void *);
69static void	nouveau_pci_attach(device_t, device_t, void *);
70static void	nouveau_attach_real(device_t);
71static int	nouveau_pci_detach(device_t, int);
72
73static bool	nouveau_pci_suspend(device_t, const pmf_qual_t *);
74static bool	nouveau_pci_resume(device_t, const pmf_qual_t *);
75
76static void	nouveau_pci_task_work(struct work *, void *);
77
78CFATTACH_DECL_NEW(nouveau_pci, sizeof(struct nouveau_pci_softc),
79    nouveau_pci_match, nouveau_pci_attach, nouveau_pci_detach, NULL);
80
81/* Kludge to get this from nouveau_drm.c.  */
82extern struct drm_driver *const nouveau_drm_driver;
83
84static int
85nouveau_pci_match(device_t parent, cfdata_t match, void *aux)
86{
87	const struct pci_attach_args *const pa = aux;
88
89	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA &&
90	    PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA_SGS)
91		return 0;
92
93	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
94		return 0;
95
96	return 6;		/* XXX Beat genfb_pci...  */
97}
98
99extern char *nouveau_config;
100extern char *nouveau_debug;
101
102static void
103nouveau_pci_attach(device_t parent, device_t self, void *aux)
104{
105	struct nouveau_pci_softc *const sc = device_private(self);
106	const struct pci_attach_args *const pa = aux;
107
108	pci_aprint_devinfo(pa, NULL);
109
110	sc->sc_dev = self;
111	sc->sc_pa = *pa;
112
113	if (!pmf_device_register(self, &nouveau_pci_suspend,
114		&nouveau_pci_resume))
115		aprint_error_dev(self, "unable to establish power handler\n");
116
117	config_mountroot(self, &nouveau_attach_real);
118}
119
120static void
121nouveau_attach_real(device_t self)
122{
123	struct nouveau_pci_softc *const sc = device_private(self);
124	const struct pci_attach_args *const pa = &sc->sc_pa;
125	uint64_t devname;
126	int error;
127
128	sc->sc_task_state = NOUVEAU_TASK_ATTACH;
129	SIMPLEQ_INIT(&sc->sc_task_u.attach);
130
131	devname = (uint64_t)device_unit(device_parent(self)) << 32;
132	devname |= pa->pa_bus << 16;
133	/* XXX errno Linux->NetBSD */
134	error = -nouveau_device_create(&sc->sc_pci_dev, NOUVEAU_BUS_PCI,
135	    devname, device_xname(self), nouveau_config, nouveau_debug,
136	    &sc->sc_nv_dev);
137	if (error) {
138		aprint_error_dev(self, "unable to create nouveau device: %d\n",
139		    error);
140		return;
141	}
142
143	/* XXX errno Linux->NetBSD */
144	error = -drm_pci_attach(self, pa, &sc->sc_pci_dev, nouveau_drm_driver,
145	    0, &sc->sc_drm_dev);
146	if (error) {
147		aprint_error_dev(self, "unable to attach drm: %d\n", error);
148		return;
149	}
150
151	while (!SIMPLEQ_EMPTY(&sc->sc_task_u.attach)) {
152		struct nouveau_pci_task *const task =
153		    SIMPLEQ_FIRST(&sc->sc_task_u.attach);
154
155		SIMPLEQ_REMOVE_HEAD(&sc->sc_task_u.attach, nt_u.queue);
156		(*task->nt_fn)(task);
157	}
158
159	sc->sc_task_state = NOUVEAU_TASK_WORKQUEUE;
160	error = workqueue_create(&sc->sc_task_u.workqueue, "nouveau_pci",
161	    &nouveau_pci_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE);
162	if (error) {
163		aprint_error_dev(self, "unable to create workqueue: %d\n",
164		    error);
165		sc->sc_task_u.workqueue = NULL;
166		return;
167	}
168}
169
170static int
171nouveau_pci_detach(device_t self, int flags)
172{
173	struct nouveau_pci_softc *const sc = device_private(self);
174	int error;
175
176	/* XXX Check for in-use before tearing it all down...  */
177	error = config_detach_children(self, flags);
178	if (error)
179		return error;
180
181	if (sc->sc_task_state == NOUVEAU_TASK_ATTACH)
182		goto out0;
183	if (sc->sc_task_u.workqueue != NULL) {
184		workqueue_destroy(sc->sc_task_u.workqueue);
185		sc->sc_task_u.workqueue = NULL;
186	}
187
188	if (sc->sc_nv_dev == NULL)
189		goto out0;
190
191	if (sc->sc_drm_dev == NULL)
192		goto out1;
193	/* XXX errno Linux->NetBSD */
194	error = -drm_pci_detach(sc->sc_drm_dev, flags);
195	if (error)
196		/* XXX Kinda too late to fail now...  */
197		return error;
198	sc->sc_drm_dev = NULL;
199
200out1:	nouveau_object_ref(NULL, (void *)&sc->sc_nv_dev);
201out0:	pmf_device_deregister(self);
202	return 0;
203}
204
205/*
206 * XXX Synchronize with nouveau_do_suspend in nouveau_drm.c.
207 */
208static bool
209nouveau_pci_suspend(device_t self, const pmf_qual_t *qual __unused)
210{
211	struct nouveau_pci_softc *const sc = device_private(self);
212
213	return nouveau_pmops_suspend(sc->sc_drm_dev) == 0;
214}
215
216static bool
217nouveau_pci_resume(device_t self, const pmf_qual_t *qual)
218{
219	struct nouveau_pci_softc *const sc = device_private(self);
220
221	return nouveau_pmops_resume(sc->sc_drm_dev) == 0;
222}
223
224static void
225nouveau_pci_task_work(struct work *work, void *cookie __unused)
226{
227	struct nouveau_pci_task *const task = container_of(work,
228	    struct nouveau_pci_task, nt_u.work);
229
230	(*task->nt_fn)(task);
231}
232
233int
234nouveau_pci_task_schedule(device_t self, struct nouveau_pci_task *task)
235{
236	struct nouveau_pci_softc *const sc = device_private(self);
237
238	switch (sc->sc_task_state) {
239	case NOUVEAU_TASK_ATTACH:
240		SIMPLEQ_INSERT_TAIL(&sc->sc_task_u.attach, task, nt_u.queue);
241		return 0;
242	case NOUVEAU_TASK_WORKQUEUE:
243		if (sc->sc_task_u.workqueue == NULL) {
244			aprint_error_dev(self, "unable to schedule task\n");
245			return EIO;
246		}
247		workqueue_enqueue(sc->sc_task_u.workqueue, &task->nt_u.work,
248		    NULL);
249		return 0;
250	default:
251		panic("nouveau in invalid task state: %d\n",
252		    (int)sc->sc_task_state);
253	}
254}
255
256static int
257nouveau_pci_modcmd(modcmd_t cmd, void *arg __unused)
258{
259	int error;
260
261	switch (cmd) {
262	case MODULE_CMD_INIT:
263		error = drm_pci_init(nouveau_drm_driver, NULL);
264		if (error) {
265			aprint_error("nouveau_pci: failed to init: %d\n",
266			    error);
267			return error;
268		}
269#if 0		/* XXX nouveau acpi */
270		nouveau_register_dsm_handler();
271#endif
272		break;
273	case MODULE_CMD_FINI:
274#if 0		/* XXX nouveau acpi */
275		nouveau_unregister_dsm_handler();
276#endif
277		drm_pci_exit(nouveau_drm_driver, NULL);
278		break;
279	default:
280		return ENOTTY;
281	}
282
283	return 0;
284}
285