1 /* $NetBSD: tegra_drm_fb.c,v 1.10 2021/12/19 12:44:14 riastradh 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.10 2021/12/19 12:44:14 riastradh Exp $");
31
32 #include <drm/drm_crtc.h>
33 #include <drm/drm_crtc_helper.h>
34 #include <drm/drm_drv.h>
35 #include <drm/drm_fb_helper.h>
36 #include <drm/drm_fourcc.h>
37
38 #include <arm/nvidia/tegra_drm.h>
39
40 static int tegra_fb_init(struct drm_device *, struct drm_framebuffer *,
41 struct drm_fb_helper_surface_size *);
42
43 static int tegra_fb_probe(struct drm_fb_helper *,
44 struct drm_fb_helper_surface_size *);
45
46 static struct drm_fb_helper_funcs tegra_fb_helper_funcs = {
47 .fb_probe = tegra_fb_probe
48 };
49
50 int
tegra_drm_fb_init(struct drm_device * ddev)51 tegra_drm_fb_init(struct drm_device *ddev)
52 {
53 struct tegra_fbdev *fbdev;
54 int error;
55
56 fbdev = kmem_zalloc(sizeof(*fbdev), KM_SLEEP);
57
58 drm_fb_helper_prepare(ddev, &fbdev->helper, &tegra_fb_helper_funcs);
59
60 error = drm_fb_helper_init(ddev, &fbdev->helper, 1);
61 if (error) {
62 kmem_free(fbdev, sizeof(*fbdev));
63 return error;
64 }
65
66 /*
67 * This might seem silly, but it saves us from having to allocate
68 * enough memory for the largest possible framebuffer (around 35MB).
69 *
70 * Allocate the fb_helper's framebuffer here but don't initialize
71 * it yet. The fb helper won't configure a CRTC unless this field is
72 * set, and we don't know the preferred size at this time.
73 *
74 * The fb_probe callback will eventually be called with the preferred
75 * surface size, so defer allocating the buffer object until then.
76 */
77 fbdev->helper.fb =
78 kmem_zalloc(sizeof(struct tegra_framebuffer), KM_SLEEP);
79
80 drm_fb_helper_single_add_all_connectors(&fbdev->helper);
81
82 drm_helper_disable_unused_functions(ddev);
83
84 drm_fb_helper_initial_config(&fbdev->helper, 32);
85
86 return 0;
87 }
88
89 static int
tegra_fb_probe(struct drm_fb_helper * helper,struct drm_fb_helper_surface_size * sizes)90 tegra_fb_probe(struct drm_fb_helper *helper,
91 struct drm_fb_helper_surface_size *sizes)
92 {
93 struct tegra_drm_softc * const sc = tegra_drm_private(helper->dev);
94 struct drm_device *ddev = helper->dev;
95 struct tegra_drmfb_attach_args tfa;
96
97 if (tegra_fb_init(ddev, helper->fb, sizes) != 0) {
98 DRM_ERROR("failed to initialize framebuffer\n");
99 return -ENOMEM;
100 }
101
102 memset(&tfa, 0, sizeof(tfa));
103 tfa.tfa_drm_dev = ddev;
104 tfa.tfa_fb_helper = helper;
105 tfa.tfa_fb_sizes = *sizes;
106 tfa.tfa_fb_bst = sc->sc_bst;
107 tfa.tfa_fb_dmat = sc->sc_dmat;
108 tfa.tfa_fb_linebytes = helper->fb->pitches[0];
109
110 helper->fbdev = config_found(ddev->dev, &tfa, NULL,
111 CFARGS(.iattr = "tegrafbbus"));
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
tegra_fb_init(struct drm_device * ddev,struct drm_framebuffer * fb,struct drm_fb_helper_surface_size * sizes)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->modifier = 0;
140 fb->flags = 0;
141 fb->format = drm_format_info(DRM_FORMAT_XRGB8888); /* XXX endian? */
142 fb->dev = ddev;
143 tegra_drm_framebuffer_init(ddev, tegra_fb);
144
145 return 0;
146 }
147