nouveau_hw.h 14 KB

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