radeon_pci.c revision 1.10.18.1 1 1.10.18.1 christos /* $NetBSD: radeon_pci.c,v 1.10.18.1 2019/06/10 22:08:33 christos Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2014 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.18.1 christos __KERNEL_RCSID(0, "$NetBSD: radeon_pci.c,v 1.10.18.1 2019/06/10 22:08:33 christos Exp $");
34 1.1 riastrad
35 1.1 riastrad #ifdef _KERNEL_OPT
36 1.1 riastrad #include "vga.h"
37 1.1 riastrad #endif
38 1.1 riastrad
39 1.1 riastrad #include <sys/types.h>
40 1.1 riastrad #include <sys/queue.h>
41 1.1 riastrad #include <sys/systm.h>
42 1.1 riastrad #include <sys/workqueue.h>
43 1.1 riastrad
44 1.1 riastrad #include <dev/pci/pciio.h>
45 1.1 riastrad #include <dev/pci/pcireg.h>
46 1.1 riastrad #include <dev/pci/pcivar.h>
47 1.1 riastrad
48 1.1 riastrad #include <dev/pci/wsdisplay_pci.h>
49 1.1 riastrad #include <dev/wsfb/genfbvar.h>
50 1.1 riastrad
51 1.1 riastrad #if NVGA > 0
52 1.1 riastrad /*
53 1.1 riastrad * XXX All we really need is vga_is_console from vgavar.h, but the
54 1.1 riastrad * header files are missing their own dependencies, so we need to
55 1.1 riastrad * explicitly drag in the other crap.
56 1.1 riastrad */
57 1.1 riastrad #include <dev/ic/mc6845reg.h>
58 1.1 riastrad #include <dev/ic/pcdisplayvar.h>
59 1.1 riastrad #include <dev/ic/vgareg.h>
60 1.1 riastrad #include <dev/ic/vgavar.h>
61 1.1 riastrad #endif
62 1.1 riastrad
63 1.1 riastrad #include <drm/drmP.h>
64 1.1 riastrad #include <drm/drm_fb_helper.h>
65 1.1 riastrad
66 1.1 riastrad #include <radeon.h>
67 1.1 riastrad #include "radeon_drv.h"
68 1.2 riastrad #include "radeon_task.h"
69 1.1 riastrad
70 1.2 riastrad SIMPLEQ_HEAD(radeon_task_head, radeon_task);
71 1.1 riastrad
72 1.1 riastrad struct radeon_softc {
73 1.1 riastrad device_t sc_dev;
74 1.3 riastrad struct pci_attach_args sc_pa;
75 1.2 riastrad enum {
76 1.2 riastrad RADEON_TASK_ATTACH,
77 1.2 riastrad RADEON_TASK_WORKQUEUE,
78 1.2 riastrad } sc_task_state;
79 1.2 riastrad union {
80 1.2 riastrad struct workqueue *workqueue;
81 1.2 riastrad struct radeon_task_head attach;
82 1.2 riastrad } sc_task_u;
83 1.1 riastrad struct drm_device *sc_drm_dev;
84 1.1 riastrad struct pci_dev sc_pci_dev;
85 1.7 mrg #if defined(__i386__)
86 1.5 mrg #define RADEON_PCI_UGLY_MAP_HACK
87 1.5 mrg /* XXX Used to claim the VGA device before attach_real */
88 1.5 mrg bus_space_handle_t sc_temp_memh;
89 1.5 mrg bool sc_temp_set;
90 1.5 mrg #endif
91 1.1 riastrad };
92 1.1 riastrad
93 1.2 riastrad struct radeon_device *
94 1.2 riastrad radeon_device_private(device_t self)
95 1.2 riastrad {
96 1.2 riastrad struct radeon_softc *const sc = device_private(self);
97 1.2 riastrad
98 1.2 riastrad return sc->sc_drm_dev->dev_private;
99 1.2 riastrad }
100 1.1 riastrad
101 1.1 riastrad static bool radeon_pci_lookup(const struct pci_attach_args *,
102 1.1 riastrad unsigned long *);
103 1.1 riastrad
104 1.1 riastrad static int radeon_match(device_t, cfdata_t, void *);
105 1.1 riastrad static void radeon_attach(device_t, device_t, void *);
106 1.3 riastrad static void radeon_attach_real(device_t);
107 1.1 riastrad static int radeon_detach(device_t, int);
108 1.8 mrg static bool radeon_do_suspend(device_t, const pmf_qual_t *);
109 1.8 mrg static bool radeon_do_resume(device_t, const pmf_qual_t *);
110 1.1 riastrad
111 1.2 riastrad static void radeon_task_work(struct work *, void *);
112 1.1 riastrad
113 1.4 riastrad CFATTACH_DECL_NEW(radeon, sizeof(struct radeon_softc),
114 1.1 riastrad radeon_match, radeon_attach, radeon_detach, NULL);
115 1.1 riastrad
116 1.1 riastrad /* XXX Kludge to get these from radeon_drv.c. */
117 1.1 riastrad extern struct drm_driver *const radeon_drm_driver;
118 1.1 riastrad extern const struct pci_device_id *const radeon_device_ids;
119 1.1 riastrad extern const size_t radeon_n_device_ids;
120 1.1 riastrad
121 1.10 mrg /* Set this to false if you want to match R100/R200 */
122 1.10 mrg bool radeon_pci_ignore_r100_r200 = true;
123 1.10 mrg
124 1.1 riastrad static bool
125 1.1 riastrad radeon_pci_lookup(const struct pci_attach_args *pa, unsigned long *flags)
126 1.1 riastrad {
127 1.1 riastrad size_t i;
128 1.10 mrg enum radeon_family fam;
129 1.1 riastrad
130 1.1 riastrad for (i = 0; i < radeon_n_device_ids; i++) {
131 1.1 riastrad if ((PCI_VENDOR(pa->pa_id) == radeon_device_ids[i].vendor) &&
132 1.1 riastrad (PCI_PRODUCT(pa->pa_id) == radeon_device_ids[i].device))
133 1.1 riastrad break;
134 1.1 riastrad }
135 1.1 riastrad
136 1.1 riastrad /* Did we find it? */
137 1.1 riastrad if (i == radeon_n_device_ids)
138 1.1 riastrad return false;
139 1.1 riastrad
140 1.10 mrg /* NetBSD drm2 fails on R100 and many R200 chipsets, disable for now */
141 1.10 mrg fam = radeon_device_ids[i].driver_data & RADEON_FAMILY_MASK;
142 1.10 mrg if (radeon_pci_ignore_r100_r200 && fam < CHIP_RV280)
143 1.10 mrg return false;
144 1.10 mrg
145 1.1 riastrad if (flags)
146 1.1 riastrad *flags = radeon_device_ids[i].driver_data;
147 1.1 riastrad return true;
148 1.1 riastrad }
149 1.1 riastrad
150 1.1 riastrad static int
151 1.1 riastrad radeon_match(device_t parent, cfdata_t match, void *aux)
152 1.1 riastrad {
153 1.1 riastrad extern int radeon_guarantee_initialized(void);
154 1.1 riastrad const struct pci_attach_args *const pa = aux;
155 1.1 riastrad int error;
156 1.1 riastrad
157 1.1 riastrad error = radeon_guarantee_initialized();
158 1.1 riastrad if (error) {
159 1.1 riastrad aprint_error("radeon: failed to initialize: %d\n", error);
160 1.1 riastrad return 0;
161 1.1 riastrad }
162 1.1 riastrad
163 1.1 riastrad if (!radeon_pci_lookup(pa, NULL))
164 1.1 riastrad return 0;
165 1.1 riastrad
166 1.1 riastrad return 6; /* XXX Beat genfb_pci... */
167 1.1 riastrad }
168 1.1 riastrad
169 1.1 riastrad static void
170 1.1 riastrad radeon_attach(device_t parent, device_t self, void *aux)
171 1.1 riastrad {
172 1.1 riastrad struct radeon_softc *const sc = device_private(self);
173 1.1 riastrad const struct pci_attach_args *const pa = aux;
174 1.3 riastrad
175 1.3 riastrad pci_aprint_devinfo(pa, NULL);
176 1.3 riastrad
177 1.8 mrg if (!pmf_device_register(self, &radeon_do_suspend, &radeon_do_resume))
178 1.8 mrg aprint_error_dev(self, "unable to establish power handler\n");
179 1.8 mrg
180 1.3 riastrad /*
181 1.3 riastrad * Trivial initialization first; the rest will come after we
182 1.3 riastrad * have mounted the root file system and can load firmware
183 1.3 riastrad * images.
184 1.3 riastrad */
185 1.3 riastrad sc->sc_dev = NULL;
186 1.3 riastrad sc->sc_pa = *pa;
187 1.3 riastrad
188 1.5 mrg #ifdef RADEON_PCI_UGLY_MAP_HACK
189 1.5 mrg /*
190 1.5 mrg * XXX
191 1.7 mrg * We try to map the VGA registers, in case we can prevent vga@isa or
192 1.7 mrg * pcdisplay@isa attaching, and stealing wsdisplay0. This only works
193 1.7 mrg * with serial console, as actual VGA console has already mapped them.
194 1.7 mrg * The only way to handle that is for vga@isa to not attach.
195 1.5 mrg */
196 1.6 mrg int rv = bus_space_map(pa->pa_memt, 0xb0000, 0x10000, 0,
197 1.6 mrg &sc->sc_temp_memh);
198 1.6 mrg sc->sc_temp_set = rv == 0;
199 1.6 mrg if (rv != 0)
200 1.6 mrg aprint_error_dev(self, "unable to reserve VGA registers for "
201 1.6 mrg "i386 radeondrmkms hack\n");
202 1.5 mrg #endif
203 1.5 mrg
204 1.3 riastrad config_mountroot(self, &radeon_attach_real);
205 1.3 riastrad }
206 1.3 riastrad
207 1.3 riastrad static void
208 1.3 riastrad radeon_attach_real(device_t self)
209 1.3 riastrad {
210 1.3 riastrad struct radeon_softc *const sc = device_private(self);
211 1.3 riastrad const struct pci_attach_args *const pa = &sc->sc_pa;
212 1.1 riastrad bool ok __diagused;
213 1.1 riastrad unsigned long flags;
214 1.1 riastrad int error;
215 1.1 riastrad
216 1.1 riastrad ok = radeon_pci_lookup(pa, &flags);
217 1.1 riastrad KASSERT(ok);
218 1.1 riastrad
219 1.5 mrg #ifdef RADEON_PCI_UGLY_MAP_HACK
220 1.5 mrg /*
221 1.5 mrg * XXX
222 1.7 mrg * Unmap the VGA registers.
223 1.5 mrg */
224 1.5 mrg if (sc->sc_temp_set)
225 1.5 mrg bus_space_unmap(pa->pa_memt, sc->sc_temp_memh, 0x10000);
226 1.5 mrg #endif
227 1.5 mrg
228 1.2 riastrad sc->sc_task_state = RADEON_TASK_ATTACH;
229 1.2 riastrad SIMPLEQ_INIT(&sc->sc_task_u.attach);
230 1.1 riastrad
231 1.10.18.1 christos /* Initialize the Linux PCI device descriptor. */
232 1.10.18.1 christos linux_pci_dev_init(&sc->sc_pci_dev, self, device_parent(self), pa, 0);
233 1.10.18.1 christos
234 1.1 riastrad /* XXX errno Linux->NetBSD */
235 1.1 riastrad error = -drm_pci_attach(self, pa, &sc->sc_pci_dev, radeon_drm_driver,
236 1.1 riastrad flags, &sc->sc_drm_dev);
237 1.1 riastrad if (error) {
238 1.1 riastrad aprint_error_dev(self, "unable to attach drm: %d\n", error);
239 1.3 riastrad goto out;
240 1.1 riastrad }
241 1.1 riastrad
242 1.2 riastrad while (!SIMPLEQ_EMPTY(&sc->sc_task_u.attach)) {
243 1.2 riastrad struct radeon_task *const task =
244 1.2 riastrad SIMPLEQ_FIRST(&sc->sc_task_u.attach);
245 1.1 riastrad
246 1.2 riastrad SIMPLEQ_REMOVE_HEAD(&sc->sc_task_u.attach, rt_u.queue);
247 1.2 riastrad (*task->rt_fn)(task);
248 1.1 riastrad }
249 1.1 riastrad
250 1.2 riastrad sc->sc_task_state = RADEON_TASK_WORKQUEUE;
251 1.2 riastrad error = workqueue_create(&sc->sc_task_u.workqueue, "radeonfb",
252 1.2 riastrad &radeon_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE);
253 1.1 riastrad if (error) {
254 1.1 riastrad aprint_error_dev(self, "unable to create workqueue: %d\n",
255 1.1 riastrad error);
256 1.2 riastrad sc->sc_task_u.workqueue = NULL;
257 1.3 riastrad goto out;
258 1.1 riastrad }
259 1.3 riastrad
260 1.3 riastrad out: sc->sc_dev = self;
261 1.1 riastrad }
262 1.1 riastrad
263 1.1 riastrad static int
264 1.1 riastrad radeon_detach(device_t self, int flags)
265 1.1 riastrad {
266 1.1 riastrad struct radeon_softc *const sc = device_private(self);
267 1.1 riastrad int error;
268 1.1 riastrad
269 1.3 riastrad if (sc->sc_dev == NULL)
270 1.3 riastrad /* Not done attaching. */
271 1.3 riastrad return EBUSY;
272 1.3 riastrad
273 1.1 riastrad /* XXX Check for in-use before tearing it all down... */
274 1.1 riastrad error = config_detach_children(self, flags);
275 1.1 riastrad if (error)
276 1.1 riastrad return error;
277 1.1 riastrad
278 1.2 riastrad if (sc->sc_task_state == RADEON_TASK_ATTACH)
279 1.9 mrg goto out;
280 1.2 riastrad if (sc->sc_task_u.workqueue != NULL) {
281 1.2 riastrad workqueue_destroy(sc->sc_task_u.workqueue);
282 1.2 riastrad sc->sc_task_u.workqueue = NULL;
283 1.2 riastrad }
284 1.1 riastrad
285 1.1 riastrad if (sc->sc_drm_dev == NULL)
286 1.9 mrg goto out;
287 1.1 riastrad /* XXX errno Linux->NetBSD */
288 1.1 riastrad error = -drm_pci_detach(sc->sc_drm_dev, flags);
289 1.1 riastrad if (error)
290 1.2 riastrad /* XXX Kinda too late to fail now... */
291 1.1 riastrad return error;
292 1.2 riastrad sc->sc_drm_dev = NULL;
293 1.1 riastrad
294 1.10.18.1 christos out: linux_pci_dev_destroy(&sc->sc_pci_dev);
295 1.10.18.1 christos pmf_device_deregister(self);
296 1.9 mrg
297 1.1 riastrad return 0;
298 1.1 riastrad }
299 1.1 riastrad
300 1.8 mrg static bool
301 1.8 mrg radeon_do_suspend(device_t self, const pmf_qual_t *qual)
302 1.8 mrg {
303 1.8 mrg struct radeon_softc *const sc = device_private(self);
304 1.8 mrg struct drm_device *const dev = sc->sc_drm_dev;
305 1.8 mrg int ret;
306 1.9 mrg bool is_console = true; /* XXX */
307 1.8 mrg
308 1.8 mrg if (dev == NULL)
309 1.8 mrg return true;
310 1.8 mrg
311 1.9 mrg ret = radeon_suspend_kms(dev, true, is_console);
312 1.8 mrg if (ret)
313 1.8 mrg return false;
314 1.8 mrg
315 1.8 mrg return true;
316 1.8 mrg }
317 1.8 mrg
318 1.8 mrg static bool
319 1.8 mrg radeon_do_resume(device_t self, const pmf_qual_t *qual)
320 1.8 mrg {
321 1.8 mrg struct radeon_softc *const sc = device_private(self);
322 1.8 mrg struct drm_device *const dev = sc->sc_drm_dev;
323 1.8 mrg int ret;
324 1.9 mrg bool is_console = true; /* XXX */
325 1.8 mrg
326 1.8 mrg if (dev == NULL)
327 1.8 mrg return true;
328 1.8 mrg
329 1.9 mrg ret = radeon_resume_kms(dev, true, is_console);
330 1.8 mrg if (ret)
331 1.8 mrg return false;
332 1.8 mrg
333 1.8 mrg return true;
334 1.8 mrg }
335 1.8 mrg
336 1.1 riastrad static void
337 1.2 riastrad radeon_task_work(struct work *work, void *cookie __unused)
338 1.1 riastrad {
339 1.2 riastrad struct radeon_task *const task = container_of(work, struct radeon_task,
340 1.2 riastrad rt_u.work);
341 1.1 riastrad
342 1.2 riastrad (*task->rt_fn)(task);
343 1.1 riastrad }
344 1.1 riastrad
345 1.2 riastrad int
346 1.2 riastrad radeon_task_schedule(device_t self, struct radeon_task *task)
347 1.1 riastrad {
348 1.2 riastrad struct radeon_softc *const sc = device_private(self);
349 1.1 riastrad
350 1.2 riastrad switch (sc->sc_task_state) {
351 1.2 riastrad case RADEON_TASK_ATTACH:
352 1.2 riastrad SIMPLEQ_INSERT_TAIL(&sc->sc_task_u.attach, task, rt_u.queue);
353 1.2 riastrad return 0;
354 1.2 riastrad case RADEON_TASK_WORKQUEUE:
355 1.2 riastrad if (sc->sc_task_u.workqueue == NULL) {
356 1.2 riastrad aprint_error_dev(self, "unable to schedule task\n");
357 1.2 riastrad return EIO;
358 1.2 riastrad }
359 1.2 riastrad workqueue_enqueue(sc->sc_task_u.workqueue, &task->rt_u.work,
360 1.2 riastrad NULL);
361 1.1 riastrad return 0;
362 1.1 riastrad default:
363 1.2 riastrad panic("radeon in invalid task state: %d\n",
364 1.2 riastrad (int)sc->sc_task_state);
365 1.1 riastrad }
366 1.1 riastrad }
367