nouveau_pci.c revision 1.10 1 1.10 mrg /* $NetBSD: nouveau_pci.c,v 1.10 2018/05/31 09:18:31 mrg 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.10 mrg __KERNEL_RCSID(0, "$NetBSD: nouveau_pci.c,v 1.10 2018/05/31 09:18:31 mrg Exp $");
34 1.1 riastrad
35 1.1 riastrad #include <sys/types.h>
36 1.1 riastrad #include <sys/device.h>
37 1.1 riastrad #include <sys/queue.h>
38 1.1 riastrad #include <sys/workqueue.h>
39 1.4 jmcneill #include <sys/module.h>
40 1.1 riastrad
41 1.1 riastrad #include <drm/drmP.h>
42 1.1 riastrad
43 1.3 riastrad #include <engine/device.h>
44 1.3 riastrad
45 1.1 riastrad #include "nouveau_drm.h"
46 1.1 riastrad #include "nouveau_pci.h"
47 1.1 riastrad
48 1.4 jmcneill MODULE(MODULE_CLASS_DRIVER, nouveau_pci, "nouveau,drmkms_pci");
49 1.1 riastrad
50 1.4 jmcneill SIMPLEQ_HEAD(nouveau_pci_task_head, nouveau_pci_task);
51 1.4 jmcneill
52 1.4 jmcneill struct nouveau_pci_softc {
53 1.1 riastrad device_t sc_dev;
54 1.1 riastrad enum {
55 1.1 riastrad NOUVEAU_TASK_ATTACH,
56 1.1 riastrad NOUVEAU_TASK_WORKQUEUE,
57 1.1 riastrad } sc_task_state;
58 1.1 riastrad union {
59 1.1 riastrad struct workqueue *workqueue;
60 1.4 jmcneill struct nouveau_pci_task_head attach;
61 1.1 riastrad } sc_task_u;
62 1.1 riastrad struct drm_device *sc_drm_dev;
63 1.1 riastrad struct pci_dev sc_pci_dev;
64 1.3 riastrad struct nouveau_device *sc_nv_dev;
65 1.1 riastrad };
66 1.1 riastrad
67 1.4 jmcneill static int nouveau_pci_match(device_t, cfdata_t, void *);
68 1.4 jmcneill static void nouveau_pci_attach(device_t, device_t, void *);
69 1.4 jmcneill static int nouveau_pci_detach(device_t, int);
70 1.1 riastrad
71 1.4 jmcneill static bool nouveau_pci_suspend(device_t, const pmf_qual_t *);
72 1.4 jmcneill static bool nouveau_pci_resume(device_t, const pmf_qual_t *);
73 1.1 riastrad
74 1.4 jmcneill static void nouveau_pci_task_work(struct work *, void *);
75 1.1 riastrad
76 1.4 jmcneill CFATTACH_DECL_NEW(nouveau_pci, sizeof(struct nouveau_pci_softc),
77 1.4 jmcneill nouveau_pci_match, nouveau_pci_attach, nouveau_pci_detach, NULL);
78 1.1 riastrad
79 1.1 riastrad /* Kludge to get this from nouveau_drm.c. */
80 1.1 riastrad extern struct drm_driver *const nouveau_drm_driver;
81 1.1 riastrad
82 1.1 riastrad static int
83 1.4 jmcneill nouveau_pci_match(device_t parent, cfdata_t match, void *aux)
84 1.1 riastrad {
85 1.1 riastrad const struct pci_attach_args *const pa = aux;
86 1.1 riastrad
87 1.1 riastrad if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA &&
88 1.1 riastrad PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA_SGS)
89 1.1 riastrad return 0;
90 1.1 riastrad
91 1.1 riastrad if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
92 1.1 riastrad return 0;
93 1.1 riastrad
94 1.9 mrg #define IS_BETWEEN(x,y) \
95 1.9 mrg (PCI_PRODUCT(pa->pa_id) >= (x) && PCI_PRODUCT(pa->pa_id) <= (y))
96 1.9 mrg
97 1.9 mrg /*
98 1.9 mrg * NetBSD drm2 doesn't support Pascal-based cards:
99 1.9 mrg * 0x1580-0x15ff GP100
100 1.9 mrg * 0x1b00-0x1b7f GP102
101 1.9 mrg * 0x1b80-0x1bff GP104
102 1.9 mrg * 0x1c00-0x1c7f GP106
103 1.9 mrg * 0x1c80-0x1cff GP107
104 1.9 mrg * 0x1d00-0x1d7f GP108
105 1.9 mrg * 0x1d80-0x1dff GV100
106 1.9 mrg */
107 1.9 mrg
108 1.9 mrg if (IS_BETWEEN(0x1580, 0x15ff) ||
109 1.9 mrg IS_BETWEEN(0x1b00, 0x1b7f) ||
110 1.9 mrg IS_BETWEEN(0x1b80, 0x1bff) ||
111 1.9 mrg IS_BETWEEN(0x1c00, 0x1c7f) ||
112 1.9 mrg IS_BETWEEN(0x1c80, 0x1cff) ||
113 1.9 mrg IS_BETWEEN(0x1d00, 0x1d7f) ||
114 1.9 mrg IS_BETWEEN(0x1d80, 0x1dff))
115 1.9 mrg return 0;
116 1.9 mrg #undef IS_BETWEEN
117 1.9 mrg
118 1.10 mrg #define IS_BETWEEN(x,y) \
119 1.10 mrg (PCI_PRODUCT(pa->pa_id) >= (x) && PCI_PRODUCT(pa->pa_id) <= (y))
120 1.10 mrg /*
121 1.10 mrg * NetBSD drm2 needs missing-so-far firmware for Maxwell-based cards:
122 1.10 mrg * 0x1380-0x13bf GM107
123 1.10 mrg */
124 1.10 mrg if (IS_BETWEEN(0x1380, 0x13bf))
125 1.10 mrg return 0;
126 1.10 mrg
127 1.10 mrg /*
128 1.10 mrg * NetBSD drm2 doesn't support Pascal-based cards:
129 1.10 mrg * 0x1580-0x15ff GP100
130 1.10 mrg * 0x1b00-0x1b7f GP102
131 1.10 mrg * 0x1b80-0x1bff GP104
132 1.10 mrg * 0x1c00-0x1c7f GP106
133 1.10 mrg * 0x1c80-0x1cff GP107
134 1.10 mrg * 0x1d00-0x1d7f GP108
135 1.10 mrg * 0x1d80-0x1dff GV100
136 1.10 mrg */
137 1.10 mrg
138 1.10 mrg if (IS_BETWEEN(0x1580, 0x15ff) ||
139 1.10 mrg IS_BETWEEN(0x1b00, 0x1b7f) ||
140 1.10 mrg IS_BETWEEN(0x1b80, 0x1bff) ||
141 1.10 mrg IS_BETWEEN(0x1c00, 0x1c7f) ||
142 1.10 mrg IS_BETWEEN(0x1c80, 0x1cff) ||
143 1.10 mrg IS_BETWEEN(0x1d00, 0x1d7f) ||
144 1.10 mrg IS_BETWEEN(0x1d80, 0x1dff))
145 1.10 mrg return 0;
146 1.10 mrg #undef IS_BETWEEN
147 1.10 mrg
148 1.1 riastrad return 6; /* XXX Beat genfb_pci... */
149 1.1 riastrad }
150 1.1 riastrad
151 1.3 riastrad extern char *nouveau_config;
152 1.3 riastrad extern char *nouveau_debug;
153 1.3 riastrad
154 1.1 riastrad static void
155 1.4 jmcneill nouveau_pci_attach(device_t parent, device_t self, void *aux)
156 1.1 riastrad {
157 1.4 jmcneill struct nouveau_pci_softc *const sc = device_private(self);
158 1.1 riastrad const struct pci_attach_args *const pa = aux;
159 1.8 mrg uint64_t devname;
160 1.8 mrg int error;
161 1.1 riastrad
162 1.1 riastrad pci_aprint_devinfo(pa, NULL);
163 1.1 riastrad
164 1.1 riastrad sc->sc_dev = self;
165 1.1 riastrad
166 1.4 jmcneill if (!pmf_device_register(self, &nouveau_pci_suspend,
167 1.4 jmcneill &nouveau_pci_resume))
168 1.1 riastrad aprint_error_dev(self, "unable to establish power handler\n");
169 1.1 riastrad
170 1.1 riastrad sc->sc_task_state = NOUVEAU_TASK_ATTACH;
171 1.1 riastrad SIMPLEQ_INIT(&sc->sc_task_u.attach);
172 1.1 riastrad
173 1.3 riastrad devname = (uint64_t)device_unit(device_parent(self)) << 32;
174 1.3 riastrad devname |= pa->pa_bus << 16;
175 1.3 riastrad /* XXX errno Linux->NetBSD */
176 1.3 riastrad error = -nouveau_device_create(&sc->sc_pci_dev, NOUVEAU_BUS_PCI,
177 1.3 riastrad devname, device_xname(self), nouveau_config, nouveau_debug,
178 1.3 riastrad &sc->sc_nv_dev);
179 1.3 riastrad if (error) {
180 1.3 riastrad aprint_error_dev(self, "unable to create nouveau device: %d\n",
181 1.3 riastrad error);
182 1.3 riastrad return;
183 1.3 riastrad }
184 1.3 riastrad
185 1.1 riastrad /* XXX errno Linux->NetBSD */
186 1.1 riastrad error = -drm_pci_attach(self, pa, &sc->sc_pci_dev, nouveau_drm_driver,
187 1.1 riastrad 0, &sc->sc_drm_dev);
188 1.1 riastrad if (error) {
189 1.1 riastrad aprint_error_dev(self, "unable to attach drm: %d\n", error);
190 1.1 riastrad return;
191 1.1 riastrad }
192 1.1 riastrad
193 1.1 riastrad while (!SIMPLEQ_EMPTY(&sc->sc_task_u.attach)) {
194 1.4 jmcneill struct nouveau_pci_task *const task =
195 1.1 riastrad SIMPLEQ_FIRST(&sc->sc_task_u.attach);
196 1.1 riastrad
197 1.1 riastrad SIMPLEQ_REMOVE_HEAD(&sc->sc_task_u.attach, nt_u.queue);
198 1.1 riastrad (*task->nt_fn)(task);
199 1.1 riastrad }
200 1.1 riastrad
201 1.1 riastrad sc->sc_task_state = NOUVEAU_TASK_WORKQUEUE;
202 1.4 jmcneill error = workqueue_create(&sc->sc_task_u.workqueue, "nouveau_pci",
203 1.4 jmcneill &nouveau_pci_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE);
204 1.1 riastrad if (error) {
205 1.1 riastrad aprint_error_dev(self, "unable to create workqueue: %d\n",
206 1.1 riastrad error);
207 1.1 riastrad sc->sc_task_u.workqueue = NULL;
208 1.1 riastrad return;
209 1.1 riastrad }
210 1.1 riastrad }
211 1.1 riastrad
212 1.1 riastrad static int
213 1.4 jmcneill nouveau_pci_detach(device_t self, int flags)
214 1.1 riastrad {
215 1.4 jmcneill struct nouveau_pci_softc *const sc = device_private(self);
216 1.1 riastrad int error;
217 1.1 riastrad
218 1.1 riastrad /* XXX Check for in-use before tearing it all down... */
219 1.1 riastrad error = config_detach_children(self, flags);
220 1.1 riastrad if (error)
221 1.1 riastrad return error;
222 1.1 riastrad
223 1.1 riastrad if (sc->sc_task_state == NOUVEAU_TASK_ATTACH)
224 1.3 riastrad goto out0;
225 1.1 riastrad if (sc->sc_task_u.workqueue != NULL) {
226 1.1 riastrad workqueue_destroy(sc->sc_task_u.workqueue);
227 1.1 riastrad sc->sc_task_u.workqueue = NULL;
228 1.1 riastrad }
229 1.1 riastrad
230 1.3 riastrad if (sc->sc_nv_dev == NULL)
231 1.3 riastrad goto out0;
232 1.3 riastrad
233 1.1 riastrad if (sc->sc_drm_dev == NULL)
234 1.3 riastrad goto out1;
235 1.1 riastrad /* XXX errno Linux->NetBSD */
236 1.1 riastrad error = -drm_pci_detach(sc->sc_drm_dev, flags);
237 1.1 riastrad if (error)
238 1.1 riastrad /* XXX Kinda too late to fail now... */
239 1.1 riastrad return error;
240 1.1 riastrad sc->sc_drm_dev = NULL;
241 1.1 riastrad
242 1.3 riastrad out1: nouveau_object_ref(NULL, (void *)&sc->sc_nv_dev);
243 1.3 riastrad out0: pmf_device_deregister(self);
244 1.1 riastrad return 0;
245 1.1 riastrad }
246 1.1 riastrad
247 1.1 riastrad /*
248 1.1 riastrad * XXX Synchronize with nouveau_do_suspend in nouveau_drm.c.
249 1.1 riastrad */
250 1.1 riastrad static bool
251 1.4 jmcneill nouveau_pci_suspend(device_t self, const pmf_qual_t *qual __unused)
252 1.1 riastrad {
253 1.7 riastrad struct nouveau_pci_softc *const sc = device_private(self);
254 1.1 riastrad
255 1.7 riastrad return nouveau_pmops_suspend(sc->sc_drm_dev) == 0;
256 1.1 riastrad }
257 1.1 riastrad
258 1.1 riastrad static bool
259 1.4 jmcneill nouveau_pci_resume(device_t self, const pmf_qual_t *qual)
260 1.1 riastrad {
261 1.7 riastrad struct nouveau_pci_softc *const sc = device_private(self);
262 1.1 riastrad
263 1.7 riastrad return nouveau_pmops_resume(sc->sc_drm_dev) == 0;
264 1.1 riastrad }
265 1.1 riastrad
266 1.1 riastrad static void
267 1.4 jmcneill nouveau_pci_task_work(struct work *work, void *cookie __unused)
268 1.1 riastrad {
269 1.4 jmcneill struct nouveau_pci_task *const task = container_of(work,
270 1.4 jmcneill struct nouveau_pci_task, nt_u.work);
271 1.1 riastrad
272 1.1 riastrad (*task->nt_fn)(task);
273 1.1 riastrad }
274 1.1 riastrad
275 1.1 riastrad int
276 1.4 jmcneill nouveau_pci_task_schedule(device_t self, struct nouveau_pci_task *task)
277 1.1 riastrad {
278 1.4 jmcneill struct nouveau_pci_softc *const sc = device_private(self);
279 1.1 riastrad
280 1.1 riastrad switch (sc->sc_task_state) {
281 1.1 riastrad case NOUVEAU_TASK_ATTACH:
282 1.1 riastrad SIMPLEQ_INSERT_TAIL(&sc->sc_task_u.attach, task, nt_u.queue);
283 1.1 riastrad return 0;
284 1.1 riastrad case NOUVEAU_TASK_WORKQUEUE:
285 1.1 riastrad if (sc->sc_task_u.workqueue == NULL) {
286 1.1 riastrad aprint_error_dev(self, "unable to schedule task\n");
287 1.1 riastrad return EIO;
288 1.1 riastrad }
289 1.1 riastrad workqueue_enqueue(sc->sc_task_u.workqueue, &task->nt_u.work,
290 1.1 riastrad NULL);
291 1.1 riastrad return 0;
292 1.1 riastrad default:
293 1.1 riastrad panic("nouveau in invalid task state: %d\n",
294 1.1 riastrad (int)sc->sc_task_state);
295 1.1 riastrad }
296 1.1 riastrad }
297 1.4 jmcneill
298 1.4 jmcneill static int
299 1.4 jmcneill nouveau_pci_modcmd(modcmd_t cmd, void *arg __unused)
300 1.4 jmcneill {
301 1.4 jmcneill int error;
302 1.4 jmcneill
303 1.4 jmcneill switch (cmd) {
304 1.4 jmcneill case MODULE_CMD_INIT:
305 1.4 jmcneill error = drm_pci_init(nouveau_drm_driver, NULL);
306 1.4 jmcneill if (error) {
307 1.4 jmcneill aprint_error("nouveau_pci: failed to init: %d\n",
308 1.4 jmcneill error);
309 1.4 jmcneill return error;
310 1.4 jmcneill }
311 1.4 jmcneill #if 0 /* XXX nouveau acpi */
312 1.4 jmcneill nouveau_register_dsm_handler();
313 1.4 jmcneill #endif
314 1.4 jmcneill break;
315 1.4 jmcneill case MODULE_CMD_FINI:
316 1.4 jmcneill #if 0 /* XXX nouveau acpi */
317 1.4 jmcneill nouveau_unregister_dsm_handler();
318 1.4 jmcneill #endif
319 1.4 jmcneill drm_pci_exit(nouveau_drm_driver, NULL);
320 1.4 jmcneill break;
321 1.4 jmcneill default:
322 1.4 jmcneill return ENOTTY;
323 1.4 jmcneill }
324 1.4 jmcneill
325 1.4 jmcneill return 0;
326 1.4 jmcneill }
327