radeon_pci.c revision 1.5 1 /* $NetBSD: radeon_pci.c,v 1.5 2015/02/14 06:58:12 mrg Exp $ */
2
3 /*-
4 * Copyright (c) 2014 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: radeon_pci.c,v 1.5 2015/02/14 06:58:12 mrg Exp $");
34
35 #ifdef _KERNEL_OPT
36 #include "vga.h"
37 #endif
38
39 #include <sys/types.h>
40 #include <sys/queue.h>
41 #include <sys/systm.h>
42 #include <sys/workqueue.h>
43
44 #include <dev/pci/pciio.h>
45 #include <dev/pci/pcireg.h>
46 #include <dev/pci/pcivar.h>
47
48 #include <dev/pci/wsdisplay_pci.h>
49 #include <dev/wsfb/genfbvar.h>
50
51 #if NVGA > 0
52 /*
53 * XXX All we really need is vga_is_console from vgavar.h, but the
54 * header files are missing their own dependencies, so we need to
55 * explicitly drag in the other crap.
56 */
57 #include <dev/ic/mc6845reg.h>
58 #include <dev/ic/pcdisplayvar.h>
59 #include <dev/ic/vgareg.h>
60 #include <dev/ic/vgavar.h>
61 #endif
62
63 #include <drm/drmP.h>
64 #include <drm/drm_fb_helper.h>
65
66 #include <radeon.h>
67 #include "radeon_drv.h"
68 #include "radeon_task.h"
69
70 SIMPLEQ_HEAD(radeon_task_head, radeon_task);
71
72 struct radeon_softc {
73 device_t sc_dev;
74 struct pci_attach_args sc_pa;
75 enum {
76 RADEON_TASK_ATTACH,
77 RADEON_TASK_WORKQUEUE,
78 } sc_task_state;
79 union {
80 struct workqueue *workqueue;
81 struct radeon_task_head attach;
82 } sc_task_u;
83 struct drm_device *sc_drm_dev;
84 struct pci_dev sc_pci_dev;
85 #ifdef __i386__
86 #define RADEON_PCI_UGLY_MAP_HACK
87 /* XXX Used to claim the VGA device before attach_real */
88 bus_space_handle_t sc_temp_memh;
89 bool sc_temp_set;
90 #endif
91 };
92
93 struct radeon_device *
94 radeon_device_private(device_t self)
95 {
96 struct radeon_softc *const sc = device_private(self);
97
98 return sc->sc_drm_dev->dev_private;
99 }
100
101 static bool radeon_pci_lookup(const struct pci_attach_args *,
102 unsigned long *);
103
104 static int radeon_match(device_t, cfdata_t, void *);
105 static void radeon_attach(device_t, device_t, void *);
106 static void radeon_attach_real(device_t);
107 static int radeon_detach(device_t, int);
108
109 static void radeon_task_work(struct work *, void *);
110
111 CFATTACH_DECL_NEW(radeon, sizeof(struct radeon_softc),
112 radeon_match, radeon_attach, radeon_detach, NULL);
113
114 /* XXX Kludge to get these from radeon_drv.c. */
115 extern struct drm_driver *const radeon_drm_driver;
116 extern const struct pci_device_id *const radeon_device_ids;
117 extern const size_t radeon_n_device_ids;
118
119 static bool
120 radeon_pci_lookup(const struct pci_attach_args *pa, unsigned long *flags)
121 {
122 size_t i;
123
124 for (i = 0; i < radeon_n_device_ids; i++) {
125 if ((PCI_VENDOR(pa->pa_id) == radeon_device_ids[i].vendor) &&
126 (PCI_PRODUCT(pa->pa_id) == radeon_device_ids[i].device))
127 break;
128 }
129
130 /* Did we find it? */
131 if (i == radeon_n_device_ids)
132 return false;
133
134 if (flags)
135 *flags = radeon_device_ids[i].driver_data;
136 return true;
137 }
138
139 static int
140 radeon_match(device_t parent, cfdata_t match, void *aux)
141 {
142 extern int radeon_guarantee_initialized(void);
143 const struct pci_attach_args *const pa = aux;
144 int error;
145
146 error = radeon_guarantee_initialized();
147 if (error) {
148 aprint_error("radeon: failed to initialize: %d\n", error);
149 return 0;
150 }
151
152 if (!radeon_pci_lookup(pa, NULL))
153 return 0;
154
155 return 6; /* XXX Beat genfb_pci... */
156 }
157
158 static void
159 radeon_attach(device_t parent, device_t self, void *aux)
160 {
161 struct radeon_softc *const sc = device_private(self);
162 const struct pci_attach_args *const pa = aux;
163
164 pci_aprint_devinfo(pa, NULL);
165
166 /*
167 * Trivial initialization first; the rest will come after we
168 * have mounted the root file system and can load firmware
169 * images.
170 */
171 sc->sc_dev = NULL;
172 sc->sc_pa = *pa;
173
174 #ifdef RADEON_PCI_UGLY_MAP_HACK
175 /*
176 * XXX
177 * We map the VGA registers, so that other driver don't
178 * think they can. This stops vga@isa or pcdisplay@isa
179 * attaching, and stealing wsdisplay0. Yuck.
180 */
181 sc->sc_temp_set = bus_space_map(pa->pa_memt, 0xb0000, 0x10000, 0,
182 &sc->sc_temp_memh);
183 #endif
184
185 config_mountroot(self, &radeon_attach_real);
186 }
187
188 static void
189 radeon_attach_real(device_t self)
190 {
191 struct radeon_softc *const sc = device_private(self);
192 const struct pci_attach_args *const pa = &sc->sc_pa;
193 bool ok __diagused;
194 unsigned long flags;
195 int error;
196
197 ok = radeon_pci_lookup(pa, &flags);
198 KASSERT(ok);
199
200 #ifdef RADEON_PCI_UGLY_MAP_HACK
201 /*
202 * XXX
203 * Unmap the VGA registers so the DRM code can map them.
204 */
205 if (sc->sc_temp_set)
206 bus_space_unmap(pa->pa_memt, sc->sc_temp_memh, 0x10000);
207 #endif
208
209 sc->sc_task_state = RADEON_TASK_ATTACH;
210 SIMPLEQ_INIT(&sc->sc_task_u.attach);
211
212 /* XXX errno Linux->NetBSD */
213 error = -drm_pci_attach(self, pa, &sc->sc_pci_dev, radeon_drm_driver,
214 flags, &sc->sc_drm_dev);
215 if (error) {
216 aprint_error_dev(self, "unable to attach drm: %d\n", error);
217 goto out;
218 }
219
220 while (!SIMPLEQ_EMPTY(&sc->sc_task_u.attach)) {
221 struct radeon_task *const task =
222 SIMPLEQ_FIRST(&sc->sc_task_u.attach);
223
224 SIMPLEQ_REMOVE_HEAD(&sc->sc_task_u.attach, rt_u.queue);
225 (*task->rt_fn)(task);
226 }
227
228 sc->sc_task_state = RADEON_TASK_WORKQUEUE;
229 error = workqueue_create(&sc->sc_task_u.workqueue, "radeonfb",
230 &radeon_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE);
231 if (error) {
232 aprint_error_dev(self, "unable to create workqueue: %d\n",
233 error);
234 sc->sc_task_u.workqueue = NULL;
235 goto out;
236 }
237
238 out: sc->sc_dev = self;
239 }
240
241 static int
242 radeon_detach(device_t self, int flags)
243 {
244 struct radeon_softc *const sc = device_private(self);
245 int error;
246
247 if (sc->sc_dev == NULL)
248 /* Not done attaching. */
249 return EBUSY;
250
251 /* XXX Check for in-use before tearing it all down... */
252 error = config_detach_children(self, flags);
253 if (error)
254 return error;
255
256 if (sc->sc_task_state == RADEON_TASK_ATTACH)
257 return 0;
258 if (sc->sc_task_u.workqueue != NULL) {
259 workqueue_destroy(sc->sc_task_u.workqueue);
260 sc->sc_task_u.workqueue = NULL;
261 }
262
263 if (sc->sc_drm_dev == NULL)
264 return 0;
265 /* XXX errno Linux->NetBSD */
266 error = -drm_pci_detach(sc->sc_drm_dev, flags);
267 if (error)
268 /* XXX Kinda too late to fail now... */
269 return error;
270 sc->sc_drm_dev = NULL;
271
272 return 0;
273 }
274
275 static void
276 radeon_task_work(struct work *work, void *cookie __unused)
277 {
278 struct radeon_task *const task = container_of(work, struct radeon_task,
279 rt_u.work);
280
281 (*task->rt_fn)(task);
282 }
283
284 int
285 radeon_task_schedule(device_t self, struct radeon_task *task)
286 {
287 struct radeon_softc *const sc = device_private(self);
288
289 switch (sc->sc_task_state) {
290 case RADEON_TASK_ATTACH:
291 SIMPLEQ_INSERT_TAIL(&sc->sc_task_u.attach, task, rt_u.queue);
292 return 0;
293 case RADEON_TASK_WORKQUEUE:
294 if (sc->sc_task_u.workqueue == NULL) {
295 aprint_error_dev(self, "unable to schedule task\n");
296 return EIO;
297 }
298 workqueue_enqueue(sc->sc_task_u.workqueue, &task->rt_u.work,
299 NULL);
300 return 0;
301 default:
302 panic("radeon in invalid task state: %d\n",
303 (int)sc->sc_task_state);
304 }
305 }
306