1 1.1 riastrad /* $NetBSD: ati_pcigart.c,v 1.2 2021/12/18 23:45:42 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /** 4 1.1 riastrad * \file ati_pcigart.c 5 1.1 riastrad * ATI PCI GART support 6 1.1 riastrad * 7 1.1 riastrad * \author Gareth Hughes <gareth (at) valinux.com> 8 1.1 riastrad */ 9 1.1 riastrad 10 1.1 riastrad /* 11 1.1 riastrad * Created: Wed Dec 13 21:52:19 2000 by gareth (at) valinux.com 12 1.1 riastrad * 13 1.1 riastrad * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. 14 1.1 riastrad * All Rights Reserved. 15 1.1 riastrad * 16 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 17 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 18 1.1 riastrad * to deal in the Software without restriction, including without limitation 19 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 20 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 21 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 22 1.1 riastrad * 23 1.1 riastrad * The above copyright notice and this permission notice (including the next 24 1.1 riastrad * paragraph) shall be included in all copies or substantial portions of the 25 1.1 riastrad * Software. 26 1.1 riastrad * 27 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 28 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 29 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 30 1.1 riastrad * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 31 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 32 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 33 1.1 riastrad * DEALINGS IN THE SOFTWARE. 34 1.1 riastrad */ 35 1.1 riastrad 36 1.1 riastrad #include <sys/cdefs.h> 37 1.1 riastrad __KERNEL_RCSID(0, "$NetBSD: ati_pcigart.c,v 1.2 2021/12/18 23:45:42 riastradh Exp $"); 38 1.1 riastrad 39 1.1 riastrad #include <linux/export.h> 40 1.1 riastrad 41 1.1 riastrad #include <drm/drm_device.h> 42 1.1 riastrad #include <drm/drm_pci.h> 43 1.1 riastrad #include <drm/drm_print.h> 44 1.1 riastrad 45 1.1 riastrad #include "ati_pcigart.h" 46 1.1 riastrad 47 1.1 riastrad # define ATI_PCIGART_PAGE_SIZE 4096 /**< PCI GART page size */ 48 1.1 riastrad 49 1.1 riastrad static int drm_ati_alloc_pcigart_table(struct drm_device *dev, 50 1.1 riastrad struct drm_ati_pcigart_info *gart_info) 51 1.1 riastrad { 52 1.1 riastrad gart_info->table_handle = drm_pci_alloc(dev, gart_info->table_size, 53 1.1 riastrad PAGE_SIZE); 54 1.1 riastrad if (gart_info->table_handle == NULL) 55 1.1 riastrad return -ENOMEM; 56 1.1 riastrad 57 1.1 riastrad return 0; 58 1.1 riastrad } 59 1.1 riastrad 60 1.1 riastrad static void drm_ati_free_pcigart_table(struct drm_device *dev, 61 1.1 riastrad struct drm_ati_pcigart_info *gart_info) 62 1.1 riastrad { 63 1.1 riastrad drm_pci_free(dev, gart_info->table_handle); 64 1.1 riastrad gart_info->table_handle = NULL; 65 1.1 riastrad } 66 1.1 riastrad 67 1.1 riastrad int drm_ati_pcigart_cleanup(struct drm_device *dev, struct drm_ati_pcigart_info *gart_info) 68 1.1 riastrad { 69 1.1 riastrad struct drm_sg_mem *entry = dev->sg; 70 1.1 riastrad unsigned long pages; 71 1.1 riastrad int i; 72 1.1 riastrad int max_pages; 73 1.1 riastrad 74 1.1 riastrad /* we need to support large memory configurations */ 75 1.1 riastrad if (!entry) { 76 1.1 riastrad DRM_ERROR("no scatter/gather memory!\n"); 77 1.1 riastrad return 0; 78 1.1 riastrad } 79 1.1 riastrad 80 1.1 riastrad if (gart_info->bus_addr) { 81 1.1 riastrad 82 1.1 riastrad max_pages = (gart_info->table_size / sizeof(u32)); 83 1.1 riastrad pages = (entry->pages <= max_pages) 84 1.1 riastrad ? entry->pages : max_pages; 85 1.1 riastrad 86 1.1 riastrad for (i = 0; i < pages; i++) { 87 1.1 riastrad if (!entry->busaddr[i]) 88 1.1 riastrad break; 89 1.1 riastrad pci_unmap_page(dev->pdev, entry->busaddr[i], 90 1.1 riastrad PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); 91 1.1 riastrad } 92 1.1 riastrad 93 1.1 riastrad if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) 94 1.1 riastrad gart_info->bus_addr = 0; 95 1.1 riastrad } 96 1.1 riastrad 97 1.1 riastrad if (gart_info->gart_table_location == DRM_ATI_GART_MAIN && 98 1.1 riastrad gart_info->table_handle) { 99 1.1 riastrad drm_ati_free_pcigart_table(dev, gart_info); 100 1.1 riastrad } 101 1.1 riastrad 102 1.1 riastrad return 1; 103 1.1 riastrad } 104 1.1 riastrad 105 1.1 riastrad int drm_ati_pcigart_init(struct drm_device *dev, struct drm_ati_pcigart_info *gart_info) 106 1.1 riastrad { 107 1.1 riastrad struct drm_local_map *map = &gart_info->mapping; 108 1.1 riastrad struct drm_sg_mem *entry = dev->sg; 109 1.1 riastrad void *address = NULL; 110 1.1 riastrad unsigned long pages; 111 1.1 riastrad u32 *pci_gart = NULL, page_base, gart_idx; 112 1.1 riastrad dma_addr_t bus_address = 0; 113 1.1 riastrad int i, j, ret = -ENOMEM; 114 1.1 riastrad int max_ati_pages, max_real_pages; 115 1.1 riastrad 116 1.1 riastrad if (!entry) { 117 1.1 riastrad DRM_ERROR("no scatter/gather memory!\n"); 118 1.1 riastrad goto done; 119 1.1 riastrad } 120 1.1 riastrad 121 1.1 riastrad if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) { 122 1.1 riastrad DRM_DEBUG("PCI: no table in VRAM: using normal RAM\n"); 123 1.1 riastrad 124 1.1 riastrad if (pci_set_dma_mask(dev->pdev, gart_info->table_mask)) { 125 1.1 riastrad DRM_ERROR("fail to set dma mask to 0x%Lx\n", 126 1.1 riastrad (unsigned long long)gart_info->table_mask); 127 1.1 riastrad ret = -EFAULT; 128 1.1 riastrad goto done; 129 1.1 riastrad } 130 1.1 riastrad 131 1.1 riastrad ret = drm_ati_alloc_pcigart_table(dev, gart_info); 132 1.1 riastrad if (ret) { 133 1.1 riastrad DRM_ERROR("cannot allocate PCI GART page!\n"); 134 1.1 riastrad goto done; 135 1.1 riastrad } 136 1.1 riastrad 137 1.1 riastrad pci_gart = gart_info->table_handle->vaddr; 138 1.1 riastrad address = gart_info->table_handle->vaddr; 139 1.1 riastrad bus_address = gart_info->table_handle->busaddr; 140 1.1 riastrad } else { 141 1.1 riastrad address = gart_info->addr; 142 1.1 riastrad bus_address = gart_info->bus_addr; 143 1.1 riastrad DRM_DEBUG("PCI: Gart Table: VRAM %08LX mapped at %08lX\n", 144 1.1 riastrad (unsigned long long)bus_address, 145 1.1 riastrad (unsigned long)address); 146 1.1 riastrad } 147 1.1 riastrad 148 1.1 riastrad 149 1.1 riastrad max_ati_pages = (gart_info->table_size / sizeof(u32)); 150 1.1 riastrad max_real_pages = max_ati_pages / (PAGE_SIZE / ATI_PCIGART_PAGE_SIZE); 151 1.1 riastrad pages = (entry->pages <= max_real_pages) 152 1.1 riastrad ? entry->pages : max_real_pages; 153 1.1 riastrad 154 1.1 riastrad if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) { 155 1.1 riastrad memset(pci_gart, 0, max_ati_pages * sizeof(u32)); 156 1.1 riastrad } else { 157 1.1 riastrad memset_io((void __iomem *)map->handle, 0, max_ati_pages * sizeof(u32)); 158 1.1 riastrad } 159 1.1 riastrad 160 1.1 riastrad gart_idx = 0; 161 1.1 riastrad for (i = 0; i < pages; i++) { 162 1.1 riastrad /* we need to support large memory configurations */ 163 1.1 riastrad entry->busaddr[i] = pci_map_page(dev->pdev, entry->pagelist[i], 164 1.1 riastrad 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); 165 1.1 riastrad if (pci_dma_mapping_error(dev->pdev, entry->busaddr[i])) { 166 1.1 riastrad DRM_ERROR("unable to map PCIGART pages!\n"); 167 1.1 riastrad drm_ati_pcigart_cleanup(dev, gart_info); 168 1.1 riastrad address = NULL; 169 1.1 riastrad bus_address = 0; 170 1.1 riastrad ret = -ENOMEM; 171 1.1 riastrad goto done; 172 1.1 riastrad } 173 1.1 riastrad page_base = (u32) entry->busaddr[i]; 174 1.1 riastrad 175 1.1 riastrad for (j = 0; j < (PAGE_SIZE / ATI_PCIGART_PAGE_SIZE); j++) { 176 1.1 riastrad u32 offset; 177 1.1 riastrad u32 val; 178 1.1 riastrad 179 1.1 riastrad switch(gart_info->gart_reg_if) { 180 1.1 riastrad case DRM_ATI_GART_IGP: 181 1.1 riastrad val = page_base | 0xc; 182 1.1 riastrad break; 183 1.1 riastrad case DRM_ATI_GART_PCIE: 184 1.1 riastrad val = (page_base >> 8) | 0xc; 185 1.1 riastrad break; 186 1.1 riastrad default: 187 1.1 riastrad case DRM_ATI_GART_PCI: 188 1.1 riastrad val = page_base; 189 1.1 riastrad break; 190 1.1 riastrad } 191 1.1 riastrad if (gart_info->gart_table_location == 192 1.1 riastrad DRM_ATI_GART_MAIN) { 193 1.1 riastrad pci_gart[gart_idx] = cpu_to_le32(val); 194 1.1 riastrad } else { 195 1.1 riastrad offset = gart_idx * sizeof(u32); 196 1.1 riastrad writel(val, (void __iomem *)map->handle + offset); 197 1.1 riastrad } 198 1.1 riastrad gart_idx++; 199 1.1 riastrad page_base += ATI_PCIGART_PAGE_SIZE; 200 1.1 riastrad } 201 1.1 riastrad } 202 1.1 riastrad ret = 0; 203 1.1 riastrad 204 1.1 riastrad #if defined(__i386__) || defined(__x86_64__) 205 1.1 riastrad wbinvd(); 206 1.1 riastrad #else 207 1.1 riastrad mb(); 208 1.1 riastrad #endif 209 1.1 riastrad 210 1.1 riastrad done: 211 1.1 riastrad gart_info->addr = address; 212 1.1 riastrad gart_info->bus_addr = bus_address; 213 1.1 riastrad return ret; 214 1.1 riastrad } 215