nouveau_hw.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Copyright 2008 Stuart Bennett
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
  19. * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #ifndef __NOUVEAU_HW_H__
  23. #define __NOUVEAU_HW_H__
  24. #include <drm/drmP.h>
  25. #include "nv04_display.h"
  26. #include <subdev/bios/pll.h>
  27. #define MASK(field) ( \
  28. (0xffffffff >> (31 - ((1 ? field) - (0 ? field)))) << (0 ? field))
  29. #define XLATE(src, srclowbit, outfield) ( \
  30. (((src) >> (srclowbit)) << (0 ? outfield)) & MASK(outfield))
  31. void NVWriteVgaSeq(struct drm_device *, int head, uint8_t index, uint8_t value);
  32. uint8_t NVReadVgaSeq(struct drm_device *, int head, uint8_t index);
  33. void NVWriteVgaGr(struct drm_device *, int head, uint8_t index, uint8_t value);
  34. uint8_t NVReadVgaGr(struct drm_device *, int head, uint8_t index);
  35. void NVSetOwner(struct drm_device *, int owner);
  36. void NVBlankScreen(struct drm_device *, int head, bool blank);
  37. int nouveau_hw_get_pllvals(struct drm_device *, enum nvbios_pll_type plltype,
  38. struct nouveau_pll_vals *pllvals);
  39. int nouveau_hw_pllvals_to_clk(struct nouveau_pll_vals *pllvals);
  40. int nouveau_hw_get_clock(struct drm_device *, enum nvbios_pll_type plltype);
  41. void nouveau_hw_save_vga_fonts(struct drm_device *, bool save);
  42. void nouveau_hw_save_state(struct drm_device *, int head,
  43. struct nv04_mode_state *state);
  44. void nouveau_hw_load_state(struct drm_device *, int head,
  45. struct nv04_mode_state *state);
  46. void nouveau_hw_load_state_palette(struct drm_device *, int head,
  47. struct nv04_mode_state *state);
  48. /* nouveau_calc.c */
  49. extern void nouveau_calc_arb(struct drm_device *, int vclk, int bpp,
  50. int *burst, int *lwm);
  51. static inline uint32_t NVReadCRTC(struct drm_device *dev,
  52. int head, uint32_t reg)
  53. {
  54. struct nouveau_device *device = nouveau_dev(dev);
  55. uint32_t val;
  56. if (head)
  57. reg += NV_PCRTC0_SIZE;
  58. val = nv_rd32(device, reg);
  59. return val;
  60. }
  61. static inline void NVWriteCRTC(struct drm_device *dev,
  62. int head, uint32_t reg, uint32_t val)
  63. {
  64. struct nouveau_device *device = nouveau_dev(dev);
  65. if (head)
  66. reg += NV_PCRTC0_SIZE;
  67. nv_wr32(device, reg, val);
  68. }
  69. static inline uint32_t NVReadRAMDAC(struct drm_device *dev,
  70. int head, uint32_t reg)
  71. {
  72. struct nouveau_device *device = nouveau_dev(dev);
  73. uint32_t val;
  74. if (head)
  75. reg += NV_PRAMDAC0_SIZE;
  76. val = nv_rd32(device, reg);
  77. return val;
  78. }
  79. static inline void NVWriteRAMDAC(struct drm_device *dev,
  80. int head, uint32_t reg, uint32_t val)
  81. {
  82. struct nouveau_device *device = nouveau_dev(dev);
  83. if (head)
  84. reg += NV_PRAMDAC0_SIZE;
  85. nv_wr32(device, reg, val);
  86. }
  87. static inline uint8_t nv_read_tmds(struct drm_device *dev,
  88. int or, int dl, uint8_t address)
  89. {
  90. int ramdac = (or & DCB_OUTPUT_C) >> 2;
  91. NVWriteRAMDAC(dev, ramdac, NV_PRAMDAC_FP_TMDS_CONTROL + dl * 8,
  92. NV_PRAMDAC_FP_TMDS_CONTROL_WRITE_DISABLE | address);
  93. return NVReadRAMDAC(dev, ramdac, NV_PRAMDAC_FP_TMDS_DATA + dl * 8);
  94. }
  95. static inline void nv_write_tmds(struct drm_device *dev,
  96. int or, int dl, uint8_t address,
  97. uint8_t data)
  98. {
  99. int ramdac = (or & DCB_OUTPUT_C) >> 2;
  100. NVWriteRAMDAC(dev, ramdac, NV_PRAMDAC_FP_TMDS_DATA + dl * 8, data);
  101. NVWriteRAMDAC(dev, ramdac, NV_PRAMDAC_FP_TMDS_CONTROL + dl * 8, address);
  102. }
  103. static inline void NVWriteVgaCrtc(struct drm_device *dev,
  104. int head, uint8_t index, uint8_t value)
  105. {
  106. struct nouveau_device *device = nouveau_dev(dev);
  107. nv_wr08(device, NV_PRMCIO_CRX__COLOR + head * NV_PRMCIO_SIZE, index);
  108. nv_wr08(device, NV_PRMCIO_CR__COLOR + head * NV_PRMCIO_SIZE, value);
  109. }
  110. static inline uint8_t NVReadVgaCrtc(struct drm_device *dev,
  111. int head, uint8_t index)
  112. {
  113. struct nouveau_device *device = nouveau_dev(dev);
  114. uint8_t val;
  115. nv_wr08(device, NV_PRMCIO_CRX__COLOR + head * NV_PRMCIO_SIZE, index);
  116. val = nv_rd08(device, NV_PRMCIO_CR__COLOR + head * NV_PRMCIO_SIZE);
  117. return val;
  118. }
  119. /* CR57 and CR58 are a fun pair of regs. CR57 provides an index (0-0xf) for CR58
  120. * I suspect they in fact do nothing, but are merely a way to carry useful
  121. * per-head variables around
  122. *
  123. * Known uses:
  124. * CR57 CR58
  125. * 0x00 index to the appropriate dcb entry (or 7f for inactive)
  126. * 0x02 dcb entry's "or" value (or 00 for inactive)
  127. * 0x03 bit0 set for dual link (LVDS, possibly elsewhere too)
  128. * 0x08 or 0x09 pxclk in MHz
  129. * 0x0f laptop panel info - low nibble for PEXTDEV_BOOT_0 strap
  130. * high nibble for xlat strap value
  131. */
  132. static inline void
  133. NVWriteVgaCrtc5758(struct drm_device *dev, int head, uint8_t index, uint8_t value)
  134. {
  135. NVWriteVgaCrtc(dev, head, NV_CIO_CRE_57, index);
  136. NVWriteVgaCrtc(dev, head, NV_CIO_CRE_58, value);
  137. }
  138. static inline uint8_t NVReadVgaCrtc5758(struct drm_device *dev, int head, uint8_t index)
  139. {
  140. NVWriteVgaCrtc(dev, head, NV_CIO_CRE_57, index);
  141. return NVReadVgaCrtc(dev, head, NV_CIO_CRE_58);
  142. }
  143. static inline uint8_t NVReadPRMVIO(struct drm_device *dev,
  144. int head, uint32_t reg)
  145. {
  146. struct nouveau_device *device = nouveau_dev(dev);
  147. struct nouveau_drm *drm = nouveau_drm(dev);
  148. uint8_t val;
  149. /* Only NV4x have two pvio ranges; other twoHeads cards MUST call
  150. * NVSetOwner for the relevant head to be programmed */
  151. if (head && nv_device(drm->device)->card_type == NV_40)
  152. reg += NV_PRMVIO_SIZE;
  153. val = nv_rd08(device, reg);
  154. return val;
  155. }
  156. static inline void NVWritePRMVIO(struct drm_device *dev,
  157. int head, uint32_t reg, uint8_t value)
  158. {
  159. struct nouveau_device *device = nouveau_dev(dev);
  160. struct nouveau_drm *drm = nouveau_drm(dev);
  161. /* Only NV4x have two pvio ranges; other twoHeads cards MUST call
  162. * NVSetOwner for the relevant head to be programmed */
  163. if (head && nv_device(drm->device)->card_type == NV_40)
  164. reg += NV_PRMVIO_SIZE;
  165. nv_wr08(device, reg, value);
  166. }
  167. static inline void NVSetEnablePalette(struct drm_device *dev, int head, bool enable)
  168. {
  169. struct nouveau_device *device = nouveau_dev(dev);
  170. nv_rd08(device, NV_PRMCIO_INP0__COLOR + head * NV_PRMCIO_SIZE);
  171. nv_wr08(device, NV_PRMCIO_ARX + head * NV_PRMCIO_SIZE, enable ? 0 : 0x20);
  172. }
  173. static inline bool NVGetEnablePalette(struct drm_device *dev, int head)
  174. {
  175. struct nouveau_device *device = nouveau_dev(dev);
  176. nv_rd08(device, NV_PRMCIO_INP0__COLOR + head * NV_PRMCIO_SIZE);
  177. return !(nv_rd08(device, NV_PRMCIO_ARX + head * NV_PRMCIO_SIZE) & 0x20);
  178. }
  179. static inline void NVWriteVgaAttr(struct drm_device *dev,
  180. int head, uint8_t index, uint8_t value)
  181. {
  182. struct nouveau_device *device = nouveau_dev(dev);
  183. if (NVGetEnablePalette(dev, head))
  184. index &= ~0x20;
  185. else
  186. index |= 0x20;
  187. nv_rd08(device, NV_PRMCIO_INP0__COLOR + head * NV_PRMCIO_SIZE);
  188. nv_wr08(device, NV_PRMCIO_ARX + head * NV_PRMCIO_SIZE, index);
  189. nv_wr08(device, NV_PRMCIO_AR__WRITE + head * NV_PRMCIO_SIZE, value);
  190. }
  191. static inline uint8_t NVReadVgaAttr(struct drm_device *dev,
  192. int head, uint8_t index)
  193. {
  194. struct nouveau_device *device = nouveau_dev(dev);
  195. uint8_t val;
  196. if (NVGetEnablePalette(dev, head))
  197. index &= ~0x20;
  198. else
  199. index |= 0x20;
  200. nv_rd08(device, NV_PRMCIO_INP0__COLOR + head * NV_PRMCIO_SIZE);
  201. nv_wr08(device, NV_PRMCIO_ARX + head * NV_PRMCIO_SIZE, index);
  202. val = nv_rd08(device, NV_PRMCIO_AR__READ + head * NV_PRMCIO_SIZE);
  203. return val;
  204. }
  205. static inline void NVVgaSeqReset(struct drm_device *dev, int head, bool start)
  206. {
  207. NVWriteVgaSeq(dev, head, NV_VIO_SR_RESET_INDEX, start ? 0x1 : 0x3);
  208. }
  209. static inline void NVVgaProtect(struct drm_device *dev, int head, bool protect)
  210. {
  211. uint8_t seq1 = NVReadVgaSeq(dev, head, NV_VIO_SR_CLOCK_INDEX);
  212. if (protect) {
  213. NVVgaSeqReset(dev, head, true);
  214. NVWriteVgaSeq(dev, head, NV_VIO_SR_CLOCK_INDEX, seq1 | 0x20);
  215. } else {
  216. /* Reenable sequencer, then turn on screen */
  217. NVWriteVgaSeq(dev, head, NV_VIO_SR_CLOCK_INDEX, seq1 & ~0x20); /* reenable display */
  218. NVVgaSeqReset(dev, head, false);
  219. }
  220. NVSetEnablePalette(dev, head, protect);
  221. }
  222. static inline bool
  223. nv_heads_tied(struct drm_device *dev)
  224. {
  225. struct nouveau_device *device = nouveau_dev(dev);
  226. struct nouveau_drm *drm = nouveau_drm(dev);
  227. if (nv_device(drm->device)->chipset == 0x11)
  228. return !!(nv_rd32(device, NV_PBUS_DEBUG_1) & (1 << 28));
  229. return NVReadVgaCrtc(dev, 0, NV_CIO_CRE_44) & 0x4;
  230. }
  231. /* makes cr0-7 on the specified head read-only */
  232. static inline bool
  233. nv_lock_vga_crtc_base(struct drm_device *dev, int head, bool lock)
  234. {
  235. uint8_t cr11 = NVReadVgaCrtc(dev, head, NV_CIO_CR_VRE_INDEX);
  236. bool waslocked = cr11 & 0x80;
  237. if (lock)
  238. cr11 |= 0x80;
  239. else
  240. cr11 &= ~0x80;
  241. NVWriteVgaCrtc(dev, head, NV_CIO_CR_VRE_INDEX, cr11);
  242. return waslocked;
  243. }
  244. static inline void
  245. nv_lock_vga_crtc_shadow(struct drm_device *dev, int head, int lock)
  246. {
  247. /* shadow lock: connects 0x60?3d? regs to "real" 0x3d? regs
  248. * bit7: unlocks HDT, HBS, HBE, HRS, HRE, HEB
  249. * bit6: seems to have some effect on CR09 (double scan, VBS_9)
  250. * bit5: unlocks HDE
  251. * bit4: unlocks VDE
  252. * bit3: unlocks VDT, OVL, VRS, ?VRE?, VBS, VBE, LSR, EBR
  253. * bit2: same as bit 1 of 0x60?804
  254. * bit0: same as bit 0 of 0x60?804
  255. */
  256. uint8_t cr21 = lock;
  257. if (lock < 0)
  258. /* 0xfa is generic "unlock all" mask */
  259. cr21 = NVReadVgaCrtc(dev, head, NV_CIO_CRE_21) | 0xfa;
  260. NVWriteVgaCrtc(dev, head, NV_CIO_CRE_21, cr21);
  261. }
  262. /* renders the extended crtc regs (cr19+) on all crtcs impervious:
  263. * immutable and unreadable
  264. */
  265. static inline bool
  266. NVLockVgaCrtcs(struct drm_device *dev, bool lock)
  267. {
  268. struct nouveau_drm *drm = nouveau_drm(dev);
  269. bool waslocked = !NVReadVgaCrtc(dev, 0, NV_CIO_SR_LOCK_INDEX);
  270. NVWriteVgaCrtc(dev, 0, NV_CIO_SR_LOCK_INDEX,
  271. lock ? NV_CIO_SR_LOCK_VALUE : NV_CIO_SR_UNLOCK_RW_VALUE);
  272. /* NV11 has independently lockable extended crtcs, except when tied */
  273. if (nv_device(drm->device)->chipset == 0x11 && !nv_heads_tied(dev))
  274. NVWriteVgaCrtc(dev, 1, NV_CIO_SR_LOCK_INDEX,
  275. lock ? NV_CIO_SR_LOCK_VALUE :
  276. NV_CIO_SR_UNLOCK_RW_VALUE);
  277. return waslocked;
  278. }
  279. /* nv04 cursor max dimensions of 32x32 (A1R5G5B5) */
  280. #define NV04_CURSOR_SIZE 32
  281. /* limit nv10 cursors to 64x64 (ARGB8) (we could go to 64x255) */
  282. #define NV10_CURSOR_SIZE 64
  283. static inline int nv_cursor_width(struct drm_device *dev)
  284. {
  285. struct nouveau_drm *drm = nouveau_drm(dev);
  286. return nv_device(drm->device)->card_type >= NV_10 ? NV10_CURSOR_SIZE : NV04_CURSOR_SIZE;
  287. }
  288. static inline void
  289. nv_fix_nv40_hw_cursor(struct drm_device *dev, int head)
  290. {
  291. /* on some nv40 (such as the "true" (in the NV_PFB_BOOT_0 sense) nv40,
  292. * the gf6800gt) a hardware bug requires a write to PRAMDAC_CURSOR_POS
  293. * for changes to the CRTC CURCTL regs to take effect, whether changing
  294. * the pixmap location, or just showing/hiding the cursor
  295. */
  296. uint32_t curpos = NVReadRAMDAC(dev, head, NV_PRAMDAC_CU_START_POS);
  297. NVWriteRAMDAC(dev, head, NV_PRAMDAC_CU_START_POS, curpos);
  298. }
  299. static inline void
  300. nv_set_crtc_base(struct drm_device *dev, int head, uint32_t offset)
  301. {
  302. struct nouveau_drm *drm = nouveau_drm(dev);
  303. NVWriteCRTC(dev, head, NV_PCRTC_START, offset);
  304. if (nv_device(drm->device)->card_type == NV_04) {
  305. /*
  306. * Hilarious, the 24th bit doesn't want to stick to
  307. * PCRTC_START...
  308. */
  309. int cre_heb = NVReadVgaCrtc(dev, head, NV_CIO_CRE_HEB__INDEX);
  310. NVWriteVgaCrtc(dev, head, NV_CIO_CRE_HEB__INDEX,
  311. (cre_heb & ~0x40) | ((offset >> 18) & 0x40));
  312. }
  313. }
  314. static inline void
  315. nv_show_cursor(struct drm_device *dev, int head, bool show)
  316. {
  317. struct nouveau_drm *drm = nouveau_drm(dev);
  318. uint8_t *curctl1 =
  319. &nv04_display(dev)->mode_reg.crtc_reg[head].CRTC[NV_CIO_CRE_HCUR_ADDR1_INDEX];
  320. if (show)
  321. *curctl1 |= MASK(NV_CIO_CRE_HCUR_ADDR1_ENABLE);
  322. else
  323. *curctl1 &= ~MASK(NV_CIO_CRE_HCUR_ADDR1_ENABLE);
  324. NVWriteVgaCrtc(dev, head, NV_CIO_CRE_HCUR_ADDR1_INDEX, *curctl1);
  325. if (nv_device(drm->device)->card_type == NV_40)
  326. nv_fix_nv40_hw_cursor(dev, head);
  327. }
  328. static inline uint32_t
  329. nv_pitch_align(struct drm_device *dev, uint32_t width, int bpp)
  330. {
  331. struct nouveau_drm *drm = nouveau_drm(dev);
  332. int mask;
  333. if (bpp == 15)
  334. bpp = 16;
  335. if (bpp == 24)
  336. bpp = 8;
  337. /* Alignment requirements taken from the Haiku driver */
  338. if (nv_device(drm->device)->card_type == NV_04)
  339. mask = 128 / bpp - 1;
  340. else
  341. mask = 512 / bpp - 1;
  342. return (width + mask) & ~mask;
  343. }
  344. #endif /* __NOUVEAU_HW_H__ */