nv_accel.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /***************************************************************************\
  2. |* *|
  3. |* Copyright 1993-2003 NVIDIA, Corporation. All rights reserved. *|
  4. |* *|
  5. |* NOTICE TO USER: The source code is copyrighted under U.S. and *|
  6. |* international laws. Users and possessors of this source code are *|
  7. |* hereby granted a nonexclusive, royalty-free copyright license to *|
  8. |* use this code in individual and commercial software. *|
  9. |* *|
  10. |* Any use of this source code must include, in the user documenta- *|
  11. |* tion and internal comments to the code, notices to the end user *|
  12. |* as follows: *|
  13. |* *|
  14. |* Copyright 1993-2003 NVIDIA, Corporation. All rights reserved. *|
  15. |* *|
  16. |* NVIDIA, CORPORATION MAKES NO REPRESENTATION ABOUT THE SUITABILITY *|
  17. |* OF THIS SOURCE CODE FOR ANY PURPOSE. IT IS PROVIDED "AS IS" *|
  18. |* WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND. NVIDIA, CORPOR- *|
  19. |* ATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOURCE CODE, *|
  20. |* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGE- *|
  21. |* MENT, AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL *|
  22. |* NVIDIA, CORPORATION BE LIABLE FOR ANY SPECIAL, INDIRECT, INCI- *|
  23. |* DENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RE- *|
  24. |* SULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION *|
  25. |* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF *|
  26. |* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOURCE CODE. *|
  27. |* *|
  28. |* U.S. Government End Users. This source code is a "commercial *|
  29. |* item," as that term is defined at 48 C.F.R. 2.101 (OCT 1995), *|
  30. |* consisting of "commercial computer software" and "commercial *|
  31. |* computer software documentation," as such terms are used in *|
  32. |* 48 C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Govern- *|
  33. |* ment only as a commercial end item. Consistent with 48 C.F.R. *|
  34. |* 12.212 and 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), *|
  35. |* all U.S. Government End Users acquire the source code with only *|
  36. |* those rights set forth herein. *|
  37. |* *|
  38. \***************************************************************************/
  39. /*
  40. * GPL Licensing Note - According to Mark Vojkovich, author of the Xorg/
  41. * XFree86 'nv' driver, this source code is provided under MIT-style licensing
  42. * where the source code is provided "as is" without warranty of any kind.
  43. * The only usage restriction is for the copyright notices to be retained
  44. * whenever code is used.
  45. *
  46. * Antonino Daplas <adaplas@pol.net> 2005-03-11
  47. */
  48. #include <linux/fb.h>
  49. #include "nv_type.h"
  50. #include "nv_proto.h"
  51. #include "nv_dma.h"
  52. #include "nv_local.h"
  53. /* There is a HW race condition with videoram command buffers.
  54. You can't jump to the location of your put offset. We write put
  55. at the jump offset + SKIPS dwords with noop padding in between
  56. to solve this problem */
  57. #define SKIPS 8
  58. static const int NVCopyROP[16] = {
  59. 0xCC, /* copy */
  60. 0x55 /* invert */
  61. };
  62. static const int NVCopyROP_PM[16] = {
  63. 0xCA, /* copy */
  64. 0x5A, /* invert */
  65. };
  66. static inline void nvidiafb_safe_mode(struct fb_info *info)
  67. {
  68. struct nvidia_par *par = info->par;
  69. touch_softlockup_watchdog();
  70. info->pixmap.scan_align = 1;
  71. par->lockup = 1;
  72. }
  73. static inline void NVFlush(struct fb_info *info)
  74. {
  75. struct nvidia_par *par = info->par;
  76. int count = 1000000000;
  77. while (--count && READ_GET(par) != par->dmaPut) ;
  78. if (!count) {
  79. printk("nvidiafb: DMA Flush lockup\n");
  80. nvidiafb_safe_mode(info);
  81. }
  82. }
  83. static inline void NVSync(struct fb_info *info)
  84. {
  85. struct nvidia_par *par = info->par;
  86. int count = 1000000000;
  87. while (--count && NV_RD32(par->PGRAPH, 0x0700)) ;
  88. if (!count) {
  89. printk("nvidiafb: DMA Sync lockup\n");
  90. nvidiafb_safe_mode(info);
  91. }
  92. }
  93. static void NVDmaKickoff(struct nvidia_par *par)
  94. {
  95. if (par->dmaCurrent != par->dmaPut) {
  96. par->dmaPut = par->dmaCurrent;
  97. WRITE_PUT(par, par->dmaPut);
  98. }
  99. }
  100. static void NVDmaWait(struct fb_info *info, int size)
  101. {
  102. struct nvidia_par *par = info->par;
  103. int dmaGet;
  104. int count = 1000000000, cnt;
  105. size++;
  106. while (par->dmaFree < size && --count && !par->lockup) {
  107. dmaGet = READ_GET(par);
  108. if (par->dmaPut >= dmaGet) {
  109. par->dmaFree = par->dmaMax - par->dmaCurrent;
  110. if (par->dmaFree < size) {
  111. NVDmaNext(par, 0x20000000);
  112. if (dmaGet <= SKIPS) {
  113. if (par->dmaPut <= SKIPS)
  114. WRITE_PUT(par, SKIPS + 1);
  115. cnt = 1000000000;
  116. do {
  117. dmaGet = READ_GET(par);
  118. } while (--cnt && dmaGet <= SKIPS);
  119. if (!cnt) {
  120. printk("DMA Get lockup\n");
  121. par->lockup = 1;
  122. }
  123. }
  124. WRITE_PUT(par, SKIPS);
  125. par->dmaCurrent = par->dmaPut = SKIPS;
  126. par->dmaFree = dmaGet - (SKIPS + 1);
  127. }
  128. } else
  129. par->dmaFree = dmaGet - par->dmaCurrent - 1;
  130. }
  131. if (!count) {
  132. printk("nvidiafb: DMA Wait Lockup\n");
  133. nvidiafb_safe_mode(info);
  134. }
  135. }
  136. static void NVSetPattern(struct fb_info *info, u32 clr0, u32 clr1,
  137. u32 pat0, u32 pat1)
  138. {
  139. struct nvidia_par *par = info->par;
  140. NVDmaStart(info, par, PATTERN_COLOR_0, 4);
  141. NVDmaNext(par, clr0);
  142. NVDmaNext(par, clr1);
  143. NVDmaNext(par, pat0);
  144. NVDmaNext(par, pat1);
  145. }
  146. static void NVSetRopSolid(struct fb_info *info, u32 rop, u32 planemask)
  147. {
  148. struct nvidia_par *par = info->par;
  149. if (planemask != ~0) {
  150. NVSetPattern(info, 0, planemask, ~0, ~0);
  151. if (par->currentRop != (rop + 32)) {
  152. NVDmaStart(info, par, ROP_SET, 1);
  153. NVDmaNext(par, NVCopyROP_PM[rop]);
  154. par->currentRop = rop + 32;
  155. }
  156. } else if (par->currentRop != rop) {
  157. if (par->currentRop >= 16)
  158. NVSetPattern(info, ~0, ~0, ~0, ~0);
  159. NVDmaStart(info, par, ROP_SET, 1);
  160. NVDmaNext(par, NVCopyROP[rop]);
  161. par->currentRop = rop;
  162. }
  163. }
  164. static void NVSetClippingRectangle(struct fb_info *info, int x1, int y1,
  165. int x2, int y2)
  166. {
  167. struct nvidia_par *par = info->par;
  168. int h = y2 - y1 + 1;
  169. int w = x2 - x1 + 1;
  170. NVDmaStart(info, par, CLIP_POINT, 2);
  171. NVDmaNext(par, (y1 << 16) | x1);
  172. NVDmaNext(par, (h << 16) | w);
  173. }
  174. void NVResetGraphics(struct fb_info *info)
  175. {
  176. struct nvidia_par *par = info->par;
  177. u32 surfaceFormat, patternFormat, rectFormat, lineFormat;
  178. int pitch, i;
  179. pitch = info->fix.line_length;
  180. par->dmaBase = (u32 __iomem *) (&par->FbStart[par->FbUsableSize]);
  181. for (i = 0; i < SKIPS; i++)
  182. NV_WR32(&par->dmaBase[i], 0, 0x00000000);
  183. NV_WR32(&par->dmaBase[0x0 + SKIPS], 0, 0x00040000);
  184. NV_WR32(&par->dmaBase[0x1 + SKIPS], 0, 0x80000010);
  185. NV_WR32(&par->dmaBase[0x2 + SKIPS], 0, 0x00042000);
  186. NV_WR32(&par->dmaBase[0x3 + SKIPS], 0, 0x80000011);
  187. NV_WR32(&par->dmaBase[0x4 + SKIPS], 0, 0x00044000);
  188. NV_WR32(&par->dmaBase[0x5 + SKIPS], 0, 0x80000012);
  189. NV_WR32(&par->dmaBase[0x6 + SKIPS], 0, 0x00046000);
  190. NV_WR32(&par->dmaBase[0x7 + SKIPS], 0, 0x80000013);
  191. NV_WR32(&par->dmaBase[0x8 + SKIPS], 0, 0x00048000);
  192. NV_WR32(&par->dmaBase[0x9 + SKIPS], 0, 0x80000014);
  193. NV_WR32(&par->dmaBase[0xA + SKIPS], 0, 0x0004A000);
  194. NV_WR32(&par->dmaBase[0xB + SKIPS], 0, 0x80000015);
  195. NV_WR32(&par->dmaBase[0xC + SKIPS], 0, 0x0004C000);
  196. NV_WR32(&par->dmaBase[0xD + SKIPS], 0, 0x80000016);
  197. NV_WR32(&par->dmaBase[0xE + SKIPS], 0, 0x0004E000);
  198. NV_WR32(&par->dmaBase[0xF + SKIPS], 0, 0x80000017);
  199. par->dmaPut = 0;
  200. par->dmaCurrent = 16 + SKIPS;
  201. par->dmaMax = 8191;
  202. par->dmaFree = par->dmaMax - par->dmaCurrent;
  203. switch (info->var.bits_per_pixel) {
  204. case 32:
  205. case 24:
  206. surfaceFormat = SURFACE_FORMAT_DEPTH24;
  207. patternFormat = PATTERN_FORMAT_DEPTH24;
  208. rectFormat = RECT_FORMAT_DEPTH24;
  209. lineFormat = LINE_FORMAT_DEPTH24;
  210. break;
  211. case 16:
  212. surfaceFormat = SURFACE_FORMAT_DEPTH16;
  213. patternFormat = PATTERN_FORMAT_DEPTH16;
  214. rectFormat = RECT_FORMAT_DEPTH16;
  215. lineFormat = LINE_FORMAT_DEPTH16;
  216. break;
  217. default:
  218. surfaceFormat = SURFACE_FORMAT_DEPTH8;
  219. patternFormat = PATTERN_FORMAT_DEPTH8;
  220. rectFormat = RECT_FORMAT_DEPTH8;
  221. lineFormat = LINE_FORMAT_DEPTH8;
  222. break;
  223. }
  224. NVDmaStart(info, par, SURFACE_FORMAT, 4);
  225. NVDmaNext(par, surfaceFormat);
  226. NVDmaNext(par, pitch | (pitch << 16));
  227. NVDmaNext(par, 0);
  228. NVDmaNext(par, 0);
  229. NVDmaStart(info, par, PATTERN_FORMAT, 1);
  230. NVDmaNext(par, patternFormat);
  231. NVDmaStart(info, par, RECT_FORMAT, 1);
  232. NVDmaNext(par, rectFormat);
  233. NVDmaStart(info, par, LINE_FORMAT, 1);
  234. NVDmaNext(par, lineFormat);
  235. par->currentRop = ~0; /* set to something invalid */
  236. NVSetRopSolid(info, ROP_COPY, ~0);
  237. NVSetClippingRectangle(info, 0, 0, info->var.xres_virtual,
  238. info->var.yres_virtual);
  239. NVDmaKickoff(par);
  240. }
  241. int nvidiafb_sync(struct fb_info *info)
  242. {
  243. struct nvidia_par *par = info->par;
  244. if (info->state != FBINFO_STATE_RUNNING)
  245. return 0;
  246. if (!par->lockup)
  247. NVFlush(info);
  248. if (!par->lockup)
  249. NVSync(info);
  250. return 0;
  251. }
  252. void nvidiafb_copyarea(struct fb_info *info, const struct fb_copyarea *region)
  253. {
  254. struct nvidia_par *par = info->par;
  255. if (info->state != FBINFO_STATE_RUNNING)
  256. return;
  257. if (par->lockup) {
  258. cfb_copyarea(info, region);
  259. return;
  260. }
  261. NVDmaStart(info, par, BLIT_POINT_SRC, 3);
  262. NVDmaNext(par, (region->sy << 16) | region->sx);
  263. NVDmaNext(par, (region->dy << 16) | region->dx);
  264. NVDmaNext(par, (region->height << 16) | region->width);
  265. NVDmaKickoff(par);
  266. }
  267. void nvidiafb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
  268. {
  269. struct nvidia_par *par = info->par;
  270. u32 color;
  271. if (info->state != FBINFO_STATE_RUNNING)
  272. return;
  273. if (par->lockup) {
  274. cfb_fillrect(info, rect);
  275. return;
  276. }
  277. if (info->var.bits_per_pixel == 8)
  278. color = rect->color;
  279. else
  280. color = ((u32 *) info->pseudo_palette)[rect->color];
  281. if (rect->rop != ROP_COPY)
  282. NVSetRopSolid(info, rect->rop, ~0);
  283. NVDmaStart(info, par, RECT_SOLID_COLOR, 1);
  284. NVDmaNext(par, color);
  285. NVDmaStart(info, par, RECT_SOLID_RECTS(0), 2);
  286. NVDmaNext(par, (rect->dx << 16) | rect->dy);
  287. NVDmaNext(par, (rect->width << 16) | rect->height);
  288. NVDmaKickoff(par);
  289. if (rect->rop != ROP_COPY)
  290. NVSetRopSolid(info, ROP_COPY, ~0);
  291. }
  292. static void nvidiafb_mono_color_expand(struct fb_info *info,
  293. const struct fb_image *image)
  294. {
  295. struct nvidia_par *par = info->par;
  296. u32 fg, bg, mask = ~(~0 >> (32 - info->var.bits_per_pixel));
  297. u32 dsize, width, *data = (u32 *) image->data, tmp;
  298. int j, k = 0;
  299. width = (image->width + 31) & ~31;
  300. dsize = (width * image->height) >> 5;
  301. if (info->var.bits_per_pixel == 8) {
  302. fg = image->fg_color | mask;
  303. bg = image->bg_color | mask;
  304. } else {
  305. fg = ((u32 *) info->pseudo_palette)[image->fg_color] | mask;
  306. bg = ((u32 *) info->pseudo_palette)[image->bg_color] | mask;
  307. }
  308. NVDmaStart(info, par, RECT_EXPAND_TWO_COLOR_CLIP, 7);
  309. NVDmaNext(par, (image->dy << 16) | (image->dx & 0xffff));
  310. NVDmaNext(par, ((image->dy + image->height) << 16) |
  311. ((image->dx + image->width) & 0xffff));
  312. NVDmaNext(par, bg);
  313. NVDmaNext(par, fg);
  314. NVDmaNext(par, (image->height << 16) | width);
  315. NVDmaNext(par, (image->height << 16) | width);
  316. NVDmaNext(par, (image->dy << 16) | (image->dx & 0xffff));
  317. while (dsize >= RECT_EXPAND_TWO_COLOR_DATA_MAX_DWORDS) {
  318. NVDmaStart(info, par, RECT_EXPAND_TWO_COLOR_DATA(0),
  319. RECT_EXPAND_TWO_COLOR_DATA_MAX_DWORDS);
  320. for (j = RECT_EXPAND_TWO_COLOR_DATA_MAX_DWORDS; j--;) {
  321. tmp = data[k++];
  322. reverse_order(&tmp);
  323. NVDmaNext(par, tmp);
  324. }
  325. dsize -= RECT_EXPAND_TWO_COLOR_DATA_MAX_DWORDS;
  326. }
  327. if (dsize) {
  328. NVDmaStart(info, par, RECT_EXPAND_TWO_COLOR_DATA(0), dsize);
  329. for (j = dsize; j--;) {
  330. tmp = data[k++];
  331. reverse_order(&tmp);
  332. NVDmaNext(par, tmp);
  333. }
  334. }
  335. NVDmaKickoff(par);
  336. }
  337. void nvidiafb_imageblit(struct fb_info *info, const struct fb_image *image)
  338. {
  339. struct nvidia_par *par = info->par;
  340. if (info->state != FBINFO_STATE_RUNNING)
  341. return;
  342. if (image->depth == 1 && !par->lockup)
  343. nvidiafb_mono_color_expand(info, image);
  344. else
  345. cfb_imageblit(info, image);
  346. }