tegra_drm_fb.c revision 1.2 1 /* $NetBSD: tegra_drm_fb.c,v 1.2 2015/11/12 00:43: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.2 2015/11/12 00:43: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 if (fbdev == NULL)
57 return -ENOMEM;
58 fbdev->helper.funcs = &tegra_fb_helper_funcs;
59
60 error = drm_fb_helper_init(ddev, &fbdev->helper, 2, 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 if (fbdev->helper.fb == NULL) {
80 DRM_ERROR("failed to create framebuffer\n");
81 }
82
83 drm_fb_helper_single_add_all_connectors(&fbdev->helper);
84
85 drm_helper_disable_unused_functions(ddev);
86
87 drm_fb_helper_initial_config(&fbdev->helper, 32);
88
89 return 0;
90 }
91
92 static int
93 tegra_fb_probe(struct drm_fb_helper *helper,
94 struct drm_fb_helper_surface_size *sizes)
95 {
96 struct tegra_drm_softc * const sc = tegra_drm_private(helper->dev);
97 struct drm_device *ddev = helper->dev;
98 struct tegra_drmfb_attach_args tfa;
99
100 if (tegra_fb_init(ddev, helper->fb, sizes) != 0) {
101 DRM_ERROR("failed to initialize framebuffer\n");
102 return -ENOMEM;
103 }
104
105 memset(&tfa, 0, sizeof(tfa));
106 tfa.tfa_drm_dev = ddev;
107 tfa.tfa_fb_helper = helper;
108 tfa.tfa_fb_sizes = *sizes;
109 tfa.tfa_fb_bst = sc->sc_bst;
110 tfa.tfa_fb_dmat = sc->sc_dmat;
111
112 helper->fbdev = config_found_ia(ddev->dev, "tegrafbbus", &tfa, NULL);
113 if (helper->fbdev == NULL) {
114 DRM_ERROR("unable to attach tegrafb\n");
115 return -ENXIO;
116 }
117
118 return 0;
119 }
120
121 static int
122 tegra_fb_init(struct drm_device *ddev, struct drm_framebuffer *fb,
123 struct drm_fb_helper_surface_size *sizes)
124 {
125 struct tegra_framebuffer *tegra_fb = to_tegra_framebuffer(fb);
126 const u_int width = sizes->surface_width;
127 const u_int height = sizes->surface_height;
128 const u_int pitch = width * (32 / 8);
129
130 const size_t size = roundup(height * pitch, PAGE_SIZE);
131
132 tegra_fb->obj = tegra_drm_obj_alloc(ddev, size);
133 if (tegra_fb->obj == NULL)
134 return -ENOMEM;
135
136 fb->pitches[0] = pitch;
137 fb->offsets[0] = 0;
138 fb->width = width;
139 fb->height = height;
140 fb->pixel_format = DRM_FORMAT_ARGB8888;
141 drm_fb_get_bpp_depth(fb->pixel_format, &fb->depth,
142 &fb->bits_per_pixel);
143 tegra_drm_framebuffer_init(ddev, tegra_fb);
144
145 return 0;
146 }
147