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