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