vmwgfx_blit.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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. #include "vmwgfx_bo.h"
  30. #include <linux/highmem.h>
  31. /*
  32. * Template that implements find_first_diff() for a generic
  33. * unsigned integer type. @size and return value are in bytes.
  34. */
  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)\
  38. { \
  39. size_t i; \
  40. \
  41. for (i = 0; i < size; i += sizeof(_type)) { \
  42. if (*dst++ != *src++) \
  43. break; \
  44. } \
  45. \
  46. return i; \
  47. }
  48. /*
  49. * Template that implements find_last_diff() for a generic
  50. * unsigned integer type. Pointers point to the item following the
  51. * *end* of the area to be examined. @size and return value are in
  52. * bytes.
  53. */
  54. #define VMW_FIND_LAST_DIFF(_type) \
  55. static ssize_t vmw_find_last_diff_ ## _type( \
  56. const _type * dst, const _type * src, size_t size) \
  57. { \
  58. while (size) { \
  59. if (*--dst != *--src) \
  60. break; \
  61. \
  62. size -= sizeof(_type); \
  63. } \
  64. return size; \
  65. }
  66. /*
  67. * Instantiate find diff functions for relevant unsigned integer sizes,
  68. * assuming that wider integers are faster (including aligning) up to the
  69. * architecture native width, which is assumed to be 32 bit unless
  70. * CONFIG_64BIT is defined.
  71. */
  72. VMW_FIND_FIRST_DIFF(u8);
  73. VMW_FIND_LAST_DIFF(u8);
  74. VMW_FIND_FIRST_DIFF(u16);
  75. VMW_FIND_LAST_DIFF(u16);
  76. VMW_FIND_FIRST_DIFF(u32);
  77. VMW_FIND_LAST_DIFF(u32);
  78. #ifdef CONFIG_64BIT
  79. VMW_FIND_FIRST_DIFF(u64);
  80. VMW_FIND_LAST_DIFF(u64);
  81. #endif
  82. /* We use size aligned copies. This computes (addr - align(addr)) */
  83. #define SPILL(_var, _type) ((unsigned long) _var & (sizeof(_type) - 1))
  84. /*
  85. * Template to compute find_first_diff() for a certain integer type
  86. * including a head copy for alignment, and adjustment of parameters
  87. * for tail find or increased resolution find using an unsigned integer find
  88. * of smaller width. If finding is complete, and resolution is sufficient,
  89. * the macro executes a return statement. Otherwise it falls through.
  90. */
  91. #define VMW_TRY_FIND_FIRST_DIFF(_type) \
  92. do { \
  93. unsigned int spill = SPILL(dst, _type); \
  94. size_t diff_offs; \
  95. \
  96. if (spill && spill == SPILL(src, _type) && \
  97. sizeof(_type) - spill <= size) { \
  98. spill = sizeof(_type) - spill; \
  99. diff_offs = vmw_find_first_diff_u8(dst, src, spill); \
  100. if (diff_offs < spill) \
  101. return round_down(offset + diff_offs, granularity); \
  102. \
  103. dst += spill; \
  104. src += spill; \
  105. size -= spill; \
  106. offset += spill; \
  107. spill = 0; \
  108. } \
  109. if (!spill && !SPILL(src, _type)) { \
  110. size_t to_copy = size & ~(sizeof(_type) - 1); \
  111. \
  112. diff_offs = vmw_find_first_diff_ ## _type \
  113. ((_type *) dst, (_type *) src, to_copy); \
  114. if (diff_offs >= size || granularity == sizeof(_type)) \
  115. return (offset + diff_offs); \
  116. \
  117. dst += diff_offs; \
  118. src += diff_offs; \
  119. size -= diff_offs; \
  120. offset += diff_offs; \
  121. } \
  122. } while (0) \
  123. /**
  124. * vmw_find_first_diff - find the first difference between dst and src
  125. *
  126. * @dst: The destination address
  127. * @src: The source address
  128. * @size: Number of bytes to compare
  129. * @granularity: The granularity needed for the return value in bytes.
  130. * return: The offset from find start where the first difference was
  131. * encountered in bytes. If no difference was found, the function returns
  132. * a value >= @size.
  133. */
  134. static size_t vmw_find_first_diff(const u8 *dst, const u8 *src, size_t size,
  135. size_t granularity)
  136. {
  137. size_t offset = 0;
  138. /*
  139. * Try finding with large integers if alignment allows, or we can
  140. * fix it. Fall through if we need better resolution or alignment
  141. * was bad.
  142. */
  143. #ifdef CONFIG_64BIT
  144. VMW_TRY_FIND_FIRST_DIFF(u64);
  145. #endif
  146. VMW_TRY_FIND_FIRST_DIFF(u32);
  147. VMW_TRY_FIND_FIRST_DIFF(u16);
  148. return round_down(offset + vmw_find_first_diff_u8(dst, src, size),
  149. granularity);
  150. }
  151. /*
  152. * Template to compute find_last_diff() for a certain integer type
  153. * including a tail copy for alignment, and adjustment of parameters
  154. * for head find or increased resolution find using an unsigned integer find
  155. * of smaller width. If finding is complete, and resolution is sufficient,
  156. * the macro executes a return statement. Otherwise it falls through.
  157. */
  158. #define VMW_TRY_FIND_LAST_DIFF(_type) \
  159. do { \
  160. unsigned int spill = SPILL(dst, _type); \
  161. ssize_t location; \
  162. ssize_t diff_offs; \
  163. \
  164. if (spill && spill <= size && spill == SPILL(src, _type)) { \
  165. diff_offs = vmw_find_last_diff_u8(dst, src, spill); \
  166. if (diff_offs) { \
  167. location = size - spill + diff_offs - 1; \
  168. return round_down(location, granularity); \
  169. } \
  170. \
  171. dst -= spill; \
  172. src -= spill; \
  173. size -= spill; \
  174. spill = 0; \
  175. } \
  176. if (!spill && !SPILL(src, _type)) { \
  177. size_t to_copy = round_down(size, sizeof(_type)); \
  178. \
  179. diff_offs = vmw_find_last_diff_ ## _type \
  180. ((_type *) dst, (_type *) src, to_copy); \
  181. location = size - to_copy + diff_offs - sizeof(_type); \
  182. if (location < 0 || granularity == sizeof(_type)) \
  183. return location; \
  184. \
  185. dst -= to_copy - diff_offs; \
  186. src -= to_copy - diff_offs; \
  187. size -= to_copy - diff_offs; \
  188. } \
  189. } while (0)
  190. /**
  191. * vmw_find_last_diff - find the last difference between dst and src
  192. *
  193. * @dst: The destination address
  194. * @src: The source address
  195. * @size: Number of bytes to compare
  196. * @granularity: The granularity needed for the return value in bytes.
  197. * return: The offset from find start where the last difference was
  198. * encountered in bytes, or a negative value if no difference was found.
  199. */
  200. static ssize_t vmw_find_last_diff(const u8 *dst, const u8 *src, size_t size,
  201. size_t granularity)
  202. {
  203. dst += size;
  204. src += size;
  205. #ifdef CONFIG_64BIT
  206. VMW_TRY_FIND_LAST_DIFF(u64);
  207. #endif
  208. VMW_TRY_FIND_LAST_DIFF(u32);
  209. VMW_TRY_FIND_LAST_DIFF(u16);
  210. return round_down(vmw_find_last_diff_u8(dst, src, size) - 1,
  211. granularity);
  212. }
  213. /**
  214. * vmw_memcpy - A wrapper around kernel memcpy with allowing to plug it into a
  215. * struct vmw_diff_cpy.
  216. *
  217. * @diff: The struct vmw_diff_cpy closure argument (unused).
  218. * @dest: The copy destination.
  219. * @src: The copy source.
  220. * @n: Number of bytes to copy.
  221. */
  222. void vmw_memcpy(struct vmw_diff_cpy *diff, u8 *dest, const u8 *src, size_t n)
  223. {
  224. memcpy(dest, src, n);
  225. }
  226. /**
  227. * vmw_adjust_rect - Adjust rectangle coordinates for newly found difference
  228. *
  229. * @diff: The struct vmw_diff_cpy used to track the modified bounding box.
  230. * @diff_offs: The offset from @diff->line_offset where the difference was
  231. * found.
  232. */
  233. static void vmw_adjust_rect(struct vmw_diff_cpy *diff, size_t diff_offs)
  234. {
  235. size_t offs = (diff_offs + diff->line_offset) / diff->cpp;
  236. struct drm_rect *rect = &diff->rect;
  237. rect->x1 = min_t(int, rect->x1, offs);
  238. rect->x2 = max_t(int, rect->x2, offs + 1);
  239. rect->y1 = min_t(int, rect->y1, diff->line);
  240. rect->y2 = max_t(int, rect->y2, diff->line + 1);
  241. }
  242. /**
  243. * vmw_diff_memcpy - memcpy that creates a bounding box of modified content.
  244. *
  245. * @diff: The struct vmw_diff_cpy used to track the modified bounding box.
  246. * @dest: The copy destination.
  247. * @src: The copy source.
  248. * @n: Number of bytes to copy.
  249. *
  250. * In order to correctly track the modified content, the field @diff->line must
  251. * be pre-loaded with the current line number, the field @diff->line_offset must
  252. * be pre-loaded with the line offset in bytes where the copy starts, and
  253. * finally the field @diff->cpp need to be preloaded with the number of bytes
  254. * per unit in the horizontal direction of the area we're examining.
  255. * Typically bytes per pixel.
  256. * This is needed to know the needed granularity of the difference computing
  257. * operations. A higher cpp generally leads to faster execution at the cost of
  258. * bounding box width precision.
  259. */
  260. void vmw_diff_memcpy(struct vmw_diff_cpy *diff, u8 *dest, const u8 *src,
  261. size_t n)
  262. {
  263. ssize_t csize, byte_len;
  264. if (WARN_ON_ONCE(round_down(n, diff->cpp) != n))
  265. return;
  266. /* TODO: Possibly use a single vmw_find_first_diff per line? */
  267. csize = vmw_find_first_diff(dest, src, n, diff->cpp);
  268. if (csize < n) {
  269. vmw_adjust_rect(diff, csize);
  270. byte_len = diff->cpp;
  271. /*
  272. * Starting from where first difference was found, find
  273. * location of last difference, and then copy.
  274. */
  275. diff->line_offset += csize;
  276. dest += csize;
  277. src += csize;
  278. n -= csize;
  279. csize = vmw_find_last_diff(dest, src, n, diff->cpp);
  280. if (csize >= 0) {
  281. byte_len += csize;
  282. vmw_adjust_rect(diff, csize);
  283. }
  284. memcpy(dest, src, byte_len);
  285. }
  286. diff->line_offset += n;
  287. }
  288. /**
  289. * struct vmw_bo_blit_line_data - Convenience argument to vmw_bo_cpu_blit_line
  290. *
  291. * @mapped_dst: Already mapped destination page index in @dst_pages.
  292. * @dst_addr: Kernel virtual address of mapped destination page.
  293. * @dst_pages: Array of destination bo pages.
  294. * @dst_num_pages: Number of destination bo pages.
  295. * @dst_prot: Destination bo page protection.
  296. * @mapped_src: Already mapped source page index in @dst_pages.
  297. * @src_addr: Kernel virtual address of mapped source page.
  298. * @src_pages: Array of source bo pages.
  299. * @src_num_pages: Number of source bo pages.
  300. * @src_prot: Source bo page protection.
  301. * @diff: Struct vmw_diff_cpy, in the end forwarded to the memcpy routine.
  302. */
  303. struct vmw_bo_blit_line_data {
  304. u32 mapped_dst;
  305. u8 *dst_addr;
  306. struct page **dst_pages;
  307. u32 dst_num_pages;
  308. pgprot_t dst_prot;
  309. u32 mapped_src;
  310. u8 *src_addr;
  311. struct page **src_pages;
  312. u32 src_num_pages;
  313. pgprot_t src_prot;
  314. struct vmw_diff_cpy *diff;
  315. };
  316. /**
  317. * vmw_bo_cpu_blit_line - Blit part of a line from one bo to another.
  318. *
  319. * @d: Blit data as described above.
  320. * @dst_offset: Destination copy start offset from start of bo.
  321. * @src_offset: Source copy start offset from start of bo.
  322. * @bytes_to_copy: Number of bytes to copy in this line.
  323. */
  324. static int vmw_bo_cpu_blit_line(struct vmw_bo_blit_line_data *d,
  325. u32 dst_offset,
  326. u32 src_offset,
  327. u32 bytes_to_copy)
  328. {
  329. struct vmw_diff_cpy *diff = d->diff;
  330. while (bytes_to_copy) {
  331. u32 copy_size = bytes_to_copy;
  332. u32 dst_page = dst_offset >> PAGE_SHIFT;
  333. u32 src_page = src_offset >> PAGE_SHIFT;
  334. u32 dst_page_offset = dst_offset & ~PAGE_MASK;
  335. u32 src_page_offset = src_offset & ~PAGE_MASK;
  336. bool unmap_dst = d->dst_addr && dst_page != d->mapped_dst;
  337. bool unmap_src = d->src_addr && (src_page != d->mapped_src ||
  338. unmap_dst);
  339. copy_size = min_t(u32, copy_size, PAGE_SIZE - dst_page_offset);
  340. copy_size = min_t(u32, copy_size, PAGE_SIZE - src_page_offset);
  341. if (unmap_src) {
  342. kunmap_atomic(d->src_addr);
  343. d->src_addr = NULL;
  344. }
  345. if (unmap_dst) {
  346. kunmap_atomic(d->dst_addr);
  347. d->dst_addr = NULL;
  348. }
  349. if (!d->dst_addr) {
  350. if (WARN_ON_ONCE(dst_page >= d->dst_num_pages))
  351. return -EINVAL;
  352. d->dst_addr =
  353. kmap_atomic_prot(d->dst_pages[dst_page],
  354. d->dst_prot);
  355. if (!d->dst_addr)
  356. return -ENOMEM;
  357. d->mapped_dst = dst_page;
  358. }
  359. if (!d->src_addr) {
  360. if (WARN_ON_ONCE(src_page >= d->src_num_pages))
  361. return -EINVAL;
  362. d->src_addr =
  363. kmap_atomic_prot(d->src_pages[src_page],
  364. d->src_prot);
  365. if (!d->src_addr)
  366. return -ENOMEM;
  367. d->mapped_src = src_page;
  368. }
  369. diff->do_cpy(diff, d->dst_addr + dst_page_offset,
  370. d->src_addr + src_page_offset, copy_size);
  371. bytes_to_copy -= copy_size;
  372. dst_offset += copy_size;
  373. src_offset += copy_size;
  374. }
  375. return 0;
  376. }
  377. static void *map_external(struct vmw_bo *bo, struct iosys_map *map)
  378. {
  379. struct vmw_private *vmw =
  380. container_of(bo->tbo.bdev, struct vmw_private, bdev);
  381. void *ptr = NULL;
  382. int ret;
  383. if (drm_gem_is_imported(&bo->tbo.base)) {
  384. ret = dma_buf_vmap(bo->tbo.base.dma_buf, map);
  385. if (ret) {
  386. drm_dbg_driver(&vmw->drm,
  387. "Wasn't able to map external bo!\n");
  388. goto out;
  389. }
  390. ptr = map->vaddr;
  391. } else {
  392. ptr = vmw_bo_map_and_cache(bo);
  393. }
  394. out:
  395. return ptr;
  396. }
  397. static void unmap_external(struct vmw_bo *bo, struct iosys_map *map)
  398. {
  399. if (drm_gem_is_imported(&bo->tbo.base))
  400. dma_buf_vunmap(bo->tbo.base.dma_buf, map);
  401. else
  402. vmw_bo_unmap(bo);
  403. }
  404. static int vmw_external_bo_copy(struct vmw_bo *dst, u32 dst_offset,
  405. u32 dst_stride, struct vmw_bo *src,
  406. u32 src_offset, u32 src_stride,
  407. u32 width_in_bytes, u32 height,
  408. struct vmw_diff_cpy *diff)
  409. {
  410. struct vmw_private *vmw =
  411. container_of(dst->tbo.bdev, struct vmw_private, bdev);
  412. size_t dst_size = dst->tbo.resource->size;
  413. size_t src_size = src->tbo.resource->size;
  414. struct iosys_map dst_map = {0};
  415. struct iosys_map src_map = {0};
  416. int ret, i;
  417. int x_in_bytes;
  418. u8 *vsrc;
  419. u8 *vdst;
  420. vsrc = map_external(src, &src_map);
  421. if (!vsrc) {
  422. drm_dbg_driver(&vmw->drm, "Wasn't able to map src\n");
  423. ret = -ENOMEM;
  424. goto out;
  425. }
  426. vdst = map_external(dst, &dst_map);
  427. if (!vdst) {
  428. drm_dbg_driver(&vmw->drm, "Wasn't able to map dst\n");
  429. ret = -ENOMEM;
  430. goto out;
  431. }
  432. vsrc += src_offset;
  433. vdst += dst_offset;
  434. if (src_stride == dst_stride) {
  435. dst_size -= dst_offset;
  436. src_size -= src_offset;
  437. memcpy(vdst, vsrc,
  438. min(dst_stride * height, min(dst_size, src_size)));
  439. } else {
  440. WARN_ON(dst_stride < width_in_bytes);
  441. for (i = 0; i < height; ++i) {
  442. memcpy(vdst, vsrc, width_in_bytes);
  443. vsrc += src_stride;
  444. vdst += dst_stride;
  445. }
  446. }
  447. x_in_bytes = (dst_offset % dst_stride);
  448. diff->rect.x1 = x_in_bytes / diff->cpp;
  449. diff->rect.y1 = ((dst_offset - x_in_bytes) / dst_stride);
  450. diff->rect.x2 = diff->rect.x1 + width_in_bytes / diff->cpp;
  451. diff->rect.y2 = diff->rect.y1 + height;
  452. ret = 0;
  453. out:
  454. unmap_external(src, &src_map);
  455. unmap_external(dst, &dst_map);
  456. return ret;
  457. }
  458. /**
  459. * vmw_bo_cpu_blit - in-kernel cpu blit.
  460. *
  461. * @vmw_dst: Destination buffer object.
  462. * @dst_offset: Destination offset of blit start in bytes.
  463. * @dst_stride: Destination stride in bytes.
  464. * @vmw_src: Source buffer object.
  465. * @src_offset: Source offset of blit start in bytes.
  466. * @src_stride: Source stride in bytes.
  467. * @w: Width of blit.
  468. * @h: Height of blit.
  469. * @diff: The struct vmw_diff_cpy used to track the modified bounding box.
  470. * return: Zero on success. Negative error value on failure. Will print out
  471. * kernel warnings on caller bugs.
  472. *
  473. * Performs a CPU blit from one buffer object to another avoiding a full
  474. * bo vmap which may exhaust- or fragment vmalloc space.
  475. * On supported architectures (x86), we're using kmap_atomic which avoids
  476. * cross-processor TLB- and cache flushes and may, on non-HIGHMEM systems
  477. * reference already set-up mappings.
  478. *
  479. * Neither of the buffer objects may be placed in PCI memory
  480. * (Fixed memory in TTM terminology) when using this function.
  481. */
  482. int vmw_bo_cpu_blit(struct vmw_bo *vmw_dst,
  483. u32 dst_offset, u32 dst_stride,
  484. struct vmw_bo *vmw_src,
  485. u32 src_offset, u32 src_stride,
  486. u32 w, u32 h,
  487. struct vmw_diff_cpy *diff)
  488. {
  489. struct ttm_buffer_object *src = &vmw_src->tbo;
  490. struct ttm_buffer_object *dst = &vmw_dst->tbo;
  491. struct ttm_operation_ctx ctx = {
  492. .interruptible = false,
  493. .no_wait_gpu = false
  494. };
  495. u32 j, initial_line = dst_offset / dst_stride;
  496. struct vmw_bo_blit_line_data d = {0};
  497. int ret = 0;
  498. struct page **dst_pages = NULL;
  499. struct page **src_pages = NULL;
  500. bool src_external = (src->ttm->page_flags & TTM_TT_FLAG_EXTERNAL) != 0;
  501. bool dst_external = (dst->ttm->page_flags & TTM_TT_FLAG_EXTERNAL) != 0;
  502. if (WARN_ON(dst == src))
  503. return -EINVAL;
  504. /* Buffer objects need to be either pinned or reserved: */
  505. if (!(dst->pin_count))
  506. dma_resv_assert_held(dst->base.resv);
  507. if (!(src->pin_count))
  508. dma_resv_assert_held(src->base.resv);
  509. if (!ttm_tt_is_populated(dst->ttm)) {
  510. ret = dst->bdev->funcs->ttm_tt_populate(dst->bdev, dst->ttm, &ctx);
  511. if (ret)
  512. return ret;
  513. }
  514. if (!ttm_tt_is_populated(src->ttm)) {
  515. ret = src->bdev->funcs->ttm_tt_populate(src->bdev, src->ttm, &ctx);
  516. if (ret)
  517. return ret;
  518. }
  519. if (src_external || dst_external)
  520. return vmw_external_bo_copy(vmw_dst, dst_offset, dst_stride,
  521. vmw_src, src_offset, src_stride,
  522. w, h, diff);
  523. if (!src->ttm->pages && src->ttm->sg) {
  524. src_pages = kvmalloc_objs(struct page *, src->ttm->num_pages);
  525. if (!src_pages)
  526. return -ENOMEM;
  527. ret = drm_prime_sg_to_page_array(src->ttm->sg, src_pages,
  528. src->ttm->num_pages);
  529. if (ret)
  530. goto out;
  531. }
  532. if (!dst->ttm->pages && dst->ttm->sg) {
  533. dst_pages = kvmalloc_objs(struct page *, dst->ttm->num_pages);
  534. if (!dst_pages) {
  535. ret = -ENOMEM;
  536. goto out;
  537. }
  538. ret = drm_prime_sg_to_page_array(dst->ttm->sg, dst_pages,
  539. dst->ttm->num_pages);
  540. if (ret)
  541. goto out;
  542. }
  543. d.mapped_dst = 0;
  544. d.mapped_src = 0;
  545. d.dst_addr = NULL;
  546. d.src_addr = NULL;
  547. d.dst_pages = dst->ttm->pages ? dst->ttm->pages : dst_pages;
  548. d.src_pages = src->ttm->pages ? src->ttm->pages : src_pages;
  549. d.dst_num_pages = PFN_UP(dst->resource->size);
  550. d.src_num_pages = PFN_UP(src->resource->size);
  551. d.dst_prot = ttm_io_prot(dst, dst->resource, PAGE_KERNEL);
  552. d.src_prot = ttm_io_prot(src, src->resource, PAGE_KERNEL);
  553. d.diff = diff;
  554. for (j = 0; j < h; ++j) {
  555. diff->line = j + initial_line;
  556. diff->line_offset = dst_offset % dst_stride;
  557. ret = vmw_bo_cpu_blit_line(&d, dst_offset, src_offset, w);
  558. if (ret)
  559. goto out;
  560. dst_offset += dst_stride;
  561. src_offset += src_stride;
  562. }
  563. out:
  564. if (d.src_addr)
  565. kunmap_atomic(d.src_addr);
  566. if (d.dst_addr)
  567. kunmap_atomic(d.dst_addr);
  568. kvfree(src_pages);
  569. kvfree(dst_pages);
  570. return ret;
  571. }