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