Home | History | Annotate | Line # | Download | only in sunxi
sunxi_drm.c revision 1.25
      1  1.25     skrll /* $NetBSD: sunxi_drm.c,v 1.25 2022/06/28 05:19:03 skrll 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.25     skrll __KERNEL_RCSID(0, "$NetBSD: sunxi_drm.c,v 1.25 2022/06/28 05:19:03 skrll Exp $");
     31   1.1  jmcneill 
     32   1.1  jmcneill #include <sys/param.h>
     33   1.1  jmcneill #include <sys/bus.h>
     34  1.17  riastrad #include <sys/conf.h>
     35   1.1  jmcneill #include <sys/device.h>
     36   1.1  jmcneill #include <sys/intr.h>
     37  1.17  riastrad #include <sys/kernel.h>
     38   1.1  jmcneill #include <sys/systm.h>
     39   1.1  jmcneill 
     40  1.17  riastrad #include <uvm/uvm_device.h>
     41   1.1  jmcneill #include <uvm/uvm_extern.h>
     42   1.1  jmcneill #include <uvm/uvm_object.h>
     43  1.17  riastrad 
     44  1.17  riastrad #include <dev/fdt/fdt_port.h>
     45  1.17  riastrad #include <dev/fdt/fdtvar.h>
     46  1.17  riastrad 
     47  1.17  riastrad #include <arm/sunxi/sunxi_drm.h>
     48   1.1  jmcneill 
     49  1.16  riastrad #include <drm/drm_auth.h>
     50   1.1  jmcneill #include <drm/drm_crtc_helper.h>
     51  1.16  riastrad #include <drm/drm_drv.h>
     52   1.1  jmcneill #include <drm/drm_fb_helper.h>
     53  1.16  riastrad #include <drm/drm_fourcc.h>
     54  1.16  riastrad #include <drm/drm_vblank.h>
     55   1.1  jmcneill 
     56   1.8  jmcneill #define	SUNXI_DRM_MAX_WIDTH	3840
     57   1.8  jmcneill #define	SUNXI_DRM_MAX_HEIGHT	2160
     58   1.8  jmcneill 
     59  1.11  jmcneill /*
     60  1.11  jmcneill  * The DRM headers break trunc_page/round_page macros with a redefinition
     61  1.11  jmcneill  * of PAGE_MASK. Use our own macros instead.
     62  1.11  jmcneill  */
     63  1.11  jmcneill #define	SUNXI_PAGE_MASK		(PAGE_SIZE - 1)
     64  1.11  jmcneill #define	SUNXI_TRUNC_PAGE(x)	((x) & ~SUNXI_PAGE_MASK)
     65  1.11  jmcneill #define	SUNXI_ROUND_PAGE(x)	(((x) + SUNXI_PAGE_MASK) & ~SUNXI_PAGE_MASK)
     66  1.11  jmcneill 
     67   1.1  jmcneill static TAILQ_HEAD(, sunxi_drm_endpoint) sunxi_drm_endpoints =
     68   1.1  jmcneill     TAILQ_HEAD_INITIALIZER(sunxi_drm_endpoints);
     69   1.1  jmcneill 
     70  1.12   thorpej static const struct device_compatible_entry compat_data[] = {
     71  1.12   thorpej 	{ .compat = "allwinner,sun8i-h3-display-engine" },
     72  1.25     skrll 	{ .compat = "allwinner,sun8i-v3s-display-engine" },
     73  1.13       rin 	{ .compat = "allwinner,sun50i-a64-display-engine" },
     74  1.12   thorpej 	DEVICE_COMPAT_EOL
     75   1.1  jmcneill };
     76   1.1  jmcneill 
     77   1.3  jmcneill static const char * fb_compatible[] = {
     78   1.3  jmcneill 	"allwinner,simple-framebuffer",
     79   1.3  jmcneill 	NULL
     80   1.3  jmcneill };
     81   1.3  jmcneill 
     82   1.1  jmcneill static int	sunxi_drm_match(device_t, cfdata_t, void *);
     83   1.1  jmcneill static void	sunxi_drm_attach(device_t, device_t, void *);
     84   1.1  jmcneill 
     85   1.1  jmcneill static void	sunxi_drm_init(device_t);
     86   1.8  jmcneill static vmem_t	*sunxi_drm_alloc_cma_pool(struct drm_device *, size_t);
     87   1.1  jmcneill 
     88   1.6  jmcneill static uint32_t	sunxi_drm_get_vblank_counter(struct drm_device *, unsigned int);
     89   1.6  jmcneill static int	sunxi_drm_enable_vblank(struct drm_device *, unsigned int);
     90   1.6  jmcneill static void	sunxi_drm_disable_vblank(struct drm_device *, unsigned int);
     91   1.6  jmcneill 
     92   1.1  jmcneill static int	sunxi_drm_load(struct drm_device *, unsigned long);
     93  1.16  riastrad static void	sunxi_drm_unload(struct drm_device *);
     94   1.1  jmcneill 
     95  1.22  riastrad static void	sunxi_drm_task_work(struct work *, void *);
     96  1.22  riastrad 
     97   1.1  jmcneill static struct drm_driver sunxi_drm_driver = {
     98  1.16  riastrad 	.driver_features = DRIVER_MODESET | DRIVER_GEM,
     99   1.1  jmcneill 	.dev_priv_size = 0,
    100   1.1  jmcneill 	.load = sunxi_drm_load,
    101   1.1  jmcneill 	.unload = sunxi_drm_unload,
    102   1.1  jmcneill 
    103   1.1  jmcneill 	.gem_free_object = drm_gem_cma_free_object,
    104   1.1  jmcneill 	.mmap_object = drm_gem_or_legacy_mmap_object,
    105   1.1  jmcneill 	.gem_uvm_ops = &drm_gem_cma_uvm_ops,
    106   1.1  jmcneill 
    107   1.1  jmcneill 	.dumb_create = drm_gem_cma_dumb_create,
    108   1.1  jmcneill 	.dumb_destroy = drm_gem_dumb_destroy,
    109   1.1  jmcneill 
    110   1.1  jmcneill 	.get_vblank_counter = sunxi_drm_get_vblank_counter,
    111   1.1  jmcneill 	.enable_vblank = sunxi_drm_enable_vblank,
    112   1.1  jmcneill 	.disable_vblank = sunxi_drm_disable_vblank,
    113   1.1  jmcneill 
    114   1.1  jmcneill 	.name = DRIVER_NAME,
    115   1.1  jmcneill 	.desc = DRIVER_DESC,
    116   1.1  jmcneill 	.date = DRIVER_DATE,
    117   1.1  jmcneill 	.major = DRIVER_MAJOR,
    118   1.1  jmcneill 	.minor = DRIVER_MINOR,
    119   1.1  jmcneill 	.patchlevel = DRIVER_PATCHLEVEL,
    120   1.1  jmcneill };
    121   1.1  jmcneill 
    122   1.1  jmcneill CFATTACH_DECL_NEW(sunxi_drm, sizeof(struct sunxi_drm_softc),
    123   1.1  jmcneill 	sunxi_drm_match, sunxi_drm_attach, NULL, NULL);
    124   1.1  jmcneill 
    125   1.1  jmcneill static int
    126   1.1  jmcneill sunxi_drm_match(device_t parent, cfdata_t cf, void *aux)
    127   1.1  jmcneill {
    128   1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    129   1.1  jmcneill 
    130  1.12   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    131   1.1  jmcneill }
    132   1.1  jmcneill 
    133   1.1  jmcneill static void
    134   1.1  jmcneill sunxi_drm_attach(device_t parent, device_t self, void *aux)
    135   1.1  jmcneill {
    136   1.1  jmcneill 	struct sunxi_drm_softc * const sc = device_private(self);
    137   1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    138   1.1  jmcneill 	struct drm_driver * const driver = &sunxi_drm_driver;
    139   1.5  jmcneill 	prop_dictionary_t dict = device_properties(self);
    140   1.5  jmcneill 	bool is_disabled;
    141   1.1  jmcneill 
    142  1.23  riastrad 	aprint_naive("\n");
    143  1.23  riastrad 
    144  1.23  riastrad 	if (prop_dictionary_get_bool(dict, "disabled", &is_disabled) &&
    145  1.23  riastrad 	    is_disabled) {
    146  1.23  riastrad 		aprint_normal(": Display Engine Pipeline (disabled)\n");
    147  1.23  riastrad 		return;
    148  1.23  riastrad 	}
    149  1.23  riastrad 
    150  1.23  riastrad 	aprint_normal(": Display Engine Pipeline\n");
    151  1.23  riastrad 
    152   1.1  jmcneill 	sc->sc_dev = self;
    153   1.1  jmcneill 	sc->sc_dmat = faa->faa_dmat;
    154   1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    155   1.1  jmcneill 	sc->sc_phandle = faa->faa_phandle;
    156  1.22  riastrad 	sc->sc_task_thread = NULL;
    157  1.22  riastrad 	SIMPLEQ_INIT(&sc->sc_tasks);
    158  1.22  riastrad 	if (workqueue_create(&sc->sc_task_wq, "sunxidrm",
    159  1.22  riastrad 		&sunxi_drm_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE)) {
    160  1.22  riastrad 		aprint_error_dev(self, "unable to create workqueue\n");
    161  1.22  riastrad 		sc->sc_task_wq = NULL;
    162  1.22  riastrad 		return;
    163  1.22  riastrad 	}
    164   1.1  jmcneill 
    165   1.1  jmcneill 	sc->sc_ddev = drm_dev_alloc(driver, sc->sc_dev);
    166  1.18  riastrad 	if (IS_ERR(sc->sc_ddev)) {
    167   1.1  jmcneill 		aprint_error_dev(self, "couldn't allocate DRM device\n");
    168   1.1  jmcneill 		return;
    169   1.1  jmcneill 	}
    170   1.1  jmcneill 	sc->sc_ddev->dev_private = sc;
    171   1.1  jmcneill 	sc->sc_ddev->bst = sc->sc_bst;
    172   1.1  jmcneill 	sc->sc_ddev->bus_dmat = sc->sc_dmat;
    173   1.1  jmcneill 	sc->sc_ddev->dmat = sc->sc_ddev->bus_dmat;
    174   1.1  jmcneill 	sc->sc_ddev->dmat_subregion_p = false;
    175   1.1  jmcneill 
    176   1.3  jmcneill 	fdt_remove_bycompat(fb_compatible);
    177   1.3  jmcneill 
    178   1.1  jmcneill 	config_defer(self, sunxi_drm_init);
    179   1.1  jmcneill }
    180   1.1  jmcneill 
    181   1.1  jmcneill static void
    182   1.1  jmcneill sunxi_drm_init(device_t dev)
    183   1.1  jmcneill {
    184   1.1  jmcneill 	struct sunxi_drm_softc * const sc = device_private(dev);
    185   1.1  jmcneill 	struct drm_driver * const driver = &sunxi_drm_driver;
    186   1.1  jmcneill 	int error;
    187   1.1  jmcneill 
    188  1.22  riastrad 	/*
    189  1.22  riastrad 	 * Cause any tasks issued synchronously during attach to be
    190  1.22  riastrad 	 * processed at the end of this function.
    191  1.22  riastrad 	 */
    192  1.22  riastrad 	sc->sc_task_thread = curlwp;
    193  1.22  riastrad 
    194   1.1  jmcneill 	error = -drm_dev_register(sc->sc_ddev, 0);
    195   1.1  jmcneill 	if (error) {
    196   1.1  jmcneill 		aprint_error_dev(dev, "couldn't register DRM device: %d\n",
    197   1.1  jmcneill 		    error);
    198  1.22  riastrad 		goto out;
    199   1.1  jmcneill 	}
    200  1.22  riastrad 	sc->sc_dev_registered = true;
    201   1.1  jmcneill 
    202   1.1  jmcneill 	aprint_normal_dev(dev, "initialized %s %d.%d.%d %s on minor %d\n",
    203   1.1  jmcneill 	    driver->name, driver->major, driver->minor, driver->patchlevel,
    204   1.1  jmcneill 	    driver->date, sc->sc_ddev->primary->index);
    205  1.22  riastrad 
    206  1.22  riastrad 	/*
    207  1.22  riastrad 	 * Process asynchronous tasks queued synchronously during
    208  1.22  riastrad 	 * attach.  This will be for display detection to attach a
    209  1.22  riastrad 	 * framebuffer, so we have the opportunity for a console device
    210  1.22  riastrad 	 * to attach before autoconf has completed, in time for init(8)
    211  1.22  riastrad 	 * to find that console without panicking.
    212  1.22  riastrad 	 */
    213  1.22  riastrad 	while (!SIMPLEQ_EMPTY(&sc->sc_tasks)) {
    214  1.22  riastrad 		struct sunxi_drm_task *const task =
    215  1.22  riastrad 		    SIMPLEQ_FIRST(&sc->sc_tasks);
    216  1.22  riastrad 
    217  1.22  riastrad 		SIMPLEQ_REMOVE_HEAD(&sc->sc_tasks, sdt_u.queue);
    218  1.22  riastrad 		(*task->sdt_fn)(task);
    219  1.22  riastrad 	}
    220  1.22  riastrad 
    221  1.24    andvar out:	/* Cause any subsequent tasks to be processed by the workqueue.  */
    222  1.22  riastrad 	atomic_store_relaxed(&sc->sc_task_thread, NULL);
    223   1.1  jmcneill }
    224   1.1  jmcneill 
    225   1.8  jmcneill static vmem_t *
    226   1.8  jmcneill sunxi_drm_alloc_cma_pool(struct drm_device *ddev, size_t cma_size)
    227   1.8  jmcneill {
    228   1.8  jmcneill 	struct sunxi_drm_softc * const sc = sunxi_drm_private(ddev);
    229   1.8  jmcneill 	bus_dma_segment_t segs[1];
    230   1.8  jmcneill 	int nsegs;
    231   1.8  jmcneill 	int error;
    232   1.8  jmcneill 
    233   1.8  jmcneill 	error = bus_dmamem_alloc(sc->sc_dmat, cma_size, PAGE_SIZE, 0,
    234   1.8  jmcneill 	    segs, 1, &nsegs, BUS_DMA_NOWAIT);
    235   1.8  jmcneill 	if (error) {
    236   1.8  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't allocate CMA pool\n");
    237   1.8  jmcneill 		return NULL;
    238   1.8  jmcneill 	}
    239   1.8  jmcneill 
    240   1.8  jmcneill 	return vmem_create("sunxidrm", segs[0].ds_addr, segs[0].ds_len,
    241   1.8  jmcneill 	    PAGE_SIZE, NULL, NULL, NULL, 0, VM_SLEEP, IPL_NONE);
    242   1.8  jmcneill }
    243   1.8  jmcneill 
    244   1.1  jmcneill static int
    245   1.1  jmcneill sunxi_drm_fb_create_handle(struct drm_framebuffer *fb,
    246   1.1  jmcneill     struct drm_file *file, unsigned int *handle)
    247   1.1  jmcneill {
    248   1.1  jmcneill 	struct sunxi_drm_framebuffer *sfb = to_sunxi_drm_framebuffer(fb);
    249   1.1  jmcneill 
    250   1.1  jmcneill 	return drm_gem_handle_create(file, &sfb->obj->base, handle);
    251   1.1  jmcneill }
    252   1.1  jmcneill 
    253   1.1  jmcneill static void
    254   1.1  jmcneill sunxi_drm_fb_destroy(struct drm_framebuffer *fb)
    255   1.1  jmcneill {
    256   1.1  jmcneill 	struct sunxi_drm_framebuffer *sfb = to_sunxi_drm_framebuffer(fb);
    257   1.1  jmcneill 
    258   1.1  jmcneill 	drm_framebuffer_cleanup(fb);
    259  1.16  riastrad 	drm_gem_object_put_unlocked(&sfb->obj->base);
    260   1.1  jmcneill 	kmem_free(sfb, sizeof(*sfb));
    261   1.1  jmcneill }
    262   1.1  jmcneill 
    263   1.1  jmcneill static const struct drm_framebuffer_funcs sunxi_drm_framebuffer_funcs = {
    264   1.1  jmcneill 	.create_handle = sunxi_drm_fb_create_handle,
    265   1.1  jmcneill 	.destroy = sunxi_drm_fb_destroy,
    266   1.1  jmcneill };
    267   1.1  jmcneill 
    268   1.1  jmcneill static struct drm_framebuffer *
    269   1.1  jmcneill sunxi_drm_fb_create(struct drm_device *ddev, struct drm_file *file,
    270  1.16  riastrad     const struct drm_mode_fb_cmd2 *cmd)
    271   1.1  jmcneill {
    272   1.1  jmcneill 	struct sunxi_drm_framebuffer *fb;
    273   1.1  jmcneill 	struct drm_gem_object *gem_obj;
    274   1.1  jmcneill 	int error;
    275   1.1  jmcneill 
    276   1.1  jmcneill 	if (cmd->flags)
    277   1.1  jmcneill 		return NULL;
    278   1.1  jmcneill 
    279  1.16  riastrad 	gem_obj = drm_gem_object_lookup(file, cmd->handles[0]);
    280   1.1  jmcneill 	if (gem_obj == NULL)
    281   1.1  jmcneill 		return NULL;
    282   1.1  jmcneill 
    283   1.1  jmcneill 	fb = kmem_zalloc(sizeof(*fb), KM_SLEEP);
    284   1.1  jmcneill 	fb->obj = to_drm_gem_cma_obj(gem_obj);
    285  1.20  riastrad 	drm_helper_mode_fill_fb_struct(ddev, &fb->base, cmd);
    286   1.1  jmcneill 
    287   1.1  jmcneill 	error = drm_framebuffer_init(ddev, &fb->base, &sunxi_drm_framebuffer_funcs);
    288   1.1  jmcneill 	if (error != 0)
    289   1.1  jmcneill 		goto dealloc;
    290   1.1  jmcneill 
    291   1.1  jmcneill 	return &fb->base;
    292   1.1  jmcneill 
    293   1.1  jmcneill dealloc:
    294   1.1  jmcneill 	drm_framebuffer_cleanup(&fb->base);
    295   1.1  jmcneill 	kmem_free(fb, sizeof(*fb));
    296  1.16  riastrad 	drm_gem_object_put_unlocked(gem_obj);
    297   1.1  jmcneill 
    298   1.1  jmcneill 	return NULL;
    299   1.1  jmcneill }
    300   1.1  jmcneill 
    301   1.1  jmcneill static struct drm_mode_config_funcs sunxi_drm_mode_config_funcs = {
    302   1.1  jmcneill 	.fb_create = sunxi_drm_fb_create,
    303   1.1  jmcneill };
    304   1.1  jmcneill 
    305   1.1  jmcneill static int
    306   1.9  jmcneill sunxi_drm_simplefb_lookup(bus_addr_t *paddr, bus_size_t *psize)
    307   1.9  jmcneill {
    308  1.12   thorpej 	static const struct device_compatible_entry simplefb_compat[] = {
    309  1.12   thorpej 		{ .compat = "simple-framebuffer" },
    310  1.12   thorpej 		DEVICE_COMPAT_EOL
    311  1.12   thorpej 	};
    312  1.11  jmcneill 	int chosen, child, error;
    313  1.11  jmcneill 	bus_addr_t addr_end;
    314   1.9  jmcneill 
    315   1.9  jmcneill 	chosen = OF_finddevice("/chosen");
    316   1.9  jmcneill 	if (chosen == -1)
    317   1.9  jmcneill 		return ENOENT;
    318   1.9  jmcneill 
    319   1.9  jmcneill 	for (child = OF_child(chosen); child; child = OF_peer(child)) {
    320   1.9  jmcneill 		if (!fdtbus_status_okay(child))
    321   1.9  jmcneill 			continue;
    322  1.12   thorpej 		if (!of_compatible_match(child, simplefb_compat))
    323   1.9  jmcneill 			continue;
    324  1.11  jmcneill 		error = fdtbus_get_reg(child, 0, paddr, psize);
    325  1.11  jmcneill 		if (error != 0)
    326  1.11  jmcneill 			return error;
    327  1.11  jmcneill 
    328  1.11  jmcneill 		/* Reclaim entire pages used by the simplefb */
    329  1.11  jmcneill 		addr_end = *paddr + *psize;
    330  1.11  jmcneill 		*paddr = SUNXI_TRUNC_PAGE(*paddr);
    331  1.11  jmcneill 		*psize = SUNXI_ROUND_PAGE(addr_end - *paddr);
    332  1.11  jmcneill 		return 0;
    333   1.9  jmcneill 	}
    334   1.9  jmcneill 
    335   1.9  jmcneill 	return ENOENT;
    336   1.9  jmcneill }
    337   1.9  jmcneill 
    338   1.9  jmcneill static int
    339   1.1  jmcneill sunxi_drm_fb_probe(struct drm_fb_helper *helper, struct drm_fb_helper_surface_size *sizes)
    340   1.1  jmcneill {
    341   1.1  jmcneill 	struct sunxi_drm_softc * const sc = sunxi_drm_private(helper->dev);
    342   1.1  jmcneill 	struct drm_device *ddev = helper->dev;
    343   1.1  jmcneill 	struct sunxi_drm_framebuffer *sfb = to_sunxi_drm_framebuffer(helper->fb);
    344   1.1  jmcneill 	struct drm_framebuffer *fb = helper->fb;
    345   1.1  jmcneill 	struct sunxi_drmfb_attach_args sfa;
    346   1.9  jmcneill 	bus_addr_t sfb_addr;
    347   1.9  jmcneill 	bus_size_t sfb_size;
    348   1.8  jmcneill 	size_t cma_size;
    349   1.1  jmcneill 	int error;
    350   1.1  jmcneill 
    351   1.1  jmcneill 	const u_int width = sizes->surface_width;
    352   1.1  jmcneill 	const u_int height = sizes->surface_height;
    353   1.1  jmcneill 	const u_int pitch = width * (32 / 8);
    354   1.1  jmcneill 
    355   1.1  jmcneill 	const size_t size = roundup(height * pitch, PAGE_SIZE);
    356   1.1  jmcneill 
    357   1.9  jmcneill 	if (sunxi_drm_simplefb_lookup(&sfb_addr, &sfb_size) != 0)
    358   1.9  jmcneill 		sfb_size = 0;
    359   1.9  jmcneill 
    360   1.9  jmcneill 	/* Reserve enough memory for a 4K plane, rounded to 1MB */
    361   1.9  jmcneill 	cma_size = (SUNXI_DRM_MAX_WIDTH * SUNXI_DRM_MAX_HEIGHT * 4);
    362   1.9  jmcneill 	if (sfb_size == 0) {
    363   1.9  jmcneill 		/* Add memory for FB console if we cannot reclaim bootloader memory */
    364   1.9  jmcneill 		cma_size += size;
    365   1.9  jmcneill 	}
    366   1.8  jmcneill 	cma_size = roundup(cma_size, 1024 * 1024);
    367   1.8  jmcneill 	sc->sc_ddev->cma_pool = sunxi_drm_alloc_cma_pool(sc->sc_ddev, cma_size);
    368   1.9  jmcneill 	if (sc->sc_ddev->cma_pool != NULL) {
    369   1.9  jmcneill 		if (sfb_size != 0) {
    370   1.9  jmcneill 			error = vmem_add(sc->sc_ddev->cma_pool, sfb_addr,
    371   1.9  jmcneill 			    sfb_size, VM_SLEEP);
    372   1.9  jmcneill 			if (error != 0)
    373   1.9  jmcneill 				sfb_size = 0;
    374   1.9  jmcneill 		}
    375   1.9  jmcneill 		aprint_normal_dev(sc->sc_dev, "reserved %u MB DRAM for CMA",
    376   1.9  jmcneill 		    (u_int)((cma_size + sfb_size) / (1024 * 1024)));
    377   1.9  jmcneill 		if (sfb_size != 0)
    378   1.9  jmcneill 			aprint_normal(" (%u MB reclaimed from bootloader)",
    379   1.9  jmcneill 			    (u_int)(sfb_size / (1024 * 1024)));
    380   1.9  jmcneill 		aprint_normal("\n");
    381   1.9  jmcneill 	}
    382   1.8  jmcneill 
    383   1.1  jmcneill 	sfb->obj = drm_gem_cma_create(ddev, size);
    384   1.1  jmcneill 	if (sfb->obj == NULL) {
    385   1.1  jmcneill 		DRM_ERROR("failed to allocate memory for framebuffer\n");
    386   1.1  jmcneill 		return -ENOMEM;
    387   1.1  jmcneill 	}
    388   1.1  jmcneill 
    389   1.1  jmcneill 	fb->pitches[0] = pitch;
    390   1.1  jmcneill 	fb->offsets[0] = 0;
    391   1.1  jmcneill 	fb->width = width;
    392   1.1  jmcneill 	fb->height = height;
    393  1.16  riastrad 	fb->format = drm_format_info(DRM_FORMAT_XRGB8888);
    394  1.19  riastrad 	fb->dev = ddev;
    395   1.1  jmcneill 
    396   1.1  jmcneill 	error = drm_framebuffer_init(ddev, fb, &sunxi_drm_framebuffer_funcs);
    397   1.1  jmcneill 	if (error != 0) {
    398   1.1  jmcneill 		DRM_ERROR("failed to initialize framebuffer\n");
    399   1.1  jmcneill 		return error;
    400   1.1  jmcneill 	}
    401   1.1  jmcneill 
    402   1.1  jmcneill 	memset(&sfa, 0, sizeof(sfa));
    403   1.1  jmcneill 	sfa.sfa_drm_dev = ddev;
    404   1.1  jmcneill 	sfa.sfa_fb_helper = helper;
    405   1.1  jmcneill 	sfa.sfa_fb_sizes = *sizes;
    406   1.1  jmcneill 	sfa.sfa_fb_bst = sc->sc_bst;
    407   1.1  jmcneill 	sfa.sfa_fb_dmat = sc->sc_dmat;
    408   1.1  jmcneill 	sfa.sfa_fb_linebytes = helper->fb->pitches[0];
    409   1.1  jmcneill 
    410  1.14   thorpej 	helper->fbdev = config_found(ddev->dev, &sfa, NULL,
    411  1.15   thorpej 	    CFARGS(.iattr = "sunxifbbus"));
    412   1.1  jmcneill 	if (helper->fbdev == NULL) {
    413   1.1  jmcneill 		DRM_ERROR("unable to attach framebuffer\n");
    414   1.1  jmcneill 		return -ENXIO;
    415   1.1  jmcneill 	}
    416   1.1  jmcneill 
    417   1.1  jmcneill 	return 0;
    418   1.1  jmcneill }
    419   1.1  jmcneill 
    420   1.1  jmcneill static struct drm_fb_helper_funcs sunxi_drm_fb_helper_funcs = {
    421   1.1  jmcneill 	.fb_probe = sunxi_drm_fb_probe,
    422   1.1  jmcneill };
    423   1.1  jmcneill 
    424   1.1  jmcneill static int
    425   1.1  jmcneill sunxi_drm_load(struct drm_device *ddev, unsigned long flags)
    426   1.1  jmcneill {
    427   1.1  jmcneill 	struct sunxi_drm_softc * const sc = sunxi_drm_private(ddev);
    428   1.1  jmcneill 	struct sunxi_drm_endpoint *sep;
    429   1.1  jmcneill 	struct sunxi_drm_fbdev *fbdev;
    430   1.1  jmcneill 	const u_int *data;
    431   1.1  jmcneill 	int datalen, error, num_crtc;
    432   1.1  jmcneill 
    433   1.1  jmcneill 	drm_mode_config_init(ddev);
    434   1.1  jmcneill 	ddev->mode_config.min_width = 0;
    435   1.1  jmcneill 	ddev->mode_config.min_height = 0;
    436   1.8  jmcneill 	ddev->mode_config.max_width = SUNXI_DRM_MAX_WIDTH;
    437   1.8  jmcneill 	ddev->mode_config.max_height = SUNXI_DRM_MAX_HEIGHT;
    438   1.1  jmcneill 	ddev->mode_config.funcs = &sunxi_drm_mode_config_funcs;
    439   1.1  jmcneill 
    440   1.1  jmcneill 	num_crtc = 0;
    441   1.1  jmcneill 	data = fdtbus_get_prop(sc->sc_phandle, "allwinner,pipelines", &datalen);
    442   1.1  jmcneill 	while (datalen >= 4) {
    443   1.1  jmcneill 		const int crtc_phandle = fdtbus_get_phandle_from_native(be32dec(data));
    444   1.1  jmcneill 
    445   1.1  jmcneill 		TAILQ_FOREACH(sep, &sunxi_drm_endpoints, entries)
    446   1.1  jmcneill 			if (sep->phandle == crtc_phandle && sep->ddev == NULL) {
    447   1.1  jmcneill 				sep->ddev = ddev;
    448   1.1  jmcneill 				error = fdt_endpoint_activate_direct(sep->ep, true);
    449   1.1  jmcneill 				if (error != 0) {
    450   1.1  jmcneill 					aprint_error_dev(sc->sc_dev, "failed to activate endpoint: %d\n",
    451   1.1  jmcneill 					    error);
    452   1.1  jmcneill 				}
    453   1.1  jmcneill 				if (fdt_endpoint_type(sep->ep) == EP_DRM_CRTC)
    454   1.1  jmcneill 					num_crtc++;
    455   1.1  jmcneill 			}
    456   1.1  jmcneill 
    457   1.1  jmcneill 		datalen -= 4;
    458   1.1  jmcneill 		data++;
    459   1.1  jmcneill 	}
    460   1.1  jmcneill 
    461   1.1  jmcneill 	if (num_crtc == 0) {
    462   1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "no pipelines configured\n");
    463  1.10       mrg 		error = ENXIO;
    464  1.10       mrg 		goto drmerr;
    465   1.1  jmcneill 	}
    466   1.1  jmcneill 
    467   1.1  jmcneill 	fbdev = kmem_zalloc(sizeof(*fbdev), KM_SLEEP);
    468   1.1  jmcneill 
    469   1.1  jmcneill 	drm_fb_helper_prepare(ddev, &fbdev->helper, &sunxi_drm_fb_helper_funcs);
    470   1.1  jmcneill 
    471  1.16  riastrad 	error = drm_fb_helper_init(ddev, &fbdev->helper, num_crtc);
    472   1.1  jmcneill 	if (error)
    473  1.10       mrg 		goto allocerr;
    474   1.1  jmcneill 
    475   1.1  jmcneill 	fbdev->helper.fb = kmem_zalloc(sizeof(struct sunxi_drm_framebuffer), KM_SLEEP);
    476   1.1  jmcneill 
    477   1.1  jmcneill 	drm_fb_helper_single_add_all_connectors(&fbdev->helper);
    478   1.1  jmcneill 
    479   1.1  jmcneill 	drm_helper_disable_unused_functions(ddev);
    480   1.1  jmcneill 
    481   1.1  jmcneill 	drm_fb_helper_initial_config(&fbdev->helper, 32);
    482   1.1  jmcneill 
    483   1.6  jmcneill 	/* XXX */
    484   1.6  jmcneill 	ddev->irq_enabled = true;
    485   1.6  jmcneill 	drm_vblank_init(ddev, num_crtc);
    486   1.6  jmcneill 
    487   1.1  jmcneill 	return 0;
    488   1.1  jmcneill 
    489  1.10       mrg allocerr:
    490  1.10       mrg 	kmem_free(fbdev, sizeof(*fbdev));
    491   1.1  jmcneill drmerr:
    492   1.1  jmcneill 	drm_mode_config_cleanup(ddev);
    493   1.1  jmcneill 
    494   1.1  jmcneill 	return error;
    495   1.1  jmcneill }
    496   1.1  jmcneill 
    497   1.6  jmcneill static uint32_t
    498   1.6  jmcneill sunxi_drm_get_vblank_counter(struct drm_device *ddev, unsigned int crtc)
    499   1.6  jmcneill {
    500   1.6  jmcneill 	struct sunxi_drm_softc * const sc = sunxi_drm_private(ddev);
    501   1.6  jmcneill 
    502   1.6  jmcneill 	if (crtc >= __arraycount(sc->sc_vbl))
    503   1.6  jmcneill 		return 0;
    504   1.6  jmcneill 
    505   1.6  jmcneill 	if (sc->sc_vbl[crtc].get_vblank_counter == NULL)
    506   1.6  jmcneill 		return 0;
    507   1.6  jmcneill 
    508   1.6  jmcneill 	return sc->sc_vbl[crtc].get_vblank_counter(sc->sc_vbl[crtc].priv);
    509   1.6  jmcneill }
    510   1.6  jmcneill 
    511   1.6  jmcneill static int
    512   1.6  jmcneill sunxi_drm_enable_vblank(struct drm_device *ddev, unsigned int crtc)
    513   1.6  jmcneill {
    514   1.6  jmcneill 	struct sunxi_drm_softc * const sc = sunxi_drm_private(ddev);
    515   1.6  jmcneill 
    516   1.6  jmcneill 	if (crtc >= __arraycount(sc->sc_vbl))
    517   1.6  jmcneill 		return 0;
    518   1.6  jmcneill 
    519   1.6  jmcneill 	if (sc->sc_vbl[crtc].enable_vblank == NULL)
    520   1.6  jmcneill 		return 0;
    521   1.6  jmcneill 
    522   1.6  jmcneill 	sc->sc_vbl[crtc].enable_vblank(sc->sc_vbl[crtc].priv);
    523   1.6  jmcneill 
    524   1.6  jmcneill 	return 0;
    525   1.6  jmcneill }
    526   1.6  jmcneill 
    527   1.6  jmcneill static void
    528   1.6  jmcneill sunxi_drm_disable_vblank(struct drm_device *ddev, unsigned int crtc)
    529   1.6  jmcneill {
    530   1.6  jmcneill 	struct sunxi_drm_softc * const sc = sunxi_drm_private(ddev);
    531   1.6  jmcneill 
    532   1.6  jmcneill 	if (crtc >= __arraycount(sc->sc_vbl))
    533   1.6  jmcneill 		return;
    534   1.6  jmcneill 
    535   1.6  jmcneill 	if (sc->sc_vbl[crtc].disable_vblank == NULL)
    536   1.6  jmcneill 		return;
    537   1.6  jmcneill 
    538   1.6  jmcneill 	sc->sc_vbl[crtc].disable_vblank(sc->sc_vbl[crtc].priv);
    539   1.6  jmcneill }
    540   1.6  jmcneill 
    541  1.16  riastrad static void
    542   1.1  jmcneill sunxi_drm_unload(struct drm_device *ddev)
    543   1.1  jmcneill {
    544   1.1  jmcneill 	drm_mode_config_cleanup(ddev);
    545   1.1  jmcneill }
    546   1.1  jmcneill 
    547   1.1  jmcneill int
    548   1.1  jmcneill sunxi_drm_register_endpoint(int phandle, struct fdt_endpoint *ep)
    549   1.1  jmcneill {
    550   1.1  jmcneill 	struct sunxi_drm_endpoint *sep;
    551   1.1  jmcneill 
    552   1.1  jmcneill 	sep = kmem_zalloc(sizeof(*sep), KM_SLEEP);
    553   1.1  jmcneill 	sep->phandle = phandle;
    554   1.1  jmcneill 	sep->ep = ep;
    555   1.1  jmcneill 	sep->ddev = NULL;
    556   1.1  jmcneill 	TAILQ_INSERT_TAIL(&sunxi_drm_endpoints, sep, entries);
    557   1.1  jmcneill 
    558   1.1  jmcneill 	return 0;
    559   1.1  jmcneill }
    560   1.1  jmcneill 
    561   1.1  jmcneill struct drm_device *
    562   1.1  jmcneill sunxi_drm_endpoint_device(struct fdt_endpoint *ep)
    563   1.1  jmcneill {
    564   1.1  jmcneill 	struct sunxi_drm_endpoint *sep;
    565   1.1  jmcneill 
    566   1.1  jmcneill 	TAILQ_FOREACH(sep, &sunxi_drm_endpoints, entries)
    567   1.1  jmcneill 		if (sep->ep == ep)
    568   1.1  jmcneill 			return sep->ddev;
    569   1.1  jmcneill 
    570   1.1  jmcneill 	return NULL;
    571   1.1  jmcneill }
    572  1.22  riastrad 
    573  1.22  riastrad static void
    574  1.22  riastrad sunxi_drm_task_work(struct work *work, void *cookie)
    575  1.22  riastrad {
    576  1.22  riastrad 	struct sunxi_drm_task *task = container_of(work, struct sunxi_drm_task,
    577  1.22  riastrad 	    sdt_u.work);
    578  1.22  riastrad 
    579  1.22  riastrad 	(*task->sdt_fn)(task);
    580  1.22  riastrad }
    581  1.22  riastrad 
    582  1.22  riastrad void
    583  1.22  riastrad sunxi_task_init(struct sunxi_drm_task *task,
    584  1.22  riastrad     void (*fn)(struct sunxi_drm_task *))
    585  1.22  riastrad {
    586  1.22  riastrad 
    587  1.22  riastrad 	task->sdt_fn = fn;
    588  1.22  riastrad }
    589  1.22  riastrad 
    590  1.22  riastrad void
    591  1.22  riastrad sunxi_task_schedule(device_t self, struct sunxi_drm_task *task)
    592  1.22  riastrad {
    593  1.22  riastrad 	struct sunxi_drm_softc *sc = device_private(self);
    594  1.22  riastrad 
    595  1.22  riastrad 	if (atomic_load_relaxed(&sc->sc_task_thread) == curlwp)
    596  1.22  riastrad 		SIMPLEQ_INSERT_TAIL(&sc->sc_tasks, task, sdt_u.queue);
    597  1.22  riastrad 	else
    598  1.22  riastrad 		workqueue_enqueue(sc->sc_task_wq, &task->sdt_u.work, NULL);
    599  1.22  riastrad }
    600