1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
4 * Copyright 2017 VMware, Inc., Palo Alto, CA., USA
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
29 #include "vmwgfx_drv.h"
32 * Template that implements find_first_diff() for a generic
33 * unsigned integer type. @size and return value are in bytes.
35 #define VMW_FIND_FIRST_DIFF(_type) \
36 static size_t vmw_find_first_diff_ ## _type \
37 (const _type * dst, const _type * src, size_t size)\
41 for (i = 0; i < size; i += sizeof(_type)) { \
42 if (*dst++ != *src++) \
51 * Template that implements find_last_diff() for a generic
52 * unsigned integer type. Pointers point to the item following the
53 * *end* of the area to be examined. @size and return value are in
56 #define VMW_FIND_LAST_DIFF(_type) \
57 static ssize_t vmw_find_last_diff_ ## _type( \
58 const _type * dst, const _type * src, size_t size) \
61 if (*--dst != *--src) \
64 size -= sizeof(_type); \
71 * Instantiate find diff functions for relevant unsigned integer sizes,
72 * assuming that wider integers are faster (including aligning) up to the
73 * architecture native width, which is assumed to be 32 bit unless
74 * CONFIG_64BIT is defined.
76 VMW_FIND_FIRST_DIFF(u8
);
77 VMW_FIND_LAST_DIFF(u8
);
79 VMW_FIND_FIRST_DIFF(u16
);
80 VMW_FIND_LAST_DIFF(u16
);
82 VMW_FIND_FIRST_DIFF(u32
);
83 VMW_FIND_LAST_DIFF(u32
);
86 VMW_FIND_FIRST_DIFF(u64
);
87 VMW_FIND_LAST_DIFF(u64
);
91 /* We use size aligned copies. This computes (addr - align(addr)) */
92 #define SPILL(_var, _type) ((unsigned long) _var & (sizeof(_type) - 1))
96 * Template to compute find_first_diff() for a certain integer type
97 * including a head copy for alignment, and adjustment of parameters
98 * for tail find or increased resolution find using an unsigned integer find
99 * of smaller width. If finding is complete, and resolution is sufficient,
100 * the macro executes a return statement. Otherwise it falls through.
102 #define VMW_TRY_FIND_FIRST_DIFF(_type) \
104 unsigned int spill = SPILL(dst, _type); \
107 if (spill && spill == SPILL(src, _type) && \
108 sizeof(_type) - spill <= size) { \
109 spill = sizeof(_type) - spill; \
110 diff_offs = vmw_find_first_diff_u8(dst, src, spill); \
111 if (diff_offs < spill) \
112 return round_down(offset + diff_offs, granularity); \
120 if (!spill && !SPILL(src, _type)) { \
121 size_t to_copy = size & ~(sizeof(_type) - 1); \
123 diff_offs = vmw_find_first_diff_ ## _type \
124 ((_type *) dst, (_type *) src, to_copy); \
125 if (diff_offs >= size || granularity == sizeof(_type)) \
126 return (offset + diff_offs); \
131 offset += diff_offs; \
137 * vmw_find_first_diff - find the first difference between dst and src
139 * @dst: The destination address
140 * @src: The source address
141 * @size: Number of bytes to compare
142 * @granularity: The granularity needed for the return value in bytes.
143 * return: The offset from find start where the first difference was
144 * encountered in bytes. If no difference was found, the function returns
147 static size_t vmw_find_first_diff(const u8
*dst
, const u8
*src
, size_t size
,
153 * Try finding with large integers if alignment allows, or we can
154 * fix it. Fall through if we need better resolution or alignment
158 VMW_TRY_FIND_FIRST_DIFF(u64
);
160 VMW_TRY_FIND_FIRST_DIFF(u32
);
161 VMW_TRY_FIND_FIRST_DIFF(u16
);
163 return round_down(offset
+ vmw_find_first_diff_u8(dst
, src
, size
),
169 * Template to compute find_last_diff() for a certain integer type
170 * including a tail copy for alignment, and adjustment of parameters
171 * for head find or increased resolution find using an unsigned integer find
172 * of smaller width. If finding is complete, and resolution is sufficient,
173 * the macro executes a return statement. Otherwise it falls through.
175 #define VMW_TRY_FIND_LAST_DIFF(_type) \
177 unsigned int spill = SPILL(dst, _type); \
181 if (spill && spill <= size && spill == SPILL(src, _type)) { \
182 diff_offs = vmw_find_last_diff_u8(dst, src, spill); \
184 location = size - spill + diff_offs - 1; \
185 return round_down(location, granularity); \
193 if (!spill && !SPILL(src, _type)) { \
194 size_t to_copy = round_down(size, sizeof(_type)); \
196 diff_offs = vmw_find_last_diff_ ## _type \
197 ((_type *) dst, (_type *) src, to_copy); \
198 location = size - to_copy + diff_offs - sizeof(_type); \
199 if (location < 0 || granularity == sizeof(_type)) \
202 dst -= to_copy - diff_offs; \
203 src -= to_copy - diff_offs; \
204 size -= to_copy - diff_offs; \
210 * vmw_find_last_diff - find the last difference between dst and src
212 * @dst: The destination address
213 * @src: The source address
214 * @size: Number of bytes to compare
215 * @granularity: The granularity needed for the return value in bytes.
216 * return: The offset from find start where the last difference was
217 * encountered in bytes, or a negative value if no difference was found.
219 static ssize_t
vmw_find_last_diff(const u8
*dst
, const u8
*src
, size_t size
,
226 VMW_TRY_FIND_LAST_DIFF(u64
);
228 VMW_TRY_FIND_LAST_DIFF(u32
);
229 VMW_TRY_FIND_LAST_DIFF(u16
);
231 return round_down(vmw_find_last_diff_u8(dst
, src
, size
) - 1,
237 * vmw_memcpy - A wrapper around kernel memcpy with allowing to plug it into a
238 * struct vmw_diff_cpy.
240 * @diff: The struct vmw_diff_cpy closure argument (unused).
241 * @dest: The copy destination.
242 * @src: The copy source.
243 * @n: Number of bytes to copy.
245 void vmw_memcpy(struct vmw_diff_cpy
*diff
, u8
*dest
, const u8
*src
, size_t n
)
247 memcpy(dest
, src
, n
);
252 * vmw_adjust_rect - Adjust rectangle coordinates for newly found difference
254 * @diff: The struct vmw_diff_cpy used to track the modified bounding box.
255 * @diff_offs: The offset from @diff->line_offset where the difference was
258 static void vmw_adjust_rect(struct vmw_diff_cpy
*diff
, size_t diff_offs
)
260 size_t offs
= (diff_offs
+ diff
->line_offset
) / diff
->cpp
;
261 struct drm_rect
*rect
= &diff
->rect
;
263 rect
->x1
= min_t(int, rect
->x1
, offs
);
264 rect
->x2
= max_t(int, rect
->x2
, offs
+ 1);
265 rect
->y1
= min_t(int, rect
->y1
, diff
->line
);
266 rect
->y2
= max_t(int, rect
->y2
, diff
->line
+ 1);
270 * vmw_diff_memcpy - memcpy that creates a bounding box of modified content.
272 * @diff: The struct vmw_diff_cpy used to track the modified bounding box.
273 * @dest: The copy destination.
274 * @src: The copy source.
275 * @n: Number of bytes to copy.
277 * In order to correctly track the modified content, the field @diff->line must
278 * be pre-loaded with the current line number, the field @diff->line_offset must
279 * be pre-loaded with the line offset in bytes where the copy starts, and
280 * finally the field @diff->cpp need to be preloaded with the number of bytes
281 * per unit in the horizontal direction of the area we're examining.
282 * Typically bytes per pixel.
283 * This is needed to know the needed granularity of the difference computing
284 * operations. A higher cpp generally leads to faster execution at the cost of
285 * bounding box width precision.
287 void vmw_diff_memcpy(struct vmw_diff_cpy
*diff
, u8
*dest
, const u8
*src
,
290 ssize_t csize
, byte_len
;
292 if (WARN_ON_ONCE(round_down(n
, diff
->cpp
) != n
))
295 /* TODO: Possibly use a single vmw_find_first_diff per line? */
296 csize
= vmw_find_first_diff(dest
, src
, n
, diff
->cpp
);
298 vmw_adjust_rect(diff
, csize
);
299 byte_len
= diff
->cpp
;
302 * Starting from where first difference was found, find
303 * location of last difference, and then copy.
305 diff
->line_offset
+= csize
;
309 csize
= vmw_find_last_diff(dest
, src
, n
, diff
->cpp
);
312 vmw_adjust_rect(diff
, csize
);
314 memcpy(dest
, src
, byte_len
);
316 diff
->line_offset
+= n
;
320 * struct vmw_bo_blit_line_data - Convenience argument to vmw_bo_cpu_blit_line
322 * @mapped_dst: Already mapped destination page index in @dst_pages.
323 * @dst_addr: Kernel virtual address of mapped destination page.
324 * @dst_pages: Array of destination bo pages.
325 * @dst_num_pages: Number of destination bo pages.
326 * @dst_prot: Destination bo page protection.
327 * @mapped_src: Already mapped source page index in @dst_pages.
328 * @src_addr: Kernel virtual address of mapped source page.
329 * @src_pages: Array of source bo pages.
330 * @src_num_pages: Number of source bo pages.
331 * @src_prot: Source bo page protection.
332 * @diff: Struct vmw_diff_cpy, in the end forwarded to the memcpy routine.
334 struct vmw_bo_blit_line_data
{
337 struct page
**dst_pages
;
342 struct page
**src_pages
;
345 struct vmw_diff_cpy
*diff
;
349 * vmw_bo_cpu_blit_line - Blit part of a line from one bo to another.
351 * @d: Blit data as described above.
352 * @dst_offset: Destination copy start offset from start of bo.
353 * @src_offset: Source copy start offset from start of bo.
354 * @bytes_to_copy: Number of bytes to copy in this line.
356 static int vmw_bo_cpu_blit_line(struct vmw_bo_blit_line_data
*d
,
361 struct vmw_diff_cpy
*diff
= d
->diff
;
363 while (bytes_to_copy
) {
364 u32 copy_size
= bytes_to_copy
;
365 u32 dst_page
= dst_offset
>> PAGE_SHIFT
;
366 u32 src_page
= src_offset
>> PAGE_SHIFT
;
367 u32 dst_page_offset
= dst_offset
& ~PAGE_MASK
;
368 u32 src_page_offset
= src_offset
& ~PAGE_MASK
;
369 bool unmap_dst
= d
->dst_addr
&& dst_page
!= d
->mapped_dst
;
370 bool unmap_src
= d
->src_addr
&& (src_page
!= d
->mapped_src
||
373 copy_size
= min_t(u32
, copy_size
, PAGE_SIZE
- dst_page_offset
);
374 copy_size
= min_t(u32
, copy_size
, PAGE_SIZE
- src_page_offset
);
377 ttm_kunmap_atomic_prot(d
->src_addr
, d
->src_prot
);
382 ttm_kunmap_atomic_prot(d
->dst_addr
, d
->dst_prot
);
387 if (WARN_ON_ONCE(dst_page
>= d
->dst_num_pages
))
391 ttm_kmap_atomic_prot(d
->dst_pages
[dst_page
],
396 d
->mapped_dst
= dst_page
;
400 if (WARN_ON_ONCE(src_page
>= d
->src_num_pages
))
404 ttm_kmap_atomic_prot(d
->src_pages
[src_page
],
409 d
->mapped_src
= src_page
;
411 diff
->do_cpy(diff
, d
->dst_addr
+ dst_page_offset
,
412 d
->src_addr
+ src_page_offset
, copy_size
);
414 bytes_to_copy
-= copy_size
;
415 dst_offset
+= copy_size
;
416 src_offset
+= copy_size
;
423 * ttm_bo_cpu_blit - in-kernel cpu blit.
425 * @dst: Destination buffer object.
426 * @dst_offset: Destination offset of blit start in bytes.
427 * @dst_stride: Destination stride in bytes.
428 * @src: Source buffer object.
429 * @src_offset: Source offset of blit start in bytes.
430 * @src_stride: Source stride in bytes.
432 * @h: Height of blit.
433 * return: Zero on success. Negative error value on failure. Will print out
434 * kernel warnings on caller bugs.
436 * Performs a CPU blit from one buffer object to another avoiding a full
437 * bo vmap which may exhaust- or fragment vmalloc space.
438 * On supported architectures (x86), we're using kmap_atomic which avoids
439 * cross-processor TLB- and cache flushes and may, on non-HIGHMEM systems
440 * reference already set-up mappings.
442 * Neither of the buffer objects may be placed in PCI memory
443 * (Fixed memory in TTM terminology) when using this function.
445 int vmw_bo_cpu_blit(struct ttm_buffer_object
*dst
,
446 u32 dst_offset
, u32 dst_stride
,
447 struct ttm_buffer_object
*src
,
448 u32 src_offset
, u32 src_stride
,
450 struct vmw_diff_cpy
*diff
)
452 struct ttm_operation_ctx ctx
= {
453 .interruptible
= false,
456 u32 j
, initial_line
= dst_offset
/ dst_stride
;
457 struct vmw_bo_blit_line_data d
;
460 /* Buffer objects need to be either pinned or reserved: */
461 if (!(dst
->mem
.placement
& TTM_PL_FLAG_NO_EVICT
))
462 lockdep_assert_held(&dst
->resv
->lock
.base
);
463 if (!(src
->mem
.placement
& TTM_PL_FLAG_NO_EVICT
))
464 lockdep_assert_held(&src
->resv
->lock
.base
);
466 if (dst
->ttm
->state
== tt_unpopulated
) {
467 ret
= dst
->ttm
->bdev
->driver
->ttm_tt_populate(dst
->ttm
, &ctx
);
472 if (src
->ttm
->state
== tt_unpopulated
) {
473 ret
= src
->ttm
->bdev
->driver
->ttm_tt_populate(src
->ttm
, &ctx
);
482 d
.dst_pages
= dst
->ttm
->pages
;
483 d
.src_pages
= src
->ttm
->pages
;
484 d
.dst_num_pages
= dst
->num_pages
;
485 d
.src_num_pages
= src
->num_pages
;
486 d
.dst_prot
= ttm_io_prot(dst
->mem
.placement
, PAGE_KERNEL
);
487 d
.src_prot
= ttm_io_prot(src
->mem
.placement
, PAGE_KERNEL
);
490 for (j
= 0; j
< h
; ++j
) {
491 diff
->line
= j
+ initial_line
;
492 diff
->line_offset
= dst_offset
% dst_stride
;
493 ret
= vmw_bo_cpu_blit_line(&d
, dst_offset
, src_offset
, w
);
497 dst_offset
+= dst_stride
;
498 src_offset
+= src_stride
;
502 ttm_kunmap_atomic_prot(d
.src_addr
, d
.src_prot
);
504 ttm_kunmap_atomic_prot(d
.dst_addr
, d
.dst_prot
);