xref: /openbmc/linux/drivers/gpu/drm/tegra/fbdev.c (revision eb9fe179)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012-2013 Avionic Design GmbH
4  * Copyright (C) 2012 NVIDIA CORPORATION.  All rights reserved.
5  *
6  * Based on the KMS/FB DMA helpers
7  *   Copyright (C) 2012 Analog Devices Inc.
8  */
9 
10 #include <linux/console.h>
11 #include <linux/fb.h>
12 #include <linux/vmalloc.h>
13 
14 #include <drm/drm_drv.h>
15 #include <drm/drm_crtc_helper.h>
16 #include <drm/drm_fb_helper.h>
17 #include <drm/drm_fourcc.h>
18 #include <drm/drm_framebuffer.h>
19 #include <drm/drm_gem_framebuffer_helper.h>
20 #include <drm/drm_modeset_helper.h>
21 
22 #include "drm.h"
23 #include "gem.h"
24 
25 static int tegra_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
26 {
27 	struct drm_fb_helper *helper = info->par;
28 	struct tegra_bo *bo;
29 	int err;
30 
31 	bo = tegra_fb_get_plane(helper->fb, 0);
32 
33 	err = drm_gem_mmap_obj(&bo->gem, bo->gem.size, vma);
34 	if (err < 0)
35 		return err;
36 
37 	return __tegra_gem_mmap(&bo->gem, vma);
38 }
39 
40 static void tegra_fbdev_fb_destroy(struct fb_info *info)
41 {
42 	struct drm_fb_helper *helper = info->par;
43 	struct drm_framebuffer *fb = helper->fb;
44 	struct tegra_bo *bo = tegra_fb_get_plane(fb, 0);
45 
46 	drm_fb_helper_fini(helper);
47 
48 	/* Undo the special mapping we made in fbdev probe. */
49 	if (bo->pages) {
50 		vunmap(bo->vaddr);
51 		bo->vaddr = NULL;
52 	}
53 	drm_framebuffer_remove(fb);
54 
55 	drm_client_release(&helper->client);
56 	drm_fb_helper_unprepare(helper);
57 	kfree(helper);
58 }
59 
60 static const struct fb_ops tegra_fb_ops = {
61 	.owner = THIS_MODULE,
62 	__FB_DEFAULT_SYS_OPS_RDWR,
63 	DRM_FB_HELPER_DEFAULT_OPS,
64 	__FB_DEFAULT_SYS_OPS_DRAW,
65 	.fb_mmap = tegra_fb_mmap,
66 	.fb_destroy = tegra_fbdev_fb_destroy,
67 };
68 
69 static int tegra_fbdev_probe(struct drm_fb_helper *helper,
70 			     struct drm_fb_helper_surface_size *sizes)
71 {
72 	struct tegra_drm *tegra = helper->dev->dev_private;
73 	struct drm_device *drm = helper->dev;
74 	struct drm_mode_fb_cmd2 cmd = { 0 };
75 	unsigned int bytes_per_pixel;
76 	struct drm_framebuffer *fb;
77 	unsigned long offset;
78 	struct fb_info *info;
79 	struct tegra_bo *bo;
80 	size_t size;
81 	int err;
82 
83 	bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
84 
85 	cmd.width = sizes->surface_width;
86 	cmd.height = sizes->surface_height;
87 	cmd.pitches[0] = round_up(sizes->surface_width * bytes_per_pixel,
88 				  tegra->pitch_align);
89 
90 	cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
91 						     sizes->surface_depth);
92 
93 	size = cmd.pitches[0] * cmd.height;
94 
95 	bo = tegra_bo_create(drm, size, 0);
96 	if (IS_ERR(bo))
97 		return PTR_ERR(bo);
98 
99 	info = drm_fb_helper_alloc_info(helper);
100 	if (IS_ERR(info)) {
101 		dev_err(drm->dev, "failed to allocate framebuffer info\n");
102 		drm_gem_object_put(&bo->gem);
103 		return PTR_ERR(info);
104 	}
105 
106 	fb = tegra_fb_alloc(drm, &cmd, &bo, 1);
107 	if (IS_ERR(fb)) {
108 		err = PTR_ERR(fb);
109 		dev_err(drm->dev, "failed to allocate DRM framebuffer: %d\n",
110 			err);
111 		drm_gem_object_put(&bo->gem);
112 		return PTR_ERR(fb);
113 	}
114 
115 	helper->fb = fb;
116 	helper->info = info;
117 
118 	info->fbops = &tegra_fb_ops;
119 
120 	drm_fb_helper_fill_info(info, helper, sizes);
121 
122 	offset = info->var.xoffset * bytes_per_pixel +
123 		 info->var.yoffset * fb->pitches[0];
124 
125 	if (bo->pages) {
126 		bo->vaddr = vmap(bo->pages, bo->num_pages, VM_MAP,
127 				 pgprot_writecombine(PAGE_KERNEL));
128 		if (!bo->vaddr) {
129 			dev_err(drm->dev, "failed to vmap() framebuffer\n");
130 			err = -ENOMEM;
131 			goto destroy;
132 		}
133 	}
134 
135 	info->screen_base = (void __iomem *)bo->vaddr + offset;
136 	info->screen_size = size;
137 	info->fix.smem_start = (unsigned long)(bo->iova + offset);
138 	info->fix.smem_len = size;
139 
140 	return 0;
141 
142 destroy:
143 	drm_framebuffer_remove(fb);
144 	return err;
145 }
146 
147 static const struct drm_fb_helper_funcs tegra_fb_helper_funcs = {
148 	.fb_probe = tegra_fbdev_probe,
149 };
150 
151 /*
152  * struct drm_client
153  */
154 
155 static void tegra_fbdev_client_unregister(struct drm_client_dev *client)
156 {
157 	struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
158 
159 	if (fb_helper->info) {
160 		drm_fb_helper_unregister_info(fb_helper);
161 	} else {
162 		drm_client_release(&fb_helper->client);
163 		drm_fb_helper_unprepare(fb_helper);
164 		kfree(fb_helper);
165 	}
166 }
167 
168 static int tegra_fbdev_client_restore(struct drm_client_dev *client)
169 {
170 	drm_fb_helper_lastclose(client->dev);
171 
172 	return 0;
173 }
174 
175 static int tegra_fbdev_client_hotplug(struct drm_client_dev *client)
176 {
177 	struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
178 	struct drm_device *dev = client->dev;
179 	int ret;
180 
181 	if (dev->fb_helper)
182 		return drm_fb_helper_hotplug_event(dev->fb_helper);
183 
184 	ret = drm_fb_helper_init(dev, fb_helper);
185 	if (ret)
186 		goto err_drm_err;
187 
188 	if (!drm_drv_uses_atomic_modeset(dev))
189 		drm_helper_disable_unused_functions(dev);
190 
191 	ret = drm_fb_helper_initial_config(fb_helper);
192 	if (ret)
193 		goto err_drm_fb_helper_fini;
194 
195 	return 0;
196 
197 err_drm_fb_helper_fini:
198 	drm_fb_helper_fini(fb_helper);
199 err_drm_err:
200 	drm_err(dev, "Failed to setup fbdev emulation (ret=%d)\n", ret);
201 	return ret;
202 }
203 
204 static const struct drm_client_funcs tegra_fbdev_client_funcs = {
205 	.owner		= THIS_MODULE,
206 	.unregister	= tegra_fbdev_client_unregister,
207 	.restore	= tegra_fbdev_client_restore,
208 	.hotplug	= tegra_fbdev_client_hotplug,
209 };
210 
211 void tegra_fbdev_setup(struct drm_device *dev)
212 {
213 	struct drm_fb_helper *helper;
214 	int ret;
215 
216 	drm_WARN(dev, !dev->registered, "Device has not been registered.\n");
217 	drm_WARN(dev, dev->fb_helper, "fb_helper is already set!\n");
218 
219 	helper = kzalloc(sizeof(*helper), GFP_KERNEL);
220 	if (!helper)
221 		return;
222 	drm_fb_helper_prepare(dev, helper, 32, &tegra_fb_helper_funcs);
223 
224 	ret = drm_client_init(dev, &helper->client, "fbdev", &tegra_fbdev_client_funcs);
225 	if (ret)
226 		goto err_drm_client_init;
227 
228 	drm_client_register(&helper->client);
229 
230 	return;
231 
232 err_drm_client_init:
233 	drm_fb_helper_unprepare(helper);
234 	kfree(helper);
235 }
236