Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /** |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 2 | * \file ati_pcigart.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * ATI PCI GART support |
| 4 | * |
| 5 | * \author Gareth Hughes <gareth@valinux.com> |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Created: Wed Dec 13 21:52:19 2000 by gareth@valinux.com |
| 10 | * |
| 11 | * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. |
| 12 | * All Rights Reserved. |
| 13 | * |
| 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: |
| 20 | * |
| 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 |
| 23 | * Software. |
| 24 | * |
| 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. |
| 32 | */ |
| 33 | |
| 34 | #include "drmP.h" |
| 35 | |
| 36 | #if PAGE_SIZE == 65536 |
| 37 | # define ATI_PCIGART_TABLE_ORDER 0 |
| 38 | # define ATI_PCIGART_TABLE_PAGES (1 << 0) |
| 39 | #elif PAGE_SIZE == 16384 |
| 40 | # define ATI_PCIGART_TABLE_ORDER 1 |
| 41 | # define ATI_PCIGART_TABLE_PAGES (1 << 1) |
| 42 | #elif PAGE_SIZE == 8192 |
| 43 | # define ATI_PCIGART_TABLE_ORDER 2 |
| 44 | # define ATI_PCIGART_TABLE_PAGES (1 << 2) |
| 45 | #elif PAGE_SIZE == 4096 |
| 46 | # define ATI_PCIGART_TABLE_ORDER 3 |
| 47 | # define ATI_PCIGART_TABLE_PAGES (1 << 3) |
| 48 | #else |
| 49 | # error - PAGE_SIZE not 64K, 16K, 8K or 4K |
| 50 | #endif |
| 51 | |
| 52 | # define ATI_MAX_PCIGART_PAGES 8192 /**< 32 MB aperture, 4K pages */ |
| 53 | # define ATI_PCIGART_PAGE_SIZE 4096 /**< PCI GART page size */ |
| 54 | |
Dave Airlie | f26c473 | 2006-01-02 17:18:39 +1100 | [diff] [blame^] | 55 | static void *drm_ati_alloc_pcigart_table(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | { |
| 57 | unsigned long address; |
| 58 | struct page *page; |
| 59 | int i; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 60 | DRM_DEBUG("%s\n", __FUNCTION__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 62 | address = __get_free_pages(GFP_KERNEL, ATI_PCIGART_TABLE_ORDER); |
| 63 | if (address == 0UL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | return 0; |
| 65 | } |
| 66 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 67 | page = virt_to_page(address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 69 | for (i = 0; i < ATI_PCIGART_TABLE_PAGES; i++, page++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | get_page(page); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 71 | SetPageReserved(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 74 | DRM_DEBUG("%s: returning 0x%08lx\n", __FUNCTION__, address); |
Dave Airlie | f26c473 | 2006-01-02 17:18:39 +1100 | [diff] [blame^] | 75 | return (void *)address; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Dave Airlie | f26c473 | 2006-01-02 17:18:39 +1100 | [diff] [blame^] | 78 | static void drm_ati_free_pcigart_table(void *address) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
| 80 | struct page *page; |
| 81 | int i; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 82 | DRM_DEBUG("%s\n", __FUNCTION__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Dave Airlie | f26c473 | 2006-01-02 17:18:39 +1100 | [diff] [blame^] | 84 | page = virt_to_page((unsigned long)address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 86 | for (i = 0; i < ATI_PCIGART_TABLE_PAGES; i++, page++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | __put_page(page); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 88 | ClearPageReserved(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Dave Airlie | f26c473 | 2006-01-02 17:18:39 +1100 | [diff] [blame^] | 91 | free_pages((unsigned long)address, ATI_PCIGART_TABLE_ORDER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Dave Airlie | f26c473 | 2006-01-02 17:18:39 +1100 | [diff] [blame^] | 94 | int drm_ati_pcigart_cleanup(drm_device_t *dev, drm_ati_pcigart_info *gart_info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
| 96 | drm_sg_mem_t *entry = dev->sg; |
| 97 | unsigned long pages; |
| 98 | int i; |
| 99 | |
| 100 | /* we need to support large memory configurations */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 101 | if (!entry) { |
| 102 | DRM_ERROR("no scatter/gather memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | return 0; |
| 104 | } |
| 105 | |
Dave Airlie | ea98a92 | 2005-09-11 20:28:11 +1000 | [diff] [blame] | 106 | if (gart_info->bus_addr) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 107 | if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) { |
Dave Airlie | ea98a92 | 2005-09-11 20:28:11 +1000 | [diff] [blame] | 108 | pci_unmap_single(dev->pdev, gart_info->bus_addr, |
| 109 | ATI_PCIGART_TABLE_PAGES * PAGE_SIZE, |
| 110 | PCI_DMA_TODEVICE); |
| 111 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 113 | pages = (entry->pages <= ATI_MAX_PCIGART_PAGES) |
| 114 | ? entry->pages : ATI_MAX_PCIGART_PAGES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 116 | for (i = 0; i < pages; i++) { |
| 117 | if (!entry->busaddr[i]) |
| 118 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | pci_unmap_single(dev->pdev, entry->busaddr[i], |
| 120 | PAGE_SIZE, PCI_DMA_TODEVICE); |
| 121 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 122 | |
| 123 | if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) |
| 124 | gart_info->bus_addr = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 127 | if (gart_info->gart_table_location == DRM_ATI_GART_MAIN |
| 128 | && gart_info->addr) { |
Dave Airlie | ea98a92 | 2005-09-11 20:28:11 +1000 | [diff] [blame] | 129 | drm_ati_free_pcigart_table(gart_info->addr); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 130 | gart_info->addr = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | return 1; |
| 134 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | EXPORT_SYMBOL(drm_ati_pcigart_cleanup); |
| 137 | |
Dave Airlie | f26c473 | 2006-01-02 17:18:39 +1100 | [diff] [blame^] | 138 | int drm_ati_pcigart_init(drm_device_t *dev, drm_ati_pcigart_info *gart_info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | { |
| 140 | drm_sg_mem_t *entry = dev->sg; |
Dave Airlie | f26c473 | 2006-01-02 17:18:39 +1100 | [diff] [blame^] | 141 | void *address = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | unsigned long pages; |
| 143 | u32 *pci_gart, page_base, bus_address = 0; |
| 144 | int i, j, ret = 0; |
| 145 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 146 | if (!entry) { |
| 147 | DRM_ERROR("no scatter/gather memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | goto done; |
| 149 | } |
| 150 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 151 | if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) { |
Dave Airlie | ea98a92 | 2005-09-11 20:28:11 +1000 | [diff] [blame] | 152 | DRM_DEBUG("PCI: no table in VRAM: using normal RAM\n"); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 153 | |
Dave Airlie | ea98a92 | 2005-09-11 20:28:11 +1000 | [diff] [blame] | 154 | address = drm_ati_alloc_pcigart_table(); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 155 | if (!address) { |
| 156 | DRM_ERROR("cannot allocate PCI GART page!\n"); |
Dave Airlie | ea98a92 | 2005-09-11 20:28:11 +1000 | [diff] [blame] | 157 | goto done; |
| 158 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 159 | |
| 160 | if (!dev->pdev) { |
| 161 | DRM_ERROR("PCI device unknown!\n"); |
Dave Airlie | ea98a92 | 2005-09-11 20:28:11 +1000 | [diff] [blame] | 162 | goto done; |
| 163 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
Dave Airlie | f26c473 | 2006-01-02 17:18:39 +1100 | [diff] [blame^] | 165 | bus_address = pci_map_single(dev->pdev, address, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 166 | ATI_PCIGART_TABLE_PAGES * |
| 167 | PAGE_SIZE, PCI_DMA_TODEVICE); |
Dave Airlie | ea98a92 | 2005-09-11 20:28:11 +1000 | [diff] [blame] | 168 | if (bus_address == 0) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 169 | DRM_ERROR("unable to map PCIGART pages!\n"); |
| 170 | drm_ati_free_pcigart_table(address); |
Dave Airlie | ea98a92 | 2005-09-11 20:28:11 +1000 | [diff] [blame] | 171 | address = 0; |
| 172 | goto done; |
| 173 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 174 | } else { |
Dave Airlie | ea98a92 | 2005-09-11 20:28:11 +1000 | [diff] [blame] | 175 | address = gart_info->addr; |
| 176 | bus_address = gart_info->bus_addr; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 177 | DRM_DEBUG("PCI: Gart Table: VRAM %08X mapped at %08lX\n", |
Dave Airlie | f26c473 | 2006-01-02 17:18:39 +1100 | [diff] [blame^] | 178 | bus_address, (unsigned long)address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 181 | pci_gart = (u32 *) address; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 183 | pages = (entry->pages <= ATI_MAX_PCIGART_PAGES) |
| 184 | ? entry->pages : ATI_MAX_PCIGART_PAGES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 186 | memset(pci_gart, 0, ATI_MAX_PCIGART_PAGES * sizeof(u32)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 188 | for (i = 0; i < pages; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | /* we need to support large memory configurations */ |
| 190 | entry->busaddr[i] = pci_map_single(dev->pdev, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 191 | page_address(entry-> |
| 192 | pagelist[i]), |
| 193 | PAGE_SIZE, PCI_DMA_TODEVICE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | if (entry->busaddr[i] == 0) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 195 | DRM_ERROR("unable to map PCIGART pages!\n"); |
Dave Airlie | ea98a92 | 2005-09-11 20:28:11 +1000 | [diff] [blame] | 196 | drm_ati_pcigart_cleanup(dev, gart_info); |
Dave Airlie | f26c473 | 2006-01-02 17:18:39 +1100 | [diff] [blame^] | 197 | address = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | bus_address = 0; |
| 199 | goto done; |
| 200 | } |
| 201 | page_base = (u32) entry->busaddr[i]; |
| 202 | |
| 203 | for (j = 0; j < (PAGE_SIZE / ATI_PCIGART_PAGE_SIZE); j++) { |
Dave Airlie | ea98a92 | 2005-09-11 20:28:11 +1000 | [diff] [blame] | 204 | if (gart_info->is_pcie) |
Dave Airlie | d34d7ae | 2005-11-08 21:34:31 -0800 | [diff] [blame] | 205 | *pci_gart = cpu_to_le32((page_base >> 8) | 0xc); |
Dave Airlie | ea98a92 | 2005-09-11 20:28:11 +1000 | [diff] [blame] | 206 | else |
Dave Airlie | 3d5efad | 2005-09-30 19:12:46 +1000 | [diff] [blame] | 207 | *pci_gart = cpu_to_le32(page_base); |
Dave Airlie | d34d7ae | 2005-11-08 21:34:31 -0800 | [diff] [blame] | 208 | pci_gart++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | page_base += ATI_PCIGART_PAGE_SIZE; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | ret = 1; |
| 214 | |
| 215 | #if defined(__i386__) || defined(__x86_64__) |
| 216 | wbinvd(); |
| 217 | #else |
| 218 | mb(); |
| 219 | #endif |
| 220 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 221 | done: |
Dave Airlie | ea98a92 | 2005-09-11 20:28:11 +1000 | [diff] [blame] | 222 | gart_info->addr = address; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 223 | gart_info->bus_addr = bus_address; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | return ret; |
| 225 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | EXPORT_SYMBOL(drm_ati_pcigart_init); |