Home | History | Annotate | Line # | Download | only in sunxi
sunxi_drm.c revision 1.9
      1  1.9  jmcneill /* $NetBSD: sunxi_drm.c,v 1.9 2019/11/24 12:21:14 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2019 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  jmcneill  * SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include <sys/cdefs.h>
     30  1.9  jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_drm.c,v 1.9 2019/11/24 12:21:14 jmcneill Exp $");
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <sys/param.h>
     33  1.1  jmcneill #include <sys/bus.h>
     34  1.1  jmcneill #include <sys/device.h>
     35  1.1  jmcneill #include <sys/intr.h>
     36  1.1  jmcneill #include <sys/systm.h>
     37  1.1  jmcneill #include <sys/kernel.h>
     38  1.1  jmcneill #include <sys/conf.h>
     39  1.1  jmcneill 
     40  1.1  jmcneill #include <uvm/uvm_extern.h>
     41  1.1  jmcneill #include <uvm/uvm_object.h>
     42  1.1  jmcneill #include <uvm/uvm_device.h>
     43  1.1  jmcneill 
     44  1.1  jmcneill #include <drm/drmP.h>
     45  1.1  jmcneill #include <drm/drm_crtc_helper.h>
     46  1.1  jmcneill #include <drm/drm_fb_helper.h>
     47  1.1  jmcneill 
     48  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     49  1.1  jmcneill #include <dev/fdt/fdt_port.h>
     50  1.1  jmcneill 
     51  1.1  jmcneill #include <arm/sunxi/sunxi_drm.h>
     52  1.1  jmcneill 
     53  1.8  jmcneill #define	SUNXI_DRM_MAX_WIDTH	3840
     54  1.8  jmcneill #define	SUNXI_DRM_MAX_HEIGHT	2160
     55  1.8  jmcneill 
     56  1.1  jmcneill static TAILQ_HEAD(, sunxi_drm_endpoint) sunxi_drm_endpoints =
     57  1.1  jmcneill     TAILQ_HEAD_INITIALIZER(sunxi_drm_endpoints);
     58  1.1  jmcneill 
     59  1.1  jmcneill static const char * const compatible[] = {
     60  1.4  jmcneill 	"allwinner,sun8i-h3-display-engine",
     61  1.1  jmcneill 	"allwinner,sun50i-a64-display-engine",
     62  1.1  jmcneill 	NULL
     63  1.1  jmcneill };
     64  1.1  jmcneill 
     65  1.3  jmcneill static const char * fb_compatible[] = {
     66  1.3  jmcneill 	"allwinner,simple-framebuffer",
     67  1.3  jmcneill 	NULL
     68  1.3  jmcneill };
     69  1.3  jmcneill 
     70  1.1  jmcneill static int	sunxi_drm_match(device_t, cfdata_t, void *);
     71  1.1  jmcneill static void	sunxi_drm_attach(device_t, device_t, void *);
     72  1.1  jmcneill 
     73  1.1  jmcneill static void	sunxi_drm_init(device_t);
     74  1.8  jmcneill static vmem_t	*sunxi_drm_alloc_cma_pool(struct drm_device *, size_t);
     75  1.1  jmcneill 
     76  1.1  jmcneill static int	sunxi_drm_set_busid(struct drm_device *, struct drm_master *);
     77  1.1  jmcneill 
     78  1.6  jmcneill static uint32_t	sunxi_drm_get_vblank_counter(struct drm_device *, unsigned int);
     79  1.6  jmcneill static int	sunxi_drm_enable_vblank(struct drm_device *, unsigned int);
     80  1.6  jmcneill static void	sunxi_drm_disable_vblank(struct drm_device *, unsigned int);
     81  1.6  jmcneill 
     82  1.1  jmcneill static int	sunxi_drm_load(struct drm_device *, unsigned long);
     83  1.1  jmcneill static int	sunxi_drm_unload(struct drm_device *);
     84  1.1  jmcneill 
     85  1.1  jmcneill static struct drm_driver sunxi_drm_driver = {
     86  1.1  jmcneill 	.driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
     87  1.1  jmcneill 	.dev_priv_size = 0,
     88  1.1  jmcneill 	.load = sunxi_drm_load,
     89  1.1  jmcneill 	.unload = sunxi_drm_unload,
     90  1.1  jmcneill 
     91  1.1  jmcneill 	.gem_free_object = drm_gem_cma_free_object,
     92  1.1  jmcneill 	.mmap_object = drm_gem_or_legacy_mmap_object,
     93  1.1  jmcneill 	.gem_uvm_ops = &drm_gem_cma_uvm_ops,
     94  1.1  jmcneill 
     95  1.1  jmcneill 	.dumb_create = drm_gem_cma_dumb_create,
     96  1.1  jmcneill 	.dumb_map_offset = drm_gem_cma_dumb_map_offset,
     97  1.1  jmcneill 	.dumb_destroy = drm_gem_dumb_destroy,
     98  1.1  jmcneill 
     99  1.1  jmcneill 	.get_vblank_counter = sunxi_drm_get_vblank_counter,
    100  1.1  jmcneill 	.enable_vblank = sunxi_drm_enable_vblank,
    101  1.1  jmcneill 	.disable_vblank = sunxi_drm_disable_vblank,
    102  1.1  jmcneill 
    103  1.1  jmcneill 	.name = DRIVER_NAME,
    104  1.1  jmcneill 	.desc = DRIVER_DESC,
    105  1.1  jmcneill 	.date = DRIVER_DATE,
    106  1.1  jmcneill 	.major = DRIVER_MAJOR,
    107  1.1  jmcneill 	.minor = DRIVER_MINOR,
    108  1.1  jmcneill 	.patchlevel = DRIVER_PATCHLEVEL,
    109  1.1  jmcneill 
    110  1.1  jmcneill 	.set_busid = sunxi_drm_set_busid,
    111  1.1  jmcneill };
    112  1.1  jmcneill 
    113  1.1  jmcneill CFATTACH_DECL_NEW(sunxi_drm, sizeof(struct sunxi_drm_softc),
    114  1.1  jmcneill 	sunxi_drm_match, sunxi_drm_attach, NULL, NULL);
    115  1.1  jmcneill 
    116  1.1  jmcneill static int
    117  1.1  jmcneill sunxi_drm_match(device_t parent, cfdata_t cf, void *aux)
    118  1.1  jmcneill {
    119  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    120  1.1  jmcneill 
    121  1.1  jmcneill 	return of_match_compatible(faa->faa_phandle, compatible);
    122  1.1  jmcneill }
    123  1.1  jmcneill 
    124  1.1  jmcneill static void
    125  1.1  jmcneill sunxi_drm_attach(device_t parent, device_t self, void *aux)
    126  1.1  jmcneill {
    127  1.1  jmcneill 	struct sunxi_drm_softc * const sc = device_private(self);
    128  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    129  1.1  jmcneill 	struct drm_driver * const driver = &sunxi_drm_driver;
    130  1.5  jmcneill 	prop_dictionary_t dict = device_properties(self);
    131  1.5  jmcneill 	bool is_disabled;
    132  1.1  jmcneill 
    133  1.1  jmcneill 	sc->sc_dev = self;
    134  1.1  jmcneill 	sc->sc_dmat = faa->faa_dmat;
    135  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    136  1.1  jmcneill 	sc->sc_phandle = faa->faa_phandle;
    137  1.1  jmcneill 
    138  1.1  jmcneill 	aprint_naive("\n");
    139  1.5  jmcneill 
    140  1.5  jmcneill 	if (prop_dictionary_get_bool(dict, "disabled", &is_disabled) && is_disabled) {
    141  1.5  jmcneill 		aprint_normal(": Display Engine Pipeline (disabled)\n");
    142  1.5  jmcneill 		return;
    143  1.5  jmcneill 	}
    144  1.5  jmcneill 
    145  1.1  jmcneill 	aprint_normal(": Display Engine Pipeline\n");
    146  1.1  jmcneill 
    147  1.1  jmcneill 	sc->sc_ddev = drm_dev_alloc(driver, sc->sc_dev);
    148  1.1  jmcneill 	if (sc->sc_ddev == NULL) {
    149  1.1  jmcneill 		aprint_error_dev(self, "couldn't allocate DRM device\n");
    150  1.1  jmcneill 		return;
    151  1.1  jmcneill 	}
    152  1.1  jmcneill 	sc->sc_ddev->dev_private = sc;
    153  1.1  jmcneill 	sc->sc_ddev->bst = sc->sc_bst;
    154  1.1  jmcneill 	sc->sc_ddev->bus_dmat = sc->sc_dmat;
    155  1.1  jmcneill 	sc->sc_ddev->dmat = sc->sc_ddev->bus_dmat;
    156  1.1  jmcneill 	sc->sc_ddev->dmat_subregion_p = false;
    157  1.1  jmcneill 
    158  1.3  jmcneill 	fdt_remove_bycompat(fb_compatible);
    159  1.3  jmcneill 
    160  1.1  jmcneill 	config_defer(self, sunxi_drm_init);
    161  1.1  jmcneill }
    162  1.1  jmcneill 
    163  1.1  jmcneill static void
    164  1.1  jmcneill sunxi_drm_init(device_t dev)
    165  1.1  jmcneill {
    166  1.1  jmcneill 	struct sunxi_drm_softc * const sc = device_private(dev);
    167  1.1  jmcneill 	struct drm_driver * const driver = &sunxi_drm_driver;
    168  1.1  jmcneill 	int error;
    169  1.1  jmcneill 
    170  1.1  jmcneill 	error = -drm_dev_register(sc->sc_ddev, 0);
    171  1.1  jmcneill 	if (error) {
    172  1.1  jmcneill 		drm_dev_unref(sc->sc_ddev);
    173  1.1  jmcneill 		aprint_error_dev(dev, "couldn't register DRM device: %d\n",
    174  1.1  jmcneill 		    error);
    175  1.1  jmcneill 		return;
    176  1.1  jmcneill 	}
    177  1.1  jmcneill 
    178  1.1  jmcneill 	aprint_normal_dev(dev, "initialized %s %d.%d.%d %s on minor %d\n",
    179  1.1  jmcneill 	    driver->name, driver->major, driver->minor, driver->patchlevel,
    180  1.1  jmcneill 	    driver->date, sc->sc_ddev->primary->index);
    181  1.1  jmcneill }
    182  1.1  jmcneill 
    183  1.8  jmcneill static vmem_t *
    184  1.8  jmcneill sunxi_drm_alloc_cma_pool(struct drm_device *ddev, size_t cma_size)
    185  1.8  jmcneill {
    186  1.8  jmcneill 	struct sunxi_drm_softc * const sc = sunxi_drm_private(ddev);
    187  1.8  jmcneill 	bus_dma_segment_t segs[1];
    188  1.8  jmcneill 	int nsegs;
    189  1.8  jmcneill 	int error;
    190  1.8  jmcneill 
    191  1.8  jmcneill 	error = bus_dmamem_alloc(sc->sc_dmat, cma_size, PAGE_SIZE, 0,
    192  1.8  jmcneill 	    segs, 1, &nsegs, BUS_DMA_NOWAIT);
    193  1.8  jmcneill 	if (error) {
    194  1.8  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't allocate CMA pool\n");
    195  1.8  jmcneill 		return NULL;
    196  1.8  jmcneill 	}
    197  1.8  jmcneill 
    198  1.8  jmcneill 	return vmem_create("sunxidrm", segs[0].ds_addr, segs[0].ds_len,
    199  1.8  jmcneill 	    PAGE_SIZE, NULL, NULL, NULL, 0, VM_SLEEP, IPL_NONE);
    200  1.8  jmcneill }
    201  1.8  jmcneill 
    202  1.1  jmcneill static int
    203  1.1  jmcneill sunxi_drm_set_busid(struct drm_device *ddev, struct drm_master *master)
    204  1.1  jmcneill {
    205  1.1  jmcneill 	struct sunxi_drm_softc * const sc = sunxi_drm_private(ddev);
    206  1.1  jmcneill 	char id[32];
    207  1.1  jmcneill 
    208  1.1  jmcneill 	snprintf(id, sizeof(id), "platform:sunxi:%u", device_unit(sc->sc_dev));
    209  1.1  jmcneill 
    210  1.1  jmcneill 	master->unique = kzalloc(strlen(id) + 1, GFP_KERNEL);
    211  1.1  jmcneill 	if (master->unique == NULL)
    212  1.1  jmcneill 		return -ENOMEM;
    213  1.1  jmcneill 	strcpy(master->unique, id);
    214  1.1  jmcneill 	master->unique_len = strlen(master->unique);
    215  1.1  jmcneill 
    216  1.1  jmcneill 	return 0;
    217  1.1  jmcneill }
    218  1.1  jmcneill 
    219  1.1  jmcneill static int
    220  1.1  jmcneill sunxi_drm_fb_create_handle(struct drm_framebuffer *fb,
    221  1.1  jmcneill     struct drm_file *file, unsigned int *handle)
    222  1.1  jmcneill {
    223  1.1  jmcneill 	struct sunxi_drm_framebuffer *sfb = to_sunxi_drm_framebuffer(fb);
    224  1.1  jmcneill 
    225  1.1  jmcneill 	return drm_gem_handle_create(file, &sfb->obj->base, handle);
    226  1.1  jmcneill }
    227  1.1  jmcneill 
    228  1.1  jmcneill static void
    229  1.1  jmcneill sunxi_drm_fb_destroy(struct drm_framebuffer *fb)
    230  1.1  jmcneill {
    231  1.1  jmcneill 	struct sunxi_drm_framebuffer *sfb = to_sunxi_drm_framebuffer(fb);
    232  1.1  jmcneill 
    233  1.1  jmcneill 	drm_framebuffer_cleanup(fb);
    234  1.1  jmcneill 	drm_gem_object_unreference_unlocked(&sfb->obj->base);
    235  1.1  jmcneill 	kmem_free(sfb, sizeof(*sfb));
    236  1.1  jmcneill }
    237  1.1  jmcneill 
    238  1.1  jmcneill static const struct drm_framebuffer_funcs sunxi_drm_framebuffer_funcs = {
    239  1.1  jmcneill 	.create_handle = sunxi_drm_fb_create_handle,
    240  1.1  jmcneill 	.destroy = sunxi_drm_fb_destroy,
    241  1.1  jmcneill };
    242  1.1  jmcneill 
    243  1.1  jmcneill static struct drm_framebuffer *
    244  1.1  jmcneill sunxi_drm_fb_create(struct drm_device *ddev, struct drm_file *file,
    245  1.1  jmcneill     struct drm_mode_fb_cmd2 *cmd)
    246  1.1  jmcneill {
    247  1.1  jmcneill 	struct sunxi_drm_framebuffer *fb;
    248  1.1  jmcneill 	struct drm_gem_object *gem_obj;
    249  1.1  jmcneill 	int error;
    250  1.1  jmcneill 
    251  1.1  jmcneill 	if (cmd->flags)
    252  1.1  jmcneill 		return NULL;
    253  1.1  jmcneill 
    254  1.1  jmcneill 	gem_obj = drm_gem_object_lookup(ddev, file, cmd->handles[0]);
    255  1.1  jmcneill 	if (gem_obj == NULL)
    256  1.1  jmcneill 		return NULL;
    257  1.1  jmcneill 
    258  1.1  jmcneill 	fb = kmem_zalloc(sizeof(*fb), KM_SLEEP);
    259  1.1  jmcneill 	fb->obj = to_drm_gem_cma_obj(gem_obj);
    260  1.1  jmcneill 	fb->base.pitches[0] = cmd->pitches[0];
    261  1.6  jmcneill 	fb->base.pitches[1] = cmd->pitches[1];
    262  1.6  jmcneill 	fb->base.pitches[2] = cmd->pitches[2];
    263  1.1  jmcneill 	fb->base.offsets[0] = cmd->offsets[0];
    264  1.6  jmcneill 	fb->base.offsets[1] = cmd->offsets[2];
    265  1.6  jmcneill 	fb->base.offsets[2] = cmd->offsets[1];
    266  1.1  jmcneill 	fb->base.width = cmd->width;
    267  1.1  jmcneill 	fb->base.height = cmd->height;
    268  1.1  jmcneill 	fb->base.pixel_format = cmd->pixel_format;
    269  1.6  jmcneill 	fb->base.bits_per_pixel = drm_format_plane_cpp(fb->base.pixel_format, 0) * 8;
    270  1.6  jmcneill 
    271  1.6  jmcneill 	switch (fb->base.pixel_format) {
    272  1.6  jmcneill 	case DRM_FORMAT_XRGB8888:
    273  1.7  jmcneill 	case DRM_FORMAT_ARGB8888:
    274  1.6  jmcneill 		fb->base.depth = 32;
    275  1.6  jmcneill 		break;
    276  1.6  jmcneill 	default:
    277  1.6  jmcneill 		break;
    278  1.6  jmcneill 	}
    279  1.1  jmcneill 
    280  1.1  jmcneill 	error = drm_framebuffer_init(ddev, &fb->base, &sunxi_drm_framebuffer_funcs);
    281  1.1  jmcneill 	if (error != 0)
    282  1.1  jmcneill 		goto dealloc;
    283  1.1  jmcneill 
    284  1.1  jmcneill 	return &fb->base;
    285  1.1  jmcneill 
    286  1.1  jmcneill dealloc:
    287  1.1  jmcneill 	drm_framebuffer_cleanup(&fb->base);
    288  1.1  jmcneill 	kmem_free(fb, sizeof(*fb));
    289  1.1  jmcneill 	drm_gem_object_unreference_unlocked(gem_obj);
    290  1.1  jmcneill 
    291  1.1  jmcneill 	return NULL;
    292  1.1  jmcneill }
    293  1.1  jmcneill 
    294  1.1  jmcneill static struct drm_mode_config_funcs sunxi_drm_mode_config_funcs = {
    295  1.1  jmcneill 	.fb_create = sunxi_drm_fb_create,
    296  1.1  jmcneill };
    297  1.1  jmcneill 
    298  1.1  jmcneill static int
    299  1.9  jmcneill sunxi_drm_simplefb_lookup(bus_addr_t *paddr, bus_size_t *psize)
    300  1.9  jmcneill {
    301  1.9  jmcneill 	static const char * compat[] = { "simple-framebuffer", NULL };
    302  1.9  jmcneill 	int chosen, child;
    303  1.9  jmcneill 
    304  1.9  jmcneill 	chosen = OF_finddevice("/chosen");
    305  1.9  jmcneill 	if (chosen == -1)
    306  1.9  jmcneill 		return ENOENT;
    307  1.9  jmcneill 
    308  1.9  jmcneill 	for (child = OF_child(chosen); child; child = OF_peer(child)) {
    309  1.9  jmcneill 		if (!fdtbus_status_okay(child))
    310  1.9  jmcneill 			continue;
    311  1.9  jmcneill 		if (!of_match_compatible(child, compat))
    312  1.9  jmcneill 			continue;
    313  1.9  jmcneill 		return fdtbus_get_reg(child, 0, paddr, psize);
    314  1.9  jmcneill 	}
    315  1.9  jmcneill 
    316  1.9  jmcneill 	return ENOENT;
    317  1.9  jmcneill }
    318  1.9  jmcneill 
    319  1.9  jmcneill static int
    320  1.1  jmcneill sunxi_drm_fb_probe(struct drm_fb_helper *helper, struct drm_fb_helper_surface_size *sizes)
    321  1.1  jmcneill {
    322  1.1  jmcneill 	struct sunxi_drm_softc * const sc = sunxi_drm_private(helper->dev);
    323  1.1  jmcneill 	struct drm_device *ddev = helper->dev;
    324  1.1  jmcneill 	struct sunxi_drm_framebuffer *sfb = to_sunxi_drm_framebuffer(helper->fb);
    325  1.1  jmcneill 	struct drm_framebuffer *fb = helper->fb;
    326  1.1  jmcneill 	struct sunxi_drmfb_attach_args sfa;
    327  1.9  jmcneill 	bus_addr_t sfb_addr;
    328  1.9  jmcneill 	bus_size_t sfb_size;
    329  1.8  jmcneill 	size_t cma_size;
    330  1.1  jmcneill 	int error;
    331  1.1  jmcneill 
    332  1.1  jmcneill 	const u_int width = sizes->surface_width;
    333  1.1  jmcneill 	const u_int height = sizes->surface_height;
    334  1.1  jmcneill 	const u_int pitch = width * (32 / 8);
    335  1.1  jmcneill 
    336  1.1  jmcneill 	const size_t size = roundup(height * pitch, PAGE_SIZE);
    337  1.1  jmcneill 
    338  1.9  jmcneill 	if (sunxi_drm_simplefb_lookup(&sfb_addr, &sfb_size) != 0)
    339  1.9  jmcneill 		sfb_size = 0;
    340  1.9  jmcneill 
    341  1.9  jmcneill 	/* Reserve enough memory for a 4K plane, rounded to 1MB */
    342  1.9  jmcneill 	cma_size = (SUNXI_DRM_MAX_WIDTH * SUNXI_DRM_MAX_HEIGHT * 4);
    343  1.9  jmcneill 	if (sfb_size == 0) {
    344  1.9  jmcneill 		/* Add memory for FB console if we cannot reclaim bootloader memory */
    345  1.9  jmcneill 		cma_size += size;
    346  1.9  jmcneill 	}
    347  1.8  jmcneill 	cma_size = roundup(cma_size, 1024 * 1024);
    348  1.8  jmcneill 	sc->sc_ddev->cma_pool = sunxi_drm_alloc_cma_pool(sc->sc_ddev, cma_size);
    349  1.9  jmcneill 	if (sc->sc_ddev->cma_pool != NULL) {
    350  1.9  jmcneill 		if (sfb_size != 0) {
    351  1.9  jmcneill 			error = vmem_add(sc->sc_ddev->cma_pool, sfb_addr,
    352  1.9  jmcneill 			    sfb_size, VM_SLEEP);
    353  1.9  jmcneill 			if (error != 0)
    354  1.9  jmcneill 				sfb_size = 0;
    355  1.9  jmcneill 		}
    356  1.9  jmcneill 		aprint_normal_dev(sc->sc_dev, "reserved %u MB DRAM for CMA",
    357  1.9  jmcneill 		    (u_int)((cma_size + sfb_size) / (1024 * 1024)));
    358  1.9  jmcneill 		if (sfb_size != 0)
    359  1.9  jmcneill 			aprint_normal(" (%u MB reclaimed from bootloader)",
    360  1.9  jmcneill 			    (u_int)(sfb_size / (1024 * 1024)));
    361  1.9  jmcneill 		aprint_normal("\n");
    362  1.9  jmcneill 	}
    363  1.8  jmcneill 
    364  1.1  jmcneill 	sfb->obj = drm_gem_cma_create(ddev, size);
    365  1.1  jmcneill 	if (sfb->obj == NULL) {
    366  1.1  jmcneill 		DRM_ERROR("failed to allocate memory for framebuffer\n");
    367  1.1  jmcneill 		return -ENOMEM;
    368  1.1  jmcneill 	}
    369  1.1  jmcneill 
    370  1.1  jmcneill 	fb->pitches[0] = pitch;
    371  1.1  jmcneill 	fb->offsets[0] = 0;
    372  1.1  jmcneill 	fb->width = width;
    373  1.1  jmcneill 	fb->height = height;
    374  1.1  jmcneill 	fb->pixel_format = DRM_FORMAT_XRGB8888;
    375  1.1  jmcneill 	drm_fb_get_bpp_depth(fb->pixel_format, &fb->depth, &fb->bits_per_pixel);
    376  1.1  jmcneill 
    377  1.1  jmcneill 	error = drm_framebuffer_init(ddev, fb, &sunxi_drm_framebuffer_funcs);
    378  1.1  jmcneill 	if (error != 0) {
    379  1.1  jmcneill 		DRM_ERROR("failed to initialize framebuffer\n");
    380  1.1  jmcneill 		return error;
    381  1.1  jmcneill 	}
    382  1.1  jmcneill 
    383  1.1  jmcneill 	memset(&sfa, 0, sizeof(sfa));
    384  1.1  jmcneill 	sfa.sfa_drm_dev = ddev;
    385  1.1  jmcneill 	sfa.sfa_fb_helper = helper;
    386  1.1  jmcneill 	sfa.sfa_fb_sizes = *sizes;
    387  1.1  jmcneill 	sfa.sfa_fb_bst = sc->sc_bst;
    388  1.1  jmcneill 	sfa.sfa_fb_dmat = sc->sc_dmat;
    389  1.1  jmcneill 	sfa.sfa_fb_linebytes = helper->fb->pitches[0];
    390  1.1  jmcneill 
    391  1.1  jmcneill 	helper->fbdev = config_found_ia(ddev->dev, "sunxifbbus", &sfa, NULL);
    392  1.1  jmcneill 	if (helper->fbdev == NULL) {
    393  1.1  jmcneill 		DRM_ERROR("unable to attach framebuffer\n");
    394  1.1  jmcneill 		return -ENXIO;
    395  1.1  jmcneill 	}
    396  1.1  jmcneill 
    397  1.1  jmcneill 	return 0;
    398  1.1  jmcneill }
    399  1.1  jmcneill 
    400  1.1  jmcneill static struct drm_fb_helper_funcs sunxi_drm_fb_helper_funcs = {
    401  1.1  jmcneill 	.fb_probe = sunxi_drm_fb_probe,
    402  1.1  jmcneill };
    403  1.1  jmcneill 
    404  1.1  jmcneill static int
    405  1.1  jmcneill sunxi_drm_load(struct drm_device *ddev, unsigned long flags)
    406  1.1  jmcneill {
    407  1.1  jmcneill 	struct sunxi_drm_softc * const sc = sunxi_drm_private(ddev);
    408  1.1  jmcneill 	struct sunxi_drm_endpoint *sep;
    409  1.1  jmcneill 	struct sunxi_drm_fbdev *fbdev;
    410  1.1  jmcneill 	const u_int *data;
    411  1.1  jmcneill 	int datalen, error, num_crtc;
    412  1.1  jmcneill 
    413  1.1  jmcneill 	drm_mode_config_init(ddev);
    414  1.1  jmcneill 	ddev->mode_config.min_width = 0;
    415  1.1  jmcneill 	ddev->mode_config.min_height = 0;
    416  1.8  jmcneill 	ddev->mode_config.max_width = SUNXI_DRM_MAX_WIDTH;
    417  1.8  jmcneill 	ddev->mode_config.max_height = SUNXI_DRM_MAX_HEIGHT;
    418  1.1  jmcneill 	ddev->mode_config.funcs = &sunxi_drm_mode_config_funcs;
    419  1.1  jmcneill 
    420  1.1  jmcneill 	num_crtc = 0;
    421  1.1  jmcneill 	data = fdtbus_get_prop(sc->sc_phandle, "allwinner,pipelines", &datalen);
    422  1.1  jmcneill 	while (datalen >= 4) {
    423  1.1  jmcneill 		const int crtc_phandle = fdtbus_get_phandle_from_native(be32dec(data));
    424  1.1  jmcneill 
    425  1.1  jmcneill 		TAILQ_FOREACH(sep, &sunxi_drm_endpoints, entries)
    426  1.1  jmcneill 			if (sep->phandle == crtc_phandle && sep->ddev == NULL) {
    427  1.1  jmcneill 				sep->ddev = ddev;
    428  1.1  jmcneill 				error = fdt_endpoint_activate_direct(sep->ep, true);
    429  1.1  jmcneill 				if (error != 0) {
    430  1.1  jmcneill 					aprint_error_dev(sc->sc_dev, "failed to activate endpoint: %d\n",
    431  1.1  jmcneill 					    error);
    432  1.1  jmcneill 				}
    433  1.1  jmcneill 				if (fdt_endpoint_type(sep->ep) == EP_DRM_CRTC)
    434  1.1  jmcneill 					num_crtc++;
    435  1.1  jmcneill 			}
    436  1.1  jmcneill 
    437  1.1  jmcneill 		datalen -= 4;
    438  1.1  jmcneill 		data++;
    439  1.1  jmcneill 	}
    440  1.1  jmcneill 
    441  1.1  jmcneill 	if (num_crtc == 0) {
    442  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "no pipelines configured\n");
    443  1.1  jmcneill 		return ENXIO;
    444  1.1  jmcneill 	}
    445  1.1  jmcneill 
    446  1.1  jmcneill 	fbdev = kmem_zalloc(sizeof(*fbdev), KM_SLEEP);
    447  1.1  jmcneill 
    448  1.1  jmcneill 	drm_fb_helper_prepare(ddev, &fbdev->helper, &sunxi_drm_fb_helper_funcs);
    449  1.1  jmcneill 
    450  1.1  jmcneill 	error = drm_fb_helper_init(ddev, &fbdev->helper, num_crtc, num_crtc);
    451  1.1  jmcneill 	if (error)
    452  1.1  jmcneill 		goto drmerr;
    453  1.1  jmcneill 
    454  1.1  jmcneill 	fbdev->helper.fb = kmem_zalloc(sizeof(struct sunxi_drm_framebuffer), KM_SLEEP);
    455  1.1  jmcneill 
    456  1.1  jmcneill 	drm_fb_helper_single_add_all_connectors(&fbdev->helper);
    457  1.1  jmcneill 
    458  1.1  jmcneill 	drm_helper_disable_unused_functions(ddev);
    459  1.1  jmcneill 
    460  1.1  jmcneill 	drm_fb_helper_initial_config(&fbdev->helper, 32);
    461  1.1  jmcneill 
    462  1.6  jmcneill 	/* XXX */
    463  1.6  jmcneill 	ddev->irq_enabled = true;
    464  1.6  jmcneill 	drm_vblank_init(ddev, num_crtc);
    465  1.6  jmcneill 
    466  1.1  jmcneill 	return 0;
    467  1.1  jmcneill 
    468  1.1  jmcneill drmerr:
    469  1.1  jmcneill 	drm_mode_config_cleanup(ddev);
    470  1.1  jmcneill 	kmem_free(fbdev, sizeof(*fbdev));
    471  1.1  jmcneill 
    472  1.1  jmcneill 	return error;
    473  1.1  jmcneill }
    474  1.1  jmcneill 
    475  1.6  jmcneill static uint32_t
    476  1.6  jmcneill sunxi_drm_get_vblank_counter(struct drm_device *ddev, unsigned int crtc)
    477  1.6  jmcneill {
    478  1.6  jmcneill 	struct sunxi_drm_softc * const sc = sunxi_drm_private(ddev);
    479  1.6  jmcneill 
    480  1.6  jmcneill 	if (crtc >= __arraycount(sc->sc_vbl))
    481  1.6  jmcneill 		return 0;
    482  1.6  jmcneill 
    483  1.6  jmcneill 	if (sc->sc_vbl[crtc].get_vblank_counter == NULL)
    484  1.6  jmcneill 		return 0;
    485  1.6  jmcneill 
    486  1.6  jmcneill 	return sc->sc_vbl[crtc].get_vblank_counter(sc->sc_vbl[crtc].priv);
    487  1.6  jmcneill }
    488  1.6  jmcneill 
    489  1.6  jmcneill static int
    490  1.6  jmcneill sunxi_drm_enable_vblank(struct drm_device *ddev, unsigned int crtc)
    491  1.6  jmcneill {
    492  1.6  jmcneill 	struct sunxi_drm_softc * const sc = sunxi_drm_private(ddev);
    493  1.6  jmcneill 
    494  1.6  jmcneill 	if (crtc >= __arraycount(sc->sc_vbl))
    495  1.6  jmcneill 		return 0;
    496  1.6  jmcneill 
    497  1.6  jmcneill 	if (sc->sc_vbl[crtc].enable_vblank == NULL)
    498  1.6  jmcneill 		return 0;
    499  1.6  jmcneill 
    500  1.6  jmcneill 	sc->sc_vbl[crtc].enable_vblank(sc->sc_vbl[crtc].priv);
    501  1.6  jmcneill 
    502  1.6  jmcneill 	return 0;
    503  1.6  jmcneill }
    504  1.6  jmcneill 
    505  1.6  jmcneill static void
    506  1.6  jmcneill sunxi_drm_disable_vblank(struct drm_device *ddev, unsigned int crtc)
    507  1.6  jmcneill {
    508  1.6  jmcneill 	struct sunxi_drm_softc * const sc = sunxi_drm_private(ddev);
    509  1.6  jmcneill 
    510  1.6  jmcneill 	if (crtc >= __arraycount(sc->sc_vbl))
    511  1.6  jmcneill 		return;
    512  1.6  jmcneill 
    513  1.6  jmcneill 	if (sc->sc_vbl[crtc].disable_vblank == NULL)
    514  1.6  jmcneill 		return;
    515  1.6  jmcneill 
    516  1.6  jmcneill 	sc->sc_vbl[crtc].disable_vblank(sc->sc_vbl[crtc].priv);
    517  1.6  jmcneill }
    518  1.6  jmcneill 
    519  1.1  jmcneill static int
    520  1.1  jmcneill sunxi_drm_unload(struct drm_device *ddev)
    521  1.1  jmcneill {
    522  1.1  jmcneill 	drm_mode_config_cleanup(ddev);
    523  1.1  jmcneill 
    524  1.1  jmcneill 	return 0;
    525  1.1  jmcneill }
    526  1.1  jmcneill 
    527  1.1  jmcneill int
    528  1.1  jmcneill sunxi_drm_register_endpoint(int phandle, struct fdt_endpoint *ep)
    529  1.1  jmcneill {
    530  1.1  jmcneill 	struct sunxi_drm_endpoint *sep;
    531  1.1  jmcneill 
    532  1.1  jmcneill 	sep = kmem_zalloc(sizeof(*sep), KM_SLEEP);
    533  1.1  jmcneill 	sep->phandle = phandle;
    534  1.1  jmcneill 	sep->ep = ep;
    535  1.1  jmcneill 	sep->ddev = NULL;
    536  1.1  jmcneill 	TAILQ_INSERT_TAIL(&sunxi_drm_endpoints, sep, entries);
    537  1.1  jmcneill 
    538  1.1  jmcneill 	return 0;
    539  1.1  jmcneill }
    540  1.1  jmcneill 
    541  1.1  jmcneill struct drm_device *
    542  1.1  jmcneill sunxi_drm_endpoint_device(struct fdt_endpoint *ep)
    543  1.1  jmcneill {
    544  1.1  jmcneill 	struct sunxi_drm_endpoint *sep;
    545  1.1  jmcneill 
    546  1.1  jmcneill 	TAILQ_FOREACH(sep, &sunxi_drm_endpoints, entries)
    547  1.1  jmcneill 		if (sep->ep == ep)
    548  1.1  jmcneill 			return sep->ddev;
    549  1.1  jmcneill 
    550  1.1  jmcneill 	return NULL;
    551  1.1  jmcneill }
    552