Home | History | Annotate | Line # | Download | only in sunxi
sunxi_drm.c revision 1.5
      1 /* $NetBSD: sunxi_drm.c,v 1.5 2019/02/03 15:43:57 jmcneill 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.5 2019/02/03 15:43:57 jmcneill Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/bus.h>
     34 #include <sys/device.h>
     35 #include <sys/intr.h>
     36 #include <sys/systm.h>
     37 #include <sys/kernel.h>
     38 #include <sys/conf.h>
     39 
     40 #include <uvm/uvm_extern.h>
     41 #include <uvm/uvm_object.h>
     42 #include <uvm/uvm_device.h>
     43 
     44 #include <drm/drmP.h>
     45 #include <drm/drm_crtc_helper.h>
     46 #include <drm/drm_fb_helper.h>
     47 
     48 #include <dev/fdt/fdtvar.h>
     49 #include <dev/fdt/fdt_port.h>
     50 
     51 #include <arm/sunxi/sunxi_drm.h>
     52 
     53 static TAILQ_HEAD(, sunxi_drm_endpoint) sunxi_drm_endpoints =
     54     TAILQ_HEAD_INITIALIZER(sunxi_drm_endpoints);
     55 
     56 static const char * const compatible[] = {
     57 	"allwinner,sun8i-h3-display-engine",
     58 	"allwinner,sun50i-a64-display-engine",
     59 	NULL
     60 };
     61 
     62 static const char * fb_compatible[] = {
     63 	"allwinner,simple-framebuffer",
     64 	NULL
     65 };
     66 
     67 static int	sunxi_drm_match(device_t, cfdata_t, void *);
     68 static void	sunxi_drm_attach(device_t, device_t, void *);
     69 
     70 static void	sunxi_drm_init(device_t);
     71 
     72 static int	sunxi_drm_set_busid(struct drm_device *, struct drm_master *);
     73 
     74 static int	sunxi_drm_load(struct drm_device *, unsigned long);
     75 static int	sunxi_drm_unload(struct drm_device *);
     76 
     77 static struct drm_driver sunxi_drm_driver = {
     78 	.driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
     79 	.dev_priv_size = 0,
     80 	.load = sunxi_drm_load,
     81 	.unload = sunxi_drm_unload,
     82 
     83 	.gem_free_object = drm_gem_cma_free_object,
     84 	.mmap_object = drm_gem_or_legacy_mmap_object,
     85 	.gem_uvm_ops = &drm_gem_cma_uvm_ops,
     86 
     87 	.dumb_create = drm_gem_cma_dumb_create,
     88 	.dumb_map_offset = drm_gem_cma_dumb_map_offset,
     89 	.dumb_destroy = drm_gem_dumb_destroy,
     90 
     91 #if notyet
     92 	.get_vblank_counter = sunxi_drm_get_vblank_counter,
     93 	.enable_vblank = sunxi_drm_enable_vblank,
     94 	.disable_vblank = sunxi_drm_disable_vblank,
     95 #endif
     96 
     97 	.name = DRIVER_NAME,
     98 	.desc = DRIVER_DESC,
     99 	.date = DRIVER_DATE,
    100 	.major = DRIVER_MAJOR,
    101 	.minor = DRIVER_MINOR,
    102 	.patchlevel = DRIVER_PATCHLEVEL,
    103 
    104 	.set_busid = sunxi_drm_set_busid,
    105 };
    106 
    107 CFATTACH_DECL_NEW(sunxi_drm, sizeof(struct sunxi_drm_softc),
    108 	sunxi_drm_match, sunxi_drm_attach, NULL, NULL);
    109 
    110 static int
    111 sunxi_drm_match(device_t parent, cfdata_t cf, void *aux)
    112 {
    113 	struct fdt_attach_args * const faa = aux;
    114 
    115 	return of_match_compatible(faa->faa_phandle, compatible);
    116 }
    117 
    118 static void
    119 sunxi_drm_attach(device_t parent, device_t self, void *aux)
    120 {
    121 	struct sunxi_drm_softc * const sc = device_private(self);
    122 	struct fdt_attach_args * const faa = aux;
    123 	struct drm_driver * const driver = &sunxi_drm_driver;
    124 	prop_dictionary_t dict = device_properties(self);
    125 	bool is_disabled;
    126 
    127 	sc->sc_dev = self;
    128 	sc->sc_dmat = faa->faa_dmat;
    129 	sc->sc_bst = faa->faa_bst;
    130 	sc->sc_phandle = faa->faa_phandle;
    131 
    132 	aprint_naive("\n");
    133 
    134 	if (prop_dictionary_get_bool(dict, "disabled", &is_disabled) && is_disabled) {
    135 		aprint_normal(": Display Engine Pipeline (disabled)\n");
    136 		return;
    137 	}
    138 
    139 	aprint_normal(": Display Engine Pipeline\n");
    140 
    141 	sc->sc_ddev = drm_dev_alloc(driver, sc->sc_dev);
    142 	if (sc->sc_ddev == NULL) {
    143 		aprint_error_dev(self, "couldn't allocate DRM device\n");
    144 		return;
    145 	}
    146 	sc->sc_ddev->dev_private = sc;
    147 	sc->sc_ddev->bst = sc->sc_bst;
    148 	sc->sc_ddev->bus_dmat = sc->sc_dmat;
    149 	sc->sc_ddev->dmat = sc->sc_ddev->bus_dmat;
    150 	sc->sc_ddev->dmat_subregion_p = false;
    151 
    152 	fdt_remove_bycompat(fb_compatible);
    153 
    154 	config_defer(self, sunxi_drm_init);
    155 }
    156 
    157 static void
    158 sunxi_drm_init(device_t dev)
    159 {
    160 	struct sunxi_drm_softc * const sc = device_private(dev);
    161 	struct drm_driver * const driver = &sunxi_drm_driver;
    162 	int error;
    163 
    164 	error = -drm_dev_register(sc->sc_ddev, 0);
    165 	if (error) {
    166 		drm_dev_unref(sc->sc_ddev);
    167 		aprint_error_dev(dev, "couldn't register DRM device: %d\n",
    168 		    error);
    169 		return;
    170 	}
    171 
    172 	aprint_normal_dev(dev, "initialized %s %d.%d.%d %s on minor %d\n",
    173 	    driver->name, driver->major, driver->minor, driver->patchlevel,
    174 	    driver->date, sc->sc_ddev->primary->index);
    175 }
    176 
    177 static int
    178 sunxi_drm_set_busid(struct drm_device *ddev, struct drm_master *master)
    179 {
    180 	struct sunxi_drm_softc * const sc = sunxi_drm_private(ddev);
    181 	char id[32];
    182 
    183 	snprintf(id, sizeof(id), "platform:sunxi:%u", device_unit(sc->sc_dev));
    184 
    185 	master->unique = kzalloc(strlen(id) + 1, GFP_KERNEL);
    186 	if (master->unique == NULL)
    187 		return -ENOMEM;
    188 	strcpy(master->unique, id);
    189 	master->unique_len = strlen(master->unique);
    190 
    191 	return 0;
    192 }
    193 
    194 static int
    195 sunxi_drm_fb_create_handle(struct drm_framebuffer *fb,
    196     struct drm_file *file, unsigned int *handle)
    197 {
    198 	struct sunxi_drm_framebuffer *sfb = to_sunxi_drm_framebuffer(fb);
    199 
    200 	return drm_gem_handle_create(file, &sfb->obj->base, handle);
    201 }
    202 
    203 static void
    204 sunxi_drm_fb_destroy(struct drm_framebuffer *fb)
    205 {
    206 	struct sunxi_drm_framebuffer *sfb = to_sunxi_drm_framebuffer(fb);
    207 
    208 	drm_framebuffer_cleanup(fb);
    209 	drm_gem_object_unreference_unlocked(&sfb->obj->base);
    210 	kmem_free(sfb, sizeof(*sfb));
    211 }
    212 
    213 static const struct drm_framebuffer_funcs sunxi_drm_framebuffer_funcs = {
    214 	.create_handle = sunxi_drm_fb_create_handle,
    215 	.destroy = sunxi_drm_fb_destroy,
    216 };
    217 
    218 static struct drm_framebuffer *
    219 sunxi_drm_fb_create(struct drm_device *ddev, struct drm_file *file,
    220     struct drm_mode_fb_cmd2 *cmd)
    221 {
    222 	struct sunxi_drm_framebuffer *fb;
    223 	struct drm_gem_object *gem_obj;
    224 	int error;
    225 
    226 	if (cmd->flags)
    227 		return NULL;
    228 
    229 	gem_obj = drm_gem_object_lookup(ddev, file, cmd->handles[0]);
    230 	if (gem_obj == NULL)
    231 		return NULL;
    232 
    233 	fb = kmem_zalloc(sizeof(*fb), KM_SLEEP);
    234 	fb->obj = to_drm_gem_cma_obj(gem_obj);
    235 	fb->base.pitches[0] = cmd->pitches[0];
    236 	fb->base.offsets[0] = cmd->offsets[0];
    237 	fb->base.width = cmd->width;
    238 	fb->base.height = cmd->height;
    239 	fb->base.pixel_format = cmd->pixel_format;
    240 	drm_fb_get_bpp_depth(cmd->pixel_format, &fb->base.depth,
    241 	    &fb->base.bits_per_pixel);
    242 
    243 	error = drm_framebuffer_init(ddev, &fb->base, &sunxi_drm_framebuffer_funcs);
    244 	if (error != 0)
    245 		goto dealloc;
    246 
    247 	return &fb->base;
    248 
    249 dealloc:
    250 	drm_framebuffer_cleanup(&fb->base);
    251 	kmem_free(fb, sizeof(*fb));
    252 	drm_gem_object_unreference_unlocked(gem_obj);
    253 
    254 	return NULL;
    255 }
    256 
    257 static struct drm_mode_config_funcs sunxi_drm_mode_config_funcs = {
    258 	.fb_create = sunxi_drm_fb_create,
    259 };
    260 
    261 static int
    262 sunxi_drm_fb_probe(struct drm_fb_helper *helper, struct drm_fb_helper_surface_size *sizes)
    263 {
    264 	struct sunxi_drm_softc * const sc = sunxi_drm_private(helper->dev);
    265 	struct drm_device *ddev = helper->dev;
    266 	struct sunxi_drm_framebuffer *sfb = to_sunxi_drm_framebuffer(helper->fb);
    267 	struct drm_framebuffer *fb = helper->fb;
    268 	struct sunxi_drmfb_attach_args sfa;
    269 	int error;
    270 
    271 	const u_int width = sizes->surface_width;
    272 	const u_int height = sizes->surface_height;
    273 	const u_int pitch = width * (32 / 8);
    274 
    275 	const size_t size = roundup(height * pitch, PAGE_SIZE);
    276 
    277 	sfb->obj = drm_gem_cma_create(ddev, size);
    278 	if (sfb->obj == NULL) {
    279 		DRM_ERROR("failed to allocate memory for framebuffer\n");
    280 		return -ENOMEM;
    281 	}
    282 
    283 	fb->pitches[0] = pitch;
    284 	fb->offsets[0] = 0;
    285 	fb->width = width;
    286 	fb->height = height;
    287 	fb->pixel_format = DRM_FORMAT_XRGB8888;
    288 	drm_fb_get_bpp_depth(fb->pixel_format, &fb->depth, &fb->bits_per_pixel);
    289 
    290 	error = drm_framebuffer_init(ddev, fb, &sunxi_drm_framebuffer_funcs);
    291 	if (error != 0) {
    292 		DRM_ERROR("failed to initialize framebuffer\n");
    293 		return error;
    294 	}
    295 
    296 	memset(&sfa, 0, sizeof(sfa));
    297 	sfa.sfa_drm_dev = ddev;
    298 	sfa.sfa_fb_helper = helper;
    299 	sfa.sfa_fb_sizes = *sizes;
    300 	sfa.sfa_fb_bst = sc->sc_bst;
    301 	sfa.sfa_fb_dmat = sc->sc_dmat;
    302 	sfa.sfa_fb_linebytes = helper->fb->pitches[0];
    303 
    304 	helper->fbdev = config_found_ia(ddev->dev, "sunxifbbus", &sfa, NULL);
    305 	if (helper->fbdev == NULL) {
    306 		DRM_ERROR("unable to attach framebuffer\n");
    307 		return -ENXIO;
    308 	}
    309 
    310 	return 0;
    311 }
    312 
    313 static struct drm_fb_helper_funcs sunxi_drm_fb_helper_funcs = {
    314 	.fb_probe = sunxi_drm_fb_probe,
    315 };
    316 
    317 static int
    318 sunxi_drm_load(struct drm_device *ddev, unsigned long flags)
    319 {
    320 	struct sunxi_drm_softc * const sc = sunxi_drm_private(ddev);
    321 	struct sunxi_drm_endpoint *sep;
    322 	struct sunxi_drm_fbdev *fbdev;
    323 	const u_int *data;
    324 	int datalen, error, num_crtc;
    325 
    326 	drm_mode_config_init(ddev);
    327 	ddev->mode_config.min_width = 0;
    328 	ddev->mode_config.min_height = 0;
    329 	ddev->mode_config.max_width = 3840;
    330 	ddev->mode_config.max_height = 2160;
    331 	ddev->mode_config.funcs = &sunxi_drm_mode_config_funcs;
    332 
    333 	num_crtc = 0;
    334 	data = fdtbus_get_prop(sc->sc_phandle, "allwinner,pipelines", &datalen);
    335 	while (datalen >= 4) {
    336 		const int crtc_phandle = fdtbus_get_phandle_from_native(be32dec(data));
    337 
    338 		TAILQ_FOREACH(sep, &sunxi_drm_endpoints, entries)
    339 			if (sep->phandle == crtc_phandle && sep->ddev == NULL) {
    340 				sep->ddev = ddev;
    341 				error = fdt_endpoint_activate_direct(sep->ep, true);
    342 				if (error != 0) {
    343 					aprint_error_dev(sc->sc_dev, "failed to activate endpoint: %d\n",
    344 					    error);
    345 				}
    346 				if (fdt_endpoint_type(sep->ep) == EP_DRM_CRTC)
    347 					num_crtc++;
    348 			}
    349 
    350 		datalen -= 4;
    351 		data++;
    352 	}
    353 
    354 	if (num_crtc == 0) {
    355 		aprint_error_dev(sc->sc_dev, "no pipelines configured\n");
    356 		return ENXIO;
    357 	}
    358 
    359 	fbdev = kmem_zalloc(sizeof(*fbdev), KM_SLEEP);
    360 
    361 	drm_fb_helper_prepare(ddev, &fbdev->helper, &sunxi_drm_fb_helper_funcs);
    362 
    363 	error = drm_fb_helper_init(ddev, &fbdev->helper, num_crtc, num_crtc);
    364 	if (error)
    365 		goto drmerr;
    366 
    367 	fbdev->helper.fb = kmem_zalloc(sizeof(struct sunxi_drm_framebuffer), KM_SLEEP);
    368 
    369 	drm_fb_helper_single_add_all_connectors(&fbdev->helper);
    370 
    371 	drm_helper_disable_unused_functions(ddev);
    372 
    373 	drm_fb_helper_initial_config(&fbdev->helper, 32);
    374 
    375 	return 0;
    376 
    377 drmerr:
    378 	drm_mode_config_cleanup(ddev);
    379 	kmem_free(fbdev, sizeof(*fbdev));
    380 
    381 	return error;
    382 }
    383 
    384 static int
    385 sunxi_drm_unload(struct drm_device *ddev)
    386 {
    387 	drm_mode_config_cleanup(ddev);
    388 
    389 	return 0;
    390 }
    391 
    392 int
    393 sunxi_drm_register_endpoint(int phandle, struct fdt_endpoint *ep)
    394 {
    395 	struct sunxi_drm_endpoint *sep;
    396 
    397 	sep = kmem_zalloc(sizeof(*sep), KM_SLEEP);
    398 	sep->phandle = phandle;
    399 	sep->ep = ep;
    400 	sep->ddev = NULL;
    401 	TAILQ_INSERT_TAIL(&sunxi_drm_endpoints, sep, entries);
    402 
    403 	return 0;
    404 }
    405 
    406 struct drm_device *
    407 sunxi_drm_endpoint_device(struct fdt_endpoint *ep)
    408 {
    409 	struct sunxi_drm_endpoint *sep;
    410 
    411 	TAILQ_FOREACH(sep, &sunxi_drm_endpoints, entries)
    412 		if (sep->ep == ep)
    413 			return sep->ddev;
    414 
    415 	return NULL;
    416 }
    417