cfbcopyarea.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 <asm/types.h>
  29. #include <asm/io.h>
  30. #include "fb_draw.h"
  31. #if BITS_PER_LONG == 32
  32. # define FB_WRITEL fb_writel
  33. # define FB_READL fb_readl
  34. #else
  35. # define FB_WRITEL fb_writeq
  36. # define FB_READL fb_readq
  37. #endif
  38. /*
  39. * Generic bitwise copy algorithm
  40. */
  41. static void
  42. bitcpy(struct fb_info *p, unsigned long __iomem *dst, int dst_idx,
  43. const unsigned long __iomem *src, int src_idx, int bits,
  44. unsigned n, u32 bswapmask)
  45. {
  46. unsigned long first, last;
  47. int const shift = dst_idx-src_idx;
  48. int left, right;
  49. first = fb_shifted_pixels_mask_long(p, dst_idx, bswapmask);
  50. last = ~fb_shifted_pixels_mask_long(p, (dst_idx+n) % bits, bswapmask);
  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. /* Different alignment for source and dest */
  88. unsigned long d0, d1;
  89. int m;
  90. right = shift & (bits - 1);
  91. left = -shift & (bits - 1);
  92. bswapmask &= shift;
  93. if (dst_idx+n <= bits) {
  94. // Single destination word
  95. if (last)
  96. first &= last;
  97. d0 = FB_READL(src);
  98. d0 = fb_rev_pixels_in_long(d0, bswapmask);
  99. if (shift > 0) {
  100. // Single source word
  101. d0 >>= right;
  102. } else if (src_idx+n <= bits) {
  103. // Single source word
  104. d0 <<= left;
  105. } else {
  106. // 2 source words
  107. d1 = FB_READL(src + 1);
  108. d1 = fb_rev_pixels_in_long(d1, bswapmask);
  109. d0 = d0<<left | d1>>right;
  110. }
  111. d0 = fb_rev_pixels_in_long(d0, bswapmask);
  112. FB_WRITEL(comp(d0, FB_READL(dst), first), dst);
  113. } else {
  114. // Multiple destination words
  115. /** We must always remember the last value read, because in case
  116. SRC and DST overlap bitwise (e.g. when moving just one pixel in
  117. 1bpp), we always collect one full long for DST and that might
  118. overlap with the current long from SRC. We store this value in
  119. 'd0'. */
  120. d0 = FB_READL(src++);
  121. d0 = fb_rev_pixels_in_long(d0, bswapmask);
  122. // Leading bits
  123. if (shift > 0) {
  124. // Single source word
  125. d1 = d0;
  126. d0 >>= right;
  127. dst++;
  128. n -= bits - dst_idx;
  129. } else {
  130. // 2 source words
  131. d1 = FB_READL(src++);
  132. d1 = fb_rev_pixels_in_long(d1, bswapmask);
  133. d0 = d0<<left | d1>>right;
  134. dst++;
  135. n -= bits - dst_idx;
  136. }
  137. d0 = fb_rev_pixels_in_long(d0, bswapmask);
  138. FB_WRITEL(comp(d0, FB_READL(dst), first), dst);
  139. d0 = d1;
  140. // Main chunk
  141. m = n % bits;
  142. n /= bits;
  143. while ((n >= 4) && !bswapmask) {
  144. d1 = FB_READL(src++);
  145. FB_WRITEL(d0 << left | d1 >> right, dst++);
  146. d0 = d1;
  147. d1 = FB_READL(src++);
  148. FB_WRITEL(d0 << left | d1 >> right, dst++);
  149. d0 = d1;
  150. d1 = FB_READL(src++);
  151. FB_WRITEL(d0 << left | d1 >> right, dst++);
  152. d0 = d1;
  153. d1 = FB_READL(src++);
  154. FB_WRITEL(d0 << left | d1 >> right, dst++);
  155. d0 = d1;
  156. n -= 4;
  157. }
  158. while (n--) {
  159. d1 = FB_READL(src++);
  160. d1 = fb_rev_pixels_in_long(d1, bswapmask);
  161. d0 = d0 << left | d1 >> right;
  162. d0 = fb_rev_pixels_in_long(d0, bswapmask);
  163. FB_WRITEL(d0, dst++);
  164. d0 = d1;
  165. }
  166. // Trailing bits
  167. if (last) {
  168. if (m <= right) {
  169. // Single source word
  170. d0 <<= left;
  171. } else {
  172. // 2 source words
  173. d1 = FB_READL(src);
  174. d1 = fb_rev_pixels_in_long(d1,
  175. bswapmask);
  176. d0 = d0<<left | d1>>right;
  177. }
  178. d0 = fb_rev_pixels_in_long(d0, bswapmask);
  179. FB_WRITEL(comp(d0, FB_READL(dst), last), dst);
  180. }
  181. }
  182. }
  183. }
  184. /*
  185. * Generic bitwise copy algorithm, operating backward
  186. */
  187. static void
  188. bitcpy_rev(struct fb_info *p, unsigned long __iomem *dst, int dst_idx,
  189. const unsigned long __iomem *src, int src_idx, int bits,
  190. unsigned n, u32 bswapmask)
  191. {
  192. unsigned long first, last;
  193. int shift;
  194. dst += (n-1)/bits;
  195. src += (n-1)/bits;
  196. if ((n-1) % bits) {
  197. dst_idx += (n-1) % bits;
  198. dst += dst_idx >> (ffs(bits) - 1);
  199. dst_idx &= bits - 1;
  200. src_idx += (n-1) % bits;
  201. src += src_idx >> (ffs(bits) - 1);
  202. src_idx &= bits - 1;
  203. }
  204. shift = dst_idx-src_idx;
  205. first = fb_shifted_pixels_mask_long(p, bits - 1 - dst_idx, bswapmask);
  206. last = ~fb_shifted_pixels_mask_long(p, bits - 1 - ((dst_idx-n) % bits),
  207. bswapmask);
  208. if (!shift) {
  209. // Same alignment for source and dest
  210. if ((unsigned long)dst_idx+1 >= n) {
  211. // Single word
  212. if (last)
  213. first &= last;
  214. FB_WRITEL( comp( FB_READL(src), FB_READL(dst), first), dst);
  215. } else {
  216. // Multiple destination words
  217. // Leading bits
  218. if (first != ~0UL) {
  219. FB_WRITEL( comp( FB_READL(src), FB_READL(dst), first), dst);
  220. dst--;
  221. src--;
  222. n -= dst_idx+1;
  223. }
  224. // Main chunk
  225. n /= bits;
  226. while (n >= 8) {
  227. FB_WRITEL(FB_READL(src--), dst--);
  228. FB_WRITEL(FB_READL(src--), dst--);
  229. FB_WRITEL(FB_READL(src--), dst--);
  230. FB_WRITEL(FB_READL(src--), dst--);
  231. FB_WRITEL(FB_READL(src--), dst--);
  232. FB_WRITEL(FB_READL(src--), dst--);
  233. FB_WRITEL(FB_READL(src--), dst--);
  234. FB_WRITEL(FB_READL(src--), dst--);
  235. n -= 8;
  236. }
  237. while (n--)
  238. FB_WRITEL(FB_READL(src--), dst--);
  239. // Trailing bits
  240. if (last)
  241. FB_WRITEL( comp( FB_READL(src), FB_READL(dst), last), dst);
  242. }
  243. } else {
  244. // Different alignment for source and dest
  245. unsigned long d0, d1;
  246. int m;
  247. int const left = -shift & (bits-1);
  248. int const right = shift & (bits-1);
  249. bswapmask &= shift;
  250. if ((unsigned long)dst_idx+1 >= n) {
  251. // Single destination word
  252. if (last)
  253. first &= last;
  254. d0 = FB_READL(src);
  255. if (shift < 0) {
  256. // Single source word
  257. d0 <<= left;
  258. } else if (1+(unsigned long)src_idx >= n) {
  259. // Single source word
  260. d0 >>= right;
  261. } else {
  262. // 2 source words
  263. d1 = FB_READL(src - 1);
  264. d1 = fb_rev_pixels_in_long(d1, bswapmask);
  265. d0 = d0>>right | d1<<left;
  266. }
  267. d0 = fb_rev_pixels_in_long(d0, bswapmask);
  268. FB_WRITEL(comp(d0, FB_READL(dst), first), dst);
  269. } else {
  270. // Multiple destination words
  271. /** We must always remember the last value read, because in case
  272. SRC and DST overlap bitwise (e.g. when moving just one pixel in
  273. 1bpp), we always collect one full long for DST and that might
  274. overlap with the current long from SRC. We store this value in
  275. 'd0'. */
  276. d0 = FB_READL(src--);
  277. d0 = fb_rev_pixels_in_long(d0, bswapmask);
  278. // Leading bits
  279. if (shift < 0) {
  280. // Single source word
  281. d1 = d0;
  282. d0 <<= left;
  283. } else {
  284. // 2 source words
  285. d1 = FB_READL(src--);
  286. d1 = fb_rev_pixels_in_long(d1, bswapmask);
  287. d0 = d0>>right | d1<<left;
  288. }
  289. d0 = fb_rev_pixels_in_long(d0, bswapmask);
  290. FB_WRITEL(comp(d0, FB_READL(dst), first), dst);
  291. d0 = d1;
  292. dst--;
  293. n -= dst_idx+1;
  294. // Main chunk
  295. m = n % bits;
  296. n /= bits;
  297. while ((n >= 4) && !bswapmask) {
  298. d1 = FB_READL(src--);
  299. FB_WRITEL(d0 >> right | d1 << left, dst--);
  300. d0 = d1;
  301. d1 = FB_READL(src--);
  302. FB_WRITEL(d0 >> right | d1 << left, dst--);
  303. d0 = d1;
  304. d1 = FB_READL(src--);
  305. FB_WRITEL(d0 >> right | d1 << left, dst--);
  306. d0 = d1;
  307. d1 = FB_READL(src--);
  308. FB_WRITEL(d0 >> right | d1 << left, dst--);
  309. d0 = d1;
  310. n -= 4;
  311. }
  312. while (n--) {
  313. d1 = FB_READL(src--);
  314. d1 = fb_rev_pixels_in_long(d1, bswapmask);
  315. d0 = d0 >> right | d1 << left;
  316. d0 = fb_rev_pixels_in_long(d0, bswapmask);
  317. FB_WRITEL(d0, dst--);
  318. d0 = d1;
  319. }
  320. // Trailing bits
  321. if (last) {
  322. if (m <= left) {
  323. // Single source word
  324. d0 >>= right;
  325. } else {
  326. // 2 source words
  327. d1 = FB_READL(src);
  328. d1 = fb_rev_pixels_in_long(d1,
  329. bswapmask);
  330. d0 = d0>>right | d1<<left;
  331. }
  332. d0 = fb_rev_pixels_in_long(d0, bswapmask);
  333. FB_WRITEL(comp(d0, FB_READL(dst), last), dst);
  334. }
  335. }
  336. }
  337. }
  338. void cfb_copyarea(struct fb_info *p, const struct fb_copyarea *area)
  339. {
  340. u32 dx = area->dx, dy = area->dy, sx = area->sx, sy = area->sy;
  341. u32 height = area->height, width = area->width;
  342. unsigned long const bits_per_line = p->fix.line_length*8u;
  343. unsigned long __iomem *dst = NULL, *src = NULL;
  344. int bits = BITS_PER_LONG, bytes = bits >> 3;
  345. int dst_idx = 0, src_idx = 0, rev_copy = 0;
  346. u32 bswapmask = fb_compute_bswapmask(p);
  347. if (p->state != FBINFO_STATE_RUNNING)
  348. return;
  349. /* if the beginning of the target area might overlap with the end of
  350. the source area, be have to copy the area reverse. */
  351. if ((dy == sy && dx > sx) || (dy > sy)) {
  352. dy += height;
  353. sy += height;
  354. rev_copy = 1;
  355. }
  356. // split the base of the framebuffer into a long-aligned address and the
  357. // index of the first bit
  358. dst = src = (unsigned long __iomem *)((unsigned long)p->screen_base & ~(bytes-1));
  359. dst_idx = src_idx = 8*((unsigned long)p->screen_base & (bytes-1));
  360. // add offset of source and target area
  361. dst_idx += dy*bits_per_line + dx*p->var.bits_per_pixel;
  362. src_idx += sy*bits_per_line + sx*p->var.bits_per_pixel;
  363. if (p->fbops->fb_sync)
  364. p->fbops->fb_sync(p);
  365. if (rev_copy) {
  366. while (height--) {
  367. dst_idx -= bits_per_line;
  368. src_idx -= bits_per_line;
  369. dst += dst_idx >> (ffs(bits) - 1);
  370. dst_idx &= (bytes - 1);
  371. src += src_idx >> (ffs(bits) - 1);
  372. src_idx &= (bytes - 1);
  373. bitcpy_rev(p, dst, dst_idx, src, src_idx, bits,
  374. width*p->var.bits_per_pixel, bswapmask);
  375. }
  376. } else {
  377. while (height--) {
  378. dst += dst_idx >> (ffs(bits) - 1);
  379. dst_idx &= (bytes - 1);
  380. src += src_idx >> (ffs(bits) - 1);
  381. src_idx &= (bytes - 1);
  382. bitcpy(p, dst, dst_idx, src, src_idx, bits,
  383. width*p->var.bits_per_pixel, bswapmask);
  384. dst_idx += bits_per_line;
  385. src_idx += bits_per_line;
  386. }
  387. }
  388. }
  389. EXPORT_SYMBOL(cfb_copyarea);
  390. MODULE_AUTHOR("James Simmons <jsimmons@users.sf.net>");
  391. MODULE_DESCRIPTION("Generic software accelerated copyarea");
  392. MODULE_LICENSE("GPL");