2 * Copyright 2008 Stuart Bennett
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
23 #ifndef __NOUVEAU_HW_H__
24 #define __NOUVEAU_HW_H__
29 #include <subdev/bios/pll.h>
31 #define MASK(field) ( \
32 (0xffffffff >> (31 - ((1 ? field) - (0 ? field)))) << (0 ? field))
34 #define XLATE(src, srclowbit, outfield) ( \
35 (((src) >> (srclowbit)) << (0 ? outfield)) & MASK(outfield))
37 void NVWriteVgaSeq(struct drm_device
*, int head
, uint8_t index
, uint8_t value
);
38 uint8_t NVReadVgaSeq(struct drm_device
*, int head
, uint8_t index
);
39 void NVWriteVgaGr(struct drm_device
*, int head
, uint8_t index
, uint8_t value
);
40 uint8_t NVReadVgaGr(struct drm_device
*, int head
, uint8_t index
);
41 void NVSetOwner(struct drm_device
*, int owner
);
42 void NVBlankScreen(struct drm_device
*, int head
, bool blank
);
43 int nouveau_hw_get_pllvals(struct drm_device
*, enum nvbios_pll_type plltype
,
44 struct nvkm_pll_vals
*pllvals
);
45 int nouveau_hw_pllvals_to_clk(struct nvkm_pll_vals
*pllvals
);
46 int nouveau_hw_get_clock(struct drm_device
*, enum nvbios_pll_type plltype
);
47 void nouveau_hw_save_vga_fonts(struct drm_device
*, bool save
);
48 void nouveau_hw_save_state(struct drm_device
*, int head
,
49 struct nv04_mode_state
*state
);
50 void nouveau_hw_load_state(struct drm_device
*, int head
,
51 struct nv04_mode_state
*state
);
52 void nouveau_hw_load_state_palette(struct drm_device
*, int head
,
53 struct nv04_mode_state
*state
);
56 extern void nouveau_calc_arb(struct drm_device
*, int vclk
, int bpp
,
57 int *burst
, int *lwm
);
59 static inline uint32_t NVReadCRTC(struct drm_device
*dev
,
60 int head
, uint32_t reg
)
62 struct nvif_object
*device
= &nouveau_drm(dev
)->client
.device
.object
;
65 reg
+= NV_PCRTC0_SIZE
;
66 val
= nvif_rd32(device
, reg
);
70 static inline void NVWriteCRTC(struct drm_device
*dev
,
71 int head
, uint32_t reg
, uint32_t val
)
73 struct nvif_object
*device
= &nouveau_drm(dev
)->client
.device
.object
;
75 reg
+= NV_PCRTC0_SIZE
;
76 nvif_wr32(device
, reg
, val
);
79 static inline uint32_t NVReadRAMDAC(struct drm_device
*dev
,
80 int head
, uint32_t reg
)
82 struct nvif_object
*device
= &nouveau_drm(dev
)->client
.device
.object
;
85 reg
+= NV_PRAMDAC0_SIZE
;
86 val
= nvif_rd32(device
, reg
);
90 static inline void NVWriteRAMDAC(struct drm_device
*dev
,
91 int head
, uint32_t reg
, uint32_t val
)
93 struct nvif_object
*device
= &nouveau_drm(dev
)->client
.device
.object
;
95 reg
+= NV_PRAMDAC0_SIZE
;
96 nvif_wr32(device
, reg
, val
);
99 static inline uint8_t nv_read_tmds(struct drm_device
*dev
,
100 int or, int dl
, uint8_t address
)
102 int ramdac
= (or & DCB_OUTPUT_C
) >> 2;
104 NVWriteRAMDAC(dev
, ramdac
, NV_PRAMDAC_FP_TMDS_CONTROL
+ dl
* 8,
105 NV_PRAMDAC_FP_TMDS_CONTROL_WRITE_DISABLE
| address
);
106 return NVReadRAMDAC(dev
, ramdac
, NV_PRAMDAC_FP_TMDS_DATA
+ dl
* 8);
109 static inline void nv_write_tmds(struct drm_device
*dev
,
110 int or, int dl
, uint8_t address
,
113 int ramdac
= (or & DCB_OUTPUT_C
) >> 2;
115 NVWriteRAMDAC(dev
, ramdac
, NV_PRAMDAC_FP_TMDS_DATA
+ dl
* 8, data
);
116 NVWriteRAMDAC(dev
, ramdac
, NV_PRAMDAC_FP_TMDS_CONTROL
+ dl
* 8, address
);
119 static inline void NVWriteVgaCrtc(struct drm_device
*dev
,
120 int head
, uint8_t index
, uint8_t value
)
122 struct nvif_object
*device
= &nouveau_drm(dev
)->client
.device
.object
;
123 nvif_wr08(device
, NV_PRMCIO_CRX__COLOR
+ head
* NV_PRMCIO_SIZE
, index
);
124 nvif_wr08(device
, NV_PRMCIO_CR__COLOR
+ head
* NV_PRMCIO_SIZE
, value
);
127 static inline uint8_t NVReadVgaCrtc(struct drm_device
*dev
,
128 int head
, uint8_t index
)
130 struct nvif_object
*device
= &nouveau_drm(dev
)->client
.device
.object
;
132 nvif_wr08(device
, NV_PRMCIO_CRX__COLOR
+ head
* NV_PRMCIO_SIZE
, index
);
133 val
= nvif_rd08(device
, NV_PRMCIO_CR__COLOR
+ head
* NV_PRMCIO_SIZE
);
137 /* CR57 and CR58 are a fun pair of regs. CR57 provides an index (0-0xf) for CR58
138 * I suspect they in fact do nothing, but are merely a way to carry useful
139 * per-head variables around
143 * 0x00 index to the appropriate dcb entry (or 7f for inactive)
144 * 0x02 dcb entry's "or" value (or 00 for inactive)
145 * 0x03 bit0 set for dual link (LVDS, possibly elsewhere too)
146 * 0x08 or 0x09 pxclk in MHz
147 * 0x0f laptop panel info - low nibble for PEXTDEV_BOOT_0 strap
148 * high nibble for xlat strap value
152 NVWriteVgaCrtc5758(struct drm_device
*dev
, int head
, uint8_t index
, uint8_t value
)
154 NVWriteVgaCrtc(dev
, head
, NV_CIO_CRE_57
, index
);
155 NVWriteVgaCrtc(dev
, head
, NV_CIO_CRE_58
, value
);
158 static inline uint8_t NVReadVgaCrtc5758(struct drm_device
*dev
, int head
, uint8_t index
)
160 NVWriteVgaCrtc(dev
, head
, NV_CIO_CRE_57
, index
);
161 return NVReadVgaCrtc(dev
, head
, NV_CIO_CRE_58
);
164 static inline uint8_t NVReadPRMVIO(struct drm_device
*dev
,
165 int head
, uint32_t reg
)
167 struct nvif_object
*device
= &nouveau_drm(dev
)->client
.device
.object
;
168 struct nouveau_drm
*drm
= nouveau_drm(dev
);
171 /* Only NV4x have two pvio ranges; other twoHeads cards MUST call
172 * NVSetOwner for the relevant head to be programmed */
173 if (head
&& drm
->client
.device
.info
.family
== NV_DEVICE_INFO_V0_CURIE
)
174 reg
+= NV_PRMVIO_SIZE
;
176 val
= nvif_rd08(device
, reg
);
180 static inline void NVWritePRMVIO(struct drm_device
*dev
,
181 int head
, uint32_t reg
, uint8_t value
)
183 struct nvif_object
*device
= &nouveau_drm(dev
)->client
.device
.object
;
184 struct nouveau_drm
*drm
= nouveau_drm(dev
);
186 /* Only NV4x have two pvio ranges; other twoHeads cards MUST call
187 * NVSetOwner for the relevant head to be programmed */
188 if (head
&& drm
->client
.device
.info
.family
== NV_DEVICE_INFO_V0_CURIE
)
189 reg
+= NV_PRMVIO_SIZE
;
191 nvif_wr08(device
, reg
, value
);
194 static inline void NVSetEnablePalette(struct drm_device
*dev
, int head
, bool enable
)
196 struct nvif_object
*device
= &nouveau_drm(dev
)->client
.device
.object
;
197 nvif_rd08(device
, NV_PRMCIO_INP0__COLOR
+ head
* NV_PRMCIO_SIZE
);
198 nvif_wr08(device
, NV_PRMCIO_ARX
+ head
* NV_PRMCIO_SIZE
, enable
? 0 : 0x20);
201 static inline bool NVGetEnablePalette(struct drm_device
*dev
, int head
)
203 struct nvif_object
*device
= &nouveau_drm(dev
)->client
.device
.object
;
204 nvif_rd08(device
, NV_PRMCIO_INP0__COLOR
+ head
* NV_PRMCIO_SIZE
);
205 return !(nvif_rd08(device
, NV_PRMCIO_ARX
+ head
* NV_PRMCIO_SIZE
) & 0x20);
208 static inline void NVWriteVgaAttr(struct drm_device
*dev
,
209 int head
, uint8_t index
, uint8_t value
)
211 struct nvif_object
*device
= &nouveau_drm(dev
)->client
.device
.object
;
212 if (NVGetEnablePalette(dev
, head
))
217 nvif_rd08(device
, NV_PRMCIO_INP0__COLOR
+ head
* NV_PRMCIO_SIZE
);
218 nvif_wr08(device
, NV_PRMCIO_ARX
+ head
* NV_PRMCIO_SIZE
, index
);
219 nvif_wr08(device
, NV_PRMCIO_AR__WRITE
+ head
* NV_PRMCIO_SIZE
, value
);
222 static inline uint8_t NVReadVgaAttr(struct drm_device
*dev
,
223 int head
, uint8_t index
)
225 struct nvif_object
*device
= &nouveau_drm(dev
)->client
.device
.object
;
227 if (NVGetEnablePalette(dev
, head
))
232 nvif_rd08(device
, NV_PRMCIO_INP0__COLOR
+ head
* NV_PRMCIO_SIZE
);
233 nvif_wr08(device
, NV_PRMCIO_ARX
+ head
* NV_PRMCIO_SIZE
, index
);
234 val
= nvif_rd08(device
, NV_PRMCIO_AR__READ
+ head
* NV_PRMCIO_SIZE
);
238 static inline void NVVgaSeqReset(struct drm_device
*dev
, int head
, bool start
)
240 NVWriteVgaSeq(dev
, head
, NV_VIO_SR_RESET_INDEX
, start
? 0x1 : 0x3);
243 static inline void NVVgaProtect(struct drm_device
*dev
, int head
, bool protect
)
245 uint8_t seq1
= NVReadVgaSeq(dev
, head
, NV_VIO_SR_CLOCK_INDEX
);
248 NVVgaSeqReset(dev
, head
, true);
249 NVWriteVgaSeq(dev
, head
, NV_VIO_SR_CLOCK_INDEX
, seq1
| 0x20);
251 /* Reenable sequencer, then turn on screen */
252 NVWriteVgaSeq(dev
, head
, NV_VIO_SR_CLOCK_INDEX
, seq1
& ~0x20); /* reenable display */
253 NVVgaSeqReset(dev
, head
, false);
255 NVSetEnablePalette(dev
, head
, protect
);
259 nv_heads_tied(struct drm_device
*dev
)
261 struct nvif_object
*device
= &nouveau_drm(dev
)->client
.device
.object
;
262 struct nouveau_drm
*drm
= nouveau_drm(dev
);
264 if (drm
->client
.device
.info
.chipset
== 0x11)
265 return !!(nvif_rd32(device
, NV_PBUS_DEBUG_1
) & (1 << 28));
267 return NVReadVgaCrtc(dev
, 0, NV_CIO_CRE_44
) & 0x4;
270 /* makes cr0-7 on the specified head read-only */
272 nv_lock_vga_crtc_base(struct drm_device
*dev
, int head
, bool lock
)
274 uint8_t cr11
= NVReadVgaCrtc(dev
, head
, NV_CIO_CR_VRE_INDEX
);
275 bool waslocked
= cr11
& 0x80;
281 NVWriteVgaCrtc(dev
, head
, NV_CIO_CR_VRE_INDEX
, cr11
);
287 nv_lock_vga_crtc_shadow(struct drm_device
*dev
, int head
, int lock
)
289 /* shadow lock: connects 0x60?3d? regs to "real" 0x3d? regs
290 * bit7: unlocks HDT, HBS, HBE, HRS, HRE, HEB
291 * bit6: seems to have some effect on CR09 (double scan, VBS_9)
294 * bit3: unlocks VDT, OVL, VRS, ?VRE?, VBS, VBE, LSR, EBR
295 * bit2: same as bit 1 of 0x60?804
296 * bit0: same as bit 0 of 0x60?804
302 /* 0xfa is generic "unlock all" mask */
303 cr21
= NVReadVgaCrtc(dev
, head
, NV_CIO_CRE_21
) | 0xfa;
305 NVWriteVgaCrtc(dev
, head
, NV_CIO_CRE_21
, cr21
);
308 /* renders the extended crtc regs (cr19+) on all crtcs impervious:
309 * immutable and unreadable
312 NVLockVgaCrtcs(struct drm_device
*dev
, bool lock
)
314 struct nouveau_drm
*drm
= nouveau_drm(dev
);
315 bool waslocked
= !NVReadVgaCrtc(dev
, 0, NV_CIO_SR_LOCK_INDEX
);
317 NVWriteVgaCrtc(dev
, 0, NV_CIO_SR_LOCK_INDEX
,
318 lock
? NV_CIO_SR_LOCK_VALUE
: NV_CIO_SR_UNLOCK_RW_VALUE
);
319 /* NV11 has independently lockable extended crtcs, except when tied */
320 if (drm
->client
.device
.info
.chipset
== 0x11 && !nv_heads_tied(dev
))
321 NVWriteVgaCrtc(dev
, 1, NV_CIO_SR_LOCK_INDEX
,
322 lock
? NV_CIO_SR_LOCK_VALUE
:
323 NV_CIO_SR_UNLOCK_RW_VALUE
);
328 /* nv04 cursor max dimensions of 32x32 (A1R5G5B5) */
329 #define NV04_CURSOR_SIZE 32
330 /* limit nv10 cursors to 64x64 (ARGB8) (we could go to 64x255) */
331 #define NV10_CURSOR_SIZE 64
333 static inline int nv_cursor_width(struct drm_device
*dev
)
335 struct nouveau_drm
*drm
= nouveau_drm(dev
);
337 return drm
->client
.device
.info
.family
>= NV_DEVICE_INFO_V0_CELSIUS
? NV10_CURSOR_SIZE
: NV04_CURSOR_SIZE
;
341 nv_fix_nv40_hw_cursor(struct drm_device
*dev
, int head
)
343 /* on some nv40 (such as the "true" (in the NV_PFB_BOOT_0 sense) nv40,
344 * the gf6800gt) a hardware bug requires a write to PRAMDAC_CURSOR_POS
345 * for changes to the CRTC CURCTL regs to take effect, whether changing
346 * the pixmap location, or just showing/hiding the cursor
348 uint32_t curpos
= NVReadRAMDAC(dev
, head
, NV_PRAMDAC_CU_START_POS
);
349 NVWriteRAMDAC(dev
, head
, NV_PRAMDAC_CU_START_POS
, curpos
);
353 nv_set_crtc_base(struct drm_device
*dev
, int head
, uint32_t offset
)
355 struct nouveau_drm
*drm
= nouveau_drm(dev
);
357 NVWriteCRTC(dev
, head
, NV_PCRTC_START
, offset
);
359 if (drm
->client
.device
.info
.family
== NV_DEVICE_INFO_V0_TNT
) {
361 * Hilarious, the 24th bit doesn't want to stick to
364 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));
372 nv_show_cursor(struct drm_device
*dev
, int head
, bool show
)
374 struct nouveau_drm
*drm
= nouveau_drm(dev
);
376 &nv04_display(dev
)->mode_reg
.crtc_reg
[head
].CRTC
[NV_CIO_CRE_HCUR_ADDR1_INDEX
];
379 *curctl1
|= MASK(NV_CIO_CRE_HCUR_ADDR1_ENABLE
);
381 *curctl1
&= ~MASK(NV_CIO_CRE_HCUR_ADDR1_ENABLE
);
382 NVWriteVgaCrtc(dev
, head
, NV_CIO_CRE_HCUR_ADDR1_INDEX
, *curctl1
);
384 if (drm
->client
.device
.info
.family
== NV_DEVICE_INFO_V0_CURIE
)
385 nv_fix_nv40_hw_cursor(dev
, head
);
388 static inline uint32_t
389 nv_pitch_align(struct drm_device
*dev
, uint32_t width
, int bpp
)
391 struct nouveau_drm
*drm
= nouveau_drm(dev
);
399 /* Alignment requirements taken from the Haiku driver */
400 if (drm
->client
.device
.info
.family
== NV_DEVICE_INFO_V0_TNT
)
401 mask
= 128 / bpp
- 1;
403 mask
= 512 / bpp
- 1;
405 return (width
+ mask
) & ~mask
;
408 #endif /* __NOUVEAU_HW_H__ */