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