syscopyarea.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * Generic Bit Block Transfer for frame buffers located in system RAM with
  3. * packed pixels of any depth.
  4. *
  5. * Based almost entirely from cfbcopyarea.c (which is based almost entirely
  6. * on Geert Uytterhoeven's copyarea routine)
  7. *
  8. * Copyright (C) 2007 Antonino Daplas <adaplas@pol.net>
  9. *
  10. * This file is subject to the terms and conditions of the GNU General Public
  11. * License. See the file COPYING in the main directory of this archive for
  12. * more details.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/string.h>
  18. #include <linux/fb.h>
  19. #include <linux/slab.h>
  20. #include <asm/types.h>
  21. #include <asm/io.h>
  22. /*
  23. * Compose two values, using a bitmask as decision value
  24. * This is equivalent to (a & mask) | (b & ~mask)
  25. */
  26. static inline unsigned long
  27. comp(unsigned long a, unsigned long b, unsigned long mask)
  28. {
  29. return ((a ^ b) & mask) ^ b;
  30. }
  31. /*
  32. * Generic bitwise copy algorithm
  33. */
  34. static void
  35. bitcpy(unsigned long *dst, int dst_idx, const unsigned long *src,
  36. int src_idx, int bits, unsigned n)
  37. {
  38. unsigned long first, last;
  39. int const shift = dst_idx-src_idx;
  40. int left, right;
  41. first = FB_SHIFT_HIGH(~0UL, dst_idx);
  42. last = ~(FB_SHIFT_HIGH(~0UL, (dst_idx+n) % bits));
  43. if (!shift) {
  44. /* Same alignment for source and dest */
  45. if (dst_idx+n <= bits) {
  46. /* Single word */
  47. if (last)
  48. first &= last;
  49. *dst = comp(*src, *dst, first);
  50. } else {
  51. /* Multiple destination words */
  52. /* Leading bits */
  53. if (first != ~0UL) {
  54. *dst = comp(*src, *dst, first);
  55. dst++;
  56. src++;
  57. n -= bits - dst_idx;
  58. }
  59. /* Main chunk */
  60. n /= bits;
  61. while (n >= 8) {
  62. *dst++ = *src++;
  63. *dst++ = *src++;
  64. *dst++ = *src++;
  65. *dst++ = *src++;
  66. *dst++ = *src++;
  67. *dst++ = *src++;
  68. *dst++ = *src++;
  69. *dst++ = *src++;
  70. n -= 8;
  71. }
  72. while (n--)
  73. *dst++ = *src++;
  74. /* Trailing bits */
  75. if (last)
  76. *dst = comp(*src, *dst, last);
  77. }
  78. } else {
  79. unsigned long d0, d1;
  80. int m;
  81. /* Different alignment for source and dest */
  82. right = shift & (bits - 1);
  83. left = -shift & (bits - 1);
  84. if (dst_idx+n <= bits) {
  85. /* Single destination word */
  86. if (last)
  87. first &= last;
  88. if (shift > 0) {
  89. /* Single source word */
  90. *dst = comp(*src >> right, *dst, first);
  91. } else if (src_idx+n <= bits) {
  92. /* Single source word */
  93. *dst = comp(*src << left, *dst, first);
  94. } else {
  95. /* 2 source words */
  96. d0 = *src++;
  97. d1 = *src;
  98. *dst = comp(d0 << left | d1 >> right, *dst,
  99. first);
  100. }
  101. } else {
  102. /* Multiple destination words */
  103. /** We must always remember the last value read,
  104. because in case SRC and DST overlap bitwise (e.g.
  105. when moving just one pixel in 1bpp), we always
  106. collect one full long for DST and that might
  107. overlap with the current long from SRC. We store
  108. this value in 'd0'. */
  109. d0 = *src++;
  110. /* Leading bits */
  111. if (shift > 0) {
  112. /* Single source word */
  113. *dst = comp(d0 >> right, *dst, first);
  114. dst++;
  115. n -= bits - dst_idx;
  116. } else {
  117. /* 2 source words */
  118. d1 = *src++;
  119. *dst = comp(d0 << left | *dst >> right, *dst, first);
  120. d0 = d1;
  121. dst++;
  122. n -= bits - dst_idx;
  123. }
  124. /* Main chunk */
  125. m = n % bits;
  126. n /= bits;
  127. while (n >= 4) {
  128. d1 = *src++;
  129. *dst++ = d0 << left | d1 >> right;
  130. d0 = d1;
  131. d1 = *src++;
  132. *dst++ = d0 << left | d1 >> right;
  133. d0 = d1;
  134. d1 = *src++;
  135. *dst++ = d0 << left | d1 >> right;
  136. d0 = d1;
  137. d1 = *src++;
  138. *dst++ = d0 << left | d1 >> right;
  139. d0 = d1;
  140. n -= 4;
  141. }
  142. while (n--) {
  143. d1 = *src++;
  144. *dst++ = d0 << left | d1 >> right;
  145. d0 = d1;
  146. }
  147. /* Trailing bits */
  148. if (last) {
  149. if (m <= right) {
  150. /* Single source word */
  151. *dst = comp(d0 << left, *dst, last);
  152. } else {
  153. /* 2 source words */
  154. d1 = *src;
  155. *dst = comp(d0 << left | d1 >> right,
  156. *dst, last);
  157. }
  158. }
  159. }
  160. }
  161. }
  162. /*
  163. * Generic bitwise copy algorithm, operating backward
  164. */
  165. static void
  166. bitcpy_rev(unsigned long *dst, int dst_idx, const unsigned long *src,
  167. int src_idx, int bits, unsigned n)
  168. {
  169. unsigned long first, last;
  170. int shift;
  171. dst += (n-1)/bits;
  172. src += (n-1)/bits;
  173. if ((n-1) % bits) {
  174. dst_idx += (n-1) % bits;
  175. dst += dst_idx >> (ffs(bits) - 1);
  176. dst_idx &= bits - 1;
  177. src_idx += (n-1) % bits;
  178. src += src_idx >> (ffs(bits) - 1);
  179. src_idx &= bits - 1;
  180. }
  181. shift = dst_idx-src_idx;
  182. first = FB_SHIFT_LOW(~0UL, bits - 1 - dst_idx);
  183. last = ~(FB_SHIFT_LOW(~0UL, bits - 1 - ((dst_idx-n) % bits)));
  184. if (!shift) {
  185. /* Same alignment for source and dest */
  186. if ((unsigned long)dst_idx+1 >= n) {
  187. /* Single word */
  188. if (last)
  189. first &= last;
  190. *dst = comp(*src, *dst, first);
  191. } else {
  192. /* Multiple destination words */
  193. /* Leading bits */
  194. if (first != ~0UL) {
  195. *dst = comp(*src, *dst, first);
  196. dst--;
  197. src--;
  198. n -= dst_idx+1;
  199. }
  200. /* Main chunk */
  201. n /= bits;
  202. while (n >= 8) {
  203. *dst-- = *src--;
  204. *dst-- = *src--;
  205. *dst-- = *src--;
  206. *dst-- = *src--;
  207. *dst-- = *src--;
  208. *dst-- = *src--;
  209. *dst-- = *src--;
  210. *dst-- = *src--;
  211. n -= 8;
  212. }
  213. while (n--)
  214. *dst-- = *src--;
  215. /* Trailing bits */
  216. if (last)
  217. *dst = comp(*src, *dst, last);
  218. }
  219. } else {
  220. /* Different alignment for source and dest */
  221. int const left = -shift & (bits-1);
  222. int const right = shift & (bits-1);
  223. if ((unsigned long)dst_idx+1 >= n) {
  224. /* Single destination word */
  225. if (last)
  226. first &= last;
  227. if (shift < 0) {
  228. /* Single source word */
  229. *dst = comp(*src << left, *dst, first);
  230. } else if (1+(unsigned long)src_idx >= n) {
  231. /* Single source word */
  232. *dst = comp(*src >> right, *dst, first);
  233. } else {
  234. /* 2 source words */
  235. *dst = comp(*src >> right | *(src-1) << left,
  236. *dst, first);
  237. }
  238. } else {
  239. /* Multiple destination words */
  240. /** We must always remember the last value read,
  241. because in case SRC and DST overlap bitwise (e.g.
  242. when moving just one pixel in 1bpp), we always
  243. collect one full long for DST and that might
  244. overlap with the current long from SRC. We store
  245. this value in 'd0'. */
  246. unsigned long d0, d1;
  247. int m;
  248. d0 = *src--;
  249. /* Leading bits */
  250. if (shift < 0) {
  251. /* Single source word */
  252. *dst = comp(d0 << left, *dst, first);
  253. } else {
  254. /* 2 source words */
  255. d1 = *src--;
  256. *dst = comp(d0 >> right | d1 << left, *dst,
  257. first);
  258. d0 = d1;
  259. }
  260. dst--;
  261. n -= dst_idx+1;
  262. /* Main chunk */
  263. m = n % bits;
  264. n /= bits;
  265. while (n >= 4) {
  266. d1 = *src--;
  267. *dst-- = d0 >> right | d1 << left;
  268. d0 = d1;
  269. d1 = *src--;
  270. *dst-- = d0 >> right | d1 << left;
  271. d0 = d1;
  272. d1 = *src--;
  273. *dst-- = d0 >> right | d1 << left;
  274. d0 = d1;
  275. d1 = *src--;
  276. *dst-- = d0 >> right | d1 << left;
  277. d0 = d1;
  278. n -= 4;
  279. }
  280. while (n--) {
  281. d1 = *src--;
  282. *dst-- = d0 >> right | d1 << left;
  283. d0 = d1;
  284. }
  285. /* Trailing bits */
  286. if (last) {
  287. if (m <= left) {
  288. /* Single source word */
  289. *dst = comp(d0 >> right, *dst, last);
  290. } else {
  291. /* 2 source words */
  292. d1 = *src;
  293. *dst = comp(d0 >> right | d1 << left,
  294. *dst, last);
  295. }
  296. }
  297. }
  298. }
  299. }
  300. void sys_copyarea(struct fb_info *p, const struct fb_copyarea *area)
  301. {
  302. u32 dx = area->dx, dy = area->dy, sx = area->sx, sy = area->sy;
  303. u32 height = area->height, width = area->width;
  304. unsigned long const bits_per_line = p->fix.line_length*8u;
  305. unsigned long *dst = NULL, *src = NULL;
  306. int bits = BITS_PER_LONG, bytes = bits >> 3;
  307. int dst_idx = 0, src_idx = 0, rev_copy = 0;
  308. if (p->state != FBINFO_STATE_RUNNING)
  309. return;
  310. /* if the beginning of the target area might overlap with the end of
  311. the source area, be have to copy the area reverse. */
  312. if ((dy == sy && dx > sx) || (dy > sy)) {
  313. dy += height;
  314. sy += height;
  315. rev_copy = 1;
  316. }
  317. /* split the base of the framebuffer into a long-aligned address and
  318. the index of the first bit */
  319. dst = src = (unsigned long *)((unsigned long)p->screen_base &
  320. ~(bytes-1));
  321. dst_idx = src_idx = 8*((unsigned long)p->screen_base & (bytes-1));
  322. /* add offset of source and target area */
  323. dst_idx += dy*bits_per_line + dx*p->var.bits_per_pixel;
  324. src_idx += sy*bits_per_line + sx*p->var.bits_per_pixel;
  325. if (p->fbops->fb_sync)
  326. p->fbops->fb_sync(p);
  327. if (rev_copy) {
  328. while (height--) {
  329. dst_idx -= bits_per_line;
  330. src_idx -= bits_per_line;
  331. dst += dst_idx >> (ffs(bits) - 1);
  332. dst_idx &= (bytes - 1);
  333. src += src_idx >> (ffs(bits) - 1);
  334. src_idx &= (bytes - 1);
  335. bitcpy_rev(dst, dst_idx, src, src_idx, bits,
  336. width*p->var.bits_per_pixel);
  337. }
  338. } else {
  339. while (height--) {
  340. dst += dst_idx >> (ffs(bits) - 1);
  341. dst_idx &= (bytes - 1);
  342. src += src_idx >> (ffs(bits) - 1);
  343. src_idx &= (bytes - 1);
  344. bitcpy(dst, dst_idx, src, src_idx, bits,
  345. width*p->var.bits_per_pixel);
  346. dst_idx += bits_per_line;
  347. src_idx += bits_per_line;
  348. }
  349. }
  350. }
  351. EXPORT_SYMBOL(sys_copyarea);
  352. MODULE_AUTHOR("Antonino Daplas <adaplas@pol.net>");
  353. MODULE_DESCRIPTION("Generic copyarea (sys-to-sys)");
  354. MODULE_LICENSE("GPL");