matroxfb_accel.c 15 KB

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