radeon.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 only 2 KB (0x49800..0x49000) and a
  21. * 16 KB stack allocation overflows it.
  22. */
  23. #include "radeon.h"
  24. #include "pci.h"
  25. #include "paging.h"
  26. #include "serial.h"
  27. #include "vga.h"
  28. static unsigned int radeon_mmio; /* BAR2 register aperture (1:1 mapped) */
  29. static int radeon_present;
  30. #define RADEON_MM_INDEX 0x0000
  31. #define RADEON_MM_DATA 0x0004
  32. #define RADEON_CRTC_GEN_CNTL 0x0050
  33. #define RADEON_CRTC_CUR_EN (1u << 16)
  34. #define RADEON_CRTC_CUR_MODE_MASK (7u << 20)
  35. #define RADEON_CRTC_CUR_MODE_24BPP (2u << 20)
  36. #define RADEON_CUR_OFFSET 0x0260
  37. #define RADEON_CUR_HORZ_VERT_POSN 0x0264
  38. #define RADEON_CUR_HORZ_VERT_OFF 0x0268
  39. #define RADEON_CUR_SIZE 64 /* 64x64 pixels */
  40. #define RADEON_CUR_BYTES (RADEON_CUR_SIZE * RADEON_CUR_SIZE * 4)
  41. /* Cursor bitmap byte offset within card memory (offscreen, above the visible
  42. scanout and inside the 16 MB mapped LFB window). */
  43. #define RADEON_CUR_VRAM_OFS 0x800000
  44. static inline unsigned int mmio_read(unsigned int ofs)
  45. {
  46. volatile unsigned int *p =
  47. (volatile unsigned int *)(radeon_mmio + ofs);
  48. return *p;
  49. }
  50. static inline void mmio_write(unsigned int ofs, unsigned int v)
  51. {
  52. volatile unsigned int *p =
  53. (volatile unsigned int *)(radeon_mmio + ofs);
  54. *p = v;
  55. }
  56. /* Access an MM register-file entry via MM_INDEX / MM_DATA. */
  57. static inline unsigned int mmio_mm_read(unsigned int reg)
  58. {
  59. mmio_write(RADEON_MM_INDEX, reg);
  60. return mmio_read(RADEON_MM_DATA);
  61. }
  62. static inline void mmio_mm_write(unsigned int reg, unsigned int v)
  63. {
  64. mmio_write(RADEON_MM_INDEX, reg);
  65. mmio_write(RADEON_MM_DATA, v);
  66. }
  67. /* 16x16 arrow (MSB = column 0), matching the software cursor shape. */
  68. static const unsigned short radeon_arrow[16] = {
  69. 0x8000, 0xC000, 0xE000, 0xF000, 0xF800, 0xFC00, 0xFE00, 0xFF00,
  70. 0xFC00, 0xE000, 0x8000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  71. };
  72. void radeon_init(unsigned int bus, unsigned int dev, unsigned int device_id)
  73. {
  74. unsigned int bar2, bar2_base, bar2_size;
  75. radeon_present = 0;
  76. radeon_mmio = 0;
  77. (void)device_id;
  78. /* BAR2 is the register aperture on Radeon. */
  79. bar2 = pci_cfg_read32(bus, dev, 0, 0x18);
  80. if ((bar2 & 1) || !(bar2 & 0xFFFFFFF0))
  81. return;
  82. bar2_base = bar2 & 0xFFFFFFF0;
  83. pci_cfg_write32(bus, dev, 0, 0x18, 0xFFFFFFFF);
  84. bar2_size = pci_cfg_read32(bus, dev, 0, 0x18);
  85. pci_cfg_write32(bus, dev, 0, 0x18, bar2);
  86. bar2_size = (~(bar2_size & 0xFFFFFFF0)) + 1;
  87. if (bar2_size < 0x10000)
  88. return;
  89. paging_map_mmio(bar2_base, bar2_size);
  90. radeon_mmio = bar2_base;
  91. /* Validate the aperture: a master abort reads back all-ones. */
  92. mmio_write(RADEON_MM_INDEX, RADEON_CRTC_GEN_CNTL);
  93. if (mmio_read(RADEON_MM_DATA) == 0xFFFFFFFF) {
  94. radeon_mmio = 0;
  95. return;
  96. }
  97. radeon_present = 1;
  98. serial_puts(SERIAL_COM1, "radeon: cursor MMIO ");
  99. serial_puthex(SERIAL_COM1, radeon_mmio);
  100. serial_puts(SERIAL_COM1, "\n");
  101. }
  102. int radeon_available(void)
  103. {
  104. return radeon_present;
  105. }
  106. void radeon_cursor_setup(void)
  107. {
  108. unsigned char *dst;
  109. unsigned int vram;
  110. unsigned int i, r, c, g;
  111. if (!radeon_present)
  112. return;
  113. vram = vga_fb_vram_kb() * 1024;
  114. if (vram < RADEON_CUR_VRAM_OFS + RADEON_CUR_BYTES) {
  115. radeon_present = 0;
  116. return;
  117. }
  118. dst = (unsigned char *)(vga_fb_lfb_addr() + RADEON_CUR_VRAM_OFS);
  119. /* Transparent background (alpha 0), then the opaque white arrow. */
  120. for (i = 0; i < RADEON_CUR_BYTES; i++)
  121. dst[i] = 0;
  122. for (r = 0; r < 16; r++) {
  123. unsigned short bits = radeon_arrow[r];
  124. for (c = 0; c < 16; c++) {
  125. if (bits & (0x8000u >> c)) {
  126. unsigned int off = (r * RADEON_CUR_SIZE + c) * 4;
  127. dst[off + 0] = 0xFF; /* B */
  128. dst[off + 1] = 0xFF; /* G */
  129. dst[off + 2] = 0xFF; /* R */
  130. dst[off + 3] = 0xFF; /* A */
  131. }
  132. }
  133. }
  134. mmio_write(RADEON_CUR_OFFSET, RADEON_CUR_VRAM_OFS);
  135. mmio_write(RADEON_CUR_HORZ_VERT_OFF, 0); /* hotspot at (0,0) */
  136. mmio_write(RADEON_CUR_HORZ_VERT_POSN, 0); /* position set on move */
  137. /* Enable the 24 bpp (ARGB8888) hardware cursor. */
  138. g = mmio_mm_read(RADEON_CRTC_GEN_CNTL);
  139. g &= ~(RADEON_CRTC_CUR_EN | RADEON_CRTC_CUR_MODE_MASK);
  140. g |= RADEON_CRTC_CUR_EN | RADEON_CRTC_CUR_MODE_24BPP;
  141. mmio_mm_write(RADEON_CRTC_GEN_CNTL, g);
  142. }
  143. void radeon_cursor_move(unsigned int x, unsigned int y)
  144. {
  145. if (!radeon_present)
  146. return;
  147. mmio_write(RADEON_CUR_HORZ_VERT_POSN, (x << 16) | y);
  148. }
  149. void radeon_cursor_show(int show)
  150. {
  151. unsigned int g;
  152. if (!radeon_present)
  153. return;
  154. g = mmio_mm_read(RADEON_CRTC_GEN_CNTL);
  155. if (show)
  156. g |= RADEON_CRTC_CUR_EN;
  157. else
  158. g &= ~RADEON_CRTC_CUR_EN;
  159. mmio_mm_write(RADEON_CRTC_GEN_CNTL, g);
  160. }