radeon_pci.c revision 1.7 1 1.7 mrg /* $NetBSD: radeon_pci.c,v 1.7 2015/03/01 10:07:01 mrg 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.7 mrg __KERNEL_RCSID(0, "$NetBSD: radeon_pci.c,v 1.7 2015/03/01 10:07:01 mrg 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.1 riastrad
109 1.2 riastrad static void radeon_task_work(struct work *, void *);
110 1.1 riastrad
111 1.4 riastrad CFATTACH_DECL_NEW(radeon, sizeof(struct radeon_softc),
112 1.1 riastrad radeon_match, radeon_attach, radeon_detach, NULL);
113 1.1 riastrad
114 1.1 riastrad /* XXX Kludge to get these from radeon_drv.c. */
115 1.1 riastrad extern struct drm_driver *const radeon_drm_driver;
116 1.1 riastrad extern const struct pci_device_id *const radeon_device_ids;
117 1.1 riastrad extern const size_t radeon_n_device_ids;
118 1.1 riastrad
119 1.1 riastrad static bool
120 1.1 riastrad radeon_pci_lookup(const struct pci_attach_args *pa, unsigned long *flags)
121 1.1 riastrad {
122 1.1 riastrad size_t i;
123 1.1 riastrad
124 1.1 riastrad for (i = 0; i < radeon_n_device_ids; i++) {
125 1.1 riastrad if ((PCI_VENDOR(pa->pa_id) == radeon_device_ids[i].vendor) &&
126 1.1 riastrad (PCI_PRODUCT(pa->pa_id) == radeon_device_ids[i].device))
127 1.1 riastrad break;
128 1.1 riastrad }
129 1.1 riastrad
130 1.1 riastrad /* Did we find it? */
131 1.1 riastrad if (i == radeon_n_device_ids)
132 1.1 riastrad return false;
133 1.1 riastrad
134 1.1 riastrad if (flags)
135 1.1 riastrad *flags = radeon_device_ids[i].driver_data;
136 1.1 riastrad return true;
137 1.1 riastrad }
138 1.1 riastrad
139 1.1 riastrad static int
140 1.1 riastrad radeon_match(device_t parent, cfdata_t match, void *aux)
141 1.1 riastrad {
142 1.1 riastrad extern int radeon_guarantee_initialized(void);
143 1.1 riastrad const struct pci_attach_args *const pa = aux;
144 1.1 riastrad int error;
145 1.1 riastrad
146 1.1 riastrad error = radeon_guarantee_initialized();
147 1.1 riastrad if (error) {
148 1.1 riastrad aprint_error("radeon: failed to initialize: %d\n", error);
149 1.1 riastrad return 0;
150 1.1 riastrad }
151 1.1 riastrad
152 1.1 riastrad if (!radeon_pci_lookup(pa, NULL))
153 1.1 riastrad return 0;
154 1.1 riastrad
155 1.1 riastrad return 6; /* XXX Beat genfb_pci... */
156 1.1 riastrad }
157 1.1 riastrad
158 1.1 riastrad static void
159 1.1 riastrad radeon_attach(device_t parent, device_t self, void *aux)
160 1.1 riastrad {
161 1.1 riastrad struct radeon_softc *const sc = device_private(self);
162 1.1 riastrad const struct pci_attach_args *const pa = aux;
163 1.3 riastrad
164 1.3 riastrad pci_aprint_devinfo(pa, NULL);
165 1.3 riastrad
166 1.3 riastrad /*
167 1.3 riastrad * Trivial initialization first; the rest will come after we
168 1.3 riastrad * have mounted the root file system and can load firmware
169 1.3 riastrad * images.
170 1.3 riastrad */
171 1.3 riastrad sc->sc_dev = NULL;
172 1.3 riastrad sc->sc_pa = *pa;
173 1.3 riastrad
174 1.5 mrg #ifdef RADEON_PCI_UGLY_MAP_HACK
175 1.5 mrg /*
176 1.5 mrg * XXX
177 1.7 mrg * We try to map the VGA registers, in case we can prevent vga@isa or
178 1.7 mrg * pcdisplay@isa attaching, and stealing wsdisplay0. This only works
179 1.7 mrg * with serial console, as actual VGA console has already mapped them.
180 1.7 mrg * The only way to handle that is for vga@isa to not attach.
181 1.5 mrg */
182 1.6 mrg int rv = bus_space_map(pa->pa_memt, 0xb0000, 0x10000, 0,
183 1.6 mrg &sc->sc_temp_memh);
184 1.6 mrg sc->sc_temp_set = rv == 0;
185 1.6 mrg if (rv != 0)
186 1.6 mrg aprint_error_dev(self, "unable to reserve VGA registers for "
187 1.6 mrg "i386 radeondrmkms hack\n");
188 1.5 mrg #endif
189 1.5 mrg
190 1.3 riastrad config_mountroot(self, &radeon_attach_real);
191 1.3 riastrad }
192 1.3 riastrad
193 1.3 riastrad static void
194 1.3 riastrad radeon_attach_real(device_t self)
195 1.3 riastrad {
196 1.3 riastrad struct radeon_softc *const sc = device_private(self);
197 1.3 riastrad const struct pci_attach_args *const pa = &sc->sc_pa;
198 1.1 riastrad bool ok __diagused;
199 1.1 riastrad unsigned long flags;
200 1.1 riastrad int error;
201 1.1 riastrad
202 1.1 riastrad ok = radeon_pci_lookup(pa, &flags);
203 1.1 riastrad KASSERT(ok);
204 1.1 riastrad
205 1.5 mrg #ifdef RADEON_PCI_UGLY_MAP_HACK
206 1.5 mrg /*
207 1.5 mrg * XXX
208 1.7 mrg * Unmap the VGA registers.
209 1.5 mrg */
210 1.5 mrg if (sc->sc_temp_set)
211 1.5 mrg bus_space_unmap(pa->pa_memt, sc->sc_temp_memh, 0x10000);
212 1.5 mrg #endif
213 1.5 mrg
214 1.2 riastrad sc->sc_task_state = RADEON_TASK_ATTACH;
215 1.2 riastrad SIMPLEQ_INIT(&sc->sc_task_u.attach);
216 1.1 riastrad
217 1.1 riastrad /* XXX errno Linux->NetBSD */
218 1.1 riastrad error = -drm_pci_attach(self, pa, &sc->sc_pci_dev, radeon_drm_driver,
219 1.1 riastrad flags, &sc->sc_drm_dev);
220 1.1 riastrad if (error) {
221 1.1 riastrad aprint_error_dev(self, "unable to attach drm: %d\n", error);
222 1.3 riastrad goto out;
223 1.1 riastrad }
224 1.1 riastrad
225 1.2 riastrad while (!SIMPLEQ_EMPTY(&sc->sc_task_u.attach)) {
226 1.2 riastrad struct radeon_task *const task =
227 1.2 riastrad SIMPLEQ_FIRST(&sc->sc_task_u.attach);
228 1.1 riastrad
229 1.2 riastrad SIMPLEQ_REMOVE_HEAD(&sc->sc_task_u.attach, rt_u.queue);
230 1.2 riastrad (*task->rt_fn)(task);
231 1.1 riastrad }
232 1.1 riastrad
233 1.2 riastrad sc->sc_task_state = RADEON_TASK_WORKQUEUE;
234 1.2 riastrad error = workqueue_create(&sc->sc_task_u.workqueue, "radeonfb",
235 1.2 riastrad &radeon_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE);
236 1.1 riastrad if (error) {
237 1.1 riastrad aprint_error_dev(self, "unable to create workqueue: %d\n",
238 1.1 riastrad error);
239 1.2 riastrad sc->sc_task_u.workqueue = NULL;
240 1.3 riastrad goto out;
241 1.1 riastrad }
242 1.3 riastrad
243 1.3 riastrad out: sc->sc_dev = self;
244 1.1 riastrad }
245 1.1 riastrad
246 1.1 riastrad static int
247 1.1 riastrad radeon_detach(device_t self, int flags)
248 1.1 riastrad {
249 1.1 riastrad struct radeon_softc *const sc = device_private(self);
250 1.1 riastrad int error;
251 1.1 riastrad
252 1.3 riastrad if (sc->sc_dev == NULL)
253 1.3 riastrad /* Not done attaching. */
254 1.3 riastrad return EBUSY;
255 1.3 riastrad
256 1.1 riastrad /* XXX Check for in-use before tearing it all down... */
257 1.1 riastrad error = config_detach_children(self, flags);
258 1.1 riastrad if (error)
259 1.1 riastrad return error;
260 1.1 riastrad
261 1.2 riastrad if (sc->sc_task_state == RADEON_TASK_ATTACH)
262 1.1 riastrad return 0;
263 1.2 riastrad if (sc->sc_task_u.workqueue != NULL) {
264 1.2 riastrad workqueue_destroy(sc->sc_task_u.workqueue);
265 1.2 riastrad sc->sc_task_u.workqueue = NULL;
266 1.2 riastrad }
267 1.1 riastrad
268 1.1 riastrad if (sc->sc_drm_dev == NULL)
269 1.1 riastrad return 0;
270 1.1 riastrad /* XXX errno Linux->NetBSD */
271 1.1 riastrad error = -drm_pci_detach(sc->sc_drm_dev, flags);
272 1.1 riastrad if (error)
273 1.2 riastrad /* XXX Kinda too late to fail now... */
274 1.1 riastrad return error;
275 1.2 riastrad sc->sc_drm_dev = NULL;
276 1.1 riastrad
277 1.1 riastrad return 0;
278 1.1 riastrad }
279 1.1 riastrad
280 1.1 riastrad static void
281 1.2 riastrad radeon_task_work(struct work *work, void *cookie __unused)
282 1.1 riastrad {
283 1.2 riastrad struct radeon_task *const task = container_of(work, struct radeon_task,
284 1.2 riastrad rt_u.work);
285 1.1 riastrad
286 1.2 riastrad (*task->rt_fn)(task);
287 1.1 riastrad }
288 1.1 riastrad
289 1.2 riastrad int
290 1.2 riastrad radeon_task_schedule(device_t self, struct radeon_task *task)
291 1.1 riastrad {
292 1.2 riastrad struct radeon_softc *const sc = device_private(self);
293 1.1 riastrad
294 1.2 riastrad switch (sc->sc_task_state) {
295 1.2 riastrad case RADEON_TASK_ATTACH:
296 1.2 riastrad SIMPLEQ_INSERT_TAIL(&sc->sc_task_u.attach, task, rt_u.queue);
297 1.2 riastrad return 0;
298 1.2 riastrad case RADEON_TASK_WORKQUEUE:
299 1.2 riastrad if (sc->sc_task_u.workqueue == NULL) {
300 1.2 riastrad aprint_error_dev(self, "unable to schedule task\n");
301 1.2 riastrad return EIO;
302 1.2 riastrad }
303 1.2 riastrad workqueue_enqueue(sc->sc_task_u.workqueue, &task->rt_u.work,
304 1.2 riastrad NULL);
305 1.1 riastrad return 0;
306 1.1 riastrad default:
307 1.2 riastrad panic("radeon in invalid task state: %d\n",
308 1.2 riastrad (int)sc->sc_task_state);
309 1.1 riastrad }
310 1.1 riastrad }
311