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