matroxfb_accel.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 writting 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) ACCESS_FBINFO2(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. pal[i] = 0xFFFFFFFF;
  90. }
  91. static inline void matrox_cfb8_pal(u_int32_t* pal) {
  92. unsigned int i;
  93. for (i = 0; i < 16; i++) {
  94. pal[i] = i * 0x01010101U;
  95. }
  96. pal[i] = 0x0F0F0F0F;
  97. }
  98. static void matroxfb_copyarea(struct fb_info* info, const struct fb_copyarea* area);
  99. static void matroxfb_fillrect(struct fb_info* info, const struct fb_fillrect* rect);
  100. static void matroxfb_imageblit(struct fb_info* info, const struct fb_image* image);
  101. static void matroxfb_cfb4_fillrect(struct fb_info* info, const struct fb_fillrect* rect);
  102. static void matroxfb_cfb4_copyarea(struct fb_info* info, const struct fb_copyarea* area);
  103. void matrox_cfbX_init(WPMINFO2) {
  104. u_int32_t maccess;
  105. u_int32_t mpitch;
  106. u_int32_t mopmode;
  107. int accel;
  108. DBG(__FUNCTION__)
  109. mpitch = ACCESS_FBINFO(fbcon).var.xres_virtual;
  110. ACCESS_FBINFO(fbops).fb_copyarea = cfb_copyarea;
  111. ACCESS_FBINFO(fbops).fb_fillrect = cfb_fillrect;
  112. ACCESS_FBINFO(fbops).fb_imageblit = cfb_imageblit;
  113. ACCESS_FBINFO(fbops).fb_cursor = NULL;
  114. accel = (ACCESS_FBINFO(fbcon).var.accel_flags & FB_ACCELF_TEXT) == FB_ACCELF_TEXT;
  115. switch (ACCESS_FBINFO(fbcon).var.bits_per_pixel) {
  116. case 4: maccess = 0x00000000; /* accelerate as 8bpp video */
  117. mpitch = (mpitch >> 1) | 0x8000; /* disable linearization */
  118. mopmode = M_OPMODE_4BPP;
  119. matrox_cfb4_pal(ACCESS_FBINFO(cmap));
  120. if (accel && !(mpitch & 1)) {
  121. ACCESS_FBINFO(fbops).fb_copyarea = matroxfb_cfb4_copyarea;
  122. ACCESS_FBINFO(fbops).fb_fillrect = matroxfb_cfb4_fillrect;
  123. }
  124. break;
  125. case 8: maccess = 0x00000000;
  126. mopmode = M_OPMODE_8BPP;
  127. matrox_cfb8_pal(ACCESS_FBINFO(cmap));
  128. if (accel) {
  129. ACCESS_FBINFO(fbops).fb_copyarea = matroxfb_copyarea;
  130. ACCESS_FBINFO(fbops).fb_fillrect = matroxfb_fillrect;
  131. ACCESS_FBINFO(fbops).fb_imageblit = matroxfb_imageblit;
  132. }
  133. break;
  134. case 16: if (ACCESS_FBINFO(fbcon).var.green.length == 5) {
  135. maccess = 0xC0000001;
  136. ACCESS_FBINFO(cmap[16]) = 0x7FFF7FFF;
  137. } else {
  138. maccess = 0x40000001;
  139. ACCESS_FBINFO(cmap[16]) = 0xFFFFFFFF;
  140. }
  141. mopmode = M_OPMODE_16BPP;
  142. if (accel) {
  143. ACCESS_FBINFO(fbops).fb_copyarea = matroxfb_copyarea;
  144. ACCESS_FBINFO(fbops).fb_fillrect = matroxfb_fillrect;
  145. ACCESS_FBINFO(fbops).fb_imageblit = matroxfb_imageblit;
  146. }
  147. break;
  148. case 24: maccess = 0x00000003;
  149. mopmode = M_OPMODE_24BPP;
  150. ACCESS_FBINFO(cmap[16]) = 0xFFFFFFFF;
  151. if (accel) {
  152. ACCESS_FBINFO(fbops).fb_copyarea = matroxfb_copyarea;
  153. ACCESS_FBINFO(fbops).fb_fillrect = matroxfb_fillrect;
  154. ACCESS_FBINFO(fbops).fb_imageblit = matroxfb_imageblit;
  155. }
  156. break;
  157. case 32: maccess = 0x00000002;
  158. mopmode = M_OPMODE_32BPP;
  159. ACCESS_FBINFO(cmap[16]) = 0xFFFFFFFF;
  160. if (accel) {
  161. ACCESS_FBINFO(fbops).fb_copyarea = matroxfb_copyarea;
  162. ACCESS_FBINFO(fbops).fb_fillrect = matroxfb_fillrect;
  163. ACCESS_FBINFO(fbops).fb_imageblit = matroxfb_imageblit;
  164. }
  165. break;
  166. default: maccess = 0x00000000;
  167. mopmode = 0x00000000;
  168. break; /* turn off acceleration!!! */
  169. }
  170. mga_fifo(8);
  171. mga_outl(M_PITCH, mpitch);
  172. mga_outl(M_YDSTORG, curr_ydstorg(MINFO));
  173. if (ACCESS_FBINFO(capable.plnwt))
  174. mga_outl(M_PLNWT, -1);
  175. if (ACCESS_FBINFO(capable.srcorg)) {
  176. mga_outl(M_SRCORG, 0);
  177. mga_outl(M_DSTORG, 0);
  178. }
  179. mga_outl(M_OPMODE, mopmode);
  180. mga_outl(M_CXBNDRY, 0xFFFF0000);
  181. mga_outl(M_YTOP, 0);
  182. mga_outl(M_YBOT, 0x01FFFFFF);
  183. mga_outl(M_MACCESS, maccess);
  184. ACCESS_FBINFO(accel.m_dwg_rect) = M_DWG_TRAP | M_DWG_SOLID | M_DWG_ARZERO | M_DWG_SGNZERO | M_DWG_SHIFTZERO;
  185. if (isMilleniumII(MINFO)) ACCESS_FBINFO(accel.m_dwg_rect) |= M_DWG_TRANSC;
  186. ACCESS_FBINFO(accel.m_opmode) = mopmode;
  187. }
  188. EXPORT_SYMBOL(matrox_cfbX_init);
  189. static void matrox_accel_bmove(WPMINFO int vxres, int sy, int sx, int dy, int dx, int height, int width) {
  190. int start, end;
  191. CRITFLAGS
  192. DBG(__FUNCTION__)
  193. CRITBEGIN
  194. if ((dy < sy) || ((dy == sy) && (dx <= sx))) {
  195. mga_fifo(2);
  196. mga_outl(M_DWGCTL, M_DWG_BITBLT | M_DWG_SHIFTZERO | M_DWG_SGNZERO |
  197. M_DWG_BFCOL | M_DWG_REPLACE);
  198. mga_outl(M_AR5, vxres);
  199. width--;
  200. start = sy*vxres+sx+curr_ydstorg(MINFO);
  201. end = start+width;
  202. } else {
  203. mga_fifo(3);
  204. mga_outl(M_DWGCTL, M_DWG_BITBLT | M_DWG_SHIFTZERO | M_DWG_BFCOL | M_DWG_REPLACE);
  205. mga_outl(M_SGN, 5);
  206. mga_outl(M_AR5, -vxres);
  207. width--;
  208. end = (sy+height-1)*vxres+sx+curr_ydstorg(MINFO);
  209. start = end+width;
  210. dy += height-1;
  211. }
  212. mga_fifo(4);
  213. mga_outl(M_AR0, end);
  214. mga_outl(M_AR3, start);
  215. mga_outl(M_FXBNDRY, ((dx+width)<<16) | dx);
  216. mga_ydstlen(dy, height);
  217. WaitTillIdle();
  218. CRITEND
  219. }
  220. static void matrox_accel_bmove_lin(WPMINFO int vxres, int sy, int sx, int dy, int dx, int height, int width) {
  221. int start, end;
  222. CRITFLAGS
  223. DBG(__FUNCTION__)
  224. CRITBEGIN
  225. if ((dy < sy) || ((dy == sy) && (dx <= sx))) {
  226. mga_fifo(2);
  227. mga_outl(M_DWGCTL, M_DWG_BITBLT | M_DWG_SHIFTZERO | M_DWG_SGNZERO |
  228. M_DWG_BFCOL | M_DWG_REPLACE);
  229. mga_outl(M_AR5, vxres);
  230. width--;
  231. start = sy*vxres+sx+curr_ydstorg(MINFO);
  232. end = start+width;
  233. } else {
  234. mga_fifo(3);
  235. mga_outl(M_DWGCTL, M_DWG_BITBLT | M_DWG_SHIFTZERO | M_DWG_BFCOL | M_DWG_REPLACE);
  236. mga_outl(M_SGN, 5);
  237. mga_outl(M_AR5, -vxres);
  238. width--;
  239. end = (sy+height-1)*vxres+sx+curr_ydstorg(MINFO);
  240. start = end+width;
  241. dy += height-1;
  242. }
  243. mga_fifo(5);
  244. mga_outl(M_AR0, end);
  245. mga_outl(M_AR3, start);
  246. mga_outl(M_FXBNDRY, ((dx+width)<<16) | dx);
  247. mga_outl(M_YDST, dy*vxres >> 5);
  248. mga_outl(M_LEN | M_EXEC, height);
  249. WaitTillIdle();
  250. CRITEND
  251. }
  252. static void matroxfb_cfb4_copyarea(struct fb_info* info, const struct fb_copyarea* area) {
  253. MINFO_FROM_INFO(info);
  254. if ((area->sx | area->dx | area->width) & 1)
  255. cfb_copyarea(info, area);
  256. else
  257. matrox_accel_bmove_lin(PMINFO ACCESS_FBINFO(fbcon.var.xres_virtual) >> 1, area->sy, area->sx >> 1, area->dy, area->dx >> 1, area->height, area->width >> 1);
  258. }
  259. static void matroxfb_copyarea(struct fb_info* info, const struct fb_copyarea* area) {
  260. MINFO_FROM_INFO(info);
  261. matrox_accel_bmove(PMINFO ACCESS_FBINFO(fbcon.var.xres_virtual), area->sy, area->sx, area->dy, area->dx, area->height, area->width);
  262. }
  263. static void matroxfb_accel_clear(WPMINFO u_int32_t color, int sy, int sx, int height,
  264. int width) {
  265. CRITFLAGS
  266. DBG(__FUNCTION__)
  267. CRITBEGIN
  268. mga_fifo(5);
  269. mga_outl(M_DWGCTL, ACCESS_FBINFO(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. MINFO_FROM_INFO(info);
  278. switch (rect->rop) {
  279. case ROP_COPY:
  280. matroxfb_accel_clear(PMINFO ((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(WPMINFO u_int32_t bgx, int sy, int sx, int height, int width) {
  285. int whattodo;
  286. CRITFLAGS
  287. DBG(__FUNCTION__)
  288. CRITBEGIN
  289. whattodo = 0;
  290. if (sx & 1) {
  291. sx ++;
  292. if (!width) return;
  293. width --;
  294. whattodo = 1;
  295. }
  296. if (width & 1) {
  297. whattodo |= 2;
  298. }
  299. width >>= 1;
  300. sx >>= 1;
  301. if (width) {
  302. mga_fifo(5);
  303. mga_outl(M_DWGCTL, ACCESS_FBINFO(accel.m_dwg_rect) | M_DWG_REPLACE2);
  304. mga_outl(M_FCOL, bgx);
  305. mga_outl(M_FXBNDRY, ((sx + width) << 16) | sx);
  306. mga_outl(M_YDST, sy * ACCESS_FBINFO(fbcon).var.xres_virtual >> 6);
  307. mga_outl(M_LEN | M_EXEC, height);
  308. WaitTillIdle();
  309. }
  310. if (whattodo) {
  311. u_int32_t step = ACCESS_FBINFO(fbcon).var.xres_virtual >> 1;
  312. vaddr_t vbase = ACCESS_FBINFO(video.vbase);
  313. if (whattodo & 1) {
  314. unsigned int uaddr = sy * step + sx - 1;
  315. u_int32_t loop;
  316. u_int8_t bgx2 = bgx & 0xF0;
  317. for (loop = height; loop > 0; loop --) {
  318. mga_writeb(vbase, uaddr, (mga_readb(vbase, uaddr) & 0x0F) | bgx2);
  319. uaddr += step;
  320. }
  321. }
  322. if (whattodo & 2) {
  323. unsigned int uaddr = sy * step + sx + width;
  324. u_int32_t loop;
  325. u_int8_t bgx2 = bgx & 0x0F;
  326. for (loop = height; loop > 0; loop --) {
  327. mga_writeb(vbase, uaddr, (mga_readb(vbase, uaddr) & 0xF0) | bgx2);
  328. uaddr += step;
  329. }
  330. }
  331. }
  332. CRITEND
  333. }
  334. static void matroxfb_cfb4_fillrect(struct fb_info* info, const struct fb_fillrect* rect) {
  335. MINFO_FROM_INFO(info);
  336. switch (rect->rop) {
  337. case ROP_COPY:
  338. matroxfb_cfb4_clear(PMINFO ((u_int32_t*)info->pseudo_palette)[rect->color], rect->dy, rect->dx, rect->height, rect->width);
  339. break;
  340. }
  341. }
  342. static void matroxfb_1bpp_imageblit(WPMINFO u_int32_t fgx, u_int32_t bgx,
  343. const u_int8_t* chardata, int width, int height, int yy, int xx) {
  344. u_int32_t step;
  345. u_int32_t ydstlen;
  346. u_int32_t xlen;
  347. u_int32_t ar0;
  348. u_int32_t charcell;
  349. u_int32_t fxbndry;
  350. vaddr_t mmio;
  351. int easy;
  352. CRITFLAGS
  353. DBG_HEAVY(__FUNCTION__);
  354. step = (width + 7) >> 3;
  355. charcell = height * step;
  356. xlen = (charcell + 3) & ~3;
  357. ydstlen = (yy << 16) | height;
  358. if (width == step << 3) {
  359. ar0 = height * width - 1;
  360. easy = 1;
  361. } else {
  362. ar0 = width - 1;
  363. easy = 0;
  364. }
  365. CRITBEGIN
  366. mga_fifo(3);
  367. if (easy)
  368. mga_outl(M_DWGCTL, M_DWG_ILOAD | M_DWG_SGNZERO | M_DWG_SHIFTZERO | M_DWG_BMONOWF | M_DWG_LINEAR | M_DWG_REPLACE);
  369. else
  370. mga_outl(M_DWGCTL, M_DWG_ILOAD | M_DWG_SGNZERO | M_DWG_SHIFTZERO | M_DWG_BMONOWF | M_DWG_REPLACE);
  371. mga_outl(M_FCOL, fgx);
  372. mga_outl(M_BCOL, bgx);
  373. fxbndry = ((xx + width - 1) << 16) | xx;
  374. mmio = ACCESS_FBINFO(mmio.vbase);
  375. mga_fifo(6);
  376. mga_writel(mmio, M_FXBNDRY, fxbndry);
  377. mga_writel(mmio, M_AR0, ar0);
  378. mga_writel(mmio, M_AR3, 0);
  379. if (easy) {
  380. mga_writel(mmio, M_YDSTLEN | M_EXEC, ydstlen);
  381. mga_memcpy_toio(mmio, chardata, xlen);
  382. } else {
  383. mga_writel(mmio, M_AR5, 0);
  384. mga_writel(mmio, M_YDSTLEN | M_EXEC, ydstlen);
  385. if ((step & 3) == 0) {
  386. /* Great. Source has 32bit aligned lines, so we can feed them
  387. directly to the accelerator. */
  388. mga_memcpy_toio(mmio, chardata, charcell);
  389. } else if (step == 1) {
  390. /* Special case for 1..8bit widths */
  391. while (height--) {
  392. #if defined(__BIG_ENDIAN)
  393. fb_writel((*chardata) << 24, mmio.vaddr);
  394. #else
  395. fb_writel(*chardata, mmio.vaddr);
  396. #endif
  397. chardata++;
  398. }
  399. } else if (step == 2) {
  400. /* Special case for 9..15bit widths */
  401. while (height--) {
  402. #if defined(__BIG_ENDIAN)
  403. fb_writel((*(u_int16_t*)chardata) << 16, mmio.vaddr);
  404. #else
  405. fb_writel(*(u_int16_t*)chardata, mmio.vaddr);
  406. #endif
  407. chardata += 2;
  408. }
  409. } else {
  410. /* Tell... well, why bother... */
  411. while (height--) {
  412. size_t i;
  413. for (i = 0; i < step; i += 4) {
  414. /* Hope that there are at least three readable bytes beyond the end of bitmap */
  415. fb_writel(get_unaligned((u_int32_t*)(chardata + i)),mmio.vaddr);
  416. }
  417. chardata += step;
  418. }
  419. }
  420. }
  421. WaitTillIdle();
  422. CRITEND
  423. }
  424. static void matroxfb_imageblit(struct fb_info* info, const struct fb_image* image) {
  425. MINFO_FROM_INFO(info);
  426. DBG_HEAVY(__FUNCTION__);
  427. if (image->depth == 1) {
  428. u_int32_t fgx, bgx;
  429. fgx = ((u_int32_t*)info->pseudo_palette)[image->fg_color];
  430. bgx = ((u_int32_t*)info->pseudo_palette)[image->bg_color];
  431. matroxfb_1bpp_imageblit(PMINFO fgx, bgx, image->data, image->width, image->height, image->dy, image->dx);
  432. } else {
  433. /* Danger! image->depth is useless: logo painting code always
  434. passes framebuffer color depth here, although logo data are
  435. always 8bpp and info->pseudo_palette is changed to contain
  436. logo palette to be used (but only for true/direct-color... sic...).
  437. So do it completely in software... */
  438. cfb_imageblit(info, image);
  439. }
  440. }
  441. MODULE_LICENSE("GPL");