radeon_accel.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "radeonfb.h"
  3. /* the accelerated functions here are patterned after the
  4. * "ACCEL_MMIO" ifdef branches in XFree86
  5. * --dte
  6. */
  7. static void radeon_fixup_offset(struct radeonfb_info *rinfo)
  8. {
  9. u32 local_base;
  10. /* *** Ugly workaround *** */
  11. /*
  12. * On some platforms, the video memory is mapped at 0 in radeon chip space
  13. * (like PPCs) by the firmware. X will always move it up so that it's seen
  14. * by the chip to be at the same address as the PCI BAR.
  15. * That means that when switching back from X, there is a mismatch between
  16. * the offsets programmed into the engine. This means that potentially,
  17. * accel operations done before radeonfb has a chance to re-init the engine
  18. * will have incorrect offsets, and potentially trash system memory !
  19. *
  20. * The correct fix is for fbcon to never call any accel op before the engine
  21. * has properly been re-initialized (by a call to set_var), but this is a
  22. * complex fix. This workaround in the meantime, called before every accel
  23. * operation, makes sure the offsets are in sync.
  24. */
  25. radeon_fifo_wait (1);
  26. local_base = INREG(MC_FB_LOCATION) << 16;
  27. if (local_base == rinfo->fb_local_base)
  28. return;
  29. rinfo->fb_local_base = local_base;
  30. radeon_fifo_wait (3);
  31. OUTREG(DEFAULT_PITCH_OFFSET, (rinfo->pitch << 0x16) |
  32. (rinfo->fb_local_base >> 10));
  33. OUTREG(DST_PITCH_OFFSET, (rinfo->pitch << 0x16) | (rinfo->fb_local_base >> 10));
  34. OUTREG(SRC_PITCH_OFFSET, (rinfo->pitch << 0x16) | (rinfo->fb_local_base >> 10));
  35. }
  36. static void radeonfb_prim_fillrect(struct radeonfb_info *rinfo,
  37. const struct fb_fillrect *region)
  38. {
  39. radeon_fifo_wait(4);
  40. OUTREG(DP_GUI_MASTER_CNTL,
  41. rinfo->dp_gui_master_cntl /* contains, like GMC_DST_32BPP */
  42. | GMC_BRUSH_SOLID_COLOR
  43. | ROP3_P);
  44. if (radeon_get_dstbpp(rinfo->depth) != DST_8BPP)
  45. OUTREG(DP_BRUSH_FRGD_CLR, rinfo->pseudo_palette[region->color]);
  46. else
  47. OUTREG(DP_BRUSH_FRGD_CLR, region->color);
  48. OUTREG(DP_WRITE_MSK, 0xffffffff);
  49. OUTREG(DP_CNTL, (DST_X_LEFT_TO_RIGHT | DST_Y_TOP_TO_BOTTOM));
  50. radeon_fifo_wait(2);
  51. OUTREG(DSTCACHE_CTLSTAT, RB2D_DC_FLUSH_ALL);
  52. OUTREG(WAIT_UNTIL, (WAIT_2D_IDLECLEAN | WAIT_DMA_GUI_IDLE));
  53. radeon_fifo_wait(2);
  54. OUTREG(DST_Y_X, (region->dy << 16) | region->dx);
  55. OUTREG(DST_WIDTH_HEIGHT, (region->width << 16) | region->height);
  56. }
  57. void radeonfb_fillrect(struct fb_info *info, const struct fb_fillrect *region)
  58. {
  59. struct radeonfb_info *rinfo = info->par;
  60. struct fb_fillrect modded;
  61. int vxres, vyres;
  62. if (info->state != FBINFO_STATE_RUNNING)
  63. return;
  64. if (info->flags & FBINFO_HWACCEL_DISABLED) {
  65. cfb_fillrect(info, region);
  66. return;
  67. }
  68. radeon_fixup_offset(rinfo);
  69. vxres = info->var.xres_virtual;
  70. vyres = info->var.yres_virtual;
  71. memcpy(&modded, region, sizeof(struct fb_fillrect));
  72. if(!modded.width || !modded.height ||
  73. modded.dx >= vxres || modded.dy >= vyres)
  74. return;
  75. if(modded.dx + modded.width > vxres) modded.width = vxres - modded.dx;
  76. if(modded.dy + modded.height > vyres) modded.height = vyres - modded.dy;
  77. radeonfb_prim_fillrect(rinfo, &modded);
  78. }
  79. static void radeonfb_prim_copyarea(struct radeonfb_info *rinfo,
  80. const struct fb_copyarea *area)
  81. {
  82. int xdir, ydir;
  83. u32 sx, sy, dx, dy, w, h;
  84. w = area->width; h = area->height;
  85. dx = area->dx; dy = area->dy;
  86. sx = area->sx; sy = area->sy;
  87. xdir = sx - dx;
  88. ydir = sy - dy;
  89. if ( xdir < 0 ) { sx += w-1; dx += w-1; }
  90. if ( ydir < 0 ) { sy += h-1; dy += h-1; }
  91. radeon_fifo_wait(3);
  92. OUTREG(DP_GUI_MASTER_CNTL,
  93. rinfo->dp_gui_master_cntl /* i.e. GMC_DST_32BPP */
  94. | GMC_BRUSH_NONE
  95. | GMC_SRC_DSTCOLOR
  96. | ROP3_S
  97. | DP_SRC_SOURCE_MEMORY );
  98. OUTREG(DP_WRITE_MSK, 0xffffffff);
  99. OUTREG(DP_CNTL, (xdir>=0 ? DST_X_LEFT_TO_RIGHT : 0)
  100. | (ydir>=0 ? DST_Y_TOP_TO_BOTTOM : 0));
  101. radeon_fifo_wait(2);
  102. OUTREG(DSTCACHE_CTLSTAT, RB2D_DC_FLUSH_ALL);
  103. OUTREG(WAIT_UNTIL, (WAIT_2D_IDLECLEAN | WAIT_DMA_GUI_IDLE));
  104. radeon_fifo_wait(3);
  105. OUTREG(SRC_Y_X, (sy << 16) | sx);
  106. OUTREG(DST_Y_X, (dy << 16) | dx);
  107. OUTREG(DST_HEIGHT_WIDTH, (h << 16) | w);
  108. }
  109. void radeonfb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
  110. {
  111. struct radeonfb_info *rinfo = info->par;
  112. struct fb_copyarea modded;
  113. u32 vxres, vyres;
  114. modded.sx = area->sx;
  115. modded.sy = area->sy;
  116. modded.dx = area->dx;
  117. modded.dy = area->dy;
  118. modded.width = area->width;
  119. modded.height = area->height;
  120. if (info->state != FBINFO_STATE_RUNNING)
  121. return;
  122. if (info->flags & FBINFO_HWACCEL_DISABLED) {
  123. cfb_copyarea(info, area);
  124. return;
  125. }
  126. radeon_fixup_offset(rinfo);
  127. vxres = info->var.xres_virtual;
  128. vyres = info->var.yres_virtual;
  129. if(!modded.width || !modded.height ||
  130. modded.sx >= vxres || modded.sy >= vyres ||
  131. modded.dx >= vxres || modded.dy >= vyres)
  132. return;
  133. if(modded.sx + modded.width > vxres) modded.width = vxres - modded.sx;
  134. if(modded.dx + modded.width > vxres) modded.width = vxres - modded.dx;
  135. if(modded.sy + modded.height > vyres) modded.height = vyres - modded.sy;
  136. if(modded.dy + modded.height > vyres) modded.height = vyres - modded.dy;
  137. radeonfb_prim_copyarea(rinfo, &modded);
  138. }
  139. void radeonfb_imageblit(struct fb_info *info, const struct fb_image *image)
  140. {
  141. struct radeonfb_info *rinfo = info->par;
  142. if (info->state != FBINFO_STATE_RUNNING)
  143. return;
  144. radeon_engine_idle();
  145. cfb_imageblit(info, image);
  146. }
  147. int radeonfb_sync(struct fb_info *info)
  148. {
  149. struct radeonfb_info *rinfo = info->par;
  150. if (info->state != FBINFO_STATE_RUNNING)
  151. return 0;
  152. radeon_engine_idle();
  153. return 0;
  154. }
  155. void radeonfb_engine_reset(struct radeonfb_info *rinfo)
  156. {
  157. u32 clock_cntl_index, mclk_cntl, rbbm_soft_reset;
  158. u32 host_path_cntl;
  159. radeon_engine_flush (rinfo);
  160. clock_cntl_index = INREG(CLOCK_CNTL_INDEX);
  161. mclk_cntl = INPLL(MCLK_CNTL);
  162. OUTPLL(MCLK_CNTL, (mclk_cntl |
  163. FORCEON_MCLKA |
  164. FORCEON_MCLKB |
  165. FORCEON_YCLKA |
  166. FORCEON_YCLKB |
  167. FORCEON_MC |
  168. FORCEON_AIC));
  169. host_path_cntl = INREG(HOST_PATH_CNTL);
  170. rbbm_soft_reset = INREG(RBBM_SOFT_RESET);
  171. if (IS_R300_VARIANT(rinfo)) {
  172. u32 tmp;
  173. OUTREG(RBBM_SOFT_RESET, (rbbm_soft_reset |
  174. SOFT_RESET_CP |
  175. SOFT_RESET_HI |
  176. SOFT_RESET_E2));
  177. INREG(RBBM_SOFT_RESET);
  178. OUTREG(RBBM_SOFT_RESET, 0);
  179. tmp = INREG(RB2D_DSTCACHE_MODE);
  180. OUTREG(RB2D_DSTCACHE_MODE, tmp | (1 << 17)); /* FIXME */
  181. } else {
  182. OUTREG(RBBM_SOFT_RESET, rbbm_soft_reset |
  183. SOFT_RESET_CP |
  184. SOFT_RESET_HI |
  185. SOFT_RESET_SE |
  186. SOFT_RESET_RE |
  187. SOFT_RESET_PP |
  188. SOFT_RESET_E2 |
  189. SOFT_RESET_RB);
  190. INREG(RBBM_SOFT_RESET);
  191. OUTREG(RBBM_SOFT_RESET, rbbm_soft_reset & (u32)
  192. ~(SOFT_RESET_CP |
  193. SOFT_RESET_HI |
  194. SOFT_RESET_SE |
  195. SOFT_RESET_RE |
  196. SOFT_RESET_PP |
  197. SOFT_RESET_E2 |
  198. SOFT_RESET_RB));
  199. INREG(RBBM_SOFT_RESET);
  200. }
  201. OUTREG(HOST_PATH_CNTL, host_path_cntl | HDP_SOFT_RESET);
  202. INREG(HOST_PATH_CNTL);
  203. OUTREG(HOST_PATH_CNTL, host_path_cntl);
  204. if (!IS_R300_VARIANT(rinfo))
  205. OUTREG(RBBM_SOFT_RESET, rbbm_soft_reset);
  206. OUTREG(CLOCK_CNTL_INDEX, clock_cntl_index);
  207. OUTPLL(MCLK_CNTL, mclk_cntl);
  208. }
  209. void radeonfb_engine_init (struct radeonfb_info *rinfo)
  210. {
  211. unsigned long temp;
  212. /* disable 3D engine */
  213. OUTREG(RB3D_CNTL, 0);
  214. radeonfb_engine_reset(rinfo);
  215. radeon_fifo_wait (1);
  216. if (IS_R300_VARIANT(rinfo)) {
  217. OUTREG(RB2D_DSTCACHE_MODE, INREG(RB2D_DSTCACHE_MODE) |
  218. RB2D_DC_AUTOFLUSH_ENABLE |
  219. RB2D_DC_DC_DISABLE_IGNORE_PE);
  220. } else {
  221. /* This needs to be double checked with ATI. Latest X driver
  222. * completely "forgets" to set this register on < r3xx, and
  223. * we used to just write 0 there... I'll keep the 0 and update
  224. * that when we have sorted things out on X side.
  225. */
  226. OUTREG(RB2D_DSTCACHE_MODE, 0);
  227. }
  228. radeon_fifo_wait (3);
  229. /* We re-read MC_FB_LOCATION from card as it can have been
  230. * modified by XFree drivers (ouch !)
  231. */
  232. rinfo->fb_local_base = INREG(MC_FB_LOCATION) << 16;
  233. OUTREG(DEFAULT_PITCH_OFFSET, (rinfo->pitch << 0x16) |
  234. (rinfo->fb_local_base >> 10));
  235. OUTREG(DST_PITCH_OFFSET, (rinfo->pitch << 0x16) | (rinfo->fb_local_base >> 10));
  236. OUTREG(SRC_PITCH_OFFSET, (rinfo->pitch << 0x16) | (rinfo->fb_local_base >> 10));
  237. radeon_fifo_wait (1);
  238. #if defined(__BIG_ENDIAN)
  239. OUTREGP(DP_DATATYPE, HOST_BIG_ENDIAN_EN, ~HOST_BIG_ENDIAN_EN);
  240. #else
  241. OUTREGP(DP_DATATYPE, 0, ~HOST_BIG_ENDIAN_EN);
  242. #endif
  243. radeon_fifo_wait (2);
  244. OUTREG(DEFAULT_SC_TOP_LEFT, 0);
  245. OUTREG(DEFAULT_SC_BOTTOM_RIGHT, (DEFAULT_SC_RIGHT_MAX |
  246. DEFAULT_SC_BOTTOM_MAX));
  247. temp = radeon_get_dstbpp(rinfo->depth);
  248. rinfo->dp_gui_master_cntl = ((temp << 8) | GMC_CLR_CMP_CNTL_DIS);
  249. radeon_fifo_wait (1);
  250. OUTREG(DP_GUI_MASTER_CNTL, (rinfo->dp_gui_master_cntl |
  251. GMC_BRUSH_SOLID_COLOR |
  252. GMC_SRC_DATATYPE_COLOR));
  253. radeon_fifo_wait (7);
  254. /* clear line drawing regs */
  255. OUTREG(DST_LINE_START, 0);
  256. OUTREG(DST_LINE_END, 0);
  257. /* set brush color regs */
  258. OUTREG(DP_BRUSH_FRGD_CLR, 0xffffffff);
  259. OUTREG(DP_BRUSH_BKGD_CLR, 0x00000000);
  260. /* set source color regs */
  261. OUTREG(DP_SRC_FRGD_CLR, 0xffffffff);
  262. OUTREG(DP_SRC_BKGD_CLR, 0x00000000);
  263. /* default write mask */
  264. OUTREG(DP_WRITE_MSK, 0xffffffff);
  265. radeon_engine_idle ();
  266. }