487348fb68c337167fde89fc18dc39e5b0e5da56
[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  * $DragonFly: src/sys/dev/drm/ati_pcigart.c,v 1.1 2008/04/05 18:12:29 hasso Exp $
28  */
29
30 /** @file ati_pcigart.c
31  * Implementation of ATI's PCIGART, which provides an aperture in card virtual
32  * address space with addresses remapped to system memory.
33  */
34
35 #include "drmP.h"
36
37 #define ATI_PCIGART_PAGE_SIZE           4096    /* PCI GART page size */
38
39 int drm_ati_pcigart_init(drm_device_t *dev, struct drm_ati_pcigart_info *gart_info)
40 {
41         unsigned long pages;
42         u32 *pci_gart = NULL, page_base;
43         int i, j;
44
45         if (dev->sg == NULL) {
46                 DRM_ERROR( "no scatter/gather memory!\n" );
47                 return 0;
48         }
49
50         if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) {
51                 /* GART table in system memory */
52                 dev->sg->dmah = drm_pci_alloc(dev, gart_info->table_size, 0,
53                     0xfffffffful);
54                 if (dev->sg->dmah == NULL) {
55                         DRM_ERROR("cannot allocate PCI GART table!\n");
56                         return 0;
57                 }
58         
59                 gart_info->addr = (void *)dev->sg->dmah->vaddr;
60                 gart_info->bus_addr = dev->sg->dmah->busaddr;
61                 pci_gart = (u32 *)dev->sg->dmah->vaddr;
62         } else {
63                 /* GART table in framebuffer memory */
64                 pci_gart = gart_info->addr;
65         }
66         
67         pages = DRM_MIN(dev->sg->pages, gart_info->table_size / sizeof(u32));
68
69         bzero(pci_gart, gart_info->table_size);
70
71         KASSERT(PAGE_SIZE >= ATI_PCIGART_PAGE_SIZE, ("page size too small"));
72
73         for ( i = 0 ; i < pages ; i++ ) {
74                 page_base = (u32) dev->sg->busaddr[i];
75
76                 for (j = 0; j < (PAGE_SIZE / ATI_PCIGART_PAGE_SIZE); j++) {
77                         switch(gart_info->gart_reg_if) {
78                         case DRM_ATI_GART_IGP:
79                                 *pci_gart = cpu_to_le32(page_base | 0xc);
80                                 break;
81                         case DRM_ATI_GART_PCIE:
82                                 *pci_gart = cpu_to_le32((page_base >> 8) | 0xc);
83                                 break;
84                         default:
85                                 *pci_gart = cpu_to_le32(page_base);
86                                 break;
87                         }
88                         pci_gart++;
89                         page_base += ATI_PCIGART_PAGE_SIZE;
90                 }
91         }
92
93         DRM_MEMORYBARRIER();
94
95         return 1;
96 }
97
98 int drm_ati_pcigart_cleanup(drm_device_t *dev, struct drm_ati_pcigart_info *gart_info)
99 {
100         if (dev->sg == NULL) {
101                 DRM_ERROR( "no scatter/gather memory!\n" );
102                 return 0;
103         }
104
105         drm_pci_free(dev, dev->sg->dmah);
106
107         return 1;
108 }