xref: /linux/drivers/gpu/drm/vmwgfx/vmwgfx_blit.c (revision 20371ba120635d9ab7fc7670497105af8f33eb08)
1dff96888SDirk Hohndel (VMware) // SPDX-License-Identifier: GPL-2.0 OR MIT
279273e1bSThomas Hellstrom /**************************************************************************
379273e1bSThomas Hellstrom  *
4dff96888SDirk Hohndel (VMware)  * Copyright 2017 VMware, Inc., Palo Alto, CA., USA
579273e1bSThomas Hellstrom  * All Rights Reserved.
679273e1bSThomas Hellstrom  *
779273e1bSThomas Hellstrom  * Permission is hereby granted, free of charge, to any person obtaining a
879273e1bSThomas Hellstrom  * copy of this software and associated documentation files (the
979273e1bSThomas Hellstrom  * "Software"), to deal in the Software without restriction, including
1079273e1bSThomas Hellstrom  * without limitation the rights to use, copy, modify, merge, publish,
1179273e1bSThomas Hellstrom  * distribute, sub license, and/or sell copies of the Software, and to
1279273e1bSThomas Hellstrom  * permit persons to whom the Software is furnished to do so, subject to
1379273e1bSThomas Hellstrom  * the following conditions:
1479273e1bSThomas Hellstrom  *
1579273e1bSThomas Hellstrom  * The above copyright notice and this permission notice (including the
1679273e1bSThomas Hellstrom  * next paragraph) shall be included in all copies or substantial portions
1779273e1bSThomas Hellstrom  * of the Software.
1879273e1bSThomas Hellstrom  *
1979273e1bSThomas Hellstrom  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2079273e1bSThomas Hellstrom  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2179273e1bSThomas Hellstrom  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
2279273e1bSThomas Hellstrom  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
2379273e1bSThomas Hellstrom  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
2479273e1bSThomas Hellstrom  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2579273e1bSThomas Hellstrom  * USE OR OTHER DEALINGS IN THE SOFTWARE.
2679273e1bSThomas Hellstrom  *
2779273e1bSThomas Hellstrom  **************************************************************************/
2879273e1bSThomas Hellstrom 
2979273e1bSThomas Hellstrom #include "vmwgfx_drv.h"
30*50f11992SZack Rusin 
31*50f11992SZack Rusin #include "vmwgfx_bo.h"
32915ecc22SIra Weiny #include <linux/highmem.h>
3379273e1bSThomas Hellstrom 
3479273e1bSThomas Hellstrom /*
3579273e1bSThomas Hellstrom  * Template that implements find_first_diff() for a generic
3679273e1bSThomas Hellstrom  * unsigned integer type. @size and return value are in bytes.
3779273e1bSThomas Hellstrom  */
3879273e1bSThomas Hellstrom #define VMW_FIND_FIRST_DIFF(_type)			 \
3979273e1bSThomas Hellstrom static size_t vmw_find_first_diff_ ## _type		 \
4079273e1bSThomas Hellstrom 	(const _type * dst, const _type * src, size_t size)\
4179273e1bSThomas Hellstrom {							 \
4279273e1bSThomas Hellstrom 	size_t i;					 \
4379273e1bSThomas Hellstrom 							 \
4479273e1bSThomas Hellstrom 	for (i = 0; i < size; i += sizeof(_type)) {	 \
4579273e1bSThomas Hellstrom 		if (*dst++ != *src++)			 \
4679273e1bSThomas Hellstrom 			break;				 \
4779273e1bSThomas Hellstrom 	}						 \
4879273e1bSThomas Hellstrom 							 \
4979273e1bSThomas Hellstrom 	return i;					 \
5079273e1bSThomas Hellstrom }
5179273e1bSThomas Hellstrom 
5279273e1bSThomas Hellstrom 
5379273e1bSThomas Hellstrom /*
5479273e1bSThomas Hellstrom  * Template that implements find_last_diff() for a generic
5579273e1bSThomas Hellstrom  * unsigned integer type. Pointers point to the item following the
5679273e1bSThomas Hellstrom  * *end* of the area to be examined. @size and return value are in
5779273e1bSThomas Hellstrom  * bytes.
5879273e1bSThomas Hellstrom  */
5979273e1bSThomas Hellstrom #define VMW_FIND_LAST_DIFF(_type)					\
6079273e1bSThomas Hellstrom static ssize_t vmw_find_last_diff_ ## _type(				\
6179273e1bSThomas Hellstrom 	const _type * dst, const _type * src, size_t size)		\
6279273e1bSThomas Hellstrom {									\
6379273e1bSThomas Hellstrom 	while (size) {							\
6479273e1bSThomas Hellstrom 		if (*--dst != *--src)					\
6579273e1bSThomas Hellstrom 			break;						\
6679273e1bSThomas Hellstrom 									\
6779273e1bSThomas Hellstrom 		size -= sizeof(_type);					\
6879273e1bSThomas Hellstrom 	}								\
6979273e1bSThomas Hellstrom 	return size;							\
7079273e1bSThomas Hellstrom }
7179273e1bSThomas Hellstrom 
7279273e1bSThomas Hellstrom 
7379273e1bSThomas Hellstrom /*
7479273e1bSThomas Hellstrom  * Instantiate find diff functions for relevant unsigned integer sizes,
7579273e1bSThomas Hellstrom  * assuming that wider integers are faster (including aligning) up to the
7679273e1bSThomas Hellstrom  * architecture native width, which is assumed to be 32 bit unless
7779273e1bSThomas Hellstrom  * CONFIG_64BIT is defined.
7879273e1bSThomas Hellstrom  */
7979273e1bSThomas Hellstrom VMW_FIND_FIRST_DIFF(u8);
8079273e1bSThomas Hellstrom VMW_FIND_LAST_DIFF(u8);
8179273e1bSThomas Hellstrom 
8279273e1bSThomas Hellstrom VMW_FIND_FIRST_DIFF(u16);
8379273e1bSThomas Hellstrom VMW_FIND_LAST_DIFF(u16);
8479273e1bSThomas Hellstrom 
8579273e1bSThomas Hellstrom VMW_FIND_FIRST_DIFF(u32);
8679273e1bSThomas Hellstrom VMW_FIND_LAST_DIFF(u32);
8779273e1bSThomas Hellstrom 
8879273e1bSThomas Hellstrom #ifdef CONFIG_64BIT
8979273e1bSThomas Hellstrom VMW_FIND_FIRST_DIFF(u64);
9079273e1bSThomas Hellstrom VMW_FIND_LAST_DIFF(u64);
9179273e1bSThomas Hellstrom #endif
9279273e1bSThomas Hellstrom 
9379273e1bSThomas Hellstrom 
9479273e1bSThomas Hellstrom /* We use size aligned copies. This computes (addr - align(addr)) */
9579273e1bSThomas Hellstrom #define SPILL(_var, _type) ((unsigned long) _var & (sizeof(_type) - 1))
9679273e1bSThomas Hellstrom 
9779273e1bSThomas Hellstrom 
9879273e1bSThomas Hellstrom /*
9979273e1bSThomas Hellstrom  * Template to compute find_first_diff() for a certain integer type
10079273e1bSThomas Hellstrom  * including a head copy for alignment, and adjustment of parameters
10179273e1bSThomas Hellstrom  * for tail find or increased resolution find using an unsigned integer find
10279273e1bSThomas Hellstrom  * of smaller width. If finding is complete, and resolution is sufficient,
10379273e1bSThomas Hellstrom  * the macro executes a return statement. Otherwise it falls through.
10479273e1bSThomas Hellstrom  */
10579273e1bSThomas Hellstrom #define VMW_TRY_FIND_FIRST_DIFF(_type)					\
10679273e1bSThomas Hellstrom do {									\
10779273e1bSThomas Hellstrom 	unsigned int spill = SPILL(dst, _type);				\
10879273e1bSThomas Hellstrom 	size_t diff_offs;						\
10979273e1bSThomas Hellstrom 									\
11079273e1bSThomas Hellstrom 	if (spill && spill == SPILL(src, _type) &&			\
11179273e1bSThomas Hellstrom 	    sizeof(_type) - spill <= size) {				\
11279273e1bSThomas Hellstrom 		spill = sizeof(_type) - spill;				\
11379273e1bSThomas Hellstrom 		diff_offs = vmw_find_first_diff_u8(dst, src, spill);	\
11479273e1bSThomas Hellstrom 		if (diff_offs < spill)					\
11579273e1bSThomas Hellstrom 			return round_down(offset + diff_offs, granularity); \
11679273e1bSThomas Hellstrom 									\
11779273e1bSThomas Hellstrom 		dst += spill;						\
11879273e1bSThomas Hellstrom 		src += spill;						\
11979273e1bSThomas Hellstrom 		size -= spill;						\
12079273e1bSThomas Hellstrom 		offset += spill;					\
12179273e1bSThomas Hellstrom 		spill = 0;						\
12279273e1bSThomas Hellstrom 	}								\
12379273e1bSThomas Hellstrom 	if (!spill && !SPILL(src, _type)) {				\
12479273e1bSThomas Hellstrom 		size_t to_copy = size &	 ~(sizeof(_type) - 1);		\
12579273e1bSThomas Hellstrom 									\
12679273e1bSThomas Hellstrom 		diff_offs = vmw_find_first_diff_ ## _type		\
12779273e1bSThomas Hellstrom 			((_type *) dst, (_type *) src, to_copy);	\
12879273e1bSThomas Hellstrom 		if (diff_offs >= size || granularity == sizeof(_type))	\
12979273e1bSThomas Hellstrom 			return (offset + diff_offs);			\
13079273e1bSThomas Hellstrom 									\
13179273e1bSThomas Hellstrom 		dst += diff_offs;					\
13279273e1bSThomas Hellstrom 		src += diff_offs;					\
13379273e1bSThomas Hellstrom 		size -= diff_offs;					\
13479273e1bSThomas Hellstrom 		offset += diff_offs;					\
13579273e1bSThomas Hellstrom 	}								\
13679273e1bSThomas Hellstrom } while (0)								\
13779273e1bSThomas Hellstrom 
13879273e1bSThomas Hellstrom 
13979273e1bSThomas Hellstrom /**
14079273e1bSThomas Hellstrom  * vmw_find_first_diff - find the first difference between dst and src
14179273e1bSThomas Hellstrom  *
14279273e1bSThomas Hellstrom  * @dst: The destination address
14379273e1bSThomas Hellstrom  * @src: The source address
14479273e1bSThomas Hellstrom  * @size: Number of bytes to compare
14579273e1bSThomas Hellstrom  * @granularity: The granularity needed for the return value in bytes.
14679273e1bSThomas Hellstrom  * return: The offset from find start where the first difference was
14779273e1bSThomas Hellstrom  * encountered in bytes. If no difference was found, the function returns
14879273e1bSThomas Hellstrom  * a value >= @size.
14979273e1bSThomas Hellstrom  */
vmw_find_first_diff(const u8 * dst,const u8 * src,size_t size,size_t granularity)15079273e1bSThomas Hellstrom static size_t vmw_find_first_diff(const u8 *dst, const u8 *src, size_t size,
15179273e1bSThomas Hellstrom 				  size_t granularity)
15279273e1bSThomas Hellstrom {
15379273e1bSThomas Hellstrom 	size_t offset = 0;
15479273e1bSThomas Hellstrom 
15579273e1bSThomas Hellstrom 	/*
15679273e1bSThomas Hellstrom 	 * Try finding with large integers if alignment allows, or we can
15779273e1bSThomas Hellstrom 	 * fix it. Fall through if we need better resolution or alignment
15879273e1bSThomas Hellstrom 	 * was bad.
15979273e1bSThomas Hellstrom 	 */
16079273e1bSThomas Hellstrom #ifdef CONFIG_64BIT
16179273e1bSThomas Hellstrom 	VMW_TRY_FIND_FIRST_DIFF(u64);
16279273e1bSThomas Hellstrom #endif
16379273e1bSThomas Hellstrom 	VMW_TRY_FIND_FIRST_DIFF(u32);
16479273e1bSThomas Hellstrom 	VMW_TRY_FIND_FIRST_DIFF(u16);
16579273e1bSThomas Hellstrom 
16679273e1bSThomas Hellstrom 	return round_down(offset + vmw_find_first_diff_u8(dst, src, size),
16779273e1bSThomas Hellstrom 			  granularity);
16879273e1bSThomas Hellstrom }
16979273e1bSThomas Hellstrom 
17079273e1bSThomas Hellstrom 
17179273e1bSThomas Hellstrom /*
17279273e1bSThomas Hellstrom  * Template to compute find_last_diff() for a certain integer type
17379273e1bSThomas Hellstrom  * including a tail copy for alignment, and adjustment of parameters
17479273e1bSThomas Hellstrom  * for head find or increased resolution find using an unsigned integer find
17579273e1bSThomas Hellstrom  * of smaller width. If finding is complete, and resolution is sufficient,
17679273e1bSThomas Hellstrom  * the macro executes a return statement. Otherwise it falls through.
17779273e1bSThomas Hellstrom  */
17879273e1bSThomas Hellstrom #define VMW_TRY_FIND_LAST_DIFF(_type)					\
17979273e1bSThomas Hellstrom do {									\
18079273e1bSThomas Hellstrom 	unsigned int spill = SPILL(dst, _type);				\
18179273e1bSThomas Hellstrom 	ssize_t location;						\
18279273e1bSThomas Hellstrom 	ssize_t diff_offs;						\
18379273e1bSThomas Hellstrom 									\
18479273e1bSThomas Hellstrom 	if (spill && spill <= size && spill == SPILL(src, _type)) {	\
18579273e1bSThomas Hellstrom 		diff_offs = vmw_find_last_diff_u8(dst, src, spill);	\
18679273e1bSThomas Hellstrom 		if (diff_offs) {					\
18779273e1bSThomas Hellstrom 			location = size - spill + diff_offs - 1;	\
18879273e1bSThomas Hellstrom 			return round_down(location, granularity);	\
18979273e1bSThomas Hellstrom 		}							\
19079273e1bSThomas Hellstrom 									\
19179273e1bSThomas Hellstrom 		dst -= spill;						\
19279273e1bSThomas Hellstrom 		src -= spill;						\
19379273e1bSThomas Hellstrom 		size -= spill;						\
19479273e1bSThomas Hellstrom 		spill = 0;						\
19579273e1bSThomas Hellstrom 	}								\
19679273e1bSThomas Hellstrom 	if (!spill && !SPILL(src, _type)) {				\
19779273e1bSThomas Hellstrom 		size_t to_copy = round_down(size, sizeof(_type));	\
19879273e1bSThomas Hellstrom 									\
19979273e1bSThomas Hellstrom 		diff_offs = vmw_find_last_diff_ ## _type		\
20079273e1bSThomas Hellstrom 			((_type *) dst, (_type *) src, to_copy);	\
20179273e1bSThomas Hellstrom 		location = size - to_copy + diff_offs - sizeof(_type);	\
20279273e1bSThomas Hellstrom 		if (location < 0 || granularity == sizeof(_type))	\
20379273e1bSThomas Hellstrom 			return location;				\
20479273e1bSThomas Hellstrom 									\
20579273e1bSThomas Hellstrom 		dst -= to_copy - diff_offs;				\
20679273e1bSThomas Hellstrom 		src -= to_copy - diff_offs;				\
20779273e1bSThomas Hellstrom 		size -= to_copy - diff_offs;				\
20879273e1bSThomas Hellstrom 	}								\
20979273e1bSThomas Hellstrom } while (0)
21079273e1bSThomas Hellstrom 
21179273e1bSThomas Hellstrom 
21279273e1bSThomas Hellstrom /**
21379273e1bSThomas Hellstrom  * vmw_find_last_diff - find the last difference between dst and src
21479273e1bSThomas Hellstrom  *
21579273e1bSThomas Hellstrom  * @dst: The destination address
21679273e1bSThomas Hellstrom  * @src: The source address
21779273e1bSThomas Hellstrom  * @size: Number of bytes to compare
21879273e1bSThomas Hellstrom  * @granularity: The granularity needed for the return value in bytes.
21979273e1bSThomas Hellstrom  * return: The offset from find start where the last difference was
22079273e1bSThomas Hellstrom  * encountered in bytes, or a negative value if no difference was found.
22179273e1bSThomas Hellstrom  */
vmw_find_last_diff(const u8 * dst,const u8 * src,size_t size,size_t granularity)22279273e1bSThomas Hellstrom static ssize_t vmw_find_last_diff(const u8 *dst, const u8 *src, size_t size,
22379273e1bSThomas Hellstrom 				  size_t granularity)
22479273e1bSThomas Hellstrom {
22579273e1bSThomas Hellstrom 	dst += size;
22679273e1bSThomas Hellstrom 	src += size;
22779273e1bSThomas Hellstrom 
22879273e1bSThomas Hellstrom #ifdef CONFIG_64BIT
22979273e1bSThomas Hellstrom 	VMW_TRY_FIND_LAST_DIFF(u64);
23079273e1bSThomas Hellstrom #endif
23179273e1bSThomas Hellstrom 	VMW_TRY_FIND_LAST_DIFF(u32);
23279273e1bSThomas Hellstrom 	VMW_TRY_FIND_LAST_DIFF(u16);
23379273e1bSThomas Hellstrom 
23479273e1bSThomas Hellstrom 	return round_down(vmw_find_last_diff_u8(dst, src, size) - 1,
23579273e1bSThomas Hellstrom 			  granularity);
23679273e1bSThomas Hellstrom }
23779273e1bSThomas Hellstrom 
23879273e1bSThomas Hellstrom 
23979273e1bSThomas Hellstrom /**
24079273e1bSThomas Hellstrom  * vmw_memcpy - A wrapper around kernel memcpy with allowing to plug it into a
24179273e1bSThomas Hellstrom  * struct vmw_diff_cpy.
24279273e1bSThomas Hellstrom  *
24379273e1bSThomas Hellstrom  * @diff: The struct vmw_diff_cpy closure argument (unused).
24479273e1bSThomas Hellstrom  * @dest: The copy destination.
24579273e1bSThomas Hellstrom  * @src: The copy source.
24679273e1bSThomas Hellstrom  * @n: Number of bytes to copy.
24779273e1bSThomas Hellstrom  */
vmw_memcpy(struct vmw_diff_cpy * diff,u8 * dest,const u8 * src,size_t n)24879273e1bSThomas Hellstrom void vmw_memcpy(struct vmw_diff_cpy *diff, u8 *dest, const u8 *src, size_t n)
24979273e1bSThomas Hellstrom {
25079273e1bSThomas Hellstrom 	memcpy(dest, src, n);
25179273e1bSThomas Hellstrom }
25279273e1bSThomas Hellstrom 
25379273e1bSThomas Hellstrom 
25479273e1bSThomas Hellstrom /**
25579273e1bSThomas Hellstrom  * vmw_adjust_rect - Adjust rectangle coordinates for newly found difference
25679273e1bSThomas Hellstrom  *
25779273e1bSThomas Hellstrom  * @diff: The struct vmw_diff_cpy used to track the modified bounding box.
25879273e1bSThomas Hellstrom  * @diff_offs: The offset from @diff->line_offset where the difference was
25979273e1bSThomas Hellstrom  * found.
26079273e1bSThomas Hellstrom  */
vmw_adjust_rect(struct vmw_diff_cpy * diff,size_t diff_offs)26179273e1bSThomas Hellstrom static void vmw_adjust_rect(struct vmw_diff_cpy *diff, size_t diff_offs)
26279273e1bSThomas Hellstrom {
26379273e1bSThomas Hellstrom 	size_t offs = (diff_offs + diff->line_offset) / diff->cpp;
26479273e1bSThomas Hellstrom 	struct drm_rect *rect = &diff->rect;
26579273e1bSThomas Hellstrom 
26679273e1bSThomas Hellstrom 	rect->x1 = min_t(int, rect->x1, offs);
26779273e1bSThomas Hellstrom 	rect->x2 = max_t(int, rect->x2, offs + 1);
26879273e1bSThomas Hellstrom 	rect->y1 = min_t(int, rect->y1, diff->line);
26979273e1bSThomas Hellstrom 	rect->y2 = max_t(int, rect->y2, diff->line + 1);
27079273e1bSThomas Hellstrom }
27179273e1bSThomas Hellstrom 
27279273e1bSThomas Hellstrom /**
27379273e1bSThomas Hellstrom  * vmw_diff_memcpy - memcpy that creates a bounding box of modified content.
27479273e1bSThomas Hellstrom  *
27579273e1bSThomas Hellstrom  * @diff: The struct vmw_diff_cpy used to track the modified bounding box.
27679273e1bSThomas Hellstrom  * @dest: The copy destination.
27779273e1bSThomas Hellstrom  * @src: The copy source.
27879273e1bSThomas Hellstrom  * @n: Number of bytes to copy.
27979273e1bSThomas Hellstrom  *
28079273e1bSThomas Hellstrom  * In order to correctly track the modified content, the field @diff->line must
28179273e1bSThomas Hellstrom  * be pre-loaded with the current line number, the field @diff->line_offset must
28279273e1bSThomas Hellstrom  * be pre-loaded with the line offset in bytes where the copy starts, and
28379273e1bSThomas Hellstrom  * finally the field @diff->cpp need to be preloaded with the number of bytes
28479273e1bSThomas Hellstrom  * per unit in the horizontal direction of the area we're examining.
28579273e1bSThomas Hellstrom  * Typically bytes per pixel.
28679273e1bSThomas Hellstrom  * This is needed to know the needed granularity of the difference computing
28779273e1bSThomas Hellstrom  * operations. A higher cpp generally leads to faster execution at the cost of
28879273e1bSThomas Hellstrom  * bounding box width precision.
28979273e1bSThomas Hellstrom  */
vmw_diff_memcpy(struct vmw_diff_cpy * diff,u8 * dest,const u8 * src,size_t n)29079273e1bSThomas Hellstrom void vmw_diff_memcpy(struct vmw_diff_cpy *diff, u8 *dest, const u8 *src,
29179273e1bSThomas Hellstrom 		     size_t n)
29279273e1bSThomas Hellstrom {
29379273e1bSThomas Hellstrom 	ssize_t csize, byte_len;
29479273e1bSThomas Hellstrom 
29579273e1bSThomas Hellstrom 	if (WARN_ON_ONCE(round_down(n, diff->cpp) != n))
29679273e1bSThomas Hellstrom 		return;
29779273e1bSThomas Hellstrom 
29879273e1bSThomas Hellstrom 	/* TODO: Possibly use a single vmw_find_first_diff per line? */
29979273e1bSThomas Hellstrom 	csize = vmw_find_first_diff(dest, src, n, diff->cpp);
30079273e1bSThomas Hellstrom 	if (csize < n) {
30179273e1bSThomas Hellstrom 		vmw_adjust_rect(diff, csize);
30279273e1bSThomas Hellstrom 		byte_len = diff->cpp;
30379273e1bSThomas Hellstrom 
30479273e1bSThomas Hellstrom 		/*
30579273e1bSThomas Hellstrom 		 * Starting from where first difference was found, find
30679273e1bSThomas Hellstrom 		 * location of last difference, and then copy.
30779273e1bSThomas Hellstrom 		 */
30879273e1bSThomas Hellstrom 		diff->line_offset += csize;
30979273e1bSThomas Hellstrom 		dest += csize;
31079273e1bSThomas Hellstrom 		src += csize;
31179273e1bSThomas Hellstrom 		n -= csize;
31279273e1bSThomas Hellstrom 		csize = vmw_find_last_diff(dest, src, n, diff->cpp);
31379273e1bSThomas Hellstrom 		if (csize >= 0) {
31479273e1bSThomas Hellstrom 			byte_len += csize;
31579273e1bSThomas Hellstrom 			vmw_adjust_rect(diff, csize);
31679273e1bSThomas Hellstrom 		}
31779273e1bSThomas Hellstrom 		memcpy(dest, src, byte_len);
31879273e1bSThomas Hellstrom 	}
31979273e1bSThomas Hellstrom 	diff->line_offset += n;
32079273e1bSThomas Hellstrom }
32179273e1bSThomas Hellstrom 
32279273e1bSThomas Hellstrom /**
32379273e1bSThomas Hellstrom  * struct vmw_bo_blit_line_data - Convenience argument to vmw_bo_cpu_blit_line
32479273e1bSThomas Hellstrom  *
32579273e1bSThomas Hellstrom  * @mapped_dst: Already mapped destination page index in @dst_pages.
32679273e1bSThomas Hellstrom  * @dst_addr: Kernel virtual address of mapped destination page.
32779273e1bSThomas Hellstrom  * @dst_pages: Array of destination bo pages.
32879273e1bSThomas Hellstrom  * @dst_num_pages: Number of destination bo pages.
32979273e1bSThomas Hellstrom  * @dst_prot: Destination bo page protection.
33079273e1bSThomas Hellstrom  * @mapped_src: Already mapped source page index in @dst_pages.
33179273e1bSThomas Hellstrom  * @src_addr: Kernel virtual address of mapped source page.
33279273e1bSThomas Hellstrom  * @src_pages: Array of source bo pages.
33379273e1bSThomas Hellstrom  * @src_num_pages: Number of source bo pages.
33479273e1bSThomas Hellstrom  * @src_prot: Source bo page protection.
33579273e1bSThomas Hellstrom  * @diff: Struct vmw_diff_cpy, in the end forwarded to the memcpy routine.
33679273e1bSThomas Hellstrom  */
33779273e1bSThomas Hellstrom struct vmw_bo_blit_line_data {
33879273e1bSThomas Hellstrom 	u32 mapped_dst;
33979273e1bSThomas Hellstrom 	u8 *dst_addr;
34079273e1bSThomas Hellstrom 	struct page **dst_pages;
34179273e1bSThomas Hellstrom 	u32 dst_num_pages;
34279273e1bSThomas Hellstrom 	pgprot_t dst_prot;
34379273e1bSThomas Hellstrom 	u32 mapped_src;
34479273e1bSThomas Hellstrom 	u8 *src_addr;
34579273e1bSThomas Hellstrom 	struct page **src_pages;
34679273e1bSThomas Hellstrom 	u32 src_num_pages;
34779273e1bSThomas Hellstrom 	pgprot_t src_prot;
34879273e1bSThomas Hellstrom 	struct vmw_diff_cpy *diff;
34979273e1bSThomas Hellstrom };
35079273e1bSThomas Hellstrom 
35179273e1bSThomas Hellstrom /**
35279273e1bSThomas Hellstrom  * vmw_bo_cpu_blit_line - Blit part of a line from one bo to another.
35379273e1bSThomas Hellstrom  *
35479273e1bSThomas Hellstrom  * @d: Blit data as described above.
35579273e1bSThomas Hellstrom  * @dst_offset: Destination copy start offset from start of bo.
35679273e1bSThomas Hellstrom  * @src_offset: Source copy start offset from start of bo.
35779273e1bSThomas Hellstrom  * @bytes_to_copy: Number of bytes to copy in this line.
35879273e1bSThomas Hellstrom  */
vmw_bo_cpu_blit_line(struct vmw_bo_blit_line_data * d,u32 dst_offset,u32 src_offset,u32 bytes_to_copy)35979273e1bSThomas Hellstrom static int vmw_bo_cpu_blit_line(struct vmw_bo_blit_line_data *d,
36079273e1bSThomas Hellstrom 				u32 dst_offset,
36179273e1bSThomas Hellstrom 				u32 src_offset,
36279273e1bSThomas Hellstrom 				u32 bytes_to_copy)
36379273e1bSThomas Hellstrom {
36479273e1bSThomas Hellstrom 	struct vmw_diff_cpy *diff = d->diff;
36579273e1bSThomas Hellstrom 
36679273e1bSThomas Hellstrom 	while (bytes_to_copy) {
36779273e1bSThomas Hellstrom 		u32 copy_size = bytes_to_copy;
36879273e1bSThomas Hellstrom 		u32 dst_page = dst_offset >> PAGE_SHIFT;
36979273e1bSThomas Hellstrom 		u32 src_page = src_offset >> PAGE_SHIFT;
37079273e1bSThomas Hellstrom 		u32 dst_page_offset = dst_offset & ~PAGE_MASK;
37179273e1bSThomas Hellstrom 		u32 src_page_offset = src_offset & ~PAGE_MASK;
37279273e1bSThomas Hellstrom 		bool unmap_dst = d->dst_addr && dst_page != d->mapped_dst;
37379273e1bSThomas Hellstrom 		bool unmap_src = d->src_addr && (src_page != d->mapped_src ||
37479273e1bSThomas Hellstrom 						 unmap_dst);
37579273e1bSThomas Hellstrom 
37679273e1bSThomas Hellstrom 		copy_size = min_t(u32, copy_size, PAGE_SIZE - dst_page_offset);
37779273e1bSThomas Hellstrom 		copy_size = min_t(u32, copy_size, PAGE_SIZE - src_page_offset);
37879273e1bSThomas Hellstrom 
37979273e1bSThomas Hellstrom 		if (unmap_src) {
380915ecc22SIra Weiny 			kunmap_atomic(d->src_addr);
38179273e1bSThomas Hellstrom 			d->src_addr = NULL;
38279273e1bSThomas Hellstrom 		}
38379273e1bSThomas Hellstrom 
38479273e1bSThomas Hellstrom 		if (unmap_dst) {
385915ecc22SIra Weiny 			kunmap_atomic(d->dst_addr);
38679273e1bSThomas Hellstrom 			d->dst_addr = NULL;
38779273e1bSThomas Hellstrom 		}
38879273e1bSThomas Hellstrom 
38979273e1bSThomas Hellstrom 		if (!d->dst_addr) {
39079273e1bSThomas Hellstrom 			if (WARN_ON_ONCE(dst_page >= d->dst_num_pages))
39179273e1bSThomas Hellstrom 				return -EINVAL;
39279273e1bSThomas Hellstrom 
39379273e1bSThomas Hellstrom 			d->dst_addr =
394915ecc22SIra Weiny 				kmap_atomic_prot(d->dst_pages[dst_page],
39579273e1bSThomas Hellstrom 						 d->dst_prot);
39679273e1bSThomas Hellstrom 			if (!d->dst_addr)
39779273e1bSThomas Hellstrom 				return -ENOMEM;
39879273e1bSThomas Hellstrom 
39979273e1bSThomas Hellstrom 			d->mapped_dst = dst_page;
40079273e1bSThomas Hellstrom 		}
40179273e1bSThomas Hellstrom 
40279273e1bSThomas Hellstrom 		if (!d->src_addr) {
40379273e1bSThomas Hellstrom 			if (WARN_ON_ONCE(src_page >= d->src_num_pages))
40479273e1bSThomas Hellstrom 				return -EINVAL;
40579273e1bSThomas Hellstrom 
40679273e1bSThomas Hellstrom 			d->src_addr =
407915ecc22SIra Weiny 				kmap_atomic_prot(d->src_pages[src_page],
40879273e1bSThomas Hellstrom 						 d->src_prot);
40979273e1bSThomas Hellstrom 			if (!d->src_addr)
41079273e1bSThomas Hellstrom 				return -ENOMEM;
41179273e1bSThomas Hellstrom 
41279273e1bSThomas Hellstrom 			d->mapped_src = src_page;
41379273e1bSThomas Hellstrom 		}
41479273e1bSThomas Hellstrom 		diff->do_cpy(diff, d->dst_addr + dst_page_offset,
41579273e1bSThomas Hellstrom 			     d->src_addr + src_page_offset, copy_size);
41679273e1bSThomas Hellstrom 
41779273e1bSThomas Hellstrom 		bytes_to_copy -= copy_size;
41879273e1bSThomas Hellstrom 		dst_offset += copy_size;
41979273e1bSThomas Hellstrom 		src_offset += copy_size;
42079273e1bSThomas Hellstrom 	}
42179273e1bSThomas Hellstrom 
42279273e1bSThomas Hellstrom 	return 0;
42379273e1bSThomas Hellstrom }
42479273e1bSThomas Hellstrom 
map_external(struct vmw_bo * bo,struct iosys_map * map)425*50f11992SZack Rusin static void *map_external(struct vmw_bo *bo, struct iosys_map *map)
426*50f11992SZack Rusin {
427*50f11992SZack Rusin 	struct vmw_private *vmw =
428*50f11992SZack Rusin 		container_of(bo->tbo.bdev, struct vmw_private, bdev);
429*50f11992SZack Rusin 	void *ptr = NULL;
430*50f11992SZack Rusin 	int ret;
431*50f11992SZack Rusin 
432*50f11992SZack Rusin 	if (bo->tbo.base.import_attach) {
433*50f11992SZack Rusin 		ret = dma_buf_vmap(bo->tbo.base.dma_buf, map);
434*50f11992SZack Rusin 		if (ret) {
435*50f11992SZack Rusin 			drm_dbg_driver(&vmw->drm,
436*50f11992SZack Rusin 				       "Wasn't able to map external bo!\n");
437*50f11992SZack Rusin 			goto out;
438*50f11992SZack Rusin 		}
439*50f11992SZack Rusin 		ptr = map->vaddr;
440*50f11992SZack Rusin 	} else {
441*50f11992SZack Rusin 		ptr = vmw_bo_map_and_cache(bo);
442*50f11992SZack Rusin 	}
443*50f11992SZack Rusin 
444*50f11992SZack Rusin out:
445*50f11992SZack Rusin 	return ptr;
446*50f11992SZack Rusin }
447*50f11992SZack Rusin 
unmap_external(struct vmw_bo * bo,struct iosys_map * map)448*50f11992SZack Rusin static void unmap_external(struct vmw_bo *bo, struct iosys_map *map)
449*50f11992SZack Rusin {
450*50f11992SZack Rusin 	if (bo->tbo.base.import_attach)
451*50f11992SZack Rusin 		dma_buf_vunmap(bo->tbo.base.dma_buf, map);
452*50f11992SZack Rusin 	else
453*50f11992SZack Rusin 		vmw_bo_unmap(bo);
454*50f11992SZack Rusin }
455*50f11992SZack Rusin 
vmw_external_bo_copy(struct vmw_bo * dst,u32 dst_offset,u32 dst_stride,struct vmw_bo * src,u32 src_offset,u32 src_stride,u32 width_in_bytes,u32 height,struct vmw_diff_cpy * diff)456*50f11992SZack Rusin static int vmw_external_bo_copy(struct vmw_bo *dst, u32 dst_offset,
457*50f11992SZack Rusin 				u32 dst_stride, struct vmw_bo *src,
458*50f11992SZack Rusin 				u32 src_offset, u32 src_stride,
459*50f11992SZack Rusin 				u32 width_in_bytes, u32 height,
460*50f11992SZack Rusin 				struct vmw_diff_cpy *diff)
461*50f11992SZack Rusin {
462*50f11992SZack Rusin 	struct vmw_private *vmw =
463*50f11992SZack Rusin 		container_of(dst->tbo.bdev, struct vmw_private, bdev);
464*50f11992SZack Rusin 	size_t dst_size = dst->tbo.resource->size;
465*50f11992SZack Rusin 	size_t src_size = src->tbo.resource->size;
466*50f11992SZack Rusin 	struct iosys_map dst_map = {0};
467*50f11992SZack Rusin 	struct iosys_map src_map = {0};
468*50f11992SZack Rusin 	int ret, i;
469*50f11992SZack Rusin 	int x_in_bytes;
470*50f11992SZack Rusin 	u8 *vsrc;
471*50f11992SZack Rusin 	u8 *vdst;
472*50f11992SZack Rusin 
473*50f11992SZack Rusin 	vsrc = map_external(src, &src_map);
474*50f11992SZack Rusin 	if (!vsrc) {
475*50f11992SZack Rusin 		drm_dbg_driver(&vmw->drm, "Wasn't able to map src\n");
476*50f11992SZack Rusin 		ret = -ENOMEM;
477*50f11992SZack Rusin 		goto out;
478*50f11992SZack Rusin 	}
479*50f11992SZack Rusin 
480*50f11992SZack Rusin 	vdst = map_external(dst, &dst_map);
481*50f11992SZack Rusin 	if (!vdst) {
482*50f11992SZack Rusin 		drm_dbg_driver(&vmw->drm, "Wasn't able to map dst\n");
483*50f11992SZack Rusin 		ret = -ENOMEM;
484*50f11992SZack Rusin 		goto out;
485*50f11992SZack Rusin 	}
486*50f11992SZack Rusin 
487*50f11992SZack Rusin 	vsrc += src_offset;
488*50f11992SZack Rusin 	vdst += dst_offset;
489*50f11992SZack Rusin 	if (src_stride == dst_stride) {
490*50f11992SZack Rusin 		dst_size -= dst_offset;
491*50f11992SZack Rusin 		src_size -= src_offset;
492*50f11992SZack Rusin 		memcpy(vdst, vsrc,
493*50f11992SZack Rusin 		       min(dst_stride * height, min(dst_size, src_size)));
494*50f11992SZack Rusin 	} else {
495*50f11992SZack Rusin 		WARN_ON(dst_stride < width_in_bytes);
496*50f11992SZack Rusin 		for (i = 0; i < height; ++i) {
497*50f11992SZack Rusin 			memcpy(vdst, vsrc, width_in_bytes);
498*50f11992SZack Rusin 			vsrc += src_stride;
499*50f11992SZack Rusin 			vdst += dst_stride;
500*50f11992SZack Rusin 		}
501*50f11992SZack Rusin 	}
502*50f11992SZack Rusin 
503*50f11992SZack Rusin 	x_in_bytes = (dst_offset % dst_stride);
504*50f11992SZack Rusin 	diff->rect.x1 =  x_in_bytes / diff->cpp;
505*50f11992SZack Rusin 	diff->rect.y1 = ((dst_offset - x_in_bytes) / dst_stride);
506*50f11992SZack Rusin 	diff->rect.x2 = diff->rect.x1 + width_in_bytes / diff->cpp;
507*50f11992SZack Rusin 	diff->rect.y2 = diff->rect.y1 + height;
508*50f11992SZack Rusin 
509*50f11992SZack Rusin 	ret = 0;
510*50f11992SZack Rusin out:
511*50f11992SZack Rusin 	unmap_external(src, &src_map);
512*50f11992SZack Rusin 	unmap_external(dst, &dst_map);
513*50f11992SZack Rusin 
514*50f11992SZack Rusin 	return ret;
515*50f11992SZack Rusin }
516*50f11992SZack Rusin 
51779273e1bSThomas Hellstrom /**
5182cd80dbdSZack Rusin  * vmw_bo_cpu_blit - in-kernel cpu blit.
51979273e1bSThomas Hellstrom  *
520*50f11992SZack Rusin  * @vmw_dst: Destination buffer object.
52179273e1bSThomas Hellstrom  * @dst_offset: Destination offset of blit start in bytes.
52279273e1bSThomas Hellstrom  * @dst_stride: Destination stride in bytes.
523*50f11992SZack Rusin  * @vmw_src: Source buffer object.
52479273e1bSThomas Hellstrom  * @src_offset: Source offset of blit start in bytes.
52579273e1bSThomas Hellstrom  * @src_stride: Source stride in bytes.
52679273e1bSThomas Hellstrom  * @w: Width of blit.
52779273e1bSThomas Hellstrom  * @h: Height of blit.
5289983a31dSLee Jones  * @diff: The struct vmw_diff_cpy used to track the modified bounding box.
52979273e1bSThomas Hellstrom  * return: Zero on success. Negative error value on failure. Will print out
53079273e1bSThomas Hellstrom  * kernel warnings on caller bugs.
53179273e1bSThomas Hellstrom  *
53279273e1bSThomas Hellstrom  * Performs a CPU blit from one buffer object to another avoiding a full
53379273e1bSThomas Hellstrom  * bo vmap which may exhaust- or fragment vmalloc space.
53479273e1bSThomas Hellstrom  * On supported architectures (x86), we're using kmap_atomic which avoids
53579273e1bSThomas Hellstrom  * cross-processor TLB- and cache flushes and may, on non-HIGHMEM systems
53679273e1bSThomas Hellstrom  * reference already set-up mappings.
53779273e1bSThomas Hellstrom  *
53879273e1bSThomas Hellstrom  * Neither of the buffer objects may be placed in PCI memory
53979273e1bSThomas Hellstrom  * (Fixed memory in TTM terminology) when using this function.
54079273e1bSThomas Hellstrom  */
vmw_bo_cpu_blit(struct vmw_bo * vmw_dst,u32 dst_offset,u32 dst_stride,struct vmw_bo * vmw_src,u32 src_offset,u32 src_stride,u32 w,u32 h,struct vmw_diff_cpy * diff)541*50f11992SZack Rusin int vmw_bo_cpu_blit(struct vmw_bo *vmw_dst,
54279273e1bSThomas Hellstrom 		    u32 dst_offset, u32 dst_stride,
543*50f11992SZack Rusin 		    struct vmw_bo *vmw_src,
54479273e1bSThomas Hellstrom 		    u32 src_offset, u32 src_stride,
54579273e1bSThomas Hellstrom 		    u32 w, u32 h,
54679273e1bSThomas Hellstrom 		    struct vmw_diff_cpy *diff)
54779273e1bSThomas Hellstrom {
548*50f11992SZack Rusin 	struct ttm_buffer_object *src = &vmw_src->tbo;
549*50f11992SZack Rusin 	struct ttm_buffer_object *dst = &vmw_dst->tbo;
55079273e1bSThomas Hellstrom 	struct ttm_operation_ctx ctx = {
55179273e1bSThomas Hellstrom 		.interruptible = false,
55279273e1bSThomas Hellstrom 		.no_wait_gpu = false
55379273e1bSThomas Hellstrom 	};
55479273e1bSThomas Hellstrom 	u32 j, initial_line = dst_offset / dst_stride;
555b32233acSZack Rusin 	struct vmw_bo_blit_line_data d = {0};
55679273e1bSThomas Hellstrom 	int ret = 0;
557b32233acSZack Rusin 	struct page **dst_pages = NULL;
558b32233acSZack Rusin 	struct page **src_pages = NULL;
559*50f11992SZack Rusin 	bool src_external = (src->ttm->page_flags & TTM_TT_FLAG_EXTERNAL) != 0;
560*50f11992SZack Rusin 	bool dst_external = (dst->ttm->page_flags & TTM_TT_FLAG_EXTERNAL) != 0;
561*50f11992SZack Rusin 
562*50f11992SZack Rusin 	if (WARN_ON(dst == src))
563*50f11992SZack Rusin 		return -EINVAL;
56479273e1bSThomas Hellstrom 
56579273e1bSThomas Hellstrom 	/* Buffer objects need to be either pinned or reserved: */
566fbe86ca5SChristian König 	if (!(dst->pin_count))
56752791eeeSChristian König 		dma_resv_assert_held(dst->base.resv);
568fbe86ca5SChristian König 	if (!(src->pin_count))
56952791eeeSChristian König 		dma_resv_assert_held(src->base.resv);
57079273e1bSThomas Hellstrom 
5717eec9151SDave Airlie 	if (!ttm_tt_is_populated(dst->ttm)) {
5728af8a109SChristian König 		ret = dst->bdev->funcs->ttm_tt_populate(dst->bdev, dst->ttm, &ctx);
57379273e1bSThomas Hellstrom 		if (ret)
57479273e1bSThomas Hellstrom 			return ret;
57579273e1bSThomas Hellstrom 	}
57679273e1bSThomas Hellstrom 
5777eec9151SDave Airlie 	if (!ttm_tt_is_populated(src->ttm)) {
5788af8a109SChristian König 		ret = src->bdev->funcs->ttm_tt_populate(src->bdev, src->ttm, &ctx);
57979273e1bSThomas Hellstrom 		if (ret)
58079273e1bSThomas Hellstrom 			return ret;
58179273e1bSThomas Hellstrom 	}
58279273e1bSThomas Hellstrom 
583*50f11992SZack Rusin 	if (src_external || dst_external)
584*50f11992SZack Rusin 		return vmw_external_bo_copy(vmw_dst, dst_offset, dst_stride,
585*50f11992SZack Rusin 					    vmw_src, src_offset, src_stride,
586*50f11992SZack Rusin 					    w, h, diff);
587*50f11992SZack Rusin 
588b32233acSZack Rusin 	if (!src->ttm->pages && src->ttm->sg) {
589b32233acSZack Rusin 		src_pages = kvmalloc_array(src->ttm->num_pages,
590b32233acSZack Rusin 					   sizeof(struct page *), GFP_KERNEL);
591b32233acSZack Rusin 		if (!src_pages)
592b32233acSZack Rusin 			return -ENOMEM;
593b32233acSZack Rusin 		ret = drm_prime_sg_to_page_array(src->ttm->sg, src_pages,
594b32233acSZack Rusin 						 src->ttm->num_pages);
595b32233acSZack Rusin 		if (ret)
596b32233acSZack Rusin 			goto out;
597b32233acSZack Rusin 	}
598b32233acSZack Rusin 	if (!dst->ttm->pages && dst->ttm->sg) {
599b32233acSZack Rusin 		dst_pages = kvmalloc_array(dst->ttm->num_pages,
600b32233acSZack Rusin 					   sizeof(struct page *), GFP_KERNEL);
601b32233acSZack Rusin 		if (!dst_pages) {
602b32233acSZack Rusin 			ret = -ENOMEM;
603b32233acSZack Rusin 			goto out;
604b32233acSZack Rusin 		}
605b32233acSZack Rusin 		ret = drm_prime_sg_to_page_array(dst->ttm->sg, dst_pages,
606b32233acSZack Rusin 						 dst->ttm->num_pages);
607b32233acSZack Rusin 		if (ret)
608b32233acSZack Rusin 			goto out;
609b32233acSZack Rusin 	}
610b32233acSZack Rusin 
61179273e1bSThomas Hellstrom 	d.mapped_dst = 0;
61279273e1bSThomas Hellstrom 	d.mapped_src = 0;
61379273e1bSThomas Hellstrom 	d.dst_addr = NULL;
61479273e1bSThomas Hellstrom 	d.src_addr = NULL;
615b32233acSZack Rusin 	d.dst_pages = dst->ttm->pages ? dst->ttm->pages : dst_pages;
616b32233acSZack Rusin 	d.src_pages = src->ttm->pages ? src->ttm->pages : src_pages;
617e3c92eb4SSomalapuram Amaranath 	d.dst_num_pages = PFN_UP(dst->resource->size);
618e3c92eb4SSomalapuram Amaranath 	d.src_num_pages = PFN_UP(src->resource->size);
619d3116756SChristian König 	d.dst_prot = ttm_io_prot(dst, dst->resource, PAGE_KERNEL);
620d3116756SChristian König 	d.src_prot = ttm_io_prot(src, src->resource, PAGE_KERNEL);
62179273e1bSThomas Hellstrom 	d.diff = diff;
62279273e1bSThomas Hellstrom 
62379273e1bSThomas Hellstrom 	for (j = 0; j < h; ++j) {
62479273e1bSThomas Hellstrom 		diff->line = j + initial_line;
62579273e1bSThomas Hellstrom 		diff->line_offset = dst_offset % dst_stride;
62679273e1bSThomas Hellstrom 		ret = vmw_bo_cpu_blit_line(&d, dst_offset, src_offset, w);
62779273e1bSThomas Hellstrom 		if (ret)
62879273e1bSThomas Hellstrom 			goto out;
62979273e1bSThomas Hellstrom 
63079273e1bSThomas Hellstrom 		dst_offset += dst_stride;
63179273e1bSThomas Hellstrom 		src_offset += src_stride;
63279273e1bSThomas Hellstrom 	}
63379273e1bSThomas Hellstrom out:
63479273e1bSThomas Hellstrom 	if (d.src_addr)
635915ecc22SIra Weiny 		kunmap_atomic(d.src_addr);
63679273e1bSThomas Hellstrom 	if (d.dst_addr)
637915ecc22SIra Weiny 		kunmap_atomic(d.dst_addr);
638b32233acSZack Rusin 	if (src_pages)
639b32233acSZack Rusin 		kvfree(src_pages);
640b32233acSZack Rusin 	if (dst_pages)
641b32233acSZack Rusin 		kvfree(dst_pages);
64279273e1bSThomas Hellstrom 
64379273e1bSThomas Hellstrom 	return ret;
64479273e1bSThomas Hellstrom }
645