OSDN Git Service

Use headers copied from kernel instead of shared-core
[android-x86/external-libdrm.git] / bsd-core / ati_pcigart.c
1 /*-
2  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *   Gareth Hughes <gareth@valinux.com>
26  *
27  */
28
29 /** @file ati_pcigart.c
30  * Implementation of ATI's PCIGART, which provides an aperture in card virtual
31  * address space with addresses remapped to system memory.
32  */
33
34 #include "drmP.h"
35
36 #define ATI_PCIGART_PAGE_SIZE           4096    /* PCI GART page size */
37 #define ATI_PCIGART_PAGE_MASK           (~(ATI_PCIGART_PAGE_SIZE-1))
38
39 #define ATI_PCIE_WRITE 0x4
40 #define ATI_PCIE_READ 0x8
41
42 static void
43 drm_ati_alloc_pcigart_table_cb(void *arg, bus_dma_segment_t *segs,
44                                int nsegs, int error)
45 {
46         struct drm_dma_handle *dmah = arg;
47
48         if (error != 0)
49                 return;
50
51         KASSERT(nsegs == 1,
52             ("drm_ati_alloc_pcigart_table_cb: bad dma segment count"));
53
54         dmah->busaddr = segs[0].ds_addr;
55 }
56
57 static int
58 drm_ati_alloc_pcigart_table(struct drm_device *dev,
59                             struct drm_ati_pcigart_info *gart_info)
60 {
61         struct drm_dma_handle *dmah;
62         int flags, ret;
63
64         dmah = malloc(sizeof(struct drm_dma_handle), DRM_MEM_DMA,
65             M_ZERO | M_NOWAIT);
66         if (dmah == NULL)
67                 return ENOMEM;
68
69         DRM_UNLOCK();
70         ret = bus_dma_tag_create(NULL, PAGE_SIZE, 0, /* tag, align, boundary */
71             gart_info->table_mask, BUS_SPACE_MAXADDR, /* lowaddr, highaddr */
72             NULL, NULL, /* filtfunc, filtfuncargs */
73             gart_info->table_size, 1, /* maxsize, nsegs */
74             gart_info->table_size, /* maxsegsize */
75             BUS_DMA_ALLOCNOW, NULL, NULL, /* flags, lockfunc, lockfuncargs */
76             &dmah->tag);
77         if (ret != 0) {
78                 free(dmah, DRM_MEM_DMA);
79                 return ENOMEM;
80         }
81
82         flags = BUS_DMA_NOWAIT | BUS_DMA_ZERO;
83         if (gart_info->gart_reg_if == DRM_ATI_GART_IGP)
84             flags |= BUS_DMA_NOCACHE;
85         
86         ret = bus_dmamem_alloc(dmah->tag, &dmah->vaddr, flags, &dmah->map);
87         if (ret != 0) {
88                 bus_dma_tag_destroy(dmah->tag);
89                 free(dmah, DRM_MEM_DMA);
90                 return ENOMEM;
91         }
92         DRM_LOCK();
93
94         ret = bus_dmamap_load(dmah->tag, dmah->map, dmah->vaddr,
95             gart_info->table_size, drm_ati_alloc_pcigart_table_cb, dmah, 0);
96         if (ret != 0) {
97                 bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
98                 bus_dma_tag_destroy(dmah->tag);
99                 free(dmah, DRM_MEM_DMA);
100                 return ENOMEM;
101         }
102
103         dev->sg->dmah = dmah;
104
105         return 0;
106 }
107
108 static void
109 drm_ati_free_pcigart_table(struct drm_device *dev,
110                            struct drm_ati_pcigart_info *gart_info)
111 {
112         struct drm_dma_handle *dmah = dev->sg->dmah;
113
114         bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
115         bus_dma_tag_destroy(dmah->tag);
116         free(dmah, DRM_MEM_DMA);
117         dev->sg->dmah = NULL;
118 }
119
120 int
121 drm_ati_pcigart_cleanup(struct drm_device *dev,
122                         struct drm_ati_pcigart_info *gart_info)
123 {
124         /* we need to support large memory configurations */
125         if (dev->sg == NULL) {
126                 DRM_ERROR("no scatter/gather memory!\n");
127                 return 0;
128         }
129
130         if (gart_info->bus_addr) {
131                 if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) {
132                         gart_info->bus_addr = 0;
133                         if (dev->sg->dmah)
134                                 drm_ati_free_pcigart_table(dev, gart_info);
135                 }
136         }
137
138         return 1;
139 }
140
141 int
142 drm_ati_pcigart_init(struct drm_device *dev,
143                      struct drm_ati_pcigart_info *gart_info)
144 {
145         void *address = NULL;
146         unsigned long pages;
147         u32 *pci_gart, page_base;
148         dma_addr_t bus_address = 0;
149         dma_addr_t entry_addr;
150         int i, j, ret = 0;
151         int max_pages;
152
153         /* we need to support large memory configurations */
154         if (dev->sg == NULL) {
155                 DRM_ERROR("no scatter/gather memory!\n");
156                 goto done;
157         }
158
159         if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) {
160                 DRM_DEBUG("PCI: no table in VRAM: using normal RAM\n");
161
162                 ret = drm_ati_alloc_pcigart_table(dev, gart_info);
163                 if (ret) {
164                         DRM_ERROR("cannot allocate PCI GART page!\n");
165                         goto done;
166                 }
167
168                 address = (void *)dev->sg->dmah->vaddr;
169                 bus_address = dev->sg->dmah->busaddr;
170         } else {
171                 address = gart_info->addr;
172                 bus_address = gart_info->bus_addr;
173                 DRM_DEBUG("PCI: Gart Table: VRAM %08X mapped at %08lX\n",
174                           (unsigned int)bus_address, (unsigned long)address);
175         }
176
177         pci_gart = (u32 *) address;
178
179         max_pages = (gart_info->table_size / sizeof(u32));
180         pages = (dev->sg->pages <= max_pages)
181             ? dev->sg->pages : max_pages;
182
183         memset(pci_gart, 0, max_pages * sizeof(u32));
184
185         KASSERT(PAGE_SIZE >= ATI_PCIGART_PAGE_SIZE, ("page size too small"));
186
187         for (i = 0; i < pages; i++) {
188                 entry_addr = dev->sg->busaddr[i];
189                 for (j = 0; j < (PAGE_SIZE / ATI_PCIGART_PAGE_SIZE); j++) {
190                         page_base = (u32) entry_addr & ATI_PCIGART_PAGE_MASK;
191                         switch(gart_info->gart_reg_if) {
192                         case DRM_ATI_GART_IGP:
193                                 page_base |=
194                                     (upper_32_bits(entry_addr) & 0xff) << 4;
195                                 page_base |= 0xc;
196                                 break;
197                         case DRM_ATI_GART_PCIE:
198                                 page_base >>= 8;
199                                 page_base |=
200                                     (upper_32_bits(entry_addr) & 0xff) << 24;
201                                 page_base |= ATI_PCIE_READ | ATI_PCIE_WRITE;
202                                 break;
203                         default:
204                         case DRM_ATI_GART_PCI:
205                                 break;
206                         }
207                         *pci_gart = cpu_to_le32(page_base);
208                         pci_gart++;
209                         entry_addr += ATI_PCIGART_PAGE_SIZE;
210                 }
211         }
212
213         ret = 1;
214
215     done:
216         gart_info->addr = address;
217         gart_info->bus_addr = bus_address;
218         return ret;
219 }