1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#include "vmwgfx_drv.h"
30#include <linux/highmem.h>
31
32
33
34
35
36#define VMW_FIND_FIRST_DIFF(_type) \
37static size_t vmw_find_first_diff_ ## _type \
38 (const _type * dst, const _type * src, size_t size)\
39{ \
40 size_t i; \
41 \
42 for (i = 0; i < size; i += sizeof(_type)) { \
43 if (*dst++ != *src++) \
44 break; \
45 } \
46 \
47 return i; \
48}
49
50
51
52
53
54
55
56
57#define VMW_FIND_LAST_DIFF(_type) \
58static ssize_t vmw_find_last_diff_ ## _type( \
59 const _type * dst, const _type * src, size_t size) \
60{ \
61 while (size) { \
62 if (*--dst != *--src) \
63 break; \
64 \
65 size -= sizeof(_type); \
66 } \
67 return size; \
68}
69
70
71
72
73
74
75
76
77VMW_FIND_FIRST_DIFF(u8);
78VMW_FIND_LAST_DIFF(u8);
79
80VMW_FIND_FIRST_DIFF(u16);
81VMW_FIND_LAST_DIFF(u16);
82
83VMW_FIND_FIRST_DIFF(u32);
84VMW_FIND_LAST_DIFF(u32);
85
86#ifdef CONFIG_64BIT
87VMW_FIND_FIRST_DIFF(u64);
88VMW_FIND_LAST_DIFF(u64);
89#endif
90
91
92
93#define SPILL(_var, _type) ((unsigned long) _var & (sizeof(_type) - 1))
94
95
96
97
98
99
100
101
102
103#define VMW_TRY_FIND_FIRST_DIFF(_type) \
104do { \
105 unsigned int spill = SPILL(dst, _type); \
106 size_t diff_offs; \
107 \
108 if (spill && spill == SPILL(src, _type) && \
109 sizeof(_type) - spill <= size) { \
110 spill = sizeof(_type) - spill; \
111 diff_offs = vmw_find_first_diff_u8(dst, src, spill); \
112 if (diff_offs < spill) \
113 return round_down(offset + diff_offs, granularity); \
114 \
115 dst += spill; \
116 src += spill; \
117 size -= spill; \
118 offset += spill; \
119 spill = 0; \
120 } \
121 if (!spill && !SPILL(src, _type)) { \
122 size_t to_copy = size & ~(sizeof(_type) - 1); \
123 \
124 diff_offs = vmw_find_first_diff_ ## _type \
125 ((_type *) dst, (_type *) src, to_copy); \
126 if (diff_offs >= size || granularity == sizeof(_type)) \
127 return (offset + diff_offs); \
128 \
129 dst += diff_offs; \
130 src += diff_offs; \
131 size -= diff_offs; \
132 offset += diff_offs; \
133 } \
134} while (0) \
135
136
137
138
139
140
141
142
143
144
145
146
147
148static size_t vmw_find_first_diff(const u8 *dst, const u8 *src, size_t size,
149 size_t granularity)
150{
151 size_t offset = 0;
152
153
154
155
156
157
158#ifdef CONFIG_64BIT
159 VMW_TRY_FIND_FIRST_DIFF(u64);
160#endif
161 VMW_TRY_FIND_FIRST_DIFF(u32);
162 VMW_TRY_FIND_FIRST_DIFF(u16);
163
164 return round_down(offset + vmw_find_first_diff_u8(dst, src, size),
165 granularity);
166}
167
168
169
170
171
172
173
174
175
176#define VMW_TRY_FIND_LAST_DIFF(_type) \
177do { \
178 unsigned int spill = SPILL(dst, _type); \
179 ssize_t location; \
180 ssize_t diff_offs; \
181 \
182 if (spill && spill <= size && spill == SPILL(src, _type)) { \
183 diff_offs = vmw_find_last_diff_u8(dst, src, spill); \
184 if (diff_offs) { \
185 location = size - spill + diff_offs - 1; \
186 return round_down(location, granularity); \
187 } \
188 \
189 dst -= spill; \
190 src -= spill; \
191 size -= spill; \
192 spill = 0; \
193 } \
194 if (!spill && !SPILL(src, _type)) { \
195 size_t to_copy = round_down(size, sizeof(_type)); \
196 \
197 diff_offs = vmw_find_last_diff_ ## _type \
198 ((_type *) dst, (_type *) src, to_copy); \
199 location = size - to_copy + diff_offs - sizeof(_type); \
200 if (location < 0 || granularity == sizeof(_type)) \
201 return location; \
202 \
203 dst -= to_copy - diff_offs; \
204 src -= to_copy - diff_offs; \
205 size -= to_copy - diff_offs; \
206 } \
207} while (0)
208
209
210
211
212
213
214
215
216
217
218
219
220static ssize_t vmw_find_last_diff(const u8 *dst, const u8 *src, size_t size,
221 size_t granularity)
222{
223 dst += size;
224 src += size;
225
226#ifdef CONFIG_64BIT
227 VMW_TRY_FIND_LAST_DIFF(u64);
228#endif
229 VMW_TRY_FIND_LAST_DIFF(u32);
230 VMW_TRY_FIND_LAST_DIFF(u16);
231
232 return round_down(vmw_find_last_diff_u8(dst, src, size) - 1,
233 granularity);
234}
235
236
237
238
239
240
241
242
243
244
245
246void vmw_memcpy(struct vmw_diff_cpy *diff, u8 *dest, const u8 *src, size_t n)
247{
248 memcpy(dest, src, n);
249}
250
251
252
253
254
255
256
257
258
259static void vmw_adjust_rect(struct vmw_diff_cpy *diff, size_t diff_offs)
260{
261 size_t offs = (diff_offs + diff->line_offset) / diff->cpp;
262 struct drm_rect *rect = &diff->rect;
263
264 rect->x1 = min_t(int, rect->x1, offs);
265 rect->x2 = max_t(int, rect->x2, offs + 1);
266 rect->y1 = min_t(int, rect->y1, diff->line);
267 rect->y2 = max_t(int, rect->y2, diff->line + 1);
268}
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288void vmw_diff_memcpy(struct vmw_diff_cpy *diff, u8 *dest, const u8 *src,
289 size_t n)
290{
291 ssize_t csize, byte_len;
292
293 if (WARN_ON_ONCE(round_down(n, diff->cpp) != n))
294 return;
295
296
297 csize = vmw_find_first_diff(dest, src, n, diff->cpp);
298 if (csize < n) {
299 vmw_adjust_rect(diff, csize);
300 byte_len = diff->cpp;
301
302
303
304
305
306 diff->line_offset += csize;
307 dest += csize;
308 src += csize;
309 n -= csize;
310 csize = vmw_find_last_diff(dest, src, n, diff->cpp);
311 if (csize >= 0) {
312 byte_len += csize;
313 vmw_adjust_rect(diff, csize);
314 }
315 memcpy(dest, src, byte_len);
316 }
317 diff->line_offset += n;
318}
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335struct vmw_bo_blit_line_data {
336 u32 mapped_dst;
337 u8 *dst_addr;
338 struct page **dst_pages;
339 u32 dst_num_pages;
340 pgprot_t dst_prot;
341 u32 mapped_src;
342 u8 *src_addr;
343 struct page **src_pages;
344 u32 src_num_pages;
345 pgprot_t src_prot;
346 struct vmw_diff_cpy *diff;
347};
348
349
350
351
352
353
354
355
356
357static int vmw_bo_cpu_blit_line(struct vmw_bo_blit_line_data *d,
358 u32 dst_offset,
359 u32 src_offset,
360 u32 bytes_to_copy)
361{
362 struct vmw_diff_cpy *diff = d->diff;
363
364 while (bytes_to_copy) {
365 u32 copy_size = bytes_to_copy;
366 u32 dst_page = dst_offset >> PAGE_SHIFT;
367 u32 src_page = src_offset >> PAGE_SHIFT;
368 u32 dst_page_offset = dst_offset & ~PAGE_MASK;
369 u32 src_page_offset = src_offset & ~PAGE_MASK;
370 bool unmap_dst = d->dst_addr && dst_page != d->mapped_dst;
371 bool unmap_src = d->src_addr && (src_page != d->mapped_src ||
372 unmap_dst);
373
374 copy_size = min_t(u32, copy_size, PAGE_SIZE - dst_page_offset);
375 copy_size = min_t(u32, copy_size, PAGE_SIZE - src_page_offset);
376
377 if (unmap_src) {
378 kunmap_atomic(d->src_addr);
379 d->src_addr = NULL;
380 }
381
382 if (unmap_dst) {
383 kunmap_atomic(d->dst_addr);
384 d->dst_addr = NULL;
385 }
386
387 if (!d->dst_addr) {
388 if (WARN_ON_ONCE(dst_page >= d->dst_num_pages))
389 return -EINVAL;
390
391 d->dst_addr =
392 kmap_atomic_prot(d->dst_pages[dst_page],
393 d->dst_prot);
394 if (!d->dst_addr)
395 return -ENOMEM;
396
397 d->mapped_dst = dst_page;
398 }
399
400 if (!d->src_addr) {
401 if (WARN_ON_ONCE(src_page >= d->src_num_pages))
402 return -EINVAL;
403
404 d->src_addr =
405 kmap_atomic_prot(d->src_pages[src_page],
406 d->src_prot);
407 if (!d->src_addr)
408 return -ENOMEM;
409
410 d->mapped_src = src_page;
411 }
412 diff->do_cpy(diff, d->dst_addr + dst_page_offset,
413 d->src_addr + src_page_offset, copy_size);
414
415 bytes_to_copy -= copy_size;
416 dst_offset += copy_size;
417 src_offset += copy_size;
418 }
419
420 return 0;
421}
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446int vmw_bo_cpu_blit(struct ttm_buffer_object *dst,
447 u32 dst_offset, u32 dst_stride,
448 struct ttm_buffer_object *src,
449 u32 src_offset, u32 src_stride,
450 u32 w, u32 h,
451 struct vmw_diff_cpy *diff)
452{
453 struct ttm_operation_ctx ctx = {
454 .interruptible = false,
455 .no_wait_gpu = false
456 };
457 u32 j, initial_line = dst_offset / dst_stride;
458 struct vmw_bo_blit_line_data d;
459 int ret = 0;
460
461
462 if (!(dst->mem.placement & TTM_PL_FLAG_NO_EVICT))
463 dma_resv_assert_held(dst->base.resv);
464 if (!(src->mem.placement & TTM_PL_FLAG_NO_EVICT))
465 dma_resv_assert_held(src->base.resv);
466
467 if (dst->ttm->state == tt_unpopulated) {
468 ret = dst->ttm->bdev->driver->ttm_tt_populate(dst->ttm, &ctx);
469 if (ret)
470 return ret;
471 }
472
473 if (src->ttm->state == tt_unpopulated) {
474 ret = src->ttm->bdev->driver->ttm_tt_populate(src->ttm, &ctx);
475 if (ret)
476 return ret;
477 }
478
479 d.mapped_dst = 0;
480 d.mapped_src = 0;
481 d.dst_addr = NULL;
482 d.src_addr = NULL;
483 d.dst_pages = dst->ttm->pages;
484 d.src_pages = src->ttm->pages;
485 d.dst_num_pages = dst->num_pages;
486 d.src_num_pages = src->num_pages;
487 d.dst_prot = ttm_io_prot(dst->mem.placement, PAGE_KERNEL);
488 d.src_prot = ttm_io_prot(src->mem.placement, PAGE_KERNEL);
489 d.diff = diff;
490
491 for (j = 0; j < h; ++j) {
492 diff->line = j + initial_line;
493 diff->line_offset = dst_offset % dst_stride;
494 ret = vmw_bo_cpu_blit_line(&d, dst_offset, src_offset, w);
495 if (ret)
496 goto out;
497
498 dst_offset += dst_stride;
499 src_offset += src_stride;
500 }
501out:
502 if (d.src_addr)
503 kunmap_atomic(d.src_addr);
504 if (d.dst_addr)
505 kunmap_atomic(d.dst_addr);
506
507 return ret;
508}
509