vmwgfx_blit.c 15 KB

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