/drivers/gpu/drm/nouveau/dispnv04/hw.h

https://gitlab.com/tbwtiot/kernel_source · C Header · 409 lines · 278 code · 64 blank · 67 comment · 32 complexity · 34613b5cab4ee75b02e0dbeeb730ab6f MD5 · raw file

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