radeon_pci.c revision 1.3 1 /* $NetBSD: radeon_pci.c,v 1.3 2014/07/26 07:32:18 riastradh 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.3 2014/07/26 07:32:18 riastradh 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 };
86
87 struct radeon_device *
88 radeon_device_private(device_t self)
89 {
90 struct radeon_softc *const sc = device_private(self);
91
92 return sc->sc_drm_dev->dev_private;
93 }
94
95 static bool radeon_pci_lookup(const struct pci_attach_args *,
96 unsigned long *);
97
98 static int radeon_match(device_t, cfdata_t, void *);
99 static void radeon_attach(device_t, device_t, void *);
100 static void radeon_attach_real(device_t);
101 static int radeon_detach(device_t, int);
102
103 static void radeon_task_work(struct work *, void *);
104
105 CFATTACH_DECL_NEW(radeondrmkms, sizeof(struct radeon_softc),
106 radeon_match, radeon_attach, radeon_detach, NULL);
107
108 /* XXX Kludge to get these from radeon_drv.c. */
109 extern struct drm_driver *const radeon_drm_driver;
110 extern const struct pci_device_id *const radeon_device_ids;
111 extern const size_t radeon_n_device_ids;
112
113 static bool
114 radeon_pci_lookup(const struct pci_attach_args *pa, unsigned long *flags)
115 {
116 size_t i;
117
118 for (i = 0; i < radeon_n_device_ids; i++) {
119 if ((PCI_VENDOR(pa->pa_id) == radeon_device_ids[i].vendor) &&
120 (PCI_PRODUCT(pa->pa_id) == radeon_device_ids[i].device))
121 break;
122 }
123
124 /* Did we find it? */
125 if (i == radeon_n_device_ids)
126 return false;
127
128 if (flags)
129 *flags = radeon_device_ids[i].driver_data;
130 return true;
131 }
132
133 static int
134 radeon_match(device_t parent, cfdata_t match, void *aux)
135 {
136 extern int radeon_guarantee_initialized(void);
137 const struct pci_attach_args *const pa = aux;
138 int error;
139
140 error = radeon_guarantee_initialized();
141 if (error) {
142 aprint_error("radeon: failed to initialize: %d\n", error);
143 return 0;
144 }
145
146 if (!radeon_pci_lookup(pa, NULL))
147 return 0;
148
149 return 6; /* XXX Beat genfb_pci... */
150 }
151
152 static void
153 radeon_attach(device_t parent, device_t self, void *aux)
154 {
155 struct radeon_softc *const sc = device_private(self);
156 const struct pci_attach_args *const pa = aux;
157
158 pci_aprint_devinfo(pa, NULL);
159
160 /*
161 * Trivial initialization first; the rest will come after we
162 * have mounted the root file system and can load firmware
163 * images.
164 */
165 sc->sc_dev = NULL;
166 sc->sc_pa = *pa;
167
168 config_mountroot(self, &radeon_attach_real);
169 }
170
171 static void
172 radeon_attach_real(device_t self)
173 {
174 struct radeon_softc *const sc = device_private(self);
175 const struct pci_attach_args *const pa = &sc->sc_pa;
176 bool ok __diagused;
177 unsigned long flags;
178 int error;
179
180 ok = radeon_pci_lookup(pa, &flags);
181 KASSERT(ok);
182
183 sc->sc_task_state = RADEON_TASK_ATTACH;
184 SIMPLEQ_INIT(&sc->sc_task_u.attach);
185
186 /* XXX errno Linux->NetBSD */
187 error = -drm_pci_attach(self, pa, &sc->sc_pci_dev, radeon_drm_driver,
188 flags, &sc->sc_drm_dev);
189 if (error) {
190 aprint_error_dev(self, "unable to attach drm: %d\n", error);
191 goto out;
192 }
193
194 while (!SIMPLEQ_EMPTY(&sc->sc_task_u.attach)) {
195 struct radeon_task *const task =
196 SIMPLEQ_FIRST(&sc->sc_task_u.attach);
197
198 SIMPLEQ_REMOVE_HEAD(&sc->sc_task_u.attach, rt_u.queue);
199 (*task->rt_fn)(task);
200 }
201
202 sc->sc_task_state = RADEON_TASK_WORKQUEUE;
203 error = workqueue_create(&sc->sc_task_u.workqueue, "radeonfb",
204 &radeon_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE);
205 if (error) {
206 aprint_error_dev(self, "unable to create workqueue: %d\n",
207 error);
208 sc->sc_task_u.workqueue = NULL;
209 goto out;
210 }
211
212 out: sc->sc_dev = self;
213 }
214
215 static int
216 radeon_detach(device_t self, int flags)
217 {
218 struct radeon_softc *const sc = device_private(self);
219 int error;
220
221 if (sc->sc_dev == NULL)
222 /* Not done attaching. */
223 return EBUSY;
224
225 /* XXX Check for in-use before tearing it all down... */
226 error = config_detach_children(self, flags);
227 if (error)
228 return error;
229
230 if (sc->sc_task_state == RADEON_TASK_ATTACH)
231 return 0;
232 if (sc->sc_task_u.workqueue != NULL) {
233 workqueue_destroy(sc->sc_task_u.workqueue);
234 sc->sc_task_u.workqueue = NULL;
235 }
236
237 if (sc->sc_drm_dev == NULL)
238 return 0;
239 /* XXX errno Linux->NetBSD */
240 error = -drm_pci_detach(sc->sc_drm_dev, flags);
241 if (error)
242 /* XXX Kinda too late to fail now... */
243 return error;
244 sc->sc_drm_dev = NULL;
245
246 return 0;
247 }
248
249 static void
250 radeon_task_work(struct work *work, void *cookie __unused)
251 {
252 struct radeon_task *const task = container_of(work, struct radeon_task,
253 rt_u.work);
254
255 (*task->rt_fn)(task);
256 }
257
258 int
259 radeon_task_schedule(device_t self, struct radeon_task *task)
260 {
261 struct radeon_softc *const sc = device_private(self);
262
263 switch (sc->sc_task_state) {
264 case RADEON_TASK_ATTACH:
265 SIMPLEQ_INSERT_TAIL(&sc->sc_task_u.attach, task, rt_u.queue);
266 return 0;
267 case RADEON_TASK_WORKQUEUE:
268 if (sc->sc_task_u.workqueue == NULL) {
269 aprint_error_dev(self, "unable to schedule task\n");
270 return EIO;
271 }
272 workqueue_enqueue(sc->sc_task_u.workqueue, &task->rt_u.work,
273 NULL);
274 return 0;
275 default:
276 panic("radeon in invalid task state: %d\n",
277 (int)sc->sc_task_state);
278 }
279 }
280