radeon_accel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 src_bytes, 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 |
  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. radeon_fifo_wait(rinfo, 1);
  162. OUTREG(DST_Y_X, (image->dy << 16) | image->dx);
  163. /* Ensure the dst cache is flushed and the engine idle before
  164. * issuing the operation.
  165. *
  166. * This works around engine lockups on some cards
  167. */
  168. #if FLUSH_CACHE_WORKAROUND
  169. radeon_fifo_wait(rinfo, 2);
  170. OUTREG(DSTCACHE_CTLSTAT, RB2D_DC_FLUSH_ALL);
  171. OUTREG(WAIT_UNTIL, (WAIT_2D_IDLECLEAN | WAIT_DMA_GUI_IDLE));
  172. #endif
  173. /* X here pads width to a multiple of 32 and uses the clipper to
  174. * adjust the result. Is that really necessary ? Things seem to
  175. * work ok for me without that and the doco doesn't seem to imply
  176. * there is such a restriction.
  177. */
  178. OUTREG(DST_WIDTH_HEIGHT, (image->width << 16) | image->height);
  179. src_bytes = (((image->width * image->depth) + 7) / 8) * image->height;
  180. dwords = (src_bytes + 3) / 4;
  181. bits = (u32*)(image->data);
  182. while(dwords >= 8) {
  183. radeon_fifo_wait(rinfo, 8);
  184. #if BITS_PER_LONG == 64
  185. __raw_writeq(*((u64 *)(bits)), rinfo->mmio_base + HOST_DATA0);
  186. __raw_writeq(*((u64 *)(bits+2)), rinfo->mmio_base + HOST_DATA2);
  187. __raw_writeq(*((u64 *)(bits+4)), rinfo->mmio_base + HOST_DATA4);
  188. __raw_writeq(*((u64 *)(bits+6)), rinfo->mmio_base + HOST_DATA6);
  189. bits += 8;
  190. #else
  191. __raw_writel(*(bits++), rinfo->mmio_base + HOST_DATA0);
  192. __raw_writel(*(bits++), rinfo->mmio_base + HOST_DATA1);
  193. __raw_writel(*(bits++), rinfo->mmio_base + HOST_DATA2);
  194. __raw_writel(*(bits++), rinfo->mmio_base + HOST_DATA3);
  195. __raw_writel(*(bits++), rinfo->mmio_base + HOST_DATA4);
  196. __raw_writel(*(bits++), rinfo->mmio_base + HOST_DATA5);
  197. __raw_writel(*(bits++), rinfo->mmio_base + HOST_DATA6);
  198. __raw_writel(*(bits++), rinfo->mmio_base + HOST_DATA7);
  199. #endif
  200. dwords -= 8;
  201. }
  202. while(dwords--) {
  203. radeon_fifo_wait(rinfo, 1);
  204. __raw_writel(*(bits++), rinfo->mmio_base + HOST_DATA0);
  205. }
  206. }
  207. void radeonfb_imageblit(struct fb_info *info, const struct fb_image *image)
  208. {
  209. struct radeonfb_info *rinfo = info->par;
  210. u32 fg, bg;
  211. WARN_ON(rinfo->gfx_mode);
  212. if (info->state != FBINFO_STATE_RUNNING || rinfo->gfx_mode)
  213. return;
  214. if (!image->width || !image->height)
  215. return;
  216. /* We only do 1 bpp color expansion for now */
  217. if (info->flags & FBINFO_HWACCEL_DISABLED || image->depth != 1)
  218. goto fallback;
  219. /* Fallback if running out of the screen. We may do clipping
  220. * in the future */
  221. if ((image->dx + image->width) > info->var.xres_virtual ||
  222. (image->dy + image->height) > info->var.yres_virtual)
  223. goto fallback;
  224. if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
  225. info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
  226. fg = ((u32*)(info->pseudo_palette))[image->fg_color];
  227. bg = ((u32*)(info->pseudo_palette))[image->bg_color];
  228. } else {
  229. fg = image->fg_color;
  230. bg = image->bg_color;
  231. }
  232. radeonfb_prim_imageblit(rinfo, image, fg, bg);
  233. return;
  234. fallback:
  235. radeon_engine_idle(rinfo);
  236. cfb_imageblit(info, image);
  237. }
  238. int radeonfb_sync(struct fb_info *info)
  239. {
  240. struct radeonfb_info *rinfo = info->par;
  241. if (info->state != FBINFO_STATE_RUNNING)
  242. return 0;
  243. radeon_engine_idle(rinfo);
  244. return 0;
  245. }
  246. void radeonfb_engine_reset(struct radeonfb_info *rinfo)
  247. {
  248. u32 clock_cntl_index, mclk_cntl, rbbm_soft_reset;
  249. u32 host_path_cntl;
  250. radeon_engine_flush (rinfo);
  251. clock_cntl_index = INREG(CLOCK_CNTL_INDEX);
  252. mclk_cntl = INPLL(MCLK_CNTL);
  253. OUTPLL(MCLK_CNTL, (mclk_cntl |
  254. FORCEON_MCLKA |
  255. FORCEON_MCLKB |
  256. FORCEON_YCLKA |
  257. FORCEON_YCLKB |
  258. FORCEON_MC |
  259. FORCEON_AIC));
  260. host_path_cntl = INREG(HOST_PATH_CNTL);
  261. rbbm_soft_reset = INREG(RBBM_SOFT_RESET);
  262. if (IS_R300_VARIANT(rinfo)) {
  263. u32 tmp;
  264. OUTREG(RBBM_SOFT_RESET, (rbbm_soft_reset |
  265. SOFT_RESET_CP |
  266. SOFT_RESET_HI |
  267. SOFT_RESET_E2));
  268. INREG(RBBM_SOFT_RESET);
  269. OUTREG(RBBM_SOFT_RESET, 0);
  270. tmp = INREG(RB2D_DSTCACHE_MODE);
  271. OUTREG(RB2D_DSTCACHE_MODE, tmp | (1 << 17)); /* FIXME */
  272. } else {
  273. OUTREG(RBBM_SOFT_RESET, rbbm_soft_reset |
  274. SOFT_RESET_CP |
  275. SOFT_RESET_HI |
  276. SOFT_RESET_SE |
  277. SOFT_RESET_RE |
  278. SOFT_RESET_PP |
  279. SOFT_RESET_E2 |
  280. SOFT_RESET_RB);
  281. INREG(RBBM_SOFT_RESET);
  282. OUTREG(RBBM_SOFT_RESET, rbbm_soft_reset & (u32)
  283. ~(SOFT_RESET_CP |
  284. SOFT_RESET_HI |
  285. SOFT_RESET_SE |
  286. SOFT_RESET_RE |
  287. SOFT_RESET_PP |
  288. SOFT_RESET_E2 |
  289. SOFT_RESET_RB));
  290. INREG(RBBM_SOFT_RESET);
  291. }
  292. OUTREG(HOST_PATH_CNTL, host_path_cntl | HDP_SOFT_RESET);
  293. INREG(HOST_PATH_CNTL);
  294. OUTREG(HOST_PATH_CNTL, host_path_cntl);
  295. if (!IS_R300_VARIANT(rinfo))
  296. OUTREG(RBBM_SOFT_RESET, rbbm_soft_reset);
  297. OUTREG(CLOCK_CNTL_INDEX, clock_cntl_index);
  298. OUTPLL(MCLK_CNTL, mclk_cntl);
  299. }
  300. void radeonfb_engine_init (struct radeonfb_info *rinfo)
  301. {
  302. unsigned long temp;
  303. /* disable 3D engine */
  304. OUTREG(RB3D_CNTL, 0);
  305. rinfo->fifo_free = 0;
  306. radeonfb_engine_reset(rinfo);
  307. radeon_fifo_wait(rinfo, 1);
  308. if (IS_R300_VARIANT(rinfo)) {
  309. OUTREG(RB2D_DSTCACHE_MODE, INREG(RB2D_DSTCACHE_MODE) |
  310. RB2D_DC_AUTOFLUSH_ENABLE |
  311. RB2D_DC_DC_DISABLE_IGNORE_PE);
  312. } else {
  313. /* This needs to be double checked with ATI. Latest X driver
  314. * completely "forgets" to set this register on < r3xx, and
  315. * we used to just write 0 there... I'll keep the 0 and update
  316. * that when we have sorted things out on X side.
  317. */
  318. OUTREG(RB2D_DSTCACHE_MODE, 0);
  319. }
  320. radeon_fifo_wait(rinfo, 3);
  321. /* We re-read MC_FB_LOCATION from card as it can have been
  322. * modified by XFree drivers (ouch !)
  323. */
  324. rinfo->fb_local_base = INREG(MC_FB_LOCATION) << 16;
  325. OUTREG(DEFAULT_PITCH_OFFSET, (rinfo->pitch << 0x16) |
  326. (rinfo->fb_local_base >> 10));
  327. OUTREG(DST_PITCH_OFFSET, (rinfo->pitch << 0x16) | (rinfo->fb_local_base >> 10));
  328. OUTREG(SRC_PITCH_OFFSET, (rinfo->pitch << 0x16) | (rinfo->fb_local_base >> 10));
  329. radeon_fifo_wait(rinfo, 1);
  330. #ifdef __BIG_ENDIAN
  331. OUTREGP(DP_DATATYPE, HOST_BIG_ENDIAN_EN, ~HOST_BIG_ENDIAN_EN);
  332. #else
  333. OUTREGP(DP_DATATYPE, 0, ~HOST_BIG_ENDIAN_EN);
  334. #endif
  335. radeon_fifo_wait(rinfo, 2);
  336. OUTREG(DEFAULT_SC_TOP_LEFT, 0);
  337. OUTREG(DEFAULT_SC_BOTTOM_RIGHT, (DEFAULT_SC_RIGHT_MAX |
  338. DEFAULT_SC_BOTTOM_MAX));
  339. /* set default DP_GUI_MASTER_CNTL */
  340. temp = radeon_get_dstbpp(rinfo->depth);
  341. rinfo->dp_gui_mc_base = ((temp << 8) | GMC_CLR_CMP_CNTL_DIS);
  342. rinfo->dp_gui_mc_cache = rinfo->dp_gui_mc_base |
  343. GMC_BRUSH_SOLID_COLOR |
  344. GMC_SRC_DATATYPE_COLOR;
  345. radeon_fifo_wait(rinfo, 1);
  346. OUTREG(DP_GUI_MASTER_CNTL, rinfo->dp_gui_mc_cache);
  347. /* clear line drawing regs */
  348. radeon_fifo_wait(rinfo, 2);
  349. OUTREG(DST_LINE_START, 0);
  350. OUTREG(DST_LINE_END, 0);
  351. /* set brush and source color regs */
  352. rinfo->dp_brush_fg_cache = 0xffffffff;
  353. rinfo->dp_brush_bg_cache = 0x00000000;
  354. rinfo->dp_src_fg_cache = 0xffffffff;
  355. rinfo->dp_src_bg_cache = 0x00000000;
  356. radeon_fifo_wait(rinfo, 4);
  357. OUTREG(DP_BRUSH_FRGD_CLR, rinfo->dp_brush_fg_cache);
  358. OUTREG(DP_BRUSH_BKGD_CLR, rinfo->dp_brush_bg_cache);
  359. OUTREG(DP_SRC_FRGD_CLR, rinfo->dp_src_fg_cache);
  360. OUTREG(DP_SRC_BKGD_CLR, rinfo->dp_src_bg_cache);
  361. /* Default direction */
  362. rinfo->dp_cntl_cache = DST_X_LEFT_TO_RIGHT | DST_Y_TOP_TO_BOTTOM;
  363. radeon_fifo_wait(rinfo, 1);
  364. OUTREG(DP_CNTL, rinfo->dp_cntl_cache);
  365. /* default write mask */
  366. radeon_fifo_wait(rinfo, 1);
  367. OUTREG(DP_WRITE_MSK, 0xffffffff);
  368. /* Default to no swapping of host data */
  369. radeon_fifo_wait(rinfo, 1);
  370. OUTREG(RBBM_GUICNTL, RBBM_GUICNTL_HOST_DATA_SWAP_NONE);
  371. /* Make sure it's settled */
  372. radeon_engine_idle(rinfo);
  373. }