sis_accel.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * SiS 300/540/630[S]/730[S],
  4. * SiS 315[E|PRO]/550/[M]650/651/[M]661[F|M]X/740/[M]741[GX]/330/[M]760[GX],
  5. * XGI V3XT/V5/V8, Z7
  6. * frame buffer driver for Linux kernels >= 2.4.14 and >=2.6.3
  7. *
  8. * 2D acceleration part
  9. *
  10. * Based on the XFree86/X.org driver which is
  11. * Copyright (C) 2001-2005 by Thomas Winischhofer, Vienna, Austria
  12. *
  13. * Author: Thomas Winischhofer <thomas@winischhofer.net>
  14. * (see http://www.winischhofer.net/
  15. * for more information and updates)
  16. */
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/fb.h>
  20. #include <linux/ioport.h>
  21. #include <linux/types.h>
  22. #include <asm/io.h>
  23. #include "sis.h"
  24. #include "sis_accel.h"
  25. static const u8 sisALUConv[] =
  26. {
  27. 0x00, /* dest = 0; 0, GXclear, 0 */
  28. 0x88, /* dest &= src; DSa, GXand, 0x1 */
  29. 0x44, /* dest = src & ~dest; SDna, GXandReverse, 0x2 */
  30. 0xCC, /* dest = src; S, GXcopy, 0x3 */
  31. 0x22, /* dest &= ~src; DSna, GXandInverted, 0x4 */
  32. 0xAA, /* dest = dest; D, GXnoop, 0x5 */
  33. 0x66, /* dest = ^src; DSx, GXxor, 0x6 */
  34. 0xEE, /* dest |= src; DSo, GXor, 0x7 */
  35. 0x11, /* dest = ~src & ~dest; DSon, GXnor, 0x8 */
  36. 0x99, /* dest ^= ~src ; DSxn, GXequiv, 0x9 */
  37. 0x55, /* dest = ~dest; Dn, GXInvert, 0xA */
  38. 0xDD, /* dest = src|~dest ; SDno, GXorReverse, 0xB */
  39. 0x33, /* dest = ~src; Sn, GXcopyInverted, 0xC */
  40. 0xBB, /* dest |= ~src; DSno, GXorInverted, 0xD */
  41. 0x77, /* dest = ~src|~dest; DSan, GXnand, 0xE */
  42. 0xFF, /* dest = 0xFF; 1, GXset, 0xF */
  43. };
  44. /* same ROP but with Pattern as Source */
  45. static const u8 sisPatALUConv[] =
  46. {
  47. 0x00, /* dest = 0; 0, GXclear, 0 */
  48. 0xA0, /* dest &= src; DPa, GXand, 0x1 */
  49. 0x50, /* dest = src & ~dest; PDna, GXandReverse, 0x2 */
  50. 0xF0, /* dest = src; P, GXcopy, 0x3 */
  51. 0x0A, /* dest &= ~src; DPna, GXandInverted, 0x4 */
  52. 0xAA, /* dest = dest; D, GXnoop, 0x5 */
  53. 0x5A, /* dest = ^src; DPx, GXxor, 0x6 */
  54. 0xFA, /* dest |= src; DPo, GXor, 0x7 */
  55. 0x05, /* dest = ~src & ~dest; DPon, GXnor, 0x8 */
  56. 0xA5, /* dest ^= ~src ; DPxn, GXequiv, 0x9 */
  57. 0x55, /* dest = ~dest; Dn, GXInvert, 0xA */
  58. 0xF5, /* dest = src|~dest ; PDno, GXorReverse, 0xB */
  59. 0x0F, /* dest = ~src; Pn, GXcopyInverted, 0xC */
  60. 0xAF, /* dest |= ~src; DPno, GXorInverted, 0xD */
  61. 0x5F, /* dest = ~src|~dest; DPan, GXnand, 0xE */
  62. 0xFF, /* dest = 0xFF; 1, GXset, 0xF */
  63. };
  64. static const int myrops[] = {
  65. 3, 10, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3
  66. };
  67. /* 300 series ----------------------------------------------------- */
  68. #ifdef CONFIG_FB_SIS_300
  69. static void
  70. SiS300Sync(struct sis_video_info *ivideo)
  71. {
  72. SiS300Idle
  73. }
  74. static void
  75. SiS300SetupForScreenToScreenCopy(struct sis_video_info *ivideo, int xdir, int ydir,
  76. int rop, int trans_color)
  77. {
  78. SiS300SetupDSTColorDepth(ivideo->DstColor);
  79. SiS300SetupSRCPitch(ivideo->video_linelength)
  80. SiS300SetupDSTRect(ivideo->video_linelength, 0xffff)
  81. if(trans_color != -1) {
  82. SiS300SetupROP(0x0A)
  83. SiS300SetupSRCTrans(trans_color)
  84. SiS300SetupCMDFlag(TRANSPARENT_BITBLT)
  85. } else {
  86. SiS300SetupROP(sisALUConv[rop])
  87. }
  88. if(xdir > 0) {
  89. SiS300SetupCMDFlag(X_INC)
  90. }
  91. if(ydir > 0) {
  92. SiS300SetupCMDFlag(Y_INC)
  93. }
  94. }
  95. static void
  96. SiS300SubsequentScreenToScreenCopy(struct sis_video_info *ivideo, int src_x,
  97. int src_y, int dst_x, int dst_y, int width, int height)
  98. {
  99. u32 srcbase = 0, dstbase = 0;
  100. if(src_y >= 2048) {
  101. srcbase = ivideo->video_linelength * src_y;
  102. src_y = 0;
  103. }
  104. if(dst_y >= 2048) {
  105. dstbase = ivideo->video_linelength * dst_y;
  106. dst_y = 0;
  107. }
  108. SiS300SetupSRCBase(srcbase);
  109. SiS300SetupDSTBase(dstbase);
  110. if(!(ivideo->CommandReg & X_INC)) {
  111. src_x += width-1;
  112. dst_x += width-1;
  113. }
  114. if(!(ivideo->CommandReg & Y_INC)) {
  115. src_y += height-1;
  116. dst_y += height-1;
  117. }
  118. SiS300SetupRect(width, height)
  119. SiS300SetupSRCXY(src_x, src_y)
  120. SiS300SetupDSTXY(dst_x, dst_y)
  121. SiS300DoCMD
  122. }
  123. static void
  124. SiS300SetupForSolidFill(struct sis_video_info *ivideo, u32 color, int rop)
  125. {
  126. SiS300SetupPATFG(color)
  127. SiS300SetupDSTRect(ivideo->video_linelength, 0xffff)
  128. SiS300SetupDSTColorDepth(ivideo->DstColor);
  129. SiS300SetupROP(sisPatALUConv[rop])
  130. SiS300SetupCMDFlag(PATFG)
  131. }
  132. static void
  133. SiS300SubsequentSolidFillRect(struct sis_video_info *ivideo, int x, int y, int w, int h)
  134. {
  135. u32 dstbase = 0;
  136. if(y >= 2048) {
  137. dstbase = ivideo->video_linelength * y;
  138. y = 0;
  139. }
  140. SiS300SetupDSTBase(dstbase)
  141. SiS300SetupDSTXY(x,y)
  142. SiS300SetupRect(w,h)
  143. SiS300SetupCMDFlag(X_INC | Y_INC | BITBLT)
  144. SiS300DoCMD
  145. }
  146. #endif
  147. /* 315/330/340 series ---------------------------------------------- */
  148. #ifdef CONFIG_FB_SIS_315
  149. static void
  150. SiS310Sync(struct sis_video_info *ivideo)
  151. {
  152. SiS310Idle
  153. }
  154. static void
  155. SiS310SetupForScreenToScreenCopy(struct sis_video_info *ivideo, int rop, int trans_color)
  156. {
  157. SiS310SetupDSTColorDepth(ivideo->DstColor);
  158. SiS310SetupSRCPitch(ivideo->video_linelength)
  159. SiS310SetupDSTRect(ivideo->video_linelength, 0x0fff)
  160. if(trans_color != -1) {
  161. SiS310SetupROP(0x0A)
  162. SiS310SetupSRCTrans(trans_color)
  163. SiS310SetupCMDFlag(TRANSPARENT_BITBLT)
  164. } else {
  165. SiS310SetupROP(sisALUConv[rop])
  166. /* Set command - not needed, both 0 */
  167. /* SiSSetupCMDFlag(BITBLT | SRCVIDEO) */
  168. }
  169. SiS310SetupCMDFlag(ivideo->SiS310_AccelDepth)
  170. /* The chip is smart enough to know the direction */
  171. }
  172. static void
  173. SiS310SubsequentScreenToScreenCopy(struct sis_video_info *ivideo, int src_x, int src_y,
  174. int dst_x, int dst_y, int width, int height)
  175. {
  176. u32 srcbase = 0, dstbase = 0;
  177. int mymin = min(src_y, dst_y);
  178. int mymax = max(src_y, dst_y);
  179. /* Although the chip knows the direction to use
  180. * if the source and destination areas overlap,
  181. * that logic fails if we fiddle with the bitmap
  182. * addresses. Therefore, we check if the source
  183. * and destination blitting areas overlap and
  184. * adapt the bitmap addresses synchronously
  185. * if the coordinates exceed the valid range.
  186. * The areas do not overlap, we do our
  187. * normal check.
  188. */
  189. if((mymax - mymin) < height) {
  190. if((src_y >= 2048) || (dst_y >= 2048)) {
  191. srcbase = ivideo->video_linelength * mymin;
  192. dstbase = ivideo->video_linelength * mymin;
  193. src_y -= mymin;
  194. dst_y -= mymin;
  195. }
  196. } else {
  197. if(src_y >= 2048) {
  198. srcbase = ivideo->video_linelength * src_y;
  199. src_y = 0;
  200. }
  201. if(dst_y >= 2048) {
  202. dstbase = ivideo->video_linelength * dst_y;
  203. dst_y = 0;
  204. }
  205. }
  206. srcbase += ivideo->video_offset;
  207. dstbase += ivideo->video_offset;
  208. SiS310SetupSRCBase(srcbase);
  209. SiS310SetupDSTBase(dstbase);
  210. SiS310SetupRect(width, height)
  211. SiS310SetupSRCXY(src_x, src_y)
  212. SiS310SetupDSTXY(dst_x, dst_y)
  213. SiS310DoCMD
  214. }
  215. static void
  216. SiS310SetupForSolidFill(struct sis_video_info *ivideo, u32 color, int rop)
  217. {
  218. SiS310SetupPATFG(color)
  219. SiS310SetupDSTRect(ivideo->video_linelength, 0x0fff)
  220. SiS310SetupDSTColorDepth(ivideo->DstColor);
  221. SiS310SetupROP(sisPatALUConv[rop])
  222. SiS310SetupCMDFlag(PATFG | ivideo->SiS310_AccelDepth)
  223. }
  224. static void
  225. SiS310SubsequentSolidFillRect(struct sis_video_info *ivideo, int x, int y, int w, int h)
  226. {
  227. u32 dstbase = 0;
  228. if(y >= 2048) {
  229. dstbase = ivideo->video_linelength * y;
  230. y = 0;
  231. }
  232. dstbase += ivideo->video_offset;
  233. SiS310SetupDSTBase(dstbase)
  234. SiS310SetupDSTXY(x,y)
  235. SiS310SetupRect(w,h)
  236. SiS310SetupCMDFlag(BITBLT)
  237. SiS310DoCMD
  238. }
  239. #endif
  240. /* --------------------------------------------------------------------- */
  241. /* The exported routines */
  242. int sisfb_initaccel(struct sis_video_info *ivideo)
  243. {
  244. #ifdef SISFB_USE_SPINLOCKS
  245. spin_lock_init(&ivideo->lockaccel);
  246. #endif
  247. return 0;
  248. }
  249. void sisfb_syncaccel(struct sis_video_info *ivideo)
  250. {
  251. if(ivideo->sisvga_engine == SIS_300_VGA) {
  252. #ifdef CONFIG_FB_SIS_300
  253. SiS300Sync(ivideo);
  254. #endif
  255. } else {
  256. #ifdef CONFIG_FB_SIS_315
  257. SiS310Sync(ivideo);
  258. #endif
  259. }
  260. }
  261. int fbcon_sis_sync(struct fb_info *info)
  262. {
  263. struct sis_video_info *ivideo = (struct sis_video_info *)info->par;
  264. CRITFLAGS
  265. if((!ivideo->accel) || (!ivideo->engineok))
  266. return 0;
  267. CRITBEGIN
  268. sisfb_syncaccel(ivideo);
  269. CRITEND
  270. return 0;
  271. }
  272. void fbcon_sis_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
  273. {
  274. struct sis_video_info *ivideo = (struct sis_video_info *)info->par;
  275. u32 col = 0;
  276. u32 vxres = info->var.xres_virtual;
  277. u32 vyres = info->var.yres_virtual;
  278. int width, height;
  279. CRITFLAGS
  280. if(info->state != FBINFO_STATE_RUNNING)
  281. return;
  282. if((!ivideo->accel) || (!ivideo->engineok)) {
  283. cfb_fillrect(info, rect);
  284. return;
  285. }
  286. if(!rect->width || !rect->height || rect->dx >= vxres || rect->dy >= vyres)
  287. return;
  288. /* Clipping */
  289. width = ((rect->dx + rect->width) > vxres) ? (vxres - rect->dx) : rect->width;
  290. height = ((rect->dy + rect->height) > vyres) ? (vyres - rect->dy) : rect->height;
  291. switch(info->var.bits_per_pixel) {
  292. case 8: col = rect->color;
  293. break;
  294. case 16:
  295. case 32: col = ((u32 *)(info->pseudo_palette))[rect->color];
  296. break;
  297. }
  298. if(ivideo->sisvga_engine == SIS_300_VGA) {
  299. #ifdef CONFIG_FB_SIS_300
  300. CRITBEGIN
  301. SiS300SetupForSolidFill(ivideo, col, myrops[rect->rop]);
  302. SiS300SubsequentSolidFillRect(ivideo, rect->dx, rect->dy, width, height);
  303. CRITEND
  304. #endif
  305. } else {
  306. #ifdef CONFIG_FB_SIS_315
  307. CRITBEGIN
  308. SiS310SetupForSolidFill(ivideo, col, myrops[rect->rop]);
  309. SiS310SubsequentSolidFillRect(ivideo, rect->dx, rect->dy, width, height);
  310. CRITEND
  311. #endif
  312. }
  313. sisfb_syncaccel(ivideo);
  314. }
  315. void fbcon_sis_copyarea(struct fb_info *info, const struct fb_copyarea *area)
  316. {
  317. struct sis_video_info *ivideo = (struct sis_video_info *)info->par;
  318. u32 vxres = info->var.xres_virtual;
  319. u32 vyres = info->var.yres_virtual;
  320. int width = area->width;
  321. int height = area->height;
  322. CRITFLAGS
  323. if(info->state != FBINFO_STATE_RUNNING)
  324. return;
  325. if((!ivideo->accel) || (!ivideo->engineok)) {
  326. cfb_copyarea(info, area);
  327. return;
  328. }
  329. if(!width || !height ||
  330. area->sx >= vxres || area->sy >= vyres ||
  331. area->dx >= vxres || area->dy >= vyres)
  332. return;
  333. /* Clipping */
  334. if((area->sx + width) > vxres) width = vxres - area->sx;
  335. if((area->dx + width) > vxres) width = vxres - area->dx;
  336. if((area->sy + height) > vyres) height = vyres - area->sy;
  337. if((area->dy + height) > vyres) height = vyres - area->dy;
  338. if(ivideo->sisvga_engine == SIS_300_VGA) {
  339. #ifdef CONFIG_FB_SIS_300
  340. int xdir, ydir;
  341. if(area->sx < area->dx) xdir = 0;
  342. else xdir = 1;
  343. if(area->sy < area->dy) ydir = 0;
  344. else ydir = 1;
  345. CRITBEGIN
  346. SiS300SetupForScreenToScreenCopy(ivideo, xdir, ydir, 3, -1);
  347. SiS300SubsequentScreenToScreenCopy(ivideo, area->sx, area->sy,
  348. area->dx, area->dy, width, height);
  349. CRITEND
  350. #endif
  351. } else {
  352. #ifdef CONFIG_FB_SIS_315
  353. CRITBEGIN
  354. SiS310SetupForScreenToScreenCopy(ivideo, 3, -1);
  355. SiS310SubsequentScreenToScreenCopy(ivideo, area->sx, area->sy,
  356. area->dx, area->dy, width, height);
  357. CRITEND
  358. #endif
  359. }
  360. sisfb_syncaccel(ivideo);
  361. }