tegra_drm_fb.c revision 1.8 1 /* $NetBSD: tegra_drm_fb.c,v 1.8 2021/04/24 23:36:27 thorpej 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.8 2021/04/24 23:36:27 thorpej 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(ddev->dev, &tfa, NULL,
110 CFARG_IATTR, "tegrafbbus",
111 CFARG_EOL);
112 if (helper->fbdev == NULL) {
113 DRM_ERROR("unable to attach tegrafb\n");
114 return -ENXIO;
115 }
116
117 return 0;
118 }
119
120 static int
121 tegra_fb_init(struct drm_device *ddev, struct drm_framebuffer *fb,
122 struct drm_fb_helper_surface_size *sizes)
123 {
124 struct tegra_framebuffer *tegra_fb = to_tegra_framebuffer(fb);
125 const u_int width = sizes->surface_width;
126 const u_int height = sizes->surface_height;
127 const u_int pitch = width * (32 / 8);
128
129 const size_t size = roundup(height * pitch, PAGE_SIZE);
130
131 tegra_fb->obj = drm_gem_cma_create(ddev, size);
132 if (tegra_fb->obj == NULL)
133 return -ENOMEM;
134
135 fb->pitches[0] = pitch;
136 fb->offsets[0] = 0;
137 fb->width = width;
138 fb->height = height;
139 fb->pixel_format = DRM_FORMAT_XRGB8888;
140 drm_fb_get_bpp_depth(fb->pixel_format, &fb->depth,
141 &fb->bits_per_pixel);
142 tegra_drm_framebuffer_init(ddev, tegra_fb);
143
144 return 0;
145 }
146