intel.c 12 KB

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