cfbcopyarea.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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, 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(dst_idx, bswapmask);
  50. last = ~fb_shifted_pixels_mask_long((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(unsigned long __iomem *dst, int dst_idx, const unsigned long __iomem *src,
  189. int src_idx, int bits, unsigned n, u32 bswapmask)
  190. {
  191. unsigned long first, last;
  192. int shift;
  193. dst += (n-1)/bits;
  194. src += (n-1)/bits;
  195. if ((n-1) % bits) {
  196. dst_idx += (n-1) % bits;
  197. dst += dst_idx >> (ffs(bits) - 1);
  198. dst_idx &= bits - 1;
  199. src_idx += (n-1) % bits;
  200. src += src_idx >> (ffs(bits) - 1);
  201. src_idx &= bits - 1;
  202. }
  203. shift = dst_idx-src_idx;
  204. first = fb_shifted_pixels_mask_long(bits - 1 - dst_idx, bswapmask);
  205. last = ~fb_shifted_pixels_mask_long(bits - 1 - ((dst_idx-n) % bits), bswapmask);
  206. if (!shift) {
  207. // Same alignment for source and dest
  208. if ((unsigned long)dst_idx+1 >= n) {
  209. // Single word
  210. if (last)
  211. first &= last;
  212. FB_WRITEL( comp( FB_READL(src), FB_READL(dst), first), dst);
  213. } else {
  214. // Multiple destination words
  215. // Leading bits
  216. if (first != ~0UL) {
  217. FB_WRITEL( comp( FB_READL(src), FB_READL(dst), first), dst);
  218. dst--;
  219. src--;
  220. n -= dst_idx+1;
  221. }
  222. // Main chunk
  223. n /= bits;
  224. while (n >= 8) {
  225. FB_WRITEL(FB_READL(src--), dst--);
  226. FB_WRITEL(FB_READL(src--), dst--);
  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. n -= 8;
  234. }
  235. while (n--)
  236. FB_WRITEL(FB_READL(src--), dst--);
  237. // Trailing bits
  238. if (last)
  239. FB_WRITEL( comp( FB_READL(src), FB_READL(dst), last), dst);
  240. }
  241. } else {
  242. // Different alignment for source and dest
  243. unsigned long d0, d1;
  244. int m;
  245. int const left = -shift & (bits-1);
  246. int const right = shift & (bits-1);
  247. bswapmask &= shift;
  248. if ((unsigned long)dst_idx+1 >= n) {
  249. // Single destination word
  250. if (last)
  251. first &= last;
  252. d0 = FB_READL(src);
  253. if (shift < 0) {
  254. // Single source word
  255. d0 <<= left;
  256. } else if (1+(unsigned long)src_idx >= n) {
  257. // Single source word
  258. d0 >>= right;
  259. } else {
  260. // 2 source words
  261. d1 = FB_READL(src - 1);
  262. d1 = fb_rev_pixels_in_long(d1, bswapmask);
  263. d0 = d0>>right | d1<<left;
  264. }
  265. d0 = fb_rev_pixels_in_long(d0, bswapmask);
  266. FB_WRITEL(comp(d0, FB_READL(dst), first), dst);
  267. } else {
  268. // Multiple destination words
  269. /** We must always remember the last value read, because in case
  270. SRC and DST overlap bitwise (e.g. when moving just one pixel in
  271. 1bpp), we always collect one full long for DST and that might
  272. overlap with the current long from SRC. We store this value in
  273. 'd0'. */
  274. d0 = FB_READL(src--);
  275. d0 = fb_rev_pixels_in_long(d0, bswapmask);
  276. // Leading bits
  277. if (shift < 0) {
  278. // Single source word
  279. d1 = d0;
  280. d0 <<= left;
  281. } else {
  282. // 2 source words
  283. d1 = FB_READL(src--);
  284. d1 = fb_rev_pixels_in_long(d1, bswapmask);
  285. d0 = d0>>right | d1<<left;
  286. }
  287. d0 = fb_rev_pixels_in_long(d0, bswapmask);
  288. FB_WRITEL(comp(d0, FB_READL(dst), first), dst);
  289. d0 = d1;
  290. dst--;
  291. n -= dst_idx+1;
  292. // Main chunk
  293. m = n % bits;
  294. n /= bits;
  295. while ((n >= 4) && !bswapmask) {
  296. d1 = FB_READL(src--);
  297. FB_WRITEL(d0 >> right | d1 << left, dst--);
  298. d0 = d1;
  299. d1 = FB_READL(src--);
  300. FB_WRITEL(d0 >> right | d1 << left, dst--);
  301. d0 = d1;
  302. d1 = FB_READL(src--);
  303. FB_WRITEL(d0 >> right | d1 << left, dst--);
  304. d0 = d1;
  305. d1 = FB_READL(src--);
  306. FB_WRITEL(d0 >> right | d1 << left, dst--);
  307. d0 = d1;
  308. n -= 4;
  309. }
  310. while (n--) {
  311. d1 = FB_READL(src--);
  312. d1 = fb_rev_pixels_in_long(d1, bswapmask);
  313. d0 = d0 >> right | d1 << left;
  314. d0 = fb_rev_pixels_in_long(d0, bswapmask);
  315. FB_WRITEL(d0, dst--);
  316. d0 = d1;
  317. }
  318. // Trailing bits
  319. if (last) {
  320. if (m <= left) {
  321. // Single source word
  322. d0 >>= right;
  323. } else {
  324. // 2 source words
  325. d1 = FB_READL(src);
  326. d1 = fb_rev_pixels_in_long(d1,
  327. bswapmask);
  328. d0 = d0>>right | d1<<left;
  329. }
  330. d0 = fb_rev_pixels_in_long(d0, bswapmask);
  331. FB_WRITEL(comp(d0, FB_READL(dst), last), dst);
  332. }
  333. }
  334. }
  335. }
  336. void cfb_copyarea(struct fb_info *p, const struct fb_copyarea *area)
  337. {
  338. u32 dx = area->dx, dy = area->dy, sx = area->sx, sy = area->sy;
  339. u32 height = area->height, width = area->width;
  340. unsigned long const bits_per_line = p->fix.line_length*8u;
  341. unsigned long __iomem *dst = NULL, *src = NULL;
  342. int bits = BITS_PER_LONG, bytes = bits >> 3;
  343. int dst_idx = 0, src_idx = 0, rev_copy = 0;
  344. u32 bswapmask = fb_compute_bswapmask(p);
  345. if (p->state != FBINFO_STATE_RUNNING)
  346. return;
  347. /* if the beginning of the target area might overlap with the end of
  348. the source area, be have to copy the area reverse. */
  349. if ((dy == sy && dx > sx) || (dy > sy)) {
  350. dy += height;
  351. sy += height;
  352. rev_copy = 1;
  353. }
  354. // split the base of the framebuffer into a long-aligned address and the
  355. // index of the first bit
  356. dst = src = (unsigned long __iomem *)((unsigned long)p->screen_base & ~(bytes-1));
  357. dst_idx = src_idx = 8*((unsigned long)p->screen_base & (bytes-1));
  358. // add offset of source and target area
  359. dst_idx += dy*bits_per_line + dx*p->var.bits_per_pixel;
  360. src_idx += sy*bits_per_line + sx*p->var.bits_per_pixel;
  361. if (p->fbops->fb_sync)
  362. p->fbops->fb_sync(p);
  363. if (rev_copy) {
  364. while (height--) {
  365. dst_idx -= bits_per_line;
  366. src_idx -= bits_per_line;
  367. dst += dst_idx >> (ffs(bits) - 1);
  368. dst_idx &= (bytes - 1);
  369. src += src_idx >> (ffs(bits) - 1);
  370. src_idx &= (bytes - 1);
  371. bitcpy_rev(dst, dst_idx, src, src_idx, bits,
  372. width*p->var.bits_per_pixel, bswapmask);
  373. }
  374. } else {
  375. while (height--) {
  376. dst += dst_idx >> (ffs(bits) - 1);
  377. dst_idx &= (bytes - 1);
  378. src += src_idx >> (ffs(bits) - 1);
  379. src_idx &= (bytes - 1);
  380. bitcpy(dst, dst_idx, src, src_idx, bits,
  381. width*p->var.bits_per_pixel, bswapmask);
  382. dst_idx += bits_per_line;
  383. src_idx += bits_per_line;
  384. }
  385. }
  386. }
  387. EXPORT_SYMBOL(cfb_copyarea);
  388. MODULE_AUTHOR("James Simmons <jsimmons@users.sf.net>");
  389. MODULE_DESCRIPTION("Generic software accelerated copyarea");
  390. MODULE_LICENSE("GPL");