radeondrmkmsfb.c revision 1.3.4.1 1 1.3.4.1 martin /* $NetBSD: radeondrmkmsfb.c,v 1.3.4.1 2014/11/28 10:03:03 martin 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
33 1.1 riastrad #include <sys/cdefs.h>
34 1.3.4.1 martin __KERNEL_RCSID(0, "$NetBSD: radeondrmkmsfb.c,v 1.3.4.1 2014/11/28 10:03:03 martin Exp $");
35 1.1 riastrad
36 1.1 riastrad #ifdef _KERNEL_OPT
37 1.1 riastrad #include "vga.h"
38 1.1 riastrad #endif
39 1.1 riastrad
40 1.1 riastrad #include <sys/types.h>
41 1.1 riastrad #include <sys/device.h>
42 1.1 riastrad
43 1.1 riastrad #include <dev/pci/pciio.h>
44 1.1 riastrad #include <dev/pci/pcireg.h>
45 1.1 riastrad #include <dev/pci/pcivar.h>
46 1.1 riastrad
47 1.1 riastrad #include <dev/pci/wsdisplay_pci.h>
48 1.1 riastrad #include <dev/wsfb/genfbvar.h>
49 1.1 riastrad
50 1.1 riastrad #if NVGA > 0
51 1.1 riastrad /*
52 1.1 riastrad * XXX All we really need is vga_is_console from vgavar.h, but the
53 1.1 riastrad * header files are missing their own dependencies, so we need to
54 1.1 riastrad * explicitly drag in the other crap.
55 1.1 riastrad */
56 1.1 riastrad #include <dev/ic/mc6845reg.h>
57 1.1 riastrad #include <dev/ic/pcdisplayvar.h>
58 1.1 riastrad #include <dev/ic/vgareg.h>
59 1.1 riastrad #include <dev/ic/vgavar.h>
60 1.1 riastrad #endif
61 1.1 riastrad
62 1.1 riastrad #include <drm/drmP.h>
63 1.1 riastrad #include <drm/drm_fb_helper.h>
64 1.1 riastrad
65 1.1 riastrad #include <radeon.h>
66 1.1 riastrad #include "radeon_drv.h"
67 1.1 riastrad #include "radeon_task.h"
68 1.1 riastrad #include "radeondrmkmsfb.h"
69 1.1 riastrad
70 1.1 riastrad struct radeonfb_softc {
71 1.1 riastrad /* XXX genfb requires the genfb_softc to be first. */
72 1.1 riastrad struct genfb_softc sc_genfb;
73 1.1 riastrad device_t sc_dev;
74 1.1 riastrad struct radeonfb_attach_args sc_rfa;
75 1.1 riastrad struct radeon_task sc_setconfig_task;
76 1.1 riastrad bool sc_scheduled:1;
77 1.1 riastrad bool sc_attached:1;
78 1.1 riastrad };
79 1.1 riastrad
80 1.1 riastrad static int radeonfb_match(device_t, cfdata_t, void *);
81 1.1 riastrad static void radeonfb_attach(device_t, device_t, void *);
82 1.1 riastrad static int radeonfb_detach(device_t, int);
83 1.1 riastrad
84 1.1 riastrad static void radeonfb_setconfig_task(struct radeon_task *);
85 1.1 riastrad
86 1.1 riastrad static int radeonfb_genfb_ioctl(void *, void *, unsigned long, void *,
87 1.1 riastrad int, struct lwp *);
88 1.1 riastrad static paddr_t radeonfb_genfb_mmap(void *, void *, off_t, int);
89 1.1 riastrad static int radeonfb_genfb_enable_polling(void *);
90 1.1 riastrad static int radeonfb_genfb_disable_polling(void *);
91 1.1 riastrad
92 1.1 riastrad CFATTACH_DECL_NEW(radeondrmkmsfb, sizeof(struct radeonfb_softc),
93 1.1 riastrad radeonfb_match, radeonfb_attach, radeonfb_detach, NULL);
94 1.1 riastrad
95 1.1 riastrad static int
96 1.1 riastrad radeonfb_match(device_t parent, cfdata_t match, void *aux)
97 1.1 riastrad {
98 1.1 riastrad
99 1.1 riastrad return 1;
100 1.1 riastrad }
101 1.1 riastrad
102 1.1 riastrad static void
103 1.1 riastrad radeonfb_attach(device_t parent, device_t self, void *aux)
104 1.1 riastrad {
105 1.1 riastrad struct radeonfb_softc *const sc = device_private(self);
106 1.1 riastrad const struct radeonfb_attach_args *const rfa = aux;
107 1.1 riastrad int error;
108 1.1 riastrad
109 1.1 riastrad sc->sc_dev = self;
110 1.1 riastrad sc->sc_rfa = *rfa;
111 1.1 riastrad sc->sc_scheduled = false;
112 1.1 riastrad sc->sc_attached = false;
113 1.1 riastrad
114 1.2 riastrad aprint_naive("\n");
115 1.2 riastrad aprint_normal("\n");
116 1.2 riastrad
117 1.1 riastrad radeon_task_init(&sc->sc_setconfig_task, &radeonfb_setconfig_task);
118 1.1 riastrad error = radeon_task_schedule(parent, &sc->sc_setconfig_task);
119 1.1 riastrad if (error) {
120 1.1 riastrad aprint_error_dev(self, "failed to schedule mode set: %d\n",
121 1.1 riastrad error);
122 1.1 riastrad goto fail0;
123 1.1 riastrad }
124 1.1 riastrad sc->sc_scheduled = true;
125 1.1 riastrad
126 1.1 riastrad /* Success! */
127 1.1 riastrad return;
128 1.1 riastrad
129 1.1 riastrad fail0: return;
130 1.1 riastrad }
131 1.1 riastrad
132 1.1 riastrad static int
133 1.1 riastrad radeonfb_detach(device_t self, int flags)
134 1.1 riastrad {
135 1.1 riastrad struct radeonfb_softc *const sc = device_private(self);
136 1.1 riastrad
137 1.1 riastrad if (sc->sc_scheduled)
138 1.1 riastrad return EBUSY;
139 1.1 riastrad
140 1.1 riastrad if (sc->sc_attached) {
141 1.1 riastrad /* XXX genfb detach? Help? */
142 1.1 riastrad sc->sc_attached = false;
143 1.1 riastrad }
144 1.1 riastrad
145 1.1 riastrad return 0;
146 1.1 riastrad }
147 1.1 riastrad
148 1.1 riastrad static void
149 1.1 riastrad radeonfb_setconfig_task(struct radeon_task *task)
150 1.1 riastrad {
151 1.1 riastrad struct radeonfb_softc *const sc = container_of(task,
152 1.1 riastrad struct radeonfb_softc, sc_setconfig_task);
153 1.1 riastrad const prop_dictionary_t dict = device_properties(sc->sc_dev);
154 1.1 riastrad const struct radeonfb_attach_args *const rfa = &sc->sc_rfa;
155 1.1 riastrad const struct drm_fb_helper_surface_size *const sizes =
156 1.1 riastrad &rfa->rfa_fb_sizes;
157 1.1 riastrad enum { CONS_VGA, CONS_GENFB, CONS_NONE } what_was_cons;
158 1.1 riastrad static const struct genfb_ops zero_genfb_ops;
159 1.1 riastrad struct genfb_ops genfb_ops = zero_genfb_ops;
160 1.1 riastrad int error;
161 1.1 riastrad
162 1.1 riastrad KASSERT(sc->sc_scheduled);
163 1.1 riastrad
164 1.1 riastrad /* XXX Ugh... Pass these parameters some other way! */
165 1.1 riastrad prop_dictionary_set_uint32(dict, "width", sizes->fb_width);
166 1.1 riastrad prop_dictionary_set_uint32(dict, "height", sizes->fb_height);
167 1.1 riastrad prop_dictionary_set_uint8(dict, "depth", sizes->surface_bpp);
168 1.3.4.1 martin prop_dictionary_set_uint16(dict, "linebytes", rfa->rfa_fb_linebytes);
169 1.1 riastrad prop_dictionary_set_uint32(dict, "address", 0); /* XXX >32-bit */
170 1.1 riastrad CTASSERT(sizeof(uintptr_t) <= sizeof(uint64_t));
171 1.1 riastrad prop_dictionary_set_uint64(dict, "virtual_address",
172 1.1 riastrad (uint64_t)(uintptr_t)rfa->rfa_fb_ptr);
173 1.1 riastrad
174 1.1 riastrad /* XXX Whattakludge! */
175 1.1 riastrad #if NVGA > 0
176 1.1 riastrad if (vga_is_console(rfa->rfa_fb_helper->dev->pdev->pd_pa.pa_iot, -1)) {
177 1.1 riastrad what_was_cons = CONS_VGA;
178 1.1 riastrad prop_dictionary_set_bool(dict, "is_console", true);
179 1.1 riastrad vga_cndetach();
180 1.1 riastrad } else
181 1.1 riastrad #endif
182 1.1 riastrad if (genfb_is_console() && genfb_is_enabled()) {
183 1.1 riastrad what_was_cons = CONS_GENFB;
184 1.1 riastrad prop_dictionary_set_bool(dict, "is_console", true);
185 1.1 riastrad } else {
186 1.1 riastrad what_was_cons = CONS_NONE;
187 1.1 riastrad prop_dictionary_set_bool(dict, "is_console", false);
188 1.1 riastrad }
189 1.1 riastrad
190 1.1 riastrad sc->sc_genfb.sc_dev = sc->sc_dev;
191 1.1 riastrad genfb_init(&sc->sc_genfb);
192 1.1 riastrad genfb_ops.genfb_ioctl = radeonfb_genfb_ioctl;
193 1.1 riastrad genfb_ops.genfb_mmap = radeonfb_genfb_mmap;
194 1.1 riastrad genfb_ops.genfb_enable_polling = radeonfb_genfb_enable_polling;
195 1.1 riastrad genfb_ops.genfb_disable_polling = radeonfb_genfb_disable_polling;
196 1.1 riastrad
197 1.1 riastrad error = genfb_attach(&sc->sc_genfb, &genfb_ops);
198 1.1 riastrad if (error) {
199 1.1 riastrad aprint_error_dev(sc->sc_dev, "failed to attach genfb: %d\n",
200 1.1 riastrad error);
201 1.1 riastrad goto fail0;
202 1.1 riastrad }
203 1.1 riastrad sc->sc_attached = true;
204 1.1 riastrad
205 1.1 riastrad drm_fb_helper_set_config(sc->sc_rfa.rfa_fb_helper);
206 1.1 riastrad
207 1.1 riastrad /* Success! */
208 1.1 riastrad sc->sc_scheduled = false;
209 1.1 riastrad return;
210 1.1 riastrad
211 1.1 riastrad fail0: /* XXX Restore console... */
212 1.1 riastrad switch (what_was_cons) {
213 1.1 riastrad case CONS_VGA:
214 1.1 riastrad break;
215 1.1 riastrad case CONS_GENFB:
216 1.1 riastrad break;
217 1.1 riastrad case CONS_NONE:
218 1.1 riastrad break;
219 1.1 riastrad default:
220 1.1 riastrad break;
221 1.1 riastrad }
222 1.1 riastrad }
223 1.1 riastrad
224 1.1 riastrad static int
225 1.1 riastrad radeonfb_genfb_ioctl(void *v, void *vs, unsigned long cmd, void *data, int flag,
226 1.1 riastrad struct lwp *l)
227 1.1 riastrad {
228 1.1 riastrad struct genfb_softc *const genfb = v;
229 1.1 riastrad struct radeonfb_softc *const sc = container_of(genfb,
230 1.1 riastrad struct radeonfb_softc, sc_genfb);
231 1.1 riastrad struct drm_device *const dev = sc->sc_rfa.rfa_fb_helper->dev;
232 1.1 riastrad const struct pci_attach_args *const pa = &dev->pdev->pd_pa;
233 1.1 riastrad
234 1.1 riastrad switch (cmd) {
235 1.1 riastrad case WSDISPLAYIO_GTYPE:
236 1.1 riastrad *(unsigned int *)data = WSDISPLAY_TYPE_PCIVGA;
237 1.1 riastrad return 0;
238 1.1 riastrad
239 1.1 riastrad /* PCI config read/write passthrough. */
240 1.1 riastrad case PCI_IOC_CFGREAD:
241 1.1 riastrad case PCI_IOC_CFGWRITE:
242 1.1 riastrad return pci_devioctl(pa->pa_pc, pa->pa_tag, cmd, data, flag, l);
243 1.1 riastrad
244 1.1 riastrad case WSDISPLAYIO_GET_BUSID:
245 1.1 riastrad return wsdisplayio_busid_pci(dev->dev, pa->pa_pc, pa->pa_tag,
246 1.1 riastrad data);
247 1.1 riastrad
248 1.1 riastrad default:
249 1.1 riastrad return EPASSTHROUGH;
250 1.1 riastrad }
251 1.1 riastrad }
252 1.1 riastrad
253 1.1 riastrad static paddr_t
254 1.1 riastrad radeonfb_genfb_mmap(void *v, void *vs, off_t offset, int prot)
255 1.1 riastrad {
256 1.1 riastrad struct genfb_softc *const genfb = v;
257 1.1 riastrad struct radeonfb_softc *const sc = container_of(genfb,
258 1.1 riastrad struct radeonfb_softc, sc_genfb);
259 1.1 riastrad struct drm_fb_helper *const helper = sc->sc_rfa.rfa_fb_helper;
260 1.1 riastrad struct drm_framebuffer *const fb = helper->fb;
261 1.1 riastrad struct radeon_framebuffer *const rfb = container_of(fb,
262 1.1 riastrad struct radeon_framebuffer, base);
263 1.1 riastrad struct drm_gem_object *const gobj = rfb->obj;
264 1.1 riastrad struct radeon_bo *const rbo = gem_to_radeon_bo(gobj);
265 1.1 riastrad struct drm_device *const dev = helper->dev;
266 1.1 riastrad const struct pci_attach_args *const pa = &dev->pdev->pd_pa;
267 1.1 riastrad unsigned int i;
268 1.1 riastrad
269 1.1 riastrad if (offset < 0)
270 1.1 riastrad return -1;
271 1.1 riastrad
272 1.1 riastrad /* Treat low memory as the framebuffer itself. */
273 1.1 riastrad if (offset < genfb->sc_fbsize) {
274 1.1 riastrad const unsigned num_pages __diagused = rbo->tbo.num_pages;
275 1.1 riastrad int flags = 0;
276 1.1 riastrad
277 1.1 riastrad KASSERT(genfb->sc_fbsize == (num_pages << PAGE_SHIFT));
278 1.3 riastrad KASSERT(rbo->tbo.mem.bus.is_iomem);
279 1.3 riastrad
280 1.3 riastrad if (ISSET(rbo->tbo.mem.placement, TTM_PL_FLAG_WC))
281 1.1 riastrad flags |= BUS_SPACE_MAP_PREFETCHABLE;
282 1.3 riastrad
283 1.3 riastrad return bus_space_mmap(rbo->tbo.bdev->memt,
284 1.3 riastrad rbo->tbo.mem.bus.base, rbo->tbo.mem.bus.offset + offset,
285 1.3 riastrad prot, flags);
286 1.1 riastrad }
287 1.1 riastrad
288 1.1 riastrad /* XXX Cargo-culted from genfb_pci. */
289 1.1 riastrad if (kauth_authorize_machdep(kauth_cred_get(),
290 1.1 riastrad KAUTH_MACHDEP_UNMANAGEDMEM, NULL, NULL, NULL, NULL) != 0) {
291 1.1 riastrad aprint_normal_dev(dev->dev, "mmap at %"PRIxMAX" rejected\n",
292 1.1 riastrad (uintmax_t)offset);
293 1.1 riastrad return -1;
294 1.1 riastrad }
295 1.1 riastrad
296 1.1 riastrad for (i = 0; PCI_BAR(i) <= PCI_MAPREG_ROM; i++) {
297 1.1 riastrad pcireg_t type;
298 1.1 riastrad bus_addr_t addr;
299 1.1 riastrad bus_size_t size;
300 1.1 riastrad int flags;
301 1.1 riastrad
302 1.1 riastrad /* Interrogate the BAR. */
303 1.1 riastrad if (!pci_mapreg_probe(pa->pa_pc, pa->pa_tag, PCI_BAR(i),
304 1.1 riastrad &type))
305 1.1 riastrad continue;
306 1.1 riastrad if (PCI_MAPREG_TYPE(type) != PCI_MAPREG_TYPE_MEM)
307 1.1 riastrad continue;
308 1.1 riastrad if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, PCI_BAR(i), type,
309 1.1 riastrad &addr, &size, &flags))
310 1.1 riastrad continue;
311 1.1 riastrad
312 1.1 riastrad /* Try to map it if it's in range. */
313 1.1 riastrad if ((addr <= offset) && (offset < (addr + size)))
314 1.1 riastrad return bus_space_mmap(pa->pa_memt, addr,
315 1.1 riastrad (offset - addr), prot, flags);
316 1.1 riastrad
317 1.1 riastrad /* Skip a slot if this was a 64-bit BAR. */
318 1.1 riastrad if ((PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_MEM) &&
319 1.1 riastrad (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT))
320 1.1 riastrad i += 1;
321 1.1 riastrad }
322 1.1 riastrad
323 1.1 riastrad /* Failure! */
324 1.1 riastrad return -1;
325 1.1 riastrad }
326 1.1 riastrad
327 1.1 riastrad static int
328 1.1 riastrad radeonfb_genfb_enable_polling(void *cookie)
329 1.1 riastrad {
330 1.1 riastrad struct genfb_softc *const genfb = cookie;
331 1.1 riastrad struct radeonfb_softc *const sc = container_of(genfb,
332 1.1 riastrad struct radeonfb_softc, sc_genfb);
333 1.1 riastrad
334 1.1 riastrad return drm_fb_helper_debug_enter_fb(sc->sc_rfa.rfa_fb_helper);
335 1.1 riastrad }
336 1.1 riastrad
337 1.1 riastrad static int
338 1.1 riastrad radeonfb_genfb_disable_polling(void *cookie)
339 1.1 riastrad {
340 1.1 riastrad struct genfb_softc *const genfb = cookie;
341 1.1 riastrad struct radeonfb_softc *const sc = container_of(genfb,
342 1.1 riastrad struct radeonfb_softc, sc_genfb);
343 1.1 riastrad
344 1.1 riastrad return drm_fb_helper_debug_leave_fb(sc->sc_rfa.rfa_fb_helper);
345 1.1 riastrad }
346