cfbcopyarea.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Generic function for frame buffer with packed pixels of any depth.
  3. *
  4. * Copyright (C) 1999-2005 James Simmons <jsimmons@www.infradead.org>
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file COPYING in the main directory of this archive for
  8. * more details.
  9. *
  10. * NOTES:
  11. *
  12. * This is for cfb packed pixels. Iplan and such are incorporated in the
  13. * drivers that need them.
  14. *
  15. * FIXME
  16. *
  17. * Also need to add code to deal with cards endians that are different than
  18. * the native cpu endians. I also need to deal with MSB position in the word.
  19. *
  20. * The two functions or copying forward and backward could be split up like
  21. * the ones for filling, i.e. in aligned and unaligned versions. This would
  22. * help moving some redundant computations and branches out of the loop, too.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/string.h>
  27. #include <linux/fb.h>
  28. #include <linux/slab.h>
  29. #include <asm/types.h>
  30. #include <asm/io.h>
  31. #include "fb_draw.h"
  32. #if BITS_PER_LONG == 32
  33. # define FB_WRITEL fb_writel
  34. # define FB_READL fb_readl
  35. #else
  36. # define FB_WRITEL fb_writeq
  37. # define FB_READL fb_readq
  38. #endif
  39. /*
  40. * Generic bitwise copy algorithm
  41. */
  42. static void
  43. bitcpy(unsigned long __iomem *dst, int dst_idx, const unsigned long __iomem *src,
  44. int src_idx, int bits, unsigned n)
  45. {
  46. unsigned long first, last;
  47. int const shift = dst_idx-src_idx;
  48. int left, right;
  49. first = FB_SHIFT_HIGH(~0UL, dst_idx);
  50. last = ~(FB_SHIFT_HIGH(~0UL, (dst_idx+n) % bits));
  51. if (!shift) {
  52. // Same alignment for source and dest
  53. if (dst_idx+n <= bits) {
  54. // Single word
  55. if (last)
  56. first &= last;
  57. FB_WRITEL( comp( FB_READL(src), FB_READL(dst), first), dst);
  58. } else {
  59. // Multiple destination words
  60. // Leading bits
  61. if (first != ~0UL) {
  62. FB_WRITEL( comp( FB_READL(src), FB_READL(dst), first), dst);
  63. dst++;
  64. src++;
  65. n -= bits - dst_idx;
  66. }
  67. // Main chunk
  68. n /= bits;
  69. while (n >= 8) {
  70. FB_WRITEL(FB_READL(src++), dst++);
  71. FB_WRITEL(FB_READL(src++), dst++);
  72. FB_WRITEL(FB_READL(src++), dst++);
  73. FB_WRITEL(FB_READL(src++), dst++);
  74. FB_WRITEL(FB_READL(src++), dst++);
  75. FB_WRITEL(FB_READL(src++), dst++);
  76. FB_WRITEL(FB_READL(src++), dst++);
  77. FB_WRITEL(FB_READL(src++), dst++);
  78. n -= 8;
  79. }
  80. while (n--)
  81. FB_WRITEL(FB_READL(src++), dst++);
  82. // Trailing bits
  83. if (last)
  84. FB_WRITEL( comp( FB_READL(src), FB_READL(dst), last), dst);
  85. }
  86. } else {
  87. unsigned long d0, d1;
  88. int m;
  89. // Different alignment for source and dest
  90. right = shift & (bits - 1);
  91. left = -shift & (bits - 1);
  92. if (dst_idx+n <= bits) {
  93. // Single destination word
  94. if (last)
  95. first &= last;
  96. if (shift > 0) {
  97. // Single source word
  98. FB_WRITEL( comp( FB_READL(src) >> right, FB_READL(dst), first), dst);
  99. } else if (src_idx+n <= bits) {
  100. // Single source word
  101. FB_WRITEL( comp(FB_READL(src) << left, FB_READL(dst), first), dst);
  102. } else {
  103. // 2 source words
  104. d0 = FB_READL(src++);
  105. d1 = FB_READL(src);
  106. FB_WRITEL( comp(d0<<left | d1>>right, FB_READL(dst), first), dst);
  107. }
  108. } else {
  109. // Multiple destination words
  110. /** We must always remember the last value read, because in case
  111. SRC and DST overlap bitwise (e.g. when moving just one pixel in
  112. 1bpp), we always collect one full long for DST and that might
  113. overlap with the current long from SRC. We store this value in
  114. 'd0'. */
  115. d0 = FB_READL(src++);
  116. // Leading bits
  117. if (shift > 0) {
  118. // Single source word
  119. FB_WRITEL( comp(d0 >> right, FB_READL(dst), first), dst);
  120. dst++;
  121. n -= bits - dst_idx;
  122. } else {
  123. // 2 source words
  124. d1 = FB_READL(src++);
  125. FB_WRITEL( comp(d0<<left | d1>>right, FB_READL(dst), first), dst);
  126. d0 = d1;
  127. dst++;
  128. n -= bits - dst_idx;
  129. }
  130. // Main chunk
  131. m = n % bits;
  132. n /= bits;
  133. while (n >= 4) {
  134. d1 = FB_READL(src++);
  135. FB_WRITEL(d0 << left | d1 >> right, dst++);
  136. d0 = d1;
  137. d1 = FB_READL(src++);
  138. FB_WRITEL(d0 << left | d1 >> right, dst++);
  139. d0 = d1;
  140. d1 = FB_READL(src++);
  141. FB_WRITEL(d0 << left | d1 >> right, dst++);
  142. d0 = d1;
  143. d1 = FB_READL(src++);
  144. FB_WRITEL(d0 << left | d1 >> right, dst++);
  145. d0 = d1;
  146. n -= 4;
  147. }
  148. while (n--) {
  149. d1 = FB_READL(src++);
  150. FB_WRITEL(d0 << left | d1 >> right, dst++);
  151. d0 = d1;
  152. }
  153. // Trailing bits
  154. if (last) {
  155. if (m <= right) {
  156. // Single source word
  157. FB_WRITEL( comp(d0 << left, FB_READL(dst), last), dst);
  158. } else {
  159. // 2 source words
  160. d1 = FB_READL(src);
  161. FB_WRITEL( comp(d0<<left | d1>>right, FB_READL(dst), last), dst);
  162. }
  163. }
  164. }
  165. }
  166. }
  167. /*
  168. * Generic bitwise copy algorithm, operating backward
  169. */
  170. static void
  171. bitcpy_rev(unsigned long __iomem *dst, int dst_idx, const unsigned long __iomem *src,
  172. int src_idx, int bits, unsigned n)
  173. {
  174. unsigned long first, last;
  175. int shift;
  176. dst += (n-1)/bits;
  177. src += (n-1)/bits;
  178. if ((n-1) % bits) {
  179. dst_idx += (n-1) % bits;
  180. dst += dst_idx >> (ffs(bits) - 1);
  181. dst_idx &= bits - 1;
  182. src_idx += (n-1) % bits;
  183. src += src_idx >> (ffs(bits) - 1);
  184. src_idx &= bits - 1;
  185. }
  186. shift = dst_idx-src_idx;
  187. first = FB_SHIFT_LOW(~0UL, bits - 1 - dst_idx);
  188. last = ~(FB_SHIFT_LOW(~0UL, bits - 1 - ((dst_idx-n) % bits)));
  189. if (!shift) {
  190. // Same alignment for source and dest
  191. if ((unsigned long)dst_idx+1 >= n) {
  192. // Single word
  193. if (last)
  194. first &= last;
  195. FB_WRITEL( comp( FB_READL(src), FB_READL(dst), first), dst);
  196. } else {
  197. // Multiple destination words
  198. // Leading bits
  199. if (first != ~0UL) {
  200. FB_WRITEL( comp( FB_READL(src), FB_READL(dst), first), dst);
  201. dst--;
  202. src--;
  203. n -= dst_idx+1;
  204. }
  205. // Main chunk
  206. n /= bits;
  207. while (n >= 8) {
  208. FB_WRITEL(FB_READL(src--), dst--);
  209. FB_WRITEL(FB_READL(src--), dst--);
  210. FB_WRITEL(FB_READL(src--), dst--);
  211. FB_WRITEL(FB_READL(src--), dst--);
  212. FB_WRITEL(FB_READL(src--), dst--);
  213. FB_WRITEL(FB_READL(src--), dst--);
  214. FB_WRITEL(FB_READL(src--), dst--);
  215. FB_WRITEL(FB_READL(src--), dst--);
  216. n -= 8;
  217. }
  218. while (n--)
  219. FB_WRITEL(FB_READL(src--), dst--);
  220. // Trailing bits
  221. if (last)
  222. FB_WRITEL( comp( FB_READL(src), FB_READL(dst), last), dst);
  223. }
  224. } else {
  225. // Different alignment for source and dest
  226. int const left = -shift & (bits-1);
  227. int const right = shift & (bits-1);
  228. if ((unsigned long)dst_idx+1 >= n) {
  229. // Single destination word
  230. if (last)
  231. first &= last;
  232. if (shift < 0) {
  233. // Single source word
  234. FB_WRITEL( comp( FB_READL(src)<<left, FB_READL(dst), first), dst);
  235. } else if (1+(unsigned long)src_idx >= n) {
  236. // Single source word
  237. FB_WRITEL( comp( FB_READL(src)>>right, FB_READL(dst), first), dst);
  238. } else {
  239. // 2 source words
  240. FB_WRITEL( comp( (FB_READL(src)>>right | FB_READL(src-1)<<left), FB_READL(dst), first), dst);
  241. }
  242. } else {
  243. // Multiple destination words
  244. /** We must always remember the last value read, because in case
  245. SRC and DST overlap bitwise (e.g. when moving just one pixel in
  246. 1bpp), we always collect one full long for DST and that might
  247. overlap with the current long from SRC. We store this value in
  248. 'd0'. */
  249. unsigned long d0, d1;
  250. int m;
  251. d0 = FB_READL(src--);
  252. // Leading bits
  253. if (shift < 0) {
  254. // Single source word
  255. FB_WRITEL( comp( (d0 << left), FB_READL(dst), first), dst);
  256. } else {
  257. // 2 source words
  258. d1 = FB_READL(src--);
  259. FB_WRITEL( comp( (d0>>right | d1<<left), FB_READL(dst), first), dst);
  260. d0 = d1;
  261. }
  262. dst--;
  263. n -= dst_idx+1;
  264. // Main chunk
  265. m = n % bits;
  266. n /= bits;
  267. while (n >= 4) {
  268. d1 = FB_READL(src--);
  269. FB_WRITEL(d0 >> right | d1 << left, dst--);
  270. d0 = d1;
  271. d1 = FB_READL(src--);
  272. FB_WRITEL(d0 >> right | d1 << left, dst--);
  273. d0 = d1;
  274. d1 = FB_READL(src--);
  275. FB_WRITEL(d0 >> right | d1 << left, dst--);
  276. d0 = d1;
  277. d1 = FB_READL(src--);
  278. FB_WRITEL(d0 >> right | d1 << left, dst--);
  279. d0 = d1;
  280. n -= 4;
  281. }
  282. while (n--) {
  283. d1 = FB_READL(src--);
  284. FB_WRITEL(d0 >> right | d1 << left, dst--);
  285. d0 = d1;
  286. }
  287. // Trailing bits
  288. if (last) {
  289. if (m <= left) {
  290. // Single source word
  291. FB_WRITEL( comp(d0 >> right, FB_READL(dst), last), dst);
  292. } else {
  293. // 2 source words
  294. d1 = FB_READL(src);
  295. FB_WRITEL( comp(d0>>right | d1<<left, FB_READL(dst), last), dst);
  296. }
  297. }
  298. }
  299. }
  300. }
  301. void cfb_copyarea(struct fb_info *p, const struct fb_copyarea *area)
  302. {
  303. u32 dx = area->dx, dy = area->dy, sx = area->sx, sy = area->sy;
  304. u32 height = area->height, width = area->width;
  305. unsigned long const bits_per_line = p->fix.line_length*8u;
  306. unsigned long __iomem *dst = NULL, *src = NULL;
  307. int bits = BITS_PER_LONG, bytes = bits >> 3;
  308. int dst_idx = 0, src_idx = 0, rev_copy = 0;
  309. if (p->state != FBINFO_STATE_RUNNING)
  310. return;
  311. /* if the beginning of the target area might overlap with the end of
  312. the source area, be have to copy the area reverse. */
  313. if ((dy == sy && dx > sx) || (dy > sy)) {
  314. dy += height;
  315. sy += height;
  316. rev_copy = 1;
  317. }
  318. // split the base of the framebuffer into a long-aligned address and the
  319. // index of the first bit
  320. dst = src = (unsigned long __iomem *)((unsigned long)p->screen_base & ~(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(cfb_copyarea);
  352. MODULE_AUTHOR("James Simmons <jsimmons@users.sf.net>");
  353. MODULE_DESCRIPTION("Generic software accelerated copyarea");
  354. MODULE_LICENSE("GPL");