radeon.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * ATI Radeon (R200-family / RV280) hardware cursor.
  3. *
  4. * The RV280's VBIOS sets a clean VBE mode, so (unlike Mach64/Rage) it needs
  5. * no CRTC fixup. What it can give us is a hardware cursor: a 64x64 ARGB8888
  6. * overlay composited by the display engine, so moving the pointer no longer
  7. * costs a save/restore + dirty-row flush on the CPU.
  8. *
  9. * Register semantics follow the Linux "radeon" driver (R100/R200 legacy
  10. * cursor path):
  11. *
  12. * - Cursor image: 64x64 ARGB8888 (32 bpp, 16 KB), byte order B,G,R,A.
  13. * - RADEON_CUR_* are direct-MMIO registers in BAR2.
  14. * - RADEON_CRTC_GEN_CNTL is an MM register file entry, accessed via
  15. * RADEON_MM_INDEX / RADEON_MM_DATA.
  16. * - Position format is (x << 16) | y (x in bits 30:16, y in bits 15:0).
  17. * - Cursor mode is RADEON_CRTC_CUR_MODE_24BPP (2) in bits 22:20.
  18. *
  19. * NOTE: the bitmap is written straight into VRAM. Do not use a local
  20. * [16 KB] array -- the kernel stack is 8 KB (the in-image .stack section,
  21. * see kernel.ld) and a 16 KB stack allocation overflows it.
  22. */
  23. #include "radeon.h"
  24. #include "cursor.h"
  25. #include "pci.h"
  26. #include "paging.h"
  27. #include "serial.h"
  28. #include "vga.h"
  29. static unsigned int radeon_mmio; /* BAR2 register aperture (1:1 mapped) */
  30. static int radeon_present;
  31. #define RADEON_MM_INDEX 0x0000
  32. #define RADEON_MM_DATA 0x0004
  33. #define RADEON_CRTC_GEN_CNTL 0x0050
  34. #define RADEON_CRTC_CUR_EN (1u << 16)
  35. #define RADEON_CRTC_CUR_MODE_MASK (7u << 20)
  36. #define RADEON_CRTC_CUR_MODE_24BPP (2u << 20)
  37. #define RADEON_CUR_OFFSET 0x0260
  38. #define RADEON_CUR_HORZ_VERT_POSN 0x0264
  39. #define RADEON_CUR_HORZ_VERT_OFF 0x0268
  40. #define RADEON_CUR_SIZE 64 /* 64x64 pixels */
  41. #define RADEON_CUR_BYTES (RADEON_CUR_SIZE * RADEON_CUR_SIZE * 4)
  42. /* Cursor bitmap byte offset within card memory (offscreen, above the visible
  43. scanout and inside the 16 MB mapped LFB window). */
  44. #define RADEON_CUR_VRAM_OFS 0x800000
  45. static inline unsigned int mmio_read(unsigned int ofs)
  46. {
  47. volatile unsigned int *p =
  48. (volatile unsigned int *)(radeon_mmio + ofs);
  49. return *p;
  50. }
  51. static inline void mmio_write(unsigned int ofs, unsigned int v)
  52. {
  53. volatile unsigned int *p =
  54. (volatile unsigned int *)(radeon_mmio + ofs);
  55. *p = v;
  56. }
  57. /* Access an MM register-file entry via MM_INDEX / MM_DATA. */
  58. static inline unsigned int mmio_mm_read(unsigned int reg)
  59. {
  60. mmio_write(RADEON_MM_INDEX, reg);
  61. return mmio_read(RADEON_MM_DATA);
  62. }
  63. static inline void mmio_mm_write(unsigned int reg, unsigned int v)
  64. {
  65. mmio_write(RADEON_MM_INDEX, reg);
  66. mmio_write(RADEON_MM_DATA, v);
  67. }
  68. void radeon_init(unsigned int bus, unsigned int dev, unsigned int device_id)
  69. {
  70. unsigned int bar2, bar2_base, bar2_size;
  71. radeon_present = 0;
  72. radeon_mmio = 0;
  73. (void)device_id;
  74. /* BAR2 is the register aperture on Radeon. */
  75. bar2 = pci_cfg_read32(bus, dev, 0, 0x18);
  76. if ((bar2 & 1) || !(bar2 & 0xFFFFFFF0))
  77. return;
  78. bar2_base = bar2 & 0xFFFFFFF0;
  79. pci_cfg_write32(bus, dev, 0, 0x18, 0xFFFFFFFF);
  80. bar2_size = pci_cfg_read32(bus, dev, 0, 0x18);
  81. pci_cfg_write32(bus, dev, 0, 0x18, bar2);
  82. bar2_size = (~(bar2_size & 0xFFFFFFF0)) + 1;
  83. if (bar2_size < 0x10000)
  84. return;
  85. paging_map_mmio(bar2_base, bar2_size);
  86. radeon_mmio = bar2_base;
  87. /* Validate the aperture: a master abort reads back all-ones. */
  88. mmio_write(RADEON_MM_INDEX, RADEON_CRTC_GEN_CNTL);
  89. if (mmio_read(RADEON_MM_DATA) == 0xFFFFFFFF) {
  90. radeon_mmio = 0;
  91. return;
  92. }
  93. radeon_present = 1;
  94. serial_puts(SERIAL_COM1, "radeon: cursor MMIO ");
  95. serial_puthex(SERIAL_COM1, radeon_mmio);
  96. serial_puts(SERIAL_COM1, "\n");
  97. }
  98. int radeon_available(void)
  99. {
  100. return radeon_present;
  101. }
  102. #define RADEON_CUR_LOCK (1u << 31)
  103. /* Copy a cursor image into the top-left of the 64x64 ARGB plane (the image
  104. * is premultiplied ARGB8888, which the R200 cursor blends as-is). */
  105. void radeon_cursor_load(const struct cursor_image *img)
  106. {
  107. unsigned int *dst;
  108. unsigned int i, r, c;
  109. if (!radeon_present)
  110. return;
  111. dst = (unsigned int *)(vga_fb_lfb_addr() + RADEON_CUR_VRAM_OFS);
  112. for (i = 0; i < RADEON_CUR_SIZE * RADEON_CUR_SIZE; i++)
  113. dst[i] = 0;
  114. if (!img || !img->px)
  115. return;
  116. for (r = 0; r < img->h && r < RADEON_CUR_SIZE; r++)
  117. for (c = 0; c < img->w && c < RADEON_CUR_SIZE; c++)
  118. dst[r * RADEON_CUR_SIZE + c] = img->px[r * img->w + c];
  119. }
  120. void radeon_cursor_setup(const struct cursor_image *img)
  121. {
  122. unsigned int vram, g;
  123. if (!radeon_present)
  124. return;
  125. vram = vga_fb_vram_kb() * 1024;
  126. if (vram < RADEON_CUR_VRAM_OFS + RADEON_CUR_BYTES) {
  127. radeon_present = 0;
  128. return;
  129. }
  130. radeon_cursor_load(img);
  131. mmio_write(RADEON_CUR_OFFSET, RADEON_CUR_VRAM_OFS);
  132. mmio_write(RADEON_CUR_HORZ_VERT_OFF, 0);
  133. mmio_write(RADEON_CUR_HORZ_VERT_POSN, 0); /* position set on move */
  134. /* Enable the 24 bpp (ARGB8888) hardware cursor. */
  135. g = mmio_mm_read(RADEON_CRTC_GEN_CNTL);
  136. g &= ~(RADEON_CRTC_CUR_EN | RADEON_CRTC_CUR_MODE_MASK);
  137. g |= RADEON_CRTC_CUR_EN | RADEON_CRTC_CUR_MODE_24BPP;
  138. mmio_mm_write(RADEON_CRTC_GEN_CNTL, g);
  139. }
  140. /* (x,y) is the image's top-left. The position register cannot go
  141. * negative: like Linux's legacy radeon_cursor_move_locked(), clamp it at 0
  142. * and skip the hidden part through HORZ_VERT_OFF (x) and a CUR_OFFSET that
  143. * starts yorigin rows (256 bytes each) into the image. */
  144. void radeon_cursor_move(int x, int y)
  145. {
  146. unsigned int xo = 0, yo = 0;
  147. if (!radeon_present)
  148. return;
  149. if (x < 0) {
  150. xo = (unsigned int)-x;
  151. if (xo > RADEON_CUR_SIZE - 1)
  152. xo = RADEON_CUR_SIZE - 1;
  153. x = 0;
  154. }
  155. if (y < 0) {
  156. yo = (unsigned int)-y;
  157. if (yo > RADEON_CUR_SIZE - 1)
  158. yo = RADEON_CUR_SIZE - 1;
  159. y = 0;
  160. }
  161. mmio_write(RADEON_CUR_HORZ_VERT_OFF, RADEON_CUR_LOCK | (xo << 16) | yo);
  162. mmio_write(RADEON_CUR_HORZ_VERT_POSN, RADEON_CUR_LOCK |
  163. ((unsigned int)x << 16) | (unsigned int)y);
  164. mmio_write(RADEON_CUR_OFFSET, RADEON_CUR_VRAM_OFS + yo * 256);
  165. }
  166. void radeon_cursor_show(int show)
  167. {
  168. unsigned int g;
  169. if (!radeon_present)
  170. return;
  171. g = mmio_mm_read(RADEON_CRTC_GEN_CNTL);
  172. if (show)
  173. g |= RADEON_CRTC_CUR_EN;
  174. else
  175. g &= ~RADEON_CRTC_CUR_EN;
  176. mmio_mm_write(RADEON_CRTC_GEN_CNTL, g);
  177. }