80d3a8bc067d9059f3664e88178a3c9b9c40a02d
[dragonfly.git] / sys / dev / drm / 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 <drm/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 = kmalloc(sizeof(struct drm_dma_handle), DRM_MEM_DMA,
65             M_ZERO | M_NOWAIT);
66         if (dmah == NULL)
67                 return ENOMEM;
68
69         DRM_UNLOCK(dev);
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             0, /* flags */
76             &dmah->tag);
77         if (ret != 0) {
78                 kfree(dmah, DRM_MEM_DMA);
79                 return ENOMEM;
80         }
81
82         flags = BUS_DMA_WAITOK | BUS_DMA_ZERO;
83
84         if (gart_info->gart_reg_if == DRM_ATI_GART_IGP)
85             flags |= BUS_DMA_NOCACHE;
86         
87         ret = bus_dmamem_alloc(dmah->tag, &dmah->vaddr, flags, &dmah->map);
88         if (ret != 0) {
89                 bus_dma_tag_destroy(dmah->tag);
90                 kfree(dmah, DRM_MEM_DMA);
91                 return ENOMEM;
92         }
93         DRM_LOCK(dev);
94
95         ret = bus_dmamap_load(dmah->tag, dmah->map, dmah->vaddr,
96             gart_info->table_size, drm_ati_alloc_pcigart_table_cb, dmah,
97             BUS_DMA_NOWAIT);
98         if (ret != 0) {
99                 bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
100                 bus_dma_tag_destroy(dmah->tag);
101                 kfree(dmah, DRM_MEM_DMA);
102                 return ENOMEM;
103         }
104
105         gart_info->dmah = dmah;
106
107         return 0;
108 }
109
110 static void
111 drm_ati_free_pcigart_table(struct drm_device *dev,
112                            struct drm_ati_pcigart_info *gart_info)
113 {
114         struct drm_dma_handle *dmah = gart_info->dmah;
115
116         bus_dmamem_free(dmah->tag, dmah->vaddr, dmah->map);
117         bus_dma_tag_destroy(dmah->tag);
118         kfree(dmah, DRM_MEM_DMA);
119         gart_info->dmah = NULL;
120 }
121
122 int
123 drm_ati_pcigart_cleanup(struct drm_device *dev,
124                         struct drm_ati_pcigart_info *gart_info)
125 {
126         /* we need to support large memory configurations */
127         if (dev->sg == NULL) {
128                 DRM_ERROR("no scatter/gather memory!\n");
129                 return 0;
130         }
131
132         if (gart_info->bus_addr) {
133                 if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) {
134                         gart_info->bus_addr = 0;
135                         if (gart_info->dmah)
136                                 drm_ati_free_pcigart_table(dev, gart_info);
137                 }
138         }
139
140         return 1;
141 }
142
143 int
144 drm_ati_pcigart_init(struct drm_device *dev,
145                      struct drm_ati_pcigart_info *gart_info)
146 {
147         void *address = NULL;
148         unsigned long pages;
149         u32 *pci_gart, page_base;
150         dma_addr_t bus_address = 0;
151         dma_addr_t entry_addr;
152         int i, j, ret = 0;
153         int max_pages;
154
155         /* we need to support large memory configurations */
156         if (dev->sg == NULL) {
157                 DRM_ERROR("no scatter/gather memory!\n");
158                 goto done;
159         }
160
161         if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) {
162                 DRM_DEBUG("PCI: no table in VRAM: using normal RAM\n");
163
164                 ret = drm_ati_alloc_pcigart_table(dev, gart_info);
165                 if (ret) {
166                         DRM_ERROR("cannot allocate PCI GART page!\n");
167                         goto done;
168                 }
169
170                 address = (void *)gart_info->dmah->vaddr;
171                 bus_address = gart_info->dmah->busaddr;
172         } else {
173                 address = gart_info->addr;
174                 bus_address = gart_info->bus_addr;
175                 DRM_DEBUG("PCI: Gart Table: VRAM %08X mapped at %08lX\n",
176                           (unsigned int)bus_address, (unsigned long)address);
177         }
178
179         pci_gart = (u32 *) address;
180
181         max_pages = (gart_info->table_size / sizeof(u32));
182         pages = (dev->sg->pages <= max_pages)
183             ? dev->sg->pages : max_pages;
184
185         memset(pci_gart, 0, max_pages * sizeof(u32));
186
187         KASSERT(PAGE_SIZE >= ATI_PCIGART_PAGE_SIZE, ("page size too small"));
188
189         for (i = 0; i < pages; i++) {
190                 entry_addr = dev->sg->busaddr[i];
191                 for (j = 0; j < (PAGE_SIZE / ATI_PCIGART_PAGE_SIZE); j++) {
192                         page_base = (u32) entry_addr & ATI_PCIGART_PAGE_MASK;
193                         switch(gart_info->gart_reg_if) {
194                         case DRM_ATI_GART_IGP:
195                                 page_base |=
196                                     (upper_32_bits(entry_addr) & 0xff) << 4;
197                                 page_base |= 0xc;
198                                 break;
199                         case DRM_ATI_GART_PCIE:
200                                 page_base >>= 8;
201                                 page_base |=
202                                     (upper_32_bits(entry_addr) & 0xff) << 24;
203                                 page_base |= ATI_PCIE_READ | ATI_PCIE_WRITE;
204                                 break;
205                         default:
206                         case DRM_ATI_GART_PCI:
207                                 break;
208                         }
209                         *pci_gart = cpu_to_le32(page_base);
210                         pci_gart++;
211                         entry_addr += ATI_PCIGART_PAGE_SIZE;
212                 }
213         }
214
215         ret = 1;
216
217     done:
218         gart_info->addr = address;
219         gart_info->bus_addr = bus_address;
220         return ret;
221 }