1 /*
2  * drivers/staging/omapdrm/omap_fb.c
3  *
4  * Copyright (C) 2011 Texas Instruments
5  * Author: Rob Clark <rob@ti.com>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License version 2 as published by
9  * the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "omap_drv.h"
21 
22 #include "drm_crtc.h"
23 #include "drm_crtc_helper.h"
24 
25 /*
26  * framebuffer funcs
27  */
28 
29 /* per-format info: */
30 struct format {
31 	enum omap_color_mode dss_format;
32 	uint32_t pixel_format;
33 	struct {
34 		int stride_bpp;           /* this times width is stride */
35 		int sub_y;                /* sub-sample in y dimension */
36 	} planes[4];
37 	bool yuv;
38 };
39 
40 static const struct format formats[] = {
41 	/* 16bpp [A]RGB: */
42 	{ OMAP_DSS_COLOR_RGB16,       DRM_FORMAT_RGB565,   {{2, 1}}, false }, /* RGB16-565 */
43 	{ OMAP_DSS_COLOR_RGB12U,      DRM_FORMAT_RGBX4444, {{2, 1}}, false }, /* RGB12x-4444 */
44 	{ OMAP_DSS_COLOR_RGBX16,      DRM_FORMAT_XRGB4444, {{2, 1}}, false }, /* xRGB12-4444 */
45 	{ OMAP_DSS_COLOR_RGBA16,      DRM_FORMAT_RGBA4444, {{2, 1}}, false }, /* RGBA12-4444 */
46 	{ OMAP_DSS_COLOR_ARGB16,      DRM_FORMAT_ARGB4444, {{2, 1}}, false }, /* ARGB16-4444 */
47 	{ OMAP_DSS_COLOR_XRGB16_1555, DRM_FORMAT_XRGB1555, {{2, 1}}, false }, /* xRGB15-1555 */
48 	{ OMAP_DSS_COLOR_ARGB16_1555, DRM_FORMAT_ARGB1555, {{2, 1}}, false }, /* ARGB16-1555 */
49 	/* 24bpp RGB: */
50 	{ OMAP_DSS_COLOR_RGB24P,      DRM_FORMAT_RGB888,   {{3, 1}}, false }, /* RGB24-888 */
51 	/* 32bpp [A]RGB: */
52 	{ OMAP_DSS_COLOR_RGBX32,      DRM_FORMAT_RGBX8888, {{4, 1}}, false }, /* RGBx24-8888 */
53 	{ OMAP_DSS_COLOR_RGB24U,      DRM_FORMAT_XRGB8888, {{4, 1}}, false }, /* xRGB24-8888 */
54 	{ OMAP_DSS_COLOR_RGBA32,      DRM_FORMAT_RGBA8888, {{4, 1}}, false }, /* RGBA32-8888 */
55 	{ OMAP_DSS_COLOR_ARGB32,      DRM_FORMAT_ARGB8888, {{4, 1}}, false }, /* ARGB32-8888 */
56 	/* YUV: */
57 	{ OMAP_DSS_COLOR_NV12,        DRM_FORMAT_NV12,     {{1, 1}, {1, 2}}, true },
58 	{ OMAP_DSS_COLOR_YUV2,        DRM_FORMAT_YUYV,     {{2, 1}}, true },
59 	{ OMAP_DSS_COLOR_UYVY,        DRM_FORMAT_UYVY,     {{2, 1}}, true },
60 };
61 
62 /* per-plane info for the fb: */
63 struct plane {
64 	struct drm_gem_object *bo;
65 	uint32_t pitch;
66 	uint32_t offset;
67 	dma_addr_t paddr;
68 };
69 
70 #define to_omap_framebuffer(x) container_of(x, struct omap_framebuffer, base)
71 
72 struct omap_framebuffer {
73 	struct drm_framebuffer base;
74 	const struct format *format;
75 	struct plane planes[4];
76 };
77 
omap_framebuffer_create_handle(struct drm_framebuffer * fb,struct drm_file * file_priv,unsigned int * handle)78 static int omap_framebuffer_create_handle(struct drm_framebuffer *fb,
79 		struct drm_file *file_priv,
80 		unsigned int *handle)
81 {
82 	struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
83 	return drm_gem_handle_create(file_priv,
84 			omap_fb->planes[0].bo, handle);
85 }
86 
omap_framebuffer_destroy(struct drm_framebuffer * fb)87 static void omap_framebuffer_destroy(struct drm_framebuffer *fb)
88 {
89 	struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
90 	int i, n = drm_format_num_planes(omap_fb->format->pixel_format);
91 
92 	DBG("destroy: FB ID: %d (%p)", fb->base.id, fb);
93 
94 	drm_framebuffer_cleanup(fb);
95 
96 	for (i = 0; i < n; i++) {
97 		struct plane *plane = &omap_fb->planes[i];
98 		if (plane->bo)
99 			drm_gem_object_unreference_unlocked(plane->bo);
100 	}
101 
102 	kfree(omap_fb);
103 }
104 
omap_framebuffer_dirty(struct drm_framebuffer * fb,struct drm_file * file_priv,unsigned flags,unsigned color,struct drm_clip_rect * clips,unsigned num_clips)105 static int omap_framebuffer_dirty(struct drm_framebuffer *fb,
106 		struct drm_file *file_priv, unsigned flags, unsigned color,
107 		struct drm_clip_rect *clips, unsigned num_clips)
108 {
109 	int i;
110 
111 	for (i = 0; i < num_clips; i++) {
112 		omap_framebuffer_flush(fb, clips[i].x1, clips[i].y1,
113 					clips[i].x2 - clips[i].x1,
114 					clips[i].y2 - clips[i].y1);
115 	}
116 
117 	return 0;
118 }
119 
120 static const struct drm_framebuffer_funcs omap_framebuffer_funcs = {
121 	.create_handle = omap_framebuffer_create_handle,
122 	.destroy = omap_framebuffer_destroy,
123 	.dirty = omap_framebuffer_dirty,
124 };
125 
126 /* pins buffer in preparation for scanout */
omap_framebuffer_pin(struct drm_framebuffer * fb)127 int omap_framebuffer_pin(struct drm_framebuffer *fb)
128 {
129 	struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
130 	int ret, i, n = drm_format_num_planes(omap_fb->format->pixel_format);
131 
132 	for (i = 0; i < n; i++) {
133 		struct plane *plane = &omap_fb->planes[i];
134 		ret = omap_gem_get_paddr(plane->bo, &plane->paddr, true);
135 		if (ret)
136 			goto fail;
137 	}
138 
139 	return 0;
140 
141 fail:
142 	while (--i > 0) {
143 		struct plane *plane = &omap_fb->planes[i];
144 		omap_gem_put_paddr(plane->bo);
145 	}
146 	return ret;
147 }
148 
149 /* releases buffer when done with scanout */
omap_framebuffer_unpin(struct drm_framebuffer * fb)150 void omap_framebuffer_unpin(struct drm_framebuffer *fb)
151 {
152 	struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
153 	int i, n = drm_format_num_planes(omap_fb->format->pixel_format);
154 
155 	for (i = 0; i < n; i++) {
156 		struct plane *plane = &omap_fb->planes[i];
157 		omap_gem_put_paddr(plane->bo);
158 	}
159 }
160 
161 /* update ovl info for scanout, handles cases of multi-planar fb's, etc.
162  */
omap_framebuffer_update_scanout(struct drm_framebuffer * fb,int x,int y,struct omap_overlay_info * info)163 void omap_framebuffer_update_scanout(struct drm_framebuffer *fb, int x, int y,
164 		struct omap_overlay_info *info)
165 {
166 	struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
167 	const struct format *format = omap_fb->format;
168 	struct plane *plane = &omap_fb->planes[0];
169 	unsigned int offset;
170 
171 	offset = plane->offset +
172 			(x * format->planes[0].stride_bpp) +
173 			(y * plane->pitch / format->planes[0].sub_y);
174 
175 	info->color_mode   = format->dss_format;
176 	info->paddr        = plane->paddr + offset;
177 	info->screen_width = plane->pitch / format->planes[0].stride_bpp;
178 
179 	if (format->dss_format == OMAP_DSS_COLOR_NV12) {
180 		plane = &omap_fb->planes[1];
181 		offset = plane->offset +
182 				(x * format->planes[1].stride_bpp) +
183 				(y * plane->pitch / format->planes[1].sub_y);
184 		info->p_uv_addr = plane->paddr + offset;
185 	} else {
186 		info->p_uv_addr = 0;
187 	}
188 }
189 
omap_framebuffer_bo(struct drm_framebuffer * fb,int p)190 struct drm_gem_object *omap_framebuffer_bo(struct drm_framebuffer *fb, int p)
191 {
192 	struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
193 	if (p >= drm_format_num_planes(omap_fb->format->pixel_format))
194 		return NULL;
195 	return omap_fb->planes[p].bo;
196 }
197 
198 /* iterate thru all the connectors, returning ones that are attached
199  * to the same fb..
200  */
omap_framebuffer_get_next_connector(struct drm_framebuffer * fb,struct drm_connector * from)201 struct drm_connector *omap_framebuffer_get_next_connector(
202 		struct drm_framebuffer *fb, struct drm_connector *from)
203 {
204 	struct drm_device *dev = fb->dev;
205 	struct list_head *connector_list = &dev->mode_config.connector_list;
206 	struct drm_connector *connector = from;
207 
208 	if (!from) {
209 		return list_first_entry(connector_list, typeof(*from), head);
210 	}
211 
212 	list_for_each_entry_from(connector, connector_list, head) {
213 		if (connector != from) {
214 			struct drm_encoder *encoder = connector->encoder;
215 			struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
216 			if (crtc && crtc->fb == fb) {
217 				return connector;
218 			}
219 		}
220 	}
221 
222 	return NULL;
223 }
224 
225 /* flush an area of the framebuffer (in case of manual update display that
226  * is not automatically flushed)
227  */
omap_framebuffer_flush(struct drm_framebuffer * fb,int x,int y,int w,int h)228 void omap_framebuffer_flush(struct drm_framebuffer *fb,
229 		int x, int y, int w, int h)
230 {
231 	struct drm_connector *connector = NULL;
232 
233 	VERB("flush: %d,%d %dx%d, fb=%p", x, y, w, h, fb);
234 
235 	while ((connector = omap_framebuffer_get_next_connector(fb, connector))) {
236 		/* only consider connectors that are part of a chain */
237 		if (connector->encoder && connector->encoder->crtc) {
238 			/* TODO: maybe this should propagate thru the crtc who
239 			 * could do the coordinate translation..
240 			 */
241 			struct drm_crtc *crtc = connector->encoder->crtc;
242 			int cx = max(0, x - crtc->x);
243 			int cy = max(0, y - crtc->y);
244 			int cw = w + (x - crtc->x) - cx;
245 			int ch = h + (y - crtc->y) - cy;
246 
247 			omap_connector_flush(connector, cx, cy, cw, ch);
248 		}
249 	}
250 }
251 
omap_framebuffer_create(struct drm_device * dev,struct drm_file * file,struct drm_mode_fb_cmd2 * mode_cmd)252 struct drm_framebuffer *omap_framebuffer_create(struct drm_device *dev,
253 		struct drm_file *file, struct drm_mode_fb_cmd2 *mode_cmd)
254 {
255 	struct drm_gem_object *bos[4];
256 	struct drm_framebuffer *fb;
257 	int ret;
258 
259 	ret = objects_lookup(dev, file, mode_cmd->pixel_format,
260 			bos, mode_cmd->handles);
261 	if (ret)
262 		return ERR_PTR(ret);
263 
264 	fb = omap_framebuffer_init(dev, mode_cmd, bos);
265 	if (IS_ERR(fb)) {
266 		int i, n = drm_format_num_planes(mode_cmd->pixel_format);
267 		for (i = 0; i < n; i++)
268 			drm_gem_object_unreference_unlocked(bos[i]);
269 		return fb;
270 	}
271 	return fb;
272 }
273 
omap_framebuffer_init(struct drm_device * dev,struct drm_mode_fb_cmd2 * mode_cmd,struct drm_gem_object ** bos)274 struct drm_framebuffer *omap_framebuffer_init(struct drm_device *dev,
275 		struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
276 {
277 	struct omap_framebuffer *omap_fb;
278 	struct drm_framebuffer *fb = NULL;
279 	const struct format *format = NULL;
280 	int ret, i, n = drm_format_num_planes(mode_cmd->pixel_format);
281 
282 	DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
283 			dev, mode_cmd, mode_cmd->width, mode_cmd->height,
284 			(char *)&mode_cmd->pixel_format);
285 
286 	for (i = 0; i < ARRAY_SIZE(formats); i++) {
287 		if (formats[i].pixel_format == mode_cmd->pixel_format) {
288 			format = &formats[i];
289 			break;
290 		}
291 	}
292 
293 	if (!format) {
294 		dev_err(dev->dev, "unsupported pixel format: %4.4s\n",
295 				(char *)&mode_cmd->pixel_format);
296 		ret = -EINVAL;
297 		goto fail;
298 	}
299 
300 	omap_fb = kzalloc(sizeof(*omap_fb), GFP_KERNEL);
301 	if (!omap_fb) {
302 		dev_err(dev->dev, "could not allocate fb\n");
303 		ret = -ENOMEM;
304 		goto fail;
305 	}
306 
307 	fb = &omap_fb->base;
308 	ret = drm_framebuffer_init(dev, fb, &omap_framebuffer_funcs);
309 	if (ret) {
310 		dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
311 		goto fail;
312 	}
313 
314 	DBG("create: FB ID: %d (%p)", fb->base.id, fb);
315 
316 	omap_fb->format = format;
317 
318 	for (i = 0; i < n; i++) {
319 		struct plane *plane = &omap_fb->planes[i];
320 		int size, pitch = mode_cmd->pitches[i];
321 
322 		if (pitch < (mode_cmd->width * format->planes[i].stride_bpp)) {
323 			dev_err(dev->dev, "provided buffer pitch is too small! %d < %d\n",
324 					pitch, mode_cmd->width * format->planes[i].stride_bpp);
325 			ret = -EINVAL;
326 			goto fail;
327 		}
328 
329 		size = pitch * mode_cmd->height / format->planes[i].sub_y;
330 
331 		if (size > (bos[i]->size - mode_cmd->offsets[i])) {
332 			dev_err(dev->dev, "provided buffer object is too small! %d < %d\n",
333 					bos[i]->size - mode_cmd->offsets[i], size);
334 			ret = -EINVAL;
335 			goto fail;
336 		}
337 
338 		plane->bo     = bos[i];
339 		plane->offset = mode_cmd->offsets[i];
340 		plane->pitch  = mode_cmd->pitches[i];
341 		plane->paddr  = pitch;
342 	}
343 
344 	drm_helper_mode_fill_fb_struct(fb, mode_cmd);
345 
346 	return fb;
347 
348 fail:
349 	if (fb) {
350 		omap_framebuffer_destroy(fb);
351 	}
352 	return ERR_PTR(ret);
353 }
354