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