18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
38c2ecf20Sopenharmony_ci * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
68c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
78c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
88c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sub license,
98c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
108c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice (including the
138c2ecf20Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
148c2ecf20Sopenharmony_ci * 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 NON-INFRINGEMENT. IN NO EVENT SHALL
198c2ecf20Sopenharmony_ci * VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
208c2ecf20Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
218c2ecf20Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
228c2ecf20Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
238c2ecf20Sopenharmony_ci */
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include <linux/pci.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#include <drm/drm_device.h>
288c2ecf20Sopenharmony_ci#include <drm/drm_vblank.h>
298c2ecf20Sopenharmony_ci#include <drm/via_drm.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#include "via_drv.h"
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic int via_do_init_map(struct drm_device *dev, drm_via_init_t *init)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	drm_via_private_t *dev_priv = dev->dev_private;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	DRM_DEBUG("\n");
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	dev_priv->sarea = drm_legacy_getsarea(dev);
408c2ecf20Sopenharmony_ci	if (!dev_priv->sarea) {
418c2ecf20Sopenharmony_ci		DRM_ERROR("could not find sarea!\n");
428c2ecf20Sopenharmony_ci		dev->dev_private = (void *)dev_priv;
438c2ecf20Sopenharmony_ci		via_do_cleanup_map(dev);
448c2ecf20Sopenharmony_ci		return -EINVAL;
458c2ecf20Sopenharmony_ci	}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	dev_priv->fb = drm_legacy_findmap(dev, init->fb_offset);
488c2ecf20Sopenharmony_ci	if (!dev_priv->fb) {
498c2ecf20Sopenharmony_ci		DRM_ERROR("could not find framebuffer!\n");
508c2ecf20Sopenharmony_ci		dev->dev_private = (void *)dev_priv;
518c2ecf20Sopenharmony_ci		via_do_cleanup_map(dev);
528c2ecf20Sopenharmony_ci		return -EINVAL;
538c2ecf20Sopenharmony_ci	}
548c2ecf20Sopenharmony_ci	dev_priv->mmio = drm_legacy_findmap(dev, init->mmio_offset);
558c2ecf20Sopenharmony_ci	if (!dev_priv->mmio) {
568c2ecf20Sopenharmony_ci		DRM_ERROR("could not find mmio region!\n");
578c2ecf20Sopenharmony_ci		dev->dev_private = (void *)dev_priv;
588c2ecf20Sopenharmony_ci		via_do_cleanup_map(dev);
598c2ecf20Sopenharmony_ci		return -EINVAL;
608c2ecf20Sopenharmony_ci	}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	dev_priv->sarea_priv =
638c2ecf20Sopenharmony_ci	    (drm_via_sarea_t *) ((u8 *) dev_priv->sarea->handle +
648c2ecf20Sopenharmony_ci				 init->sarea_priv_offset);
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	dev_priv->agpAddr = init->agpAddr;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	via_init_futex(dev_priv);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	via_init_dmablit(dev);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	dev->dev_private = (void *)dev_priv;
738c2ecf20Sopenharmony_ci	return 0;
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ciint via_do_cleanup_map(struct drm_device *dev)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	via_dma_cleanup(dev);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	return 0;
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ciint via_map_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	drm_via_init_t *init = data;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	DRM_DEBUG("\n");
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	switch (init->func) {
908c2ecf20Sopenharmony_ci	case VIA_INIT_MAP:
918c2ecf20Sopenharmony_ci		return via_do_init_map(dev, init);
928c2ecf20Sopenharmony_ci	case VIA_CLEANUP_MAP:
938c2ecf20Sopenharmony_ci		return via_do_cleanup_map(dev);
948c2ecf20Sopenharmony_ci	}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	return -EINVAL;
978c2ecf20Sopenharmony_ci}
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ciint via_driver_load(struct drm_device *dev, unsigned long chipset)
1008c2ecf20Sopenharmony_ci{
1018c2ecf20Sopenharmony_ci	drm_via_private_t *dev_priv;
1028c2ecf20Sopenharmony_ci	int ret = 0;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	dev_priv = kzalloc(sizeof(drm_via_private_t), GFP_KERNEL);
1058c2ecf20Sopenharmony_ci	if (dev_priv == NULL)
1068c2ecf20Sopenharmony_ci		return -ENOMEM;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	idr_init(&dev_priv->object_idr);
1098c2ecf20Sopenharmony_ci	dev->dev_private = (void *)dev_priv;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	dev_priv->chipset = chipset;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	pci_set_master(dev->pdev);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	ret = drm_vblank_init(dev, 1);
1168c2ecf20Sopenharmony_ci	if (ret) {
1178c2ecf20Sopenharmony_ci		kfree(dev_priv);
1188c2ecf20Sopenharmony_ci		return ret;
1198c2ecf20Sopenharmony_ci	}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	return 0;
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_civoid via_driver_unload(struct drm_device *dev)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	drm_via_private_t *dev_priv = dev->dev_private;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	idr_destroy(&dev_priv->object_idr);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	kfree(dev_priv);
1318c2ecf20Sopenharmony_ci}
132