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, 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;
208 #if defined(__i386__) || defined(__x86_64__)
215 gart_info->addr = address;
216 gart_info->bus_addr = bus_address;
219 EXPORT_SYMBOL(drm_ati_pcigart_init);
221 static int ati_pcigart_needs_unbind_cache_adjust(struct drm_ttm_backend *backend)
223 return ((backend->flags & DRM_BE_FLAG_BOUND_CACHED) ? 0 : 1);
226 static int ati_pcigart_populate(struct drm_ttm_backend *backend,
227 unsigned long num_pages,
229 struct page *dummy_read_page)
231 struct ati_pcigart_ttm_backend *atipci_be =
232 container_of(backend, struct ati_pcigart_ttm_backend, backend);
234 atipci_be->pages = pages;
235 atipci_be->num_pages = num_pages;
236 atipci_be->populated = 1;
240 static int ati_pcigart_bind_ttm(struct drm_ttm_backend *backend,
241 struct drm_bo_mem_reg *bo_mem)
243 struct ati_pcigart_ttm_backend *atipci_be =
244 container_of(backend, struct ati_pcigart_ttm_backend, backend);
247 struct drm_ati_pcigart_info *info = atipci_be->gart_info;
249 dma_addr_t offset = bo_mem->mm_node->start;
250 dma_addr_t page_base;
252 pci_gart = info->addr;
255 while (j < (offset + atipci_be->num_pages)) {
256 if (gart_get_page_from_table(info, pci_gart+j))
261 for (i = 0, j = offset; i < atipci_be->num_pages; i++, j++) {
262 struct page *cur_page = atipci_be->pages[i];
264 page_base = page_to_phys(cur_page);
265 gart_insert_page_into_table(info, page_base, pci_gart + j);
268 #if defined(__i386__) || defined(__x86_64__)
274 atipci_be->gart_flush_fn(atipci_be->dev);
276 atipci_be->bound = 1;
277 atipci_be->offset = offset;
278 /* need to traverse table and add entries */
283 static int ati_pcigart_unbind_ttm(struct drm_ttm_backend *backend)
285 struct ati_pcigart_ttm_backend *atipci_be =
286 container_of(backend, struct ati_pcigart_ttm_backend, backend);
287 struct drm_ati_pcigart_info *info = atipci_be->gart_info;
288 unsigned long offset = atipci_be->offset;
291 u32 *pci_gart = info->addr;
293 if (atipci_be->bound != 1)
296 for (i = 0, j = offset; i < atipci_be->num_pages; i++, j++) {
299 atipci_be->gart_flush_fn(atipci_be->dev);
300 atipci_be->bound = 0;
301 atipci_be->offset = 0;
305 static void ati_pcigart_clear_ttm(struct drm_ttm_backend *backend)
307 struct ati_pcigart_ttm_backend *atipci_be =
308 container_of(backend, struct ati_pcigart_ttm_backend, backend);
311 if (atipci_be->pages) {
312 backend->func->unbind(backend);
313 atipci_be->pages = NULL;
316 atipci_be->num_pages = 0;
319 static void ati_pcigart_destroy_ttm(struct drm_ttm_backend *backend)
321 struct ati_pcigart_ttm_backend *atipci_be;
324 atipci_be = container_of(backend, struct ati_pcigart_ttm_backend, backend);
326 if (atipci_be->pages) {
327 backend->func->clear(backend);
329 drm_ctl_free(atipci_be, sizeof(*atipci_be), DRM_MEM_TTM);
334 static struct drm_ttm_backend_func ati_pcigart_ttm_backend =
336 .needs_ub_cache_adjust = ati_pcigart_needs_unbind_cache_adjust,
337 .populate = ati_pcigart_populate,
338 .clear = ati_pcigart_clear_ttm,
339 .bind = ati_pcigart_bind_ttm,
340 .unbind = ati_pcigart_unbind_ttm,
341 .destroy = ati_pcigart_destroy_ttm,
344 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))
346 struct ati_pcigart_ttm_backend *atipci_be;
348 atipci_be = drm_ctl_calloc(1, sizeof (*atipci_be), DRM_MEM_TTM);
352 atipci_be->populated = 0;
353 atipci_be->backend.func = &ati_pcigart_ttm_backend;
354 // atipci_be->backend.mem_type = DRM_BO_MEM_TT;
355 atipci_be->gart_info = info;
356 atipci_be->gart_flush_fn = gart_flush_fn;
357 atipci_be->dev = dev;
359 return &atipci_be->backend;
361 EXPORT_SYMBOL(ati_pcigart_init_ttm);