matroxfb_accel.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. *
  3. * Hardware accelerated Matrox Millennium I, II, Mystique, G100, G200 and G400
  4. *
  5. * (c) 1998-2002 Petr Vandrovec <vandrove@vc.cvut.cz>
  6. *
  7. * Version: 1.65 2002/08/14
  8. *
  9. * MTRR stuff: 1998 Tom Rini <trini@kernel.crashing.org>
  10. *
  11. * Contributors: "menion?" <menion@mindless.com>
  12. * Betatesting, fixes, ideas
  13. *
  14. * "Kurt Garloff" <garloff@suse.de>
  15. * Betatesting, fixes, ideas, videomodes, videomodes timmings
  16. *
  17. * "Tom Rini" <trini@kernel.crashing.org>
  18. * MTRR stuff, PPC cleanups, betatesting, fixes, ideas
  19. *
  20. * "Bibek Sahu" <scorpio@dodds.net>
  21. * Access device through readb|w|l and write b|w|l
  22. * Extensive debugging stuff
  23. *
  24. * "Daniel Haun" <haund@usa.net>
  25. * Testing, hardware cursor fixes
  26. *
  27. * "Scott Wood" <sawst46+@pitt.edu>
  28. * Fixes
  29. *
  30. * "Gerd Knorr" <kraxel@goldbach.isdn.cs.tu-berlin.de>
  31. * Betatesting
  32. *
  33. * "Kelly French" <targon@hazmat.com>
  34. * "Fernando Herrera" <fherrera@eurielec.etsit.upm.es>
  35. * Betatesting, bug reporting
  36. *
  37. * "Pablo Bianucci" <pbian@pccp.com.ar>
  38. * Fixes, ideas, betatesting
  39. *
  40. * "Inaky Perez Gonzalez" <inaky@peloncho.fis.ucm.es>
  41. * Fixes, enhandcements, ideas, betatesting
  42. *
  43. * "Ryuichi Oikawa" <roikawa@rr.iiij4u.or.jp>
  44. * PPC betatesting, PPC support, backward compatibility
  45. *
  46. * "Paul Womar" <Paul@pwomar.demon.co.uk>
  47. * "Owen Waller" <O.Waller@ee.qub.ac.uk>
  48. * PPC betatesting
  49. *
  50. * "Thomas Pornin" <pornin@bolet.ens.fr>
  51. * Alpha betatesting
  52. *
  53. * "Pieter van Leuven" <pvl@iae.nl>
  54. * "Ulf Jaenicke-Roessler" <ujr@physik.phy.tu-dresden.de>
  55. * G100 testing
  56. *
  57. * "H. Peter Arvin" <hpa@transmeta.com>
  58. * Ideas
  59. *
  60. * "Cort Dougan" <cort@cs.nmt.edu>
  61. * CHRP fixes and PReP cleanup
  62. *
  63. * "Mark Vojkovich" <mvojkovi@ucsd.edu>
  64. * G400 support
  65. *
  66. * (following author is not in any relation with this code, but his code
  67. * is included in this driver)
  68. *
  69. * Based on framebuffer driver for VBE 2.0 compliant graphic boards
  70. * (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de>
  71. *
  72. * (following author is not in any relation with this code, but his ideas
  73. * were used when writing this driver)
  74. *
  75. * FreeVBE/AF (Matrox), "Shawn Hargreaves" <shawn@talula.demon.co.uk>
  76. *
  77. */
  78. #include "matroxfb_accel.h"
  79. #include "matroxfb_DAC1064.h"
  80. #include "matroxfb_Ti3026.h"
  81. #include "matroxfb_misc.h"
  82. #define curr_ydstorg(x) ((x)->curr.ydstorg.pixels)
  83. #define mga_ydstlen(y,l) mga_outl(M_YDSTLEN | M_EXEC, ((y) << 16) | (l))
  84. static inline void matrox_cfb4_pal(u_int32_t* pal) {
  85. unsigned int i;
  86. for (i = 0; i < 16; i++) {
  87. pal[i] = i * 0x11111111U;
  88. }
  89. }
  90. static inline void matrox_cfb8_pal(u_int32_t* pal) {
  91. unsigned int i;
  92. for (i = 0; i < 16; i++) {
  93. pal[i] = i * 0x01010101U;
  94. }
  95. }
  96. static void matroxfb_copyarea(struct fb_info* info, const struct fb_copyarea* area);
  97. static void matroxfb_fillrect(struct fb_info* info, const struct fb_fillrect* rect);
  98. static void matroxfb_imageblit(struct fb_info* info, const struct fb_image* image);
  99. static void matroxfb_cfb4_fillrect(struct fb_info* info, const struct fb_fillrect* rect);
  100. static void matroxfb_cfb4_copyarea(struct fb_info* info, const struct fb_copyarea* area);
  101. void matrox_cfbX_init(struct matrox_fb_info *minfo)
  102. {
  103. u_int32_t maccess;
  104. u_int32_t mpitch;
  105. u_int32_t mopmode;
  106. int accel;
  107. DBG(__func__)
  108. mpitch = minfo->fbcon.var.xres_virtual;
  109. minfo->fbops.fb_copyarea = cfb_copyarea;
  110. minfo->fbops.fb_fillrect = cfb_fillrect;
  111. minfo->fbops.fb_imageblit = cfb_imageblit;
  112. minfo->fbops.fb_cursor = NULL;
  113. accel = (minfo->fbcon.var.accel_flags & FB_ACCELF_TEXT) == FB_ACCELF_TEXT;
  114. switch (minfo->fbcon.var.bits_per_pixel) {
  115. case 4: maccess = 0x00000000; /* accelerate as 8bpp video */
  116. mpitch = (mpitch >> 1) | 0x8000; /* disable linearization */
  117. mopmode = M_OPMODE_4BPP;
  118. matrox_cfb4_pal(minfo->cmap);
  119. if (accel && !(mpitch & 1)) {
  120. minfo->fbops.fb_copyarea = matroxfb_cfb4_copyarea;
  121. minfo->fbops.fb_fillrect = matroxfb_cfb4_fillrect;
  122. }
  123. break;
  124. case 8: maccess = 0x00000000;
  125. mopmode = M_OPMODE_8BPP;
  126. matrox_cfb8_pal(minfo->cmap);
  127. if (accel) {
  128. minfo->fbops.fb_copyarea = matroxfb_copyarea;
  129. minfo->fbops.fb_fillrect = matroxfb_fillrect;
  130. minfo->fbops.fb_imageblit = matroxfb_imageblit;
  131. }
  132. break;
  133. case 16: if (minfo->fbcon.var.green.length == 5)
  134. maccess = 0xC0000001;
  135. else
  136. maccess = 0x40000001;
  137. mopmode = M_OPMODE_16BPP;
  138. if (accel) {
  139. minfo->fbops.fb_copyarea = matroxfb_copyarea;
  140. minfo->fbops.fb_fillrect = matroxfb_fillrect;
  141. minfo->fbops.fb_imageblit = matroxfb_imageblit;
  142. }
  143. break;
  144. case 24: maccess = 0x00000003;
  145. mopmode = M_OPMODE_24BPP;
  146. if (accel) {
  147. minfo->fbops.fb_copyarea = matroxfb_copyarea;
  148. minfo->fbops.fb_fillrect = matroxfb_fillrect;
  149. minfo->fbops.fb_imageblit = matroxfb_imageblit;
  150. }
  151. break;
  152. case 32: maccess = 0x00000002;
  153. mopmode = M_OPMODE_32BPP;
  154. if (accel) {
  155. minfo->fbops.fb_copyarea = matroxfb_copyarea;
  156. minfo->fbops.fb_fillrect = matroxfb_fillrect;
  157. minfo->fbops.fb_imageblit = matroxfb_imageblit;
  158. }
  159. break;
  160. default: maccess = 0x00000000;
  161. mopmode = 0x00000000;
  162. break; /* turn off acceleration!!! */
  163. }
  164. mga_fifo(8);
  165. mga_outl(M_PITCH, mpitch);
  166. mga_outl(M_YDSTORG, curr_ydstorg(minfo));
  167. if (minfo->capable.plnwt)
  168. mga_outl(M_PLNWT, -1);
  169. if (minfo->capable.srcorg) {
  170. mga_outl(M_SRCORG, 0);
  171. mga_outl(M_DSTORG, 0);
  172. }
  173. mga_outl(M_OPMODE, mopmode);
  174. mga_outl(M_CXBNDRY, 0xFFFF0000);
  175. mga_outl(M_YTOP, 0);
  176. mga_outl(M_YBOT, 0x01FFFFFF);
  177. mga_outl(M_MACCESS, maccess);
  178. minfo->accel.m_dwg_rect = M_DWG_TRAP | M_DWG_SOLID | M_DWG_ARZERO | M_DWG_SGNZERO | M_DWG_SHIFTZERO;
  179. if (isMilleniumII(minfo)) minfo->accel.m_dwg_rect |= M_DWG_TRANSC;
  180. minfo->accel.m_opmode = mopmode;
  181. }
  182. EXPORT_SYMBOL(matrox_cfbX_init);
  183. static void matrox_accel_bmove(struct matrox_fb_info *minfo, int vxres, int sy,
  184. int sx, int dy, int dx, int height, int width)
  185. {
  186. int start, end;
  187. CRITFLAGS
  188. DBG(__func__)
  189. CRITBEGIN
  190. if ((dy < sy) || ((dy == sy) && (dx <= sx))) {
  191. mga_fifo(2);
  192. mga_outl(M_DWGCTL, M_DWG_BITBLT | M_DWG_SHIFTZERO | M_DWG_SGNZERO |
  193. M_DWG_BFCOL | M_DWG_REPLACE);
  194. mga_outl(M_AR5, vxres);
  195. width--;
  196. start = sy*vxres+sx+curr_ydstorg(minfo);
  197. end = start+width;
  198. } else {
  199. mga_fifo(3);
  200. mga_outl(M_DWGCTL, M_DWG_BITBLT | M_DWG_SHIFTZERO | M_DWG_BFCOL | M_DWG_REPLACE);
  201. mga_outl(M_SGN, 5);
  202. mga_outl(M_AR5, -vxres);
  203. width--;
  204. end = (sy+height-1)*vxres+sx+curr_ydstorg(minfo);
  205. start = end+width;
  206. dy += height-1;
  207. }
  208. mga_fifo(4);
  209. mga_outl(M_AR0, end);
  210. mga_outl(M_AR3, start);
  211. mga_outl(M_FXBNDRY, ((dx+width)<<16) | dx);
  212. mga_ydstlen(dy, height);
  213. WaitTillIdle();
  214. CRITEND
  215. }
  216. static void matrox_accel_bmove_lin(struct matrox_fb_info *minfo, int vxres,
  217. int sy, int sx, int dy, int dx, int height,
  218. int width)
  219. {
  220. int start, end;
  221. CRITFLAGS
  222. DBG(__func__)
  223. CRITBEGIN
  224. if ((dy < sy) || ((dy == sy) && (dx <= sx))) {
  225. mga_fifo(2);
  226. mga_outl(M_DWGCTL, M_DWG_BITBLT | M_DWG_SHIFTZERO | M_DWG_SGNZERO |
  227. M_DWG_BFCOL | M_DWG_REPLACE);
  228. mga_outl(M_AR5, vxres);
  229. width--;
  230. start = sy*vxres+sx+curr_ydstorg(minfo);
  231. end = start+width;
  232. } else {
  233. mga_fifo(3);
  234. mga_outl(M_DWGCTL, M_DWG_BITBLT | M_DWG_SHIFTZERO | M_DWG_BFCOL | M_DWG_REPLACE);
  235. mga_outl(M_SGN, 5);
  236. mga_outl(M_AR5, -vxres);
  237. width--;
  238. end = (sy+height-1)*vxres+sx+curr_ydstorg(minfo);
  239. start = end+width;
  240. dy += height-1;
  241. }
  242. mga_fifo(5);
  243. mga_outl(M_AR0, end);
  244. mga_outl(M_AR3, start);
  245. mga_outl(M_FXBNDRY, ((dx+width)<<16) | dx);
  246. mga_outl(M_YDST, dy*vxres >> 5);
  247. mga_outl(M_LEN | M_EXEC, height);
  248. WaitTillIdle();
  249. CRITEND
  250. }
  251. static void matroxfb_cfb4_copyarea(struct fb_info* info, const struct fb_copyarea* area) {
  252. struct matrox_fb_info *minfo = info2minfo(info);
  253. if ((area->sx | area->dx | area->width) & 1)
  254. cfb_copyarea(info, area);
  255. else
  256. matrox_accel_bmove_lin(minfo, minfo->fbcon.var.xres_virtual >> 1, area->sy, area->sx >> 1, area->dy, area->dx >> 1, area->height, area->width >> 1);
  257. }
  258. static void matroxfb_copyarea(struct fb_info* info, const struct fb_copyarea* area) {
  259. struct matrox_fb_info *minfo = info2minfo(info);
  260. matrox_accel_bmove(minfo, minfo->fbcon.var.xres_virtual, area->sy, area->sx, area->dy, area->dx, area->height, area->width);
  261. }
  262. static void matroxfb_accel_clear(struct matrox_fb_info *minfo, u_int32_t color,
  263. int sy, int sx, int height, int width)
  264. {
  265. CRITFLAGS
  266. DBG(__func__)
  267. CRITBEGIN
  268. mga_fifo(5);
  269. mga_outl(M_DWGCTL, minfo->accel.m_dwg_rect | M_DWG_REPLACE);
  270. mga_outl(M_FCOL, color);
  271. mga_outl(M_FXBNDRY, ((sx + width) << 16) | sx);
  272. mga_ydstlen(sy, height);
  273. WaitTillIdle();
  274. CRITEND
  275. }
  276. static void matroxfb_fillrect(struct fb_info* info, const struct fb_fillrect* rect) {
  277. struct matrox_fb_info *minfo = info2minfo(info);
  278. switch (rect->rop) {
  279. case ROP_COPY:
  280. matroxfb_accel_clear(minfo, ((u_int32_t *)info->pseudo_palette)[rect->color], rect->dy, rect->dx, rect->height, rect->width);
  281. break;
  282. }
  283. }
  284. static void matroxfb_cfb4_clear(struct matrox_fb_info *minfo, u_int32_t bgx,
  285. int sy, int sx, int height, int width)
  286. {
  287. int whattodo;
  288. CRITFLAGS
  289. DBG(__func__)
  290. CRITBEGIN
  291. whattodo = 0;
  292. if (sx & 1) {
  293. sx ++;
  294. if (!width) return;
  295. width --;
  296. whattodo = 1;
  297. }
  298. if (width & 1) {
  299. whattodo |= 2;
  300. }
  301. width >>= 1;
  302. sx >>= 1;
  303. if (width) {
  304. mga_fifo(5);
  305. mga_outl(M_DWGCTL, minfo->accel.m_dwg_rect | M_DWG_REPLACE2);
  306. mga_outl(M_FCOL, bgx);
  307. mga_outl(M_FXBNDRY, ((sx + width) << 16) | sx);
  308. mga_outl(M_YDST, sy * minfo->fbcon.var.xres_virtual >> 6);
  309. mga_outl(M_LEN | M_EXEC, height);
  310. WaitTillIdle();
  311. }
  312. if (whattodo) {
  313. u_int32_t step = minfo->fbcon.var.xres_virtual >> 1;
  314. vaddr_t vbase = minfo->video.vbase;
  315. if (whattodo & 1) {
  316. unsigned int uaddr = sy * step + sx - 1;
  317. u_int32_t loop;
  318. u_int8_t bgx2 = bgx & 0xF0;
  319. for (loop = height; loop > 0; loop --) {
  320. mga_writeb(vbase, uaddr, (mga_readb(vbase, uaddr) & 0x0F) | bgx2);
  321. uaddr += step;
  322. }
  323. }
  324. if (whattodo & 2) {
  325. unsigned int uaddr = sy * step + sx + width;
  326. u_int32_t loop;
  327. u_int8_t bgx2 = bgx & 0x0F;
  328. for (loop = height; loop > 0; loop --) {
  329. mga_writeb(vbase, uaddr, (mga_readb(vbase, uaddr) & 0xF0) | bgx2);
  330. uaddr += step;
  331. }
  332. }
  333. }
  334. CRITEND
  335. }
  336. static void matroxfb_cfb4_fillrect(struct fb_info* info, const struct fb_fillrect* rect) {
  337. struct matrox_fb_info *minfo = info2minfo(info);
  338. switch (rect->rop) {
  339. case ROP_COPY:
  340. matroxfb_cfb4_clear(minfo, ((u_int32_t *)info->pseudo_palette)[rect->color], rect->dy, rect->dx, rect->height, rect->width);
  341. break;
  342. }
  343. }
  344. static void matroxfb_1bpp_imageblit(struct matrox_fb_info *minfo, u_int32_t fgx,
  345. u_int32_t bgx, const u_int8_t *chardata,
  346. int width, int height, int yy, int xx)
  347. {
  348. u_int32_t step;
  349. u_int32_t ydstlen;
  350. u_int32_t xlen;
  351. u_int32_t ar0;
  352. u_int32_t charcell;
  353. u_int32_t fxbndry;
  354. vaddr_t mmio;
  355. int easy;
  356. CRITFLAGS
  357. DBG_HEAVY(__func__);
  358. step = (width + 7) >> 3;
  359. charcell = height * step;
  360. xlen = (charcell + 3) & ~3;
  361. ydstlen = (yy << 16) | height;
  362. if (width == step << 3) {
  363. ar0 = height * width - 1;
  364. easy = 1;
  365. } else {
  366. ar0 = width - 1;
  367. easy = 0;
  368. }
  369. CRITBEGIN
  370. mga_fifo(3);
  371. if (easy)
  372. mga_outl(M_DWGCTL, M_DWG_ILOAD | M_DWG_SGNZERO | M_DWG_SHIFTZERO | M_DWG_BMONOWF | M_DWG_LINEAR | M_DWG_REPLACE);
  373. else
  374. mga_outl(M_DWGCTL, M_DWG_ILOAD | M_DWG_SGNZERO | M_DWG_SHIFTZERO | M_DWG_BMONOWF | M_DWG_REPLACE);
  375. mga_outl(M_FCOL, fgx);
  376. mga_outl(M_BCOL, bgx);
  377. fxbndry = ((xx + width - 1) << 16) | xx;
  378. mmio = minfo->mmio.vbase;
  379. mga_fifo(6);
  380. mga_writel(mmio, M_FXBNDRY, fxbndry);
  381. mga_writel(mmio, M_AR0, ar0);
  382. mga_writel(mmio, M_AR3, 0);
  383. if (easy) {
  384. mga_writel(mmio, M_YDSTLEN | M_EXEC, ydstlen);
  385. mga_memcpy_toio(mmio, chardata, xlen);
  386. } else {
  387. mga_writel(mmio, M_AR5, 0);
  388. mga_writel(mmio, M_YDSTLEN | M_EXEC, ydstlen);
  389. if ((step & 3) == 0) {
  390. /* Great. Source has 32bit aligned lines, so we can feed them
  391. directly to the accelerator. */
  392. mga_memcpy_toio(mmio, chardata, charcell);
  393. } else if (step == 1) {
  394. /* Special case for 1..8bit widths */
  395. while (height--) {
  396. #if defined(__BIG_ENDIAN)
  397. fb_writel((*chardata) << 24, mmio.vaddr);
  398. #else
  399. fb_writel(*chardata, mmio.vaddr);
  400. #endif
  401. chardata++;
  402. }
  403. } else if (step == 2) {
  404. /* Special case for 9..15bit widths */
  405. while (height--) {
  406. #if defined(__BIG_ENDIAN)
  407. fb_writel((*(u_int16_t*)chardata) << 16, mmio.vaddr);
  408. #else
  409. fb_writel(*(u_int16_t*)chardata, mmio.vaddr);
  410. #endif
  411. chardata += 2;
  412. }
  413. } else {
  414. /* Tell... well, why bother... */
  415. while (height--) {
  416. size_t i;
  417. for (i = 0; i < step; i += 4) {
  418. /* Hope that there are at least three readable bytes beyond the end of bitmap */
  419. fb_writel(get_unaligned((u_int32_t*)(chardata + i)),mmio.vaddr);
  420. }
  421. chardata += step;
  422. }
  423. }
  424. }
  425. WaitTillIdle();
  426. CRITEND
  427. }
  428. static void matroxfb_imageblit(struct fb_info* info, const struct fb_image* image) {
  429. struct matrox_fb_info *minfo = info2minfo(info);
  430. DBG_HEAVY(__func__);
  431. if (image->depth == 1) {
  432. u_int32_t fgx, bgx;
  433. fgx = ((u_int32_t*)info->pseudo_palette)[image->fg_color];
  434. bgx = ((u_int32_t*)info->pseudo_palette)[image->bg_color];
  435. matroxfb_1bpp_imageblit(minfo, fgx, bgx, image->data, image->width, image->height, image->dy, image->dx);
  436. } else {
  437. /* Danger! image->depth is useless: logo painting code always
  438. passes framebuffer color depth here, although logo data are
  439. always 8bpp and info->pseudo_palette is changed to contain
  440. logo palette to be used (but only for true/direct-color... sic...).
  441. So do it completely in software... */
  442. cfb_imageblit(info, image);
  443. }
  444. }
  445. MODULE_LICENSE("GPL");