Home | History | Annotate | Line # | Download | only in nvidia
tegra_drm_fb.c revision 1.6
      1 /* $NetBSD: tegra_drm_fb.c,v 1.6 2017/12/26 14:54:52 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2015 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: tegra_drm_fb.c,v 1.6 2017/12/26 14:54:52 jmcneill Exp $");
     31 
     32 #include <drm/drmP.h>
     33 #include <drm/drm_crtc.h>
     34 #include <drm/drm_fb_helper.h>
     35 #include <drm/drm_crtc_helper.h>
     36 
     37 #include <arm/nvidia/tegra_drm.h>
     38 
     39 static int	tegra_fb_init(struct drm_device *, struct drm_framebuffer *,
     40 		    struct drm_fb_helper_surface_size *);
     41 
     42 static int	tegra_fb_probe(struct drm_fb_helper *,
     43 		    struct drm_fb_helper_surface_size *);
     44 
     45 static struct drm_fb_helper_funcs tegra_fb_helper_funcs = {
     46 	.fb_probe = tegra_fb_probe
     47 };
     48 
     49 int
     50 tegra_drm_fb_init(struct drm_device *ddev)
     51 {
     52 	struct tegra_fbdev *fbdev;
     53 	int error;
     54 
     55 	fbdev = kmem_zalloc(sizeof(*fbdev), KM_SLEEP);
     56 	fbdev->helper.funcs = &tegra_fb_helper_funcs;
     57 
     58 	error = drm_fb_helper_init(ddev, &fbdev->helper, 2, 1);
     59 	if (error) {
     60 		kmem_free(fbdev, sizeof(*fbdev));
     61 		return error;
     62 	}
     63 
     64 	/*
     65 	 * This might seem silly, but it saves us from having to allocate
     66 	 * enough memory for the largest possible framebuffer (around 35MB).
     67 	 *
     68 	 * Allocate the fb_helper's framebuffer here but don't initialize
     69 	 * it yet. The fb helper won't configure a CRTC unless this field is
     70 	 * set, and we don't know the preferred size at this time.
     71 	 *
     72 	 * The fb_probe callback will eventually be called with the preferred
     73 	 * surface size, so defer allocating the buffer object until then.
     74 	 */
     75 	fbdev->helper.fb =
     76 	    kmem_zalloc(sizeof(struct tegra_framebuffer), KM_SLEEP);
     77 
     78 	drm_fb_helper_single_add_all_connectors(&fbdev->helper);
     79 
     80 	drm_helper_disable_unused_functions(ddev);
     81 
     82 	drm_fb_helper_initial_config(&fbdev->helper, 32);
     83 
     84 	return 0;
     85 }
     86 
     87 static int
     88 tegra_fb_probe(struct drm_fb_helper *helper,
     89     struct drm_fb_helper_surface_size *sizes)
     90 {
     91 	struct tegra_drm_softc * const sc = tegra_drm_private(helper->dev);
     92 	struct drm_device *ddev = helper->dev;
     93 	struct tegra_drmfb_attach_args tfa;
     94 
     95 	if (tegra_fb_init(ddev, helper->fb, sizes) != 0) {
     96 		DRM_ERROR("failed to initialize framebuffer\n");
     97 		return -ENOMEM;
     98 	}
     99 
    100 	memset(&tfa, 0, sizeof(tfa));
    101 	tfa.tfa_drm_dev = ddev;
    102 	tfa.tfa_fb_helper = helper;
    103 	tfa.tfa_fb_sizes = *sizes;
    104 	tfa.tfa_fb_bst = sc->sc_bst;
    105 	tfa.tfa_fb_dmat = sc->sc_dmat;
    106 	tfa.tfa_fb_linebytes = helper->fb->pitches[0];
    107 
    108 	helper->fbdev = config_found_ia(ddev->dev, "tegrafbbus", &tfa, NULL);
    109 	if (helper->fbdev == NULL) {
    110 		DRM_ERROR("unable to attach tegrafb\n");
    111 		return -ENXIO;
    112 	}
    113 
    114 	return 0;
    115 }
    116 
    117 static int
    118 tegra_fb_init(struct drm_device *ddev, struct drm_framebuffer *fb,
    119     struct drm_fb_helper_surface_size *sizes)
    120 {
    121 	struct tegra_framebuffer *tegra_fb = to_tegra_framebuffer(fb);
    122 	const u_int width = sizes->surface_width;
    123 	const u_int height = sizes->surface_height;
    124 	const u_int pitch = width * (32 / 8);
    125 
    126 	const size_t size = roundup(height * pitch, PAGE_SIZE);
    127 
    128 	tegra_fb->obj = drm_gem_cma_create(ddev, size);
    129 	if (tegra_fb->obj == NULL)
    130 		return -ENOMEM;
    131 
    132         fb->pitches[0] = pitch;
    133         fb->offsets[0] = 0;
    134         fb->width = width;
    135         fb->height = height;
    136         fb->pixel_format = DRM_FORMAT_XRGB8888;
    137         drm_fb_get_bpp_depth(fb->pixel_format, &fb->depth,
    138             &fb->bits_per_pixel);
    139 	tegra_drm_framebuffer_init(ddev, tegra_fb);
    140 
    141 	return 0;
    142 }
    143