xref: /linux/drivers/gpu/drm/drm_format_helper.c (revision 60c376e4549b6844af94cf319960ef48080230a8)
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 static unsigned int clip_offset(const struct drm_rect *clip, unsigned int pitch, unsigned int cpp)
24 {
25 	return clip->y1 * pitch + clip->x1 * cpp;
26 }
27 
28 /**
29  * drm_fb_clip_offset - Returns the clipping rectangles byte-offset in a framebuffer
30  * @pitch: Framebuffer line pitch in byte
31  * @format: Framebuffer format
32  * @clip: Clip rectangle
33  *
34  * Returns:
35  * The byte offset of the clip rectangle's top-left corner within the framebuffer.
36  */
37 unsigned int drm_fb_clip_offset(unsigned int pitch, const struct drm_format_info *format,
38 				const struct drm_rect *clip)
39 {
40 	return clip_offset(clip, pitch, format->cpp[0]);
41 }
42 EXPORT_SYMBOL(drm_fb_clip_offset);
43 
44 /* TODO: Make this function work with multi-plane formats. */
45 static int __drm_fb_xfrm(void *dst, unsigned long dst_pitch, unsigned long dst_pixsize,
46 			 const void *vaddr, const struct drm_framebuffer *fb,
47 			 const struct drm_rect *clip, bool vaddr_cached_hint,
48 			 void (*xfrm_line)(void *dbuf, const void *sbuf, unsigned int npixels))
49 {
50 	unsigned long linepixels = drm_rect_width(clip);
51 	unsigned long lines = drm_rect_height(clip);
52 	size_t sbuf_len = linepixels * fb->format->cpp[0];
53 	void *stmp = NULL;
54 	unsigned long i;
55 	const void *sbuf;
56 
57 	/*
58 	 * Some source buffers, such as DMA memory, use write-combine
59 	 * caching, so reads are uncached. Speed up access by fetching
60 	 * one line at a time.
61 	 */
62 	if (!vaddr_cached_hint) {
63 		stmp = kmalloc(sbuf_len, GFP_KERNEL);
64 		if (!stmp)
65 			return -ENOMEM;
66 	}
67 
68 	if (!dst_pitch)
69 		dst_pitch = drm_rect_width(clip) * dst_pixsize;
70 	vaddr += clip_offset(clip, fb->pitches[0], fb->format->cpp[0]);
71 
72 	for (i = 0; i < lines; ++i) {
73 		if (stmp)
74 			sbuf = memcpy(stmp, vaddr, sbuf_len);
75 		else
76 			sbuf = vaddr;
77 		xfrm_line(dst, sbuf, linepixels);
78 		vaddr += fb->pitches[0];
79 		dst += dst_pitch;
80 	}
81 
82 	kfree(stmp);
83 
84 	return 0;
85 }
86 
87 /* TODO: Make this function work with multi-plane formats. */
88 static int __drm_fb_xfrm_toio(void __iomem *dst, unsigned long dst_pitch, unsigned long dst_pixsize,
89 			      const void *vaddr, const struct drm_framebuffer *fb,
90 			      const struct drm_rect *clip, bool vaddr_cached_hint,
91 			      void (*xfrm_line)(void *dbuf, const void *sbuf, unsigned int npixels))
92 {
93 	unsigned long linepixels = drm_rect_width(clip);
94 	unsigned long lines = drm_rect_height(clip);
95 	size_t dbuf_len = linepixels * dst_pixsize;
96 	size_t stmp_off = round_up(dbuf_len, ARCH_KMALLOC_MINALIGN); /* for sbuf alignment */
97 	size_t sbuf_len = linepixels * fb->format->cpp[0];
98 	void *stmp = NULL;
99 	unsigned long i;
100 	const void *sbuf;
101 	void *dbuf;
102 
103 	if (vaddr_cached_hint) {
104 		dbuf = kmalloc(dbuf_len, GFP_KERNEL);
105 	} else {
106 		dbuf = kmalloc(stmp_off + sbuf_len, GFP_KERNEL);
107 		stmp = dbuf + stmp_off;
108 	}
109 	if (!dbuf)
110 		return -ENOMEM;
111 
112 	if (!dst_pitch)
113 		dst_pitch = linepixels * dst_pixsize;
114 	vaddr += clip_offset(clip, fb->pitches[0], fb->format->cpp[0]);
115 
116 	for (i = 0; i < lines; ++i) {
117 		if (stmp)
118 			sbuf = memcpy(stmp, vaddr, sbuf_len);
119 		else
120 			sbuf = vaddr;
121 		xfrm_line(dbuf, sbuf, linepixels);
122 		memcpy_toio(dst, dbuf, dbuf_len);
123 		vaddr += fb->pitches[0];
124 		dst += dst_pitch;
125 	}
126 
127 	kfree(dbuf);
128 
129 	return 0;
130 }
131 
132 /* TODO: Make this function work with multi-plane formats. */
133 static int drm_fb_xfrm(struct iosys_map *dst,
134 		       const unsigned int *dst_pitch, const u8 *dst_pixsize,
135 		       const struct iosys_map *src, const struct drm_framebuffer *fb,
136 		       const struct drm_rect *clip, bool vaddr_cached_hint,
137 		       void (*xfrm_line)(void *dbuf, const void *sbuf, unsigned int npixels))
138 {
139 	static const unsigned int default_dst_pitch[DRM_FORMAT_MAX_PLANES] = {
140 		0, 0, 0, 0
141 	};
142 
143 	if (!dst_pitch)
144 		dst_pitch = default_dst_pitch;
145 
146 	/* TODO: handle src in I/O memory here */
147 	if (dst[0].is_iomem)
148 		return __drm_fb_xfrm_toio(dst[0].vaddr_iomem, dst_pitch[0], dst_pixsize[0],
149 					  src[0].vaddr, fb, clip, vaddr_cached_hint, xfrm_line);
150 	else
151 		return __drm_fb_xfrm(dst[0].vaddr, dst_pitch[0], dst_pixsize[0],
152 				     src[0].vaddr, fb, clip, vaddr_cached_hint, xfrm_line);
153 }
154 
155 /**
156  * drm_fb_memcpy - Copy clip buffer
157  * @dst: Array of destination buffers
158  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
159  *             within @dst; can be NULL if scanlines are stored next to each other.
160  * @src: Array of source buffers
161  * @fb: DRM framebuffer
162  * @clip: Clip rectangle area to copy
163  *
164  * This function copies parts of a framebuffer to display memory. Destination and
165  * framebuffer formats must match. No conversion takes place. The parameters @dst,
166  * @dst_pitch and @src refer to arrays. Each array must have at least as many entries
167  * as there are planes in @fb's format. Each entry stores the value for the format's
168  * respective color plane at the same index.
169  *
170  * This function does not apply clipping on @dst (i.e. the destination is at the
171  * top-left corner).
172  */
173 void drm_fb_memcpy(struct iosys_map *dst, const unsigned int *dst_pitch,
174 		   const struct iosys_map *src, const struct drm_framebuffer *fb,
175 		   const struct drm_rect *clip)
176 {
177 	static const unsigned int default_dst_pitch[DRM_FORMAT_MAX_PLANES] = {
178 		0, 0, 0, 0
179 	};
180 
181 	const struct drm_format_info *format = fb->format;
182 	unsigned int i, y, lines = drm_rect_height(clip);
183 
184 	if (!dst_pitch)
185 		dst_pitch = default_dst_pitch;
186 
187 	for (i = 0; i < format->num_planes; ++i) {
188 		unsigned int bpp_i = drm_format_info_bpp(format, i);
189 		unsigned int cpp_i = DIV_ROUND_UP(bpp_i, 8);
190 		size_t len_i = DIV_ROUND_UP(drm_rect_width(clip) * bpp_i, 8);
191 		unsigned int dst_pitch_i = dst_pitch[i];
192 		struct iosys_map dst_i = dst[i];
193 		struct iosys_map src_i = src[i];
194 
195 		if (!dst_pitch_i)
196 			dst_pitch_i = len_i;
197 
198 		iosys_map_incr(&src_i, clip_offset(clip, fb->pitches[i], cpp_i));
199 		for (y = 0; y < lines; y++) {
200 			/* TODO: handle src_i in I/O memory here */
201 			iosys_map_memcpy_to(&dst_i, 0, src_i.vaddr, len_i);
202 			iosys_map_incr(&src_i, fb->pitches[i]);
203 			iosys_map_incr(&dst_i, dst_pitch_i);
204 		}
205 	}
206 }
207 EXPORT_SYMBOL(drm_fb_memcpy);
208 
209 static void drm_fb_swab16_line(void *dbuf, const void *sbuf, unsigned int pixels)
210 {
211 	u16 *dbuf16 = dbuf;
212 	const u16 *sbuf16 = sbuf;
213 	const u16 *send16 = sbuf16 + pixels;
214 
215 	while (sbuf16 < send16)
216 		*dbuf16++ = swab16(*sbuf16++);
217 }
218 
219 static void drm_fb_swab32_line(void *dbuf, const void *sbuf, unsigned int pixels)
220 {
221 	u32 *dbuf32 = dbuf;
222 	const u32 *sbuf32 = sbuf;
223 	const u32 *send32 = sbuf32 + pixels;
224 
225 	while (sbuf32 < send32)
226 		*dbuf32++ = swab32(*sbuf32++);
227 }
228 
229 /**
230  * drm_fb_swab - Swap bytes into clip buffer
231  * @dst: Array of destination buffers
232  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
233  *             within @dst; can be NULL if scanlines are stored next to each other.
234  * @src: Array of source buffers
235  * @fb: DRM framebuffer
236  * @clip: Clip rectangle area to copy
237  * @cached: Source buffer is mapped cached (eg. not write-combined)
238  *
239  * This function copies parts of a framebuffer to display memory and swaps per-pixel
240  * bytes during the process. Destination and framebuffer formats must match. The
241  * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
242  * least as many entries as there are planes in @fb's format. Each entry stores the
243  * value for the format's respective color plane at the same index. If @cached is
244  * false a temporary buffer is used to cache one pixel line at a time to speed up
245  * slow uncached reads.
246  *
247  * This function does not apply clipping on @dst (i.e. the destination is at the
248  * top-left corner).
249  */
250 void drm_fb_swab(struct iosys_map *dst, const unsigned int *dst_pitch,
251 		 const struct iosys_map *src, const struct drm_framebuffer *fb,
252 		 const struct drm_rect *clip, bool cached)
253 {
254 	const struct drm_format_info *format = fb->format;
255 	u8 cpp = DIV_ROUND_UP(drm_format_info_bpp(format, 0), 8);
256 	void (*swab_line)(void *dbuf, const void *sbuf, unsigned int npixels);
257 
258 	switch (cpp) {
259 	case 4:
260 		swab_line = drm_fb_swab32_line;
261 		break;
262 	case 2:
263 		swab_line = drm_fb_swab16_line;
264 		break;
265 	default:
266 		drm_warn_once(fb->dev, "Format %p4cc has unsupported pixel size.\n",
267 			      &format->format);
268 		return;
269 	}
270 
271 	drm_fb_xfrm(dst, dst_pitch, &cpp, src, fb, clip, cached, swab_line);
272 }
273 EXPORT_SYMBOL(drm_fb_swab);
274 
275 static void drm_fb_xrgb8888_to_rgb332_line(void *dbuf, const void *sbuf, unsigned int pixels)
276 {
277 	u8 *dbuf8 = dbuf;
278 	const __le32 *sbuf32 = sbuf;
279 	unsigned int x;
280 	u32 pix;
281 
282 	for (x = 0; x < pixels; x++) {
283 		pix = le32_to_cpu(sbuf32[x]);
284 		dbuf8[x] = ((pix & 0x00e00000) >> 16) |
285 			   ((pix & 0x0000e000) >> 11) |
286 			   ((pix & 0x000000c0) >> 6);
287 	}
288 }
289 
290 /**
291  * drm_fb_xrgb8888_to_rgb332 - Convert XRGB8888 to RGB332 clip buffer
292  * @dst: Array of RGB332 destination buffers
293  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
294  *             within @dst; can be NULL if scanlines are stored next to each other.
295  * @src: Array of XRGB8888 source buffers
296  * @fb: DRM framebuffer
297  * @clip: Clip rectangle area to copy
298  *
299  * This function copies parts of a framebuffer to display memory and converts the
300  * color format during the process. Destination and framebuffer formats must match. The
301  * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
302  * least as many entries as there are planes in @fb's format. Each entry stores the
303  * value for the format's respective color plane at the same index.
304  *
305  * This function does not apply clipping on @dst (i.e. the destination is at the
306  * top-left corner).
307  *
308  * Drivers can use this function for RGB332 devices that don't support XRGB8888 natively.
309  */
310 void drm_fb_xrgb8888_to_rgb332(struct iosys_map *dst, const unsigned int *dst_pitch,
311 			       const struct iosys_map *src, const struct drm_framebuffer *fb,
312 			       const struct drm_rect *clip)
313 {
314 	static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
315 		1,
316 	};
317 
318 	drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
319 		    drm_fb_xrgb8888_to_rgb332_line);
320 }
321 EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb332);
322 
323 static void drm_fb_xrgb8888_to_rgb565_line(void *dbuf, const void *sbuf, unsigned int pixels)
324 {
325 	__le16 *dbuf16 = dbuf;
326 	const __le32 *sbuf32 = sbuf;
327 	unsigned int x;
328 	u16 val16;
329 	u32 pix;
330 
331 	for (x = 0; x < pixels; x++) {
332 		pix = le32_to_cpu(sbuf32[x]);
333 		val16 = ((pix & 0x00F80000) >> 8) |
334 			((pix & 0x0000FC00) >> 5) |
335 			((pix & 0x000000F8) >> 3);
336 		dbuf16[x] = cpu_to_le16(val16);
337 	}
338 }
339 
340 /* TODO: implement this helper as conversion to RGB565|BIG_ENDIAN */
341 static void drm_fb_xrgb8888_to_rgb565_swab_line(void *dbuf, const void *sbuf,
342 						unsigned int pixels)
343 {
344 	__le16 *dbuf16 = dbuf;
345 	const __le32 *sbuf32 = sbuf;
346 	unsigned int x;
347 	u16 val16;
348 	u32 pix;
349 
350 	for (x = 0; x < pixels; x++) {
351 		pix = le32_to_cpu(sbuf32[x]);
352 		val16 = ((pix & 0x00F80000) >> 8) |
353 			((pix & 0x0000FC00) >> 5) |
354 			((pix & 0x000000F8) >> 3);
355 		dbuf16[x] = cpu_to_le16(swab16(val16));
356 	}
357 }
358 
359 /**
360  * drm_fb_xrgb8888_to_rgb565 - Convert XRGB8888 to RGB565 clip buffer
361  * @dst: Array of RGB565 destination buffers
362  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
363  *             within @dst; can be NULL if scanlines are stored next to each other.
364  * @src: Array of XRGB8888 source buffer
365  * @fb: DRM framebuffer
366  * @clip: Clip rectangle area to copy
367  * @swab: Swap bytes
368  *
369  * This function copies parts of a framebuffer to display memory and converts the
370  * color format during the process. Destination and framebuffer formats must match. The
371  * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
372  * least as many entries as there are planes in @fb's format. Each entry stores the
373  * value for the format's respective color plane at the same index.
374  *
375  * This function does not apply clipping on @dst (i.e. the destination is at the
376  * top-left corner).
377  *
378  * Drivers can use this function for RGB565 devices that don't support XRGB8888 natively.
379  */
380 void drm_fb_xrgb8888_to_rgb565(struct iosys_map *dst, const unsigned int *dst_pitch,
381 			       const struct iosys_map *src, const struct drm_framebuffer *fb,
382 			       const struct drm_rect *clip, bool swab)
383 {
384 	static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
385 		2,
386 	};
387 
388 	void (*xfrm_line)(void *dbuf, const void *sbuf, unsigned int npixels);
389 
390 	if (swab)
391 		xfrm_line = drm_fb_xrgb8888_to_rgb565_swab_line;
392 	else
393 		xfrm_line = drm_fb_xrgb8888_to_rgb565_line;
394 
395 	drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false, xfrm_line);
396 }
397 EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb565);
398 
399 static void drm_fb_xrgb8888_to_xrgb1555_line(void *dbuf, const void *sbuf, unsigned int pixels)
400 {
401 	__le16 *dbuf16 = dbuf;
402 	const __le32 *sbuf32 = sbuf;
403 	unsigned int x;
404 	u16 val16;
405 	u32 pix;
406 
407 	for (x = 0; x < pixels; x++) {
408 		pix = le32_to_cpu(sbuf32[x]);
409 		val16 = ((pix & 0x00f80000) >> 9) |
410 			((pix & 0x0000f800) >> 6) |
411 			((pix & 0x000000f8) >> 3);
412 		dbuf16[x] = cpu_to_le16(val16);
413 	}
414 }
415 
416 /**
417  * drm_fb_xrgb8888_to_xrgb1555 - Convert XRGB8888 to XRGB1555 clip buffer
418  * @dst: Array of XRGB1555 destination buffers
419  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
420  *             within @dst; can be NULL if scanlines are stored next to each other.
421  * @src: Array of XRGB8888 source buffer
422  * @fb: DRM framebuffer
423  * @clip: Clip rectangle area to copy
424  *
425  * This function copies parts of a framebuffer to display memory and converts
426  * the color format during the process. The parameters @dst, @dst_pitch and
427  * @src refer to arrays. Each array must have at least as many entries as
428  * there are planes in @fb's format. Each entry stores the value for the
429  * format's respective color plane at the same index.
430  *
431  * This function does not apply clipping on @dst (i.e. the destination is at the
432  * top-left corner).
433  *
434  * Drivers can use this function for XRGB1555 devices that don't support
435  * XRGB8888 natively.
436  */
437 void drm_fb_xrgb8888_to_xrgb1555(struct iosys_map *dst, const unsigned int *dst_pitch,
438 				 const struct iosys_map *src, const struct drm_framebuffer *fb,
439 				 const struct drm_rect *clip)
440 {
441 	static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
442 		2,
443 	};
444 
445 	drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
446 		    drm_fb_xrgb8888_to_xrgb1555_line);
447 }
448 EXPORT_SYMBOL(drm_fb_xrgb8888_to_xrgb1555);
449 
450 static void drm_fb_xrgb8888_to_argb1555_line(void *dbuf, const void *sbuf, unsigned int pixels)
451 {
452 	__le16 *dbuf16 = dbuf;
453 	const __le32 *sbuf32 = sbuf;
454 	unsigned int x;
455 	u16 val16;
456 	u32 pix;
457 
458 	for (x = 0; x < pixels; x++) {
459 		pix = le32_to_cpu(sbuf32[x]);
460 		val16 = BIT(15) | /* set alpha bit */
461 			((pix & 0x00f80000) >> 9) |
462 			((pix & 0x0000f800) >> 6) |
463 			((pix & 0x000000f8) >> 3);
464 		dbuf16[x] = cpu_to_le16(val16);
465 	}
466 }
467 
468 /**
469  * drm_fb_xrgb8888_to_argb1555 - Convert XRGB8888 to ARGB1555 clip buffer
470  * @dst: Array of ARGB1555 destination buffers
471  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
472  *             within @dst; can be NULL if scanlines are stored next to each other.
473  * @src: Array of XRGB8888 source buffer
474  * @fb: DRM framebuffer
475  * @clip: Clip rectangle area to copy
476  *
477  * This function copies parts of a framebuffer to display memory and converts
478  * the color format during the process. The parameters @dst, @dst_pitch and
479  * @src refer to arrays. Each array must have at least as many entries as
480  * there are planes in @fb's format. Each entry stores the value for the
481  * format's respective color plane at the same index.
482  *
483  * This function does not apply clipping on @dst (i.e. the destination is at the
484  * top-left corner).
485  *
486  * Drivers can use this function for ARGB1555 devices that don't support
487  * XRGB8888 natively. It sets an opaque alpha channel as part of the conversion.
488  */
489 void drm_fb_xrgb8888_to_argb1555(struct iosys_map *dst, const unsigned int *dst_pitch,
490 				 const struct iosys_map *src, const struct drm_framebuffer *fb,
491 				 const struct drm_rect *clip)
492 {
493 	static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
494 		2,
495 	};
496 
497 	drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
498 		    drm_fb_xrgb8888_to_argb1555_line);
499 }
500 EXPORT_SYMBOL(drm_fb_xrgb8888_to_argb1555);
501 
502 static void drm_fb_xrgb8888_to_rgba5551_line(void *dbuf, const void *sbuf, unsigned int pixels)
503 {
504 	__le16 *dbuf16 = dbuf;
505 	const __le32 *sbuf32 = sbuf;
506 	unsigned int x;
507 	u16 val16;
508 	u32 pix;
509 
510 	for (x = 0; x < pixels; x++) {
511 		pix = le32_to_cpu(sbuf32[x]);
512 		val16 = ((pix & 0x00f80000) >> 8) |
513 			((pix & 0x0000f800) >> 5) |
514 			((pix & 0x000000f8) >> 2) |
515 			BIT(0); /* set alpha bit */
516 		dbuf16[x] = cpu_to_le16(val16);
517 	}
518 }
519 
520 /**
521  * drm_fb_xrgb8888_to_rgba5551 - Convert XRGB8888 to RGBA5551 clip buffer
522  * @dst: Array of RGBA5551 destination buffers
523  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
524  *             within @dst; can be NULL if scanlines are stored next to each other.
525  * @src: Array of XRGB8888 source buffer
526  * @fb: DRM framebuffer
527  * @clip: Clip rectangle area to copy
528  *
529  * This function copies parts of a framebuffer to display memory and converts
530  * the color format during the process. The parameters @dst, @dst_pitch and
531  * @src refer to arrays. Each array must have at least as many entries as
532  * there are planes in @fb's format. Each entry stores the value for the
533  * format's respective color plane at the same index.
534  *
535  * This function does not apply clipping on @dst (i.e. the destination is at the
536  * top-left corner).
537  *
538  * Drivers can use this function for RGBA5551 devices that don't support
539  * XRGB8888 natively. It sets an opaque alpha channel as part of the conversion.
540  */
541 void drm_fb_xrgb8888_to_rgba5551(struct iosys_map *dst, const unsigned int *dst_pitch,
542 				 const struct iosys_map *src, const struct drm_framebuffer *fb,
543 				 const struct drm_rect *clip)
544 {
545 	static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
546 		2,
547 	};
548 
549 	drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
550 		    drm_fb_xrgb8888_to_rgba5551_line);
551 }
552 EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgba5551);
553 
554 static void drm_fb_xrgb8888_to_rgb888_line(void *dbuf, const void *sbuf, unsigned int pixels)
555 {
556 	u8 *dbuf8 = dbuf;
557 	const __le32 *sbuf32 = sbuf;
558 	unsigned int x;
559 	u32 pix;
560 
561 	for (x = 0; x < pixels; x++) {
562 		pix = le32_to_cpu(sbuf32[x]);
563 		/* write blue-green-red to output in little endianness */
564 		*dbuf8++ = (pix & 0x000000FF) >>  0;
565 		*dbuf8++ = (pix & 0x0000FF00) >>  8;
566 		*dbuf8++ = (pix & 0x00FF0000) >> 16;
567 	}
568 }
569 
570 /**
571  * drm_fb_xrgb8888_to_rgb888 - Convert XRGB8888 to RGB888 clip buffer
572  * @dst: Array of RGB888 destination buffers
573  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
574  *             within @dst; can be NULL if scanlines are stored next to each other.
575  * @src: Array of XRGB8888 source buffers
576  * @fb: DRM framebuffer
577  * @clip: Clip rectangle area to copy
578  *
579  * This function copies parts of a framebuffer to display memory and converts the
580  * color format during the process. Destination and framebuffer formats must match. The
581  * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
582  * least as many entries as there are planes in @fb's format. Each entry stores the
583  * value for the format's respective color plane at the same index.
584  *
585  * This function does not apply clipping on @dst (i.e. the destination is at the
586  * top-left corner).
587  *
588  * Drivers can use this function for RGB888 devices that don't natively
589  * support XRGB8888.
590  */
591 void drm_fb_xrgb8888_to_rgb888(struct iosys_map *dst, const unsigned int *dst_pitch,
592 			       const struct iosys_map *src, const struct drm_framebuffer *fb,
593 			       const struct drm_rect *clip)
594 {
595 	static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
596 		3,
597 	};
598 
599 	drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
600 		    drm_fb_xrgb8888_to_rgb888_line);
601 }
602 EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb888);
603 
604 static void drm_fb_xrgb8888_to_argb8888_line(void *dbuf, const void *sbuf, unsigned int pixels)
605 {
606 	__le32 *dbuf32 = dbuf;
607 	const __le32 *sbuf32 = sbuf;
608 	unsigned int x;
609 	u32 pix;
610 
611 	for (x = 0; x < pixels; x++) {
612 		pix = le32_to_cpu(sbuf32[x]);
613 		pix |= GENMASK(31, 24); /* fill alpha bits */
614 		dbuf32[x] = cpu_to_le32(pix);
615 	}
616 }
617 
618 /**
619  * drm_fb_xrgb8888_to_argb8888 - Convert XRGB8888 to ARGB8888 clip buffer
620  * @dst: Array of ARGB8888 destination buffers
621  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
622  *             within @dst; can be NULL if scanlines are stored next to each other.
623  * @src: Array of XRGB8888 source buffer
624  * @fb: DRM framebuffer
625  * @clip: Clip rectangle area to copy
626  *
627  * This function copies parts of a framebuffer to display memory and converts the
628  * color format during the process. The parameters @dst, @dst_pitch and @src refer
629  * to arrays. Each array must have at least as many entries as there are planes in
630  * @fb's format. Each entry stores the value for the format's respective color plane
631  * at the same index.
632  *
633  * This function does not apply clipping on @dst (i.e. the destination is at the
634  * top-left corner).
635  *
636  * Drivers can use this function for ARGB8888 devices that don't support XRGB8888
637  * natively. It sets an opaque alpha channel as part of the conversion.
638  */
639 void drm_fb_xrgb8888_to_argb8888(struct iosys_map *dst, const unsigned int *dst_pitch,
640 				 const struct iosys_map *src, const struct drm_framebuffer *fb,
641 				 const struct drm_rect *clip)
642 {
643 	static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
644 		4,
645 	};
646 
647 	drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
648 		    drm_fb_xrgb8888_to_argb8888_line);
649 }
650 EXPORT_SYMBOL(drm_fb_xrgb8888_to_argb8888);
651 
652 static void drm_fb_xrgb8888_to_xrgb2101010_line(void *dbuf, const void *sbuf, unsigned int pixels)
653 {
654 	__le32 *dbuf32 = dbuf;
655 	const __le32 *sbuf32 = sbuf;
656 	unsigned int x;
657 	u32 val32;
658 	u32 pix;
659 
660 	for (x = 0; x < pixels; x++) {
661 		pix = le32_to_cpu(sbuf32[x]);
662 		val32 = ((pix & 0x000000FF) << 2) |
663 			((pix & 0x0000FF00) << 4) |
664 			((pix & 0x00FF0000) << 6);
665 		pix = val32 | ((val32 >> 8) & 0x00300C03);
666 		*dbuf32++ = cpu_to_le32(pix);
667 	}
668 }
669 
670 /**
671  * drm_fb_xrgb8888_to_xrgb2101010 - Convert XRGB8888 to XRGB2101010 clip buffer
672  * @dst: Array of XRGB2101010 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  *
679  * This function copies parts of a framebuffer to display memory and converts the
680  * color format during the process. Destination and framebuffer formats must match. The
681  * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
682  * least as many entries as there are planes in @fb's format. Each entry stores the
683  * value for the format's respective color plane at the same index.
684  *
685  * This function does not apply clipping on @dst (i.e. the destination is at the
686  * top-left corner).
687  *
688  * Drivers can use this function for XRGB2101010 devices that don't support XRGB8888
689  * natively.
690  */
691 void drm_fb_xrgb8888_to_xrgb2101010(struct iosys_map *dst, const unsigned int *dst_pitch,
692 				    const struct iosys_map *src, const struct drm_framebuffer *fb,
693 				    const struct drm_rect *clip)
694 {
695 	static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
696 		4,
697 	};
698 
699 	drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
700 		    drm_fb_xrgb8888_to_xrgb2101010_line);
701 }
702 EXPORT_SYMBOL(drm_fb_xrgb8888_to_xrgb2101010);
703 
704 static void drm_fb_xrgb8888_to_argb2101010_line(void *dbuf, const void *sbuf, unsigned int pixels)
705 {
706 	__le32 *dbuf32 = dbuf;
707 	const __le32 *sbuf32 = sbuf;
708 	unsigned int x;
709 	u32 val32;
710 	u32 pix;
711 
712 	for (x = 0; x < pixels; x++) {
713 		pix = le32_to_cpu(sbuf32[x]);
714 		val32 = ((pix & 0x000000ff) << 2) |
715 			((pix & 0x0000ff00) << 4) |
716 			((pix & 0x00ff0000) << 6);
717 		pix = GENMASK(31, 30) | /* set alpha bits */
718 		      val32 | ((val32 >> 8) & 0x00300c03);
719 		*dbuf32++ = cpu_to_le32(pix);
720 	}
721 }
722 
723 /**
724  * drm_fb_xrgb8888_to_argb2101010 - Convert XRGB8888 to ARGB2101010 clip buffer
725  * @dst: Array of ARGB2101010 destination buffers
726  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
727  *             within @dst; can be NULL if scanlines are stored next to each other.
728  * @src: Array of XRGB8888 source buffers
729  * @fb: DRM framebuffer
730  * @clip: Clip rectangle area to copy
731  *
732  * This function copies parts of a framebuffer to display memory and converts
733  * the color format during the process. The parameters @dst, @dst_pitch and
734  * @src refer to arrays. Each array must have at least as many entries as
735  * there are planes in @fb's format. Each entry stores the value for the
736  * format's respective color plane at the same index.
737  *
738  * This function does not apply clipping on @dst (i.e. the destination is at the
739  * top-left corner).
740  *
741  * Drivers can use this function for ARGB2101010 devices that don't support XRGB8888
742  * natively.
743  */
744 void drm_fb_xrgb8888_to_argb2101010(struct iosys_map *dst, const unsigned int *dst_pitch,
745 				    const struct iosys_map *src, const struct drm_framebuffer *fb,
746 				    const struct drm_rect *clip)
747 {
748 	static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
749 		4,
750 	};
751 
752 	drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
753 		    drm_fb_xrgb8888_to_argb2101010_line);
754 }
755 EXPORT_SYMBOL(drm_fb_xrgb8888_to_argb2101010);
756 
757 static void drm_fb_xrgb8888_to_gray8_line(void *dbuf, const void *sbuf, unsigned int pixels)
758 {
759 	u8 *dbuf8 = dbuf;
760 	const __le32 *sbuf32 = sbuf;
761 	unsigned int x;
762 
763 	for (x = 0; x < pixels; x++) {
764 		u32 pix = le32_to_cpu(sbuf32[x]);
765 		u8 r = (pix & 0x00ff0000) >> 16;
766 		u8 g = (pix & 0x0000ff00) >> 8;
767 		u8 b =  pix & 0x000000ff;
768 
769 		/* ITU BT.601: Y = 0.299 R + 0.587 G + 0.114 B */
770 		*dbuf8++ = (3 * r + 6 * g + b) / 10;
771 	}
772 }
773 
774 /**
775  * drm_fb_xrgb8888_to_gray8 - Convert XRGB8888 to grayscale
776  * @dst: Array of 8-bit grayscale destination buffers
777  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
778  *             within @dst; can be NULL if scanlines are stored next to each other.
779  * @src: Array of XRGB8888 source buffers
780  * @fb: DRM framebuffer
781  * @clip: Clip rectangle area to copy
782  *
783  * This function copies parts of a framebuffer to display memory and converts the
784  * color format during the process. Destination and framebuffer formats must match. The
785  * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
786  * least as many entries as there are planes in @fb's format. Each entry stores the
787  * value for the format's respective color plane at the same index.
788  *
789  * This function does not apply clipping on @dst (i.e. the destination is at the
790  * top-left corner).
791  *
792  * DRM doesn't have native monochrome or grayscale support. Drivers can use this
793  * function for grayscale devices that don't support XRGB8888 natively.Such
794  * drivers can announce the commonly supported XR24 format to userspace and use
795  * this function to convert to the native format. Monochrome drivers will use the
796  * most significant bit, where 1 means foreground color and 0 background color.
797  * ITU BT.601 is being used for the RGB -> luma (brightness) conversion.
798  */
799 void drm_fb_xrgb8888_to_gray8(struct iosys_map *dst, const unsigned int *dst_pitch,
800 			      const struct iosys_map *src, const struct drm_framebuffer *fb,
801 			      const struct drm_rect *clip)
802 {
803 	static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
804 		1,
805 	};
806 
807 	drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
808 		    drm_fb_xrgb8888_to_gray8_line);
809 }
810 EXPORT_SYMBOL(drm_fb_xrgb8888_to_gray8);
811 
812 /**
813  * drm_fb_blit - Copy parts of a framebuffer to display memory
814  * @dst:	Array of display-memory addresses to copy to
815  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
816  *             within @dst; can be NULL if scanlines are stored next to each other.
817  * @dst_format:	FOURCC code of the display's color format
818  * @src:	The framebuffer memory to copy from
819  * @fb:		The framebuffer to copy from
820  * @clip:	Clip rectangle area to copy
821  *
822  * This function copies parts of a framebuffer to display memory. If the
823  * formats of the display and the framebuffer mismatch, the blit function
824  * will attempt to convert between them during the process. The parameters @dst,
825  * @dst_pitch and @src refer to arrays. Each array must have at least as many
826  * entries as there are planes in @dst_format's format. Each entry stores the
827  * value for the format's respective color plane at the same index.
828  *
829  * This function does not apply clipping on @dst (i.e. the destination is at the
830  * top-left corner).
831  *
832  * Returns:
833  * 0 on success, or
834  * -EINVAL if the color-format conversion failed, or
835  * a negative error code otherwise.
836  */
837 int drm_fb_blit(struct iosys_map *dst, const unsigned int *dst_pitch, uint32_t dst_format,
838 		const struct iosys_map *src, const struct drm_framebuffer *fb,
839 		const struct drm_rect *clip)
840 {
841 	uint32_t fb_format = fb->format->format;
842 
843 	if (fb_format == dst_format) {
844 		drm_fb_memcpy(dst, dst_pitch, src, fb, clip);
845 		return 0;
846 	} else if (fb_format == (dst_format | DRM_FORMAT_BIG_ENDIAN)) {
847 		drm_fb_swab(dst, dst_pitch, src, fb, clip, false);
848 		return 0;
849 	} else if (fb_format == (dst_format & ~DRM_FORMAT_BIG_ENDIAN)) {
850 		drm_fb_swab(dst, dst_pitch, src, fb, clip, false);
851 		return 0;
852 	} else if (fb_format == DRM_FORMAT_XRGB8888) {
853 		if (dst_format == DRM_FORMAT_RGB565) {
854 			drm_fb_xrgb8888_to_rgb565(dst, dst_pitch, src, fb, clip, false);
855 			return 0;
856 		} else if (dst_format == DRM_FORMAT_XRGB1555) {
857 			drm_fb_xrgb8888_to_xrgb1555(dst, dst_pitch, src, fb, clip);
858 			return 0;
859 		} else if (dst_format == DRM_FORMAT_ARGB1555) {
860 			drm_fb_xrgb8888_to_argb1555(dst, dst_pitch, src, fb, clip);
861 			return 0;
862 		} else if (dst_format == DRM_FORMAT_RGBA5551) {
863 			drm_fb_xrgb8888_to_rgba5551(dst, dst_pitch, src, fb, clip);
864 			return 0;
865 		} else if (dst_format == DRM_FORMAT_RGB888) {
866 			drm_fb_xrgb8888_to_rgb888(dst, dst_pitch, src, fb, clip);
867 			return 0;
868 		} else if (dst_format == DRM_FORMAT_ARGB8888) {
869 			drm_fb_xrgb8888_to_argb8888(dst, dst_pitch, src, fb, clip);
870 			return 0;
871 		} else if (dst_format == DRM_FORMAT_XRGB2101010) {
872 			drm_fb_xrgb8888_to_xrgb2101010(dst, dst_pitch, src, fb, clip);
873 			return 0;
874 		} else if (dst_format == DRM_FORMAT_ARGB2101010) {
875 			drm_fb_xrgb8888_to_argb2101010(dst, dst_pitch, src, fb, clip);
876 			return 0;
877 		} else if (dst_format == DRM_FORMAT_BGRX8888) {
878 			drm_fb_swab(dst, dst_pitch, src, fb, clip, false);
879 			return 0;
880 		}
881 	}
882 
883 	drm_warn_once(fb->dev, "No conversion helper from %p4cc to %p4cc found.\n",
884 		      &fb_format, &dst_format);
885 
886 	return -EINVAL;
887 }
888 EXPORT_SYMBOL(drm_fb_blit);
889 
890 static void drm_fb_gray8_to_mono_line(void *dbuf, const void *sbuf, unsigned int pixels)
891 {
892 	u8 *dbuf8 = dbuf;
893 	const u8 *sbuf8 = sbuf;
894 
895 	while (pixels) {
896 		unsigned int i, bits = min(pixels, 8U);
897 		u8 byte = 0;
898 
899 		for (i = 0; i < bits; i++, pixels--) {
900 			if (*sbuf8++ >= 128)
901 				byte |= BIT(i);
902 		}
903 		*dbuf8++ = byte;
904 	}
905 }
906 
907 /**
908  * drm_fb_xrgb8888_to_mono - Convert XRGB8888 to monochrome
909  * @dst: Array of monochrome destination buffers (0=black, 1=white)
910  * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
911  *             within @dst; can be NULL if scanlines are stored next to each other.
912  * @src: Array of XRGB8888 source buffers
913  * @fb: DRM framebuffer
914  * @clip: Clip rectangle area to copy
915  *
916  * This function copies parts of a framebuffer to display memory and converts the
917  * color format during the process. Destination and framebuffer formats must match. The
918  * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
919  * least as many entries as there are planes in @fb's format. Each entry stores the
920  * value for the format's respective color plane at the same index.
921  *
922  * This function does not apply clipping on @dst (i.e. the destination is at the
923  * top-left corner). The first pixel (upper left corner of the clip rectangle) will
924  * be converted and copied to the first bit (LSB) in the first byte of the monochrome
925  * destination buffer. If the caller requires that the first pixel in a byte must
926  * be located at an x-coordinate that is a multiple of 8, then the caller must take
927  * care itself of supplying a suitable clip rectangle.
928  *
929  * DRM doesn't have native monochrome support. Drivers can use this function for
930  * monochrome devices that don't support XRGB8888 natively. Such drivers can
931  * announce the commonly supported XR24 format to userspace and use this function
932  * to convert to the native format.
933  *
934  * This function uses drm_fb_xrgb8888_to_gray8() to convert to grayscale and
935  * then the result is converted from grayscale to monochrome.
936  */
937 void drm_fb_xrgb8888_to_mono(struct iosys_map *dst, const unsigned int *dst_pitch,
938 			     const struct iosys_map *src, const struct drm_framebuffer *fb,
939 			     const struct drm_rect *clip)
940 {
941 	static const unsigned int default_dst_pitch[DRM_FORMAT_MAX_PLANES] = {
942 		0, 0, 0, 0
943 	};
944 	unsigned int linepixels = drm_rect_width(clip);
945 	unsigned int lines = drm_rect_height(clip);
946 	unsigned int cpp = fb->format->cpp[0];
947 	unsigned int len_src32 = linepixels * cpp;
948 	struct drm_device *dev = fb->dev;
949 	void *vaddr = src[0].vaddr;
950 	unsigned int dst_pitch_0;
951 	unsigned int y;
952 	u8 *mono = dst[0].vaddr, *gray8;
953 	u32 *src32;
954 
955 	if (drm_WARN_ON(dev, fb->format->format != DRM_FORMAT_XRGB8888))
956 		return;
957 
958 	if (!dst_pitch)
959 		dst_pitch = default_dst_pitch;
960 	dst_pitch_0 = dst_pitch[0];
961 
962 	/*
963 	 * The mono destination buffer contains 1 bit per pixel
964 	 */
965 	if (!dst_pitch_0)
966 		dst_pitch_0 = DIV_ROUND_UP(linepixels, 8);
967 
968 	/*
969 	 * The dma memory is write-combined so reads are uncached.
970 	 * Speed up by fetching one line at a time.
971 	 *
972 	 * Also, format conversion from XR24 to monochrome are done
973 	 * line-by-line but are converted to 8-bit grayscale as an
974 	 * intermediate step.
975 	 *
976 	 * Allocate a buffer to be used for both copying from the cma
977 	 * memory and to store the intermediate grayscale line pixels.
978 	 */
979 	src32 = kmalloc(len_src32 + linepixels, GFP_KERNEL);
980 	if (!src32)
981 		return;
982 
983 	gray8 = (u8 *)src32 + len_src32;
984 
985 	vaddr += clip_offset(clip, fb->pitches[0], cpp);
986 	for (y = 0; y < lines; y++) {
987 		src32 = memcpy(src32, vaddr, len_src32);
988 		drm_fb_xrgb8888_to_gray8_line(gray8, src32, linepixels);
989 		drm_fb_gray8_to_mono_line(mono, gray8, linepixels);
990 		vaddr += fb->pitches[0];
991 		mono += dst_pitch_0;
992 	}
993 
994 	kfree(src32);
995 }
996 EXPORT_SYMBOL(drm_fb_xrgb8888_to_mono);
997 
998 static uint32_t drm_fb_nonalpha_fourcc(uint32_t fourcc)
999 {
1000 	/* only handle formats with depth != 0 and alpha channel */
1001 	switch (fourcc) {
1002 	case DRM_FORMAT_ARGB1555:
1003 		return DRM_FORMAT_XRGB1555;
1004 	case DRM_FORMAT_ABGR1555:
1005 		return DRM_FORMAT_XBGR1555;
1006 	case DRM_FORMAT_RGBA5551:
1007 		return DRM_FORMAT_RGBX5551;
1008 	case DRM_FORMAT_BGRA5551:
1009 		return DRM_FORMAT_BGRX5551;
1010 	case DRM_FORMAT_ARGB8888:
1011 		return DRM_FORMAT_XRGB8888;
1012 	case DRM_FORMAT_ABGR8888:
1013 		return DRM_FORMAT_XBGR8888;
1014 	case DRM_FORMAT_RGBA8888:
1015 		return DRM_FORMAT_RGBX8888;
1016 	case DRM_FORMAT_BGRA8888:
1017 		return DRM_FORMAT_BGRX8888;
1018 	case DRM_FORMAT_ARGB2101010:
1019 		return DRM_FORMAT_XRGB2101010;
1020 	case DRM_FORMAT_ABGR2101010:
1021 		return DRM_FORMAT_XBGR2101010;
1022 	case DRM_FORMAT_RGBA1010102:
1023 		return DRM_FORMAT_RGBX1010102;
1024 	case DRM_FORMAT_BGRA1010102:
1025 		return DRM_FORMAT_BGRX1010102;
1026 	}
1027 
1028 	return fourcc;
1029 }
1030 
1031 static bool is_listed_fourcc(const uint32_t *fourccs, size_t nfourccs, uint32_t fourcc)
1032 {
1033 	const uint32_t *fourccs_end = fourccs + nfourccs;
1034 
1035 	while (fourccs < fourccs_end) {
1036 		if (*fourccs == fourcc)
1037 			return true;
1038 		++fourccs;
1039 	}
1040 	return false;
1041 }
1042 
1043 /**
1044  * drm_fb_build_fourcc_list - Filters a list of supported color formats against
1045  *                            the device's native formats
1046  * @dev: DRM device
1047  * @native_fourccs: 4CC codes of natively supported color formats
1048  * @native_nfourccs: The number of entries in @native_fourccs
1049  * @fourccs_out: Returns 4CC codes of supported color formats
1050  * @nfourccs_out: The number of available entries in @fourccs_out
1051  *
1052  * This function create a list of supported color format from natively
1053  * supported formats and additional emulated formats.
1054  * At a minimum, most userspace programs expect at least support for
1055  * XRGB8888 on the primary plane. Devices that have to emulate the
1056  * format, and possibly others, can use drm_fb_build_fourcc_list() to
1057  * create a list of supported color formats. The returned list can
1058  * be handed over to drm_universal_plane_init() et al. Native formats
1059  * will go before emulated formats. Native formats with alpha channel
1060  * will be replaced by such without, as primary planes usually don't
1061  * support alpha. Other heuristics might be applied
1062  * to optimize the order. Formats near the beginning of the list are
1063  * usually preferred over formats near the end of the list.
1064  *
1065  * Returns:
1066  * The number of color-formats 4CC codes returned in @fourccs_out.
1067  */
1068 size_t drm_fb_build_fourcc_list(struct drm_device *dev,
1069 				const u32 *native_fourccs, size_t native_nfourccs,
1070 				u32 *fourccs_out, size_t nfourccs_out)
1071 {
1072 	/*
1073 	 * XRGB8888 is the default fallback format for most of userspace
1074 	 * and it's currently the only format that should be emulated for
1075 	 * the primary plane. Only if there's ever another default fallback,
1076 	 * it should be added here.
1077 	 */
1078 	static const uint32_t extra_fourccs[] = {
1079 		DRM_FORMAT_XRGB8888,
1080 	};
1081 	static const size_t extra_nfourccs = ARRAY_SIZE(extra_fourccs);
1082 
1083 	u32 *fourccs = fourccs_out;
1084 	const u32 *fourccs_end = fourccs_out + nfourccs_out;
1085 	size_t i;
1086 
1087 	/*
1088 	 * The device's native formats go first.
1089 	 */
1090 
1091 	for (i = 0; i < native_nfourccs; ++i) {
1092 		/*
1093 		 * Several DTs, boot loaders and firmware report native
1094 		 * alpha formats that are non-alpha formats instead. So
1095 		 * replace alpha formats by non-alpha formats.
1096 		 */
1097 		u32 fourcc = drm_fb_nonalpha_fourcc(native_fourccs[i]);
1098 
1099 		if (is_listed_fourcc(fourccs_out, fourccs - fourccs_out, fourcc)) {
1100 			continue; /* skip duplicate entries */
1101 		} else if (fourccs == fourccs_end) {
1102 			drm_warn(dev, "Ignoring native format %p4cc\n", &fourcc);
1103 			continue; /* end of available output buffer */
1104 		}
1105 
1106 		drm_dbg_kms(dev, "adding native format %p4cc\n", &fourcc);
1107 
1108 		*fourccs = fourcc;
1109 		++fourccs;
1110 	}
1111 
1112 	/*
1113 	 * The extra formats, emulated by the driver, go second.
1114 	 */
1115 
1116 	for (i = 0; (i < extra_nfourccs) && (fourccs < fourccs_end); ++i) {
1117 		u32 fourcc = extra_fourccs[i];
1118 
1119 		if (is_listed_fourcc(fourccs_out, fourccs - fourccs_out, fourcc)) {
1120 			continue; /* skip duplicate and native entries */
1121 		} else if (fourccs == fourccs_end) {
1122 			drm_warn(dev, "Ignoring emulated format %p4cc\n", &fourcc);
1123 			continue; /* end of available output buffer */
1124 		}
1125 
1126 		drm_dbg_kms(dev, "adding emulated format %p4cc\n", &fourcc);
1127 
1128 		*fourccs = fourcc;
1129 		++fourccs;
1130 	}
1131 
1132 	return fourccs - fourccs_out;
1133 }
1134 EXPORT_SYMBOL(drm_fb_build_fourcc_list);
1135