radeondrmkmsfb.c revision 1.8 1 1.8 riastrad /* $NetBSD: radeondrmkmsfb.c,v 1.8 2018/08/27 13:36:14 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
33 1.1 riastrad #include <sys/cdefs.h>
34 1.8 riastrad __KERNEL_RCSID(0, "$NetBSD: radeondrmkmsfb.c,v 1.8 2018/08/27 13:36:14 riastradh 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.4 chs static bool radeonfb_genfb_shutdown(device_t, int);
92 1.4 chs static bool radeonfb_genfb_setmode(struct genfb_softc *, int);
93 1.4 chs
94 1.4 chs static const struct genfb_mode_callback radeonfb_genfb_mode_callback = {
95 1.4 chs .gmc_setmode = radeonfb_genfb_setmode,
96 1.4 chs };
97 1.1 riastrad
98 1.1 riastrad CFATTACH_DECL_NEW(radeondrmkmsfb, sizeof(struct radeonfb_softc),
99 1.1 riastrad radeonfb_match, radeonfb_attach, radeonfb_detach, NULL);
100 1.1 riastrad
101 1.1 riastrad static int
102 1.1 riastrad radeonfb_match(device_t parent, cfdata_t match, void *aux)
103 1.1 riastrad {
104 1.1 riastrad
105 1.1 riastrad return 1;
106 1.1 riastrad }
107 1.1 riastrad
108 1.1 riastrad static void
109 1.1 riastrad radeonfb_attach(device_t parent, device_t self, void *aux)
110 1.1 riastrad {
111 1.1 riastrad struct radeonfb_softc *const sc = device_private(self);
112 1.1 riastrad const struct radeonfb_attach_args *const rfa = aux;
113 1.1 riastrad int error;
114 1.1 riastrad
115 1.1 riastrad sc->sc_dev = self;
116 1.1 riastrad sc->sc_rfa = *rfa;
117 1.1 riastrad sc->sc_scheduled = false;
118 1.1 riastrad sc->sc_attached = false;
119 1.1 riastrad
120 1.2 riastrad aprint_naive("\n");
121 1.2 riastrad aprint_normal("\n");
122 1.2 riastrad
123 1.1 riastrad radeon_task_init(&sc->sc_setconfig_task, &radeonfb_setconfig_task);
124 1.1 riastrad error = radeon_task_schedule(parent, &sc->sc_setconfig_task);
125 1.1 riastrad if (error) {
126 1.1 riastrad aprint_error_dev(self, "failed to schedule mode set: %d\n",
127 1.1 riastrad error);
128 1.1 riastrad goto fail0;
129 1.1 riastrad }
130 1.1 riastrad sc->sc_scheduled = true;
131 1.1 riastrad
132 1.1 riastrad /* Success! */
133 1.1 riastrad return;
134 1.1 riastrad
135 1.1 riastrad fail0: return;
136 1.1 riastrad }
137 1.1 riastrad
138 1.1 riastrad static int
139 1.1 riastrad radeonfb_detach(device_t self, int flags)
140 1.1 riastrad {
141 1.1 riastrad struct radeonfb_softc *const sc = device_private(self);
142 1.1 riastrad
143 1.1 riastrad if (sc->sc_scheduled)
144 1.1 riastrad return EBUSY;
145 1.1 riastrad
146 1.1 riastrad if (sc->sc_attached) {
147 1.1 riastrad /* XXX genfb detach? Help? */
148 1.1 riastrad sc->sc_attached = false;
149 1.1 riastrad }
150 1.1 riastrad
151 1.1 riastrad return 0;
152 1.1 riastrad }
153 1.1 riastrad
154 1.1 riastrad static void
155 1.1 riastrad radeonfb_setconfig_task(struct radeon_task *task)
156 1.1 riastrad {
157 1.1 riastrad struct radeonfb_softc *const sc = container_of(task,
158 1.1 riastrad struct radeonfb_softc, sc_setconfig_task);
159 1.1 riastrad const prop_dictionary_t dict = device_properties(sc->sc_dev);
160 1.1 riastrad const struct radeonfb_attach_args *const rfa = &sc->sc_rfa;
161 1.1 riastrad const struct drm_fb_helper_surface_size *const sizes =
162 1.1 riastrad &rfa->rfa_fb_sizes;
163 1.1 riastrad enum { CONS_VGA, CONS_GENFB, CONS_NONE } what_was_cons;
164 1.1 riastrad static const struct genfb_ops zero_genfb_ops;
165 1.1 riastrad struct genfb_ops genfb_ops = zero_genfb_ops;
166 1.1 riastrad int error;
167 1.1 riastrad
168 1.1 riastrad KASSERT(sc->sc_scheduled);
169 1.1 riastrad
170 1.1 riastrad /* XXX Ugh... Pass these parameters some other way! */
171 1.6 mrg prop_dictionary_set_uint32(dict, "width", sizes->surface_width);
172 1.6 mrg prop_dictionary_set_uint32(dict, "height", sizes->surface_height);
173 1.1 riastrad prop_dictionary_set_uint8(dict, "depth", sizes->surface_bpp);
174 1.5 nonaka prop_dictionary_set_uint16(dict, "linebytes", rfa->rfa_fb_linebytes);
175 1.1 riastrad prop_dictionary_set_uint32(dict, "address", 0); /* XXX >32-bit */
176 1.1 riastrad CTASSERT(sizeof(uintptr_t) <= sizeof(uint64_t));
177 1.1 riastrad prop_dictionary_set_uint64(dict, "virtual_address",
178 1.1 riastrad (uint64_t)(uintptr_t)rfa->rfa_fb_ptr);
179 1.1 riastrad
180 1.4 chs prop_dictionary_set_uint64(dict, "mode_callback",
181 1.4 chs (uint64_t)(uintptr_t)&radeonfb_genfb_mode_callback);
182 1.4 chs
183 1.1 riastrad /* XXX Whattakludge! */
184 1.1 riastrad #if NVGA > 0
185 1.1 riastrad if (vga_is_console(rfa->rfa_fb_helper->dev->pdev->pd_pa.pa_iot, -1)) {
186 1.1 riastrad what_was_cons = CONS_VGA;
187 1.1 riastrad prop_dictionary_set_bool(dict, "is_console", true);
188 1.1 riastrad vga_cndetach();
189 1.1 riastrad } else
190 1.1 riastrad #endif
191 1.1 riastrad if (genfb_is_console() && genfb_is_enabled()) {
192 1.1 riastrad what_was_cons = CONS_GENFB;
193 1.1 riastrad prop_dictionary_set_bool(dict, "is_console", true);
194 1.1 riastrad } else {
195 1.1 riastrad what_was_cons = CONS_NONE;
196 1.1 riastrad prop_dictionary_set_bool(dict, "is_console", false);
197 1.1 riastrad }
198 1.1 riastrad
199 1.1 riastrad sc->sc_genfb.sc_dev = sc->sc_dev;
200 1.1 riastrad genfb_init(&sc->sc_genfb);
201 1.1 riastrad genfb_ops.genfb_ioctl = radeonfb_genfb_ioctl;
202 1.1 riastrad genfb_ops.genfb_mmap = radeonfb_genfb_mmap;
203 1.1 riastrad genfb_ops.genfb_enable_polling = radeonfb_genfb_enable_polling;
204 1.1 riastrad genfb_ops.genfb_disable_polling = radeonfb_genfb_disable_polling;
205 1.1 riastrad
206 1.1 riastrad error = genfb_attach(&sc->sc_genfb, &genfb_ops);
207 1.1 riastrad if (error) {
208 1.1 riastrad aprint_error_dev(sc->sc_dev, "failed to attach genfb: %d\n",
209 1.1 riastrad error);
210 1.1 riastrad goto fail0;
211 1.1 riastrad }
212 1.1 riastrad sc->sc_attached = true;
213 1.1 riastrad
214 1.7 maya if (!pmf_device_register1(sc->sc_dev, NULL, NULL,
215 1.7 maya radeonfb_genfb_shutdown))
216 1.7 maya aprint_error_dev(sc->sc_dev,
217 1.7 maya "couldn't establish power handler\n");
218 1.1 riastrad
219 1.1 riastrad /* Success! */
220 1.1 riastrad sc->sc_scheduled = false;
221 1.1 riastrad return;
222 1.1 riastrad
223 1.1 riastrad fail0: /* XXX Restore console... */
224 1.1 riastrad switch (what_was_cons) {
225 1.1 riastrad case CONS_VGA:
226 1.1 riastrad break;
227 1.1 riastrad case CONS_GENFB:
228 1.1 riastrad break;
229 1.1 riastrad case CONS_NONE:
230 1.1 riastrad break;
231 1.1 riastrad default:
232 1.1 riastrad break;
233 1.1 riastrad }
234 1.1 riastrad }
235 1.1 riastrad
236 1.1 riastrad static int
237 1.1 riastrad radeonfb_genfb_ioctl(void *v, void *vs, unsigned long cmd, void *data, int flag,
238 1.1 riastrad struct lwp *l)
239 1.1 riastrad {
240 1.1 riastrad struct genfb_softc *const genfb = v;
241 1.1 riastrad struct radeonfb_softc *const sc = container_of(genfb,
242 1.1 riastrad struct radeonfb_softc, sc_genfb);
243 1.1 riastrad struct drm_device *const dev = sc->sc_rfa.rfa_fb_helper->dev;
244 1.1 riastrad const struct pci_attach_args *const pa = &dev->pdev->pd_pa;
245 1.1 riastrad
246 1.1 riastrad switch (cmd) {
247 1.1 riastrad case WSDISPLAYIO_GTYPE:
248 1.1 riastrad *(unsigned int *)data = WSDISPLAY_TYPE_PCIVGA;
249 1.1 riastrad return 0;
250 1.1 riastrad
251 1.1 riastrad /* PCI config read/write passthrough. */
252 1.1 riastrad case PCI_IOC_CFGREAD:
253 1.1 riastrad case PCI_IOC_CFGWRITE:
254 1.1 riastrad return pci_devioctl(pa->pa_pc, pa->pa_tag, cmd, data, flag, l);
255 1.1 riastrad
256 1.1 riastrad case WSDISPLAYIO_GET_BUSID:
257 1.1 riastrad return wsdisplayio_busid_pci(dev->dev, pa->pa_pc, pa->pa_tag,
258 1.1 riastrad data);
259 1.1 riastrad
260 1.1 riastrad default:
261 1.1 riastrad return EPASSTHROUGH;
262 1.1 riastrad }
263 1.1 riastrad }
264 1.1 riastrad
265 1.1 riastrad static paddr_t
266 1.1 riastrad radeonfb_genfb_mmap(void *v, void *vs, off_t offset, int prot)
267 1.1 riastrad {
268 1.1 riastrad struct genfb_softc *const genfb = v;
269 1.1 riastrad struct radeonfb_softc *const sc = container_of(genfb,
270 1.1 riastrad struct radeonfb_softc, sc_genfb);
271 1.1 riastrad struct drm_fb_helper *const helper = sc->sc_rfa.rfa_fb_helper;
272 1.1 riastrad struct drm_framebuffer *const fb = helper->fb;
273 1.1 riastrad struct radeon_framebuffer *const rfb = container_of(fb,
274 1.1 riastrad struct radeon_framebuffer, base);
275 1.1 riastrad struct drm_gem_object *const gobj = rfb->obj;
276 1.1 riastrad struct radeon_bo *const rbo = gem_to_radeon_bo(gobj);
277 1.1 riastrad struct drm_device *const dev = helper->dev;
278 1.1 riastrad const struct pci_attach_args *const pa = &dev->pdev->pd_pa;
279 1.1 riastrad unsigned int i;
280 1.1 riastrad
281 1.1 riastrad if (offset < 0)
282 1.1 riastrad return -1;
283 1.1 riastrad
284 1.1 riastrad /* Treat low memory as the framebuffer itself. */
285 1.1 riastrad if (offset < genfb->sc_fbsize) {
286 1.1 riastrad const unsigned num_pages __diagused = rbo->tbo.num_pages;
287 1.1 riastrad int flags = 0;
288 1.1 riastrad
289 1.1 riastrad KASSERT(genfb->sc_fbsize == (num_pages << PAGE_SHIFT));
290 1.3 riastrad KASSERT(rbo->tbo.mem.bus.is_iomem);
291 1.3 riastrad
292 1.3 riastrad if (ISSET(rbo->tbo.mem.placement, TTM_PL_FLAG_WC))
293 1.1 riastrad flags |= BUS_SPACE_MAP_PREFETCHABLE;
294 1.3 riastrad
295 1.3 riastrad return bus_space_mmap(rbo->tbo.bdev->memt,
296 1.3 riastrad rbo->tbo.mem.bus.base, rbo->tbo.mem.bus.offset + offset,
297 1.3 riastrad prot, flags);
298 1.1 riastrad }
299 1.1 riastrad
300 1.1 riastrad /* XXX Cargo-culted from genfb_pci. */
301 1.1 riastrad if (kauth_authorize_machdep(kauth_cred_get(),
302 1.1 riastrad KAUTH_MACHDEP_UNMANAGEDMEM, NULL, NULL, NULL, NULL) != 0) {
303 1.1 riastrad aprint_normal_dev(dev->dev, "mmap at %"PRIxMAX" rejected\n",
304 1.1 riastrad (uintmax_t)offset);
305 1.1 riastrad return -1;
306 1.1 riastrad }
307 1.1 riastrad
308 1.1 riastrad for (i = 0; PCI_BAR(i) <= PCI_MAPREG_ROM; i++) {
309 1.1 riastrad pcireg_t type;
310 1.1 riastrad bus_addr_t addr;
311 1.1 riastrad bus_size_t size;
312 1.1 riastrad int flags;
313 1.1 riastrad
314 1.1 riastrad /* Interrogate the BAR. */
315 1.1 riastrad if (!pci_mapreg_probe(pa->pa_pc, pa->pa_tag, PCI_BAR(i),
316 1.1 riastrad &type))
317 1.1 riastrad continue;
318 1.1 riastrad if (PCI_MAPREG_TYPE(type) != PCI_MAPREG_TYPE_MEM)
319 1.1 riastrad continue;
320 1.1 riastrad if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, PCI_BAR(i), type,
321 1.1 riastrad &addr, &size, &flags))
322 1.1 riastrad continue;
323 1.1 riastrad
324 1.1 riastrad /* Try to map it if it's in range. */
325 1.1 riastrad if ((addr <= offset) && (offset < (addr + size)))
326 1.1 riastrad return bus_space_mmap(pa->pa_memt, addr,
327 1.1 riastrad (offset - addr), prot, flags);
328 1.1 riastrad
329 1.1 riastrad /* Skip a slot if this was a 64-bit BAR. */
330 1.1 riastrad if ((PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_MEM) &&
331 1.1 riastrad (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT))
332 1.1 riastrad i += 1;
333 1.1 riastrad }
334 1.1 riastrad
335 1.1 riastrad /* Failure! */
336 1.1 riastrad return -1;
337 1.1 riastrad }
338 1.1 riastrad
339 1.1 riastrad static int
340 1.1 riastrad radeonfb_genfb_enable_polling(void *cookie)
341 1.1 riastrad {
342 1.1 riastrad struct genfb_softc *const genfb = cookie;
343 1.1 riastrad struct radeonfb_softc *const sc = container_of(genfb,
344 1.1 riastrad struct radeonfb_softc, sc_genfb);
345 1.1 riastrad
346 1.1 riastrad return drm_fb_helper_debug_enter_fb(sc->sc_rfa.rfa_fb_helper);
347 1.1 riastrad }
348 1.1 riastrad
349 1.1 riastrad static int
350 1.1 riastrad radeonfb_genfb_disable_polling(void *cookie)
351 1.1 riastrad {
352 1.1 riastrad struct genfb_softc *const genfb = cookie;
353 1.1 riastrad struct radeonfb_softc *const sc = container_of(genfb,
354 1.1 riastrad struct radeonfb_softc, sc_genfb);
355 1.1 riastrad
356 1.1 riastrad return drm_fb_helper_debug_leave_fb(sc->sc_rfa.rfa_fb_helper);
357 1.1 riastrad }
358 1.4 chs
359 1.4 chs static bool
360 1.4 chs radeonfb_genfb_shutdown(device_t self, int flags)
361 1.4 chs {
362 1.4 chs genfb_enable_polling(self);
363 1.4 chs return true;
364 1.4 chs }
365 1.4 chs
366 1.4 chs static bool
367 1.4 chs radeonfb_genfb_setmode(struct genfb_softc *genfb, int mode)
368 1.4 chs {
369 1.4 chs struct radeonfb_softc *sc = (struct radeonfb_softc *)genfb;
370 1.8 riastrad struct drm_fb_helper *fb_helper = sc->sc_rfa.rfa_fb_helper;
371 1.4 chs
372 1.8 riastrad if (mode == WSDISPLAYIO_MODE_EMUL)
373 1.8 riastrad drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
374 1.4 chs
375 1.4 chs return true;
376 1.4 chs }
377