blob: 6c4d4b6eba80a94e1cd4d42f746ae22c63aae80c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002 * \file ati_pcigart.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040034#include <linux/export.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Dave Airliefd7e0d72014-09-10 18:23:07 +100037#include <drm/ati_pcigart.h>
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039# define ATI_PCIGART_PAGE_SIZE 4096 /**< PCI GART page size */
40
Dave Airlieb05c2382008-03-17 10:24:24 +100041static int drm_ati_alloc_pcigart_table(struct drm_device *dev,
42 struct drm_ati_pcigart_info *gart_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Dave Airlieb05c2382008-03-17 10:24:24 +100044 gart_info->table_handle = drm_pci_alloc(dev, gart_info->table_size,
Zhenyu Wange6be8d92010-01-05 11:25:05 +080045 PAGE_SIZE);
Dave Airlieb05c2382008-03-17 10:24:24 +100046 if (gart_info->table_handle == NULL)
47 return -ENOMEM;
Dave Airlief2b04cd2007-05-08 15:19:23 +100048
Dave Airlieb05c2382008-03-17 10:24:24 +100049 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
Dave Airlieb05c2382008-03-17 10:24:24 +100052static void drm_ati_free_pcigart_table(struct drm_device *dev,
53 struct drm_ati_pcigart_info *gart_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Dave Airlieb05c2382008-03-17 10:24:24 +100055 drm_pci_free(dev, gart_info->table_handle);
56 gart_info->table_handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
Dave Airlie55910512007-07-11 16:53:40 +100059int drm_ati_pcigart_cleanup(struct drm_device *dev, struct drm_ati_pcigart_info *gart_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Dave Airlie55910512007-07-11 16:53:40 +100061 struct drm_sg_mem *entry = dev->sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 unsigned long pages;
63 int i;
Dave Airlieb05c2382008-03-17 10:24:24 +100064 int max_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 /* we need to support large memory configurations */
Dave Airlieb5e89ed2005-09-25 14:28:13 +100067 if (!entry) {
68 DRM_ERROR("no scatter/gather memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return 0;
70 }
71
Dave Airlieea98a922005-09-11 20:28:11 +100072 if (gart_info->bus_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Dave Airlief2b04cd2007-05-08 15:19:23 +100074 max_pages = (gart_info->table_size / sizeof(u32));
75 pages = (entry->pages <= max_pages)
76 ? entry->pages : max_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Dave Airlieb5e89ed2005-09-25 14:28:13 +100078 for (i = 0; i < pages; i++) {
79 if (!entry->busaddr[i])
80 break;
Dave Airlie7ec700f2008-06-19 11:27:23 +100081 pci_unmap_page(dev->pdev, entry->busaddr[i],
David Miller296c6ae2009-02-12 02:15:34 -080082 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +100084
85 if (gart_info->gart_table_location == DRM_ATI_GART_MAIN)
86 gart_info->bus_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
88
Dave Airlieb05c2382008-03-17 10:24:24 +100089 if (gart_info->gart_table_location == DRM_ATI_GART_MAIN &&
90 gart_info->table_handle) {
91 drm_ati_free_pcigart_table(dev, gart_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
93
94 return 1;
95}
96EXPORT_SYMBOL(drm_ati_pcigart_cleanup);
97
Dave Airlie55910512007-07-11 16:53:40 +100098int drm_ati_pcigart_init(struct drm_device *dev, struct drm_ati_pcigart_info *gart_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
David Miller5a7aad92009-02-12 02:15:27 -0800100 struct drm_local_map *map = &gart_info->mapping;
Dave Airlie55910512007-07-11 16:53:40 +1000101 struct drm_sg_mem *entry = dev->sg;
Dave Airlief26c4732006-01-02 17:18:39 +1100102 void *address = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 unsigned long pages;
Dave Airlie6abf6602009-02-26 10:13:47 +1000104 u32 *pci_gart = NULL, page_base, gart_idx;
Dave Airlieb05c2382008-03-17 10:24:24 +1000105 dma_addr_t bus_address = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 int i, j, ret = 0;
David Millerd30333b2009-02-15 01:08:07 -0800107 int max_ati_pages, max_real_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000109 if (!entry) {
110 DRM_ERROR("no scatter/gather memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 goto done;
112 }
113
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000114 if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) {
Dave Airlieea98a922005-09-11 20:28:11 +1000115 DRM_DEBUG("PCI: no table in VRAM: using normal RAM\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000116
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800117 if (pci_set_dma_mask(dev->pdev, gart_info->table_mask)) {
118 DRM_ERROR("fail to set dma mask to 0x%Lx\n",
Randy Dunlapd7748ba2010-02-02 14:40:33 -0800119 (unsigned long long)gart_info->table_mask);
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800120 ret = 1;
121 goto done;
122 }
123
Dave Airlieb05c2382008-03-17 10:24:24 +1000124 ret = drm_ati_alloc_pcigart_table(dev, gart_info);
125 if (ret) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000126 DRM_ERROR("cannot allocate PCI GART page!\n");
Dave Airlieea98a922005-09-11 20:28:11 +1000127 goto done;
128 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000129
Dave Airlie6abf6602009-02-26 10:13:47 +1000130 pci_gart = gart_info->table_handle->vaddr;
Dave Airlieb05c2382008-03-17 10:24:24 +1000131 address = gart_info->table_handle->vaddr;
132 bus_address = gart_info->table_handle->busaddr;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000133 } else {
Dave Airlieea98a922005-09-11 20:28:11 +1000134 address = gart_info->addr;
135 bus_address = gart_info->bus_addr;
Andrew Mortonf67e74c2008-03-28 14:15:49 -0700136 DRM_DEBUG("PCI: Gart Table: VRAM %08LX mapped at %08lX\n",
137 (unsigned long long)bus_address,
138 (unsigned long)address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
David Millerd30333b2009-02-15 01:08:07 -0800142 max_ati_pages = (gart_info->table_size / sizeof(u32));
143 max_real_pages = max_ati_pages / (PAGE_SIZE / ATI_PCIGART_PAGE_SIZE);
144 pages = (entry->pages <= max_real_pages)
145 ? entry->pages : max_real_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
David Miller5a7aad92009-02-12 02:15:27 -0800147 if (gart_info->gart_table_location == DRM_ATI_GART_MAIN) {
David Millerd30333b2009-02-15 01:08:07 -0800148 memset(pci_gart, 0, max_ati_pages * sizeof(u32));
David Miller5a7aad92009-02-12 02:15:27 -0800149 } else {
Dave Airlie6abf6602009-02-26 10:13:47 +1000150 memset_io((void __iomem *)map->handle, 0, max_ati_pages * sizeof(u32));
David Miller5a7aad92009-02-12 02:15:27 -0800151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
David Miller5a7aad92009-02-12 02:15:27 -0800153 gart_idx = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000154 for (i = 0; i < pages; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 /* we need to support large memory configurations */
Dave Airlie7ec700f2008-06-19 11:27:23 +1000156 entry->busaddr[i] = pci_map_page(dev->pdev, entry->pagelist[i],
David Miller296c6ae2009-02-12 02:15:34 -0800157 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Benjamin Herrenschmidta30f6fb72010-08-10 14:48:58 +1000158 if (pci_dma_mapping_error(dev->pdev, entry->busaddr[i])) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000159 DRM_ERROR("unable to map PCIGART pages!\n");
Dave Airlieea98a922005-09-11 20:28:11 +1000160 drm_ati_pcigart_cleanup(dev, gart_info);
Dave Airlief26c4732006-01-02 17:18:39 +1100161 address = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 bus_address = 0;
163 goto done;
164 }
165 page_base = (u32) entry->busaddr[i];
166
167 for (j = 0; j < (PAGE_SIZE / ATI_PCIGART_PAGE_SIZE); j++) {
David Miller5a7aad92009-02-12 02:15:27 -0800168 u32 val;
169
Dave Airlief2b04cd2007-05-08 15:19:23 +1000170 switch(gart_info->gart_reg_if) {
171 case DRM_ATI_GART_IGP:
David Miller5a7aad92009-02-12 02:15:27 -0800172 val = page_base | 0xc;
Dave Airlief2b04cd2007-05-08 15:19:23 +1000173 break;
174 case DRM_ATI_GART_PCIE:
David Miller5a7aad92009-02-12 02:15:27 -0800175 val = (page_base >> 8) | 0xc;
Dave Airlief2b04cd2007-05-08 15:19:23 +1000176 break;
177 default:
178 case DRM_ATI_GART_PCI:
David Miller5a7aad92009-02-12 02:15:27 -0800179 val = page_base;
Dave Airlief2b04cd2007-05-08 15:19:23 +1000180 break;
181 }
David Miller5a7aad92009-02-12 02:15:27 -0800182 if (gart_info->gart_table_location ==
183 DRM_ATI_GART_MAIN)
184 pci_gart[gart_idx] = cpu_to_le32(val);
185 else
186 DRM_WRITE32(map, gart_idx * sizeof(u32), val);
187 gart_idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 page_base += ATI_PCIGART_PAGE_SIZE;
189 }
190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 ret = 1;
192
193#if defined(__i386__) || defined(__x86_64__)
194 wbinvd();
195#else
196 mb();
197#endif
198
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000199 done:
Dave Airlieea98a922005-09-11 20:28:11 +1000200 gart_info->addr = address;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000201 gart_info->bus_addr = bus_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return ret;
203}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204EXPORT_SYMBOL(drm_ati_pcigart_init);