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"
31 #include "vmwgfx_bo.h"
32 #include <linux/highmem.h>
35 * Template that implements find_first_diff() for a generic
36 * unsigned integer type. @size and return value are in bytes.
38 #define VMW_FIND_FIRST_DIFF(_type) \
39 static size_t vmw_find_first_diff_ ## _type \
40 (const _type * dst, const _type * src, size_t size)\
44 for (i = 0; i < size; i += sizeof(_type)) { \
45 if (*dst++ != *src++) \
54 * Template that implements find_last_diff() for a generic
55 * unsigned integer type. Pointers point to the item following the
56 * *end* of the area to be examined. @size and return value are in
59 #define VMW_FIND_LAST_DIFF(_type) \
60 static ssize_t vmw_find_last_diff_ ## _type( \
61 const _type * dst, const _type * src, size_t size) \
64 if (*--dst != *--src) \
67 size -= sizeof(_type); \
74 * Instantiate find diff functions for relevant unsigned integer sizes,
75 * assuming that wider integers are faster (including aligning) up to the
76 * architecture native width, which is assumed to be 32 bit unless
77 * CONFIG_64BIT is defined.
79 VMW_FIND_FIRST_DIFF(u8);
80 VMW_FIND_LAST_DIFF(u8);
82 VMW_FIND_FIRST_DIFF(u16);
83 VMW_FIND_LAST_DIFF(u16);
85 VMW_FIND_FIRST_DIFF(u32);
86 VMW_FIND_LAST_DIFF(u32);
89 VMW_FIND_FIRST_DIFF(u64);
90 VMW_FIND_LAST_DIFF(u64);
94 /* We use size aligned copies. This computes (addr - align(addr)) */
95 #define SPILL(_var, _type) ((unsigned long) _var & (sizeof(_type) - 1))
99 * Template to compute find_first_diff() for a certain integer type
100 * including a head copy for alignment, and adjustment of parameters
101 * for tail find or increased resolution find using an unsigned integer find
102 * of smaller width. If finding is complete, and resolution is sufficient,
103 * the macro executes a return statement. Otherwise it falls through.
105 #define VMW_TRY_FIND_FIRST_DIFF(_type) \
107 unsigned int spill = SPILL(dst, _type); \
110 if (spill && spill == SPILL(src, _type) && \
111 sizeof(_type) - spill <= size) { \
112 spill = sizeof(_type) - spill; \
113 diff_offs = vmw_find_first_diff_u8(dst, src, spill); \
114 if (diff_offs < spill) \
115 return round_down(offset + diff_offs, granularity); \
123 if (!spill && !SPILL(src, _type)) { \
124 size_t to_copy = size & ~(sizeof(_type) - 1); \
126 diff_offs = vmw_find_first_diff_ ## _type \
127 ((_type *) dst, (_type *) src, to_copy); \
128 if (diff_offs >= size || granularity == sizeof(_type)) \
129 return (offset + diff_offs); \
134 offset += diff_offs; \
140 * vmw_find_first_diff - find the first difference between dst and src
142 * @dst: The destination address
143 * @src: The source address
144 * @size: Number of bytes to compare
145 * @granularity: The granularity needed for the return value in bytes.
146 * return: The offset from find start where the first difference was
147 * encountered in bytes. If no difference was found, the function returns
150 static size_t vmw_find_first_diff(const u8 *dst, const u8 *src, size_t size,
156 * Try finding with large integers if alignment allows, or we can
157 * fix it. Fall through if we need better resolution or alignment
161 VMW_TRY_FIND_FIRST_DIFF(u64);
163 VMW_TRY_FIND_FIRST_DIFF(u32);
164 VMW_TRY_FIND_FIRST_DIFF(u16);
166 return round_down(offset + vmw_find_first_diff_u8(dst, src, size),
172 * Template to compute find_last_diff() for a certain integer type
173 * including a tail copy for alignment, and adjustment of parameters
174 * for head find or increased resolution find using an unsigned integer find
175 * of smaller width. If finding is complete, and resolution is sufficient,
176 * the macro executes a return statement. Otherwise it falls through.
178 #define VMW_TRY_FIND_LAST_DIFF(_type) \
180 unsigned int spill = SPILL(dst, _type); \
184 if (spill && spill <= size && spill == SPILL(src, _type)) { \
185 diff_offs = vmw_find_last_diff_u8(dst, src, spill); \
187 location = size - spill + diff_offs - 1; \
188 return round_down(location, granularity); \
196 if (!spill && !SPILL(src, _type)) { \
197 size_t to_copy = round_down(size, sizeof(_type)); \
199 diff_offs = vmw_find_last_diff_ ## _type \
200 ((_type *) dst, (_type *) src, to_copy); \
201 location = size - to_copy + diff_offs - sizeof(_type); \
202 if (location < 0 || granularity == sizeof(_type)) \
205 dst -= to_copy - diff_offs; \
206 src -= to_copy - diff_offs; \
207 size -= to_copy - diff_offs; \
213 * vmw_find_last_diff - find the last difference between dst and src
215 * @dst: The destination address
216 * @src: The source address
217 * @size: Number of bytes to compare
218 * @granularity: The granularity needed for the return value in bytes.
219 * return: The offset from find start where the last difference was
220 * encountered in bytes, or a negative value if no difference was found.
222 static ssize_t vmw_find_last_diff(const u8 *dst, const u8 *src, size_t size,
229 VMW_TRY_FIND_LAST_DIFF(u64);
231 VMW_TRY_FIND_LAST_DIFF(u32);
232 VMW_TRY_FIND_LAST_DIFF(u16);
234 return round_down(vmw_find_last_diff_u8(dst, src, size) - 1,
240 * vmw_memcpy - A wrapper around kernel memcpy with allowing to plug it into a
241 * struct vmw_diff_cpy.
243 * @diff: The struct vmw_diff_cpy closure argument (unused).
244 * @dest: The copy destination.
245 * @src: The copy source.
246 * @n: Number of bytes to copy.
248 void vmw_memcpy(struct vmw_diff_cpy *diff, u8 *dest, const u8 *src, size_t n)
250 memcpy(dest, src, n);
255 * vmw_adjust_rect - Adjust rectangle coordinates for newly found difference
257 * @diff: The struct vmw_diff_cpy used to track the modified bounding box.
258 * @diff_offs: The offset from @diff->line_offset where the difference was
261 static void vmw_adjust_rect(struct vmw_diff_cpy *diff, size_t diff_offs)
263 size_t offs = (diff_offs + diff->line_offset) / diff->cpp;
264 struct drm_rect *rect = &diff->rect;
266 rect->x1 = min_t(int, rect->x1, offs);
267 rect->x2 = max_t(int, rect->x2, offs + 1);
268 rect->y1 = min_t(int, rect->y1, diff->line);
269 rect->y2 = max_t(int, rect->y2, diff->line + 1);
273 * vmw_diff_memcpy - memcpy that creates a bounding box of modified content.
275 * @diff: The struct vmw_diff_cpy used to track the modified bounding box.
276 * @dest: The copy destination.
277 * @src: The copy source.
278 * @n: Number of bytes to copy.
280 * In order to correctly track the modified content, the field @diff->line must
281 * be pre-loaded with the current line number, the field @diff->line_offset must
282 * be pre-loaded with the line offset in bytes where the copy starts, and
283 * finally the field @diff->cpp need to be preloaded with the number of bytes
284 * per unit in the horizontal direction of the area we're examining.
285 * Typically bytes per pixel.
286 * This is needed to know the needed granularity of the difference computing
287 * operations. A higher cpp generally leads to faster execution at the cost of
288 * bounding box width precision.
290 void vmw_diff_memcpy(struct vmw_diff_cpy *diff, u8 *dest, const u8 *src,
293 ssize_t csize, byte_len;
295 if (WARN_ON_ONCE(round_down(n, diff->cpp) != n))
298 /* TODO: Possibly use a single vmw_find_first_diff per line? */
299 csize = vmw_find_first_diff(dest, src, n, diff->cpp);
301 vmw_adjust_rect(diff, csize);
302 byte_len = diff->cpp;
305 * Starting from where first difference was found, find
306 * location of last difference, and then copy.
308 diff->line_offset += csize;
312 csize = vmw_find_last_diff(dest, src, n, diff->cpp);
315 vmw_adjust_rect(diff, csize);
317 memcpy(dest, src, byte_len);
319 diff->line_offset += n;
323 * struct vmw_bo_blit_line_data - Convenience argument to vmw_bo_cpu_blit_line
325 * @mapped_dst: Already mapped destination page index in @dst_pages.
326 * @dst_addr: Kernel virtual address of mapped destination page.
327 * @dst_pages: Array of destination bo pages.
328 * @dst_num_pages: Number of destination bo pages.
329 * @dst_prot: Destination bo page protection.
330 * @mapped_src: Already mapped source page index in @dst_pages.
331 * @src_addr: Kernel virtual address of mapped source page.
332 * @src_pages: Array of source bo pages.
333 * @src_num_pages: Number of source bo pages.
334 * @src_prot: Source bo page protection.
335 * @diff: Struct vmw_diff_cpy, in the end forwarded to the memcpy routine.
337 struct vmw_bo_blit_line_data {
340 struct page **dst_pages;
345 struct page **src_pages;
348 struct vmw_diff_cpy *diff;
352 * vmw_bo_cpu_blit_line - Blit part of a line from one bo to another.
354 * @d: Blit data as described above.
355 * @dst_offset: Destination copy start offset from start of bo.
356 * @src_offset: Source copy start offset from start of bo.
357 * @bytes_to_copy: Number of bytes to copy in this line.
359 static int vmw_bo_cpu_blit_line(struct vmw_bo_blit_line_data *d,
364 struct vmw_diff_cpy *diff = d->diff;
366 while (bytes_to_copy) {
367 u32 copy_size = bytes_to_copy;
368 u32 dst_page = dst_offset >> PAGE_SHIFT;
369 u32 src_page = src_offset >> PAGE_SHIFT;
370 u32 dst_page_offset = dst_offset & ~PAGE_MASK;
371 u32 src_page_offset = src_offset & ~PAGE_MASK;
372 bool unmap_dst = d->dst_addr && dst_page != d->mapped_dst;
373 bool unmap_src = d->src_addr && (src_page != d->mapped_src ||
376 copy_size = min_t(u32, copy_size, PAGE_SIZE - dst_page_offset);
377 copy_size = min_t(u32, copy_size, PAGE_SIZE - src_page_offset);
380 kunmap_atomic(d->src_addr);
385 kunmap_atomic(d->dst_addr);
390 if (WARN_ON_ONCE(dst_page >= d->dst_num_pages))
394 kmap_atomic_prot(d->dst_pages[dst_page],
399 d->mapped_dst = dst_page;
403 if (WARN_ON_ONCE(src_page >= d->src_num_pages))
407 kmap_atomic_prot(d->src_pages[src_page],
412 d->mapped_src = src_page;
414 diff->do_cpy(diff, d->dst_addr + dst_page_offset,
415 d->src_addr + src_page_offset, copy_size);
417 bytes_to_copy -= copy_size;
418 dst_offset += copy_size;
419 src_offset += copy_size;
425 static void *map_external(struct vmw_bo *bo, struct iosys_map *map)
427 struct vmw_private *vmw =
428 container_of(bo->tbo.bdev, struct vmw_private, bdev);
432 if (bo->tbo.base.import_attach) {
433 ret = dma_buf_vmap(bo->tbo.base.dma_buf, map);
435 drm_dbg_driver(&vmw->drm,
436 "Wasn't able to map external bo!\n");
441 ptr = vmw_bo_map_and_cache(bo);
448 static void unmap_external(struct vmw_bo *bo, struct iosys_map *map)
450 if (bo->tbo.base.import_attach)
451 dma_buf_vunmap(bo->tbo.base.dma_buf, map);
456 static int vmw_external_bo_copy(struct vmw_bo *dst, u32 dst_offset,
457 u32 dst_stride, struct vmw_bo *src,
458 u32 src_offset, u32 src_stride,
459 u32 width_in_bytes, u32 height,
460 struct vmw_diff_cpy *diff)
462 struct vmw_private *vmw =
463 container_of(dst->tbo.bdev, struct vmw_private, bdev);
464 size_t dst_size = dst->tbo.resource->size;
465 size_t src_size = src->tbo.resource->size;
466 struct iosys_map dst_map = {0};
467 struct iosys_map src_map = {0};
473 vsrc = map_external(src, &src_map);
475 drm_dbg_driver(&vmw->drm, "Wasn't able to map src\n");
480 vdst = map_external(dst, &dst_map);
482 drm_dbg_driver(&vmw->drm, "Wasn't able to map dst\n");
489 if (src_stride == dst_stride) {
490 dst_size -= dst_offset;
491 src_size -= src_offset;
493 min(dst_stride * height, min(dst_size, src_size)));
495 WARN_ON(dst_stride < width_in_bytes);
496 for (i = 0; i < height; ++i) {
497 memcpy(vdst, vsrc, width_in_bytes);
503 x_in_bytes = (dst_offset % dst_stride);
504 diff->rect.x1 = x_in_bytes / diff->cpp;
505 diff->rect.y1 = ((dst_offset - x_in_bytes) / dst_stride);
506 diff->rect.x2 = diff->rect.x1 + width_in_bytes / diff->cpp;
507 diff->rect.y2 = diff->rect.y1 + height;
511 unmap_external(src, &src_map);
512 unmap_external(dst, &dst_map);
518 * vmw_bo_cpu_blit - in-kernel cpu blit.
520 * @vmw_dst: Destination buffer object.
521 * @dst_offset: Destination offset of blit start in bytes.
522 * @dst_stride: Destination stride in bytes.
523 * @vmw_src: Source buffer object.
524 * @src_offset: Source offset of blit start in bytes.
525 * @src_stride: Source stride in bytes.
527 * @h: Height of blit.
528 * @diff: The struct vmw_diff_cpy used to track the modified bounding box.
529 * return: Zero on success. Negative error value on failure. Will print out
530 * kernel warnings on caller bugs.
532 * Performs a CPU blit from one buffer object to another avoiding a full
533 * bo vmap which may exhaust- or fragment vmalloc space.
534 * On supported architectures (x86), we're using kmap_atomic which avoids
535 * cross-processor TLB- and cache flushes and may, on non-HIGHMEM systems
536 * reference already set-up mappings.
538 * Neither of the buffer objects may be placed in PCI memory
539 * (Fixed memory in TTM terminology) when using this function.
541 int vmw_bo_cpu_blit(struct vmw_bo *vmw_dst,
542 u32 dst_offset, u32 dst_stride,
543 struct vmw_bo *vmw_src,
544 u32 src_offset, u32 src_stride,
546 struct vmw_diff_cpy *diff)
548 struct ttm_buffer_object *src = &vmw_src->tbo;
549 struct ttm_buffer_object *dst = &vmw_dst->tbo;
550 struct ttm_operation_ctx ctx = {
551 .interruptible = false,
554 u32 j, initial_line = dst_offset / dst_stride;
555 struct vmw_bo_blit_line_data d = {0};
557 struct page **dst_pages = NULL;
558 struct page **src_pages = NULL;
559 bool src_external = (src->ttm->page_flags & TTM_TT_FLAG_EXTERNAL) != 0;
560 bool dst_external = (dst->ttm->page_flags & TTM_TT_FLAG_EXTERNAL) != 0;
562 if (WARN_ON(dst == src))
565 /* Buffer objects need to be either pinned or reserved: */
566 if (!(dst->pin_count))
567 dma_resv_assert_held(dst->base.resv);
568 if (!(src->pin_count))
569 dma_resv_assert_held(src->base.resv);
571 if (!ttm_tt_is_populated(dst->ttm)) {
572 ret = dst->bdev->funcs->ttm_tt_populate(dst->bdev, dst->ttm, &ctx);
577 if (!ttm_tt_is_populated(src->ttm)) {
578 ret = src->bdev->funcs->ttm_tt_populate(src->bdev, src->ttm, &ctx);
583 if (src_external || dst_external)
584 return vmw_external_bo_copy(vmw_dst, dst_offset, dst_stride,
585 vmw_src, src_offset, src_stride,
588 if (!src->ttm->pages && src->ttm->sg) {
589 src_pages = kvmalloc_array(src->ttm->num_pages,
590 sizeof(struct page *), GFP_KERNEL);
593 ret = drm_prime_sg_to_page_array(src->ttm->sg, src_pages,
594 src->ttm->num_pages);
598 if (!dst->ttm->pages && dst->ttm->sg) {
599 dst_pages = kvmalloc_array(dst->ttm->num_pages,
600 sizeof(struct page *), GFP_KERNEL);
605 ret = drm_prime_sg_to_page_array(dst->ttm->sg, dst_pages,
606 dst->ttm->num_pages);
615 d.dst_pages = dst->ttm->pages ? dst->ttm->pages : dst_pages;
616 d.src_pages = src->ttm->pages ? src->ttm->pages : src_pages;
617 d.dst_num_pages = PFN_UP(dst->resource->size);
618 d.src_num_pages = PFN_UP(src->resource->size);
619 d.dst_prot = ttm_io_prot(dst, dst->resource, PAGE_KERNEL);
620 d.src_prot = ttm_io_prot(src, src->resource, PAGE_KERNEL);
623 for (j = 0; j < h; ++j) {
624 diff->line = j + initial_line;
625 diff->line_offset = dst_offset % dst_stride;
626 ret = vmw_bo_cpu_blit_line(&d, dst_offset, src_offset, w);
630 dst_offset += dst_stride;
631 src_offset += src_stride;
635 kunmap_atomic(d.src_addr);
637 kunmap_atomic(d.dst_addr);