1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0 OR MIT
2*4882a593Smuzhiyun /**************************************************************************
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright 2017 VMware, Inc., Palo Alto, CA., USA
5*4882a593Smuzhiyun * All Rights Reserved.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person obtaining a
8*4882a593Smuzhiyun * copy of this software and associated documentation files (the
9*4882a593Smuzhiyun * "Software"), to deal in the Software without restriction, including
10*4882a593Smuzhiyun * without limitation the rights to use, copy, modify, merge, publish,
11*4882a593Smuzhiyun * distribute, sub license, and/or sell copies of the Software, and to
12*4882a593Smuzhiyun * permit persons to whom the Software is furnished to do so, subject to
13*4882a593Smuzhiyun * the following conditions:
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * The above copyright notice and this permission notice (including the
16*4882a593Smuzhiyun * next paragraph) shall be included in all copies or substantial portions
17*4882a593Smuzhiyun * of the Software.
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20*4882a593Smuzhiyun * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21*4882a593Smuzhiyun * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22*4882a593Smuzhiyun * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23*4882a593Smuzhiyun * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24*4882a593Smuzhiyun * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25*4882a593Smuzhiyun * USE OR OTHER DEALINGS IN THE SOFTWARE.
26*4882a593Smuzhiyun *
27*4882a593Smuzhiyun **************************************************************************/
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #include "vmwgfx_drv.h"
30*4882a593Smuzhiyun #include <linux/highmem.h>
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun /*
33*4882a593Smuzhiyun * Template that implements find_first_diff() for a generic
34*4882a593Smuzhiyun * unsigned integer type. @size and return value are in bytes.
35*4882a593Smuzhiyun */
36*4882a593Smuzhiyun #define VMW_FIND_FIRST_DIFF(_type) \
37*4882a593Smuzhiyun static size_t vmw_find_first_diff_ ## _type \
38*4882a593Smuzhiyun (const _type * dst, const _type * src, size_t size)\
39*4882a593Smuzhiyun { \
40*4882a593Smuzhiyun size_t i; \
41*4882a593Smuzhiyun \
42*4882a593Smuzhiyun for (i = 0; i < size; i += sizeof(_type)) { \
43*4882a593Smuzhiyun if (*dst++ != *src++) \
44*4882a593Smuzhiyun break; \
45*4882a593Smuzhiyun } \
46*4882a593Smuzhiyun \
47*4882a593Smuzhiyun return i; \
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun /*
52*4882a593Smuzhiyun * Template that implements find_last_diff() for a generic
53*4882a593Smuzhiyun * unsigned integer type. Pointers point to the item following the
54*4882a593Smuzhiyun * *end* of the area to be examined. @size and return value are in
55*4882a593Smuzhiyun * bytes.
56*4882a593Smuzhiyun */
57*4882a593Smuzhiyun #define VMW_FIND_LAST_DIFF(_type) \
58*4882a593Smuzhiyun static ssize_t vmw_find_last_diff_ ## _type( \
59*4882a593Smuzhiyun const _type * dst, const _type * src, size_t size) \
60*4882a593Smuzhiyun { \
61*4882a593Smuzhiyun while (size) { \
62*4882a593Smuzhiyun if (*--dst != *--src) \
63*4882a593Smuzhiyun break; \
64*4882a593Smuzhiyun \
65*4882a593Smuzhiyun size -= sizeof(_type); \
66*4882a593Smuzhiyun } \
67*4882a593Smuzhiyun return size; \
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /*
72*4882a593Smuzhiyun * Instantiate find diff functions for relevant unsigned integer sizes,
73*4882a593Smuzhiyun * assuming that wider integers are faster (including aligning) up to the
74*4882a593Smuzhiyun * architecture native width, which is assumed to be 32 bit unless
75*4882a593Smuzhiyun * CONFIG_64BIT is defined.
76*4882a593Smuzhiyun */
77*4882a593Smuzhiyun VMW_FIND_FIRST_DIFF(u8);
78*4882a593Smuzhiyun VMW_FIND_LAST_DIFF(u8);
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun VMW_FIND_FIRST_DIFF(u16);
81*4882a593Smuzhiyun VMW_FIND_LAST_DIFF(u16);
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun VMW_FIND_FIRST_DIFF(u32);
84*4882a593Smuzhiyun VMW_FIND_LAST_DIFF(u32);
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun #ifdef CONFIG_64BIT
87*4882a593Smuzhiyun VMW_FIND_FIRST_DIFF(u64);
88*4882a593Smuzhiyun VMW_FIND_LAST_DIFF(u64);
89*4882a593Smuzhiyun #endif
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun /* We use size aligned copies. This computes (addr - align(addr)) */
93*4882a593Smuzhiyun #define SPILL(_var, _type) ((unsigned long) _var & (sizeof(_type) - 1))
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun /*
97*4882a593Smuzhiyun * Template to compute find_first_diff() for a certain integer type
98*4882a593Smuzhiyun * including a head copy for alignment, and adjustment of parameters
99*4882a593Smuzhiyun * for tail find or increased resolution find using an unsigned integer find
100*4882a593Smuzhiyun * of smaller width. If finding is complete, and resolution is sufficient,
101*4882a593Smuzhiyun * the macro executes a return statement. Otherwise it falls through.
102*4882a593Smuzhiyun */
103*4882a593Smuzhiyun #define VMW_TRY_FIND_FIRST_DIFF(_type) \
104*4882a593Smuzhiyun do { \
105*4882a593Smuzhiyun unsigned int spill = SPILL(dst, _type); \
106*4882a593Smuzhiyun size_t diff_offs; \
107*4882a593Smuzhiyun \
108*4882a593Smuzhiyun if (spill && spill == SPILL(src, _type) && \
109*4882a593Smuzhiyun sizeof(_type) - spill <= size) { \
110*4882a593Smuzhiyun spill = sizeof(_type) - spill; \
111*4882a593Smuzhiyun diff_offs = vmw_find_first_diff_u8(dst, src, spill); \
112*4882a593Smuzhiyun if (diff_offs < spill) \
113*4882a593Smuzhiyun return round_down(offset + diff_offs, granularity); \
114*4882a593Smuzhiyun \
115*4882a593Smuzhiyun dst += spill; \
116*4882a593Smuzhiyun src += spill; \
117*4882a593Smuzhiyun size -= spill; \
118*4882a593Smuzhiyun offset += spill; \
119*4882a593Smuzhiyun spill = 0; \
120*4882a593Smuzhiyun } \
121*4882a593Smuzhiyun if (!spill && !SPILL(src, _type)) { \
122*4882a593Smuzhiyun size_t to_copy = size & ~(sizeof(_type) - 1); \
123*4882a593Smuzhiyun \
124*4882a593Smuzhiyun diff_offs = vmw_find_first_diff_ ## _type \
125*4882a593Smuzhiyun ((_type *) dst, (_type *) src, to_copy); \
126*4882a593Smuzhiyun if (diff_offs >= size || granularity == sizeof(_type)) \
127*4882a593Smuzhiyun return (offset + diff_offs); \
128*4882a593Smuzhiyun \
129*4882a593Smuzhiyun dst += diff_offs; \
130*4882a593Smuzhiyun src += diff_offs; \
131*4882a593Smuzhiyun size -= diff_offs; \
132*4882a593Smuzhiyun offset += diff_offs; \
133*4882a593Smuzhiyun } \
134*4882a593Smuzhiyun } while (0) \
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun /**
138*4882a593Smuzhiyun * vmw_find_first_diff - find the first difference between dst and src
139*4882a593Smuzhiyun *
140*4882a593Smuzhiyun * @dst: The destination address
141*4882a593Smuzhiyun * @src: The source address
142*4882a593Smuzhiyun * @size: Number of bytes to compare
143*4882a593Smuzhiyun * @granularity: The granularity needed for the return value in bytes.
144*4882a593Smuzhiyun * return: The offset from find start where the first difference was
145*4882a593Smuzhiyun * encountered in bytes. If no difference was found, the function returns
146*4882a593Smuzhiyun * a value >= @size.
147*4882a593Smuzhiyun */
vmw_find_first_diff(const u8 * dst,const u8 * src,size_t size,size_t granularity)148*4882a593Smuzhiyun static size_t vmw_find_first_diff(const u8 *dst, const u8 *src, size_t size,
149*4882a593Smuzhiyun size_t granularity)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun size_t offset = 0;
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun /*
154*4882a593Smuzhiyun * Try finding with large integers if alignment allows, or we can
155*4882a593Smuzhiyun * fix it. Fall through if we need better resolution or alignment
156*4882a593Smuzhiyun * was bad.
157*4882a593Smuzhiyun */
158*4882a593Smuzhiyun #ifdef CONFIG_64BIT
159*4882a593Smuzhiyun VMW_TRY_FIND_FIRST_DIFF(u64);
160*4882a593Smuzhiyun #endif
161*4882a593Smuzhiyun VMW_TRY_FIND_FIRST_DIFF(u32);
162*4882a593Smuzhiyun VMW_TRY_FIND_FIRST_DIFF(u16);
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun return round_down(offset + vmw_find_first_diff_u8(dst, src, size),
165*4882a593Smuzhiyun granularity);
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun /*
170*4882a593Smuzhiyun * Template to compute find_last_diff() for a certain integer type
171*4882a593Smuzhiyun * including a tail copy for alignment, and adjustment of parameters
172*4882a593Smuzhiyun * for head find or increased resolution find using an unsigned integer find
173*4882a593Smuzhiyun * of smaller width. If finding is complete, and resolution is sufficient,
174*4882a593Smuzhiyun * the macro executes a return statement. Otherwise it falls through.
175*4882a593Smuzhiyun */
176*4882a593Smuzhiyun #define VMW_TRY_FIND_LAST_DIFF(_type) \
177*4882a593Smuzhiyun do { \
178*4882a593Smuzhiyun unsigned int spill = SPILL(dst, _type); \
179*4882a593Smuzhiyun ssize_t location; \
180*4882a593Smuzhiyun ssize_t diff_offs; \
181*4882a593Smuzhiyun \
182*4882a593Smuzhiyun if (spill && spill <= size && spill == SPILL(src, _type)) { \
183*4882a593Smuzhiyun diff_offs = vmw_find_last_diff_u8(dst, src, spill); \
184*4882a593Smuzhiyun if (diff_offs) { \
185*4882a593Smuzhiyun location = size - spill + diff_offs - 1; \
186*4882a593Smuzhiyun return round_down(location, granularity); \
187*4882a593Smuzhiyun } \
188*4882a593Smuzhiyun \
189*4882a593Smuzhiyun dst -= spill; \
190*4882a593Smuzhiyun src -= spill; \
191*4882a593Smuzhiyun size -= spill; \
192*4882a593Smuzhiyun spill = 0; \
193*4882a593Smuzhiyun } \
194*4882a593Smuzhiyun if (!spill && !SPILL(src, _type)) { \
195*4882a593Smuzhiyun size_t to_copy = round_down(size, sizeof(_type)); \
196*4882a593Smuzhiyun \
197*4882a593Smuzhiyun diff_offs = vmw_find_last_diff_ ## _type \
198*4882a593Smuzhiyun ((_type *) dst, (_type *) src, to_copy); \
199*4882a593Smuzhiyun location = size - to_copy + diff_offs - sizeof(_type); \
200*4882a593Smuzhiyun if (location < 0 || granularity == sizeof(_type)) \
201*4882a593Smuzhiyun return location; \
202*4882a593Smuzhiyun \
203*4882a593Smuzhiyun dst -= to_copy - diff_offs; \
204*4882a593Smuzhiyun src -= to_copy - diff_offs; \
205*4882a593Smuzhiyun size -= to_copy - diff_offs; \
206*4882a593Smuzhiyun } \
207*4882a593Smuzhiyun } while (0)
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun /**
211*4882a593Smuzhiyun * vmw_find_last_diff - find the last difference between dst and src
212*4882a593Smuzhiyun *
213*4882a593Smuzhiyun * @dst: The destination address
214*4882a593Smuzhiyun * @src: The source address
215*4882a593Smuzhiyun * @size: Number of bytes to compare
216*4882a593Smuzhiyun * @granularity: The granularity needed for the return value in bytes.
217*4882a593Smuzhiyun * return: The offset from find start where the last difference was
218*4882a593Smuzhiyun * encountered in bytes, or a negative value if no difference was found.
219*4882a593Smuzhiyun */
vmw_find_last_diff(const u8 * dst,const u8 * src,size_t size,size_t granularity)220*4882a593Smuzhiyun static ssize_t vmw_find_last_diff(const u8 *dst, const u8 *src, size_t size,
221*4882a593Smuzhiyun size_t granularity)
222*4882a593Smuzhiyun {
223*4882a593Smuzhiyun dst += size;
224*4882a593Smuzhiyun src += size;
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun #ifdef CONFIG_64BIT
227*4882a593Smuzhiyun VMW_TRY_FIND_LAST_DIFF(u64);
228*4882a593Smuzhiyun #endif
229*4882a593Smuzhiyun VMW_TRY_FIND_LAST_DIFF(u32);
230*4882a593Smuzhiyun VMW_TRY_FIND_LAST_DIFF(u16);
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun return round_down(vmw_find_last_diff_u8(dst, src, size) - 1,
233*4882a593Smuzhiyun granularity);
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun /**
238*4882a593Smuzhiyun * vmw_memcpy - A wrapper around kernel memcpy with allowing to plug it into a
239*4882a593Smuzhiyun * struct vmw_diff_cpy.
240*4882a593Smuzhiyun *
241*4882a593Smuzhiyun * @diff: The struct vmw_diff_cpy closure argument (unused).
242*4882a593Smuzhiyun * @dest: The copy destination.
243*4882a593Smuzhiyun * @src: The copy source.
244*4882a593Smuzhiyun * @n: Number of bytes to copy.
245*4882a593Smuzhiyun */
vmw_memcpy(struct vmw_diff_cpy * diff,u8 * dest,const u8 * src,size_t n)246*4882a593Smuzhiyun void vmw_memcpy(struct vmw_diff_cpy *diff, u8 *dest, const u8 *src, size_t n)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun memcpy(dest, src, n);
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun /**
253*4882a593Smuzhiyun * vmw_adjust_rect - Adjust rectangle coordinates for newly found difference
254*4882a593Smuzhiyun *
255*4882a593Smuzhiyun * @diff: The struct vmw_diff_cpy used to track the modified bounding box.
256*4882a593Smuzhiyun * @diff_offs: The offset from @diff->line_offset where the difference was
257*4882a593Smuzhiyun * found.
258*4882a593Smuzhiyun */
vmw_adjust_rect(struct vmw_diff_cpy * diff,size_t diff_offs)259*4882a593Smuzhiyun static void vmw_adjust_rect(struct vmw_diff_cpy *diff, size_t diff_offs)
260*4882a593Smuzhiyun {
261*4882a593Smuzhiyun size_t offs = (diff_offs + diff->line_offset) / diff->cpp;
262*4882a593Smuzhiyun struct drm_rect *rect = &diff->rect;
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun rect->x1 = min_t(int, rect->x1, offs);
265*4882a593Smuzhiyun rect->x2 = max_t(int, rect->x2, offs + 1);
266*4882a593Smuzhiyun rect->y1 = min_t(int, rect->y1, diff->line);
267*4882a593Smuzhiyun rect->y2 = max_t(int, rect->y2, diff->line + 1);
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun /**
271*4882a593Smuzhiyun * vmw_diff_memcpy - memcpy that creates a bounding box of modified content.
272*4882a593Smuzhiyun *
273*4882a593Smuzhiyun * @diff: The struct vmw_diff_cpy used to track the modified bounding box.
274*4882a593Smuzhiyun * @dest: The copy destination.
275*4882a593Smuzhiyun * @src: The copy source.
276*4882a593Smuzhiyun * @n: Number of bytes to copy.
277*4882a593Smuzhiyun *
278*4882a593Smuzhiyun * In order to correctly track the modified content, the field @diff->line must
279*4882a593Smuzhiyun * be pre-loaded with the current line number, the field @diff->line_offset must
280*4882a593Smuzhiyun * be pre-loaded with the line offset in bytes where the copy starts, and
281*4882a593Smuzhiyun * finally the field @diff->cpp need to be preloaded with the number of bytes
282*4882a593Smuzhiyun * per unit in the horizontal direction of the area we're examining.
283*4882a593Smuzhiyun * Typically bytes per pixel.
284*4882a593Smuzhiyun * This is needed to know the needed granularity of the difference computing
285*4882a593Smuzhiyun * operations. A higher cpp generally leads to faster execution at the cost of
286*4882a593Smuzhiyun * bounding box width precision.
287*4882a593Smuzhiyun */
vmw_diff_memcpy(struct vmw_diff_cpy * diff,u8 * dest,const u8 * src,size_t n)288*4882a593Smuzhiyun void vmw_diff_memcpy(struct vmw_diff_cpy *diff, u8 *dest, const u8 *src,
289*4882a593Smuzhiyun size_t n)
290*4882a593Smuzhiyun {
291*4882a593Smuzhiyun ssize_t csize, byte_len;
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun if (WARN_ON_ONCE(round_down(n, diff->cpp) != n))
294*4882a593Smuzhiyun return;
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun /* TODO: Possibly use a single vmw_find_first_diff per line? */
297*4882a593Smuzhiyun csize = vmw_find_first_diff(dest, src, n, diff->cpp);
298*4882a593Smuzhiyun if (csize < n) {
299*4882a593Smuzhiyun vmw_adjust_rect(diff, csize);
300*4882a593Smuzhiyun byte_len = diff->cpp;
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun /*
303*4882a593Smuzhiyun * Starting from where first difference was found, find
304*4882a593Smuzhiyun * location of last difference, and then copy.
305*4882a593Smuzhiyun */
306*4882a593Smuzhiyun diff->line_offset += csize;
307*4882a593Smuzhiyun dest += csize;
308*4882a593Smuzhiyun src += csize;
309*4882a593Smuzhiyun n -= csize;
310*4882a593Smuzhiyun csize = vmw_find_last_diff(dest, src, n, diff->cpp);
311*4882a593Smuzhiyun if (csize >= 0) {
312*4882a593Smuzhiyun byte_len += csize;
313*4882a593Smuzhiyun vmw_adjust_rect(diff, csize);
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun memcpy(dest, src, byte_len);
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun diff->line_offset += n;
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun /**
321*4882a593Smuzhiyun * struct vmw_bo_blit_line_data - Convenience argument to vmw_bo_cpu_blit_line
322*4882a593Smuzhiyun *
323*4882a593Smuzhiyun * @mapped_dst: Already mapped destination page index in @dst_pages.
324*4882a593Smuzhiyun * @dst_addr: Kernel virtual address of mapped destination page.
325*4882a593Smuzhiyun * @dst_pages: Array of destination bo pages.
326*4882a593Smuzhiyun * @dst_num_pages: Number of destination bo pages.
327*4882a593Smuzhiyun * @dst_prot: Destination bo page protection.
328*4882a593Smuzhiyun * @mapped_src: Already mapped source page index in @dst_pages.
329*4882a593Smuzhiyun * @src_addr: Kernel virtual address of mapped source page.
330*4882a593Smuzhiyun * @src_pages: Array of source bo pages.
331*4882a593Smuzhiyun * @src_num_pages: Number of source bo pages.
332*4882a593Smuzhiyun * @src_prot: Source bo page protection.
333*4882a593Smuzhiyun * @diff: Struct vmw_diff_cpy, in the end forwarded to the memcpy routine.
334*4882a593Smuzhiyun */
335*4882a593Smuzhiyun struct vmw_bo_blit_line_data {
336*4882a593Smuzhiyun u32 mapped_dst;
337*4882a593Smuzhiyun u8 *dst_addr;
338*4882a593Smuzhiyun struct page **dst_pages;
339*4882a593Smuzhiyun u32 dst_num_pages;
340*4882a593Smuzhiyun pgprot_t dst_prot;
341*4882a593Smuzhiyun u32 mapped_src;
342*4882a593Smuzhiyun u8 *src_addr;
343*4882a593Smuzhiyun struct page **src_pages;
344*4882a593Smuzhiyun u32 src_num_pages;
345*4882a593Smuzhiyun pgprot_t src_prot;
346*4882a593Smuzhiyun struct vmw_diff_cpy *diff;
347*4882a593Smuzhiyun };
348*4882a593Smuzhiyun
349*4882a593Smuzhiyun /**
350*4882a593Smuzhiyun * vmw_bo_cpu_blit_line - Blit part of a line from one bo to another.
351*4882a593Smuzhiyun *
352*4882a593Smuzhiyun * @d: Blit data as described above.
353*4882a593Smuzhiyun * @dst_offset: Destination copy start offset from start of bo.
354*4882a593Smuzhiyun * @src_offset: Source copy start offset from start of bo.
355*4882a593Smuzhiyun * @bytes_to_copy: Number of bytes to copy in this line.
356*4882a593Smuzhiyun */
vmw_bo_cpu_blit_line(struct vmw_bo_blit_line_data * d,u32 dst_offset,u32 src_offset,u32 bytes_to_copy)357*4882a593Smuzhiyun static int vmw_bo_cpu_blit_line(struct vmw_bo_blit_line_data *d,
358*4882a593Smuzhiyun u32 dst_offset,
359*4882a593Smuzhiyun u32 src_offset,
360*4882a593Smuzhiyun u32 bytes_to_copy)
361*4882a593Smuzhiyun {
362*4882a593Smuzhiyun struct vmw_diff_cpy *diff = d->diff;
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun while (bytes_to_copy) {
365*4882a593Smuzhiyun u32 copy_size = bytes_to_copy;
366*4882a593Smuzhiyun u32 dst_page = dst_offset >> PAGE_SHIFT;
367*4882a593Smuzhiyun u32 src_page = src_offset >> PAGE_SHIFT;
368*4882a593Smuzhiyun u32 dst_page_offset = dst_offset & ~PAGE_MASK;
369*4882a593Smuzhiyun u32 src_page_offset = src_offset & ~PAGE_MASK;
370*4882a593Smuzhiyun bool unmap_dst = d->dst_addr && dst_page != d->mapped_dst;
371*4882a593Smuzhiyun bool unmap_src = d->src_addr && (src_page != d->mapped_src ||
372*4882a593Smuzhiyun unmap_dst);
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun copy_size = min_t(u32, copy_size, PAGE_SIZE - dst_page_offset);
375*4882a593Smuzhiyun copy_size = min_t(u32, copy_size, PAGE_SIZE - src_page_offset);
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun if (unmap_src) {
378*4882a593Smuzhiyun kunmap_atomic(d->src_addr);
379*4882a593Smuzhiyun d->src_addr = NULL;
380*4882a593Smuzhiyun }
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun if (unmap_dst) {
383*4882a593Smuzhiyun kunmap_atomic(d->dst_addr);
384*4882a593Smuzhiyun d->dst_addr = NULL;
385*4882a593Smuzhiyun }
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun if (!d->dst_addr) {
388*4882a593Smuzhiyun if (WARN_ON_ONCE(dst_page >= d->dst_num_pages))
389*4882a593Smuzhiyun return -EINVAL;
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun d->dst_addr =
392*4882a593Smuzhiyun kmap_atomic_prot(d->dst_pages[dst_page],
393*4882a593Smuzhiyun d->dst_prot);
394*4882a593Smuzhiyun if (!d->dst_addr)
395*4882a593Smuzhiyun return -ENOMEM;
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun d->mapped_dst = dst_page;
398*4882a593Smuzhiyun }
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun if (!d->src_addr) {
401*4882a593Smuzhiyun if (WARN_ON_ONCE(src_page >= d->src_num_pages))
402*4882a593Smuzhiyun return -EINVAL;
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun d->src_addr =
405*4882a593Smuzhiyun kmap_atomic_prot(d->src_pages[src_page],
406*4882a593Smuzhiyun d->src_prot);
407*4882a593Smuzhiyun if (!d->src_addr)
408*4882a593Smuzhiyun return -ENOMEM;
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun d->mapped_src = src_page;
411*4882a593Smuzhiyun }
412*4882a593Smuzhiyun diff->do_cpy(diff, d->dst_addr + dst_page_offset,
413*4882a593Smuzhiyun d->src_addr + src_page_offset, copy_size);
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun bytes_to_copy -= copy_size;
416*4882a593Smuzhiyun dst_offset += copy_size;
417*4882a593Smuzhiyun src_offset += copy_size;
418*4882a593Smuzhiyun }
419*4882a593Smuzhiyun
420*4882a593Smuzhiyun return 0;
421*4882a593Smuzhiyun }
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun /**
424*4882a593Smuzhiyun * ttm_bo_cpu_blit - in-kernel cpu blit.
425*4882a593Smuzhiyun *
426*4882a593Smuzhiyun * @dst: Destination buffer object.
427*4882a593Smuzhiyun * @dst_offset: Destination offset of blit start in bytes.
428*4882a593Smuzhiyun * @dst_stride: Destination stride in bytes.
429*4882a593Smuzhiyun * @src: Source buffer object.
430*4882a593Smuzhiyun * @src_offset: Source offset of blit start in bytes.
431*4882a593Smuzhiyun * @src_stride: Source stride in bytes.
432*4882a593Smuzhiyun * @w: Width of blit.
433*4882a593Smuzhiyun * @h: Height of blit.
434*4882a593Smuzhiyun * return: Zero on success. Negative error value on failure. Will print out
435*4882a593Smuzhiyun * kernel warnings on caller bugs.
436*4882a593Smuzhiyun *
437*4882a593Smuzhiyun * Performs a CPU blit from one buffer object to another avoiding a full
438*4882a593Smuzhiyun * bo vmap which may exhaust- or fragment vmalloc space.
439*4882a593Smuzhiyun * On supported architectures (x86), we're using kmap_atomic which avoids
440*4882a593Smuzhiyun * cross-processor TLB- and cache flushes and may, on non-HIGHMEM systems
441*4882a593Smuzhiyun * reference already set-up mappings.
442*4882a593Smuzhiyun *
443*4882a593Smuzhiyun * Neither of the buffer objects may be placed in PCI memory
444*4882a593Smuzhiyun * (Fixed memory in TTM terminology) when using this function.
445*4882a593Smuzhiyun */
vmw_bo_cpu_blit(struct ttm_buffer_object * dst,u32 dst_offset,u32 dst_stride,struct ttm_buffer_object * src,u32 src_offset,u32 src_stride,u32 w,u32 h,struct vmw_diff_cpy * diff)446*4882a593Smuzhiyun int vmw_bo_cpu_blit(struct ttm_buffer_object *dst,
447*4882a593Smuzhiyun u32 dst_offset, u32 dst_stride,
448*4882a593Smuzhiyun struct ttm_buffer_object *src,
449*4882a593Smuzhiyun u32 src_offset, u32 src_stride,
450*4882a593Smuzhiyun u32 w, u32 h,
451*4882a593Smuzhiyun struct vmw_diff_cpy *diff)
452*4882a593Smuzhiyun {
453*4882a593Smuzhiyun struct ttm_operation_ctx ctx = {
454*4882a593Smuzhiyun .interruptible = false,
455*4882a593Smuzhiyun .no_wait_gpu = false
456*4882a593Smuzhiyun };
457*4882a593Smuzhiyun u32 j, initial_line = dst_offset / dst_stride;
458*4882a593Smuzhiyun struct vmw_bo_blit_line_data d;
459*4882a593Smuzhiyun int ret = 0;
460*4882a593Smuzhiyun
461*4882a593Smuzhiyun /* Buffer objects need to be either pinned or reserved: */
462*4882a593Smuzhiyun if (!(dst->mem.placement & TTM_PL_FLAG_NO_EVICT))
463*4882a593Smuzhiyun dma_resv_assert_held(dst->base.resv);
464*4882a593Smuzhiyun if (!(src->mem.placement & TTM_PL_FLAG_NO_EVICT))
465*4882a593Smuzhiyun dma_resv_assert_held(src->base.resv);
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun if (!ttm_tt_is_populated(dst->ttm)) {
468*4882a593Smuzhiyun ret = dst->bdev->driver->ttm_tt_populate(dst->bdev, dst->ttm, &ctx);
469*4882a593Smuzhiyun if (ret)
470*4882a593Smuzhiyun return ret;
471*4882a593Smuzhiyun }
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun if (!ttm_tt_is_populated(src->ttm)) {
474*4882a593Smuzhiyun ret = src->bdev->driver->ttm_tt_populate(src->bdev, src->ttm, &ctx);
475*4882a593Smuzhiyun if (ret)
476*4882a593Smuzhiyun return ret;
477*4882a593Smuzhiyun }
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun d.mapped_dst = 0;
480*4882a593Smuzhiyun d.mapped_src = 0;
481*4882a593Smuzhiyun d.dst_addr = NULL;
482*4882a593Smuzhiyun d.src_addr = NULL;
483*4882a593Smuzhiyun d.dst_pages = dst->ttm->pages;
484*4882a593Smuzhiyun d.src_pages = src->ttm->pages;
485*4882a593Smuzhiyun d.dst_num_pages = dst->num_pages;
486*4882a593Smuzhiyun d.src_num_pages = src->num_pages;
487*4882a593Smuzhiyun d.dst_prot = ttm_io_prot(dst->mem.placement, PAGE_KERNEL);
488*4882a593Smuzhiyun d.src_prot = ttm_io_prot(src->mem.placement, PAGE_KERNEL);
489*4882a593Smuzhiyun d.diff = diff;
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun for (j = 0; j < h; ++j) {
492*4882a593Smuzhiyun diff->line = j + initial_line;
493*4882a593Smuzhiyun diff->line_offset = dst_offset % dst_stride;
494*4882a593Smuzhiyun ret = vmw_bo_cpu_blit_line(&d, dst_offset, src_offset, w);
495*4882a593Smuzhiyun if (ret)
496*4882a593Smuzhiyun goto out;
497*4882a593Smuzhiyun
498*4882a593Smuzhiyun dst_offset += dst_stride;
499*4882a593Smuzhiyun src_offset += src_stride;
500*4882a593Smuzhiyun }
501*4882a593Smuzhiyun out:
502*4882a593Smuzhiyun if (d.src_addr)
503*4882a593Smuzhiyun kunmap_atomic(d.src_addr);
504*4882a593Smuzhiyun if (d.dst_addr)
505*4882a593Smuzhiyun kunmap_atomic(d.dst_addr);
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun return ret;
508*4882a593Smuzhiyun }
509