1 // SPDX-License-Identifier: GPL-2.0 or MIT
2 /*
3  * Copyright (C) 2016 Noralf Trønnes
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  */
10 
11 #include <linux/io.h>
12 #include <linux/iosys-map.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 
16 #include <drm/drm_device.h>
17 #include <drm/drm_format_helper.h>
18 #include <drm/drm_framebuffer.h>
19 #include <drm/drm_fourcc.h>
20 #include <drm/drm_print.h>
21 #include <drm/drm_rect.h>
22 
23 /**
24  * drm_format_conv_state_init - Initialize format-conversion state
25  * @state: The state to initialize
26  *
27  * Clears all fields in struct drm_format_conv_state. The state will
28  * be empty with no preallocated resources.
29  */
drm_format_conv_state_init(struct drm_format_conv_state * state)30 void drm_format_conv_state_init(struct drm_format_conv_state *state)
31 {
32 	state->tmp.mem = NULL;
33 	state->tmp.size = 0;
34 	state->tmp.preallocated = false;
35 }
36 EXPORT_SYMBOL(drm_format_conv_state_init);
37 
38 /**
39  * drm_format_conv_state_copy - Copy format-conversion state
40  * @state: Destination state
41  * @old_state: Source state
42  *
43  * Copies format-conversion state from @old_state to @state; except for
44  * temporary storage.
45  */
drm_format_conv_state_copy(struct drm_format_conv_state * state,const struct drm_format_conv_state * old_state)46 void drm_format_conv_state_copy(struct drm_format_conv_state *state,
47 				const struct drm_format_conv_state *old_state)
48 {
49 	/*
50 	 * So far, there's only temporary storage here, which we don't
51 	 * duplicate. Just clear the fields.
52 	 */
53 	state->tmp.mem = NULL;
54 	state->tmp.size = 0;
55 	state->tmp.preallocated = false;
56 }
57 EXPORT_SYMBOL(drm_format_conv_state_copy);
58 
59 /**
60  * drm_format_conv_state_reserve - Allocates storage for format conversion
61  * @state: The format-conversion state
62  * @new_size: The minimum allocation size
63  * @flags: Flags for kmalloc()
64  *
65  * Allocates at least @new_size bytes and returns a pointer to the memory
66  * range. After calling this function, previously returned memory blocks
67  * are invalid. It's best to collect all memory requirements of a format
68  * conversion and call this function once to allocate the range.
69  *
70  * Returns:
71  * A pointer to the allocated memory range, or NULL otherwise.
72  */
drm_format_conv_state_reserve(struct drm_format_conv_state * state,size_t new_size,gfp_t flags)73 void *drm_format_conv_state_reserve(struct drm_format_conv_state *state,
74 				    size_t new_size, gfp_t flags)
75 {
76 	void *mem;
77 
78 	if (new_size <= state->tmp.size)
79 		goto out;
80 	else if (state->tmp.preallocated)
81 		return NULL;
82 
83 	mem = krealloc(state->tmp.mem, new_size, flags);
84 	if (!mem)
85 		return NULL;
86 
87 	state->tmp.mem = mem;
88 	state->tmp.size = new_size;
89 
90 out:
91 	return state->tmp.mem;
92 }
93 EXPORT_SYMBOL(drm_format_conv_state_reserve);
94 
95 /**
96  * drm_format_conv_state_release - Releases an format-conversion storage
97  * @state: The format-conversion state
98  *
99  * Releases the memory range references by the format-conversion state.
100  * After this call, all pointers to the memory are invalid. Prefer
101  * drm_format_conv_state_init() for cleaning up and unloading a driver.
102  */
drm_format_conv_state_release(struct drm_format_conv_state * state)103 void drm_format_conv_state_release(struct drm_format_conv_state *state)
104 {
105 	if (state->tmp.preallocated)
106 		return;
107 
108 	kfree(state->tmp.mem);
109 	state->tmp.mem = NULL;
110 	state->tmp.size = 0;
111 }
112 EXPORT_SYMBOL(drm_format_conv_state_release);
113 
clip_offset(const struct drm_rect * clip,unsigned int pitch,unsigned int cpp)114 static unsigned int clip_offset(const struct drm_rect *clip, unsigned int pitch, unsigned int cpp)
115 {
116 	return clip->y1 * pitch + clip->x1 * cpp;
117 }
118 
119 /**
120  * drm_fb_clip_offset - Returns the clipping rectangles byte-offset in a framebuffer
121  * @pitch: Framebuffer line pitch in byte
122  * @format: Framebuffer format
123  * @clip: Clip rectangle
124  *
125  * Returns:
126  * The byte offset of the clip rectangle's top-left corner within the framebuffer.
127  */
drm_fb_clip_offset(unsigned int pitch,const struct drm_format_info * format,const struct drm_rect * clip)128 unsigned int drm_fb_clip_offset(unsigned int pitch, const struct drm_format_info *format,
129 				const struct drm_rect *clip)
130 {
131 	return clip_offset(clip, pitch, format->cpp[0]);
132 }
133 EXPORT_SYMBOL(drm_fb_clip_offset);
134 
135 /* TODO: Make this function work with multi-plane formats. */
__drm_fb_xfrm(void * dst,unsigned long dst_pitch,unsigned long dst_pixsize,const void * vaddr,const struct drm_framebuffer * fb,const struct drm_rect * clip,bool vaddr_cached_hint,struct drm_format_conv_state * state,void (* xfrm_line)(void * dbuf,const void * sbuf,unsigned int npixels))136 static int __drm_fb_xfrm(void *dst, unsigned long dst_pitch, unsigned long dst_pixsize,
137 			 const void *vaddr, const struct drm_framebuffer *fb,
138 			 const struct drm_rect *clip, bool vaddr_cached_hint,
139 			 struct drm_format_conv_state *state,
140 			 void (*xfrm_line)(void *dbuf, const void *sbuf, unsigned int npixels))
141 {
142 	unsigned long linepixels = drm_rect_width(clip);
143 	unsigned long lines = drm_rect_height(clip);
144 	size_t sbuf_len = linepixels * fb->format->cpp[0];
145 	void *stmp = NULL;
146 	unsigned long i;
147 	const void *sbuf;
148 
149 	/*
150 	 * Some source buffers, such as DMA memory, use write-combine
151 	 * caching, so reads are uncached. Speed up access by fetching
152 	 * one line at a time.
153 	 */
154 	if (!vaddr_cached_hint) {
155 		stmp = drm_format_conv_state_reserve(state, sbuf_len, GFP_KERNEL);
156 		if (!stmp)
157 			return -ENOMEM;
158 	}
159 
160 	if (!dst_pitch)
161 		dst_pitch = drm_rect_width(clip) * dst_pixsize;
162 	vaddr += clip_offset(clip, fb->pitches[0], fb->format->cpp[0]);
163 
164 	for (i = 0; i < lines; ++i) {
165 		if (stmp)
166 			sbuf = memcpy(stmp, vaddr, sbuf_len);
167 		else
168 			sbuf = vaddr;
169 		xfrm_line(dst, sbuf, linepixels);
170 		vaddr += fb->pitches[0];
171 		dst += dst_pitch;
172 	}
173 
174 	return 0;
175 }
176 
177 /* TODO: Make this function work with multi-plane formats. */
__drm_fb_xfrm_toio(void __iomem * dst,unsigned long dst_pitch,unsigned long dst_pixsize,const void * vaddr,const struct drm_framebuffer * fb,const struct drm_rect * clip,bool vaddr_cached_hint,struct drm_format_conv_state * state,void (* xfrm_line)(void * dbuf,const void * sbuf,unsigned int npixels))178 static int __drm_fb_xfrm_toio(void __iomem *dst, unsigned long dst_pitch, unsigned long dst_pixsize,
179 			      const void *vaddr, const struct drm_framebuffer *fb,
180 			      const struct drm_rect *clip, bool vaddr_cached_hint,
181 			      struct drm_format_conv_state *state,
182 			      void (*xfrm_line)(void *dbuf, const void *sbuf, unsigned int npixels))
183 {
184 	unsigned long linepixels = drm_rect_width(clip);
185 	unsigned long lines = drm_rect_height(clip);
186 	size_t dbuf_len = linepixels * dst_pixsize;
187 	size_t stmp_off = round_up(dbuf_len, ARCH_KMALLOC_MINALIGN); /* for sbuf alignment */
188 	size_t sbuf_len = linepixels * fb->format->cpp[0];
189 	void *stmp = NULL;
190 	unsigned long i;
191 	const void *sbuf;
192 	void *dbuf;
193 
194 	if (vaddr_cached_hint) {
195 		dbuf = drm_format_conv_state_reserve(state, dbuf_len, GFP_KERNEL);
196 	} else {
197 		dbuf = drm_format_conv_state_reserve(state, stmp_off + sbuf_len, GFP_KERNEL);
198 		stmp = dbuf + stmp_off;
199 	}
200 	if (!dbuf)
201 		return -ENOMEM;
202 
203 	if (!dst_pitch)
204 		dst_pitch = linepixels * dst_pixsize;
205 	vaddr += clip_offset(clip, fb->pitches[0], fb->format->cpp[0]);
206 
207 	for (i = 0; i < lines; ++i) {
208 		if (stmp)
209 			sbuf = memcpy(stmp, vaddr, sbuf_len);
210 		else
211 			sbuf = vaddr;
212 		xfrm_line(dbuf, sbuf, linepixels);
213 		memcpy_toio(dst, dbuf, dbuf_len);
214 		vaddr += fb->pitches[0];
215 		dst += dst_pitch;
216 	}
217 
218 	return 0;
219 }
220 
221 /* TODO: Make this function work with multi-plane formats. */
drm_fb_xfrm(struct iosys_map * dst,const unsigned int * dst_pitch,const u8 * dst_pixsize,const struct iosys_map * src,const struct drm_framebuffer * fb,const struct drm_rect * clip,bool vaddr_cached_hint,struct drm_format_conv_state * state,void (* xfrm_line)(void * dbuf,const void * sbuf,unsigned int npixels))222 static int drm_fb_xfrm(struct iosys_map *dst,
223 		       const unsigned int *dst_pitch, const u8 *dst_pixsize,
224 		       const struct iosys_map *src, const struct drm_framebuffer *fb,
225 		       const struct drm_rect *clip, bool vaddr_cached_hint,
226 		       struct drm_format_conv_state *state,
227 		       void (*xfrm_line)(void *dbuf, const void *sbuf, unsigned int npixels))
228 {
229 	static const unsigned int default_dst_pitch[DRM_FORMAT_MAX_PLANES] = {
230 		0, 0, 0, 0
231 	};
232 
233 	if (!dst_pitch)
234 		dst_pitch = default_dst_pitch;
235 
236 	/* TODO: handle src in I/O memory here */
237 	if (dst[0].is_iomem)
238 		return __drm_fb_xfrm_toio(dst[0].vaddr_iomem, dst_pitch[0], dst_pixsize[0],
239 					  src[0].vaddr, fb, clip, vaddr_cached_hint, state,
240 					  xfrm_line);
241 	else
242 		return __drm_fb_xfrm(dst[0].vaddr, dst_pitch[0], dst_pixsize[0],
243 				     src[0].vaddr, fb, clip, vaddr_cached_hint, state,
244 				     xfrm_line);
245 }
246 
247 /**
248  * drm_fb_memcpy - Copy clip buffer
249  * @dst: Array of destination buffers
250  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
251  *             within @dst; can be NULL if scanlines are stored next to each other.
252  * @src: Array of source buffers
253  * @fb: DRM framebuffer
254  * @clip: Clip rectangle area to copy
255  *
256  * This function copies parts of a framebuffer to display memory. Destination and
257  * framebuffer formats must match. No conversion takes place. The parameters @dst,
258  * @dst_pitch and @src refer to arrays. Each array must have at least as many entries
259  * as there are planes in @fb's format. Each entry stores the value for the format's
260  * respective color plane at the same index.
261  *
262  * This function does not apply clipping on @dst (i.e. the destination is at the
263  * top-left corner).
264  */
drm_fb_memcpy(struct iosys_map * dst,const unsigned int * dst_pitch,const struct iosys_map * src,const struct drm_framebuffer * fb,const struct drm_rect * clip)265 void drm_fb_memcpy(struct iosys_map *dst, const unsigned int *dst_pitch,
266 		   const struct iosys_map *src, const struct drm_framebuffer *fb,
267 		   const struct drm_rect *clip)
268 {
269 	static const unsigned int default_dst_pitch[DRM_FORMAT_MAX_PLANES] = {
270 		0, 0, 0, 0
271 	};
272 
273 	const struct drm_format_info *format = fb->format;
274 	unsigned int i, y, lines = drm_rect_height(clip);
275 
276 	if (!dst_pitch)
277 		dst_pitch = default_dst_pitch;
278 
279 	for (i = 0; i < format->num_planes; ++i) {
280 		unsigned int bpp_i = drm_format_info_bpp(format, i);
281 		unsigned int cpp_i = DIV_ROUND_UP(bpp_i, 8);
282 		size_t len_i = DIV_ROUND_UP(drm_rect_width(clip) * bpp_i, 8);
283 		unsigned int dst_pitch_i = dst_pitch[i];
284 		struct iosys_map dst_i = dst[i];
285 		struct iosys_map src_i = src[i];
286 
287 		if (!dst_pitch_i)
288 			dst_pitch_i = len_i;
289 
290 		iosys_map_incr(&src_i, clip_offset(clip, fb->pitches[i], cpp_i));
291 		for (y = 0; y < lines; y++) {
292 			/* TODO: handle src_i in I/O memory here */
293 			iosys_map_memcpy_to(&dst_i, 0, src_i.vaddr, len_i);
294 			iosys_map_incr(&src_i, fb->pitches[i]);
295 			iosys_map_incr(&dst_i, dst_pitch_i);
296 		}
297 	}
298 }
299 EXPORT_SYMBOL(drm_fb_memcpy);
300 
drm_fb_swab16_line(void * dbuf,const void * sbuf,unsigned int pixels)301 static void drm_fb_swab16_line(void *dbuf, const void *sbuf, unsigned int pixels)
302 {
303 	u16 *dbuf16 = dbuf;
304 	const u16 *sbuf16 = sbuf;
305 	const u16 *send16 = sbuf16 + pixels;
306 
307 	while (sbuf16 < send16)
308 		*dbuf16++ = swab16(*sbuf16++);
309 }
310 
drm_fb_swab32_line(void * dbuf,const void * sbuf,unsigned int pixels)311 static void drm_fb_swab32_line(void *dbuf, const void *sbuf, unsigned int pixels)
312 {
313 	u32 *dbuf32 = dbuf;
314 	const u32 *sbuf32 = sbuf;
315 	const u32 *send32 = sbuf32 + pixels;
316 
317 	while (sbuf32 < send32)
318 		*dbuf32++ = swab32(*sbuf32++);
319 }
320 
321 /**
322  * drm_fb_swab - Swap bytes into clip buffer
323  * @dst: Array of destination buffers
324  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
325  *             within @dst; can be NULL if scanlines are stored next to each other.
326  * @src: Array of source buffers
327  * @fb: DRM framebuffer
328  * @clip: Clip rectangle area to copy
329  * @cached: Source buffer is mapped cached (eg. not write-combined)
330  * @state: Transform and conversion state
331  *
332  * This function copies parts of a framebuffer to display memory and swaps per-pixel
333  * bytes during the process. Destination and framebuffer formats must match. The
334  * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
335  * least as many entries as there are planes in @fb's format. Each entry stores the
336  * value for the format's respective color plane at the same index. If @cached is
337  * false a temporary buffer is used to cache one pixel line at a time to speed up
338  * slow uncached reads.
339  *
340  * This function does not apply clipping on @dst (i.e. the destination is at the
341  * top-left corner).
342  */
drm_fb_swab(struct iosys_map * dst,const unsigned int * dst_pitch,const struct iosys_map * src,const struct drm_framebuffer * fb,const struct drm_rect * clip,bool cached,struct drm_format_conv_state * state)343 void drm_fb_swab(struct iosys_map *dst, const unsigned int *dst_pitch,
344 		 const struct iosys_map *src, const struct drm_framebuffer *fb,
345 		 const struct drm_rect *clip, bool cached,
346 		 struct drm_format_conv_state *state)
347 {
348 	const struct drm_format_info *format = fb->format;
349 	u8 cpp = DIV_ROUND_UP(drm_format_info_bpp(format, 0), 8);
350 	void (*swab_line)(void *dbuf, const void *sbuf, unsigned int npixels);
351 
352 	switch (cpp) {
353 	case 4:
354 		swab_line = drm_fb_swab32_line;
355 		break;
356 	case 2:
357 		swab_line = drm_fb_swab16_line;
358 		break;
359 	default:
360 		drm_warn_once(fb->dev, "Format %p4cc has unsupported pixel size.\n",
361 			      &format->format);
362 		return;
363 	}
364 
365 	drm_fb_xfrm(dst, dst_pitch, &cpp, src, fb, clip, cached, state, swab_line);
366 }
367 EXPORT_SYMBOL(drm_fb_swab);
368 
drm_fb_xrgb8888_to_rgb332_line(void * dbuf,const void * sbuf,unsigned int pixels)369 static void drm_fb_xrgb8888_to_rgb332_line(void *dbuf, const void *sbuf, unsigned int pixels)
370 {
371 	u8 *dbuf8 = dbuf;
372 	const __le32 *sbuf32 = sbuf;
373 	unsigned int x;
374 	u32 pix;
375 
376 	for (x = 0; x < pixels; x++) {
377 		pix = le32_to_cpu(sbuf32[x]);
378 		dbuf8[x] = ((pix & 0x00e00000) >> 16) |
379 			   ((pix & 0x0000e000) >> 11) |
380 			   ((pix & 0x000000c0) >> 6);
381 	}
382 }
383 
384 /**
385  * drm_fb_xrgb8888_to_rgb332 - Convert XRGB8888 to RGB332 clip buffer
386  * @dst: Array of RGB332 destination buffers
387  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
388  *             within @dst; can be NULL if scanlines are stored next to each other.
389  * @src: Array of XRGB8888 source buffers
390  * @fb: DRM framebuffer
391  * @clip: Clip rectangle area to copy
392  * @state: Transform and conversion state
393  *
394  * This function copies parts of a framebuffer to display memory and converts the
395  * color format during the process. Destination and framebuffer formats must match. The
396  * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
397  * least as many entries as there are planes in @fb's format. Each entry stores the
398  * value for the format's respective color plane at the same index.
399  *
400  * This function does not apply clipping on @dst (i.e. the destination is at the
401  * top-left corner).
402  *
403  * Drivers can use this function for RGB332 devices that don't support XRGB8888 natively.
404  */
drm_fb_xrgb8888_to_rgb332(struct iosys_map * dst,const unsigned int * dst_pitch,const struct iosys_map * src,const struct drm_framebuffer * fb,const struct drm_rect * clip,struct drm_format_conv_state * state)405 void drm_fb_xrgb8888_to_rgb332(struct iosys_map *dst, const unsigned int *dst_pitch,
406 			       const struct iosys_map *src, const struct drm_framebuffer *fb,
407 			       const struct drm_rect *clip, struct drm_format_conv_state *state)
408 {
409 	static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
410 		1,
411 	};
412 
413 	drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false, state,
414 		    drm_fb_xrgb8888_to_rgb332_line);
415 }
416 EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb332);
417 
drm_fb_xrgb8888_to_rgb565_line(void * dbuf,const void * sbuf,unsigned int pixels)418 static void drm_fb_xrgb8888_to_rgb565_line(void *dbuf, const void *sbuf, unsigned int pixels)
419 {
420 	__le16 *dbuf16 = dbuf;
421 	const __le32 *sbuf32 = sbuf;
422 	unsigned int x;
423 	u16 val16;
424 	u32 pix;
425 
426 	for (x = 0; x < pixels; x++) {
427 		pix = le32_to_cpu(sbuf32[x]);
428 		val16 = ((pix & 0x00F80000) >> 8) |
429 			((pix & 0x0000FC00) >> 5) |
430 			((pix & 0x000000F8) >> 3);
431 		dbuf16[x] = cpu_to_le16(val16);
432 	}
433 }
434 
435 /* TODO: implement this helper as conversion to RGB565|BIG_ENDIAN */
drm_fb_xrgb8888_to_rgb565_swab_line(void * dbuf,const void * sbuf,unsigned int pixels)436 static void drm_fb_xrgb8888_to_rgb565_swab_line(void *dbuf, const void *sbuf,
437 						unsigned int pixels)
438 {
439 	__le16 *dbuf16 = dbuf;
440 	const __le32 *sbuf32 = sbuf;
441 	unsigned int x;
442 	u16 val16;
443 	u32 pix;
444 
445 	for (x = 0; x < pixels; x++) {
446 		pix = le32_to_cpu(sbuf32[x]);
447 		val16 = ((pix & 0x00F80000) >> 8) |
448 			((pix & 0x0000FC00) >> 5) |
449 			((pix & 0x000000F8) >> 3);
450 		dbuf16[x] = cpu_to_le16(swab16(val16));
451 	}
452 }
453 
454 /**
455  * drm_fb_xrgb8888_to_rgb565 - Convert XRGB8888 to RGB565 clip buffer
456  * @dst: Array of RGB565 destination buffers
457  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
458  *             within @dst; can be NULL if scanlines are stored next to each other.
459  * @src: Array of XRGB8888 source buffer
460  * @fb: DRM framebuffer
461  * @clip: Clip rectangle area to copy
462  * @state: Transform and conversion state
463  * @swab: Swap bytes
464  *
465  * This function copies parts of a framebuffer to display memory and converts the
466  * color format during the process. Destination and framebuffer formats must match. The
467  * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
468  * least as many entries as there are planes in @fb's format. Each entry stores the
469  * value for the format's respective color plane at the same index.
470  *
471  * This function does not apply clipping on @dst (i.e. the destination is at the
472  * top-left corner).
473  *
474  * Drivers can use this function for RGB565 devices that don't support XRGB8888 natively.
475  */
drm_fb_xrgb8888_to_rgb565(struct iosys_map * dst,const unsigned int * dst_pitch,const struct iosys_map * src,const struct drm_framebuffer * fb,const struct drm_rect * clip,struct drm_format_conv_state * state,bool swab)476 void drm_fb_xrgb8888_to_rgb565(struct iosys_map *dst, const unsigned int *dst_pitch,
477 			       const struct iosys_map *src, const struct drm_framebuffer *fb,
478 			       const struct drm_rect *clip, struct drm_format_conv_state *state,
479 			       bool swab)
480 {
481 	static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
482 		2,
483 	};
484 
485 	void (*xfrm_line)(void *dbuf, const void *sbuf, unsigned int npixels);
486 
487 	if (swab)
488 		xfrm_line = drm_fb_xrgb8888_to_rgb565_swab_line;
489 	else
490 		xfrm_line = drm_fb_xrgb8888_to_rgb565_line;
491 
492 	drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false, state, xfrm_line);
493 }
494 EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb565);
495 
drm_fb_xrgb8888_to_xrgb1555_line(void * dbuf,const void * sbuf,unsigned int pixels)496 static void drm_fb_xrgb8888_to_xrgb1555_line(void *dbuf, const void *sbuf, unsigned int pixels)
497 {
498 	__le16 *dbuf16 = dbuf;
499 	const __le32 *sbuf32 = sbuf;
500 	unsigned int x;
501 	u16 val16;
502 	u32 pix;
503 
504 	for (x = 0; x < pixels; x++) {
505 		pix = le32_to_cpu(sbuf32[x]);
506 		val16 = ((pix & 0x00f80000) >> 9) |
507 			((pix & 0x0000f800) >> 6) |
508 			((pix & 0x000000f8) >> 3);
509 		dbuf16[x] = cpu_to_le16(val16);
510 	}
511 }
512 
513 /**
514  * drm_fb_xrgb8888_to_xrgb1555 - Convert XRGB8888 to XRGB1555 clip buffer
515  * @dst: Array of XRGB1555 destination buffers
516  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
517  *             within @dst; can be NULL if scanlines are stored next to each other.
518  * @src: Array of XRGB8888 source buffer
519  * @fb: DRM framebuffer
520  * @clip: Clip rectangle area to copy
521  * @state: Transform and conversion state
522  *
523  * This function copies parts of a framebuffer to display memory and converts
524  * the color format during the process. The parameters @dst, @dst_pitch and
525  * @src refer to arrays. Each array must have at least as many entries as
526  * there are planes in @fb's format. Each entry stores the value for the
527  * format's respective color plane at the same index.
528  *
529  * This function does not apply clipping on @dst (i.e. the destination is at the
530  * top-left corner).
531  *
532  * Drivers can use this function for XRGB1555 devices that don't support
533  * XRGB8888 natively.
534  */
drm_fb_xrgb8888_to_xrgb1555(struct iosys_map * dst,const unsigned int * dst_pitch,const struct iosys_map * src,const struct drm_framebuffer * fb,const struct drm_rect * clip,struct drm_format_conv_state * state)535 void drm_fb_xrgb8888_to_xrgb1555(struct iosys_map *dst, const unsigned int *dst_pitch,
536 				 const struct iosys_map *src, const struct drm_framebuffer *fb,
537 				 const struct drm_rect *clip, struct drm_format_conv_state *state)
538 {
539 	static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
540 		2,
541 	};
542 
543 	drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false, state,
544 		    drm_fb_xrgb8888_to_xrgb1555_line);
545 }
546 EXPORT_SYMBOL(drm_fb_xrgb8888_to_xrgb1555);
547 
drm_fb_xrgb8888_to_argb1555_line(void * dbuf,const void * sbuf,unsigned int pixels)548 static void drm_fb_xrgb8888_to_argb1555_line(void *dbuf, const void *sbuf, unsigned int pixels)
549 {
550 	__le16 *dbuf16 = dbuf;
551 	const __le32 *sbuf32 = sbuf;
552 	unsigned int x;
553 	u16 val16;
554 	u32 pix;
555 
556 	for (x = 0; x < pixels; x++) {
557 		pix = le32_to_cpu(sbuf32[x]);
558 		val16 = BIT(15) | /* set alpha bit */
559 			((pix & 0x00f80000) >> 9) |
560 			((pix & 0x0000f800) >> 6) |
561 			((pix & 0x000000f8) >> 3);
562 		dbuf16[x] = cpu_to_le16(val16);
563 	}
564 }
565 
566 /**
567  * drm_fb_xrgb8888_to_argb1555 - Convert XRGB8888 to ARGB1555 clip buffer
568  * @dst: Array of ARGB1555 destination buffers
569  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
570  *             within @dst; can be NULL if scanlines are stored next to each other.
571  * @src: Array of XRGB8888 source buffer
572  * @fb: DRM framebuffer
573  * @clip: Clip rectangle area to copy
574  * @state: Transform and conversion state
575  *
576  * This function copies parts of a framebuffer to display memory and converts
577  * the color format during the process. The parameters @dst, @dst_pitch and
578  * @src refer to arrays. Each array must have at least as many entries as
579  * there are planes in @fb's format. Each entry stores the value for the
580  * format's respective color plane at the same index.
581  *
582  * This function does not apply clipping on @dst (i.e. the destination is at the
583  * top-left corner).
584  *
585  * Drivers can use this function for ARGB1555 devices that don't support
586  * XRGB8888 natively. It sets an opaque alpha channel as part of the conversion.
587  */
drm_fb_xrgb8888_to_argb1555(struct iosys_map * dst,const unsigned int * dst_pitch,const struct iosys_map * src,const struct drm_framebuffer * fb,const struct drm_rect * clip,struct drm_format_conv_state * state)588 void drm_fb_xrgb8888_to_argb1555(struct iosys_map *dst, const unsigned int *dst_pitch,
589 				 const struct iosys_map *src, const struct drm_framebuffer *fb,
590 				 const struct drm_rect *clip, struct drm_format_conv_state *state)
591 {
592 	static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
593 		2,
594 	};
595 
596 	drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false, state,
597 		    drm_fb_xrgb8888_to_argb1555_line);
598 }
599 EXPORT_SYMBOL(drm_fb_xrgb8888_to_argb1555);
600 
drm_fb_xrgb8888_to_rgba5551_line(void * dbuf,const void * sbuf,unsigned int pixels)601 static void drm_fb_xrgb8888_to_rgba5551_line(void *dbuf, const void *sbuf, unsigned int pixels)
602 {
603 	__le16 *dbuf16 = dbuf;
604 	const __le32 *sbuf32 = sbuf;
605 	unsigned int x;
606 	u16 val16;
607 	u32 pix;
608 
609 	for (x = 0; x < pixels; x++) {
610 		pix = le32_to_cpu(sbuf32[x]);
611 		val16 = ((pix & 0x00f80000) >> 8) |
612 			((pix & 0x0000f800) >> 5) |
613 			((pix & 0x000000f8) >> 2) |
614 			BIT(0); /* set alpha bit */
615 		dbuf16[x] = cpu_to_le16(val16);
616 	}
617 }
618 
619 /**
620  * drm_fb_xrgb8888_to_rgba5551 - Convert XRGB8888 to RGBA5551 clip buffer
621  * @dst: Array of RGBA5551 destination buffers
622  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
623  *             within @dst; can be NULL if scanlines are stored next to each other.
624  * @src: Array of XRGB8888 source buffer
625  * @fb: DRM framebuffer
626  * @clip: Clip rectangle area to copy
627  * @state: Transform and conversion state
628  *
629  * This function copies parts of a framebuffer to display memory and converts
630  * the color format during the process. The parameters @dst, @dst_pitch and
631  * @src refer to arrays. Each array must have at least as many entries as
632  * there are planes in @fb's format. Each entry stores the value for the
633  * format's respective color plane at the same index.
634  *
635  * This function does not apply clipping on @dst (i.e. the destination is at the
636  * top-left corner).
637  *
638  * Drivers can use this function for RGBA5551 devices that don't support
639  * XRGB8888 natively. It sets an opaque alpha channel as part of the conversion.
640  */
drm_fb_xrgb8888_to_rgba5551(struct iosys_map * dst,const unsigned int * dst_pitch,const struct iosys_map * src,const struct drm_framebuffer * fb,const struct drm_rect * clip,struct drm_format_conv_state * state)641 void drm_fb_xrgb8888_to_rgba5551(struct iosys_map *dst, const unsigned int *dst_pitch,
642 				 const struct iosys_map *src, const struct drm_framebuffer *fb,
643 				 const struct drm_rect *clip, struct drm_format_conv_state *state)
644 {
645 	static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
646 		2,
647 	};
648 
649 	drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false, state,
650 		    drm_fb_xrgb8888_to_rgba5551_line);
651 }
652 EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgba5551);
653 
drm_fb_xrgb8888_to_rgb888_line(void * dbuf,const void * sbuf,unsigned int pixels)654 static void drm_fb_xrgb8888_to_rgb888_line(void *dbuf, const void *sbuf, unsigned int pixels)
655 {
656 	u8 *dbuf8 = dbuf;
657 	const __le32 *sbuf32 = sbuf;
658 	unsigned int x;
659 	u32 pix;
660 
661 	for (x = 0; x < pixels; x++) {
662 		pix = le32_to_cpu(sbuf32[x]);
663 		/* write blue-green-red to output in little endianness */
664 		*dbuf8++ = (pix & 0x000000FF) >>  0;
665 		*dbuf8++ = (pix & 0x0000FF00) >>  8;
666 		*dbuf8++ = (pix & 0x00FF0000) >> 16;
667 	}
668 }
669 
670 /**
671  * drm_fb_xrgb8888_to_rgb888 - Convert XRGB8888 to RGB888 clip buffer
672  * @dst: Array of RGB888 destination buffers
673  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
674  *             within @dst; can be NULL if scanlines are stored next to each other.
675  * @src: Array of XRGB8888 source buffers
676  * @fb: DRM framebuffer
677  * @clip: Clip rectangle area to copy
678  * @state: Transform and conversion state
679  *
680  * This function copies parts of a framebuffer to display memory and converts the
681  * color format during the process. Destination and framebuffer formats must match. The
682  * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
683  * least as many entries as there are planes in @fb's format. Each entry stores the
684  * value for the format's respective color plane at the same index.
685  *
686  * This function does not apply clipping on @dst (i.e. the destination is at the
687  * top-left corner).
688  *
689  * Drivers can use this function for RGB888 devices that don't natively
690  * support XRGB8888.
691  */
drm_fb_xrgb8888_to_rgb888(struct iosys_map * dst,const unsigned int * dst_pitch,const struct iosys_map * src,const struct drm_framebuffer * fb,const struct drm_rect * clip,struct drm_format_conv_state * state)692 void drm_fb_xrgb8888_to_rgb888(struct iosys_map *dst, const unsigned int *dst_pitch,
693 			       const struct iosys_map *src, const struct drm_framebuffer *fb,
694 			       const struct drm_rect *clip, struct drm_format_conv_state *state)
695 {
696 	static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
697 		3,
698 	};
699 
700 	drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false, state,
701 		    drm_fb_xrgb8888_to_rgb888_line);
702 }
703 EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb888);
704 
drm_fb_xrgb8888_to_bgr888_line(void * dbuf,const void * sbuf,unsigned int pixels)705 static void drm_fb_xrgb8888_to_bgr888_line(void *dbuf, const void *sbuf, unsigned int pixels)
706 {
707 	u8 *dbuf8 = dbuf;
708 	const __le32 *sbuf32 = sbuf;
709 	unsigned int x;
710 	u32 pix;
711 
712 	for (x = 0; x < pixels; x++) {
713 		pix = le32_to_cpu(sbuf32[x]);
714 		/* write red-green-blue to output in little endianness */
715 		*dbuf8++ = (pix & 0x00ff0000) >> 16;
716 		*dbuf8++ = (pix & 0x0000ff00) >> 8;
717 		*dbuf8++ = (pix & 0x000000ff) >> 0;
718 	}
719 }
720 
721 /**
722  * drm_fb_xrgb8888_to_bgr888 - Convert XRGB8888 to BGR888 clip buffer
723  * @dst: Array of BGR888 destination buffers
724  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
725  *             within @dst; can be NULL if scanlines are stored next to each other.
726  * @src: Array of XRGB8888 source buffers
727  * @fb: DRM framebuffer
728  * @clip: Clip rectangle area to copy
729  * @state: Transform and conversion state
730  *
731  * This function copies parts of a framebuffer to display memory and converts the
732  * color format during the process. Destination and framebuffer formats must match. The
733  * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
734  * least as many entries as there are planes in @fb's format. Each entry stores the
735  * value for the format's respective color plane at the same index.
736  *
737  * This function does not apply clipping on @dst (i.e. the destination is at the
738  * top-left corner).
739  *
740  * Drivers can use this function for BGR888 devices that don't natively
741  * support XRGB8888.
742  */
drm_fb_xrgb8888_to_bgr888(struct iosys_map * dst,const unsigned int * dst_pitch,const struct iosys_map * src,const struct drm_framebuffer * fb,const struct drm_rect * clip,struct drm_format_conv_state * state)743 void drm_fb_xrgb8888_to_bgr888(struct iosys_map *dst, const unsigned int *dst_pitch,
744 			       const struct iosys_map *src, const struct drm_framebuffer *fb,
745 			       const struct drm_rect *clip, struct drm_format_conv_state *state)
746 {
747 	static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
748 		3,
749 	};
750 
751 	drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false, state,
752 		    drm_fb_xrgb8888_to_bgr888_line);
753 }
754 EXPORT_SYMBOL(drm_fb_xrgb8888_to_bgr888);
755 
drm_fb_xrgb8888_to_argb8888_line(void * dbuf,const void * sbuf,unsigned int pixels)756 static void drm_fb_xrgb8888_to_argb8888_line(void *dbuf, const void *sbuf, unsigned int pixels)
757 {
758 	__le32 *dbuf32 = dbuf;
759 	const __le32 *sbuf32 = sbuf;
760 	unsigned int x;
761 	u32 pix;
762 
763 	for (x = 0; x < pixels; x++) {
764 		pix = le32_to_cpu(sbuf32[x]);
765 		pix |= GENMASK(31, 24); /* fill alpha bits */
766 		dbuf32[x] = cpu_to_le32(pix);
767 	}
768 }
769 
770 /**
771  * drm_fb_xrgb8888_to_argb8888 - Convert XRGB8888 to ARGB8888 clip buffer
772  * @dst: Array of ARGB8888 destination buffers
773  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
774  *             within @dst; can be NULL if scanlines are stored next to each other.
775  * @src: Array of XRGB8888 source buffer
776  * @fb: DRM framebuffer
777  * @clip: Clip rectangle area to copy
778  * @state: Transform and conversion state
779  *
780  * This function copies parts of a framebuffer to display memory and converts the
781  * color format during the process. The parameters @dst, @dst_pitch and @src refer
782  * to arrays. Each array must have at least as many entries as there are planes in
783  * @fb's format. Each entry stores the value for the format's respective color plane
784  * at the same index.
785  *
786  * This function does not apply clipping on @dst (i.e. the destination is at the
787  * top-left corner).
788  *
789  * Drivers can use this function for ARGB8888 devices that don't support XRGB8888
790  * natively. It sets an opaque alpha channel as part of the conversion.
791  */
drm_fb_xrgb8888_to_argb8888(struct iosys_map * dst,const unsigned int * dst_pitch,const struct iosys_map * src,const struct drm_framebuffer * fb,const struct drm_rect * clip,struct drm_format_conv_state * state)792 void drm_fb_xrgb8888_to_argb8888(struct iosys_map *dst, const unsigned int *dst_pitch,
793 				 const struct iosys_map *src, const struct drm_framebuffer *fb,
794 				 const struct drm_rect *clip, struct drm_format_conv_state *state)
795 {
796 	static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
797 		4,
798 	};
799 
800 	drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false, state,
801 		    drm_fb_xrgb8888_to_argb8888_line);
802 }
803 EXPORT_SYMBOL(drm_fb_xrgb8888_to_argb8888);
804 
drm_fb_xrgb8888_to_abgr8888_line(void * dbuf,const void * sbuf,unsigned int pixels)805 static void drm_fb_xrgb8888_to_abgr8888_line(void *dbuf, const void *sbuf, unsigned int pixels)
806 {
807 	__le32 *dbuf32 = dbuf;
808 	const __le32 *sbuf32 = sbuf;
809 	unsigned int x;
810 	u32 pix;
811 
812 	for (x = 0; x < pixels; x++) {
813 		pix = le32_to_cpu(sbuf32[x]);
814 		pix = ((pix & 0x00ff0000) >> 16) <<  0 |
815 		      ((pix & 0x0000ff00) >>  8) <<  8 |
816 		      ((pix & 0x000000ff) >>  0) << 16 |
817 		      GENMASK(31, 24); /* fill alpha bits */
818 		*dbuf32++ = cpu_to_le32(pix);
819 	}
820 }
821 
drm_fb_xrgb8888_to_abgr8888(struct iosys_map * dst,const unsigned int * dst_pitch,const struct iosys_map * src,const struct drm_framebuffer * fb,const struct drm_rect * clip,struct drm_format_conv_state * state)822 static void drm_fb_xrgb8888_to_abgr8888(struct iosys_map *dst, const unsigned int *dst_pitch,
823 					const struct iosys_map *src,
824 					const struct drm_framebuffer *fb,
825 					const struct drm_rect *clip,
826 					struct drm_format_conv_state *state)
827 {
828 	static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
829 		4,
830 	};
831 
832 	drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false, state,
833 		    drm_fb_xrgb8888_to_abgr8888_line);
834 }
835 
drm_fb_xrgb8888_to_xbgr8888_line(void * dbuf,const void * sbuf,unsigned int pixels)836 static void drm_fb_xrgb8888_to_xbgr8888_line(void *dbuf, const void *sbuf, unsigned int pixels)
837 {
838 	__le32 *dbuf32 = dbuf;
839 	const __le32 *sbuf32 = sbuf;
840 	unsigned int x;
841 	u32 pix;
842 
843 	for (x = 0; x < pixels; x++) {
844 		pix = le32_to_cpu(sbuf32[x]);
845 		pix = ((pix & 0x00ff0000) >> 16) <<  0 |
846 		      ((pix & 0x0000ff00) >>  8) <<  8 |
847 		      ((pix & 0x000000ff) >>  0) << 16 |
848 		      ((pix & 0xff000000) >> 24) << 24;
849 		*dbuf32++ = cpu_to_le32(pix);
850 	}
851 }
852 
drm_fb_xrgb8888_to_xbgr8888(struct iosys_map * dst,const unsigned int * dst_pitch,const struct iosys_map * src,const struct drm_framebuffer * fb,const struct drm_rect * clip,struct drm_format_conv_state * state)853 static void drm_fb_xrgb8888_to_xbgr8888(struct iosys_map *dst, const unsigned int *dst_pitch,
854 					const struct iosys_map *src,
855 					const struct drm_framebuffer *fb,
856 					const struct drm_rect *clip,
857 					struct drm_format_conv_state *state)
858 {
859 	static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
860 		4,
861 	};
862 
863 	drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false, state,
864 		    drm_fb_xrgb8888_to_xbgr8888_line);
865 }
866 
drm_fb_xrgb8888_to_xrgb2101010_line(void * dbuf,const void * sbuf,unsigned int pixels)867 static void drm_fb_xrgb8888_to_xrgb2101010_line(void *dbuf, const void *sbuf, unsigned int pixels)
868 {
869 	__le32 *dbuf32 = dbuf;
870 	const __le32 *sbuf32 = sbuf;
871 	unsigned int x;
872 	u32 val32;
873 	u32 pix;
874 
875 	for (x = 0; x < pixels; x++) {
876 		pix = le32_to_cpu(sbuf32[x]);
877 		val32 = ((pix & 0x000000FF) << 2) |
878 			((pix & 0x0000FF00) << 4) |
879 			((pix & 0x00FF0000) << 6);
880 		pix = val32 | ((val32 >> 8) & 0x00300C03);
881 		*dbuf32++ = cpu_to_le32(pix);
882 	}
883 }
884 
885 /**
886  * drm_fb_xrgb8888_to_xrgb2101010 - Convert XRGB8888 to XRGB2101010 clip buffer
887  * @dst: Array of XRGB2101010 destination buffers
888  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
889  *             within @dst; can be NULL if scanlines are stored next to each other.
890  * @src: Array of XRGB8888 source buffers
891  * @fb: DRM framebuffer
892  * @clip: Clip rectangle area to copy
893  * @state: Transform and conversion state
894  *
895  * This function copies parts of a framebuffer to display memory and converts the
896  * color format during the process. Destination and framebuffer formats must match. The
897  * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
898  * least as many entries as there are planes in @fb's format. Each entry stores the
899  * value for the format's respective color plane at the same index.
900  *
901  * This function does not apply clipping on @dst (i.e. the destination is at the
902  * top-left corner).
903  *
904  * Drivers can use this function for XRGB2101010 devices that don't support XRGB8888
905  * natively.
906  */
drm_fb_xrgb8888_to_xrgb2101010(struct iosys_map * dst,const unsigned int * dst_pitch,const struct iosys_map * src,const struct drm_framebuffer * fb,const struct drm_rect * clip,struct drm_format_conv_state * state)907 void drm_fb_xrgb8888_to_xrgb2101010(struct iosys_map *dst, const unsigned int *dst_pitch,
908 				    const struct iosys_map *src, const struct drm_framebuffer *fb,
909 				    const struct drm_rect *clip,
910 				    struct drm_format_conv_state *state)
911 {
912 	static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
913 		4,
914 	};
915 
916 	drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false, state,
917 		    drm_fb_xrgb8888_to_xrgb2101010_line);
918 }
919 EXPORT_SYMBOL(drm_fb_xrgb8888_to_xrgb2101010);
920 
drm_fb_xrgb8888_to_argb2101010_line(void * dbuf,const void * sbuf,unsigned int pixels)921 static void drm_fb_xrgb8888_to_argb2101010_line(void *dbuf, const void *sbuf, unsigned int pixels)
922 {
923 	__le32 *dbuf32 = dbuf;
924 	const __le32 *sbuf32 = sbuf;
925 	unsigned int x;
926 	u32 val32;
927 	u32 pix;
928 
929 	for (x = 0; x < pixels; x++) {
930 		pix = le32_to_cpu(sbuf32[x]);
931 		val32 = ((pix & 0x000000ff) << 2) |
932 			((pix & 0x0000ff00) << 4) |
933 			((pix & 0x00ff0000) << 6);
934 		pix = GENMASK(31, 30) | /* set alpha bits */
935 		      val32 | ((val32 >> 8) & 0x00300c03);
936 		*dbuf32++ = cpu_to_le32(pix);
937 	}
938 }
939 
940 /**
941  * drm_fb_xrgb8888_to_argb2101010 - Convert XRGB8888 to ARGB2101010 clip buffer
942  * @dst: Array of ARGB2101010 destination buffers
943  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
944  *             within @dst; can be NULL if scanlines are stored next to each other.
945  * @src: Array of XRGB8888 source buffers
946  * @fb: DRM framebuffer
947  * @clip: Clip rectangle area to copy
948  * @state: Transform and conversion state
949  *
950  * This function copies parts of a framebuffer to display memory and converts
951  * the color format during the process. The parameters @dst, @dst_pitch and
952  * @src refer to arrays. Each array must have at least as many entries as
953  * there are planes in @fb's format. Each entry stores the value for the
954  * format's respective color plane at the same index.
955  *
956  * This function does not apply clipping on @dst (i.e. the destination is at the
957  * top-left corner).
958  *
959  * Drivers can use this function for ARGB2101010 devices that don't support XRGB8888
960  * natively.
961  */
drm_fb_xrgb8888_to_argb2101010(struct iosys_map * dst,const unsigned int * dst_pitch,const struct iosys_map * src,const struct drm_framebuffer * fb,const struct drm_rect * clip,struct drm_format_conv_state * state)962 void drm_fb_xrgb8888_to_argb2101010(struct iosys_map *dst, const unsigned int *dst_pitch,
963 				    const struct iosys_map *src, const struct drm_framebuffer *fb,
964 				    const struct drm_rect *clip,
965 				    struct drm_format_conv_state *state)
966 {
967 	static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
968 		4,
969 	};
970 
971 	drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false, state,
972 		    drm_fb_xrgb8888_to_argb2101010_line);
973 }
974 EXPORT_SYMBOL(drm_fb_xrgb8888_to_argb2101010);
975 
drm_fb_xrgb8888_to_gray8_line(void * dbuf,const void * sbuf,unsigned int pixels)976 static void drm_fb_xrgb8888_to_gray8_line(void *dbuf, const void *sbuf, unsigned int pixels)
977 {
978 	u8 *dbuf8 = dbuf;
979 	const __le32 *sbuf32 = sbuf;
980 	unsigned int x;
981 
982 	for (x = 0; x < pixels; x++) {
983 		u32 pix = le32_to_cpu(sbuf32[x]);
984 		u8 r = (pix & 0x00ff0000) >> 16;
985 		u8 g = (pix & 0x0000ff00) >> 8;
986 		u8 b =  pix & 0x000000ff;
987 
988 		/* ITU BT.601: Y = 0.299 R + 0.587 G + 0.114 B */
989 		*dbuf8++ = (3 * r + 6 * g + b) / 10;
990 	}
991 }
992 
993 /**
994  * drm_fb_xrgb8888_to_gray8 - Convert XRGB8888 to grayscale
995  * @dst: Array of 8-bit grayscale destination buffers
996  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
997  *             within @dst; can be NULL if scanlines are stored next to each other.
998  * @src: Array of XRGB8888 source buffers
999  * @fb: DRM framebuffer
1000  * @clip: Clip rectangle area to copy
1001  * @state: Transform and conversion state
1002  *
1003  * This function copies parts of a framebuffer to display memory and converts the
1004  * color format during the process. Destination and framebuffer formats must match. The
1005  * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
1006  * least as many entries as there are planes in @fb's format. Each entry stores the
1007  * value for the format's respective color plane at the same index.
1008  *
1009  * This function does not apply clipping on @dst (i.e. the destination is at the
1010  * top-left corner).
1011  *
1012  * DRM doesn't have native monochrome or grayscale support. Drivers can use this
1013  * function for grayscale devices that don't support XRGB8888 natively.Such
1014  * drivers can announce the commonly supported XR24 format to userspace and use
1015  * this function to convert to the native format. Monochrome drivers will use the
1016  * most significant bit, where 1 means foreground color and 0 background color.
1017  * ITU BT.601 is being used for the RGB -> luma (brightness) conversion.
1018  */
drm_fb_xrgb8888_to_gray8(struct iosys_map * dst,const unsigned int * dst_pitch,const struct iosys_map * src,const struct drm_framebuffer * fb,const struct drm_rect * clip,struct drm_format_conv_state * state)1019 void drm_fb_xrgb8888_to_gray8(struct iosys_map *dst, const unsigned int *dst_pitch,
1020 			      const struct iosys_map *src, const struct drm_framebuffer *fb,
1021 			      const struct drm_rect *clip, struct drm_format_conv_state *state)
1022 {
1023 	static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
1024 		1,
1025 	};
1026 
1027 	drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false, state,
1028 		    drm_fb_xrgb8888_to_gray8_line);
1029 }
1030 EXPORT_SYMBOL(drm_fb_xrgb8888_to_gray8);
1031 
drm_fb_argb8888_to_argb4444_line(void * dbuf,const void * sbuf,unsigned int pixels)1032 static void drm_fb_argb8888_to_argb4444_line(void *dbuf, const void *sbuf, unsigned int pixels)
1033 {
1034 	unsigned int pixels2 = pixels & ~GENMASK_ULL(0, 0);
1035 	__le32 *dbuf32 = dbuf;
1036 	__le16 *dbuf16 = dbuf + pixels2 * sizeof(*dbuf16);
1037 	const __le32 *sbuf32 = sbuf;
1038 	unsigned int x;
1039 	u32 val32;
1040 	u16 val16;
1041 	u32 pix[2];
1042 
1043 	for (x = 0; x < pixels2; x += 2, ++dbuf32) {
1044 		pix[0] = le32_to_cpu(sbuf32[x]);
1045 		pix[1] = le32_to_cpu(sbuf32[x + 1]);
1046 		val32 = ((pix[0] & 0xf0000000) >> 16) |
1047 			((pix[0] & 0x00f00000) >> 12) |
1048 			((pix[0] & 0x0000f000) >> 8) |
1049 			((pix[0] & 0x000000f0) >> 4) |
1050 			((pix[1] & 0xf0000000) >> 0) |
1051 			((pix[1] & 0x00f00000) << 4) |
1052 			((pix[1] & 0x0000f000) << 8) |
1053 			((pix[1] & 0x000000f0) << 12);
1054 		*dbuf32 = cpu_to_le32(val32);
1055 	}
1056 	for (; x < pixels; x++) {
1057 		pix[0] = le32_to_cpu(sbuf32[x]);
1058 		val16 = ((pix[0] & 0xf0000000) >> 16) |
1059 			((pix[0] & 0x00f00000) >> 12) |
1060 			((pix[0] & 0x0000f000) >> 8) |
1061 			((pix[0] & 0x000000f0) >> 4);
1062 		dbuf16[x] = cpu_to_le16(val16);
1063 	}
1064 }
1065 
1066 /**
1067  * drm_fb_argb8888_to_argb4444 - Convert ARGB8888 to ARGB4444 clip buffer
1068  * @dst: Array of ARGB4444 destination buffers
1069  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
1070  *             within @dst; can be NULL if scanlines are stored next to each other.
1071  * @src: Array of ARGB8888 source buffer
1072  * @fb: DRM framebuffer
1073  * @clip: Clip rectangle area to copy
1074  * @state: Transform and conversion state
1075  *
1076  * This function copies parts of a framebuffer to display memory and converts
1077  * the color format during the process. The parameters @dst, @dst_pitch and
1078  * @src refer to arrays. Each array must have at least as many entries as
1079  * there are planes in @fb's format. Each entry stores the value for the
1080  * format's respective color plane at the same index.
1081  *
1082  * This function does not apply clipping on @dst (i.e. the destination is at the
1083  * top-left corner).
1084  *
1085  * Drivers can use this function for ARGB4444 devices that don't support
1086  * ARGB8888 natively.
1087  */
drm_fb_argb8888_to_argb4444(struct iosys_map * dst,const unsigned int * dst_pitch,const struct iosys_map * src,const struct drm_framebuffer * fb,const struct drm_rect * clip,struct drm_format_conv_state * state)1088 void drm_fb_argb8888_to_argb4444(struct iosys_map *dst, const unsigned int *dst_pitch,
1089 				 const struct iosys_map *src, const struct drm_framebuffer *fb,
1090 				 const struct drm_rect *clip, struct drm_format_conv_state *state)
1091 {
1092 	static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
1093 		2,
1094 	};
1095 
1096 	drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false, state,
1097 		    drm_fb_argb8888_to_argb4444_line);
1098 }
1099 EXPORT_SYMBOL(drm_fb_argb8888_to_argb4444);
1100 
1101 /**
1102  * drm_fb_blit - Copy parts of a framebuffer to display memory
1103  * @dst:	Array of display-memory addresses to copy to
1104  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
1105  *             within @dst; can be NULL if scanlines are stored next to each other.
1106  * @dst_format:	FOURCC code of the display's color format
1107  * @src:	The framebuffer memory to copy from
1108  * @fb:		The framebuffer to copy from
1109  * @clip:	Clip rectangle area to copy
1110  * @state: Transform and conversion state
1111  *
1112  * This function copies parts of a framebuffer to display memory. If the
1113  * formats of the display and the framebuffer mismatch, the blit function
1114  * will attempt to convert between them during the process. The parameters @dst,
1115  * @dst_pitch and @src refer to arrays. Each array must have at least as many
1116  * entries as there are planes in @dst_format's format. Each entry stores the
1117  * value for the format's respective color plane at the same index.
1118  *
1119  * This function does not apply clipping on @dst (i.e. the destination is at the
1120  * top-left corner).
1121  *
1122  * Returns:
1123  * 0 on success, or
1124  * -EINVAL if the color-format conversion failed, or
1125  * a negative error code otherwise.
1126  */
drm_fb_blit(struct iosys_map * dst,const unsigned int * dst_pitch,uint32_t dst_format,const struct iosys_map * src,const struct drm_framebuffer * fb,const struct drm_rect * clip,struct drm_format_conv_state * state)1127 int drm_fb_blit(struct iosys_map *dst, const unsigned int *dst_pitch, uint32_t dst_format,
1128 		const struct iosys_map *src, const struct drm_framebuffer *fb,
1129 		const struct drm_rect *clip, struct drm_format_conv_state *state)
1130 {
1131 	uint32_t fb_format = fb->format->format;
1132 
1133 	if (fb_format == dst_format) {
1134 		drm_fb_memcpy(dst, dst_pitch, src, fb, clip);
1135 		return 0;
1136 	} else if (fb_format == (dst_format | DRM_FORMAT_BIG_ENDIAN)) {
1137 		drm_fb_swab(dst, dst_pitch, src, fb, clip, false, state);
1138 		return 0;
1139 	} else if (fb_format == (dst_format & ~DRM_FORMAT_BIG_ENDIAN)) {
1140 		drm_fb_swab(dst, dst_pitch, src, fb, clip, false, state);
1141 		return 0;
1142 	} else if (fb_format == DRM_FORMAT_XRGB8888) {
1143 		if (dst_format == DRM_FORMAT_RGB565) {
1144 			drm_fb_xrgb8888_to_rgb565(dst, dst_pitch, src, fb, clip, state, false);
1145 			return 0;
1146 		} else if (dst_format == DRM_FORMAT_XRGB1555) {
1147 			drm_fb_xrgb8888_to_xrgb1555(dst, dst_pitch, src, fb, clip, state);
1148 			return 0;
1149 		} else if (dst_format == DRM_FORMAT_ARGB1555) {
1150 			drm_fb_xrgb8888_to_argb1555(dst, dst_pitch, src, fb, clip, state);
1151 			return 0;
1152 		} else if (dst_format == DRM_FORMAT_RGBA5551) {
1153 			drm_fb_xrgb8888_to_rgba5551(dst, dst_pitch, src, fb, clip, state);
1154 			return 0;
1155 		} else if (dst_format == DRM_FORMAT_RGB888) {
1156 			drm_fb_xrgb8888_to_rgb888(dst, dst_pitch, src, fb, clip, state);
1157 			return 0;
1158 		} else if (dst_format == DRM_FORMAT_BGR888) {
1159 			drm_fb_xrgb8888_to_bgr888(dst, dst_pitch, src, fb, clip, state);
1160 			return 0;
1161 		} else if (dst_format == DRM_FORMAT_ARGB8888) {
1162 			drm_fb_xrgb8888_to_argb8888(dst, dst_pitch, src, fb, clip, state);
1163 			return 0;
1164 		} else if (dst_format == DRM_FORMAT_XBGR8888) {
1165 			drm_fb_xrgb8888_to_xbgr8888(dst, dst_pitch, src, fb, clip, state);
1166 			return 0;
1167 		} else if (dst_format == DRM_FORMAT_ABGR8888) {
1168 			drm_fb_xrgb8888_to_abgr8888(dst, dst_pitch, src, fb, clip, state);
1169 			return 0;
1170 		} else if (dst_format == DRM_FORMAT_XRGB2101010) {
1171 			drm_fb_xrgb8888_to_xrgb2101010(dst, dst_pitch, src, fb, clip, state);
1172 			return 0;
1173 		} else if (dst_format == DRM_FORMAT_ARGB2101010) {
1174 			drm_fb_xrgb8888_to_argb2101010(dst, dst_pitch, src, fb, clip, state);
1175 			return 0;
1176 		} else if (dst_format == DRM_FORMAT_BGRX8888) {
1177 			drm_fb_swab(dst, dst_pitch, src, fb, clip, false, state);
1178 			return 0;
1179 		}
1180 	}
1181 
1182 	drm_warn_once(fb->dev, "No conversion helper from %p4cc to %p4cc found.\n",
1183 		      &fb_format, &dst_format);
1184 
1185 	return -EINVAL;
1186 }
1187 EXPORT_SYMBOL(drm_fb_blit);
1188 
drm_fb_gray8_to_mono_line(void * dbuf,const void * sbuf,unsigned int pixels)1189 static void drm_fb_gray8_to_mono_line(void *dbuf, const void *sbuf, unsigned int pixels)
1190 {
1191 	u8 *dbuf8 = dbuf;
1192 	const u8 *sbuf8 = sbuf;
1193 
1194 	while (pixels) {
1195 		unsigned int i, bits = min(pixels, 8U);
1196 		u8 byte = 0;
1197 
1198 		for (i = 0; i < bits; i++, pixels--) {
1199 			if (*sbuf8++ >= 128)
1200 				byte |= BIT(i);
1201 		}
1202 		*dbuf8++ = byte;
1203 	}
1204 }
1205 
1206 /**
1207  * drm_fb_xrgb8888_to_mono - Convert XRGB8888 to monochrome
1208  * @dst: Array of monochrome destination buffers (0=black, 1=white)
1209  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
1210  *             within @dst; can be NULL if scanlines are stored next to each other.
1211  * @src: Array of XRGB8888 source buffers
1212  * @fb: DRM framebuffer
1213  * @clip: Clip rectangle area to copy
1214  * @state: Transform and conversion state
1215  *
1216  * This function copies parts of a framebuffer to display memory and converts the
1217  * color format during the process. Destination and framebuffer formats must match. The
1218  * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
1219  * least as many entries as there are planes in @fb's format. Each entry stores the
1220  * value for the format's respective color plane at the same index.
1221  *
1222  * This function does not apply clipping on @dst (i.e. the destination is at the
1223  * top-left corner). The first pixel (upper left corner of the clip rectangle) will
1224  * be converted and copied to the first bit (LSB) in the first byte of the monochrome
1225  * destination buffer. If the caller requires that the first pixel in a byte must
1226  * be located at an x-coordinate that is a multiple of 8, then the caller must take
1227  * care itself of supplying a suitable clip rectangle.
1228  *
1229  * DRM doesn't have native monochrome support. Drivers can use this function for
1230  * monochrome devices that don't support XRGB8888 natively. Such drivers can
1231  * announce the commonly supported XR24 format to userspace and use this function
1232  * to convert to the native format.
1233  *
1234  * This function uses drm_fb_xrgb8888_to_gray8() to convert to grayscale and
1235  * then the result is converted from grayscale to monochrome.
1236  */
drm_fb_xrgb8888_to_mono(struct iosys_map * dst,const unsigned int * dst_pitch,const struct iosys_map * src,const struct drm_framebuffer * fb,const struct drm_rect * clip,struct drm_format_conv_state * state)1237 void drm_fb_xrgb8888_to_mono(struct iosys_map *dst, const unsigned int *dst_pitch,
1238 			     const struct iosys_map *src, const struct drm_framebuffer *fb,
1239 			     const struct drm_rect *clip, struct drm_format_conv_state *state)
1240 {
1241 	static const unsigned int default_dst_pitch[DRM_FORMAT_MAX_PLANES] = {
1242 		0, 0, 0, 0
1243 	};
1244 	unsigned int linepixels = drm_rect_width(clip);
1245 	unsigned int lines = drm_rect_height(clip);
1246 	unsigned int cpp = fb->format->cpp[0];
1247 	unsigned int len_src32 = linepixels * cpp;
1248 	struct drm_device *dev = fb->dev;
1249 	void *vaddr = src[0].vaddr;
1250 	unsigned int dst_pitch_0;
1251 	unsigned int y;
1252 	u8 *mono = dst[0].vaddr, *gray8;
1253 	u32 *src32;
1254 
1255 	if (drm_WARN_ON(dev, fb->format->format != DRM_FORMAT_XRGB8888))
1256 		return;
1257 
1258 	if (!dst_pitch)
1259 		dst_pitch = default_dst_pitch;
1260 	dst_pitch_0 = dst_pitch[0];
1261 
1262 	/*
1263 	 * The mono destination buffer contains 1 bit per pixel
1264 	 */
1265 	if (!dst_pitch_0)
1266 		dst_pitch_0 = DIV_ROUND_UP(linepixels, 8);
1267 
1268 	/*
1269 	 * The dma memory is write-combined so reads are uncached.
1270 	 * Speed up by fetching one line at a time.
1271 	 *
1272 	 * Also, format conversion from XR24 to monochrome are done
1273 	 * line-by-line but are converted to 8-bit grayscale as an
1274 	 * intermediate step.
1275 	 *
1276 	 * Allocate a buffer to be used for both copying from the cma
1277 	 * memory and to store the intermediate grayscale line pixels.
1278 	 */
1279 	src32 = drm_format_conv_state_reserve(state, len_src32 + linepixels, GFP_KERNEL);
1280 	if (!src32)
1281 		return;
1282 
1283 	gray8 = (u8 *)src32 + len_src32;
1284 
1285 	vaddr += clip_offset(clip, fb->pitches[0], cpp);
1286 	for (y = 0; y < lines; y++) {
1287 		src32 = memcpy(src32, vaddr, len_src32);
1288 		drm_fb_xrgb8888_to_gray8_line(gray8, src32, linepixels);
1289 		drm_fb_gray8_to_mono_line(mono, gray8, linepixels);
1290 		vaddr += fb->pitches[0];
1291 		mono += dst_pitch_0;
1292 	}
1293 }
1294 EXPORT_SYMBOL(drm_fb_xrgb8888_to_mono);
1295 
drm_fb_nonalpha_fourcc(uint32_t fourcc)1296 static uint32_t drm_fb_nonalpha_fourcc(uint32_t fourcc)
1297 {
1298 	/* only handle formats with depth != 0 and alpha channel */
1299 	switch (fourcc) {
1300 	case DRM_FORMAT_ARGB1555:
1301 		return DRM_FORMAT_XRGB1555;
1302 	case DRM_FORMAT_ABGR1555:
1303 		return DRM_FORMAT_XBGR1555;
1304 	case DRM_FORMAT_RGBA5551:
1305 		return DRM_FORMAT_RGBX5551;
1306 	case DRM_FORMAT_BGRA5551:
1307 		return DRM_FORMAT_BGRX5551;
1308 	case DRM_FORMAT_ARGB8888:
1309 		return DRM_FORMAT_XRGB8888;
1310 	case DRM_FORMAT_ABGR8888:
1311 		return DRM_FORMAT_XBGR8888;
1312 	case DRM_FORMAT_RGBA8888:
1313 		return DRM_FORMAT_RGBX8888;
1314 	case DRM_FORMAT_BGRA8888:
1315 		return DRM_FORMAT_BGRX8888;
1316 	case DRM_FORMAT_ARGB2101010:
1317 		return DRM_FORMAT_XRGB2101010;
1318 	case DRM_FORMAT_ABGR2101010:
1319 		return DRM_FORMAT_XBGR2101010;
1320 	case DRM_FORMAT_RGBA1010102:
1321 		return DRM_FORMAT_RGBX1010102;
1322 	case DRM_FORMAT_BGRA1010102:
1323 		return DRM_FORMAT_BGRX1010102;
1324 	}
1325 
1326 	return fourcc;
1327 }
1328 
is_listed_fourcc(const uint32_t * fourccs,size_t nfourccs,uint32_t fourcc)1329 static bool is_listed_fourcc(const uint32_t *fourccs, size_t nfourccs, uint32_t fourcc)
1330 {
1331 	const uint32_t *fourccs_end = fourccs + nfourccs;
1332 
1333 	while (fourccs < fourccs_end) {
1334 		if (*fourccs == fourcc)
1335 			return true;
1336 		++fourccs;
1337 	}
1338 	return false;
1339 }
1340 
1341 /**
1342  * drm_fb_build_fourcc_list - Filters a list of supported color formats against
1343  *                            the device's native formats
1344  * @dev: DRM device
1345  * @native_fourccs: 4CC codes of natively supported color formats
1346  * @native_nfourccs: The number of entries in @native_fourccs
1347  * @fourccs_out: Returns 4CC codes of supported color formats
1348  * @nfourccs_out: The number of available entries in @fourccs_out
1349  *
1350  * This function create a list of supported color format from natively
1351  * supported formats and additional emulated formats.
1352  * At a minimum, most userspace programs expect at least support for
1353  * XRGB8888 on the primary plane. Devices that have to emulate the
1354  * format, and possibly others, can use drm_fb_build_fourcc_list() to
1355  * create a list of supported color formats. The returned list can
1356  * be handed over to drm_universal_plane_init() et al. Native formats
1357  * will go before emulated formats. Native formats with alpha channel
1358  * will be replaced by such without, as primary planes usually don't
1359  * support alpha. Other heuristics might be applied
1360  * to optimize the order. Formats near the beginning of the list are
1361  * usually preferred over formats near the end of the list.
1362  *
1363  * Returns:
1364  * The number of color-formats 4CC codes returned in @fourccs_out.
1365  */
drm_fb_build_fourcc_list(struct drm_device * dev,const u32 * native_fourccs,size_t native_nfourccs,u32 * fourccs_out,size_t nfourccs_out)1366 size_t drm_fb_build_fourcc_list(struct drm_device *dev,
1367 				const u32 *native_fourccs, size_t native_nfourccs,
1368 				u32 *fourccs_out, size_t nfourccs_out)
1369 {
1370 	/*
1371 	 * XRGB8888 is the default fallback format for most of userspace
1372 	 * and it's currently the only format that should be emulated for
1373 	 * the primary plane. Only if there's ever another default fallback,
1374 	 * it should be added here.
1375 	 */
1376 	static const uint32_t extra_fourccs[] = {
1377 		DRM_FORMAT_XRGB8888,
1378 	};
1379 	static const size_t extra_nfourccs = ARRAY_SIZE(extra_fourccs);
1380 
1381 	u32 *fourccs = fourccs_out;
1382 	const u32 *fourccs_end = fourccs_out + nfourccs_out;
1383 	size_t i;
1384 
1385 	/*
1386 	 * The device's native formats go first.
1387 	 */
1388 
1389 	for (i = 0; i < native_nfourccs; ++i) {
1390 		/*
1391 		 * Several DTs, boot loaders and firmware report native
1392 		 * alpha formats that are non-alpha formats instead. So
1393 		 * replace alpha formats by non-alpha formats.
1394 		 */
1395 		u32 fourcc = drm_fb_nonalpha_fourcc(native_fourccs[i]);
1396 
1397 		if (is_listed_fourcc(fourccs_out, fourccs - fourccs_out, fourcc)) {
1398 			continue; /* skip duplicate entries */
1399 		} else if (fourccs == fourccs_end) {
1400 			drm_warn(dev, "Ignoring native format %p4cc\n", &fourcc);
1401 			continue; /* end of available output buffer */
1402 		}
1403 
1404 		drm_dbg_kms(dev, "adding native format %p4cc\n", &fourcc);
1405 
1406 		*fourccs = fourcc;
1407 		++fourccs;
1408 	}
1409 
1410 	/*
1411 	 * The extra formats, emulated by the driver, go second.
1412 	 */
1413 
1414 	for (i = 0; (i < extra_nfourccs) && (fourccs < fourccs_end); ++i) {
1415 		u32 fourcc = extra_fourccs[i];
1416 
1417 		if (is_listed_fourcc(fourccs_out, fourccs - fourccs_out, fourcc)) {
1418 			continue; /* skip duplicate and native entries */
1419 		} else if (fourccs == fourccs_end) {
1420 			drm_warn(dev, "Ignoring emulated format %p4cc\n", &fourcc);
1421 			continue; /* end of available output buffer */
1422 		}
1423 
1424 		drm_dbg_kms(dev, "adding emulated format %p4cc\n", &fourcc);
1425 
1426 		*fourccs = fourcc;
1427 		++fourccs;
1428 	}
1429 
1430 	return fourccs - fourccs_out;
1431 }
1432 EXPORT_SYMBOL(drm_fb_build_fourcc_list);
1433