mga_warp.c revision 145132
195584Sanholt/* mga_warp.c -- Matrox G200/G400 WARP engine management -*- linux-c -*-
2139749Simp * Created: Thu Jan 11 21:29:32 2001 by gareth@valinux.com */
3139749Simp/*-
495584Sanholt * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
595584Sanholt * All Rights Reserved.
695584Sanholt *
795584Sanholt * Permission is hereby granted, free of charge, to any person obtaining a
895584Sanholt * copy of this software and associated documentation files (the "Software"),
995584Sanholt * to deal in the Software without restriction, including without limitation
1095584Sanholt * the rights to use, copy, modify, merge, publish, distribute, sublicense,
1195584Sanholt * and/or sell copies of the Software, and to permit persons to whom the
1295584Sanholt * Software is furnished to do so, subject to the following conditions:
1395584Sanholt *
1495584Sanholt * The above copyright notice and this permission notice (including the next
1595584Sanholt * paragraph) shall be included in all copies or substantial portions of the
1695584Sanholt * Software.
1795584Sanholt *
1895584Sanholt * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1995584Sanholt * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2095584Sanholt * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
2195584Sanholt * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
2295584Sanholt * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
2395584Sanholt * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2495584Sanholt * OTHER DEALINGS IN THE SOFTWARE.
2595584Sanholt *
2695584Sanholt * Authors:
2795584Sanholt *    Gareth Hughes <gareth@valinux.com>
2895584Sanholt *
2995584Sanholt * $FreeBSD: head/sys/dev/drm/mga_warp.c 145132 2005-04-16 03:44:47Z anholt $
3095584Sanholt */
3195584Sanholt
3295584Sanholt#include "dev/drm/drmP.h"
33112015Sanholt#include "dev/drm/drm.h"
3495746Sanholt#include "dev/drm/mga_drm.h"
3595584Sanholt#include "dev/drm/mga_drv.h"
3695584Sanholt#include "dev/drm/mga_ucode.h"
3795584Sanholt
38145132Sanholt#define MGA_WARP_CODE_ALIGN		256	/* in bytes */
3995584Sanholt
4095584Sanholt#define WARP_UCODE_SIZE( which )					\
4195584Sanholt	((sizeof(which) / MGA_WARP_CODE_ALIGN + 1) * MGA_WARP_CODE_ALIGN)
4295584Sanholt
4395584Sanholt#define WARP_UCODE_INSTALL( which, where )				\
4495584Sanholtdo {									\
4595584Sanholt	DRM_DEBUG( " pcbase = 0x%08lx  vcbase = %p\n", pcbase, vcbase );\
4695584Sanholt	dev_priv->warp_pipe_phys[where] = pcbase;			\
4795584Sanholt	memcpy( vcbase, which, sizeof(which) );				\
4895584Sanholt	pcbase += WARP_UCODE_SIZE( which );				\
4995584Sanholt	vcbase += WARP_UCODE_SIZE( which );				\
5095584Sanholt} while (0)
5195584Sanholt
52145132Sanholtstatic unsigned int mga_warp_g400_microcode_size(drm_mga_private_t * dev_priv)
5395584Sanholt{
5495584Sanholt	unsigned int size;
5595584Sanholt
56145132Sanholt	size = (WARP_UCODE_SIZE(warp_g400_tgz) +
57145132Sanholt		WARP_UCODE_SIZE(warp_g400_tgza) +
58145132Sanholt		WARP_UCODE_SIZE(warp_g400_tgzaf) +
59145132Sanholt		WARP_UCODE_SIZE(warp_g400_tgzf) +
60145132Sanholt		WARP_UCODE_SIZE(warp_g400_tgzs) +
61145132Sanholt		WARP_UCODE_SIZE(warp_g400_tgzsa) +
62145132Sanholt		WARP_UCODE_SIZE(warp_g400_tgzsaf) +
63145132Sanholt		WARP_UCODE_SIZE(warp_g400_tgzsf) +
64145132Sanholt		WARP_UCODE_SIZE(warp_g400_t2gz) +
65145132Sanholt		WARP_UCODE_SIZE(warp_g400_t2gza) +
66145132Sanholt		WARP_UCODE_SIZE(warp_g400_t2gzaf) +
67145132Sanholt		WARP_UCODE_SIZE(warp_g400_t2gzf) +
68145132Sanholt		WARP_UCODE_SIZE(warp_g400_t2gzs) +
69145132Sanholt		WARP_UCODE_SIZE(warp_g400_t2gzsa) +
70145132Sanholt		WARP_UCODE_SIZE(warp_g400_t2gzsaf) +
71145132Sanholt		WARP_UCODE_SIZE(warp_g400_t2gzsf));
7295584Sanholt
73145132Sanholt	size = PAGE_ALIGN(size);
7495584Sanholt
75145132Sanholt	DRM_DEBUG("G400 ucode size = %d bytes\n", size);
7695584Sanholt	return size;
7795584Sanholt}
7895584Sanholt
79145132Sanholtstatic unsigned int mga_warp_g200_microcode_size(drm_mga_private_t * dev_priv)
8095584Sanholt{
8195584Sanholt	unsigned int size;
8295584Sanholt
83145132Sanholt	size = (WARP_UCODE_SIZE(warp_g200_tgz) +
84145132Sanholt		WARP_UCODE_SIZE(warp_g200_tgza) +
85145132Sanholt		WARP_UCODE_SIZE(warp_g200_tgzaf) +
86145132Sanholt		WARP_UCODE_SIZE(warp_g200_tgzf) +
87145132Sanholt		WARP_UCODE_SIZE(warp_g200_tgzs) +
88145132Sanholt		WARP_UCODE_SIZE(warp_g200_tgzsa) +
89145132Sanholt		WARP_UCODE_SIZE(warp_g200_tgzsaf) +
90145132Sanholt		WARP_UCODE_SIZE(warp_g200_tgzsf));
9195584Sanholt
92145132Sanholt	size = PAGE_ALIGN(size);
9395584Sanholt
94145132Sanholt	DRM_DEBUG("G200 ucode size = %d bytes\n", size);
9595584Sanholt	return size;
9695584Sanholt}
9795584Sanholt
98145132Sanholtstatic int mga_warp_install_g400_microcode(drm_mga_private_t * dev_priv)
9995584Sanholt{
10095584Sanholt	unsigned char *vcbase = dev_priv->warp->handle;
10195584Sanholt	unsigned long pcbase = dev_priv->warp->offset;
10295584Sanholt	unsigned int size;
10395584Sanholt
104145132Sanholt	size = mga_warp_g400_microcode_size(dev_priv);
105145132Sanholt	if (size > dev_priv->warp->size) {
106145132Sanholt		DRM_ERROR("microcode too large! (%u > %lu)\n",
107145132Sanholt			  size, dev_priv->warp->size);
108112015Sanholt		return DRM_ERR(ENOMEM);
10995584Sanholt	}
11095584Sanholt
111145132Sanholt	memset(dev_priv->warp_pipe_phys, 0, sizeof(dev_priv->warp_pipe_phys));
11295584Sanholt
113145132Sanholt	WARP_UCODE_INSTALL(warp_g400_tgz, MGA_WARP_TGZ);
114145132Sanholt	WARP_UCODE_INSTALL(warp_g400_tgzf, MGA_WARP_TGZF);
115145132Sanholt	WARP_UCODE_INSTALL(warp_g400_tgza, MGA_WARP_TGZA);
116145132Sanholt	WARP_UCODE_INSTALL(warp_g400_tgzaf, MGA_WARP_TGZAF);
117145132Sanholt	WARP_UCODE_INSTALL(warp_g400_tgzs, MGA_WARP_TGZS);
118145132Sanholt	WARP_UCODE_INSTALL(warp_g400_tgzsf, MGA_WARP_TGZSF);
119145132Sanholt	WARP_UCODE_INSTALL(warp_g400_tgzsa, MGA_WARP_TGZSA);
120145132Sanholt	WARP_UCODE_INSTALL(warp_g400_tgzsaf, MGA_WARP_TGZSAF);
12195584Sanholt
122145132Sanholt	WARP_UCODE_INSTALL(warp_g400_t2gz, MGA_WARP_T2GZ);
123145132Sanholt	WARP_UCODE_INSTALL(warp_g400_t2gzf, MGA_WARP_T2GZF);
124145132Sanholt	WARP_UCODE_INSTALL(warp_g400_t2gza, MGA_WARP_T2GZA);
125145132Sanholt	WARP_UCODE_INSTALL(warp_g400_t2gzaf, MGA_WARP_T2GZAF);
126145132Sanholt	WARP_UCODE_INSTALL(warp_g400_t2gzs, MGA_WARP_T2GZS);
127145132Sanholt	WARP_UCODE_INSTALL(warp_g400_t2gzsf, MGA_WARP_T2GZSF);
128145132Sanholt	WARP_UCODE_INSTALL(warp_g400_t2gzsa, MGA_WARP_T2GZSA);
129145132Sanholt	WARP_UCODE_INSTALL(warp_g400_t2gzsaf, MGA_WARP_T2GZSAF);
13095584Sanholt
13195584Sanholt	return 0;
13295584Sanholt}
13395584Sanholt
134145132Sanholtstatic int mga_warp_install_g200_microcode(drm_mga_private_t * dev_priv)
13595584Sanholt{
13695584Sanholt	unsigned char *vcbase = dev_priv->warp->handle;
13795584Sanholt	unsigned long pcbase = dev_priv->warp->offset;
13895584Sanholt	unsigned int size;
13995584Sanholt
140145132Sanholt	size = mga_warp_g200_microcode_size(dev_priv);
141145132Sanholt	if (size > dev_priv->warp->size) {
142145132Sanholt		DRM_ERROR("microcode too large! (%u > %lu)\n",
143145132Sanholt			  size, dev_priv->warp->size);
144112015Sanholt		return DRM_ERR(ENOMEM);
14595584Sanholt	}
14695584Sanholt
147145132Sanholt	memset(dev_priv->warp_pipe_phys, 0, sizeof(dev_priv->warp_pipe_phys));
14895584Sanholt
149145132Sanholt	WARP_UCODE_INSTALL(warp_g200_tgz, MGA_WARP_TGZ);
150145132Sanholt	WARP_UCODE_INSTALL(warp_g200_tgzf, MGA_WARP_TGZF);
151145132Sanholt	WARP_UCODE_INSTALL(warp_g200_tgza, MGA_WARP_TGZA);
152145132Sanholt	WARP_UCODE_INSTALL(warp_g200_tgzaf, MGA_WARP_TGZAF);
153145132Sanholt	WARP_UCODE_INSTALL(warp_g200_tgzs, MGA_WARP_TGZS);
154145132Sanholt	WARP_UCODE_INSTALL(warp_g200_tgzsf, MGA_WARP_TGZSF);
155145132Sanholt	WARP_UCODE_INSTALL(warp_g200_tgzsa, MGA_WARP_TGZSA);
156145132Sanholt	WARP_UCODE_INSTALL(warp_g200_tgzsaf, MGA_WARP_TGZSAF);
15795584Sanholt
15895584Sanholt	return 0;
15995584Sanholt}
16095584Sanholt
161145132Sanholtint mga_warp_install_microcode(drm_mga_private_t * dev_priv)
16295584Sanholt{
163145132Sanholt	switch (dev_priv->chipset) {
16495584Sanholt	case MGA_CARD_TYPE_G400:
165145132Sanholt		return mga_warp_install_g400_microcode(dev_priv);
16695584Sanholt	case MGA_CARD_TYPE_G200:
167145132Sanholt		return mga_warp_install_g200_microcode(dev_priv);
16895584Sanholt	default:
169112015Sanholt		return DRM_ERR(EINVAL);
17095584Sanholt	}
17195584Sanholt}
17295584Sanholt
17395584Sanholt#define WMISC_EXPECTED		(MGA_WUCODECACHE_ENABLE | MGA_WMASTER_ENABLE)
17495584Sanholt
175145132Sanholtint mga_warp_init(drm_mga_private_t * dev_priv)
17695584Sanholt{
17795584Sanholt	u32 wmisc;
17895584Sanholt
17995584Sanholt	/* FIXME: Get rid of these damned magic numbers...
18095584Sanholt	 */
181145132Sanholt	switch (dev_priv->chipset) {
18295584Sanholt	case MGA_CARD_TYPE_G400:
183145132Sanholt		MGA_WRITE(MGA_WIADDR2, MGA_WMODE_SUSPEND);
184145132Sanholt		MGA_WRITE(MGA_WGETMSB, 0x00000E00);
185145132Sanholt		MGA_WRITE(MGA_WVRTXSZ, 0x00001807);
186145132Sanholt		MGA_WRITE(MGA_WACCEPTSEQ, 0x18000000);
18795584Sanholt		break;
18895584Sanholt	case MGA_CARD_TYPE_G200:
189145132Sanholt		MGA_WRITE(MGA_WIADDR, MGA_WMODE_SUSPEND);
190145132Sanholt		MGA_WRITE(MGA_WGETMSB, 0x1606);
191145132Sanholt		MGA_WRITE(MGA_WVRTXSZ, 7);
19295584Sanholt		break;
19395584Sanholt	default:
194112015Sanholt		return DRM_ERR(EINVAL);
19595584Sanholt	}
19695584Sanholt
197145132Sanholt	MGA_WRITE(MGA_WMISC, (MGA_WUCODECACHE_ENABLE |
198145132Sanholt			      MGA_WMASTER_ENABLE | MGA_WCACHEFLUSH_ENABLE));
199145132Sanholt	wmisc = MGA_READ(MGA_WMISC);
200145132Sanholt	if (wmisc != WMISC_EXPECTED) {
201145132Sanholt		DRM_ERROR("WARP engine config failed! 0x%x != 0x%x\n",
202145132Sanholt			  wmisc, WMISC_EXPECTED);
203112015Sanholt		return DRM_ERR(EINVAL);
20495584Sanholt	}
20595584Sanholt
20695584Sanholt	return 0;
20795584Sanholt}
208