pci-gart.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:15k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * Dynamic DMA mapping support for AMD Hammer.
  3.  * 
  4.  * 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
  6.  * with more than 4GB. 
  7.  *
  8.  * See Documentation/DMA-mapping.txt for the interface specification.
  9.  * 
  10.  * Copyright 2002 Andi Kleen, SuSE Labs.
  11.  * $Id: pci-gart.c,v 1.12 2002/09/19 19:25:32 ak Exp $
  12.  */
  13. /* 
  14.  * Notebook:
  15. agpgart_be
  16.  check if the simple reservation scheme is enough.
  17. possible future tuning: 
  18.  fast path for sg streaming mappings 
  19.  more intelligent flush strategy - flush only a single NB?
  20.  move boundary between IOMMU and AGP in GART dynamically
  21.  could use exact fit in the gart in alloc_consistent, not order of two.
  22. */ 
  23. #include <linux/config.h>
  24. #include <linux/types.h>
  25. #include <linux/ctype.h>
  26. #include <linux/agp_backend.h>
  27. #include <linux/init.h>
  28. #include <linux/mm.h>
  29. #include <linux/string.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/pci.h>
  32. #include <linux/module.h>
  33. #include <asm/io.h>
  34. #include <asm/mtrr.h>
  35. #include <asm/bitops.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/proto.h>
  38. #include "pci-x86_64.h"
  39. unsigned long iommu_bus_base; /* GART remapping area (physical) */
  40. static unsigned long iommu_size;  /* size of remapping area bytes */
  41. static unsigned long iommu_pages; /* .. and in pages */
  42. u32 *iommu_gatt_base;  /* Remapping table */
  43. int no_iommu; 
  44. static int no_agp; 
  45. int force_mmu = 1;
  46. extern int fallback_aper_order;
  47. extern int fallback_aper_force;
  48. /* Allocation bitmap for the remapping area */ 
  49. static spinlock_t iommu_bitmap_lock = SPIN_LOCK_UNLOCKED;
  50. static unsigned long *iommu_gart_bitmap; /* guarded by iommu_bitmap_lock */
  51. #define GPTE_MASK 0xfffffff000
  52. #define GPTE_VALID    1
  53. #define GPTE_COHERENT 2
  54. #define GPTE_ENCODE(x,flag) (((x) & 0xfffffff0) | ((x) >> 28) | GPTE_VALID | (flag))
  55. #define GPTE_DECODE(x) (((x) & 0xfffff000) | (((x) & 0xff0) << 28))
  56. #define for_all_nb(dev) 
  57. pci_for_each_dev(dev) 
  58. if (dev->bus->number == 0 && PCI_FUNC(dev->devfn) == 3 && 
  59.     (PCI_SLOT(dev->devfn) >= 24) && (PCI_SLOT(dev->devfn) <= 31))
  60. #define EMERGENCY_PAGES 32 /* = 128KB */ 
  61. #ifdef CONFIG_AGP
  62. extern int agp_init(void);
  63. #define AGPEXTERN extern
  64. #else
  65. #define AGPEXTERN
  66. #endif
  67. /* backdoor interface to AGP driver */
  68. AGPEXTERN int agp_memory_reserved;
  69. AGPEXTERN __u32 *agp_gatt_table;
  70. static unsigned long next_bit;  /* protected by iommu_bitmap_lock */
  71. static unsigned long alloc_iommu(int size) 
  72. unsigned long offset, flags;
  73. spin_lock_irqsave(&iommu_bitmap_lock, flags);
  74. offset = find_next_zero_string(iommu_gart_bitmap,next_bit,iommu_pages,size);
  75. if (offset == -1) 
  76.         offset = find_next_zero_string(iommu_gart_bitmap,0,next_bit,size);
  77. if (offset != -1) { 
  78. set_bit_string(iommu_gart_bitmap, offset, size); 
  79. next_bit = offset+size; 
  80. if (next_bit >= iommu_pages) 
  81. next_bit = 0;
  82. spin_unlock_irqrestore(&iommu_bitmap_lock, flags);      
  83. return offset;
  84. static void free_iommu(unsigned long offset, int size)
  85. unsigned long flags;
  86. spin_lock_irqsave(&iommu_bitmap_lock, flags);
  87. clear_bit_string(iommu_gart_bitmap, offset, size);
  88. next_bit = offset;
  89. spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
  90. static inline void flush_gart(void) 
  91. struct pci_dev *nb; 
  92. for_all_nb(nb) { 
  93. u32 flag; 
  94. pci_read_config_dword(nb, 0x9c, &flag); /* could cache this */ 
  95. /* could complain for PTE walk errors here (bit 1 of flag) */ 
  96. flag |= 1; 
  97. pci_write_config_dword(nb, 0x9c, flag); 
  98. void *pci_alloc_consistent(struct pci_dev *hwdev, size_t size,
  99.    dma_addr_t *dma_handle)
  100. {
  101. void *memory;
  102. int gfp = GFP_ATOMIC;
  103. int order, i;
  104. unsigned long iommu_page;
  105. if (hwdev == NULL || hwdev->dma_mask < 0xffffffff || no_iommu)
  106. gfp |= GFP_DMA;
  107. /* 
  108.  * First try to allocate continuous and use directly if already 
  109.  * in lowmem. 
  110.  */ 
  111. order = get_order(size);
  112. memory = (void *)__get_free_pages(gfp, order);
  113. if (memory == NULL) {
  114. return NULL; 
  115. } else {
  116. int high = (unsigned long)virt_to_bus(memory) + size
  117. >= 0xffffffff;
  118. int mmu = high;
  119. if (force_mmu) 
  120. mmu = 1;
  121. if (no_iommu) { 
  122. if (high) goto error;
  123. mmu = 0; 
  124. memset(memory, 0, size); 
  125. if (!mmu) { 
  126. *dma_handle = virt_to_bus(memory);
  127. return memory;
  128. }
  129. iommu_page = alloc_iommu(1<<order);
  130. if (iommu_page == -1)
  131. goto error; 
  132.     /* Fill in the GATT, allocating pages as needed. */
  133. for (i = 0; i < 1<<order; i++) { 
  134. unsigned long phys_mem; 
  135. void *mem = memory + i*PAGE_SIZE;
  136. if (i > 0) 
  137. atomic_inc(&virt_to_page(mem)->count); 
  138. phys_mem = virt_to_phys(mem); 
  139. BUG_ON(phys_mem & ~PTE_MASK); 
  140. iommu_gatt_base[iommu_page + i] = GPTE_ENCODE(phys_mem,GPTE_COHERENT); 
  141. flush_gart();
  142. *dma_handle = iommu_bus_base + (iommu_page << PAGE_SHIFT);
  143. return memory; 
  144.  error:
  145. free_pages((unsigned long)memory, order); 
  146. return NULL; 
  147. }
  148. /* 
  149.  * Unmap consistent memory.
  150.  * The caller must ensure that the device has finished accessing the mapping.
  151.  */
  152. void pci_free_consistent(struct pci_dev *hwdev, size_t size,
  153.  void *vaddr, dma_addr_t bus)
  154. {
  155. u64 pte;
  156. int order = get_order(size);
  157. unsigned long iommu_page;
  158. int i;
  159. if (bus < iommu_bus_base || bus > iommu_bus_base + iommu_size) { 
  160. free_pages((unsigned long)vaddr, order); 
  161. return;
  162. iommu_page = (bus - iommu_bus_base) / PAGE_SIZE;
  163. for (i = 0; i < 1<<order; i++) {
  164. pte = iommu_gatt_base[iommu_page + i];
  165. BUG_ON((pte & GPTE_VALID) == 0); 
  166. iommu_gatt_base[iommu_page + i] = 0; 
  167. free_page((unsigned long) __va(GPTE_DECODE(pte)));
  168. flush_gart(); 
  169. free_iommu(iommu_page, 1<<order);
  170. }
  171. #ifdef CONFIG_IOMMU_LEAK
  172. /* Debugging aid for drivers that don't free their IOMMU tables */
  173. static void **iommu_leak_tab; 
  174. static int leak_trace;
  175. int iommu_leak_dumppages = 20; 
  176. void dump_leak(void)
  177. {
  178. int i;
  179. static int dump; 
  180. if (dump || !iommu_leak_tab) return;
  181. dump = 1;
  182. show_stack(NULL);
  183. printk("Dumping %d pages from end of IOMMU:n", iommu_leak_dumppages); 
  184. for (i = 0; i < iommu_leak_dumppages; i++) 
  185. printk("[%lu: %lx] ",
  186.        iommu_pages-i,(unsigned long) iommu_leak_tab[iommu_pages-i]); 
  187. printk("n");
  188. }
  189. #endif
  190. static void iommu_full(struct pci_dev *dev, void *addr, size_t size, int dir)
  191. {
  192. /* 
  193.  * Ran out of IOMMU space for this operation. This is very bad.
  194.  * Unfortunately the drivers cannot handle this operation properly.
  195.  * Return some non mapped prereserved space in the aperture and 
  196.  * let the Northbridge deal with it. This will result in garbage
  197.  * in the IO operation. When the size exceeds the prereserved space
  198.  * memory corruption will occur or random memory will be DMAed 
  199.  * out. Hopefully no network devices use single mappings that big.
  200.  */ 
  201. printk(KERN_ERR 
  202.   "PCI-DMA: Error: ran out out IOMMU space for %p size %lu at device %s[%s]n",
  203.        addr,size, dev ? dev->name : "?", dev ? dev->slot_name : "?");
  204. if (size > PAGE_SIZE*EMERGENCY_PAGES) {
  205. if (dir == PCI_DMA_FROMDEVICE || dir == PCI_DMA_BIDIRECTIONAL)
  206. panic("PCI-DMA: Memory will be corruptedn");
  207. if (dir == PCI_DMA_TODEVICE || dir == PCI_DMA_BIDIRECTIONAL) 
  208. panic("PCI-DMA: Random memory will be DMAedn"); 
  209. #ifdef CONFIG_IOMMU_LEAK
  210. dump_leak(); 
  211. #endif
  212. static inline int need_iommu(struct pci_dev *dev, unsigned long addr, size_t size)
  213. u64 mask = dev ? dev->dma_mask : 0xffffffff;
  214. int high = (~mask & (unsigned long)(addr + size)) != 0;
  215. int mmu = high;
  216. if (force_mmu) 
  217. mmu = 1; 
  218. if (no_iommu) { 
  219. if (high) 
  220. panic("pci_map_single: high address but no IOMMU.n"); 
  221. mmu = 0; 
  222. return mmu; 
  223. }
  224. dma_addr_t pci_map_single(struct pci_dev *dev, void *addr, size_t size,int dir)
  225. unsigned long iommu_page;
  226. unsigned long phys_mem, bus;
  227. int i, npages;
  228. BUG_ON(dir == PCI_DMA_NONE);
  229. phys_mem = virt_to_phys(addr); 
  230. if (!need_iommu(dev, phys_mem, size))
  231. return phys_mem; 
  232. npages = round_up(size, PAGE_SIZE) >> PAGE_SHIFT;
  233. iommu_page = alloc_iommu(npages); 
  234. if (iommu_page == -1) {
  235. iommu_full(dev, addr, size, dir); 
  236. return iommu_bus_base; 
  237. phys_mem &= PAGE_MASK;
  238. for (i = 0; i < npages; i++, phys_mem += PAGE_SIZE) {
  239. BUG_ON(phys_mem & ~PTE_MASK); 
  240. /* 
  241.  * Set coherent mapping here to avoid needing to flush
  242.  * the caches on mapping.
  243.  */
  244. iommu_gatt_base[iommu_page + i] = GPTE_ENCODE(phys_mem, GPTE_COHERENT);
  245. #ifdef CONFIG_IOMMU_LEAK
  246. /* XXX need eventually caller of pci_map_sg */
  247. if (iommu_leak_tab) 
  248. iommu_leak_tab[iommu_page + i] = __builtin_return_address(0); 
  249. #endif
  250. }
  251. flush_gart(); 
  252. bus = iommu_bus_base + iommu_page*PAGE_SIZE; 
  253. return bus + ((unsigned long)addr & ~PAGE_MASK); 
  254. /*
  255.  * Free a temporary PCI mapping.
  256.  */ 
  257. void pci_unmap_single(struct pci_dev *hwdev, dma_addr_t dma_addr,
  258.       size_t size, int direction)
  259. {
  260. unsigned long iommu_page; 
  261. int i, npages;
  262. if (dma_addr < iommu_bus_base + EMERGENCY_PAGES*PAGE_SIZE || 
  263.     dma_addr > iommu_bus_base + iommu_size)
  264. return;
  265. iommu_page = (dma_addr - iommu_bus_base)>>PAGE_SHIFT;
  266. npages = round_up(size, PAGE_SIZE) >> PAGE_SHIFT;
  267. for (i = 0; i < npages; i++) { 
  268. iommu_gatt_base[iommu_page + i] = 0; 
  269. #ifdef CONFIG_IOMMU_LEAK
  270. if (iommu_leak_tab)
  271. iommu_leak_tab[iommu_page + i] = 0; 
  272. #endif
  273. }
  274. flush_gart(); 
  275. free_iommu(iommu_page, npages);
  276. }
  277. EXPORT_SYMBOL(pci_map_single);
  278. EXPORT_SYMBOL(pci_unmap_single);
  279. static __init unsigned long check_iommu_size(unsigned long aper, u64 aper_size)
  280. unsigned long a; 
  281. if (!iommu_size) { 
  282. iommu_size = aper_size; 
  283. if (!no_agp) 
  284. iommu_size /= 2; 
  285. a = aper + iommu_size; 
  286. iommu_size -= round_up(a, LARGE_PAGE_SIZE) - a;
  287. if (iommu_size < 64*1024*1024) 
  288. printk(KERN_WARNING
  289.   "PCI-DMA: Warning: Small IOMMU %luMB. Consider increasing the AGP aperture in BIOSn",iommu_size>>20); 
  290. return iommu_size;
  291. static __init unsigned read_aperture(struct pci_dev *dev, u32 *size) 
  292. unsigned aper_size = 0, aper_base_32;
  293. u64 aper_base;
  294. unsigned aper_order;
  295. pci_read_config_dword(dev, 0x94, &aper_base_32); 
  296. pci_read_config_dword(dev, 0x90, &aper_order);
  297. aper_order = (aper_order >> 1) & 7;
  298. aper_base = aper_base_32 & 0x7fff; 
  299. aper_base <<= 25;
  300. aper_size = (32 * 1024 * 1024) << aper_order; 
  301. if (aper_base + aper_size >= 0xffffffff || !aper_size)
  302. aper_base = 0;
  303. *size = aper_size;
  304. return aper_base;
  305. /* 
  306.  * Private Northbridge GATT initialization in case we cannot use the
  307.  * AGP driver for some reason.  
  308.  */
  309. static __init int init_k8_gatt(agp_kern_info *info)
  310. struct pci_dev *dev;
  311. void *gatt;
  312. unsigned aper_base, new_aper_base;
  313. unsigned aper_size, gatt_size, new_aper_size;
  314. aper_size = aper_base = info->aper_size = 0;
  315. for_all_nb(dev) { 
  316. new_aper_base = read_aperture(dev, &new_aper_size); 
  317. if (!new_aper_base) 
  318. goto nommu; 
  319. if (!aper_base) { 
  320. aper_size = new_aper_size;
  321. aper_base = new_aper_base;
  322. }
  323. if (aper_size != new_aper_size || aper_base != new_aper_base) 
  324. goto nommu;
  325. if (!aper_base)
  326. goto nommu; 
  327. info->aper_base = aper_base;
  328. info->aper_size = aper_size>>20; 
  329. gatt_size = (aper_size >> PAGE_SHIFT) * sizeof(u32); 
  330. gatt = (void *)__get_free_pages(GFP_KERNEL, get_order(gatt_size)); 
  331. if (!gatt) 
  332. panic("Cannot allocate GATT table"); 
  333. memset(gatt, 0, gatt_size); 
  334. change_page_attr(virt_to_page(gatt), gatt_size/PAGE_SIZE, PAGE_KERNEL_NOCACHE);
  335. agp_gatt_table = gatt;
  336. for_all_nb(dev) { 
  337. u32 ctl; 
  338. u32 gatt_reg; 
  339. gatt_reg = ((u64)gatt) >> 12; 
  340. gatt_reg <<= 4; 
  341. pci_write_config_dword(dev, 0x98, gatt_reg);
  342. pci_read_config_dword(dev, 0x90, &ctl); 
  343. ctl |= 1;
  344. ctl &= ~((1<<4) | (1<<5));
  345. pci_write_config_dword(dev, 0x90, ctl); 
  346. }
  347. flush_gart(); 
  348. printk("PCI-DMA: aperture base @ %x size %u KBn", aper_base, aper_size>>10); 
  349. return 0;
  350.  nommu:
  351. /* XXX: reject 0xffffffff mask now in pci mapping functions */
  352. printk(KERN_ERR "PCI-DMA: More than 4GB of RAM and no IOMMUn"
  353.        KERN_ERR "PCI-DMA: 32bit PCI IO may malfunction."); 
  354. return -1; 
  355. void __init pci_iommu_init(void)
  356. agp_kern_info info;
  357. unsigned long aper_size;
  358. unsigned long iommu_start;
  359. #ifndef CONFIG_AGP
  360. no_agp = 1; 
  361. #else
  362. no_agp = no_agp || (agp_init() < 0) || (agp_copy_info(&info) < 0); 
  363. #endif
  364. if (no_iommu || (!force_mmu && end_pfn < 0xffffffff>>PAGE_SHIFT)) { 
  365. printk(KERN_INFO "PCI-DMA: Disabling IOMMU.n"); 
  366. no_iommu = 1;
  367. return;
  368. }
  369. if (no_agp) { 
  370. int err = -1;
  371. printk(KERN_INFO "PCI-DMA: Disabling AGP.n");
  372. no_agp = 1;
  373. if (force_mmu || end_pfn >= 0xffffffff>>PAGE_SHIFT)
  374. err = init_k8_gatt(&info);
  375. if (err < 0) { 
  376. printk(KERN_INFO "PCI-DMA: Disabling IOMMU.n"); 
  377. no_iommu = 1;
  378. return; 
  379. }
  380. aper_size = info.aper_size * 1024 * 1024;
  381. iommu_size = check_iommu_size(info.aper_base, aper_size); 
  382. iommu_pages = iommu_size >> PAGE_SHIFT; 
  383. iommu_gart_bitmap = (void*)__get_free_pages(GFP_KERNEL, 
  384.     get_order(iommu_pages/8)); 
  385. if (!iommu_gart_bitmap) 
  386. panic("Cannot allocate iommu bitmapn"); 
  387. memset(iommu_gart_bitmap, 0, iommu_pages/8);
  388. #ifdef CONFIG_IOMMU_LEAK
  389. if (leak_trace) { 
  390. iommu_leak_tab = (void *)__get_free_pages(GFP_KERNEL, 
  391.   get_order(iommu_pages*sizeof(void *)));
  392. if (iommu_leak_tab) 
  393. memset(iommu_leak_tab, 0, iommu_pages * 8); 
  394. else
  395. printk("PCI-DMA: Cannot allocate leak trace arean"); 
  396. #endif
  397. /* 
  398.  * Out of IOMMU space handling.
  399.  * Reserve some invalid pages at the beginning of the GART. 
  400.  */ 
  401. set_bit_string(iommu_gart_bitmap, 0, EMERGENCY_PAGES); 
  402. agp_memory_reserved = iommu_size;
  403. printk(KERN_INFO"PCI-DMA: Reserving %luMB of IOMMU area in the AGP aperturen",
  404.        iommu_size>>20); 
  405. iommu_start = aper_size - iommu_size;
  406. iommu_bus_base = info.aper_base + iommu_start; 
  407. iommu_gatt_base = agp_gatt_table + (iommu_start>>PAGE_SHIFT);
  408. bad_dma_address = iommu_bus_base;
  409. asm volatile("wbinvd" ::: "memory");
  410. /* iommu=[size][,noagp][,off][,force][,noforce][,leak][,memaper[=order]]
  411.    size  set size of iommu (in bytes) 
  412.    noagp don't initialize the AGP driver and use full aperture.
  413.    off   don't use the IOMMU
  414.    leak  turn on simple iommu leak tracing (only when CONFIG_IOMMU_LEAK is on)
  415.    memaper[=order] allocate an own aperture over RAM with size 32MB^order.
  416. */
  417. __init int iommu_setup(char *opt) 
  418.     int arg;
  419.     char *p = opt;
  420.     
  421.     for (;;) { 
  422.     if (!memcmp(p,"noagp", 5))
  423.     no_agp = 1; 
  424.     if (!memcmp(p,"off", 3))
  425.     no_iommu = 1;
  426.     if (!memcmp(p,"force", 5))
  427.     force_mmu = 1;
  428.     if (!memcmp(p,"noforce", 7))
  429.     force_mmu = 0;
  430.     if (!memcmp(p, "memaper", 7)) { 
  431.     fallback_aper_force = 1; 
  432.     p += 7; 
  433.     if (*p == '=' && get_option(&p, &arg))
  434.     fallback_aper_order = arg;
  435.     } 
  436. #ifdef CONFIG_IOMMU_LEAK
  437.     if (!memcmp(p,"leak", 4))
  438.     leak_trace = 1;
  439. #endif
  440.     if (isdigit(*p) && get_option(&p, &arg)) 
  441.     iommu_size = arg;
  442.     do {
  443.     if (*p == ' ' || *p == 0) 
  444.     return 0; 
  445.     } while (*p++ != ','); 
  446.     }
  447.     return 1;