Home | History | Annotate | Line # | Download | only in rockchip
rk_drm.c revision 1.16
      1  1.16  riastrad /* $NetBSD: rk_drm.c,v 1.16 2021/12/19 12:43:37 riastradh 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.16  riastrad __KERNEL_RCSID(0, "$NetBSD: rk_drm.c,v 1.16 2021/12/19 12:43:37 riastradh Exp $");
     31   1.1  jmcneill 
     32   1.1  jmcneill #include <sys/param.h>
     33   1.1  jmcneill #include <sys/bus.h>
     34   1.9  riastrad #include <sys/conf.h>
     35   1.1  jmcneill #include <sys/device.h>
     36   1.1  jmcneill #include <sys/intr.h>
     37   1.9  riastrad #include <sys/kernel.h>
     38   1.1  jmcneill #include <sys/systm.h>
     39   1.1  jmcneill 
     40   1.9  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.9  riastrad 
     44   1.9  riastrad #include <dev/fdt/fdt_port.h>
     45   1.9  riastrad #include <dev/fdt/fdtvar.h>
     46   1.9  riastrad 
     47   1.9  riastrad #include <arm/rockchip/rk_drm.h>
     48   1.1  jmcneill 
     49  1.16  riastrad #include <drm/drm_atomic_helper.h>
     50   1.8  riastrad #include <drm/drm_auth.h>
     51   1.9  riastrad #include <drm/drm_crtc_helper.h>
     52  1.16  riastrad #include <drm/drm_damage_helper.h>
     53   1.8  riastrad #include <drm/drm_drv.h>
     54   1.1  jmcneill #include <drm/drm_fb_helper.h>
     55   1.8  riastrad #include <drm/drm_fourcc.h>
     56   1.8  riastrad #include <drm/drm_vblank.h>
     57   1.1  jmcneill 
     58   1.1  jmcneill #define	RK_DRM_MAX_WIDTH	3840
     59   1.1  jmcneill #define	RK_DRM_MAX_HEIGHT	2160
     60   1.1  jmcneill 
     61   1.1  jmcneill static TAILQ_HEAD(, rk_drm_ports) rk_drm_ports =
     62   1.1  jmcneill     TAILQ_HEAD_INITIALIZER(rk_drm_ports);
     63   1.1  jmcneill 
     64   1.4   thorpej static const struct device_compatible_entry compat_data[] = {
     65   1.4   thorpej 	{ .compat = "rockchip,display-subsystem" },
     66   1.4   thorpej 	DEVICE_COMPAT_EOL
     67   1.1  jmcneill };
     68   1.1  jmcneill 
     69   1.1  jmcneill static const char * fb_compatible[] = {
     70   1.1  jmcneill 	"simple-framebuffer",
     71   1.1  jmcneill 	NULL
     72   1.1  jmcneill };
     73   1.1  jmcneill 
     74   1.1  jmcneill static int	rk_drm_match(device_t, cfdata_t, void *);
     75   1.1  jmcneill static void	rk_drm_attach(device_t, device_t, void *);
     76   1.1  jmcneill 
     77   1.1  jmcneill static void	rk_drm_init(device_t);
     78   1.1  jmcneill static vmem_t	*rk_drm_alloc_cma_pool(struct drm_device *, size_t);
     79   1.1  jmcneill 
     80   1.1  jmcneill static uint32_t	rk_drm_get_vblank_counter(struct drm_device *, unsigned int);
     81   1.1  jmcneill static int	rk_drm_enable_vblank(struct drm_device *, unsigned int);
     82   1.1  jmcneill static void	rk_drm_disable_vblank(struct drm_device *, unsigned int);
     83   1.1  jmcneill 
     84   1.1  jmcneill static int	rk_drm_load(struct drm_device *, unsigned long);
     85   1.8  riastrad static void	rk_drm_unload(struct drm_device *);
     86   1.1  jmcneill 
     87  1.12  riastrad static void	rk_drm_task_work(struct work *, void *);
     88  1.12  riastrad 
     89   1.1  jmcneill static struct drm_driver rk_drm_driver = {
     90  1.16  riastrad 	.driver_features = DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_GEM,
     91   1.1  jmcneill 	.dev_priv_size = 0,
     92   1.1  jmcneill 	.load = rk_drm_load,
     93   1.1  jmcneill 	.unload = rk_drm_unload,
     94   1.1  jmcneill 
     95   1.1  jmcneill 	.gem_free_object = drm_gem_cma_free_object,
     96   1.1  jmcneill 	.mmap_object = drm_gem_or_legacy_mmap_object,
     97   1.1  jmcneill 	.gem_uvm_ops = &drm_gem_cma_uvm_ops,
     98   1.1  jmcneill 
     99   1.1  jmcneill 	.dumb_create = drm_gem_cma_dumb_create,
    100   1.1  jmcneill 	.dumb_destroy = drm_gem_dumb_destroy,
    101   1.1  jmcneill 
    102   1.1  jmcneill 	.get_vblank_counter = rk_drm_get_vblank_counter,
    103   1.1  jmcneill 	.enable_vblank = rk_drm_enable_vblank,
    104   1.1  jmcneill 	.disable_vblank = rk_drm_disable_vblank,
    105   1.1  jmcneill 
    106   1.1  jmcneill 	.name = DRIVER_NAME,
    107   1.1  jmcneill 	.desc = DRIVER_DESC,
    108   1.1  jmcneill 	.date = DRIVER_DATE,
    109   1.1  jmcneill 	.major = DRIVER_MAJOR,
    110   1.1  jmcneill 	.minor = DRIVER_MINOR,
    111   1.1  jmcneill 	.patchlevel = DRIVER_PATCHLEVEL,
    112   1.1  jmcneill };
    113   1.1  jmcneill 
    114   1.1  jmcneill CFATTACH_DECL_NEW(rk_drm, sizeof(struct rk_drm_softc),
    115   1.1  jmcneill 	rk_drm_match, rk_drm_attach, NULL, NULL);
    116   1.1  jmcneill 
    117   1.1  jmcneill static int
    118   1.1  jmcneill rk_drm_match(device_t parent, cfdata_t cf, void *aux)
    119   1.1  jmcneill {
    120   1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    121   1.1  jmcneill 
    122   1.4   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    123   1.1  jmcneill }
    124   1.1  jmcneill 
    125   1.1  jmcneill static void
    126   1.1  jmcneill rk_drm_attach(device_t parent, device_t self, void *aux)
    127   1.1  jmcneill {
    128   1.1  jmcneill 	struct rk_drm_softc * const sc = device_private(self);
    129   1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    130   1.1  jmcneill 	struct drm_driver * const driver = &rk_drm_driver;
    131   1.1  jmcneill 	prop_dictionary_t dict = device_properties(self);
    132   1.1  jmcneill 	bool is_disabled;
    133   1.1  jmcneill 
    134  1.14  riastrad 	aprint_naive("\n");
    135  1.14  riastrad 
    136  1.14  riastrad 	if (prop_dictionary_get_bool(dict, "disabled", &is_disabled) &&
    137  1.14  riastrad 	    is_disabled) {
    138  1.14  riastrad 		aprint_normal(": (disabled)\n");
    139  1.14  riastrad 		return;
    140  1.14  riastrad 	}
    141  1.14  riastrad 
    142  1.14  riastrad 	aprint_normal("\n");
    143  1.14  riastrad 
    144   1.1  jmcneill 	sc->sc_dev = self;
    145   1.1  jmcneill 	sc->sc_dmat = faa->faa_dmat;
    146   1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    147   1.1  jmcneill 	sc->sc_phandle = faa->faa_phandle;
    148  1.12  riastrad 	sc->sc_task_thread = NULL;
    149  1.12  riastrad 	SIMPLEQ_INIT(&sc->sc_tasks);
    150  1.12  riastrad 	if (workqueue_create(&sc->sc_task_wq, "rkdrm",
    151  1.12  riastrad 	    &rk_drm_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE)) {
    152  1.12  riastrad 		aprint_error_dev(self, "unable to create workqueue\n");
    153  1.12  riastrad 		sc->sc_task_wq = NULL;
    154  1.12  riastrad 		return;
    155  1.12  riastrad 	}
    156   1.1  jmcneill 
    157   1.1  jmcneill 	sc->sc_ddev = drm_dev_alloc(driver, sc->sc_dev);
    158  1.10  riastrad 	if (IS_ERR(sc->sc_ddev)) {
    159   1.1  jmcneill 		aprint_error_dev(self, "couldn't allocate DRM device\n");
    160   1.1  jmcneill 		return;
    161   1.1  jmcneill 	}
    162   1.1  jmcneill 	sc->sc_ddev->dev_private = sc;
    163   1.1  jmcneill 	sc->sc_ddev->bst = sc->sc_bst;
    164   1.1  jmcneill 	sc->sc_ddev->bus_dmat = sc->sc_dmat;
    165   1.1  jmcneill 	sc->sc_ddev->dmat = sc->sc_ddev->bus_dmat;
    166   1.1  jmcneill 	sc->sc_ddev->dmat_subregion_p = false;
    167   1.1  jmcneill 
    168   1.1  jmcneill 	fdt_remove_bycompat(fb_compatible);
    169   1.1  jmcneill 
    170   1.1  jmcneill 	config_defer(self, rk_drm_init);
    171   1.1  jmcneill }
    172   1.1  jmcneill 
    173   1.1  jmcneill static void
    174   1.1  jmcneill rk_drm_init(device_t dev)
    175   1.1  jmcneill {
    176   1.1  jmcneill 	struct rk_drm_softc * const sc = device_private(dev);
    177   1.1  jmcneill 	struct drm_driver * const driver = &rk_drm_driver;
    178   1.1  jmcneill 	int error;
    179   1.1  jmcneill 
    180  1.12  riastrad 	/*
    181  1.12  riastrad 	 * Cause any tasks issued synchronously during attach to be
    182  1.12  riastrad 	 * processed at the end of this function.
    183  1.12  riastrad 	 */
    184  1.12  riastrad 	sc->sc_task_thread = curlwp;
    185  1.12  riastrad 
    186   1.1  jmcneill 	error = -drm_dev_register(sc->sc_ddev, 0);
    187   1.1  jmcneill 	if (error) {
    188   1.1  jmcneill 		aprint_error_dev(dev, "couldn't register DRM device: %d\n",
    189   1.1  jmcneill 		    error);
    190  1.12  riastrad 		goto out;
    191   1.1  jmcneill 	}
    192  1.12  riastrad 	sc->sc_dev_registered = true;
    193   1.1  jmcneill 
    194   1.1  jmcneill 	aprint_normal_dev(dev, "initialized %s %d.%d.%d %s on minor %d\n",
    195   1.1  jmcneill 	    driver->name, driver->major, driver->minor, driver->patchlevel,
    196   1.1  jmcneill 	    driver->date, sc->sc_ddev->primary->index);
    197  1.12  riastrad 
    198  1.12  riastrad 	/*
    199  1.12  riastrad 	 * Process asynchronous tasks queued synchronously during
    200  1.12  riastrad 	 * attach.  This will be for display detection to attach a
    201  1.12  riastrad 	 * framebuffer, so we have the opportunity for a console device
    202  1.12  riastrad 	 * to attach before autoconf has completed, in time for init(8)
    203  1.12  riastrad 	 * to find that console without panicking.
    204  1.12  riastrad 	 */
    205  1.12  riastrad 	while (!SIMPLEQ_EMPTY(&sc->sc_tasks)) {
    206  1.12  riastrad 		struct rk_drm_task *const task = SIMPLEQ_FIRST(&sc->sc_tasks);
    207  1.12  riastrad 
    208  1.12  riastrad 		SIMPLEQ_REMOVE_HEAD(&sc->sc_tasks, rdt_u.queue);
    209  1.12  riastrad 		(*task->rdt_fn)(task);
    210  1.12  riastrad 	}
    211  1.12  riastrad 
    212  1.12  riastrad out:	/* Cause any subesquent tasks to be processed by the workqueue.  */
    213  1.12  riastrad 	atomic_store_relaxed(&sc->sc_task_thread, NULL);
    214   1.1  jmcneill }
    215   1.1  jmcneill 
    216   1.1  jmcneill static vmem_t *
    217   1.1  jmcneill rk_drm_alloc_cma_pool(struct drm_device *ddev, size_t cma_size)
    218   1.1  jmcneill {
    219   1.1  jmcneill 	struct rk_drm_softc * const sc = rk_drm_private(ddev);
    220   1.1  jmcneill 	bus_dma_segment_t segs[1];
    221   1.1  jmcneill 	int nsegs;
    222   1.1  jmcneill 	int error;
    223   1.1  jmcneill 
    224   1.1  jmcneill 	error = bus_dmamem_alloc(sc->sc_dmat, cma_size, PAGE_SIZE, 0,
    225   1.1  jmcneill 	    segs, 1, &nsegs, BUS_DMA_NOWAIT);
    226   1.1  jmcneill 	if (error) {
    227   1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't allocate CMA pool\n");
    228   1.1  jmcneill 		return NULL;
    229   1.1  jmcneill 	}
    230   1.1  jmcneill 
    231   1.1  jmcneill 	return vmem_create("rkdrm", segs[0].ds_addr, segs[0].ds_len,
    232   1.1  jmcneill 	    PAGE_SIZE, NULL, NULL, NULL, 0, VM_SLEEP, IPL_NONE);
    233   1.1  jmcneill }
    234   1.1  jmcneill 
    235   1.1  jmcneill static int
    236   1.1  jmcneill rk_drm_fb_create_handle(struct drm_framebuffer *fb,
    237   1.1  jmcneill     struct drm_file *file, unsigned int *handle)
    238   1.1  jmcneill {
    239   1.1  jmcneill 	struct rk_drm_framebuffer *sfb = to_rk_drm_framebuffer(fb);
    240   1.1  jmcneill 
    241   1.1  jmcneill 	return drm_gem_handle_create(file, &sfb->obj->base, handle);
    242   1.1  jmcneill }
    243   1.1  jmcneill 
    244   1.1  jmcneill static void
    245   1.1  jmcneill rk_drm_fb_destroy(struct drm_framebuffer *fb)
    246   1.1  jmcneill {
    247   1.1  jmcneill 	struct rk_drm_framebuffer *sfb = to_rk_drm_framebuffer(fb);
    248   1.1  jmcneill 
    249   1.1  jmcneill 	drm_framebuffer_cleanup(fb);
    250   1.8  riastrad 	drm_gem_object_put_unlocked(&sfb->obj->base);
    251   1.1  jmcneill 	kmem_free(sfb, sizeof(*sfb));
    252   1.1  jmcneill }
    253   1.1  jmcneill 
    254   1.1  jmcneill static const struct drm_framebuffer_funcs rk_drm_framebuffer_funcs = {
    255   1.1  jmcneill 	.create_handle = rk_drm_fb_create_handle,
    256   1.1  jmcneill 	.destroy = rk_drm_fb_destroy,
    257  1.16  riastrad 	.dirty = drm_atomic_helper_dirtyfb,
    258   1.1  jmcneill };
    259   1.1  jmcneill 
    260   1.1  jmcneill static struct drm_framebuffer *
    261   1.1  jmcneill rk_drm_fb_create(struct drm_device *ddev, struct drm_file *file,
    262   1.8  riastrad     const struct drm_mode_fb_cmd2 *cmd)
    263   1.1  jmcneill {
    264   1.1  jmcneill 	struct rk_drm_framebuffer *fb;
    265   1.1  jmcneill 	struct drm_gem_object *gem_obj;
    266   1.1  jmcneill 	int error;
    267   1.1  jmcneill 
    268   1.1  jmcneill 	if (cmd->flags)
    269   1.1  jmcneill 		return NULL;
    270   1.1  jmcneill 
    271   1.8  riastrad 	gem_obj = drm_gem_object_lookup(file, cmd->handles[0]);
    272   1.1  jmcneill 	if (gem_obj == NULL)
    273   1.1  jmcneill 		return NULL;
    274   1.1  jmcneill 
    275   1.1  jmcneill 	fb = kmem_zalloc(sizeof(*fb), KM_SLEEP);
    276  1.13  riastrad 	drm_helper_mode_fill_fb_struct(ddev, &fb->base, cmd);
    277   1.1  jmcneill 	fb->obj = to_drm_gem_cma_obj(gem_obj);
    278   1.1  jmcneill 
    279   1.1  jmcneill 	error = drm_framebuffer_init(ddev, &fb->base, &rk_drm_framebuffer_funcs);
    280   1.1  jmcneill 	if (error != 0)
    281   1.1  jmcneill 		goto dealloc;
    282   1.1  jmcneill 
    283   1.1  jmcneill 	return &fb->base;
    284   1.1  jmcneill 
    285   1.1  jmcneill dealloc:
    286   1.1  jmcneill 	drm_framebuffer_cleanup(&fb->base);
    287   1.1  jmcneill 	kmem_free(fb, sizeof(*fb));
    288   1.8  riastrad 	drm_gem_object_put_unlocked(gem_obj);
    289   1.1  jmcneill 
    290   1.1  jmcneill 	return NULL;
    291   1.1  jmcneill }
    292   1.1  jmcneill 
    293   1.1  jmcneill static struct drm_mode_config_funcs rk_drm_mode_config_funcs = {
    294   1.1  jmcneill 	.fb_create = rk_drm_fb_create,
    295  1.16  riastrad 	.atomic_check = drm_atomic_helper_check,
    296  1.16  riastrad 	.atomic_commit = drm_atomic_helper_commit,
    297  1.16  riastrad };
    298  1.16  riastrad 
    299  1.16  riastrad static struct drm_mode_config_helper_funcs rk_drm_mode_config_helper_funcs = {
    300  1.16  riastrad 	.atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
    301   1.1  jmcneill };
    302   1.1  jmcneill 
    303   1.1  jmcneill static int
    304   1.1  jmcneill rk_drm_fb_probe(struct drm_fb_helper *helper, struct drm_fb_helper_surface_size *sizes)
    305   1.1  jmcneill {
    306   1.1  jmcneill 	struct rk_drm_softc * const sc = rk_drm_private(helper->dev);
    307   1.1  jmcneill 	struct drm_device *ddev = helper->dev;
    308   1.1  jmcneill 	struct rk_drm_framebuffer *sfb = to_rk_drm_framebuffer(helper->fb);
    309   1.1  jmcneill 	struct drm_framebuffer *fb = helper->fb;
    310   1.1  jmcneill 	struct rk_drmfb_attach_args sfa;
    311   1.1  jmcneill 	size_t cma_size;
    312   1.1  jmcneill 	int error;
    313   1.1  jmcneill 
    314   1.1  jmcneill 	const u_int width = sizes->surface_width;
    315   1.1  jmcneill 	const u_int height = sizes->surface_height;
    316   1.1  jmcneill 	const u_int pitch = width * (32 / 8);
    317   1.1  jmcneill 
    318   1.1  jmcneill 	const size_t size = roundup(height * pitch, PAGE_SIZE);
    319   1.1  jmcneill 
    320   1.1  jmcneill 	/* Reserve enough memory for the FB console plus a 4K plane, rounded to 1MB */
    321   1.1  jmcneill 	cma_size = size;
    322   1.1  jmcneill 	cma_size += (RK_DRM_MAX_WIDTH * RK_DRM_MAX_HEIGHT * 4);
    323   1.1  jmcneill 	cma_size = roundup(cma_size, 1024 * 1024);
    324   1.1  jmcneill 	sc->sc_ddev->cma_pool = rk_drm_alloc_cma_pool(sc->sc_ddev, cma_size);
    325   1.1  jmcneill 	if (sc->sc_ddev->cma_pool != NULL)
    326   1.1  jmcneill 		aprint_normal_dev(sc->sc_dev, "reserved %u MB DRAM for CMA\n",
    327   1.1  jmcneill 		    (u_int)(cma_size / (1024 * 1024)));
    328   1.1  jmcneill 
    329   1.1  jmcneill 	sfb->obj = drm_gem_cma_create(ddev, size);
    330   1.1  jmcneill 	if (sfb->obj == NULL) {
    331   1.1  jmcneill 		DRM_ERROR("failed to allocate memory for framebuffer\n");
    332   1.1  jmcneill 		return -ENOMEM;
    333   1.1  jmcneill 	}
    334   1.1  jmcneill 
    335  1.13  riastrad 	/* similar to drm_helper_mode_fill_fb_struct(), but we have no cmd */
    336   1.1  jmcneill 	fb->pitches[0] = pitch;
    337   1.1  jmcneill 	fb->offsets[0] = 0;
    338   1.1  jmcneill 	fb->width = width;
    339   1.1  jmcneill 	fb->height = height;
    340  1.13  riastrad 	fb->modifier = 0;
    341  1.13  riastrad 	fb->flags = 0;
    342   1.6       mrg #ifdef __ARM_BIG_ENDIAN
    343   1.8  riastrad 	fb->format = drm_format_info(DRM_FORMAT_BGRX8888);
    344   1.6       mrg #else
    345   1.8  riastrad 	fb->format = drm_format_info(DRM_FORMAT_XRGB8888);
    346   1.6       mrg #endif
    347  1.16  riastrad 	fb->dev = ddev;
    348   1.1  jmcneill 
    349   1.1  jmcneill 	error = drm_framebuffer_init(ddev, fb, &rk_drm_framebuffer_funcs);
    350   1.1  jmcneill 	if (error != 0) {
    351   1.1  jmcneill 		DRM_ERROR("failed to initialize framebuffer\n");
    352   1.1  jmcneill 		return error;
    353   1.1  jmcneill 	}
    354   1.1  jmcneill 
    355   1.1  jmcneill 	memset(&sfa, 0, sizeof(sfa));
    356   1.1  jmcneill 	sfa.sfa_drm_dev = ddev;
    357   1.1  jmcneill 	sfa.sfa_fb_helper = helper;
    358   1.1  jmcneill 	sfa.sfa_fb_sizes = *sizes;
    359   1.1  jmcneill 	sfa.sfa_fb_bst = sc->sc_bst;
    360   1.1  jmcneill 	sfa.sfa_fb_dmat = sc->sc_dmat;
    361   1.1  jmcneill 	sfa.sfa_fb_linebytes = helper->fb->pitches[0];
    362   1.1  jmcneill 
    363   1.5   thorpej 	helper->fbdev = config_found(ddev->dev, &sfa, NULL,
    364   1.7   thorpej 	    CFARGS(.iattr = "rkfbbus"));
    365   1.1  jmcneill 	if (helper->fbdev == NULL) {
    366   1.1  jmcneill 		DRM_ERROR("unable to attach framebuffer\n");
    367   1.1  jmcneill 		return -ENXIO;
    368   1.1  jmcneill 	}
    369   1.1  jmcneill 
    370   1.1  jmcneill 	return 0;
    371   1.1  jmcneill }
    372   1.1  jmcneill 
    373   1.1  jmcneill static struct drm_fb_helper_funcs rk_drm_fb_helper_funcs = {
    374   1.1  jmcneill 	.fb_probe = rk_drm_fb_probe,
    375   1.1  jmcneill };
    376   1.1  jmcneill 
    377   1.1  jmcneill static int
    378   1.1  jmcneill rk_drm_load(struct drm_device *ddev, unsigned long flags)
    379   1.1  jmcneill {
    380   1.1  jmcneill 	struct rk_drm_softc * const sc = rk_drm_private(ddev);
    381   1.1  jmcneill 	struct rk_drm_ports *sport;
    382   1.1  jmcneill 	struct rk_drm_fbdev *fbdev;
    383   1.1  jmcneill 	struct fdt_endpoint *ep;
    384   1.1  jmcneill 	const u_int *data;
    385   1.1  jmcneill 	int datalen, error, num_crtc, ep_index;
    386   1.1  jmcneill 
    387   1.1  jmcneill 	drm_mode_config_init(ddev);
    388   1.1  jmcneill 	ddev->mode_config.min_width = 0;
    389   1.1  jmcneill 	ddev->mode_config.min_height = 0;
    390   1.1  jmcneill 	ddev->mode_config.max_width = RK_DRM_MAX_WIDTH;
    391   1.1  jmcneill 	ddev->mode_config.max_height = RK_DRM_MAX_HEIGHT;
    392   1.1  jmcneill 	ddev->mode_config.funcs = &rk_drm_mode_config_funcs;
    393  1.16  riastrad 	ddev->mode_config.helper_private = &rk_drm_mode_config_helper_funcs;
    394   1.1  jmcneill 
    395   1.1  jmcneill 	num_crtc = 0;
    396   1.1  jmcneill 	data = fdtbus_get_prop(sc->sc_phandle, "ports", &datalen);
    397   1.1  jmcneill 	while (datalen >= 4) {
    398   1.1  jmcneill 		const int crtc_phandle = fdtbus_get_phandle_from_native(be32dec(data));
    399   1.1  jmcneill 
    400   1.1  jmcneill 		TAILQ_FOREACH(sport, &rk_drm_ports, entries)
    401   1.1  jmcneill 			if (sport->phandle == crtc_phandle && sport->ddev == NULL) {
    402   1.1  jmcneill 				sport->ddev = ddev;
    403   1.1  jmcneill 				for (ep_index = 0; (ep = fdt_endpoint_get_from_index(sport->port, 0, ep_index)) != NULL; ep_index++) {
    404   1.1  jmcneill 					error = fdt_endpoint_activate_direct(ep, true);
    405   1.1  jmcneill 					if (error != 0)
    406   1.1  jmcneill 						aprint_debug_dev(sc->sc_dev,
    407   1.1  jmcneill 						    "failed to activate endpoint %d: %d\n",
    408   1.1  jmcneill 						    ep_index, error);
    409   1.1  jmcneill 				}
    410   1.1  jmcneill 				num_crtc++;
    411   1.1  jmcneill 			}
    412   1.1  jmcneill 
    413   1.1  jmcneill 		datalen -= 4;
    414   1.1  jmcneill 		data++;
    415   1.1  jmcneill 	}
    416   1.1  jmcneill 
    417   1.1  jmcneill 	if (num_crtc == 0) {
    418   1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "no display interface ports configured\n");
    419   1.3       mrg 		error = ENXIO;
    420   1.3       mrg 		goto drmerr;
    421   1.1  jmcneill 	}
    422   1.1  jmcneill 
    423  1.15  riastrad 	drm_mode_config_reset(ddev);
    424  1.15  riastrad 
    425   1.1  jmcneill 	fbdev = kmem_zalloc(sizeof(*fbdev), KM_SLEEP);
    426   1.1  jmcneill 
    427   1.1  jmcneill 	drm_fb_helper_prepare(ddev, &fbdev->helper, &rk_drm_fb_helper_funcs);
    428   1.1  jmcneill 
    429   1.8  riastrad 	error = drm_fb_helper_init(ddev, &fbdev->helper, num_crtc);
    430   1.1  jmcneill 	if (error)
    431   1.3       mrg 		goto allocerr;
    432   1.1  jmcneill 
    433   1.1  jmcneill 	fbdev->helper.fb = kmem_zalloc(sizeof(struct rk_drm_framebuffer), KM_SLEEP);
    434   1.1  jmcneill 
    435   1.1  jmcneill 	drm_fb_helper_single_add_all_connectors(&fbdev->helper);
    436   1.1  jmcneill 
    437   1.1  jmcneill 	drm_fb_helper_initial_config(&fbdev->helper, 32);
    438   1.1  jmcneill 
    439   1.1  jmcneill 	/* XXX */
    440   1.1  jmcneill 	ddev->irq_enabled = true;
    441   1.1  jmcneill 	drm_vblank_init(ddev, num_crtc);
    442   1.1  jmcneill 
    443   1.1  jmcneill 	return 0;
    444   1.1  jmcneill 
    445   1.3       mrg allocerr:
    446   1.3       mrg 	kmem_free(fbdev, sizeof(*fbdev));
    447   1.1  jmcneill drmerr:
    448   1.1  jmcneill 	drm_mode_config_cleanup(ddev);
    449   1.1  jmcneill 
    450   1.1  jmcneill 	return error;
    451   1.1  jmcneill }
    452   1.1  jmcneill 
    453   1.1  jmcneill static uint32_t
    454   1.1  jmcneill rk_drm_get_vblank_counter(struct drm_device *ddev, unsigned int crtc)
    455   1.1  jmcneill {
    456   1.1  jmcneill 	struct rk_drm_softc * const sc = rk_drm_private(ddev);
    457   1.1  jmcneill 
    458   1.1  jmcneill 	if (crtc >= __arraycount(sc->sc_vbl))
    459   1.1  jmcneill 		return 0;
    460   1.1  jmcneill 
    461   1.1  jmcneill 	if (sc->sc_vbl[crtc].get_vblank_counter == NULL)
    462   1.1  jmcneill 		return 0;
    463   1.1  jmcneill 
    464   1.1  jmcneill 	return sc->sc_vbl[crtc].get_vblank_counter(sc->sc_vbl[crtc].priv);
    465   1.1  jmcneill }
    466   1.1  jmcneill 
    467   1.1  jmcneill static int
    468   1.1  jmcneill rk_drm_enable_vblank(struct drm_device *ddev, unsigned int crtc)
    469   1.1  jmcneill {
    470   1.1  jmcneill 	struct rk_drm_softc * const sc = rk_drm_private(ddev);
    471   1.1  jmcneill 
    472   1.1  jmcneill 	if (crtc >= __arraycount(sc->sc_vbl))
    473   1.1  jmcneill 		return 0;
    474   1.1  jmcneill 
    475   1.1  jmcneill 	if (sc->sc_vbl[crtc].enable_vblank == NULL)
    476   1.1  jmcneill 		return 0;
    477   1.1  jmcneill 
    478   1.1  jmcneill 	sc->sc_vbl[crtc].enable_vblank(sc->sc_vbl[crtc].priv);
    479   1.1  jmcneill 
    480   1.1  jmcneill 	return 0;
    481   1.1  jmcneill }
    482   1.1  jmcneill 
    483   1.1  jmcneill static void
    484   1.1  jmcneill rk_drm_disable_vblank(struct drm_device *ddev, unsigned int crtc)
    485   1.1  jmcneill {
    486   1.1  jmcneill 	struct rk_drm_softc * const sc = rk_drm_private(ddev);
    487   1.1  jmcneill 
    488   1.1  jmcneill 	if (crtc >= __arraycount(sc->sc_vbl))
    489   1.1  jmcneill 		return;
    490   1.1  jmcneill 
    491   1.1  jmcneill 	if (sc->sc_vbl[crtc].disable_vblank == NULL)
    492   1.1  jmcneill 		return;
    493   1.1  jmcneill 
    494   1.1  jmcneill 	sc->sc_vbl[crtc].disable_vblank(sc->sc_vbl[crtc].priv);
    495   1.1  jmcneill }
    496   1.1  jmcneill 
    497   1.8  riastrad static void
    498   1.1  jmcneill rk_drm_unload(struct drm_device *ddev)
    499   1.1  jmcneill {
    500   1.1  jmcneill 	drm_mode_config_cleanup(ddev);
    501   1.1  jmcneill }
    502   1.1  jmcneill 
    503   1.1  jmcneill int
    504   1.1  jmcneill rk_drm_register_port(int phandle, struct fdt_device_ports *port)
    505   1.1  jmcneill {
    506   1.1  jmcneill 	struct rk_drm_ports *sport;
    507   1.1  jmcneill 
    508   1.1  jmcneill 	sport = kmem_zalloc(sizeof(*sport), KM_SLEEP);
    509   1.1  jmcneill 	sport->phandle = phandle;
    510   1.1  jmcneill 	sport->port = port;
    511   1.1  jmcneill 	sport->ddev = NULL;
    512   1.1  jmcneill 	TAILQ_INSERT_TAIL(&rk_drm_ports, sport, entries);
    513   1.1  jmcneill 
    514   1.1  jmcneill 	return 0;
    515   1.1  jmcneill }
    516   1.1  jmcneill 
    517   1.1  jmcneill struct drm_device *
    518   1.1  jmcneill rk_drm_port_device(struct fdt_device_ports *port)
    519   1.1  jmcneill {
    520   1.1  jmcneill 	struct rk_drm_ports *sport;
    521   1.1  jmcneill 
    522   1.1  jmcneill 	TAILQ_FOREACH(sport, &rk_drm_ports, entries)
    523   1.1  jmcneill 		if (sport->port == port)
    524   1.1  jmcneill 			return sport->ddev;
    525   1.1  jmcneill 
    526   1.1  jmcneill 	return NULL;
    527   1.1  jmcneill }
    528  1.12  riastrad 
    529  1.12  riastrad static void
    530  1.12  riastrad rk_drm_task_work(struct work *work, void *cookie)
    531  1.12  riastrad {
    532  1.12  riastrad 	struct rk_drm_task *task = container_of(work, struct rk_drm_task,
    533  1.12  riastrad 	    rdt_u.work);
    534  1.12  riastrad 
    535  1.12  riastrad 	(*task->rdt_fn)(task);
    536  1.12  riastrad }
    537  1.12  riastrad 
    538  1.12  riastrad void
    539  1.12  riastrad rk_task_init(struct rk_drm_task *task,
    540  1.12  riastrad     void (*fn)(struct rk_drm_task *))
    541  1.12  riastrad {
    542  1.12  riastrad 
    543  1.12  riastrad 	task->rdt_fn = fn;
    544  1.12  riastrad }
    545  1.12  riastrad 
    546  1.12  riastrad void
    547  1.12  riastrad rk_task_schedule(device_t self, struct rk_drm_task *task)
    548  1.12  riastrad {
    549  1.12  riastrad 	struct rk_drm_softc *sc = device_private(self);
    550  1.12  riastrad 
    551  1.12  riastrad 	if (atomic_load_relaxed(&sc->sc_task_thread) == curlwp)
    552  1.12  riastrad 		SIMPLEQ_INSERT_TAIL(&sc->sc_tasks, task, rdt_u.queue);
    553  1.12  riastrad 	else
    554  1.12  riastrad 		workqueue_enqueue(sc->sc_task_wq, &task->rdt_u.work, NULL);
    555  1.12  riastrad }
    556