Home | History | Annotate | Line # | Download | only in drm
drmfb.c revision 1.10
      1  1.10  riastrad /*	$NetBSD: drmfb.c,v 1.10 2021/12/19 10:32:59 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  * drmfb: wsdisplay support, via genfb, for any drm device.  Use this
     34   1.1  riastrad  * if you're too lazy to write a hardware-accelerated framebuffer using
     35   1.1  riastrad  * wsdisplay directly.
     36   1.1  riastrad  *
     37   1.1  riastrad  * This doesn't actually do anything interesting -- just hooks up
     38   1.1  riastrad  * drmkms crap and genfb crap.
     39   1.1  riastrad  */
     40   1.1  riastrad 
     41   1.1  riastrad #include <sys/cdefs.h>
     42  1.10  riastrad __KERNEL_RCSID(0, "$NetBSD: drmfb.c,v 1.10 2021/12/19 10:32:59 riastradh Exp $");
     43   1.1  riastrad 
     44   1.1  riastrad #ifdef _KERNEL_OPT
     45   1.1  riastrad #include "vga.h"
     46   1.1  riastrad #endif
     47   1.1  riastrad 
     48   1.1  riastrad #include <sys/types.h>
     49   1.1  riastrad #include <sys/param.h>
     50   1.1  riastrad #include <sys/bus.h>
     51   1.1  riastrad #include <sys/device.h>
     52   1.1  riastrad #include <sys/kauth.h>
     53   1.1  riastrad 
     54   1.1  riastrad #if NVGA > 0
     55   1.1  riastrad /*
     56   1.1  riastrad  * XXX All we really need is vga_is_console from vgavar.h, but the
     57   1.1  riastrad  * header files are missing their own dependencies, so we need to
     58   1.1  riastrad  * explicitly drag in the other crap.
     59   1.1  riastrad  */
     60   1.1  riastrad #include <dev/ic/mc6845reg.h>
     61   1.1  riastrad #include <dev/ic/pcdisplayvar.h>
     62   1.1  riastrad #include <dev/ic/vgareg.h>
     63   1.1  riastrad #include <dev/ic/vgavar.h>
     64   1.1  riastrad #endif
     65   1.1  riastrad 
     66   1.1  riastrad #include <dev/wsfb/genfbvar.h>
     67   1.1  riastrad 
     68  1.10  riastrad #include <drm/drm_device.h>
     69   1.1  riastrad #include <drm/drm_fb_helper.h>
     70   1.1  riastrad #include <drm/drmfb.h>
     71   1.1  riastrad 
     72   1.1  riastrad static int	drmfb_genfb_ioctl(void *, void *, unsigned long, void *, int,
     73   1.1  riastrad 		    struct lwp *);
     74   1.1  riastrad static paddr_t	drmfb_genfb_mmap(void *, void *, off_t, int);
     75   1.1  riastrad static int	drmfb_genfb_enable_polling(void *);
     76   1.1  riastrad static int	drmfb_genfb_disable_polling(void *);
     77   1.1  riastrad static bool	drmfb_genfb_setmode(struct genfb_softc *, int);
     78   1.1  riastrad 
     79   1.1  riastrad static const struct genfb_mode_callback drmfb_genfb_mode_callback = {
     80   1.1  riastrad 	.gmc_setmode = drmfb_genfb_setmode,
     81   1.1  riastrad };
     82   1.1  riastrad 
     83   1.1  riastrad int
     84   1.1  riastrad drmfb_attach(struct drmfb_softc *sc, const struct drmfb_attach_args *da)
     85   1.1  riastrad {
     86   1.1  riastrad 	const struct drm_fb_helper_surface_size *const sizes = da->da_fb_sizes;
     87   1.9  riastrad 	struct drm_connector_list_iter conn_iter;
     88   1.9  riastrad 	struct drm_connector *connector;
     89   1.1  riastrad 	const prop_dictionary_t dict = device_properties(da->da_dev);
     90   1.1  riastrad #if NVGA > 0
     91   1.1  riastrad 	struct drm_device *const dev = da->da_fb_helper->dev;
     92   1.1  riastrad #endif
     93   1.1  riastrad 	static const struct genfb_ops zero_genfb_ops;
     94   1.1  riastrad 	struct genfb_ops genfb_ops = zero_genfb_ops;
     95   1.1  riastrad 	enum { CONS_VGA, CONS_GENFB, CONS_NONE } what_was_cons;
     96   1.2  jmcneill 	bool is_console;
     97   1.9  riastrad 	int error;
     98   1.1  riastrad 
     99   1.1  riastrad 	/* genfb requires this.  */
    100   1.1  riastrad 	KASSERTMSG((void *)&sc->sc_genfb == device_private(da->da_dev),
    101   1.1  riastrad 	    "drmfb_softc must be first member of device softc");
    102   1.1  riastrad 
    103   1.1  riastrad 	sc->sc_da = *da;
    104   1.1  riastrad 
    105   1.1  riastrad 	prop_dictionary_set_uint32(dict, "width", sizes->surface_width);
    106   1.1  riastrad 	prop_dictionary_set_uint32(dict, "height", sizes->surface_height);
    107   1.1  riastrad 	prop_dictionary_set_uint8(dict, "depth", sizes->surface_bpp);
    108   1.3      maya 	prop_dictionary_set_uint16(dict, "linebytes", da->da_fb_linebytes);
    109   1.1  riastrad 	prop_dictionary_set_uint32(dict, "address", 0); /* XXX >32-bit */
    110   1.1  riastrad 	CTASSERT(sizeof(uintptr_t) <= sizeof(uint64_t));
    111   1.1  riastrad 	prop_dictionary_set_uint64(dict, "virtual_address",
    112   1.1  riastrad 	    (uint64_t)(uintptr_t)da->da_fb_vaddr);
    113   1.1  riastrad 
    114   1.1  riastrad 	prop_dictionary_set_uint64(dict, "mode_callback",
    115   1.1  riastrad 	    (uint64_t)(uintptr_t)&drmfb_genfb_mode_callback);
    116   1.1  riastrad 
    117   1.2  jmcneill 	if (!prop_dictionary_get_bool(dict, "is_console", &is_console)) {
    118   1.2  jmcneill 		/* XXX Whattakludge!  */
    119   1.1  riastrad #if NVGA > 0
    120   1.2  jmcneill 		if ((da->da_params->dp_is_vga_console != NULL) &&
    121   1.2  jmcneill 		    (*da->da_params->dp_is_vga_console)(dev)) {
    122   1.2  jmcneill 			what_was_cons = CONS_VGA;
    123   1.2  jmcneill 			prop_dictionary_set_bool(dict, "is_console", true);
    124   1.2  jmcneill 			vga_cndetach();
    125   1.2  jmcneill 			if (da->da_params->dp_disable_vga)
    126   1.2  jmcneill 				(*da->da_params->dp_disable_vga)(dev);
    127   1.2  jmcneill 		} else
    128   1.1  riastrad #endif
    129   1.2  jmcneill 		if (genfb_is_console() && genfb_is_enabled()) {
    130   1.2  jmcneill 			what_was_cons = CONS_GENFB;
    131   1.2  jmcneill 			prop_dictionary_set_bool(dict, "is_console", true);
    132   1.2  jmcneill 		} else {
    133   1.2  jmcneill 			what_was_cons = CONS_NONE;
    134   1.2  jmcneill 			prop_dictionary_set_bool(dict, "is_console", false);
    135   1.2  jmcneill 		}
    136   1.1  riastrad 	} else {
    137   1.1  riastrad 		what_was_cons = CONS_NONE;
    138   1.1  riastrad 	}
    139   1.1  riastrad 
    140   1.5  jmcneill 	/* Make the first EDID we find available to wsfb */
    141   1.9  riastrad 	drm_connector_list_iter_begin(da->da_fb_helper->dev, &conn_iter);
    142   1.9  riastrad 	drm_client_for_each_connector_iter(connector, &conn_iter) {
    143   1.5  jmcneill 		struct drm_property_blob *edid = connector->edid_blob_ptr;
    144   1.7  jmcneill 		if (edid && edid->length) {
    145   1.8  jmcneill 			prop_dictionary_set_data(dict, "EDID", edid->data,
    146   1.8  jmcneill 			    edid->length);
    147   1.5  jmcneill 			break;
    148   1.5  jmcneill 		}
    149   1.5  jmcneill 	}
    150   1.9  riastrad 	drm_connector_list_iter_end(&conn_iter);
    151   1.5  jmcneill 
    152   1.1  riastrad 	sc->sc_genfb.sc_dev = sc->sc_da.da_dev;
    153   1.1  riastrad 	genfb_init(&sc->sc_genfb);
    154   1.1  riastrad 	genfb_ops.genfb_ioctl = drmfb_genfb_ioctl;
    155   1.1  riastrad 	genfb_ops.genfb_mmap = drmfb_genfb_mmap;
    156   1.1  riastrad 	genfb_ops.genfb_enable_polling = drmfb_genfb_enable_polling;
    157   1.1  riastrad 	genfb_ops.genfb_disable_polling = drmfb_genfb_disable_polling;
    158   1.1  riastrad 
    159   1.1  riastrad 	error = genfb_attach(&sc->sc_genfb, &genfb_ops);
    160   1.1  riastrad 	if (error) {
    161   1.1  riastrad 		aprint_error_dev(sc->sc_da.da_dev,
    162   1.1  riastrad 		    "failed to attach genfb: %d\n", error);
    163   1.1  riastrad 		goto fail0;
    164   1.1  riastrad 	}
    165   1.1  riastrad 
    166   1.1  riastrad 	/* Success!  */
    167   1.1  riastrad 	return 0;
    168   1.1  riastrad 
    169   1.1  riastrad fail0:	KASSERT(error);
    170   1.1  riastrad 	/* XXX Restore console...  */
    171   1.1  riastrad 	switch (what_was_cons) {
    172   1.1  riastrad 	case CONS_VGA:
    173   1.1  riastrad 		break;
    174   1.1  riastrad 	case CONS_GENFB:
    175   1.1  riastrad 		break;
    176   1.1  riastrad 	case CONS_NONE:
    177   1.1  riastrad 		break;
    178   1.1  riastrad 	default:
    179   1.1  riastrad 		break;
    180   1.1  riastrad 	}
    181   1.1  riastrad 	return error;
    182   1.1  riastrad }
    183   1.1  riastrad 
    184   1.1  riastrad int
    185   1.1  riastrad drmfb_detach(struct drmfb_softc *sc, int flags)
    186   1.1  riastrad {
    187   1.1  riastrad 
    188   1.1  riastrad 	/* XXX genfb detach?  */
    189   1.1  riastrad 	return 0;
    190   1.1  riastrad }
    191   1.1  riastrad 
    192   1.1  riastrad static int
    193   1.1  riastrad drmfb_genfb_ioctl(void *v, void *vs, unsigned long cmd, void *data, int flag,
    194   1.1  riastrad     struct lwp *l)
    195   1.1  riastrad {
    196   1.1  riastrad 	struct genfb_softc *const genfb = v;
    197   1.1  riastrad 	struct drmfb_softc *const sc = container_of(genfb, struct drmfb_softc,
    198   1.1  riastrad 	    sc_genfb);
    199   1.9  riastrad 	struct drm_connector_list_iter conn_iter;
    200   1.9  riastrad 	struct drm_connector *connector;
    201   1.1  riastrad 	int error;
    202   1.1  riastrad 
    203   1.1  riastrad 	if (sc->sc_da.da_params->dp_ioctl) {
    204   1.1  riastrad 		error = (*sc->sc_da.da_params->dp_ioctl)(sc, cmd, data, flag,
    205   1.1  riastrad 		    l);
    206   1.1  riastrad 		if (error != EPASSTHROUGH)
    207   1.1  riastrad 			return error;
    208   1.1  riastrad 	}
    209   1.1  riastrad 
    210   1.1  riastrad 	switch (cmd) {
    211   1.1  riastrad 	/*
    212   1.1  riastrad 	 * Screen blanking ioctls.  Not to be confused with backlight
    213   1.1  riastrad 	 * (can be disabled while stuff is still drawn on the screen),
    214   1.1  riastrad 	 * brightness, or contrast (which we don't support).  Backlight
    215   1.1  riastrad 	 * and brightness are done through WSDISPLAYIO_{GET,SET}PARAM.
    216   1.1  riastrad 	 * This toggles between DPMS ON and DPMS OFF; backlight toggles
    217   1.1  riastrad 	 * between DPMS ON and DPMS SUSPEND.
    218   1.1  riastrad 	 */
    219   1.1  riastrad 	case WSDISPLAYIO_GVIDEO: {
    220   1.1  riastrad 		int *onp = (int *)data;
    221   1.1  riastrad 
    222   1.1  riastrad 		/* XXX Can't really determine a single answer here.  */
    223   1.1  riastrad 		*onp = 1;
    224   1.1  riastrad 		return 0;
    225   1.1  riastrad 	}
    226   1.1  riastrad 	case WSDISPLAYIO_SVIDEO: {
    227   1.1  riastrad 		const int on = *(const int *)data;
    228   1.1  riastrad 		const int dpms_mode = on? DRM_MODE_DPMS_ON : DRM_MODE_DPMS_OFF;
    229   1.1  riastrad 		struct drm_fb_helper *const fb_helper = sc->sc_da.da_fb_helper;
    230   1.1  riastrad 		struct drm_device *const dev = fb_helper->dev;
    231   1.1  riastrad 
    232   1.1  riastrad 		drm_modeset_lock_all(dev);
    233   1.9  riastrad 		drm_connector_list_iter_begin(fb_helper->dev, &conn_iter);
    234   1.9  riastrad 		drm_client_for_each_connector_iter(connector, &conn_iter) {
    235   1.1  riastrad 			(*connector->funcs->dpms)(connector, dpms_mode);
    236   1.1  riastrad 			drm_object_property_set_value(&connector->base,
    237   1.1  riastrad 			    dev->mode_config.dpms_property, dpms_mode);
    238   1.1  riastrad 		}
    239   1.9  riastrad 		drm_connector_list_iter_end(&conn_iter);
    240   1.1  riastrad 		drm_modeset_unlock_all(dev);
    241   1.1  riastrad 
    242   1.1  riastrad 		return 0;
    243   1.1  riastrad 	}
    244   1.1  riastrad 	default:
    245   1.1  riastrad 		return EPASSTHROUGH;
    246   1.1  riastrad 	}
    247   1.1  riastrad }
    248   1.1  riastrad 
    249   1.1  riastrad static paddr_t
    250   1.1  riastrad drmfb_genfb_mmap(void *v, void *vs, off_t offset, int prot)
    251   1.1  riastrad {
    252   1.1  riastrad 	struct genfb_softc *const genfb = v;
    253   1.1  riastrad 	struct drmfb_softc *const sc = container_of(genfb, struct drmfb_softc,
    254   1.1  riastrad 	    sc_genfb);
    255   1.1  riastrad 
    256   1.1  riastrad 	KASSERT(0 <= offset);
    257   1.1  riastrad 
    258   1.1  riastrad 	if (offset < genfb->sc_fbsize) {
    259   1.1  riastrad 		if (sc->sc_da.da_params->dp_mmapfb == NULL)
    260   1.1  riastrad 			return -1;
    261   1.1  riastrad 		return (*sc->sc_da.da_params->dp_mmapfb)(sc, offset, prot);
    262   1.1  riastrad 	} else {
    263   1.1  riastrad 		if (kauth_authorize_machdep(kauth_cred_get(),
    264   1.1  riastrad 			KAUTH_MACHDEP_UNMANAGEDMEM, NULL, NULL, NULL, NULL)
    265   1.1  riastrad 		    != 0)
    266   1.1  riastrad 			return -1;
    267   1.1  riastrad 		if (sc->sc_da.da_params->dp_mmap == NULL)
    268   1.1  riastrad 			return -1;
    269   1.1  riastrad 		return (*sc->sc_da.da_params->dp_mmap)(sc, offset, prot);
    270   1.1  riastrad 	}
    271   1.1  riastrad }
    272   1.1  riastrad 
    273   1.1  riastrad static int
    274   1.1  riastrad drmfb_genfb_enable_polling(void *cookie)
    275   1.1  riastrad {
    276   1.1  riastrad 	struct genfb_softc *const genfb = cookie;
    277   1.1  riastrad 	struct drmfb_softc *const sc = container_of(genfb, struct drmfb_softc,
    278   1.1  riastrad 	    sc_genfb);
    279   1.1  riastrad 
    280   1.1  riastrad 	return drm_fb_helper_debug_enter_fb(sc->sc_da.da_fb_helper);
    281   1.1  riastrad }
    282   1.1  riastrad 
    283   1.1  riastrad static int
    284   1.1  riastrad drmfb_genfb_disable_polling(void *cookie)
    285   1.1  riastrad {
    286   1.1  riastrad 	struct genfb_softc *const genfb = cookie;
    287   1.1  riastrad 	struct drmfb_softc *const sc = container_of(genfb, struct drmfb_softc,
    288   1.1  riastrad 	    sc_genfb);
    289   1.1  riastrad 
    290   1.1  riastrad 	return drm_fb_helper_debug_leave_fb(sc->sc_da.da_fb_helper);
    291   1.1  riastrad }
    292   1.1  riastrad 
    293   1.1  riastrad static bool
    294   1.1  riastrad drmfb_genfb_setmode(struct genfb_softc *genfb, int mode)
    295   1.1  riastrad {
    296   1.1  riastrad 	struct drmfb_softc *sc = container_of(genfb, struct drmfb_softc,
    297   1.1  riastrad 	    sc_genfb);
    298   1.4  riastrad 	struct drm_fb_helper *fb_helper = sc->sc_da.da_fb_helper;
    299   1.1  riastrad 
    300   1.1  riastrad 	if (mode == WSDISPLAYIO_MODE_EMUL)
    301   1.4  riastrad 		drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
    302   1.1  riastrad 
    303   1.1  riastrad 	return true;
    304   1.1  riastrad }
    305   1.1  riastrad 
    306   1.1  riastrad bool
    307   1.1  riastrad drmfb_shutdown(struct drmfb_softc *sc, int flags __unused)
    308   1.1  riastrad {
    309   1.1  riastrad 
    310   1.1  riastrad 	genfb_enable_polling(sc->sc_da.da_dev);
    311   1.1  riastrad 	return true;
    312   1.1  riastrad }
    313