blob: f299d8a479bbb359d45da12b2743b4d47733afb9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Dynamic DMA mapping support for AMD Hammer.
Ingo Molnar05fccb02008-01-30 13:30:12 +01003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Use the integrated AGP GART in the Hammer northbridge as an IOMMU for PCI.
5 * This allows to use PCI devices that only support 32bit addresses on systems
Ingo Molnar05fccb02008-01-30 13:30:12 +01006 * with more than 4GB.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Paul Bolle395cf962011-08-15 02:02:26 +02008 * See Documentation/DMA-API-HOWTO.txt for the interface specification.
Ingo Molnar05fccb02008-01-30 13:30:12 +01009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Copyright 2002 Andi Kleen, SuSE Labs.
Andi Kleenff7f3642007-10-17 18:04:37 +020011 * Subject to the GNU General Public License v2 only.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/types.h>
15#include <linux/ctype.h>
16#include <linux/agp_backend.h>
17#include <linux/init.h>
18#include <linux/mm.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040019#include <linux/sched.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010020#include <linux/sched/debug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/string.h>
22#include <linux/spinlock.h>
23#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/topology.h>
25#include <linux/interrupt.h>
Akinobu Mitaa66022c2009-12-15 16:48:28 -080026#include <linux/bitmap.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070027#include <linux/kdebug.h>
Jens Axboe9ee1bea2007-10-04 09:35:37 +020028#include <linux/scatterlist.h>
FUJITA Tomonorifde9a102008-02-04 22:28:11 -080029#include <linux/iommu-helper.h>
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +010030#include <linux/syscore_ops.h>
Joerg Roedel237a6222008-09-25 12:13:53 +020031#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/gfp.h>
Arun Sharma600634972011-07-26 16:09:06 -070033#include <linux/atomic.h>
Christoph Hellwigea8c64a2018-01-10 16:21:13 +010034#include <linux/dma-direct.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/mtrr.h>
36#include <asm/pgtable.h>
37#include <asm/proto.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090038#include <asm/iommu.h>
Joerg Roedel395624f2007-10-24 12:49:47 +020039#include <asm/gart.h>
Laura Abbottd1163652017-05-08 15:58:11 -070040#include <asm/set_memory.h>
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010041#include <asm/swiotlb.h>
42#include <asm/dma.h>
Andreas Herrmann23ac4ae2010-09-17 18:03:43 +020043#include <asm/amd_nb.h>
FUJITA Tomonori338bac52009-10-27 16:34:44 +090044#include <asm/x86_init.h>
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -040045#include <asm/iommu_table.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Joerg Roedel79da0872007-10-24 12:49:49 +020047static unsigned long iommu_bus_base; /* GART remapping area (physical) */
Ingo Molnar05fccb02008-01-30 13:30:12 +010048static unsigned long iommu_size; /* size of remapping area bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static unsigned long iommu_pages; /* .. and in pages */
50
Ingo Molnar05fccb02008-01-30 13:30:12 +010051static u32 *iommu_gatt_base; /* Remapping table */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
FUJITA Tomonori42109192009-11-15 21:19:52 +090053static dma_addr_t bad_dma_addr;
54
Ingo Molnar05fccb02008-01-30 13:30:12 +010055/*
56 * If this is disabled the IOMMU will use an optimized flushing strategy
57 * of only flushing when an mapping is reused. With it true the GART is
58 * flushed for every mapping. Problem is that doing the lazy flush seems
59 * to trigger bugs with some popular PCI cards, in particular 3ware (but
60 * has been also also seen with Qlogic at least).
61 */
Jaswinder Singh Rajputc854c912008-12-29 20:38:09 +053062static int iommu_fullflush = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Ingo Molnar05fccb02008-01-30 13:30:12 +010064/* Allocation bitmap for the remapping area: */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static DEFINE_SPINLOCK(iommu_bitmap_lock);
Ingo Molnar05fccb02008-01-30 13:30:12 +010066/* Guarded by iommu_bitmap_lock: */
67static unsigned long *iommu_gart_bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Ingo Molnar05fccb02008-01-30 13:30:12 +010069static u32 gart_unmapped_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71#define GPTE_VALID 1
72#define GPTE_COHERENT 2
73#define GPTE_ENCODE(x) \
74 (((x) & 0xfffff000) | (((x) >> 32) << 4) | GPTE_VALID | GPTE_COHERENT)
75#define GPTE_DECODE(x) (((x) & 0xfffff000) | (((u64)(x) & 0xff0) << 28))
76
Ingo Molnar05fccb02008-01-30 13:30:12 +010077#define EMERGENCY_PAGES 32 /* = 128KB */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79#ifdef CONFIG_AGP
80#define AGPEXTERN extern
81#else
82#define AGPEXTERN
83#endif
84
Joerg Roedel665d3e22011-04-18 15:45:46 +020085/* GART can only remap to physical addresses < 1TB */
86#define GART_MAX_PHYS_ADDR (1ULL << 40)
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/* backdoor interface to AGP driver */
89AGPEXTERN int agp_memory_reserved;
90AGPEXTERN __u32 *agp_gatt_table;
91
92static unsigned long next_bit; /* protected by iommu_bitmap_lock */
Joerg Roedel3610f212008-09-25 12:13:54 +020093static bool need_flush; /* global flush state. set for each gart wrap */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
FUJITA Tomonori7b22ff52008-08-18 00:36:18 +090095static unsigned long alloc_iommu(struct device *dev, int size,
96 unsigned long align_mask)
Ingo Molnar05fccb02008-01-30 13:30:12 +010097{
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 unsigned long offset, flags;
FUJITA Tomonorifde9a102008-02-04 22:28:11 -080099 unsigned long boundary_size;
100 unsigned long base_index;
101
102 base_index = ALIGN(iommu_bus_base & dma_get_seg_boundary(dev),
103 PAGE_SIZE) >> PAGE_SHIFT;
Ingo Molnar123bf0e2009-11-15 21:19:52 +0900104 boundary_size = ALIGN((u64)dma_get_seg_boundary(dev) + 1,
FUJITA Tomonorifde9a102008-02-04 22:28:11 -0800105 PAGE_SIZE) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Ingo Molnar05fccb02008-01-30 13:30:12 +0100107 spin_lock_irqsave(&iommu_bitmap_lock, flags);
FUJITA Tomonorifde9a102008-02-04 22:28:11 -0800108 offset = iommu_area_alloc(iommu_gart_bitmap, iommu_pages, next_bit,
FUJITA Tomonori7b22ff52008-08-18 00:36:18 +0900109 size, base_index, boundary_size, align_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (offset == -1) {
Joerg Roedel3610f212008-09-25 12:13:54 +0200111 need_flush = true;
FUJITA Tomonorifde9a102008-02-04 22:28:11 -0800112 offset = iommu_area_alloc(iommu_gart_bitmap, iommu_pages, 0,
FUJITA Tomonori7b22ff52008-08-18 00:36:18 +0900113 size, base_index, boundary_size,
114 align_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
Ingo Molnar05fccb02008-01-30 13:30:12 +0100116 if (offset != -1) {
Ingo Molnar05fccb02008-01-30 13:30:12 +0100117 next_bit = offset+size;
118 if (next_bit >= iommu_pages) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 next_bit = 0;
Joerg Roedel3610f212008-09-25 12:13:54 +0200120 need_flush = true;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100121 }
122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (iommu_fullflush)
Joerg Roedel3610f212008-09-25 12:13:54 +0200124 need_flush = true;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100125 spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return offset;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100128}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130static void free_iommu(unsigned long offset, int size)
Ingo Molnar05fccb02008-01-30 13:30:12 +0100131{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 unsigned long flags;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 spin_lock_irqsave(&iommu_bitmap_lock, flags);
Akinobu Mitaa66022c2009-12-15 16:48:28 -0800135 bitmap_clear(iommu_gart_bitmap, offset, size);
Joerg Roedel70d7d352008-12-02 20:16:03 +0100136 if (offset >= next_bit)
137 next_bit = offset + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100139}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Ingo Molnar05fccb02008-01-30 13:30:12 +0100141/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 * Use global flush state to avoid races with multiple flushers.
143 */
Andi Kleena32073b2006-06-26 13:56:40 +0200144static void flush_gart(void)
Ingo Molnar05fccb02008-01-30 13:30:12 +0100145{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 unsigned long flags;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 spin_lock_irqsave(&iommu_bitmap_lock, flags);
Andi Kleena32073b2006-06-26 13:56:40 +0200149 if (need_flush) {
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200150 amd_flush_garts();
Joerg Roedel3610f212008-09-25 12:13:54 +0200151 need_flush = false;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100154}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#ifdef CONFIG_IOMMU_LEAK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/* Debugging aid for drivers that don't free their IOMMU tables */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static int leak_trace;
Joerg Roedel79da0872007-10-24 12:49:49 +0200159static int iommu_leak_pages = 20;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100160
Joerg Roedel79da0872007-10-24 12:49:49 +0200161static void dump_leak(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Ingo Molnar05fccb02008-01-30 13:30:12 +0100163 static int dump;
164
FUJITA Tomonori19c1a6f2009-04-14 09:43:19 +0900165 if (dump)
Ingo Molnar05fccb02008-01-30 13:30:12 +0100166 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 dump = 1;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100168
FUJITA Tomonori19c1a6f2009-04-14 09:43:19 +0900169 show_stack(NULL, NULL);
170 debug_dma_dump_mappings(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172#endif
173
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100174static void iommu_full(struct device *dev, size_t size, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Ingo Molnar05fccb02008-01-30 13:30:12 +0100176 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 * Ran out of IOMMU space for this operation. This is very bad.
178 * Unfortunately the drivers cannot handle this operation properly.
Ingo Molnar05fccb02008-01-30 13:30:12 +0100179 * Return some non mapped prereserved space in the aperture and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * let the Northbridge deal with it. This will result in garbage
181 * in the IO operation. When the size exceeds the prereserved space
Ingo Molnar05fccb02008-01-30 13:30:12 +0100182 * memory corruption will occur or random memory will be DMAed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * out. Hopefully no network devices use single mappings that big.
Ingo Molnar05fccb02008-01-30 13:30:12 +0100184 */
185
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200186 dev_err(dev, "PCI-DMA: Out of IOMMU space for %lu bytes\n", size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100188 if (size > PAGE_SIZE*EMERGENCY_PAGES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (dir == PCI_DMA_FROMDEVICE || dir == PCI_DMA_BIDIRECTIONAL)
190 panic("PCI-DMA: Memory would be corrupted\n");
Ingo Molnar05fccb02008-01-30 13:30:12 +0100191 if (dir == PCI_DMA_TODEVICE || dir == PCI_DMA_BIDIRECTIONAL)
192 panic(KERN_ERR
193 "PCI-DMA: Random memory would be DMAed\n");
194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195#ifdef CONFIG_IOMMU_LEAK
Ingo Molnar05fccb02008-01-30 13:30:12 +0100196 dump_leak();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Ingo Molnar05fccb02008-01-30 13:30:12 +0100200static inline int
201need_iommu(struct device *dev, unsigned long addr, size_t size)
202{
FUJITA Tomonoria4c2baa2009-07-10 10:04:55 +0900203 return force_iommu || !dma_capable(dev, addr, size);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100204}
205
206static inline int
207nonforced_iommu(struct device *dev, unsigned long addr, size_t size)
208{
FUJITA Tomonoria4c2baa2009-07-10 10:04:55 +0900209 return !dma_capable(dev, addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212/* Map a single continuous physical area into the IOMMU.
213 * Caller needs to check if the iommu is needed and flush.
214 */
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100215static dma_addr_t dma_map_area(struct device *dev, dma_addr_t phys_mem,
FUJITA Tomonori7b22ff52008-08-18 00:36:18 +0900216 size_t size, int dir, unsigned long align_mask)
Ingo Molnar05fccb02008-01-30 13:30:12 +0100217{
Joerg Roedel1477b8e2008-10-15 22:02:11 -0700218 unsigned long npages = iommu_num_pages(phys_mem, size, PAGE_SIZE);
Joerg Roedel665d3e22011-04-18 15:45:46 +0200219 unsigned long iommu_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 int i;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100221
Joerg Roedel665d3e22011-04-18 15:45:46 +0200222 if (unlikely(phys_mem + size > GART_MAX_PHYS_ADDR))
223 return bad_dma_addr;
224
225 iommu_page = alloc_iommu(dev, npages, align_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (iommu_page == -1) {
227 if (!nonforced_iommu(dev, phys_mem, size))
Ingo Molnar05fccb02008-01-30 13:30:12 +0100228 return phys_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (panic_on_overflow)
230 panic("dma_map_area overflow %lu bytes\n", size);
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100231 iommu_full(dev, size, dir);
FUJITA Tomonori42109192009-11-15 21:19:52 +0900232 return bad_dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234
235 for (i = 0; i < npages; i++) {
236 iommu_gatt_base[iommu_page + i] = GPTE_ENCODE(phys_mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 phys_mem += PAGE_SIZE;
238 }
239 return iommu_bus_base + iommu_page*PAGE_SIZE + (phys_mem & ~PAGE_MASK);
240}
241
242/* Map a single area into the IOMMU */
FUJITA Tomonori052aedb2009-01-05 23:47:23 +0900243static dma_addr_t gart_map_page(struct device *dev, struct page *page,
244 unsigned long offset, size_t size,
245 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700246 unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Ingo Molnar2be62142008-04-19 19:19:56 +0200248 unsigned long bus;
FUJITA Tomonori052aedb2009-01-05 23:47:23 +0900249 phys_addr_t paddr = page_to_phys(page) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (!dev)
Joerg Roedel6c505ce2008-08-19 16:32:45 +0200252 dev = &x86_dma_fallback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Ingo Molnar2be62142008-04-19 19:19:56 +0200254 if (!need_iommu(dev, paddr, size))
255 return paddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
FUJITA Tomonori7b22ff52008-08-18 00:36:18 +0900257 bus = dma_map_area(dev, paddr, size, dir, 0);
258 flush_gart();
Ingo Molnar05fccb02008-01-30 13:30:12 +0100259
260 return bus;
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100261}
262
263/*
Jon Mason7c2d9cd2006-06-26 13:56:37 +0200264 * Free a DMA mapping.
265 */
FUJITA Tomonori052aedb2009-01-05 23:47:23 +0900266static void gart_unmap_page(struct device *dev, dma_addr_t dma_addr,
267 size_t size, enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700268 unsigned long attrs)
Jon Mason7c2d9cd2006-06-26 13:56:37 +0200269{
270 unsigned long iommu_page;
271 int npages;
272 int i;
273
274 if (dma_addr < iommu_bus_base + EMERGENCY_PAGES*PAGE_SIZE ||
275 dma_addr >= iommu_bus_base + iommu_size)
276 return;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100277
Jon Mason7c2d9cd2006-06-26 13:56:37 +0200278 iommu_page = (dma_addr - iommu_bus_base)>>PAGE_SHIFT;
Joerg Roedel1477b8e2008-10-15 22:02:11 -0700279 npages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
Jon Mason7c2d9cd2006-06-26 13:56:37 +0200280 for (i = 0; i < npages; i++) {
281 iommu_gatt_base[iommu_page + i] = gart_unmapped_entry;
Jon Mason7c2d9cd2006-06-26 13:56:37 +0200282 }
283 free_iommu(iommu_page, npages);
284}
285
286/*
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100287 * Wrapper for pci_unmap_single working with scatterlists.
288 */
FUJITA Tomonori160c1d82009-01-05 23:59:02 +0900289static void gart_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700290 enum dma_data_direction dir, unsigned long attrs)
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100291{
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200292 struct scatterlist *s;
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100293 int i;
294
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200295 for_each_sg(sg, s, nents, i) {
Jon Mason60b08c62006-02-26 04:18:22 +0100296 if (!s->dma_length || !s->length)
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100297 break;
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700298 gart_unmap_page(dev, s->dma_address, s->dma_length, dir, 0);
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100299 }
300}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302/* Fallback for dma_map_sg in case of overflow */
303static int dma_map_sg_nonforce(struct device *dev, struct scatterlist *sg,
304 int nents, int dir)
305{
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200306 struct scatterlist *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 int i;
308
309#ifdef CONFIG_IOMMU_DEBUG
Ingo Molnar123bf0e2009-11-15 21:19:52 +0900310 pr_debug("dma_map_sg overflow\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311#endif
312
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200313 for_each_sg(sg, s, nents, i) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200314 unsigned long addr = sg_phys(s);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100315
316 if (nonforced_iommu(dev, addr, s->length)) {
FUJITA Tomonori7b22ff52008-08-18 00:36:18 +0900317 addr = dma_map_area(dev, addr, s->length, dir, 0);
FUJITA Tomonori42109192009-11-15 21:19:52 +0900318 if (addr == bad_dma_addr) {
Ingo Molnar05fccb02008-01-30 13:30:12 +0100319 if (i > 0)
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700320 gart_unmap_sg(dev, sg, i, dir, 0);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100321 nents = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 sg[0].dma_length = 0;
323 break;
324 }
325 }
326 s->dma_address = addr;
327 s->dma_length = s->length;
328 }
Andi Kleena32073b2006-06-26 13:56:40 +0200329 flush_gart();
Ingo Molnar05fccb02008-01-30 13:30:12 +0100330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return nents;
332}
333
334/* Map multiple scatterlist entries continuous into the first. */
FUJITA Tomonorifde9a102008-02-04 22:28:11 -0800335static int __dma_map_cont(struct device *dev, struct scatterlist *start,
336 int nelems, struct scatterlist *sout,
337 unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
FUJITA Tomonori7b22ff52008-08-18 00:36:18 +0900339 unsigned long iommu_start = alloc_iommu(dev, pages, 0);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100340 unsigned long iommu_page = iommu_start;
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200341 struct scatterlist *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 int i;
343
344 if (iommu_start == -1)
345 return -1;
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200346
347 for_each_sg(start, s, nelems, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 unsigned long pages, addr;
349 unsigned long phys_addr = s->dma_address;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100350
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200351 BUG_ON(s != start && s->offset);
352 if (s == start) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 sout->dma_address = iommu_bus_base;
354 sout->dma_address += iommu_page*PAGE_SIZE + s->offset;
355 sout->dma_length = s->length;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100356 } else {
357 sout->dma_length += s->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359
360 addr = phys_addr;
Joerg Roedel1477b8e2008-10-15 22:02:11 -0700361 pages = iommu_num_pages(s->offset, s->length, PAGE_SIZE);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100362 while (pages--) {
363 iommu_gatt_base[iommu_page] = GPTE_ENCODE(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 addr += PAGE_SIZE;
365 iommu_page++;
Andi Kleen0d5410642006-02-12 14:34:59 -0800366 }
Ingo Molnar05fccb02008-01-30 13:30:12 +0100367 }
368 BUG_ON(iommu_page - iommu_start != pages);
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return 0;
371}
372
Ingo Molnar05fccb02008-01-30 13:30:12 +0100373static inline int
FUJITA Tomonorifde9a102008-02-04 22:28:11 -0800374dma_map_cont(struct device *dev, struct scatterlist *start, int nelems,
375 struct scatterlist *sout, unsigned long pages, int need)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200377 if (!need) {
378 BUG_ON(nelems != 1);
FUJITA Tomonorie88a39d2007-10-25 09:13:32 +0200379 sout->dma_address = start->dma_address;
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200380 sout->dma_length = start->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return 0;
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200382 }
FUJITA Tomonorifde9a102008-02-04 22:28:11 -0800383 return __dma_map_cont(dev, start, nelems, sout, pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
Ingo Molnar05fccb02008-01-30 13:30:12 +0100385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386/*
387 * DMA map all entries in a scatterlist.
Ingo Molnar05fccb02008-01-30 13:30:12 +0100388 * Merge chunks that have page aligned sizes into a continuous mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 */
FUJITA Tomonori160c1d82009-01-05 23:59:02 +0900390static int gart_map_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700391 enum dma_data_direction dir, unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200393 struct scatterlist *s, *ps, *start_sg, *sgmap;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100394 int need = 0, nextneed, i, out, start;
395 unsigned long pages = 0;
FUJITA Tomonori42d00282008-02-04 22:27:56 -0800396 unsigned int seg_size;
397 unsigned int max_seg_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Ingo Molnar05fccb02008-01-30 13:30:12 +0100399 if (nents == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return 0;
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (!dev)
Joerg Roedel6c505ce2008-08-19 16:32:45 +0200403 dev = &x86_dma_fallback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Ingo Molnar123bf0e2009-11-15 21:19:52 +0900405 out = 0;
406 start = 0;
407 start_sg = sg;
408 sgmap = sg;
409 seg_size = 0;
410 max_seg_size = dma_get_max_seg_size(dev);
411 ps = NULL; /* shut up gcc */
412
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200413 for_each_sg(sg, s, nents, i) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200414 dma_addr_t addr = sg_phys(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Ingo Molnar05fccb02008-01-30 13:30:12 +0100416 s->dma_address = addr;
417 BUG_ON(s->length == 0);
418
419 nextneed = need_iommu(dev, addr, s->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 /* Handle the previous not yet processed entries */
422 if (i > start) {
Ingo Molnar05fccb02008-01-30 13:30:12 +0100423 /*
424 * Can only merge when the last chunk ends on a
425 * page boundary and the new one doesn't have an
426 * offset.
427 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (!iommu_merge || !nextneed || !need || s->offset ||
FUJITA Tomonori42d00282008-02-04 22:27:56 -0800429 (s->length + seg_size > max_seg_size) ||
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200430 (ps->offset + ps->length) % PAGE_SIZE) {
FUJITA Tomonorifde9a102008-02-04 22:28:11 -0800431 if (dma_map_cont(dev, start_sg, i - start,
432 sgmap, pages, need) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 goto error;
434 out++;
Ingo Molnar123bf0e2009-11-15 21:19:52 +0900435
436 seg_size = 0;
437 sgmap = sg_next(sgmap);
438 pages = 0;
439 start = i;
440 start_sg = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442 }
443
FUJITA Tomonori42d00282008-02-04 22:27:56 -0800444 seg_size += s->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 need = nextneed;
Joerg Roedel1477b8e2008-10-15 22:02:11 -0700446 pages += iommu_num_pages(s->offset, s->length, PAGE_SIZE);
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200447 ps = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
FUJITA Tomonorifde9a102008-02-04 22:28:11 -0800449 if (dma_map_cont(dev, start_sg, i - start, sgmap, pages, need) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 goto error;
451 out++;
Andi Kleena32073b2006-06-26 13:56:40 +0200452 flush_gart();
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200453 if (out < nents) {
454 sgmap = sg_next(sgmap);
455 sgmap->dma_length = 0;
456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return out;
458
459error:
Andi Kleena32073b2006-06-26 13:56:40 +0200460 flush_gart();
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700461 gart_unmap_sg(dev, sg, out, dir, 0);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100462
Kevin VanMarena1002a42006-02-03 21:51:32 +0100463 /* When it was forced or merged try again in a dumb way */
464 if (force_iommu || iommu_merge) {
465 out = dma_map_sg_nonforce(dev, sg, nents, dir);
466 if (out > 0)
467 return out;
468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (panic_on_overflow)
470 panic("dma_map_sg: overflow on %lu pages\n", pages);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100471
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100472 iommu_full(dev, pages << PAGE_SHIFT, dir);
Jens Axboe9ee1bea2007-10-04 09:35:37 +0200473 for_each_sg(sg, s, nents, i)
FUJITA Tomonori42109192009-11-15 21:19:52 +0900474 s->dma_address = bad_dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return 0;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100476}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Joerg Roedel94581092008-08-19 16:32:39 +0200478/* allocate and map a coherent mapping */
479static void *
480gart_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700481 gfp_t flag, unsigned long attrs)
Joerg Roedel94581092008-08-19 16:32:39 +0200482{
Christoph Hellwig51c7eeb2018-03-19 11:38:18 +0100483 void *vaddr;
Joerg Roedel94581092008-08-19 16:32:39 +0200484
Christoph Hellwig51c7eeb2018-03-19 11:38:18 +0100485 vaddr = dma_direct_alloc(dev, size, dma_addr, flag, attrs);
486 if (!vaddr ||
487 !force_iommu || dev->coherent_dma_mask <= DMA_BIT_MASK(24))
488 return vaddr;
Joerg Roedel94581092008-08-19 16:32:39 +0200489
Christoph Hellwig51c7eeb2018-03-19 11:38:18 +0100490 *dma_addr = dma_map_area(dev, virt_to_phys(vaddr), size,
491 DMA_BIDIRECTIONAL, (1UL << get_order(size)) - 1);
492 flush_gart();
493 if (unlikely(*dma_addr == bad_dma_addr))
494 goto out_free;
495 return vaddr;
496out_free:
497 dma_direct_free(dev, size, vaddr, *dma_addr, attrs);
Joerg Roedel94581092008-08-19 16:32:39 +0200498 return NULL;
499}
500
Joerg Roedel43a5a5a2008-08-19 16:32:40 +0200501/* free a coherent mapping */
502static void
503gart_free_coherent(struct device *dev, size_t size, void *vaddr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700504 dma_addr_t dma_addr, unsigned long attrs)
Joerg Roedel43a5a5a2008-08-19 16:32:40 +0200505{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700506 gart_unmap_page(dev, dma_addr, size, DMA_BIDIRECTIONAL, 0);
Christoph Hellwigfec777c2018-03-19 11:38:15 +0100507 dma_direct_free(dev, size, vaddr, dma_addr, attrs);
Joerg Roedel43a5a5a2008-08-19 16:32:40 +0200508}
509
FUJITA Tomonori42109192009-11-15 21:19:52 +0900510static int gart_mapping_error(struct device *dev, dma_addr_t dma_addr)
511{
512 return (dma_addr == bad_dma_addr);
513}
514
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100515static int no_agp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517static __init unsigned long check_iommu_size(unsigned long aper, u64 aper_size)
Ingo Molnar05fccb02008-01-30 13:30:12 +0100518{
519 unsigned long a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Ingo Molnar05fccb02008-01-30 13:30:12 +0100521 if (!iommu_size) {
522 iommu_size = aper_size;
523 if (!no_agp)
524 iommu_size /= 2;
525 }
526
527 a = aper + iommu_size;
Andi Kleen31422c52008-02-04 16:48:08 +0100528 iommu_size -= round_up(a, PMD_PAGE_SIZE) - a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Ingo Molnar05fccb02008-01-30 13:30:12 +0100530 if (iommu_size < 64*1024*1024) {
Ingo Molnar123bf0e2009-11-15 21:19:52 +0900531 pr_warning(
Ingo Molnar05fccb02008-01-30 13:30:12 +0100532 "PCI-DMA: Warning: Small IOMMU %luMB."
533 " Consider increasing the AGP aperture in BIOS\n",
534 iommu_size >> 20);
535 }
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return iommu_size;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100538}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Ingo Molnar05fccb02008-01-30 13:30:12 +0100540static __init unsigned read_aperture(struct pci_dev *dev, u32 *size)
541{
542 unsigned aper_size = 0, aper_base_32, aper_order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 u64 aper_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Pavel Machek3bb6fbf2008-04-15 12:43:57 +0200545 pci_read_config_dword(dev, AMD64_GARTAPERTUREBASE, &aper_base_32);
546 pci_read_config_dword(dev, AMD64_GARTAPERTURECTL, &aper_order);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100547 aper_order = (aper_order >> 1) & 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Ingo Molnar05fccb02008-01-30 13:30:12 +0100549 aper_base = aper_base_32 & 0x7fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 aper_base <<= 25;
551
Ingo Molnar05fccb02008-01-30 13:30:12 +0100552 aper_size = (32 * 1024 * 1024) << aper_order;
553 if (aper_base + aper_size > 0x100000000UL || !aper_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 aper_base = 0;
555
556 *size = aper_size;
557 return aper_base;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100558}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Rafael J. Wysocki6703f6d2008-06-10 00:10:48 +0200560static void enable_gart_translations(void)
561{
562 int i;
563
Hans Rosenfeld9653a5c2010-10-29 17:14:31 +0200564 if (!amd_nb_has_feature(AMD_NB_GART))
Andreas Herrmann900f9ac2010-09-17 18:02:54 +0200565 return;
566
Hans Rosenfeld9653a5c2010-10-29 17:14:31 +0200567 for (i = 0; i < amd_nb_num(); i++) {
568 struct pci_dev *dev = node_to_amd_nb(i)->misc;
Rafael J. Wysocki6703f6d2008-06-10 00:10:48 +0200569
570 enable_gart_translation(dev, __pa(agp_gatt_table));
571 }
Joerg Roedel4b838732010-04-07 12:57:35 +0200572
573 /* Flush the GART-TLB to remove stale entries */
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200574 amd_flush_garts();
Rafael J. Wysocki6703f6d2008-06-10 00:10:48 +0200575}
576
577/*
578 * If fix_up_north_bridges is set, the north bridges have to be fixed up on
579 * resume in the same way as they are handled in gart_iommu_hole_init().
580 */
581static bool fix_up_north_bridges;
582static u32 aperture_order;
583static u32 aperture_alloc;
584
585void set_up_gart_resume(u32 aper_order, u32 aper_alloc)
586{
587 fix_up_north_bridges = true;
588 aperture_order = aper_order;
589 aperture_alloc = aper_alloc;
590}
591
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100592static void gart_fixup_northbridges(void)
Ingo Molnar123bf0e2009-11-15 21:19:52 +0900593{
594 int i;
595
596 if (!fix_up_north_bridges)
597 return;
598
Hans Rosenfeld9653a5c2010-10-29 17:14:31 +0200599 if (!amd_nb_has_feature(AMD_NB_GART))
Andreas Herrmann900f9ac2010-09-17 18:02:54 +0200600 return;
601
Ingo Molnar123bf0e2009-11-15 21:19:52 +0900602 pr_info("PCI-DMA: Restoring GART aperture settings\n");
603
Hans Rosenfeld9653a5c2010-10-29 17:14:31 +0200604 for (i = 0; i < amd_nb_num(); i++) {
605 struct pci_dev *dev = node_to_amd_nb(i)->misc;
Ingo Molnar123bf0e2009-11-15 21:19:52 +0900606
607 /*
608 * Don't enable translations just yet. That is the next
609 * step. Restore the pre-suspend aperture settings.
610 */
Borislav Petkov260133a2010-09-03 18:39:40 +0200611 gart_set_size_and_enable(dev, aperture_order);
Ingo Molnar123bf0e2009-11-15 21:19:52 +0900612 pci_write_config_dword(dev, AMD64_GARTAPERTUREBASE, aperture_alloc >> 25);
613 }
614}
615
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100616static void gart_resume(void)
Pavel Machekcd763742008-05-29 00:30:21 -0700617{
Ingo Molnar123bf0e2009-11-15 21:19:52 +0900618 pr_info("PCI-DMA: Resuming GART IOMMU\n");
Rafael J. Wysocki6703f6d2008-06-10 00:10:48 +0200619
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100620 gart_fixup_northbridges();
Rafael J. Wysocki6703f6d2008-06-10 00:10:48 +0200621
622 enable_gart_translations();
Pavel Machekcd763742008-05-29 00:30:21 -0700623}
624
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100625static struct syscore_ops gart_syscore_ops = {
Ingo Molnar123bf0e2009-11-15 21:19:52 +0900626 .resume = gart_resume,
Pavel Machekcd763742008-05-29 00:30:21 -0700627
628};
629
Ingo Molnar05fccb02008-01-30 13:30:12 +0100630/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 * Private Northbridge GATT initialization in case we cannot use the
Ingo Molnar05fccb02008-01-30 13:30:12 +0100632 * AGP driver for some reason.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 */
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200634static __init int init_amd_gatt(struct agp_kern_info *info)
Ingo Molnar05fccb02008-01-30 13:30:12 +0100635{
636 unsigned aper_size, gatt_size, new_aper_size;
637 unsigned aper_base, new_aper_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 struct pci_dev *dev;
639 void *gatt;
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100640 int i;
Andi Kleena32073b2006-06-26 13:56:40 +0200641
Ingo Molnar123bf0e2009-11-15 21:19:52 +0900642 pr_info("PCI-DMA: Disabling AGP.\n");
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 aper_size = aper_base = info->aper_size = 0;
Andi Kleena32073b2006-06-26 13:56:40 +0200645 dev = NULL;
Hans Rosenfeld9653a5c2010-10-29 17:14:31 +0200646 for (i = 0; i < amd_nb_num(); i++) {
647 dev = node_to_amd_nb(i)->misc;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100648 new_aper_base = read_aperture(dev, &new_aper_size);
649 if (!new_aper_base)
650 goto nommu;
651
652 if (!aper_base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 aper_size = new_aper_size;
654 aper_base = new_aper_base;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100655 }
656 if (aper_size != new_aper_size || aper_base != new_aper_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 goto nommu;
658 }
659 if (!aper_base)
Ingo Molnar05fccb02008-01-30 13:30:12 +0100660 goto nommu;
Ingo Molnar123bf0e2009-11-15 21:19:52 +0900661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 info->aper_base = aper_base;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100663 info->aper_size = aper_size >> 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Ingo Molnar05fccb02008-01-30 13:30:12 +0100665 gatt_size = (aper_size >> PAGE_SHIFT) * sizeof(u32);
Joerg Roedel01142672008-09-25 12:42:12 +0200666 gatt = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
667 get_order(gatt_size));
Ingo Molnar05fccb02008-01-30 13:30:12 +0100668 if (!gatt)
Joachim Deguaracf6387d2007-04-24 13:05:36 +0200669 panic("Cannot allocate GATT table");
Arjan van de Ven6d238cc2008-01-30 13:34:06 +0100670 if (set_memory_uc((unsigned long)gatt, gatt_size >> PAGE_SHIFT))
Joachim Deguaracf6387d2007-04-24 13:05:36 +0200671 panic("Could not set GART PTEs to uncacheable pages");
Joachim Deguaracf6387d2007-04-24 13:05:36 +0200672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 agp_gatt_table = gatt;
Andi Kleena32073b2006-06-26 13:56:40 +0200674
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +0100675 register_syscore_ops(&gart_syscore_ops);
Rafael J. Wysocki6703f6d2008-06-10 00:10:48 +0200676
Andi Kleena32073b2006-06-26 13:56:40 +0200677 flush_gart();
Ingo Molnar05fccb02008-01-30 13:30:12 +0100678
Ingo Molnar123bf0e2009-11-15 21:19:52 +0900679 pr_info("PCI-DMA: aperture base @ %x size %u KB\n",
Ingo Molnar05fccb02008-01-30 13:30:12 +0100680 aper_base, aper_size>>10);
Yinghai Lu7ab073b2008-07-12 14:30:35 -0700681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return 0;
683
684 nommu:
Ingo Molnar05fccb02008-01-30 13:30:12 +0100685 /* Should not happen anymore */
Ingo Molnar123bf0e2009-11-15 21:19:52 +0900686 pr_warning("PCI-DMA: More than 4GB of RAM and no IOMMU\n"
Joe Perchesad361c92009-07-06 13:05:40 -0700687 "falling back to iommu=soft.\n");
Ingo Molnar05fccb02008-01-30 13:30:12 +0100688 return -1;
689}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Bart Van Assche52997092017-01-20 13:04:01 -0800691static const struct dma_map_ops gart_dma_ops = {
Ingo Molnar05fccb02008-01-30 13:30:12 +0100692 .map_sg = gart_map_sg,
693 .unmap_sg = gart_unmap_sg,
FUJITA Tomonori052aedb2009-01-05 23:47:23 +0900694 .map_page = gart_map_page,
695 .unmap_page = gart_unmap_page,
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +0200696 .alloc = gart_alloc_coherent,
697 .free = gart_free_coherent,
FUJITA Tomonori42109192009-11-15 21:19:52 +0900698 .mapping_error = gart_mapping_error,
Christoph Hellwigfec777c2018-03-19 11:38:15 +0100699 .dma_supported = dma_direct_supported,
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100700};
701
FUJITA Tomonori338bac52009-10-27 16:34:44 +0900702static void gart_iommu_shutdown(void)
Yinghai Lubc2cea62007-07-21 17:11:28 +0200703{
704 struct pci_dev *dev;
705 int i;
706
Yinghai Luf3eee542009-12-14 11:52:15 +0900707 /* don't shutdown it if there is AGP installed */
708 if (!no_agp)
Yinghai Lubc2cea62007-07-21 17:11:28 +0200709 return;
710
Hans Rosenfeld9653a5c2010-10-29 17:14:31 +0200711 if (!amd_nb_has_feature(AMD_NB_GART))
Andreas Herrmann900f9ac2010-09-17 18:02:54 +0200712 return;
713
Hans Rosenfeld9653a5c2010-10-29 17:14:31 +0200714 for (i = 0; i < amd_nb_num(); i++) {
Ingo Molnar05fccb02008-01-30 13:30:12 +0100715 u32 ctl;
Yinghai Lubc2cea62007-07-21 17:11:28 +0200716
Hans Rosenfeld9653a5c2010-10-29 17:14:31 +0200717 dev = node_to_amd_nb(i)->misc;
Pavel Machek3bb6fbf2008-04-15 12:43:57 +0200718 pci_read_config_dword(dev, AMD64_GARTAPERTURECTL, &ctl);
Yinghai Lubc2cea62007-07-21 17:11:28 +0200719
Pavel Machek3bb6fbf2008-04-15 12:43:57 +0200720 ctl &= ~GARTEN;
Yinghai Lubc2cea62007-07-21 17:11:28 +0200721
Pavel Machek3bb6fbf2008-04-15 12:43:57 +0200722 pci_write_config_dword(dev, AMD64_GARTAPERTURECTL, ctl);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100723 }
Yinghai Lubc2cea62007-07-21 17:11:28 +0200724}
725
FUJITA Tomonoride957622009-11-10 19:46:14 +0900726int __init gart_iommu_init(void)
Ingo Molnar05fccb02008-01-30 13:30:12 +0100727{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 struct agp_kern_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 unsigned long iommu_start;
Yinghai Lud99e9012008-10-04 15:55:12 -0700730 unsigned long aper_base, aper_size;
731 unsigned long start_pfn, end_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 unsigned long scratch;
733 long i;
734
Hans Rosenfeld9653a5c2010-10-29 17:14:31 +0200735 if (!amd_nb_has_feature(AMD_NB_GART))
FUJITA Tomonoride957622009-11-10 19:46:14 +0900736 return 0;
Andi Kleena32073b2006-06-26 13:56:40 +0200737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738#ifndef CONFIG_AGP_AMD64
Ingo Molnar05fccb02008-01-30 13:30:12 +0100739 no_agp = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740#else
741 /* Makefile puts PCI initialization via subsys_initcall first. */
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200742 /* Add other AMD AGP bridge drivers here */
Ingo Molnar05fccb02008-01-30 13:30:12 +0100743 no_agp = no_agp ||
744 (agp_amd64_init() < 0) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 (agp_copy_info(agp_bridge, &info) < 0);
Ingo Molnar05fccb02008-01-30 13:30:12 +0100746#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (no_iommu ||
Yinghai Luc987d122008-06-24 22:14:09 -0700749 (!force_iommu && max_pfn <= MAX_DMA32_PFN) ||
Joerg Roedel0440d4c2007-10-24 12:49:50 +0200750 !gart_iommu_aperture ||
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200751 (no_agp && init_amd_gatt(&info) < 0)) {
Yinghai Luc987d122008-06-24 22:14:09 -0700752 if (max_pfn > MAX_DMA32_PFN) {
Ingo Molnar123bf0e2009-11-15 21:19:52 +0900753 pr_warning("More than 4GB of memory but GART IOMMU not available.\n");
754 pr_warning("falling back to iommu=soft.\n");
Jon Mason5b7b6442006-02-03 21:51:59 +0100755 }
FUJITA Tomonoride957622009-11-10 19:46:14 +0900756 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758
Yinghai Lud99e9012008-10-04 15:55:12 -0700759 /* need to map that range */
Ingo Molnar123bf0e2009-11-15 21:19:52 +0900760 aper_size = info.aper_size << 20;
761 aper_base = info.aper_base;
762 end_pfn = (aper_base>>PAGE_SHIFT) + (aper_size>>PAGE_SHIFT);
763
Yinghai Lu51017302012-11-16 19:38:50 -0800764 start_pfn = PFN_DOWN(aper_base);
765 if (!pfn_range_is_mapped(start_pfn, end_pfn))
Yinghai Lud99e9012008-10-04 15:55:12 -0700766 init_memory_mapping(start_pfn<<PAGE_SHIFT, end_pfn<<PAGE_SHIFT);
Yinghai Lud99e9012008-10-04 15:55:12 -0700767
Ingo Molnar123bf0e2009-11-15 21:19:52 +0900768 pr_info("PCI-DMA: using GART IOMMU.\n");
Ingo Molnar05fccb02008-01-30 13:30:12 +0100769 iommu_size = check_iommu_size(info.aper_base, aper_size);
770 iommu_pages = iommu_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Joerg Roedel01142672008-09-25 12:42:12 +0200772 iommu_gart_bitmap = (void *) __get_free_pages(GFP_KERNEL | __GFP_ZERO,
Ingo Molnar05fccb02008-01-30 13:30:12 +0100773 get_order(iommu_pages/8));
774 if (!iommu_gart_bitmap)
775 panic("Cannot allocate iommu bitmap\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777#ifdef CONFIG_IOMMU_LEAK
Ingo Molnar05fccb02008-01-30 13:30:12 +0100778 if (leak_trace) {
FUJITA Tomonori19c1a6f2009-04-14 09:43:19 +0900779 int ret;
780
781 ret = dma_debug_resize_entries(iommu_pages);
782 if (ret)
Ingo Molnar123bf0e2009-11-15 21:19:52 +0900783 pr_debug("PCI-DMA: Cannot trace all the entries\n");
Ingo Molnar05fccb02008-01-30 13:30:12 +0100784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785#endif
786
Ingo Molnar05fccb02008-01-30 13:30:12 +0100787 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 * Out of IOMMU space handling.
Ingo Molnar05fccb02008-01-30 13:30:12 +0100789 * Reserve some invalid pages at the beginning of the GART.
790 */
Akinobu Mitaa66022c2009-12-15 16:48:28 -0800791 bitmap_set(iommu_gart_bitmap, 0, EMERGENCY_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Ingo Molnar123bf0e2009-11-15 21:19:52 +0900793 pr_info("PCI-DMA: Reserving %luMB of IOMMU area in the AGP aperture\n",
Ingo Molnar05fccb02008-01-30 13:30:12 +0100794 iommu_size >> 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Ingo Molnar123bf0e2009-11-15 21:19:52 +0900796 agp_memory_reserved = iommu_size;
797 iommu_start = aper_size - iommu_size;
798 iommu_bus_base = info.aper_base + iommu_start;
799 bad_dma_addr = iommu_bus_base;
800 iommu_gatt_base = agp_gatt_table + (iommu_start>>PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Ingo Molnar05fccb02008-01-30 13:30:12 +0100802 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 * Unmap the IOMMU part of the GART. The alias of the page is
804 * always mapped with cache enabled and there is no full cache
805 * coherency across the GART remapping. The unmapping avoids
806 * automatic prefetches from the CPU allocating cache lines in
807 * there. All CPU accesses are done via the direct mapping to
808 * the backing memory. The GART address is only used by PCI
Ingo Molnar05fccb02008-01-30 13:30:12 +0100809 * devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 */
Andi Kleen28d6ee42008-02-04 16:48:08 +0100811 set_memory_np((unsigned long)__va(iommu_bus_base),
812 iommu_size >> PAGE_SHIFT);
Ingo Molnar184652e2008-02-14 23:30:20 +0100813 /*
814 * Tricky. The GART table remaps the physical memory range,
815 * so the CPU wont notice potential aliases and if the memory
816 * is remapped to UC later on, we might surprise the PCI devices
817 * with a stray writeout of a cacheline. So play it sure and
818 * do an explicit, full-scale wbinvd() _after_ having marked all
819 * the pages as Not-Present:
820 */
821 wbinvd();
Ingo Molnar123bf0e2009-11-15 21:19:52 +0900822
Mark Langsdorffe2245c2009-07-05 15:50:52 -0500823 /*
824 * Now all caches are flushed and we can safely enable
825 * GART hardware. Doing it early leaves the possibility
826 * of stale cache entries that can lead to GART PTE
827 * errors.
828 */
829 enable_gart_translations();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Ingo Molnar05fccb02008-01-30 13:30:12 +0100831 /*
Pavel Machekfa3d3192008-06-26 00:25:43 +0200832 * Try to workaround a bug (thanks to BenH):
Ingo Molnar05fccb02008-01-30 13:30:12 +0100833 * Set unmapped entries to a scratch page instead of 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 * Any prefetches that hit unmapped entries won't get an bus abort
Pavel Machekfa3d3192008-06-26 00:25:43 +0200835 * then. (P2P bridge may be prefetching on DMA reads).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 */
Ingo Molnar05fccb02008-01-30 13:30:12 +0100837 scratch = get_zeroed_page(GFP_KERNEL);
838 if (!scratch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 panic("Cannot allocate iommu scratch page");
840 gart_unmapped_entry = GPTE_ENCODE(__pa(scratch));
Ingo Molnar05fccb02008-01-30 13:30:12 +0100841 for (i = EMERGENCY_PAGES; i < iommu_pages; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 iommu_gatt_base[i] = gart_unmapped_entry;
843
Andi Kleena32073b2006-06-26 13:56:40 +0200844 flush_gart();
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100845 dma_ops = &gart_dma_ops;
FUJITA Tomonori338bac52009-10-27 16:34:44 +0900846 x86_platform.iommu_shutdown = gart_iommu_shutdown;
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +0900847 swiotlb = 0;
FUJITA Tomonoride957622009-11-10 19:46:14 +0900848
849 return 0;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100850}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Sam Ravnborg43999d92007-03-16 21:07:36 +0100852void __init gart_parse_options(char *p)
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100853{
854 int arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856#ifdef CONFIG_IOMMU_LEAK
Ingo Molnar05fccb02008-01-30 13:30:12 +0100857 if (!strncmp(p, "leak", 4)) {
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100858 leak_trace = 1;
859 p += 4;
Joerg Roedel237a6222008-09-25 12:13:53 +0200860 if (*p == '=')
861 ++p;
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100862 if (isdigit(*p) && get_option(&p, &arg))
863 iommu_leak_pages = arg;
864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865#endif
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100866 if (isdigit(*p) && get_option(&p, &arg))
867 iommu_size = arg;
Joe Perches41855b72009-11-09 17:58:50 -0800868 if (!strncmp(p, "fullflush", 9))
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100869 iommu_fullflush = 1;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100870 if (!strncmp(p, "nofullflush", 11))
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100871 iommu_fullflush = 0;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100872 if (!strncmp(p, "noagp", 5))
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100873 no_agp = 1;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100874 if (!strncmp(p, "noaperture", 10))
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100875 fix_aperture = 0;
876 /* duplicated from pci-dma.c */
Ingo Molnar05fccb02008-01-30 13:30:12 +0100877 if (!strncmp(p, "force", 5))
Joerg Roedel0440d4c2007-10-24 12:49:50 +0200878 gart_iommu_aperture_allowed = 1;
Ingo Molnar05fccb02008-01-30 13:30:12 +0100879 if (!strncmp(p, "allowed", 7))
Joerg Roedel0440d4c2007-10-24 12:49:50 +0200880 gart_iommu_aperture_allowed = 1;
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100881 if (!strncmp(p, "memaper", 7)) {
882 fallback_aper_force = 1;
883 p += 7;
884 if (*p == '=') {
885 ++p;
886 if (get_option(&p, &arg))
887 fallback_aper_order = arg;
888 }
889 }
890}
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -0400891IOMMU_INIT_POST(gart_iommu_hole_init);