Home | History | Annotate | Line # | Download | only in nvidia
tegra_fb.c revision 1.5
      1 /* $NetBSD: tegra_fb.c,v 1.5 2021/12/19 12:44:14 riastradh Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: tegra_fb.c,v 1.5 2021/12/19 12:44:14 riastradh Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/bus.h>
     34 #include <sys/device.h>
     35 
     36 #include <drm/drm_drv.h>
     37 #include <drm/drmfb.h>
     38 
     39 #include <arm/nvidia/tegra_var.h>
     40 #include <arm/nvidia/tegra_drm.h>
     41 
     42 static int	tegra_fb_match(device_t, cfdata_t, void *);
     43 static void	tegra_fb_attach(device_t, device_t, void *);
     44 
     45 static bool	tegra_fb_shutdown(device_t, int);
     46 
     47 struct tegra_fb_softc {
     48 	struct drmfb_softc	sc_drmfb;
     49 	device_t		sc_dev;
     50 	struct tegra_drm_softc	*sc_drm;
     51 	struct tegra_drmfb_attach_args sc_tfa;
     52 	struct tegra_framebuffer *sc_fb;
     53 };
     54 
     55 static paddr_t	tegra_fb_mmapfb(struct drmfb_softc *, off_t, int);
     56 static int	tegra_fb_ioctl(struct drmfb_softc *, u_long, void *, int,
     57 			       lwp_t *);
     58 
     59 
     60 static const struct drmfb_params tegrafb_drmfb_params = {
     61 	.dp_mmapfb = tegra_fb_mmapfb,
     62 	.dp_ioctl = tegra_fb_ioctl,
     63 };
     64 
     65 CFATTACH_DECL_NEW(tegra_fb, sizeof(struct tegra_fb_softc),
     66 	tegra_fb_match, tegra_fb_attach, NULL, NULL);
     67 
     68 static int
     69 tegra_fb_match(device_t parent, cfdata_t cf, void *aux)
     70 {
     71 	return 1;
     72 }
     73 
     74 static void
     75 tegra_fb_attach(device_t parent, device_t self, void *aux)
     76 {
     77 	struct tegra_fb_softc * const sc = device_private(self);
     78 	struct tegra_drm_softc * const drmsc = device_private(parent);
     79 	struct tegra_drmfb_attach_args * const tfa = aux;
     80 	int error;
     81 
     82 	sc->sc_dev = self;
     83 	sc->sc_drm = drmsc;
     84 	sc->sc_tfa = *tfa;
     85 	sc->sc_fb = to_tegra_framebuffer(tfa->tfa_fb_helper->fb);
     86 
     87 	aprint_naive("\n");
     88 	aprint_normal("\n");
     89 
     90 	const struct drmfb_attach_args da = {
     91 		.da_dev = self,
     92 		.da_fb_helper = tfa->tfa_fb_helper,
     93 		.da_fb_sizes = &tfa->tfa_fb_sizes,
     94 		.da_fb_vaddr = sc->sc_fb->obj->vaddr,
     95 		.da_fb_linebytes = tfa->tfa_fb_linebytes,
     96 		.da_params = &tegrafb_drmfb_params,
     97 	};
     98 
     99 	error = drmfb_attach(&sc->sc_drmfb, &da);
    100 	if (error) {
    101 		aprint_error_dev(self, "failed to attach drmfb: %d\n", error);
    102 		return;
    103 	}
    104 
    105 	pmf_device_register1(self, NULL, NULL, tegra_fb_shutdown);
    106 }
    107 
    108 static bool
    109 tegra_fb_shutdown(device_t self, int flags)
    110 {
    111 	struct tegra_fb_softc * const sc = device_private(self);
    112 
    113 	return drmfb_shutdown(&sc->sc_drmfb, flags);
    114 }
    115 
    116 static paddr_t
    117 tegra_fb_mmapfb(struct drmfb_softc *sc, off_t off, int prot)
    118 {
    119 	struct tegra_fb_softc * const tfb_sc = (struct tegra_fb_softc *)sc;
    120 	struct drm_gem_cma_object *obj = tfb_sc->sc_fb->obj;
    121 
    122 	KASSERT(off >= 0);
    123 	KASSERT(off < obj->dmasize);
    124 
    125 	return bus_dmamem_mmap(obj->dmat, obj->dmasegs, 1, off, prot,
    126 	    BUS_DMA_PREFETCHABLE);
    127 }
    128 
    129 static int
    130 tegra_fb_ioctl(struct drmfb_softc *sc, u_long cmd, void *data, int flag,
    131     lwp_t *l)
    132 {
    133 	struct wsdisplayio_bus_id *busid;
    134 	struct wsdisplayio_fbinfo *fbi;
    135 	struct rasops_info *ri = &sc->sc_genfb.vd.active->scr_ri;
    136 	int error;
    137 
    138 	switch (cmd) {
    139 	case WSDISPLAYIO_GET_BUSID:
    140 		busid = data;
    141 		busid->bus_type = WSDISPLAYIO_BUS_SOC;
    142 		return 0;
    143 	case WSDISPLAYIO_GTYPE:
    144 		*(u_int *)data = WSDISPLAY_TYPE_TEGRA;
    145 		return 0;
    146 	case WSDISPLAYIO_GET_FBINFO:
    147 		fbi = data;
    148 		error = wsdisplayio_get_fbinfo(ri, fbi);
    149 		fbi->fbi_flags |= WSFB_VRAM_IS_RAM;
    150 		return error;
    151 	default:
    152 		return EPASSTHROUGH;
    153 	}
    154 }
    155