5 * \author Gareth Hughes <gareth@valinux.com>
9 * Created: Wed Dec 13 21:52:19 2000 by gareth@valinux.com
11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice (including the next
22 * paragraph) shall be included in all copies or substantial portions of the
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31 * DEALINGS IN THE SOFTWARE.
36 # define ATI_PCIGART_PAGE_SIZE 4096 /**< PCI GART page size */
37 # define ATI_PCIGART_PAGE_MASK (~(ATI_PCIGART_PAGE_SIZE-1))
39 #define ATI_PCIE_WRITE 0x4
40 #define ATI_PCIE_READ 0x8
42 static __inline__ void gart_insert_page_into_table(struct drm_ati_pcigart_info *gart_info, dma_addr_t addr, u32 *pci_gart)
46 page_base = (u32)addr & ATI_PCIGART_PAGE_MASK;
47 switch(gart_info->gart_reg_if) {
48 case DRM_ATI_GART_IGP:
49 page_base |= (upper_32_bits(addr) & 0xff) << 4;
52 case DRM_ATI_GART_PCIE:
54 page_base |= (upper_32_bits(addr) & 0xff) << 24;
55 page_base |= ATI_PCIE_READ | ATI_PCIE_WRITE;
58 case DRM_ATI_GART_PCI:
61 *pci_gart = cpu_to_le32(page_base);
64 static __inline__ dma_addr_t gart_get_page_from_table(struct drm_ati_pcigart_info *gart_info, u32 *pci_gart)
67 switch(gart_info->gart_reg_if) {
68 case DRM_ATI_GART_IGP:
69 retval = (*pci_gart & ATI_PCIGART_PAGE_MASK);
70 retval += (((*pci_gart & 0xf0) >> 4) << 16) << 16;
72 case DRM_ATI_GART_PCIE:
73 retval = (*pci_gart & ~0xc);
76 case DRM_ATI_GART_PCI:
84 int drm_ati_alloc_pcigart_table(struct drm_device *dev,
85 struct drm_ati_pcigart_info *gart_info)
87 gart_info->table_handle = drm_pci_alloc(dev, gart_info->table_size,
89 gart_info->table_mask);
90 if (gart_info->table_handle == NULL)
93 memset(gart_info->table_handle->vaddr, 0, gart_info->table_size);
96 EXPORT_SYMBOL(drm_ati_alloc_pcigart_table);
98 static void drm_ati_free_pcigart_table(struct drm_device *dev,
99 struct drm_ati_pcigart_info *gart_info)
101 drm_pci_free(dev, gart_info->table_handle);
102 gart_info->table_handle = NULL;
105 int drm_ati_pcigart_cleanup(struct drm_device *dev, struct drm_ati_pcigart_info *gart_info)
107 struct drm_sg_mem *entry = dev->sg;
112 /* we need to support large memory configurations */
117 if (gart_info->bus_addr) {
119 max_pages = (gart_info->table_size / sizeof(u32));
120 pages = (entry->pages <= max_pages)
121 ? entry->pages : max_pages;
123 for (i = 0; i < pages; i++) {
124 if (!entry->busaddr[i])
126 pci_unmap_page(dev->pdev, entry->busaddr[i],
127 PAGE_SIZE, PCI_DMA_TODEVICE);
130 if (gart_info->gart_table_location == DRM_ATI_GART_MAIN)
131 gart_info->bus_addr = 0;
135 if (gart_info->gart_table_location == DRM_ATI_GART_MAIN
136 && gart_info->table_handle) {
138 drm_ati_free_pcigart_table(dev, gart_info);
143 EXPORT_SYMBOL(drm_ati_pcigart_cleanup);
145 int drm_ati_pcigart_init(struct drm_device *dev, struct drm_ati_pcigart_info *gart_info)
147 struct drm_sg_mem *entry = dev->sg;
148 void *address = NULL;
151 dma_addr_t bus_address = 0;
154 dma_addr_t entry_addr;
157 if (gart_info->gart_table_location == DRM_ATI_GART_MAIN && gart_info->table_handle == NULL) {
158 DRM_DEBUG("PCI: no table in VRAM: using normal RAM\n");
160 ret = drm_ati_alloc_pcigart_table(dev, gart_info);
162 DRM_ERROR("cannot allocate PCI GART page!\n");
167 if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) {
168 address = gart_info->table_handle->vaddr;
169 bus_address = gart_info->table_handle->busaddr;
171 address = gart_info->addr;
172 bus_address = gart_info->bus_addr;
176 DRM_ERROR("no scatter/gather memory!\n");
180 pci_gart = (u32 *) address;
182 max_pages = (gart_info->table_size / sizeof(u32));
183 pages = (entry->pages <= max_pages)
184 ? entry->pages : max_pages;
186 for (i = 0; i < pages; i++) {
187 /* we need to support large memory configurations */
188 entry->busaddr[i] = pci_map_page(dev->pdev, entry->pagelist[i],
189 0, PAGE_SIZE, PCI_DMA_TODEVICE);
190 if (entry->busaddr[i] == 0) {
191 DRM_ERROR("unable to map PCIGART pages!\n");
192 drm_ati_pcigart_cleanup(dev, gart_info);
198 entry_addr = entry->busaddr[i];
199 for (j = 0; j < (PAGE_SIZE / ATI_PCIGART_PAGE_SIZE); j++) {
200 gart_insert_page_into_table(gart_info, entry_addr, pci_gart);
202 entry_addr += ATI_PCIGART_PAGE_SIZE;
211 gart_info->addr = address;
212 gart_info->bus_addr = bus_address;
215 EXPORT_SYMBOL(drm_ati_pcigart_init);
217 static int ati_pcigart_needs_unbind_cache_adjust(struct drm_ttm_backend *backend)
219 return ((backend->flags & DRM_BE_FLAG_BOUND_CACHED) ? 0 : 1);
222 static int ati_pcigart_populate(struct drm_ttm_backend *backend,
223 unsigned long num_pages,
225 struct page *dummy_read_page)
227 struct ati_pcigart_ttm_backend *atipci_be =
228 container_of(backend, struct ati_pcigart_ttm_backend, backend);
230 atipci_be->pages = pages;
231 atipci_be->num_pages = num_pages;
232 atipci_be->populated = 1;
236 static int ati_pcigart_bind_ttm(struct drm_ttm_backend *backend,
237 struct drm_bo_mem_reg *bo_mem)
239 struct ati_pcigart_ttm_backend *atipci_be =
240 container_of(backend, struct ati_pcigart_ttm_backend, backend);
243 struct drm_ati_pcigart_info *info = atipci_be->gart_info;
245 dma_addr_t offset = bo_mem->mm_node->start;
246 dma_addr_t page_base;
248 pci_gart = info->addr;
251 while (j < (offset + atipci_be->num_pages)) {
252 if (gart_get_page_from_table(info, pci_gart+j))
257 for (i = 0, j = offset; i < atipci_be->num_pages; i++, j++) {
258 struct page *cur_page = atipci_be->pages[i];
260 page_base = page_to_phys(cur_page);
261 gart_insert_page_into_table(info, page_base, pci_gart + j);
266 atipci_be->gart_flush_fn(atipci_be->dev);
268 atipci_be->bound = 1;
269 atipci_be->offset = offset;
270 /* need to traverse table and add entries */
275 static int ati_pcigart_unbind_ttm(struct drm_ttm_backend *backend)
277 struct ati_pcigart_ttm_backend *atipci_be =
278 container_of(backend, struct ati_pcigart_ttm_backend, backend);
279 struct drm_ati_pcigart_info *info = atipci_be->gart_info;
280 unsigned long offset = atipci_be->offset;
283 u32 *pci_gart = info->addr;
285 if (atipci_be->bound != 1)
288 for (i = 0, j = offset; i < atipci_be->num_pages; i++, j++) {
291 atipci_be->gart_flush_fn(atipci_be->dev);
292 atipci_be->bound = 0;
293 atipci_be->offset = 0;
297 static void ati_pcigart_clear_ttm(struct drm_ttm_backend *backend)
299 struct ati_pcigart_ttm_backend *atipci_be =
300 container_of(backend, struct ati_pcigart_ttm_backend, backend);
303 if (atipci_be->pages) {
304 backend->func->unbind(backend);
305 atipci_be->pages = NULL;
308 atipci_be->num_pages = 0;
311 static void ati_pcigart_destroy_ttm(struct drm_ttm_backend *backend)
313 struct ati_pcigart_ttm_backend *atipci_be;
316 atipci_be = container_of(backend, struct ati_pcigart_ttm_backend, backend);
318 if (atipci_be->pages) {
319 backend->func->clear(backend);
321 drm_ctl_free(atipci_be, sizeof(*atipci_be), DRM_MEM_TTM);
326 static struct drm_ttm_backend_func ati_pcigart_ttm_backend =
328 .needs_ub_cache_adjust = ati_pcigart_needs_unbind_cache_adjust,
329 .populate = ati_pcigart_populate,
330 .clear = ati_pcigart_clear_ttm,
331 .bind = ati_pcigart_bind_ttm,
332 .unbind = ati_pcigart_unbind_ttm,
333 .destroy = ati_pcigart_destroy_ttm,
336 struct drm_ttm_backend *ati_pcigart_init_ttm(struct drm_device *dev, struct drm_ati_pcigart_info *info, void (*gart_flush_fn)(struct drm_device *dev))
338 struct ati_pcigart_ttm_backend *atipci_be;
340 atipci_be = drm_ctl_calloc(1, sizeof (*atipci_be), DRM_MEM_TTM);
344 atipci_be->populated = 0;
345 atipci_be->backend.func = &ati_pcigart_ttm_backend;
346 // atipci_be->backend.mem_type = DRM_BO_MEM_TT;
347 atipci_be->gart_info = info;
348 atipci_be->gart_flush_fn = gart_flush_fn;
349 atipci_be->dev = dev;
351 return &atipci_be->backend;
353 EXPORT_SYMBOL(ati_pcigart_init_ttm);