matroxfb_accel.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. minfo->accel.m_access = maccess;
  182. minfo->accel.m_pitch = mpitch;
  183. }
  184. EXPORT_SYMBOL(matrox_cfbX_init);
  185. static void matrox_accel_restore_maccess(struct matrox_fb_info *minfo)
  186. {
  187. mga_outl(M_MACCESS, minfo->accel.m_access);
  188. mga_outl(M_PITCH, minfo->accel.m_pitch);
  189. }
  190. static void matrox_accel_bmove(struct matrox_fb_info *minfo, int vxres, int sy,
  191. int sx, int dy, int dx, int height, int width)
  192. {
  193. int start, end;
  194. CRITFLAGS
  195. DBG(__func__)
  196. CRITBEGIN
  197. if ((dy < sy) || ((dy == sy) && (dx <= sx))) {
  198. mga_fifo(4);
  199. matrox_accel_restore_maccess(minfo);
  200. mga_outl(M_DWGCTL, M_DWG_BITBLT | M_DWG_SHIFTZERO | M_DWG_SGNZERO |
  201. M_DWG_BFCOL | M_DWG_REPLACE);
  202. mga_outl(M_AR5, vxres);
  203. width--;
  204. start = sy*vxres+sx+curr_ydstorg(minfo);
  205. end = start+width;
  206. } else {
  207. mga_fifo(5);
  208. matrox_accel_restore_maccess(minfo);
  209. mga_outl(M_DWGCTL, M_DWG_BITBLT | M_DWG_SHIFTZERO | M_DWG_BFCOL | M_DWG_REPLACE);
  210. mga_outl(M_SGN, 5);
  211. mga_outl(M_AR5, -vxres);
  212. width--;
  213. end = (sy+height-1)*vxres+sx+curr_ydstorg(minfo);
  214. start = end+width;
  215. dy += height-1;
  216. }
  217. mga_fifo(6);
  218. matrox_accel_restore_maccess(minfo);
  219. mga_outl(M_AR0, end);
  220. mga_outl(M_AR3, start);
  221. mga_outl(M_FXBNDRY, ((dx+width)<<16) | dx);
  222. mga_ydstlen(dy, height);
  223. WaitTillIdle();
  224. CRITEND
  225. }
  226. static void matrox_accel_bmove_lin(struct matrox_fb_info *minfo, int vxres,
  227. int sy, int sx, int dy, int dx, int height,
  228. int width)
  229. {
  230. int start, end;
  231. CRITFLAGS
  232. DBG(__func__)
  233. CRITBEGIN
  234. if ((dy < sy) || ((dy == sy) && (dx <= sx))) {
  235. mga_fifo(4);
  236. matrox_accel_restore_maccess(minfo);
  237. mga_outl(M_DWGCTL, M_DWG_BITBLT | M_DWG_SHIFTZERO | M_DWG_SGNZERO |
  238. M_DWG_BFCOL | M_DWG_REPLACE);
  239. mga_outl(M_AR5, vxres);
  240. width--;
  241. start = sy*vxres+sx+curr_ydstorg(minfo);
  242. end = start+width;
  243. } else {
  244. mga_fifo(5);
  245. matrox_accel_restore_maccess(minfo);
  246. mga_outl(M_DWGCTL, M_DWG_BITBLT | M_DWG_SHIFTZERO | M_DWG_BFCOL | M_DWG_REPLACE);
  247. mga_outl(M_SGN, 5);
  248. mga_outl(M_AR5, -vxres);
  249. width--;
  250. end = (sy+height-1)*vxres+sx+curr_ydstorg(minfo);
  251. start = end+width;
  252. dy += height-1;
  253. }
  254. mga_fifo(7);
  255. matrox_accel_restore_maccess(minfo);
  256. mga_outl(M_AR0, end);
  257. mga_outl(M_AR3, start);
  258. mga_outl(M_FXBNDRY, ((dx+width)<<16) | dx);
  259. mga_outl(M_YDST, dy*vxres >> 5);
  260. mga_outl(M_LEN | M_EXEC, height);
  261. WaitTillIdle();
  262. CRITEND
  263. }
  264. static void matroxfb_cfb4_copyarea(struct fb_info* info, const struct fb_copyarea* area) {
  265. struct matrox_fb_info *minfo = info2minfo(info);
  266. if ((area->sx | area->dx | area->width) & 1)
  267. cfb_copyarea(info, area);
  268. else
  269. 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);
  270. }
  271. static void matroxfb_copyarea(struct fb_info* info, const struct fb_copyarea* area) {
  272. struct matrox_fb_info *minfo = info2minfo(info);
  273. matrox_accel_bmove(minfo, minfo->fbcon.var.xres_virtual, area->sy, area->sx, area->dy, area->dx, area->height, area->width);
  274. }
  275. static void matroxfb_accel_clear(struct matrox_fb_info *minfo, u_int32_t color,
  276. int sy, int sx, int height, int width)
  277. {
  278. CRITFLAGS
  279. DBG(__func__)
  280. CRITBEGIN
  281. mga_fifo(7);
  282. matrox_accel_restore_maccess(minfo);
  283. mga_outl(M_DWGCTL, minfo->accel.m_dwg_rect | M_DWG_REPLACE);
  284. mga_outl(M_FCOL, color);
  285. mga_outl(M_FXBNDRY, ((sx + width) << 16) | sx);
  286. mga_ydstlen(sy, height);
  287. WaitTillIdle();
  288. CRITEND
  289. }
  290. static void matroxfb_fillrect(struct fb_info* info, const struct fb_fillrect* rect) {
  291. struct matrox_fb_info *minfo = info2minfo(info);
  292. switch (rect->rop) {
  293. case ROP_COPY:
  294. matroxfb_accel_clear(minfo, ((u_int32_t *)info->pseudo_palette)[rect->color], rect->dy, rect->dx, rect->height, rect->width);
  295. break;
  296. }
  297. }
  298. static void matroxfb_cfb4_clear(struct matrox_fb_info *minfo, u_int32_t bgx,
  299. int sy, int sx, int height, int width)
  300. {
  301. int whattodo;
  302. CRITFLAGS
  303. DBG(__func__)
  304. CRITBEGIN
  305. whattodo = 0;
  306. if (sx & 1) {
  307. sx ++;
  308. if (!width) return;
  309. width --;
  310. whattodo = 1;
  311. }
  312. if (width & 1) {
  313. whattodo |= 2;
  314. }
  315. width >>= 1;
  316. sx >>= 1;
  317. if (width) {
  318. mga_fifo(7);
  319. matrox_accel_restore_maccess(minfo);
  320. mga_outl(M_DWGCTL, minfo->accel.m_dwg_rect | M_DWG_REPLACE2);
  321. mga_outl(M_FCOL, bgx);
  322. mga_outl(M_FXBNDRY, ((sx + width) << 16) | sx);
  323. mga_outl(M_YDST, sy * minfo->fbcon.var.xres_virtual >> 6);
  324. mga_outl(M_LEN | M_EXEC, height);
  325. WaitTillIdle();
  326. }
  327. if (whattodo) {
  328. u_int32_t step = minfo->fbcon.var.xres_virtual >> 1;
  329. vaddr_t vbase = minfo->video.vbase;
  330. if (whattodo & 1) {
  331. unsigned int uaddr = sy * step + sx - 1;
  332. u_int32_t loop;
  333. u_int8_t bgx2 = bgx & 0xF0;
  334. for (loop = height; loop > 0; loop --) {
  335. mga_writeb(vbase, uaddr, (mga_readb(vbase, uaddr) & 0x0F) | bgx2);
  336. uaddr += step;
  337. }
  338. }
  339. if (whattodo & 2) {
  340. unsigned int uaddr = sy * step + sx + width;
  341. u_int32_t loop;
  342. u_int8_t bgx2 = bgx & 0x0F;
  343. for (loop = height; loop > 0; loop --) {
  344. mga_writeb(vbase, uaddr, (mga_readb(vbase, uaddr) & 0xF0) | bgx2);
  345. uaddr += step;
  346. }
  347. }
  348. }
  349. CRITEND
  350. }
  351. static void matroxfb_cfb4_fillrect(struct fb_info* info, const struct fb_fillrect* rect) {
  352. struct matrox_fb_info *minfo = info2minfo(info);
  353. switch (rect->rop) {
  354. case ROP_COPY:
  355. matroxfb_cfb4_clear(minfo, ((u_int32_t *)info->pseudo_palette)[rect->color], rect->dy, rect->dx, rect->height, rect->width);
  356. break;
  357. }
  358. }
  359. static void matroxfb_1bpp_imageblit(struct matrox_fb_info *minfo, u_int32_t fgx,
  360. u_int32_t bgx, const u_int8_t *chardata,
  361. int width, int height, int yy, int xx)
  362. {
  363. u_int32_t step;
  364. u_int32_t ydstlen;
  365. u_int32_t xlen;
  366. u_int32_t ar0;
  367. u_int32_t charcell;
  368. u_int32_t fxbndry;
  369. vaddr_t mmio;
  370. int easy;
  371. CRITFLAGS
  372. DBG_HEAVY(__func__);
  373. step = (width + 7) >> 3;
  374. charcell = height * step;
  375. xlen = (charcell + 3) & ~3;
  376. ydstlen = (yy << 16) | height;
  377. if (width == step << 3) {
  378. ar0 = height * width - 1;
  379. easy = 1;
  380. } else {
  381. ar0 = width - 1;
  382. easy = 0;
  383. }
  384. CRITBEGIN
  385. mga_fifo(5);
  386. matrox_accel_restore_maccess(minfo);
  387. if (easy)
  388. mga_outl(M_DWGCTL, M_DWG_ILOAD | M_DWG_SGNZERO | M_DWG_SHIFTZERO | M_DWG_BMONOWF | M_DWG_LINEAR | M_DWG_REPLACE);
  389. else
  390. mga_outl(M_DWGCTL, M_DWG_ILOAD | M_DWG_SGNZERO | M_DWG_SHIFTZERO | M_DWG_BMONOWF | M_DWG_REPLACE);
  391. mga_outl(M_FCOL, fgx);
  392. mga_outl(M_BCOL, bgx);
  393. fxbndry = ((xx + width - 1) << 16) | xx;
  394. mmio = minfo->mmio.vbase;
  395. mga_fifo(8);
  396. matrox_accel_restore_maccess(minfo);
  397. mga_writel(mmio, M_FXBNDRY, fxbndry);
  398. mga_writel(mmio, M_AR0, ar0);
  399. mga_writel(mmio, M_AR3, 0);
  400. if (easy) {
  401. mga_writel(mmio, M_YDSTLEN | M_EXEC, ydstlen);
  402. mga_memcpy_toio(mmio, chardata, xlen);
  403. } else {
  404. mga_writel(mmio, M_AR5, 0);
  405. mga_writel(mmio, M_YDSTLEN | M_EXEC, ydstlen);
  406. if ((step & 3) == 0) {
  407. /* Great. Source has 32bit aligned lines, so we can feed them
  408. directly to the accelerator. */
  409. mga_memcpy_toio(mmio, chardata, charcell);
  410. } else if (step == 1) {
  411. /* Special case for 1..8bit widths */
  412. while (height--) {
  413. #if defined(__BIG_ENDIAN)
  414. fb_writel((*chardata) << 24, mmio.vaddr);
  415. #else
  416. fb_writel(*chardata, mmio.vaddr);
  417. #endif
  418. chardata++;
  419. }
  420. } else if (step == 2) {
  421. /* Special case for 9..15bit widths */
  422. while (height--) {
  423. #if defined(__BIG_ENDIAN)
  424. fb_writel((*(u_int16_t*)chardata) << 16, mmio.vaddr);
  425. #else
  426. fb_writel(*(u_int16_t*)chardata, mmio.vaddr);
  427. #endif
  428. chardata += 2;
  429. }
  430. } else {
  431. /* Tell... well, why bother... */
  432. while (height--) {
  433. size_t i;
  434. for (i = 0; i < step; i += 4) {
  435. /* Hope that there are at least three readable bytes beyond the end of bitmap */
  436. fb_writel(get_unaligned((u_int32_t*)(chardata + i)),mmio.vaddr);
  437. }
  438. chardata += step;
  439. }
  440. }
  441. }
  442. WaitTillIdle();
  443. CRITEND
  444. }
  445. static void matroxfb_imageblit(struct fb_info* info, const struct fb_image* image) {
  446. struct matrox_fb_info *minfo = info2minfo(info);
  447. DBG_HEAVY(__func__);
  448. if (image->depth == 1) {
  449. u_int32_t fgx, bgx;
  450. fgx = ((u_int32_t*)info->pseudo_palette)[image->fg_color];
  451. bgx = ((u_int32_t*)info->pseudo_palette)[image->bg_color];
  452. matroxfb_1bpp_imageblit(minfo, fgx, bgx, image->data, image->width, image->height, image->dy, image->dx);
  453. } else {
  454. /* Danger! image->depth is useless: logo painting code always
  455. passes framebuffer color depth here, although logo data are
  456. always 8bpp and info->pseudo_palette is changed to contain
  457. logo palette to be used (but only for true/direct-color... sic...).
  458. So do it completely in software... */
  459. cfb_imageblit(info, image);
  460. }
  461. }
  462. MODULE_LICENSE("GPL");