xref: /titanic_41/usr/src/uts/common/io/drm/ati_pcigart.c (revision 8793b36b40d14ad0a0fecc97738dc118a928f46c)
1d0538f66Scg149915 /*
2d0538f66Scg149915  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
3d0538f66Scg149915  * Use is subject to license terms.
4d0538f66Scg149915  */
5d0538f66Scg149915 /*
6d0538f66Scg149915  * ati_pcigart.h -- ATI PCI GART support -*- linux-c -*-
7d0538f66Scg149915  * Created: Wed Dec 13 21:52:19 2000 by gareth@valinux.com
8d0538f66Scg149915  */
9d0538f66Scg149915 /*
10d0538f66Scg149915  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
11d0538f66Scg149915  * All Rights Reserved.
12d0538f66Scg149915  *
13d0538f66Scg149915  * Permission is hereby granted, free of charge, to any person obtaining a
14d0538f66Scg149915  * copy of this software and associated documentation files (the "Software"),
15d0538f66Scg149915  * to deal in the Software without restriction, including without limitation
16d0538f66Scg149915  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17d0538f66Scg149915  * and/or sell copies of the Software, and to permit persons to whom the
18d0538f66Scg149915  * Software is furnished to do so, subject to the following conditions:
19d0538f66Scg149915  *
20d0538f66Scg149915  * The above copyright notice and this permission notice (including the next
21d0538f66Scg149915  * paragraph) shall be included in all copies or substantial portions of the
22d0538f66Scg149915  * Software.
23d0538f66Scg149915  *
24d0538f66Scg149915  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25d0538f66Scg149915  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26d0538f66Scg149915  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
27d0538f66Scg149915  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
28d0538f66Scg149915  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
29d0538f66Scg149915  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30d0538f66Scg149915  * DEALINGS IN THE SOFTWARE.
31d0538f66Scg149915  *
32d0538f66Scg149915  * Authors:
33d0538f66Scg149915  *   Gareth Hughes <gareth@valinux.com>
34d0538f66Scg149915  *
35d0538f66Scg149915  */
36d0538f66Scg149915 
37d0538f66Scg149915 #include "drmP.h"
38d0538f66Scg149915 
39d0538f66Scg149915 #define	ATI_PCIGART_PAGE_SIZE		4096	/* PCI GART page size */
40d0538f66Scg149915 #define	ATI_MAX_PCIGART_PAGES		8192	/* 32 MB aperture, 4K pages */
41d0538f66Scg149915 #define	ATI_PCIGART_TABLE_SIZE		32768
42d0538f66Scg149915 
43d0538f66Scg149915 int
drm_ati_pcigart_init(drm_device_t * dev,drm_ati_pcigart_info * gart_info)44d0538f66Scg149915 drm_ati_pcigart_init(drm_device_t *dev, drm_ati_pcigart_info *gart_info)
45d0538f66Scg149915 {
46d0538f66Scg149915 	unsigned long pages;
47d0538f66Scg149915 	drm_sg_mem_t *entry;
48d0538f66Scg149915 	drm_dma_handle_t *dmah;
49d0538f66Scg149915 	u32 *pci_gart = NULL, page_base;
50d0538f66Scg149915 	int i, j, k;
51d0538f66Scg149915 	int	pagenum;
52d0538f66Scg149915 	size_t	bulksize;
53d0538f66Scg149915 
54d0538f66Scg149915 	entry = dev->sg;
55d0538f66Scg149915 	if (entry == NULL) {
56d0538f66Scg149915 		DRM_ERROR("no scatter/gather memory!\n");
57d0538f66Scg149915 		return (0);
58d0538f66Scg149915 	}
59d0538f66Scg149915 
60d0538f66Scg149915 	if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) {
61d0538f66Scg149915 		/* GART table in system memory */
62d0538f66Scg149915 		entry->dmah_gart = drm_pci_alloc(dev, ATI_PCIGART_TABLE_SIZE, 0,
63d0538f66Scg149915 		    0xfffffffful, 1);
64d0538f66Scg149915 		if (entry->dmah_gart == NULL) {
65d0538f66Scg149915 			DRM_ERROR("cannot allocate PCI GART table!\n");
66d0538f66Scg149915 			return (0);
67d0538f66Scg149915 		}
68d0538f66Scg149915 		gart_info->addr = (void *)entry->dmah_gart->vaddr;
69d0538f66Scg149915 		gart_info->bus_addr = entry->dmah_gart->paddr;
70d0538f66Scg149915 		pci_gart = (u32 *)entry->dmah_gart->vaddr;
71d0538f66Scg149915 	} else {
72d0538f66Scg149915 		/* GART table in framebuffer memory */
73d0538f66Scg149915 		pci_gart = gart_info->addr;
74d0538f66Scg149915 	}
75d0538f66Scg149915 
76d0538f66Scg149915 	pages = DRM_MIN(entry->pages, ATI_MAX_PCIGART_PAGES);
77d0538f66Scg149915 	bzero(pci_gart, ATI_PCIGART_TABLE_SIZE);
78*8793b36bSNick Todd 	/*CONSTCOND*/
79d0538f66Scg149915 	ASSERT(PAGE_SIZE >= ATI_PCIGART_PAGE_SIZE);
80d0538f66Scg149915 
81d0538f66Scg149915 	dmah = entry->dmah_sg;
82d0538f66Scg149915 	pagenum = 0;
83d0538f66Scg149915 	for (i = 0; i < dmah->cookie_num; i++) {
84d0538f66Scg149915 		bulksize = dmah->cookie.dmac_size;
85d0538f66Scg149915 		for (k = 0; k < bulksize / PAGE_SIZE; k++) {
86d0538f66Scg149915 			entry->busaddr[pagenum] =
87d0538f66Scg149915 			    dmah->cookie.dmac_address + k * PAGE_SIZE;
88d0538f66Scg149915 			page_base =  (u32) entry->busaddr[pagenum];
89d0538f66Scg149915 			if (pagenum ++ == pages)
90d0538f66Scg149915 				goto out;
91d0538f66Scg149915 			for (j = 0; j < (PAGE_SIZE / ATI_PCIGART_PAGE_SIZE);
92d0538f66Scg149915 			    j++) {
93d0538f66Scg149915 				if (gart_info->is_pcie)
94d0538f66Scg149915 					*pci_gart = (page_base >> 8) | 0xc;
95d0538f66Scg149915 				else
96d0538f66Scg149915 					*pci_gart = page_base;
97d0538f66Scg149915 				pci_gart++;
98d0538f66Scg149915 				page_base += ATI_PCIGART_PAGE_SIZE;
99d0538f66Scg149915 			}
100d0538f66Scg149915 		}
101d0538f66Scg149915 		ddi_dma_nextcookie(dmah->dma_hdl, &dmah->cookie);
102d0538f66Scg149915 	}
103d0538f66Scg149915 
104d0538f66Scg149915 out:
105d0538f66Scg149915 	if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) {
106d0538f66Scg149915 		(void) ddi_dma_sync(entry->dmah_gart->dma_hdl, 0,
107d0538f66Scg149915 		    entry->dmah_gart->real_sz, DDI_DMA_SYNC_FORDEV);
108d0538f66Scg149915 	}
109d0538f66Scg149915 
110d0538f66Scg149915 	return (1);
111d0538f66Scg149915 }
112d0538f66Scg149915 
113d0538f66Scg149915 /*ARGSUSED*/
114d0538f66Scg149915 extern int
drm_ati_pcigart_cleanup(drm_device_t * dev,drm_ati_pcigart_info * gart_info)115d0538f66Scg149915 drm_ati_pcigart_cleanup(drm_device_t *dev, drm_ati_pcigart_info *gart_info)
116d0538f66Scg149915 {
117d0538f66Scg149915 	drm_dma_handle_t	*dmah;
118d0538f66Scg149915 
119d0538f66Scg149915 	if (dev->sg == NULL) {
120d0538f66Scg149915 		DRM_ERROR("no scatter/gather memory!\n");
121d0538f66Scg149915 		return (0);
122d0538f66Scg149915 	}
123d0538f66Scg149915 	dmah = dev->sg->dmah_gart;
124d0538f66Scg149915 	dev->sg->dmah_gart = NULL;
125d0538f66Scg149915 	if (dmah)
126d0538f66Scg149915 		drm_pci_free(dev, dmah);
127d0538f66Scg149915 	return (1);
128d0538f66Scg149915 }
129