18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2006 Dave Airlie
38c2ecf20Sopenharmony_ci * Copyright 2007 Maarten Maathuis
48c2ecf20Sopenharmony_ci * Copyright 2007-2009 Stuart Bennett
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
78c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
88c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
98c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
108c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
118c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
148c2ecf20Sopenharmony_ci * all copies or substantial portions of the Software.
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
178c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
188c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
198c2ecf20Sopenharmony_ci * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
208c2ecf20Sopenharmony_ci * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
218c2ecf20Sopenharmony_ci * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
228c2ecf20Sopenharmony_ci * SOFTWARE.
238c2ecf20Sopenharmony_ci */
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include "nouveau_drv.h"
268c2ecf20Sopenharmony_ci#include "hw.h"
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#include <subdev/bios/pll.h>
298c2ecf20Sopenharmony_ci#include <nvif/timer.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define CHIPSET_NFORCE 0x01a0
328c2ecf20Sopenharmony_ci#define CHIPSET_NFORCE2 0x01f0
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/*
358c2ecf20Sopenharmony_ci * misc hw access wrappers/control functions
368c2ecf20Sopenharmony_ci */
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_civoid
398c2ecf20Sopenharmony_ciNVWriteVgaSeq(struct drm_device *dev, int head, uint8_t index, uint8_t value)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	NVWritePRMVIO(dev, head, NV_PRMVIO_SRX, index);
428c2ecf20Sopenharmony_ci	NVWritePRMVIO(dev, head, NV_PRMVIO_SR, value);
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ciuint8_t
468c2ecf20Sopenharmony_ciNVReadVgaSeq(struct drm_device *dev, int head, uint8_t index)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	NVWritePRMVIO(dev, head, NV_PRMVIO_SRX, index);
498c2ecf20Sopenharmony_ci	return NVReadPRMVIO(dev, head, NV_PRMVIO_SR);
508c2ecf20Sopenharmony_ci}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_civoid
538c2ecf20Sopenharmony_ciNVWriteVgaGr(struct drm_device *dev, int head, uint8_t index, uint8_t value)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	NVWritePRMVIO(dev, head, NV_PRMVIO_GRX, index);
568c2ecf20Sopenharmony_ci	NVWritePRMVIO(dev, head, NV_PRMVIO_GX, value);
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ciuint8_t
608c2ecf20Sopenharmony_ciNVReadVgaGr(struct drm_device *dev, int head, uint8_t index)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	NVWritePRMVIO(dev, head, NV_PRMVIO_GRX, index);
638c2ecf20Sopenharmony_ci	return NVReadPRMVIO(dev, head, NV_PRMVIO_GX);
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci/* CR44 takes values 0 (head A), 3 (head B) and 4 (heads tied)
678c2ecf20Sopenharmony_ci * it affects only the 8 bit vga io regs, which we access using mmio at
688c2ecf20Sopenharmony_ci * 0xc{0,2}3c*, 0x60{1,3}3*, and 0x68{1,3}3d*
698c2ecf20Sopenharmony_ci * in general, the set value of cr44 does not matter: reg access works as
708c2ecf20Sopenharmony_ci * expected and values can be set for the appropriate head by using a 0x2000
718c2ecf20Sopenharmony_ci * offset as required
728c2ecf20Sopenharmony_ci * however:
738c2ecf20Sopenharmony_ci * a) pre nv40, the head B range of PRMVIO regs at 0xc23c* was not exposed and
748c2ecf20Sopenharmony_ci *    cr44 must be set to 0 or 3 for accessing values on the correct head
758c2ecf20Sopenharmony_ci *    through the common 0xc03c* addresses
768c2ecf20Sopenharmony_ci * b) in tied mode (4) head B is programmed to the values set on head A, and
778c2ecf20Sopenharmony_ci *    access using the head B addresses can have strange results, ergo we leave
788c2ecf20Sopenharmony_ci *    tied mode in init once we know to what cr44 should be restored on exit
798c2ecf20Sopenharmony_ci *
808c2ecf20Sopenharmony_ci * the owner parameter is slightly abused:
818c2ecf20Sopenharmony_ci * 0 and 1 are treated as head values and so the set value is (owner * 3)
828c2ecf20Sopenharmony_ci * other values are treated as literal values to set
838c2ecf20Sopenharmony_ci */
848c2ecf20Sopenharmony_civoid
858c2ecf20Sopenharmony_ciNVSetOwner(struct drm_device *dev, int owner)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	struct nouveau_drm *drm = nouveau_drm(dev);
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	if (owner == 1)
908c2ecf20Sopenharmony_ci		owner *= 3;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	if (drm->client.device.info.chipset == 0x11) {
938c2ecf20Sopenharmony_ci		/* This might seem stupid, but the blob does it and
948c2ecf20Sopenharmony_ci		 * omitting it often locks the system up.
958c2ecf20Sopenharmony_ci		 */
968c2ecf20Sopenharmony_ci		NVReadVgaCrtc(dev, 0, NV_CIO_SR_LOCK_INDEX);
978c2ecf20Sopenharmony_ci		NVReadVgaCrtc(dev, 1, NV_CIO_SR_LOCK_INDEX);
988c2ecf20Sopenharmony_ci	}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	/* CR44 is always changed on CRTC0 */
1018c2ecf20Sopenharmony_ci	NVWriteVgaCrtc(dev, 0, NV_CIO_CRE_44, owner);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	if (drm->client.device.info.chipset == 0x11) {	/* set me harder */
1048c2ecf20Sopenharmony_ci		NVWriteVgaCrtc(dev, 0, NV_CIO_CRE_2E, owner);
1058c2ecf20Sopenharmony_ci		NVWriteVgaCrtc(dev, 0, NV_CIO_CRE_2E, owner);
1068c2ecf20Sopenharmony_ci	}
1078c2ecf20Sopenharmony_ci}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_civoid
1108c2ecf20Sopenharmony_ciNVBlankScreen(struct drm_device *dev, int head, bool blank)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	unsigned char seq1;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	if (nv_two_heads(dev))
1158c2ecf20Sopenharmony_ci		NVSetOwner(dev, head);
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	seq1 = NVReadVgaSeq(dev, head, NV_VIO_SR_CLOCK_INDEX);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	NVVgaSeqReset(dev, head, true);
1208c2ecf20Sopenharmony_ci	if (blank)
1218c2ecf20Sopenharmony_ci		NVWriteVgaSeq(dev, head, NV_VIO_SR_CLOCK_INDEX, seq1 | 0x20);
1228c2ecf20Sopenharmony_ci	else
1238c2ecf20Sopenharmony_ci		NVWriteVgaSeq(dev, head, NV_VIO_SR_CLOCK_INDEX, seq1 & ~0x20);
1248c2ecf20Sopenharmony_ci	NVVgaSeqReset(dev, head, false);
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci/*
1288c2ecf20Sopenharmony_ci * PLL getting
1298c2ecf20Sopenharmony_ci */
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic void
1328c2ecf20Sopenharmony_cinouveau_hw_decode_pll(struct drm_device *dev, uint32_t reg1, uint32_t pll1,
1338c2ecf20Sopenharmony_ci		      uint32_t pll2, struct nvkm_pll_vals *pllvals)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	struct nouveau_drm *drm = nouveau_drm(dev);
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	/* to force parsing as single stage (i.e. nv40 vplls) pass pll2 as 0 */
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	/* log2P is & 0x7 as never more than 7, and nv30/35 only uses 3 bits */
1408c2ecf20Sopenharmony_ci	pllvals->log2P = (pll1 >> 16) & 0x7;
1418c2ecf20Sopenharmony_ci	pllvals->N2 = pllvals->M2 = 1;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	if (reg1 <= 0x405c) {
1448c2ecf20Sopenharmony_ci		pllvals->NM1 = pll2 & 0xffff;
1458c2ecf20Sopenharmony_ci		/* single stage NVPLL and VPLLs use 1 << 8, MPLL uses 1 << 12 */
1468c2ecf20Sopenharmony_ci		if (!(pll1 & 0x1100))
1478c2ecf20Sopenharmony_ci			pllvals->NM2 = pll2 >> 16;
1488c2ecf20Sopenharmony_ci	} else {
1498c2ecf20Sopenharmony_ci		pllvals->NM1 = pll1 & 0xffff;
1508c2ecf20Sopenharmony_ci		if (nv_two_reg_pll(dev) && pll2 & NV31_RAMDAC_ENABLE_VCO2)
1518c2ecf20Sopenharmony_ci			pllvals->NM2 = pll2 & 0xffff;
1528c2ecf20Sopenharmony_ci		else if (drm->client.device.info.chipset == 0x30 || drm->client.device.info.chipset == 0x35) {
1538c2ecf20Sopenharmony_ci			pllvals->M1 &= 0xf; /* only 4 bits */
1548c2ecf20Sopenharmony_ci			if (pll1 & NV30_RAMDAC_ENABLE_VCO2) {
1558c2ecf20Sopenharmony_ci				pllvals->M2 = (pll1 >> 4) & 0x7;
1568c2ecf20Sopenharmony_ci				pllvals->N2 = ((pll1 >> 21) & 0x18) |
1578c2ecf20Sopenharmony_ci					      ((pll1 >> 19) & 0x7);
1588c2ecf20Sopenharmony_ci			}
1598c2ecf20Sopenharmony_ci		}
1608c2ecf20Sopenharmony_ci	}
1618c2ecf20Sopenharmony_ci}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ciint
1648c2ecf20Sopenharmony_cinouveau_hw_get_pllvals(struct drm_device *dev, enum nvbios_pll_type plltype,
1658c2ecf20Sopenharmony_ci		       struct nvkm_pll_vals *pllvals)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci	struct nouveau_drm *drm = nouveau_drm(dev);
1688c2ecf20Sopenharmony_ci	struct nvif_object *device = &drm->client.device.object;
1698c2ecf20Sopenharmony_ci	struct nvkm_bios *bios = nvxx_bios(&drm->client.device);
1708c2ecf20Sopenharmony_ci	uint32_t reg1, pll1, pll2 = 0;
1718c2ecf20Sopenharmony_ci	struct nvbios_pll pll_lim;
1728c2ecf20Sopenharmony_ci	int ret;
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	ret = nvbios_pll_parse(bios, plltype, &pll_lim);
1758c2ecf20Sopenharmony_ci	if (ret || !(reg1 = pll_lim.reg))
1768c2ecf20Sopenharmony_ci		return -ENOENT;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	pll1 = nvif_rd32(device, reg1);
1798c2ecf20Sopenharmony_ci	if (reg1 <= 0x405c)
1808c2ecf20Sopenharmony_ci		pll2 = nvif_rd32(device, reg1 + 4);
1818c2ecf20Sopenharmony_ci	else if (nv_two_reg_pll(dev)) {
1828c2ecf20Sopenharmony_ci		uint32_t reg2 = reg1 + (reg1 == NV_RAMDAC_VPLL2 ? 0x5c : 0x70);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci		pll2 = nvif_rd32(device, reg2);
1858c2ecf20Sopenharmony_ci	}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CELSIUS && reg1 >= NV_PRAMDAC_VPLL_COEFF) {
1888c2ecf20Sopenharmony_ci		uint32_t ramdac580 = NVReadRAMDAC(dev, 0, NV_PRAMDAC_580);
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci		/* check whether vpll has been forced into single stage mode */
1918c2ecf20Sopenharmony_ci		if (reg1 == NV_PRAMDAC_VPLL_COEFF) {
1928c2ecf20Sopenharmony_ci			if (ramdac580 & NV_RAMDAC_580_VPLL1_ACTIVE)
1938c2ecf20Sopenharmony_ci				pll2 = 0;
1948c2ecf20Sopenharmony_ci		} else
1958c2ecf20Sopenharmony_ci			if (ramdac580 & NV_RAMDAC_580_VPLL2_ACTIVE)
1968c2ecf20Sopenharmony_ci				pll2 = 0;
1978c2ecf20Sopenharmony_ci	}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	nouveau_hw_decode_pll(dev, reg1, pll1, pll2, pllvals);
2008c2ecf20Sopenharmony_ci	pllvals->refclk = pll_lim.refclk;
2018c2ecf20Sopenharmony_ci	return 0;
2028c2ecf20Sopenharmony_ci}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ciint
2058c2ecf20Sopenharmony_cinouveau_hw_pllvals_to_clk(struct nvkm_pll_vals *pv)
2068c2ecf20Sopenharmony_ci{
2078c2ecf20Sopenharmony_ci	/* Avoid divide by zero if called at an inappropriate time */
2088c2ecf20Sopenharmony_ci	if (!pv->M1 || !pv->M2)
2098c2ecf20Sopenharmony_ci		return 0;
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	return pv->N1 * pv->N2 * pv->refclk / (pv->M1 * pv->M2) >> pv->log2P;
2128c2ecf20Sopenharmony_ci}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ciint
2158c2ecf20Sopenharmony_cinouveau_hw_get_clock(struct drm_device *dev, enum nvbios_pll_type plltype)
2168c2ecf20Sopenharmony_ci{
2178c2ecf20Sopenharmony_ci	struct nvkm_pll_vals pllvals;
2188c2ecf20Sopenharmony_ci	int ret;
2198c2ecf20Sopenharmony_ci	int domain;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	domain = pci_domain_nr(dev->pdev->bus);
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	if (plltype == PLL_MEMORY &&
2248c2ecf20Sopenharmony_ci	    (dev->pdev->device & 0x0ff0) == CHIPSET_NFORCE) {
2258c2ecf20Sopenharmony_ci		uint32_t mpllP;
2268c2ecf20Sopenharmony_ci		pci_read_config_dword(pci_get_domain_bus_and_slot(domain, 0, 3),
2278c2ecf20Sopenharmony_ci				      0x6c, &mpllP);
2288c2ecf20Sopenharmony_ci		mpllP = (mpllP >> 8) & 0xf;
2298c2ecf20Sopenharmony_ci		if (!mpllP)
2308c2ecf20Sopenharmony_ci			mpllP = 4;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci		return 400000 / mpllP;
2338c2ecf20Sopenharmony_ci	} else
2348c2ecf20Sopenharmony_ci	if (plltype == PLL_MEMORY &&
2358c2ecf20Sopenharmony_ci	    (dev->pdev->device & 0xff0) == CHIPSET_NFORCE2) {
2368c2ecf20Sopenharmony_ci		uint32_t clock;
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci		pci_read_config_dword(pci_get_domain_bus_and_slot(domain, 0, 5),
2398c2ecf20Sopenharmony_ci				      0x4c, &clock);
2408c2ecf20Sopenharmony_ci		return clock / 1000;
2418c2ecf20Sopenharmony_ci	}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	ret = nouveau_hw_get_pllvals(dev, plltype, &pllvals);
2448c2ecf20Sopenharmony_ci	if (ret)
2458c2ecf20Sopenharmony_ci		return ret;
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	return nouveau_hw_pllvals_to_clk(&pllvals);
2488c2ecf20Sopenharmony_ci}
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_cistatic void
2518c2ecf20Sopenharmony_cinouveau_hw_fix_bad_vpll(struct drm_device *dev, int head)
2528c2ecf20Sopenharmony_ci{
2538c2ecf20Sopenharmony_ci	/* the vpll on an unused head can come up with a random value, way
2548c2ecf20Sopenharmony_ci	 * beyond the pll limits.  for some reason this causes the chip to
2558c2ecf20Sopenharmony_ci	 * lock up when reading the dac palette regs, so set a valid pll here
2568c2ecf20Sopenharmony_ci	 * when such a condition detected.  only seen on nv11 to date
2578c2ecf20Sopenharmony_ci	 */
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	struct nouveau_drm *drm = nouveau_drm(dev);
2608c2ecf20Sopenharmony_ci	struct nvif_device *device = &drm->client.device;
2618c2ecf20Sopenharmony_ci	struct nvkm_clk *clk = nvxx_clk(device);
2628c2ecf20Sopenharmony_ci	struct nvkm_bios *bios = nvxx_bios(device);
2638c2ecf20Sopenharmony_ci	struct nvbios_pll pll_lim;
2648c2ecf20Sopenharmony_ci	struct nvkm_pll_vals pv;
2658c2ecf20Sopenharmony_ci	enum nvbios_pll_type pll = head ? PLL_VPLL1 : PLL_VPLL0;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	if (nvbios_pll_parse(bios, pll, &pll_lim))
2688c2ecf20Sopenharmony_ci		return;
2698c2ecf20Sopenharmony_ci	nouveau_hw_get_pllvals(dev, pll, &pv);
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	if (pv.M1 >= pll_lim.vco1.min_m && pv.M1 <= pll_lim.vco1.max_m &&
2728c2ecf20Sopenharmony_ci	    pv.N1 >= pll_lim.vco1.min_n && pv.N1 <= pll_lim.vco1.max_n &&
2738c2ecf20Sopenharmony_ci	    pv.log2P <= pll_lim.max_p)
2748c2ecf20Sopenharmony_ci		return;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	NV_WARN(drm, "VPLL %d outwith limits, attempting to fix\n", head + 1);
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	/* set lowest clock within static limits */
2798c2ecf20Sopenharmony_ci	pv.M1 = pll_lim.vco1.max_m;
2808c2ecf20Sopenharmony_ci	pv.N1 = pll_lim.vco1.min_n;
2818c2ecf20Sopenharmony_ci	pv.log2P = pll_lim.max_p_usable;
2828c2ecf20Sopenharmony_ci	clk->pll_prog(clk, pll_lim.reg, &pv);
2838c2ecf20Sopenharmony_ci}
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci/*
2868c2ecf20Sopenharmony_ci * vga font save/restore
2878c2ecf20Sopenharmony_ci */
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_cistatic void nouveau_vga_font_io(struct drm_device *dev,
2908c2ecf20Sopenharmony_ci				void __iomem *iovram,
2918c2ecf20Sopenharmony_ci				bool save, unsigned plane)
2928c2ecf20Sopenharmony_ci{
2938c2ecf20Sopenharmony_ci	unsigned i;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	NVWriteVgaSeq(dev, 0, NV_VIO_SR_PLANE_MASK_INDEX, 1 << plane);
2968c2ecf20Sopenharmony_ci	NVWriteVgaGr(dev, 0, NV_VIO_GX_READ_MAP_INDEX, plane);
2978c2ecf20Sopenharmony_ci	for (i = 0; i < 16384; i++) {
2988c2ecf20Sopenharmony_ci		if (save) {
2998c2ecf20Sopenharmony_ci			nv04_display(dev)->saved_vga_font[plane][i] =
3008c2ecf20Sopenharmony_ci					ioread32_native(iovram + i * 4);
3018c2ecf20Sopenharmony_ci		} else {
3028c2ecf20Sopenharmony_ci			iowrite32_native(nv04_display(dev)->saved_vga_font[plane][i],
3038c2ecf20Sopenharmony_ci							iovram + i * 4);
3048c2ecf20Sopenharmony_ci		}
3058c2ecf20Sopenharmony_ci	}
3068c2ecf20Sopenharmony_ci}
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_civoid
3098c2ecf20Sopenharmony_cinouveau_hw_save_vga_fonts(struct drm_device *dev, bool save)
3108c2ecf20Sopenharmony_ci{
3118c2ecf20Sopenharmony_ci	struct nouveau_drm *drm = nouveau_drm(dev);
3128c2ecf20Sopenharmony_ci	uint8_t misc, gr4, gr5, gr6, seq2, seq4;
3138c2ecf20Sopenharmony_ci	bool graphicsmode;
3148c2ecf20Sopenharmony_ci	unsigned plane;
3158c2ecf20Sopenharmony_ci	void __iomem *iovram;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	if (nv_two_heads(dev))
3188c2ecf20Sopenharmony_ci		NVSetOwner(dev, 0);
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	NVSetEnablePalette(dev, 0, true);
3218c2ecf20Sopenharmony_ci	graphicsmode = NVReadVgaAttr(dev, 0, NV_CIO_AR_MODE_INDEX) & 1;
3228c2ecf20Sopenharmony_ci	NVSetEnablePalette(dev, 0, false);
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	if (graphicsmode) /* graphics mode => framebuffer => no need to save */
3258c2ecf20Sopenharmony_ci		return;
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	NV_INFO(drm, "%sing VGA fonts\n", save ? "Sav" : "Restor");
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	/* map first 64KiB of VRAM, holds VGA fonts etc */
3308c2ecf20Sopenharmony_ci	iovram = ioremap(pci_resource_start(dev->pdev, 1), 65536);
3318c2ecf20Sopenharmony_ci	if (!iovram) {
3328c2ecf20Sopenharmony_ci		NV_ERROR(drm, "Failed to map VRAM, "
3338c2ecf20Sopenharmony_ci					"cannot save/restore VGA fonts.\n");
3348c2ecf20Sopenharmony_ci		return;
3358c2ecf20Sopenharmony_ci	}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	if (nv_two_heads(dev))
3388c2ecf20Sopenharmony_ci		NVBlankScreen(dev, 1, true);
3398c2ecf20Sopenharmony_ci	NVBlankScreen(dev, 0, true);
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	/* save control regs */
3428c2ecf20Sopenharmony_ci	misc = NVReadPRMVIO(dev, 0, NV_PRMVIO_MISC__READ);
3438c2ecf20Sopenharmony_ci	seq2 = NVReadVgaSeq(dev, 0, NV_VIO_SR_PLANE_MASK_INDEX);
3448c2ecf20Sopenharmony_ci	seq4 = NVReadVgaSeq(dev, 0, NV_VIO_SR_MEM_MODE_INDEX);
3458c2ecf20Sopenharmony_ci	gr4 = NVReadVgaGr(dev, 0, NV_VIO_GX_READ_MAP_INDEX);
3468c2ecf20Sopenharmony_ci	gr5 = NVReadVgaGr(dev, 0, NV_VIO_GX_MODE_INDEX);
3478c2ecf20Sopenharmony_ci	gr6 = NVReadVgaGr(dev, 0, NV_VIO_GX_MISC_INDEX);
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	NVWritePRMVIO(dev, 0, NV_PRMVIO_MISC__WRITE, 0x67);
3508c2ecf20Sopenharmony_ci	NVWriteVgaSeq(dev, 0, NV_VIO_SR_MEM_MODE_INDEX, 0x6);
3518c2ecf20Sopenharmony_ci	NVWriteVgaGr(dev, 0, NV_VIO_GX_MODE_INDEX, 0x0);
3528c2ecf20Sopenharmony_ci	NVWriteVgaGr(dev, 0, NV_VIO_GX_MISC_INDEX, 0x5);
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	/* store font in planes 0..3 */
3558c2ecf20Sopenharmony_ci	for (plane = 0; plane < 4; plane++)
3568c2ecf20Sopenharmony_ci		nouveau_vga_font_io(dev, iovram, save, plane);
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	/* restore control regs */
3598c2ecf20Sopenharmony_ci	NVWritePRMVIO(dev, 0, NV_PRMVIO_MISC__WRITE, misc);
3608c2ecf20Sopenharmony_ci	NVWriteVgaGr(dev, 0, NV_VIO_GX_READ_MAP_INDEX, gr4);
3618c2ecf20Sopenharmony_ci	NVWriteVgaGr(dev, 0, NV_VIO_GX_MODE_INDEX, gr5);
3628c2ecf20Sopenharmony_ci	NVWriteVgaGr(dev, 0, NV_VIO_GX_MISC_INDEX, gr6);
3638c2ecf20Sopenharmony_ci	NVWriteVgaSeq(dev, 0, NV_VIO_SR_PLANE_MASK_INDEX, seq2);
3648c2ecf20Sopenharmony_ci	NVWriteVgaSeq(dev, 0, NV_VIO_SR_MEM_MODE_INDEX, seq4);
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	if (nv_two_heads(dev))
3678c2ecf20Sopenharmony_ci		NVBlankScreen(dev, 1, false);
3688c2ecf20Sopenharmony_ci	NVBlankScreen(dev, 0, false);
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	iounmap(iovram);
3718c2ecf20Sopenharmony_ci}
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci/*
3748c2ecf20Sopenharmony_ci * mode state save/load
3758c2ecf20Sopenharmony_ci */
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_cistatic void
3788c2ecf20Sopenharmony_cird_cio_state(struct drm_device *dev, int head,
3798c2ecf20Sopenharmony_ci	     struct nv04_crtc_reg *crtcstate, int index)
3808c2ecf20Sopenharmony_ci{
3818c2ecf20Sopenharmony_ci	crtcstate->CRTC[index] = NVReadVgaCrtc(dev, head, index);
3828c2ecf20Sopenharmony_ci}
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_cistatic void
3858c2ecf20Sopenharmony_ciwr_cio_state(struct drm_device *dev, int head,
3868c2ecf20Sopenharmony_ci	     struct nv04_crtc_reg *crtcstate, int index)
3878c2ecf20Sopenharmony_ci{
3888c2ecf20Sopenharmony_ci	NVWriteVgaCrtc(dev, head, index, crtcstate->CRTC[index]);
3898c2ecf20Sopenharmony_ci}
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_cistatic void
3928c2ecf20Sopenharmony_cinv_save_state_ramdac(struct drm_device *dev, int head,
3938c2ecf20Sopenharmony_ci		     struct nv04_mode_state *state)
3948c2ecf20Sopenharmony_ci{
3958c2ecf20Sopenharmony_ci	struct nouveau_drm *drm = nouveau_drm(dev);
3968c2ecf20Sopenharmony_ci	struct nv04_crtc_reg *regp = &state->crtc_reg[head];
3978c2ecf20Sopenharmony_ci	int i;
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_CELSIUS)
4008c2ecf20Sopenharmony_ci		regp->nv10_cursync = NVReadRAMDAC(dev, head, NV_RAMDAC_NV10_CURSYNC);
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	nouveau_hw_get_pllvals(dev, head ? PLL_VPLL1 : PLL_VPLL0, &regp->pllvals);
4038c2ecf20Sopenharmony_ci	state->pllsel = NVReadRAMDAC(dev, 0, NV_PRAMDAC_PLL_COEFF_SELECT);
4048c2ecf20Sopenharmony_ci	if (nv_two_heads(dev))
4058c2ecf20Sopenharmony_ci		state->sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK);
4068c2ecf20Sopenharmony_ci	if (drm->client.device.info.chipset == 0x11)
4078c2ecf20Sopenharmony_ci		regp->dither = NVReadRAMDAC(dev, head, NV_RAMDAC_DITHER_NV11);
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	regp->ramdac_gen_ctrl = NVReadRAMDAC(dev, head, NV_PRAMDAC_GENERAL_CONTROL);
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	if (nv_gf4_disp_arch(dev))
4128c2ecf20Sopenharmony_ci		regp->ramdac_630 = NVReadRAMDAC(dev, head, NV_PRAMDAC_630);
4138c2ecf20Sopenharmony_ci	if (drm->client.device.info.chipset >= 0x30)
4148c2ecf20Sopenharmony_ci		regp->ramdac_634 = NVReadRAMDAC(dev, head, NV_PRAMDAC_634);
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	regp->tv_setup = NVReadRAMDAC(dev, head, NV_PRAMDAC_TV_SETUP);
4178c2ecf20Sopenharmony_ci	regp->tv_vtotal = NVReadRAMDAC(dev, head, NV_PRAMDAC_TV_VTOTAL);
4188c2ecf20Sopenharmony_ci	regp->tv_vskew = NVReadRAMDAC(dev, head, NV_PRAMDAC_TV_VSKEW);
4198c2ecf20Sopenharmony_ci	regp->tv_vsync_delay = NVReadRAMDAC(dev, head, NV_PRAMDAC_TV_VSYNC_DELAY);
4208c2ecf20Sopenharmony_ci	regp->tv_htotal = NVReadRAMDAC(dev, head, NV_PRAMDAC_TV_HTOTAL);
4218c2ecf20Sopenharmony_ci	regp->tv_hskew = NVReadRAMDAC(dev, head, NV_PRAMDAC_TV_HSKEW);
4228c2ecf20Sopenharmony_ci	regp->tv_hsync_delay = NVReadRAMDAC(dev, head, NV_PRAMDAC_TV_HSYNC_DELAY);
4238c2ecf20Sopenharmony_ci	regp->tv_hsync_delay2 = NVReadRAMDAC(dev, head, NV_PRAMDAC_TV_HSYNC_DELAY2);
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	for (i = 0; i < 7; i++) {
4268c2ecf20Sopenharmony_ci		uint32_t ramdac_reg = NV_PRAMDAC_FP_VDISPLAY_END + (i * 4);
4278c2ecf20Sopenharmony_ci		regp->fp_vert_regs[i] = NVReadRAMDAC(dev, head, ramdac_reg);
4288c2ecf20Sopenharmony_ci		regp->fp_horiz_regs[i] = NVReadRAMDAC(dev, head, ramdac_reg + 0x20);
4298c2ecf20Sopenharmony_ci	}
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	if (nv_gf4_disp_arch(dev)) {
4328c2ecf20Sopenharmony_ci		regp->dither = NVReadRAMDAC(dev, head, NV_RAMDAC_FP_DITHER);
4338c2ecf20Sopenharmony_ci		for (i = 0; i < 3; i++) {
4348c2ecf20Sopenharmony_ci			regp->dither_regs[i] = NVReadRAMDAC(dev, head, NV_PRAMDAC_850 + i * 4);
4358c2ecf20Sopenharmony_ci			regp->dither_regs[i + 3] = NVReadRAMDAC(dev, head, NV_PRAMDAC_85C + i * 4);
4368c2ecf20Sopenharmony_ci		}
4378c2ecf20Sopenharmony_ci	}
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	regp->fp_control = NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_TG_CONTROL);
4408c2ecf20Sopenharmony_ci	regp->fp_debug_0 = NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_DEBUG_0);
4418c2ecf20Sopenharmony_ci	if (!nv_gf4_disp_arch(dev) && head == 0) {
4428c2ecf20Sopenharmony_ci		/* early chips don't allow access to PRAMDAC_TMDS_* without
4438c2ecf20Sopenharmony_ci		 * the head A FPCLK on (nv11 even locks up) */
4448c2ecf20Sopenharmony_ci		NVWriteRAMDAC(dev, 0, NV_PRAMDAC_FP_DEBUG_0, regp->fp_debug_0 &
4458c2ecf20Sopenharmony_ci			      ~NV_PRAMDAC_FP_DEBUG_0_PWRDOWN_FPCLK);
4468c2ecf20Sopenharmony_ci	}
4478c2ecf20Sopenharmony_ci	regp->fp_debug_1 = NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_DEBUG_1);
4488c2ecf20Sopenharmony_ci	regp->fp_debug_2 = NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_DEBUG_2);
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	regp->fp_margin_color = NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_MARGIN_COLOR);
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	if (nv_gf4_disp_arch(dev))
4538c2ecf20Sopenharmony_ci		regp->ramdac_8c0 = NVReadRAMDAC(dev, head, NV_PRAMDAC_8C0);
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CURIE) {
4568c2ecf20Sopenharmony_ci		regp->ramdac_a20 = NVReadRAMDAC(dev, head, NV_PRAMDAC_A20);
4578c2ecf20Sopenharmony_ci		regp->ramdac_a24 = NVReadRAMDAC(dev, head, NV_PRAMDAC_A24);
4588c2ecf20Sopenharmony_ci		regp->ramdac_a34 = NVReadRAMDAC(dev, head, NV_PRAMDAC_A34);
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci		for (i = 0; i < 38; i++)
4618c2ecf20Sopenharmony_ci			regp->ctv_regs[i] = NVReadRAMDAC(dev, head,
4628c2ecf20Sopenharmony_ci							 NV_PRAMDAC_CTV + 4*i);
4638c2ecf20Sopenharmony_ci	}
4648c2ecf20Sopenharmony_ci}
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_cistatic void
4678c2ecf20Sopenharmony_cinv_load_state_ramdac(struct drm_device *dev, int head,
4688c2ecf20Sopenharmony_ci		     struct nv04_mode_state *state)
4698c2ecf20Sopenharmony_ci{
4708c2ecf20Sopenharmony_ci	struct nouveau_drm *drm = nouveau_drm(dev);
4718c2ecf20Sopenharmony_ci	struct nvkm_clk *clk = nvxx_clk(&drm->client.device);
4728c2ecf20Sopenharmony_ci	struct nv04_crtc_reg *regp = &state->crtc_reg[head];
4738c2ecf20Sopenharmony_ci	uint32_t pllreg = head ? NV_RAMDAC_VPLL2 : NV_PRAMDAC_VPLL_COEFF;
4748c2ecf20Sopenharmony_ci	int i;
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_CELSIUS)
4778c2ecf20Sopenharmony_ci		NVWriteRAMDAC(dev, head, NV_RAMDAC_NV10_CURSYNC, regp->nv10_cursync);
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	clk->pll_prog(clk, pllreg, &regp->pllvals);
4808c2ecf20Sopenharmony_ci	NVWriteRAMDAC(dev, 0, NV_PRAMDAC_PLL_COEFF_SELECT, state->pllsel);
4818c2ecf20Sopenharmony_ci	if (nv_two_heads(dev))
4828c2ecf20Sopenharmony_ci		NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, state->sel_clk);
4838c2ecf20Sopenharmony_ci	if (drm->client.device.info.chipset == 0x11)
4848c2ecf20Sopenharmony_ci		NVWriteRAMDAC(dev, head, NV_RAMDAC_DITHER_NV11, regp->dither);
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_GENERAL_CONTROL, regp->ramdac_gen_ctrl);
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	if (nv_gf4_disp_arch(dev))
4898c2ecf20Sopenharmony_ci		NVWriteRAMDAC(dev, head, NV_PRAMDAC_630, regp->ramdac_630);
4908c2ecf20Sopenharmony_ci	if (drm->client.device.info.chipset >= 0x30)
4918c2ecf20Sopenharmony_ci		NVWriteRAMDAC(dev, head, NV_PRAMDAC_634, regp->ramdac_634);
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_TV_SETUP, regp->tv_setup);
4948c2ecf20Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_TV_VTOTAL, regp->tv_vtotal);
4958c2ecf20Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_TV_VSKEW, regp->tv_vskew);
4968c2ecf20Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_TV_VSYNC_DELAY, regp->tv_vsync_delay);
4978c2ecf20Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_TV_HTOTAL, regp->tv_htotal);
4988c2ecf20Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_TV_HSKEW, regp->tv_hskew);
4998c2ecf20Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_TV_HSYNC_DELAY, regp->tv_hsync_delay);
5008c2ecf20Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_TV_HSYNC_DELAY2, regp->tv_hsync_delay2);
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	for (i = 0; i < 7; i++) {
5038c2ecf20Sopenharmony_ci		uint32_t ramdac_reg = NV_PRAMDAC_FP_VDISPLAY_END + (i * 4);
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci		NVWriteRAMDAC(dev, head, ramdac_reg, regp->fp_vert_regs[i]);
5068c2ecf20Sopenharmony_ci		NVWriteRAMDAC(dev, head, ramdac_reg + 0x20, regp->fp_horiz_regs[i]);
5078c2ecf20Sopenharmony_ci	}
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	if (nv_gf4_disp_arch(dev)) {
5108c2ecf20Sopenharmony_ci		NVWriteRAMDAC(dev, head, NV_RAMDAC_FP_DITHER, regp->dither);
5118c2ecf20Sopenharmony_ci		for (i = 0; i < 3; i++) {
5128c2ecf20Sopenharmony_ci			NVWriteRAMDAC(dev, head, NV_PRAMDAC_850 + i * 4, regp->dither_regs[i]);
5138c2ecf20Sopenharmony_ci			NVWriteRAMDAC(dev, head, NV_PRAMDAC_85C + i * 4, regp->dither_regs[i + 3]);
5148c2ecf20Sopenharmony_ci		}
5158c2ecf20Sopenharmony_ci	}
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_TG_CONTROL, regp->fp_control);
5188c2ecf20Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_DEBUG_0, regp->fp_debug_0);
5198c2ecf20Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_DEBUG_1, regp->fp_debug_1);
5208c2ecf20Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_DEBUG_2, regp->fp_debug_2);
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci	NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_MARGIN_COLOR, regp->fp_margin_color);
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	if (nv_gf4_disp_arch(dev))
5258c2ecf20Sopenharmony_ci		NVWriteRAMDAC(dev, head, NV_PRAMDAC_8C0, regp->ramdac_8c0);
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CURIE) {
5288c2ecf20Sopenharmony_ci		NVWriteRAMDAC(dev, head, NV_PRAMDAC_A20, regp->ramdac_a20);
5298c2ecf20Sopenharmony_ci		NVWriteRAMDAC(dev, head, NV_PRAMDAC_A24, regp->ramdac_a24);
5308c2ecf20Sopenharmony_ci		NVWriteRAMDAC(dev, head, NV_PRAMDAC_A34, regp->ramdac_a34);
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci		for (i = 0; i < 38; i++)
5338c2ecf20Sopenharmony_ci			NVWriteRAMDAC(dev, head,
5348c2ecf20Sopenharmony_ci				      NV_PRAMDAC_CTV + 4*i, regp->ctv_regs[i]);
5358c2ecf20Sopenharmony_ci	}
5368c2ecf20Sopenharmony_ci}
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_cistatic void
5398c2ecf20Sopenharmony_cinv_save_state_vga(struct drm_device *dev, int head,
5408c2ecf20Sopenharmony_ci		  struct nv04_mode_state *state)
5418c2ecf20Sopenharmony_ci{
5428c2ecf20Sopenharmony_ci	struct nv04_crtc_reg *regp = &state->crtc_reg[head];
5438c2ecf20Sopenharmony_ci	int i;
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	regp->MiscOutReg = NVReadPRMVIO(dev, head, NV_PRMVIO_MISC__READ);
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	for (i = 0; i < 25; i++)
5488c2ecf20Sopenharmony_ci		rd_cio_state(dev, head, regp, i);
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci	NVSetEnablePalette(dev, head, true);
5518c2ecf20Sopenharmony_ci	for (i = 0; i < 21; i++)
5528c2ecf20Sopenharmony_ci		regp->Attribute[i] = NVReadVgaAttr(dev, head, i);
5538c2ecf20Sopenharmony_ci	NVSetEnablePalette(dev, head, false);
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	for (i = 0; i < 9; i++)
5568c2ecf20Sopenharmony_ci		regp->Graphics[i] = NVReadVgaGr(dev, head, i);
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	for (i = 0; i < 5; i++)
5598c2ecf20Sopenharmony_ci		regp->Sequencer[i] = NVReadVgaSeq(dev, head, i);
5608c2ecf20Sopenharmony_ci}
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_cistatic void
5638c2ecf20Sopenharmony_cinv_load_state_vga(struct drm_device *dev, int head,
5648c2ecf20Sopenharmony_ci		  struct nv04_mode_state *state)
5658c2ecf20Sopenharmony_ci{
5668c2ecf20Sopenharmony_ci	struct nv04_crtc_reg *regp = &state->crtc_reg[head];
5678c2ecf20Sopenharmony_ci	int i;
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci	NVWritePRMVIO(dev, head, NV_PRMVIO_MISC__WRITE, regp->MiscOutReg);
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	for (i = 0; i < 5; i++)
5728c2ecf20Sopenharmony_ci		NVWriteVgaSeq(dev, head, i, regp->Sequencer[i]);
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci	nv_lock_vga_crtc_base(dev, head, false);
5758c2ecf20Sopenharmony_ci	for (i = 0; i < 25; i++)
5768c2ecf20Sopenharmony_ci		wr_cio_state(dev, head, regp, i);
5778c2ecf20Sopenharmony_ci	nv_lock_vga_crtc_base(dev, head, true);
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci	for (i = 0; i < 9; i++)
5808c2ecf20Sopenharmony_ci		NVWriteVgaGr(dev, head, i, regp->Graphics[i]);
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	NVSetEnablePalette(dev, head, true);
5838c2ecf20Sopenharmony_ci	for (i = 0; i < 21; i++)
5848c2ecf20Sopenharmony_ci		NVWriteVgaAttr(dev, head, i, regp->Attribute[i]);
5858c2ecf20Sopenharmony_ci	NVSetEnablePalette(dev, head, false);
5868c2ecf20Sopenharmony_ci}
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_cistatic void
5898c2ecf20Sopenharmony_cinv_save_state_ext(struct drm_device *dev, int head,
5908c2ecf20Sopenharmony_ci		  struct nv04_mode_state *state)
5918c2ecf20Sopenharmony_ci{
5928c2ecf20Sopenharmony_ci	struct nouveau_drm *drm = nouveau_drm(dev);
5938c2ecf20Sopenharmony_ci	struct nv04_crtc_reg *regp = &state->crtc_reg[head];
5948c2ecf20Sopenharmony_ci	int i;
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_LCD__INDEX);
5978c2ecf20Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_RPC0_INDEX);
5988c2ecf20Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_RPC1_INDEX);
5998c2ecf20Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_LSR_INDEX);
6008c2ecf20Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_PIXEL_INDEX);
6018c2ecf20Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_HEB__INDEX);
6028c2ecf20Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_ENH_INDEX);
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_FF_INDEX);
6058c2ecf20Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_FFLWM__INDEX);
6068c2ecf20Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_21);
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_KELVIN)
6098c2ecf20Sopenharmony_ci		rd_cio_state(dev, head, regp, NV_CIO_CRE_47);
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_RANKINE)
6128c2ecf20Sopenharmony_ci		rd_cio_state(dev, head, regp, 0x9f);
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_49);
6158c2ecf20Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_HCUR_ADDR0_INDEX);
6168c2ecf20Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_HCUR_ADDR1_INDEX);
6178c2ecf20Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_HCUR_ADDR2_INDEX);
6188c2ecf20Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_ILACE__INDEX);
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_CELSIUS) {
6218c2ecf20Sopenharmony_ci		regp->crtc_830 = NVReadCRTC(dev, head, NV_PCRTC_830);
6228c2ecf20Sopenharmony_ci		regp->crtc_834 = NVReadCRTC(dev, head, NV_PCRTC_834);
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci		if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_RANKINE)
6258c2ecf20Sopenharmony_ci			regp->gpio_ext = NVReadCRTC(dev, head, NV_PCRTC_GPIO_EXT);
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci		if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CURIE)
6288c2ecf20Sopenharmony_ci			regp->crtc_850 = NVReadCRTC(dev, head, NV_PCRTC_850);
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci		if (nv_two_heads(dev))
6318c2ecf20Sopenharmony_ci			regp->crtc_eng_ctrl = NVReadCRTC(dev, head, NV_PCRTC_ENGINE_CTRL);
6328c2ecf20Sopenharmony_ci		regp->cursor_cfg = NVReadCRTC(dev, head, NV_PCRTC_CURSOR_CONFIG);
6338c2ecf20Sopenharmony_ci	}
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	regp->crtc_cfg = NVReadCRTC(dev, head, NV_PCRTC_CONFIG);
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_SCRATCH3__INDEX);
6388c2ecf20Sopenharmony_ci	rd_cio_state(dev, head, regp, NV_CIO_CRE_SCRATCH4__INDEX);
6398c2ecf20Sopenharmony_ci	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_CELSIUS) {
6408c2ecf20Sopenharmony_ci		rd_cio_state(dev, head, regp, NV_CIO_CRE_EBR_INDEX);
6418c2ecf20Sopenharmony_ci		rd_cio_state(dev, head, regp, NV_CIO_CRE_CSB);
6428c2ecf20Sopenharmony_ci		rd_cio_state(dev, head, regp, NV_CIO_CRE_4B);
6438c2ecf20Sopenharmony_ci		rd_cio_state(dev, head, regp, NV_CIO_CRE_TVOUT_LATENCY);
6448c2ecf20Sopenharmony_ci	}
6458c2ecf20Sopenharmony_ci	/* NV11 and NV20 don't have this, they stop at 0x52. */
6468c2ecf20Sopenharmony_ci	if (nv_gf4_disp_arch(dev)) {
6478c2ecf20Sopenharmony_ci		rd_cio_state(dev, head, regp, NV_CIO_CRE_42);
6488c2ecf20Sopenharmony_ci		rd_cio_state(dev, head, regp, NV_CIO_CRE_53);
6498c2ecf20Sopenharmony_ci		rd_cio_state(dev, head, regp, NV_CIO_CRE_54);
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci		for (i = 0; i < 0x10; i++)
6528c2ecf20Sopenharmony_ci			regp->CR58[i] = NVReadVgaCrtc5758(dev, head, i);
6538c2ecf20Sopenharmony_ci		rd_cio_state(dev, head, regp, NV_CIO_CRE_59);
6548c2ecf20Sopenharmony_ci		rd_cio_state(dev, head, regp, NV_CIO_CRE_5B);
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci		rd_cio_state(dev, head, regp, NV_CIO_CRE_85);
6578c2ecf20Sopenharmony_ci		rd_cio_state(dev, head, regp, NV_CIO_CRE_86);
6588c2ecf20Sopenharmony_ci	}
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci	regp->fb_start = NVReadCRTC(dev, head, NV_PCRTC_START);
6618c2ecf20Sopenharmony_ci}
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_cistatic void
6648c2ecf20Sopenharmony_cinv_load_state_ext(struct drm_device *dev, int head,
6658c2ecf20Sopenharmony_ci		  struct nv04_mode_state *state)
6668c2ecf20Sopenharmony_ci{
6678c2ecf20Sopenharmony_ci	struct nouveau_drm *drm = nouveau_drm(dev);
6688c2ecf20Sopenharmony_ci	struct nvif_object *device = &drm->client.device.object;
6698c2ecf20Sopenharmony_ci	struct nv04_crtc_reg *regp = &state->crtc_reg[head];
6708c2ecf20Sopenharmony_ci	uint32_t reg900;
6718c2ecf20Sopenharmony_ci	int i;
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_CELSIUS) {
6748c2ecf20Sopenharmony_ci		if (nv_two_heads(dev))
6758c2ecf20Sopenharmony_ci			/* setting ENGINE_CTRL (EC) *must* come before
6768c2ecf20Sopenharmony_ci			 * CIO_CRE_LCD, as writing CRE_LCD sets bits 16 & 17 in
6778c2ecf20Sopenharmony_ci			 * EC that should not be overwritten by writing stale EC
6788c2ecf20Sopenharmony_ci			 */
6798c2ecf20Sopenharmony_ci			NVWriteCRTC(dev, head, NV_PCRTC_ENGINE_CTRL, regp->crtc_eng_ctrl);
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci		nvif_wr32(device, NV_PVIDEO_STOP, 1);
6828c2ecf20Sopenharmony_ci		nvif_wr32(device, NV_PVIDEO_INTR_EN, 0);
6838c2ecf20Sopenharmony_ci		nvif_wr32(device, NV_PVIDEO_OFFSET_BUFF(0), 0);
6848c2ecf20Sopenharmony_ci		nvif_wr32(device, NV_PVIDEO_OFFSET_BUFF(1), 0);
6858c2ecf20Sopenharmony_ci		nvif_wr32(device, NV_PVIDEO_LIMIT(0), drm->client.device.info.ram_size - 1);
6868c2ecf20Sopenharmony_ci		nvif_wr32(device, NV_PVIDEO_LIMIT(1), drm->client.device.info.ram_size - 1);
6878c2ecf20Sopenharmony_ci		nvif_wr32(device, NV_PVIDEO_UVPLANE_LIMIT(0), drm->client.device.info.ram_size - 1);
6888c2ecf20Sopenharmony_ci		nvif_wr32(device, NV_PVIDEO_UVPLANE_LIMIT(1), drm->client.device.info.ram_size - 1);
6898c2ecf20Sopenharmony_ci		nvif_wr32(device, NV_PBUS_POWERCTRL_2, 0);
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci		NVWriteCRTC(dev, head, NV_PCRTC_CURSOR_CONFIG, regp->cursor_cfg);
6928c2ecf20Sopenharmony_ci		NVWriteCRTC(dev, head, NV_PCRTC_830, regp->crtc_830);
6938c2ecf20Sopenharmony_ci		NVWriteCRTC(dev, head, NV_PCRTC_834, regp->crtc_834);
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci		if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_RANKINE)
6968c2ecf20Sopenharmony_ci			NVWriteCRTC(dev, head, NV_PCRTC_GPIO_EXT, regp->gpio_ext);
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci		if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CURIE) {
6998c2ecf20Sopenharmony_ci			NVWriteCRTC(dev, head, NV_PCRTC_850, regp->crtc_850);
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci			reg900 = NVReadRAMDAC(dev, head, NV_PRAMDAC_900);
7028c2ecf20Sopenharmony_ci			if (regp->crtc_cfg == NV10_PCRTC_CONFIG_START_ADDRESS_HSYNC)
7038c2ecf20Sopenharmony_ci				NVWriteRAMDAC(dev, head, NV_PRAMDAC_900, reg900 | 0x10000);
7048c2ecf20Sopenharmony_ci			else
7058c2ecf20Sopenharmony_ci				NVWriteRAMDAC(dev, head, NV_PRAMDAC_900, reg900 & ~0x10000);
7068c2ecf20Sopenharmony_ci		}
7078c2ecf20Sopenharmony_ci	}
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	NVWriteCRTC(dev, head, NV_PCRTC_CONFIG, regp->crtc_cfg);
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_RPC0_INDEX);
7128c2ecf20Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_RPC1_INDEX);
7138c2ecf20Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_LSR_INDEX);
7148c2ecf20Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_PIXEL_INDEX);
7158c2ecf20Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_LCD__INDEX);
7168c2ecf20Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_HEB__INDEX);
7178c2ecf20Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_ENH_INDEX);
7188c2ecf20Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_FF_INDEX);
7198c2ecf20Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_FFLWM__INDEX);
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_KELVIN)
7228c2ecf20Sopenharmony_ci		wr_cio_state(dev, head, regp, NV_CIO_CRE_47);
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_RANKINE)
7258c2ecf20Sopenharmony_ci		wr_cio_state(dev, head, regp, 0x9f);
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_49);
7288c2ecf20Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_HCUR_ADDR0_INDEX);
7298c2ecf20Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_HCUR_ADDR1_INDEX);
7308c2ecf20Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_HCUR_ADDR2_INDEX);
7318c2ecf20Sopenharmony_ci	if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CURIE)
7328c2ecf20Sopenharmony_ci		nv_fix_nv40_hw_cursor(dev, head);
7338c2ecf20Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_ILACE__INDEX);
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_SCRATCH3__INDEX);
7368c2ecf20Sopenharmony_ci	wr_cio_state(dev, head, regp, NV_CIO_CRE_SCRATCH4__INDEX);
7378c2ecf20Sopenharmony_ci	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_CELSIUS) {
7388c2ecf20Sopenharmony_ci		wr_cio_state(dev, head, regp, NV_CIO_CRE_EBR_INDEX);
7398c2ecf20Sopenharmony_ci		wr_cio_state(dev, head, regp, NV_CIO_CRE_CSB);
7408c2ecf20Sopenharmony_ci		wr_cio_state(dev, head, regp, NV_CIO_CRE_4B);
7418c2ecf20Sopenharmony_ci		wr_cio_state(dev, head, regp, NV_CIO_CRE_TVOUT_LATENCY);
7428c2ecf20Sopenharmony_ci	}
7438c2ecf20Sopenharmony_ci	/* NV11 and NV20 stop at 0x52. */
7448c2ecf20Sopenharmony_ci	if (nv_gf4_disp_arch(dev)) {
7458c2ecf20Sopenharmony_ci		if (drm->client.device.info.family < NV_DEVICE_INFO_V0_KELVIN) {
7468c2ecf20Sopenharmony_ci			/* Not waiting for vertical retrace before modifying
7478c2ecf20Sopenharmony_ci			   CRE_53/CRE_54 causes lockups. */
7488c2ecf20Sopenharmony_ci			nvif_msec(&drm->client.device, 650,
7498c2ecf20Sopenharmony_ci				if ( (nvif_rd32(device, NV_PRMCIO_INP0__COLOR) & 8))
7508c2ecf20Sopenharmony_ci					break;
7518c2ecf20Sopenharmony_ci			);
7528c2ecf20Sopenharmony_ci			nvif_msec(&drm->client.device, 650,
7538c2ecf20Sopenharmony_ci				if (!(nvif_rd32(device, NV_PRMCIO_INP0__COLOR) & 8))
7548c2ecf20Sopenharmony_ci					break;
7558c2ecf20Sopenharmony_ci			);
7568c2ecf20Sopenharmony_ci		}
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci		wr_cio_state(dev, head, regp, NV_CIO_CRE_42);
7598c2ecf20Sopenharmony_ci		wr_cio_state(dev, head, regp, NV_CIO_CRE_53);
7608c2ecf20Sopenharmony_ci		wr_cio_state(dev, head, regp, NV_CIO_CRE_54);
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_ci		for (i = 0; i < 0x10; i++)
7638c2ecf20Sopenharmony_ci			NVWriteVgaCrtc5758(dev, head, i, regp->CR58[i]);
7648c2ecf20Sopenharmony_ci		wr_cio_state(dev, head, regp, NV_CIO_CRE_59);
7658c2ecf20Sopenharmony_ci		wr_cio_state(dev, head, regp, NV_CIO_CRE_5B);
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci		wr_cio_state(dev, head, regp, NV_CIO_CRE_85);
7688c2ecf20Sopenharmony_ci		wr_cio_state(dev, head, regp, NV_CIO_CRE_86);
7698c2ecf20Sopenharmony_ci	}
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci	NVWriteCRTC(dev, head, NV_PCRTC_START, regp->fb_start);
7728c2ecf20Sopenharmony_ci}
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_cistatic void
7758c2ecf20Sopenharmony_cinv_save_state_palette(struct drm_device *dev, int head,
7768c2ecf20Sopenharmony_ci		      struct nv04_mode_state *state)
7778c2ecf20Sopenharmony_ci{
7788c2ecf20Sopenharmony_ci	struct nvif_object *device = &nouveau_drm(dev)->client.device.object;
7798c2ecf20Sopenharmony_ci	int head_offset = head * NV_PRMDIO_SIZE, i;
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_ci	nvif_wr08(device, NV_PRMDIO_PIXEL_MASK + head_offset,
7828c2ecf20Sopenharmony_ci				NV_PRMDIO_PIXEL_MASK_MASK);
7838c2ecf20Sopenharmony_ci	nvif_wr08(device, NV_PRMDIO_READ_MODE_ADDRESS + head_offset, 0x0);
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci	for (i = 0; i < 768; i++) {
7868c2ecf20Sopenharmony_ci		state->crtc_reg[head].DAC[i] = nvif_rd08(device,
7878c2ecf20Sopenharmony_ci				NV_PRMDIO_PALETTE_DATA + head_offset);
7888c2ecf20Sopenharmony_ci	}
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci	NVSetEnablePalette(dev, head, false);
7918c2ecf20Sopenharmony_ci}
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_civoid
7948c2ecf20Sopenharmony_cinouveau_hw_load_state_palette(struct drm_device *dev, int head,
7958c2ecf20Sopenharmony_ci			      struct nv04_mode_state *state)
7968c2ecf20Sopenharmony_ci{
7978c2ecf20Sopenharmony_ci	struct nvif_object *device = &nouveau_drm(dev)->client.device.object;
7988c2ecf20Sopenharmony_ci	int head_offset = head * NV_PRMDIO_SIZE, i;
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	nvif_wr08(device, NV_PRMDIO_PIXEL_MASK + head_offset,
8018c2ecf20Sopenharmony_ci				NV_PRMDIO_PIXEL_MASK_MASK);
8028c2ecf20Sopenharmony_ci	nvif_wr08(device, NV_PRMDIO_WRITE_MODE_ADDRESS + head_offset, 0x0);
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci	for (i = 0; i < 768; i++) {
8058c2ecf20Sopenharmony_ci		nvif_wr08(device, NV_PRMDIO_PALETTE_DATA + head_offset,
8068c2ecf20Sopenharmony_ci				state->crtc_reg[head].DAC[i]);
8078c2ecf20Sopenharmony_ci	}
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_ci	NVSetEnablePalette(dev, head, false);
8108c2ecf20Sopenharmony_ci}
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_civoid nouveau_hw_save_state(struct drm_device *dev, int head,
8138c2ecf20Sopenharmony_ci			   struct nv04_mode_state *state)
8148c2ecf20Sopenharmony_ci{
8158c2ecf20Sopenharmony_ci	struct nouveau_drm *drm = nouveau_drm(dev);
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci	if (drm->client.device.info.chipset == 0x11)
8188c2ecf20Sopenharmony_ci		/* NB: no attempt is made to restore the bad pll later on */
8198c2ecf20Sopenharmony_ci		nouveau_hw_fix_bad_vpll(dev, head);
8208c2ecf20Sopenharmony_ci	nv_save_state_ramdac(dev, head, state);
8218c2ecf20Sopenharmony_ci	nv_save_state_vga(dev, head, state);
8228c2ecf20Sopenharmony_ci	nv_save_state_palette(dev, head, state);
8238c2ecf20Sopenharmony_ci	nv_save_state_ext(dev, head, state);
8248c2ecf20Sopenharmony_ci}
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_civoid nouveau_hw_load_state(struct drm_device *dev, int head,
8278c2ecf20Sopenharmony_ci			   struct nv04_mode_state *state)
8288c2ecf20Sopenharmony_ci{
8298c2ecf20Sopenharmony_ci	NVVgaProtect(dev, head, true);
8308c2ecf20Sopenharmony_ci	nv_load_state_ramdac(dev, head, state);
8318c2ecf20Sopenharmony_ci	nv_load_state_ext(dev, head, state);
8328c2ecf20Sopenharmony_ci	nouveau_hw_load_state_palette(dev, head, state);
8338c2ecf20Sopenharmony_ci	nv_load_state_vga(dev, head, state);
8348c2ecf20Sopenharmony_ci	NVVgaProtect(dev, head, false);
8358c2ecf20Sopenharmony_ci}
836