radeon_accel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. #include "radeonfb.h"
  2. /* the accelerated functions here are patterned after the
  3. * "ACCEL_MMIO" ifdef branches in XFree86
  4. * --dte
  5. */
  6. #define FLUSH_CACHE_WORKAROUND 1
  7. void radeon_fifo_update_and_wait(struct radeonfb_info *rinfo, int entries)
  8. {
  9. int i;
  10. for (i=0; i<2000000; i++) {
  11. rinfo->fifo_free = INREG(RBBM_STATUS) & 0x7f;
  12. if (rinfo->fifo_free >= entries)
  13. return;
  14. udelay(10);
  15. }
  16. printk(KERN_ERR "radeonfb: FIFO Timeout !\n");
  17. /* XXX Todo: attempt to reset the engine */
  18. }
  19. static inline void radeon_fifo_wait(struct radeonfb_info *rinfo, int entries)
  20. {
  21. if (entries <= rinfo->fifo_free)
  22. rinfo->fifo_free -= entries;
  23. else
  24. radeon_fifo_update_and_wait(rinfo, entries);
  25. }
  26. static inline void radeonfb_set_creg(struct radeonfb_info *rinfo, u32 reg,
  27. u32 *cache, u32 new_val)
  28. {
  29. if (new_val == *cache)
  30. return;
  31. *cache = new_val;
  32. radeon_fifo_wait(rinfo, 1);
  33. OUTREG(reg, new_val);
  34. }
  35. static void radeonfb_prim_fillrect(struct radeonfb_info *rinfo,
  36. const struct fb_fillrect *region)
  37. {
  38. radeonfb_set_creg(rinfo, DP_GUI_MASTER_CNTL, &rinfo->dp_gui_mc_cache,
  39. rinfo->dp_gui_mc_base | GMC_BRUSH_SOLID_COLOR | ROP3_P);
  40. radeonfb_set_creg(rinfo, DP_CNTL, &rinfo->dp_cntl_cache,
  41. DST_X_LEFT_TO_RIGHT | DST_Y_TOP_TO_BOTTOM);
  42. radeonfb_set_creg(rinfo, DP_BRUSH_FRGD_CLR, &rinfo->dp_brush_fg_cache,
  43. region->color);
  44. /* Ensure the dst cache is flushed and the engine idle before
  45. * issuing the operation.
  46. *
  47. * This works around engine lockups on some cards
  48. */
  49. #if FLUSH_CACHE_WORKAROUND
  50. radeon_fifo_wait(rinfo, 2);
  51. OUTREG(DSTCACHE_CTLSTAT, RB2D_DC_FLUSH_ALL);
  52. OUTREG(WAIT_UNTIL, (WAIT_2D_IDLECLEAN | WAIT_DMA_GUI_IDLE));
  53. #endif
  54. radeon_fifo_wait(rinfo, 2);
  55. OUTREG(DST_Y_X, (region->dy << 16) | region->dx);
  56. OUTREG(DST_WIDTH_HEIGHT, (region->width << 16) | region->height);
  57. }
  58. void radeonfb_fillrect(struct fb_info *info, const struct fb_fillrect *region)
  59. {
  60. struct radeonfb_info *rinfo = info->par;
  61. struct fb_fillrect modded;
  62. int vxres, vyres;
  63. WARN_ON(rinfo->gfx_mode);
  64. if (info->state != FBINFO_STATE_RUNNING || rinfo->gfx_mode)
  65. return;
  66. if (info->flags & FBINFO_HWACCEL_DISABLED) {
  67. cfb_fillrect(info, region);
  68. return;
  69. }
  70. vxres = info->var.xres_virtual;
  71. vyres = info->var.yres_virtual;
  72. memcpy(&modded, region, sizeof(struct fb_fillrect));
  73. if(!modded.width || !modded.height ||
  74. modded.dx >= vxres || modded.dy >= vyres)
  75. return;
  76. if(modded.dx + modded.width > vxres) modded.width = vxres - modded.dx;
  77. if(modded.dy + modded.height > vyres) modded.height = vyres - modded.dy;
  78. if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
  79. info->fix.visual == FB_VISUAL_DIRECTCOLOR )
  80. modded.color = ((u32 *) (info->pseudo_palette))[region->color];
  81. radeonfb_prim_fillrect(rinfo, &modded);
  82. }
  83. static void radeonfb_prim_copyarea(struct radeonfb_info *rinfo,
  84. const struct fb_copyarea *area)
  85. {
  86. int xdir, ydir;
  87. u32 sx, sy, dx, dy, w, h;
  88. w = area->width; h = area->height;
  89. dx = area->dx; dy = area->dy;
  90. sx = area->sx; sy = area->sy;
  91. xdir = sx - dx;
  92. ydir = sy - dy;
  93. if ( xdir < 0 ) { sx += w-1; dx += w-1; }
  94. if ( ydir < 0 ) { sy += h-1; dy += h-1; }
  95. radeonfb_set_creg(rinfo, DP_GUI_MASTER_CNTL, &rinfo->dp_gui_mc_cache,
  96. rinfo->dp_gui_mc_base |
  97. GMC_BRUSH_NONE |
  98. GMC_SRC_DATATYPE_COLOR |
  99. ROP3_S |
  100. DP_SRC_SOURCE_MEMORY);
  101. radeonfb_set_creg(rinfo, DP_CNTL, &rinfo->dp_cntl_cache,
  102. (xdir>=0 ? DST_X_LEFT_TO_RIGHT : 0) |
  103. (ydir>=0 ? DST_Y_TOP_TO_BOTTOM : 0));
  104. #if FLUSH_CACHE_WORKAROUND
  105. radeon_fifo_wait(rinfo, 2);
  106. OUTREG(DSTCACHE_CTLSTAT, RB2D_DC_FLUSH_ALL);
  107. OUTREG(WAIT_UNTIL, (WAIT_2D_IDLECLEAN | WAIT_DMA_GUI_IDLE));
  108. #endif
  109. radeon_fifo_wait(rinfo, 3);
  110. OUTREG(SRC_Y_X, (sy << 16) | sx);
  111. OUTREG(DST_Y_X, (dy << 16) | dx);
  112. OUTREG(DST_HEIGHT_WIDTH, (h << 16) | w);
  113. }
  114. void radeonfb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
  115. {
  116. struct radeonfb_info *rinfo = info->par;
  117. struct fb_copyarea modded;
  118. u32 vxres, vyres;
  119. modded.sx = area->sx;
  120. modded.sy = area->sy;
  121. modded.dx = area->dx;
  122. modded.dy = area->dy;
  123. modded.width = area->width;
  124. modded.height = area->height;
  125. WARN_ON(rinfo->gfx_mode);
  126. if (info->state != FBINFO_STATE_RUNNING || rinfo->gfx_mode)
  127. return;
  128. if (info->flags & FBINFO_HWACCEL_DISABLED) {
  129. cfb_copyarea(info, area);
  130. return;
  131. }
  132. vxres = info->var.xres_virtual;
  133. vyres = info->var.yres_virtual;
  134. if(!modded.width || !modded.height ||
  135. modded.sx >= vxres || modded.sy >= vyres ||
  136. modded.dx >= vxres || modded.dy >= vyres)
  137. return;
  138. if(modded.sx + modded.width > vxres) modded.width = vxres - modded.sx;
  139. if(modded.dx + modded.width > vxres) modded.width = vxres - modded.dx;
  140. if(modded.sy + modded.height > vyres) modded.height = vyres - modded.sy;
  141. if(modded.dy + modded.height > vyres) modded.height = vyres - modded.dy;
  142. radeonfb_prim_copyarea(rinfo, &modded);
  143. }
  144. static void radeonfb_prim_imageblit(struct radeonfb_info *rinfo,
  145. const struct fb_image *image,
  146. u32 fg, u32 bg)
  147. {
  148. unsigned int dwords;
  149. u32 *bits;
  150. radeonfb_set_creg(rinfo, DP_GUI_MASTER_CNTL, &rinfo->dp_gui_mc_cache,
  151. rinfo->dp_gui_mc_base |
  152. GMC_BRUSH_NONE | GMC_DST_CLIP_LEAVE |
  153. GMC_SRC_DATATYPE_MONO_FG_BG |
  154. ROP3_S |
  155. GMC_BYTE_ORDER_MSB_TO_LSB |
  156. DP_SRC_SOURCE_HOST_DATA);
  157. radeonfb_set_creg(rinfo, DP_CNTL, &rinfo->dp_cntl_cache,
  158. DST_X_LEFT_TO_RIGHT | DST_Y_TOP_TO_BOTTOM);
  159. radeonfb_set_creg(rinfo, DP_SRC_FRGD_CLR, &rinfo->dp_src_fg_cache, fg);
  160. radeonfb_set_creg(rinfo, DP_SRC_BKGD_CLR, &rinfo->dp_src_bg_cache, bg);
  161. /* Ensure the dst cache is flushed and the engine idle before
  162. * issuing the operation.
  163. *
  164. * This works around engine lockups on some cards
  165. */
  166. #if FLUSH_CACHE_WORKAROUND
  167. radeon_fifo_wait(rinfo, 2);
  168. OUTREG(DSTCACHE_CTLSTAT, RB2D_DC_FLUSH_ALL);
  169. OUTREG(WAIT_UNTIL, (WAIT_2D_IDLECLEAN | WAIT_DMA_GUI_IDLE));
  170. #endif
  171. /* X here pads width to a multiple of 32 and uses the clipper to
  172. * adjust the result. Is that really necessary ? Things seem to
  173. * work ok for me without that and the doco doesn't seem to imply]
  174. * there is such a restriction.
  175. */
  176. radeon_fifo_wait(rinfo, 4);
  177. OUTREG(SC_TOP_LEFT, (image->dy << 16) | image->dx);
  178. OUTREG(SC_BOTTOM_RIGHT, ((image->dy + image->height) << 16) |
  179. (image->dx + image->width));
  180. OUTREG(DST_Y_X, (image->dy << 16) | image->dx);
  181. OUTREG(DST_HEIGHT_WIDTH, (image->height << 16) | ((image->width + 31) & ~31));
  182. dwords = (image->width + 31) >> 5;
  183. dwords *= image->height;
  184. bits = (u32*)(image->data);
  185. while(dwords >= 8) {
  186. radeon_fifo_wait(rinfo, 8);
  187. #if BITS_PER_LONG == 64
  188. __raw_writeq(*((u64 *)(bits)), rinfo->mmio_base + HOST_DATA0);
  189. __raw_writeq(*((u64 *)(bits+2)), rinfo->mmio_base + HOST_DATA2);
  190. __raw_writeq(*((u64 *)(bits+4)), rinfo->mmio_base + HOST_DATA4);
  191. __raw_writeq(*((u64 *)(bits+6)), rinfo->mmio_base + HOST_DATA6);
  192. bits += 8;
  193. #else
  194. __raw_writel(*(bits++), rinfo->mmio_base + HOST_DATA0);
  195. __raw_writel(*(bits++), rinfo->mmio_base + HOST_DATA1);
  196. __raw_writel(*(bits++), rinfo->mmio_base + HOST_DATA2);
  197. __raw_writel(*(bits++), rinfo->mmio_base + HOST_DATA3);
  198. __raw_writel(*(bits++), rinfo->mmio_base + HOST_DATA4);
  199. __raw_writel(*(bits++), rinfo->mmio_base + HOST_DATA5);
  200. __raw_writel(*(bits++), rinfo->mmio_base + HOST_DATA6);
  201. __raw_writel(*(bits++), rinfo->mmio_base + HOST_DATA7);
  202. #endif
  203. dwords -= 8;
  204. }
  205. while(dwords--) {
  206. radeon_fifo_wait(rinfo, 1);
  207. __raw_writel(*(bits++), rinfo->mmio_base + HOST_DATA0);
  208. }
  209. }
  210. void radeonfb_imageblit(struct fb_info *info, const struct fb_image *image)
  211. {
  212. struct radeonfb_info *rinfo = info->par;
  213. u32 fg, bg;
  214. WARN_ON(rinfo->gfx_mode);
  215. if (info->state != FBINFO_STATE_RUNNING || rinfo->gfx_mode)
  216. return;
  217. if (!image->width || !image->height)
  218. return;
  219. /* We only do 1 bpp color expansion for now */
  220. if (info->flags & FBINFO_HWACCEL_DISABLED || image->depth != 1)
  221. goto fallback;
  222. /* Fallback if running out of the screen. We may do clipping
  223. * in the future */
  224. if ((image->dx + image->width) > info->var.xres_virtual ||
  225. (image->dy + image->height) > info->var.yres_virtual)
  226. goto fallback;
  227. if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
  228. info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
  229. fg = ((u32*)(info->pseudo_palette))[image->fg_color];
  230. bg = ((u32*)(info->pseudo_palette))[image->bg_color];
  231. } else {
  232. fg = image->fg_color;
  233. bg = image->bg_color;
  234. }
  235. radeonfb_prim_imageblit(rinfo, image, fg, bg);
  236. return;
  237. fallback:
  238. radeon_engine_idle(rinfo);
  239. cfb_imageblit(info, image);
  240. }
  241. int radeonfb_sync(struct fb_info *info)
  242. {
  243. struct radeonfb_info *rinfo = info->par;
  244. if (info->state != FBINFO_STATE_RUNNING)
  245. return 0;
  246. radeon_engine_idle(rinfo);
  247. return 0;
  248. }
  249. void radeonfb_engine_reset(struct radeonfb_info *rinfo)
  250. {
  251. u32 clock_cntl_index, mclk_cntl, rbbm_soft_reset;
  252. u32 host_path_cntl;
  253. radeon_engine_flush (rinfo);
  254. clock_cntl_index = INREG(CLOCK_CNTL_INDEX);
  255. mclk_cntl = INPLL(MCLK_CNTL);
  256. OUTPLL(MCLK_CNTL, (mclk_cntl |
  257. FORCEON_MCLKA |
  258. FORCEON_MCLKB |
  259. FORCEON_YCLKA |
  260. FORCEON_YCLKB |
  261. FORCEON_MC |
  262. FORCEON_AIC));
  263. host_path_cntl = INREG(HOST_PATH_CNTL);
  264. rbbm_soft_reset = INREG(RBBM_SOFT_RESET);
  265. if (IS_R300_VARIANT(rinfo)) {
  266. u32 tmp;
  267. OUTREG(RBBM_SOFT_RESET, (rbbm_soft_reset |
  268. SOFT_RESET_CP |
  269. SOFT_RESET_HI |
  270. SOFT_RESET_E2));
  271. INREG(RBBM_SOFT_RESET);
  272. OUTREG(RBBM_SOFT_RESET, 0);
  273. tmp = INREG(RB2D_DSTCACHE_MODE);
  274. OUTREG(RB2D_DSTCACHE_MODE, tmp | (1 << 17)); /* FIXME */
  275. } else {
  276. OUTREG(RBBM_SOFT_RESET, rbbm_soft_reset |
  277. SOFT_RESET_CP |
  278. SOFT_RESET_HI |
  279. SOFT_RESET_SE |
  280. SOFT_RESET_RE |
  281. SOFT_RESET_PP |
  282. SOFT_RESET_E2 |
  283. SOFT_RESET_RB);
  284. INREG(RBBM_SOFT_RESET);
  285. OUTREG(RBBM_SOFT_RESET, rbbm_soft_reset & (u32)
  286. ~(SOFT_RESET_CP |
  287. SOFT_RESET_HI |
  288. SOFT_RESET_SE |
  289. SOFT_RESET_RE |
  290. SOFT_RESET_PP |
  291. SOFT_RESET_E2 |
  292. SOFT_RESET_RB));
  293. INREG(RBBM_SOFT_RESET);
  294. }
  295. OUTREG(HOST_PATH_CNTL, host_path_cntl | HDP_SOFT_RESET);
  296. INREG(HOST_PATH_CNTL);
  297. OUTREG(HOST_PATH_CNTL, host_path_cntl);
  298. if (!IS_R300_VARIANT(rinfo))
  299. OUTREG(RBBM_SOFT_RESET, rbbm_soft_reset);
  300. OUTREG(CLOCK_CNTL_INDEX, clock_cntl_index);
  301. OUTPLL(MCLK_CNTL, mclk_cntl);
  302. }
  303. void radeonfb_engine_init (struct radeonfb_info *rinfo)
  304. {
  305. unsigned long temp;
  306. /* disable 3D engine */
  307. OUTREG(RB3D_CNTL, 0);
  308. rinfo->fifo_free = 0;
  309. radeonfb_engine_reset(rinfo);
  310. radeon_fifo_wait(rinfo, 1);
  311. if (IS_R300_VARIANT(rinfo)) {
  312. OUTREG(RB2D_DSTCACHE_MODE, INREG(RB2D_DSTCACHE_MODE) |
  313. RB2D_DC_AUTOFLUSH_ENABLE |
  314. RB2D_DC_DC_DISABLE_IGNORE_PE);
  315. } else {
  316. /* This needs to be double checked with ATI. Latest X driver
  317. * completely "forgets" to set this register on < r3xx, and
  318. * we used to just write 0 there... I'll keep the 0 and update
  319. * that when we have sorted things out on X side.
  320. */
  321. OUTREG(RB2D_DSTCACHE_MODE, 0);
  322. }
  323. radeon_fifo_wait(rinfo, 3);
  324. /* We re-read MC_FB_LOCATION from card as it can have been
  325. * modified by XFree drivers (ouch !)
  326. */
  327. rinfo->fb_local_base = INREG(MC_FB_LOCATION) << 16;
  328. OUTREG(DEFAULT_PITCH_OFFSET, (rinfo->pitch << 0x16) |
  329. (rinfo->fb_local_base >> 10));
  330. OUTREG(DST_PITCH_OFFSET, (rinfo->pitch << 0x16) | (rinfo->fb_local_base >> 10));
  331. OUTREG(SRC_PITCH_OFFSET, (rinfo->pitch << 0x16) | (rinfo->fb_local_base >> 10));
  332. radeon_fifo_wait(rinfo, 1);
  333. #ifdef __BIG_ENDIAN
  334. OUTREGP(DP_DATATYPE, HOST_BIG_ENDIAN_EN, ~HOST_BIG_ENDIAN_EN);
  335. #else
  336. OUTREGP(DP_DATATYPE, 0, ~HOST_BIG_ENDIAN_EN);
  337. #endif
  338. radeon_fifo_wait(rinfo, 2);
  339. OUTREG(DEFAULT_SC_TOP_LEFT, 0);
  340. OUTREG(DEFAULT_SC_BOTTOM_RIGHT, (DEFAULT_SC_RIGHT_MAX |
  341. DEFAULT_SC_BOTTOM_MAX));
  342. /* set default DP_GUI_MASTER_CNTL */
  343. temp = radeon_get_dstbpp(rinfo->depth);
  344. rinfo->dp_gui_mc_base = ((temp << 8) | GMC_CLR_CMP_CNTL_DIS);
  345. rinfo->dp_gui_mc_cache = rinfo->dp_gui_mc_base |
  346. GMC_BRUSH_SOLID_COLOR |
  347. GMC_SRC_DATATYPE_COLOR;
  348. radeon_fifo_wait(rinfo, 1);
  349. OUTREG(DP_GUI_MASTER_CNTL, rinfo->dp_gui_mc_cache);
  350. /* clear line drawing regs */
  351. radeon_fifo_wait(rinfo, 2);
  352. OUTREG(DST_LINE_START, 0);
  353. OUTREG(DST_LINE_END, 0);
  354. /* set brush and source color regs */
  355. rinfo->dp_brush_fg_cache = 0xffffffff;
  356. rinfo->dp_brush_bg_cache = 0x00000000;
  357. rinfo->dp_src_fg_cache = 0xffffffff;
  358. rinfo->dp_src_bg_cache = 0x00000000;
  359. radeon_fifo_wait(rinfo, 4);
  360. OUTREG(DP_BRUSH_FRGD_CLR, rinfo->dp_brush_fg_cache);
  361. OUTREG(DP_BRUSH_BKGD_CLR, rinfo->dp_brush_bg_cache);
  362. OUTREG(DP_SRC_FRGD_CLR, rinfo->dp_src_fg_cache);
  363. OUTREG(DP_SRC_BKGD_CLR, rinfo->dp_src_bg_cache);
  364. /* Default direction */
  365. rinfo->dp_cntl_cache = DST_X_LEFT_TO_RIGHT | DST_Y_TOP_TO_BOTTOM;
  366. radeon_fifo_wait(rinfo, 1);
  367. OUTREG(DP_CNTL, rinfo->dp_cntl_cache);
  368. /* default write mask */
  369. radeon_fifo_wait(rinfo, 1);
  370. OUTREG(DP_WRITE_MSK, 0xffffffff);
  371. /* Default to no swapping of host data */
  372. radeon_fifo_wait(rinfo, 1);
  373. OUTREG(RBBM_GUICNTL, RBBM_GUICNTL_HOST_DATA_SWAP_NONE);
  374. /* Make sure it's settled */
  375. radeon_engine_idle(rinfo);
  376. }