intel.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * Intel Ironlake (Gen5) hardware cursor.
  3. *
  4. * The Ironlake display engine fetches the cursor through the global GTT, so
  5. * enabling it needs more than a register write: the 64x64 ARGB image must be
  6. * mapped at a free GTT slot. BAR0 contains the MMIO registers followed by
  7. * the GTT at +2 MB; the BIOS has already mapped the VBE framebuffer at GTT
  8. * offset 0, and the table has free entries above it.
  9. *
  10. * Register semantics follow the Linux i915 Ironlake path:
  11. * - CURACNTR/CURABASE/CURAPOS are per-pipe (A at 0x70080, B at 0x700c0).
  12. * - 64x64 ARGB8888 cursor, MSB A:R:G:B, hotspot at (0,0); the mouse code
  13. * positions the image's top-left, so CURPOS takes negative values in
  14. * sign-magnitude form (bit 15 = X sign, bit 31 = Y sign).
  15. * - CURCNTR bit 31 enables, mode 0x27 is 64x64 ARGB, bits 29:28 hold the
  16. * pipe select. CURBASE is written after CURPOS to arm the update.
  17. * - The GTT PTE is the physical page, its bits 35:32 in PTE bits 7:4, the
  18. * uncached mapping type (0) and the valid bit (1).
  19. */
  20. #include "intel.h"
  21. #include "cursor.h"
  22. #include "mm.h"
  23. #include "paging.h"
  24. #include "pci.h"
  25. #include "serial.h"
  26. #include "vga.h"
  27. #define INTEL_IRONLAKE_D 0x0042
  28. #define INTEL_IRONLAKE_M 0x0046
  29. #define INTEL_BAR0 0x10
  30. #define INTEL_BAR2 0x18
  31. #define INTEL_GTT_OFFSET 0x200000
  32. #define INTEL_PGETBL_CTL 0x2020
  33. /* Display pipe registers. */
  34. #define DSPACNTR 0x70180
  35. #define DSPASURF 0x7019c
  36. #define DSPBCNTR 0x71180
  37. #define DSPBSURF 0x7119c
  38. /* Cursor registers (A; B is +0x40). */
  39. #define CURACNTR 0x70080
  40. #define CURABASE 0x70084
  41. #define CURAPOS 0x70088
  42. #define CURASURFLIVE 0x700ac
  43. #define CUR_PIPE_B 0x40
  44. #define DSP_PLANE_ENABLE (1u << 31)
  45. #define CURSOR_ENABLE (1u << 31)
  46. #define CURSOR_GAMMA_ENABLE (1u << 26)
  47. #define CURSOR_MODE_64_ARGB 0x27u
  48. #define CURSOR_PIPE_SEL_SHIFT 28
  49. #define CUR_SIZE 64
  50. #define CUR_BYTES (CUR_SIZE * CUR_SIZE * 4)
  51. #define CUR_PAGES (CUR_BYTES / 4096)
  52. #define GTT_PTE_VALID 0x1u
  53. #define GTT_PTE_UNCACHED 0x0u
  54. static unsigned int intel_mmio;
  55. static volatile unsigned int *intel_gtt;
  56. static unsigned int intel_gtt_entries;
  57. static unsigned int intel_gtt_limit;
  58. static unsigned int intel_cursor_gtt;
  59. static unsigned int intel_cursor_phys;
  60. static unsigned int intel_pipe_mask;
  61. static int intel_probe_ok;
  62. static int intel_cursor_ok;
  63. static inline unsigned int mmio_read(unsigned int ofs)
  64. {
  65. return *(volatile unsigned int *)(intel_mmio + ofs);
  66. }
  67. static inline void mmio_write(unsigned int ofs, unsigned int val)
  68. {
  69. *(volatile unsigned int *)(intel_mmio + ofs) = val;
  70. }
  71. static unsigned int intel_bar_base(unsigned int value)
  72. {
  73. if (value & 1)
  74. return 0;
  75. return value & 0xFFFFFFF0u;
  76. }
  77. static unsigned int intel_bar_size(unsigned int bus, unsigned int dev,
  78. unsigned int reg)
  79. {
  80. unsigned int save, raw;
  81. save = pci_cfg_read32(bus, dev, 0, reg);
  82. pci_cfg_write32(bus, dev, 0, reg, 0xFFFFFFFFu);
  83. raw = pci_cfg_read32(bus, dev, 0, reg);
  84. pci_cfg_write32(bus, dev, 0, reg, save);
  85. if ((save & 1) || !(raw & 0xFFFFFFF0u))
  86. return 0;
  87. return (~(raw & 0xFFFFFFF0u)) + 1;
  88. }
  89. static unsigned int gtt_size_from_pgetbl(unsigned int pgetbl)
  90. {
  91. switch (pgetbl & 0xE) {
  92. case 0: return 512 * 1024;
  93. case 1: return 256 * 1024;
  94. case 2: return 128 * 1024;
  95. case 3: return 1024 * 1024;
  96. case 4: return 2048 * 1024;
  97. case 5: return 1536 * 1024;
  98. default: return 512 * 1024;
  99. }
  100. }
  101. /* The BIOS leaves unused PTEs as all-ones rather than clearing the valid
  102. * bit, so test both forms. */
  103. static int gtt_pte_free(unsigned int pte)
  104. {
  105. return pte == 0xFFFFFFFFu || !(pte & GTT_PTE_VALID);
  106. }
  107. /* Find four consecutive invalid PTEs, preferably just above the visible
  108. * framebuffer and 64 KB aligned (the VT-d workaround alignment). */
  109. static unsigned int gtt_find_slot(void)
  110. {
  111. unsigned int i, j;
  112. unsigned int limit = intel_gtt_limit ? intel_gtt_limit
  113. : intel_gtt_entries;
  114. unsigned int fb_pages;
  115. if (limit < 32)
  116. return 0;
  117. fb_pages = (vga_fb_pitch() * vga_fb_height() + 4095) / 4096;
  118. i = (fb_pages + 15) & ~15u;
  119. if (i < 16)
  120. i = 16;
  121. for (; i + CUR_PAGES <= limit; i += 16) {
  122. for (j = 0; j < CUR_PAGES; j++)
  123. if (!gtt_pte_free(intel_gtt[i + j]))
  124. break;
  125. if (j == CUR_PAGES)
  126. return i;
  127. }
  128. /* Fallback: high entries inside the aperture. */
  129. i = (limit - 64) & ~15u;
  130. while (i >= 16) {
  131. for (j = 0; j < CUR_PAGES; j++)
  132. if (!gtt_pte_free(intel_gtt[i + j]))
  133. break;
  134. if (j == CUR_PAGES)
  135. return i;
  136. i -= 16;
  137. }
  138. return 0;
  139. }
  140. /* The cursor fetch is happy with 4 KB on Ironlake unless VT-d is active;
  141. * still give it a 64 KB-aligned physical block so the VT-d workaround
  142. * alignment is covered as well. */
  143. static int intel_alloc_cursor(void)
  144. {
  145. unsigned int raw, aligned, pad, i;
  146. raw = mm_alloc_pages(32);
  147. if (!raw)
  148. return -1;
  149. aligned = (raw + 0xFFFFu) & ~0xFFFFu;
  150. pad = (aligned - raw) >> 12;
  151. for (i = 0; i < pad; i++)
  152. mm_free_page(raw + i * 4096);
  153. for (i = pad + CUR_PAGES; i < 32; i++)
  154. mm_free_page(raw + i * 4096);
  155. intel_cursor_phys = aligned;
  156. return 0;
  157. }
  158. /* Copy a cursor image (premultiplied ARGB, as the plane expects) into the
  159. * top-left of the 64x64 plane; NULL clears it. */
  160. static void intel_fill_cursor(unsigned int phys, const struct cursor_image *ci)
  161. {
  162. unsigned int *img = (unsigned int *)phys;
  163. unsigned int i, r, c;
  164. for (i = 0; i < CUR_SIZE * CUR_SIZE; i++)
  165. img[i] = 0;
  166. if (ci && ci->px)
  167. for (r = 0; r < ci->h && r < CUR_SIZE; r++)
  168. for (c = 0; c < ci->w && c < CUR_SIZE; c++)
  169. img[r * CUR_SIZE + c] = ci->px[r * ci->w + c];
  170. /* The GTT mapping is uncached, so push the WB image out to DRAM. */
  171. __asm__ volatile ("wbinvd" ::: "memory");
  172. }
  173. static int intel_map_cursor(unsigned int slot)
  174. {
  175. unsigned int i;
  176. for (i = 0; i < CUR_PAGES; i++) {
  177. unsigned int pa = intel_cursor_phys + i * 4096;
  178. unsigned int pte = (pa & 0xFFFFF000u) |
  179. ((pa >> 28) & 0xF0u) |
  180. GTT_PTE_UNCACHED | GTT_PTE_VALID;
  181. intel_gtt[slot + i] = pte;
  182. }
  183. /* Posting read: also makes the display engine invalidate its TLB. */
  184. (void)intel_gtt[slot + CUR_PAGES - 1];
  185. /* Do not point the cursor plane at a slot the GTT did not accept. */
  186. for (i = 0; i < CUR_PAGES; i++) {
  187. unsigned int pa = intel_cursor_phys + i * 4096;
  188. if ((intel_gtt[slot + i] & 0xFFFFF000u) !=
  189. (pa & 0xFFFFF000u))
  190. return -1;
  191. }
  192. return 0;
  193. }
  194. static unsigned int cursor_ctrl(unsigned int pipe)
  195. {
  196. return CURSOR_ENABLE | CURSOR_GAMMA_ENABLE |
  197. CURSOR_MODE_64_ARGB | (pipe << CURSOR_PIPE_SEL_SHIFT);
  198. }
  199. void intel_init(unsigned int bus, unsigned int dev, unsigned int device_id)
  200. {
  201. unsigned int bar0, bar0_size, bar2_base, bar2_size;
  202. unsigned int lfb, lfb_gtt = 0xFFFFFFFFu;
  203. unsigned int pgetbl, gtt_size;
  204. unsigned int dspa, dspb, surfa, surfb;
  205. unsigned int slot;
  206. intel_probe_ok = 0;
  207. intel_cursor_ok = 0;
  208. intel_mmio = 0;
  209. intel_gtt = 0;
  210. intel_pipe_mask = 0;
  211. if (device_id != INTEL_IRONLAKE_D && device_id != INTEL_IRONLAKE_M)
  212. return;
  213. bar0 = pci_cfg_read32(bus, dev, 0, INTEL_BAR0);
  214. if ((bar0 & 1) || !(bar0 & 0xFFFFFFF0u))
  215. return;
  216. if ((bar0 & 0x6) == 0x4) {
  217. /* 64-bit BAR; the kernel cannot address anything above 4 GB. */
  218. if (pci_cfg_read32(bus, dev, 0, INTEL_BAR0 + 4))
  219. return;
  220. }
  221. bar0_size = intel_bar_size(bus, dev, INTEL_BAR0);
  222. if (bar0_size < 0x400000)
  223. bar0_size = 0x400000;
  224. if (bar0_size > 0x1000000)
  225. bar0_size = 0x1000000;
  226. intel_mmio = intel_bar_base(bar0);
  227. paging_map_mmio(intel_mmio, bar0_size);
  228. pgetbl = mmio_read(INTEL_PGETBL_CTL);
  229. if (pgetbl == 0xFFFFFFFFu) {
  230. serial_puts(SERIAL_COM1, "intel: no display MMIO\n");
  231. return;
  232. }
  233. /* A kexec leaves the previous kernel's cursor enabled, pointing at a
  234. * GTT slot this kernel may no longer own. Clear both cursor planes;
  235. * the new cursor is enabled later from mouse_cursor_setup(). */
  236. mmio_write(CURACNTR, 0);
  237. mmio_write(CURACNTR + CUR_PIPE_B, 0);
  238. gtt_size = gtt_size_from_pgetbl(pgetbl);
  239. intel_gtt_entries = gtt_size / 4;
  240. intel_gtt = (volatile unsigned int *)(intel_mmio + INTEL_GTT_OFFSET);
  241. bar2_base = intel_bar_base(pci_cfg_read32(bus, dev, 0, INTEL_BAR2));
  242. bar2_size = intel_bar_size(bus, dev, INTEL_BAR2);
  243. intel_gtt_limit = intel_gtt_entries;
  244. if (bar2_size >= 0x1000000 && bar2_size / 4096 < intel_gtt_limit)
  245. intel_gtt_limit = bar2_size / 4096;
  246. lfb = vga_fb_lfb_addr();
  247. if (bar2_base && lfb >= bar2_base)
  248. lfb_gtt = (lfb - bar2_base) & 0xFFFFF000u;
  249. dspa = mmio_read(DSPACNTR);
  250. dspb = mmio_read(DSPBCNTR);
  251. surfa = mmio_read(DSPASURF) & 0xFFFFF000u;
  252. surfb = mmio_read(DSPBSURF) & 0xFFFFF000u;
  253. if ((dspa & DSP_PLANE_ENABLE) && surfa == lfb_gtt)
  254. intel_pipe_mask |= 1;
  255. if ((dspb & DSP_PLANE_ENABLE) && surfb == lfb_gtt)
  256. intel_pipe_mask |= 2;
  257. if (!intel_pipe_mask) {
  258. /* No surface match (or an unexpected BIOS layout): use every
  259. * pipe the VBIOS left enabled. */
  260. if (dspa & DSP_PLANE_ENABLE)
  261. intel_pipe_mask |= 1;
  262. if (dspb & DSP_PLANE_ENABLE)
  263. intel_pipe_mask |= 2;
  264. }
  265. if (!intel_pipe_mask) {
  266. serial_puts(SERIAL_COM1, "intel: no enabled display pipe\n");
  267. return;
  268. }
  269. slot = gtt_find_slot();
  270. if (!slot) {
  271. serial_puts(SERIAL_COM1, "intel: no free GTT slot\n");
  272. return;
  273. }
  274. if (intel_alloc_cursor() != 0) {
  275. serial_puts(SERIAL_COM1, "intel: no cursor memory\n");
  276. return;
  277. }
  278. intel_cursor_gtt = slot * 4096;
  279. intel_fill_cursor(intel_cursor_phys, 0);
  280. if (intel_map_cursor(slot) != 0) {
  281. serial_puts(SERIAL_COM1, "intel: GTT write did not stick\n");
  282. return;
  283. }
  284. serial_puts(SERIAL_COM1, "intel: Ironlake GTT ");
  285. serial_puthex(SERIAL_COM1, intel_gtt_entries * 4096);
  286. serial_puts(SERIAL_COM1, ", lfb gtt ");
  287. serial_puthex(SERIAL_COM1, lfb_gtt);
  288. serial_puts(SERIAL_COM1, ", pipe ");
  289. serial_puthex(SERIAL_COM1, intel_pipe_mask);
  290. serial_puts(SERIAL_COM1, ", cursor ");
  291. serial_puthex(SERIAL_COM1, intel_cursor_gtt);
  292. serial_puts(SERIAL_COM1, "\n");
  293. intel_probe_ok = 1;
  294. }
  295. int intel_available(void)
  296. {
  297. return intel_probe_ok;
  298. }
  299. static unsigned int intel_cursor_pos(int x, int y)
  300. {
  301. unsigned int pos = 0;
  302. if (x < 0) {
  303. pos |= 0x8000u;
  304. x = -x;
  305. }
  306. if (y < 0) {
  307. pos |= 0x80000000u;
  308. y = -y;
  309. }
  310. return pos | ((unsigned int)x & 0x7FFFu) |
  311. (((unsigned int)y & 0x7FFFu) << 16);
  312. }
  313. static void intel_cursor_program(unsigned int pipe, int x, int y)
  314. {
  315. unsigned int base = CURACNTR + (pipe ? CUR_PIPE_B : 0);
  316. mmio_write(base, cursor_ctrl(pipe));
  317. mmio_write(base + 8, intel_cursor_pos(x, y)); /* CURAPOS */
  318. mmio_write(base + 4, intel_cursor_gtt); /* CURABASE */
  319. (void)mmio_read(base + 4);
  320. }
  321. int intel_cursor_setup(int x, int y)
  322. {
  323. if (!intel_probe_ok || !intel_cursor_gtt)
  324. return -1;
  325. if (intel_pipe_mask & 1)
  326. intel_cursor_program(0, x, y);
  327. if (intel_pipe_mask & 2)
  328. intel_cursor_program(1, x, y);
  329. serial_puts(SERIAL_COM1, "intel: cursor enabled, live ");
  330. serial_puthex(SERIAL_COM1, mmio_read(CURASURFLIVE));
  331. serial_puts(SERIAL_COM1, "\n");
  332. intel_cursor_ok = 1;
  333. return 0;
  334. }
  335. void intel_cursor_load(const struct cursor_image *img)
  336. {
  337. if (!intel_probe_ok || !intel_cursor_phys)
  338. return;
  339. intel_fill_cursor(intel_cursor_phys, img);
  340. /* Re-arm CURBASE so the plane fetches the new image. */
  341. if (intel_cursor_ok) {
  342. if (intel_pipe_mask & 1)
  343. mmio_write(CURABASE, intel_cursor_gtt);
  344. if (intel_pipe_mask & 2)
  345. mmio_write(CURABASE + CUR_PIPE_B, intel_cursor_gtt);
  346. }
  347. }
  348. void intel_cursor_move(int x, int y)
  349. {
  350. unsigned int pos;
  351. if (!intel_cursor_ok)
  352. return;
  353. pos = intel_cursor_pos(x, y);
  354. if (intel_pipe_mask & 1) {
  355. mmio_write(CURAPOS, pos);
  356. mmio_write(CURABASE, intel_cursor_gtt);
  357. }
  358. if (intel_pipe_mask & 2) {
  359. mmio_write(CURAPOS + CUR_PIPE_B, pos);
  360. mmio_write(CURABASE + CUR_PIPE_B, intel_cursor_gtt);
  361. }
  362. }