nouveau_hw.h 14 KB

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