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