pci-gart.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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. */
  12. #include <linux/config.h>
  13. #include <linux/types.h>
  14. #include <linux/ctype.h>
  15. #include <linux/agp_backend.h>
  16. #include <linux/init.h>
  17. #include <linux/mm.h>
  18. #include <linux/string.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/pci.h>
  21. #include <linux/module.h>
  22. #include <linux/topology.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/bitops.h>
  25. #include <asm/atomic.h>
  26. #include <asm/io.h>
  27. #include <asm/mtrr.h>
  28. #include <asm/pgtable.h>
  29. #include <asm/proto.h>
  30. #include <asm/cacheflush.h>
  31. #include <asm/kdebug.h>
  32. dma_addr_t bad_dma_address;
  33. unsigned long iommu_bus_base; /* GART remapping area (physical) */
  34. static unsigned long iommu_size; /* size of remapping area bytes */
  35. static unsigned long iommu_pages; /* .. and in pages */
  36. u32 *iommu_gatt_base; /* Remapping table */
  37. int no_iommu;
  38. static int no_agp;
  39. #ifdef CONFIG_IOMMU_DEBUG
  40. int panic_on_overflow = 1;
  41. int force_iommu = 1;
  42. #else
  43. int panic_on_overflow = 0;
  44. int force_iommu = 0;
  45. #endif
  46. int iommu_merge = 1;
  47. int iommu_sac_force = 0;
  48. /* If this is disabled the IOMMU will use an optimized flushing strategy
  49. of only flushing when an mapping is reused. With it true the GART is flushed
  50. for every mapping. Problem is that doing the lazy flush seems to trigger
  51. bugs with some popular PCI cards, in particular 3ware (but has been also
  52. also seen with Qlogic at least). */
  53. int iommu_fullflush = 1;
  54. /* This tells the BIO block layer to assume merging. Default to off
  55. because we cannot guarantee merging later. */
  56. int iommu_bio_merge = 0;
  57. #define MAX_NB 8
  58. /* Allocation bitmap for the remapping area */
  59. static DEFINE_SPINLOCK(iommu_bitmap_lock);
  60. static unsigned long *iommu_gart_bitmap; /* guarded by iommu_bitmap_lock */
  61. static u32 gart_unmapped_entry;
  62. #define GPTE_VALID 1
  63. #define GPTE_COHERENT 2
  64. #define GPTE_ENCODE(x) \
  65. (((x) & 0xfffff000) | (((x) >> 32) << 4) | GPTE_VALID | GPTE_COHERENT)
  66. #define GPTE_DECODE(x) (((x) & 0xfffff000) | (((u64)(x) & 0xff0) << 28))
  67. #define to_pages(addr,size) \
  68. (round_up(((addr) & ~PAGE_MASK) + (size), PAGE_SIZE) >> PAGE_SHIFT)
  69. #define for_all_nb(dev) \
  70. dev = NULL; \
  71. while ((dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x1103, dev))!=NULL)\
  72. if (dev->bus->number == 0 && \
  73. (PCI_SLOT(dev->devfn) >= 24) && (PCI_SLOT(dev->devfn) <= 31))
  74. static struct pci_dev *northbridges[MAX_NB];
  75. static u32 northbridge_flush_word[MAX_NB];
  76. #define EMERGENCY_PAGES 32 /* = 128KB */
  77. #ifdef CONFIG_AGP
  78. #define AGPEXTERN extern
  79. #else
  80. #define AGPEXTERN
  81. #endif
  82. /* backdoor interface to AGP driver */
  83. AGPEXTERN int agp_memory_reserved;
  84. AGPEXTERN __u32 *agp_gatt_table;
  85. static unsigned long next_bit; /* protected by iommu_bitmap_lock */
  86. static int need_flush; /* global flush state. set for each gart wrap */
  87. static dma_addr_t dma_map_area(struct device *dev, unsigned long phys_mem,
  88. size_t size, int dir, int do_panic);
  89. /* Dummy device used for NULL arguments (normally ISA). Better would
  90. be probably a smaller DMA mask, but this is bug-to-bug compatible to i386. */
  91. static struct device fallback_dev = {
  92. .bus_id = "fallback device",
  93. .coherent_dma_mask = 0xffffffff,
  94. .dma_mask = &fallback_dev.coherent_dma_mask,
  95. };
  96. static unsigned long alloc_iommu(int size)
  97. {
  98. unsigned long offset, flags;
  99. spin_lock_irqsave(&iommu_bitmap_lock, flags);
  100. offset = find_next_zero_string(iommu_gart_bitmap,next_bit,iommu_pages,size);
  101. if (offset == -1) {
  102. need_flush = 1;
  103. offset = find_next_zero_string(iommu_gart_bitmap,0,next_bit,size);
  104. }
  105. if (offset != -1) {
  106. set_bit_string(iommu_gart_bitmap, offset, size);
  107. next_bit = offset+size;
  108. if (next_bit >= iommu_pages) {
  109. next_bit = 0;
  110. need_flush = 1;
  111. }
  112. }
  113. if (iommu_fullflush)
  114. need_flush = 1;
  115. spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
  116. return offset;
  117. }
  118. static void free_iommu(unsigned long offset, int size)
  119. {
  120. unsigned long flags;
  121. if (size == 1) {
  122. clear_bit(offset, iommu_gart_bitmap);
  123. return;
  124. }
  125. spin_lock_irqsave(&iommu_bitmap_lock, flags);
  126. __clear_bit_string(iommu_gart_bitmap, offset, size);
  127. spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
  128. }
  129. /*
  130. * Use global flush state to avoid races with multiple flushers.
  131. */
  132. static void flush_gart(struct device *dev)
  133. {
  134. unsigned long flags;
  135. int flushed = 0;
  136. int i, max;
  137. spin_lock_irqsave(&iommu_bitmap_lock, flags);
  138. if (need_flush) {
  139. max = 0;
  140. for (i = 0; i < MAX_NB; i++) {
  141. if (!northbridges[i])
  142. continue;
  143. pci_write_config_dword(northbridges[i], 0x9c,
  144. northbridge_flush_word[i] | 1);
  145. flushed++;
  146. max = i;
  147. }
  148. for (i = 0; i <= max; i++) {
  149. u32 w;
  150. if (!northbridges[i])
  151. continue;
  152. /* Make sure the hardware actually executed the flush. */
  153. do {
  154. pci_read_config_dword(northbridges[i], 0x9c, &w);
  155. } while (w & 1);
  156. }
  157. if (!flushed)
  158. printk("nothing to flush?\n");
  159. need_flush = 0;
  160. }
  161. spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
  162. }
  163. /* Allocate DMA memory on node near device */
  164. noinline
  165. static void *dma_alloc_pages(struct device *dev, unsigned gfp, unsigned order)
  166. {
  167. struct page *page;
  168. int node;
  169. if (dev->bus == &pci_bus_type) {
  170. cpumask_t mask;
  171. mask = pcibus_to_cpumask(to_pci_dev(dev)->bus);
  172. node = cpu_to_node(first_cpu(mask));
  173. } else
  174. node = numa_node_id();
  175. page = alloc_pages_node(node, gfp, order);
  176. return page ? page_address(page) : NULL;
  177. }
  178. /*
  179. * Allocate memory for a coherent mapping.
  180. */
  181. void *
  182. dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
  183. unsigned gfp)
  184. {
  185. void *memory;
  186. unsigned long dma_mask = 0;
  187. u64 bus;
  188. if (!dev)
  189. dev = &fallback_dev;
  190. dma_mask = dev->coherent_dma_mask;
  191. if (dma_mask == 0)
  192. dma_mask = 0xffffffff;
  193. /* Kludge to make it bug-to-bug compatible with i386. i386
  194. uses the normal dma_mask for alloc_coherent. */
  195. dma_mask &= *dev->dma_mask;
  196. again:
  197. memory = dma_alloc_pages(dev, gfp, get_order(size));
  198. if (memory == NULL)
  199. return NULL;
  200. {
  201. int high, mmu;
  202. bus = virt_to_bus(memory);
  203. high = (bus + size) >= dma_mask;
  204. mmu = high;
  205. if (force_iommu && !(gfp & GFP_DMA))
  206. mmu = 1;
  207. if (no_iommu || dma_mask < 0xffffffffUL) {
  208. if (high) {
  209. free_pages((unsigned long)memory,
  210. get_order(size));
  211. if (swiotlb) {
  212. return
  213. swiotlb_alloc_coherent(dev, size,
  214. dma_handle,
  215. gfp);
  216. }
  217. if (!(gfp & GFP_DMA)) {
  218. gfp |= GFP_DMA;
  219. goto again;
  220. }
  221. return NULL;
  222. }
  223. mmu = 0;
  224. }
  225. memset(memory, 0, size);
  226. if (!mmu) {
  227. *dma_handle = virt_to_bus(memory);
  228. return memory;
  229. }
  230. }
  231. *dma_handle = dma_map_area(dev, bus, size, PCI_DMA_BIDIRECTIONAL, 0);
  232. if (*dma_handle == bad_dma_address)
  233. goto error;
  234. flush_gart(dev);
  235. return memory;
  236. error:
  237. if (panic_on_overflow)
  238. panic("dma_alloc_coherent: IOMMU overflow by %lu bytes\n", size);
  239. free_pages((unsigned long)memory, get_order(size));
  240. return NULL;
  241. }
  242. /*
  243. * Unmap coherent memory.
  244. * The caller must ensure that the device has finished accessing the mapping.
  245. */
  246. void dma_free_coherent(struct device *dev, size_t size,
  247. void *vaddr, dma_addr_t bus)
  248. {
  249. if (swiotlb) {
  250. swiotlb_free_coherent(dev, size, vaddr, bus);
  251. return;
  252. }
  253. dma_unmap_single(dev, bus, size, 0);
  254. free_pages((unsigned long)vaddr, get_order(size));
  255. }
  256. #ifdef CONFIG_IOMMU_LEAK
  257. #define SET_LEAK(x) if (iommu_leak_tab) \
  258. iommu_leak_tab[x] = __builtin_return_address(0);
  259. #define CLEAR_LEAK(x) if (iommu_leak_tab) \
  260. iommu_leak_tab[x] = NULL;
  261. /* Debugging aid for drivers that don't free their IOMMU tables */
  262. static void **iommu_leak_tab;
  263. static int leak_trace;
  264. int iommu_leak_pages = 20;
  265. void dump_leak(void)
  266. {
  267. int i;
  268. static int dump;
  269. if (dump || !iommu_leak_tab) return;
  270. dump = 1;
  271. show_stack(NULL,NULL);
  272. /* Very crude. dump some from the end of the table too */
  273. printk("Dumping %d pages from end of IOMMU:\n", iommu_leak_pages);
  274. for (i = 0; i < iommu_leak_pages; i+=2) {
  275. printk("%lu: ", iommu_pages-i);
  276. printk_address((unsigned long) iommu_leak_tab[iommu_pages-i]);
  277. printk("%c", (i+1)%2 == 0 ? '\n' : ' ');
  278. }
  279. printk("\n");
  280. }
  281. #else
  282. #define SET_LEAK(x)
  283. #define CLEAR_LEAK(x)
  284. #endif
  285. static void iommu_full(struct device *dev, size_t size, int dir, int do_panic)
  286. {
  287. /*
  288. * Ran out of IOMMU space for this operation. This is very bad.
  289. * Unfortunately the drivers cannot handle this operation properly.
  290. * Return some non mapped prereserved space in the aperture and
  291. * let the Northbridge deal with it. This will result in garbage
  292. * in the IO operation. When the size exceeds the prereserved space
  293. * memory corruption will occur or random memory will be DMAed
  294. * out. Hopefully no network devices use single mappings that big.
  295. */
  296. printk(KERN_ERR
  297. "PCI-DMA: Out of IOMMU space for %lu bytes at device %s\n",
  298. size, dev->bus_id);
  299. if (size > PAGE_SIZE*EMERGENCY_PAGES && do_panic) {
  300. if (dir == PCI_DMA_FROMDEVICE || dir == PCI_DMA_BIDIRECTIONAL)
  301. panic("PCI-DMA: Memory would be corrupted\n");
  302. if (dir == PCI_DMA_TODEVICE || dir == PCI_DMA_BIDIRECTIONAL)
  303. panic("PCI-DMA: Random memory would be DMAed\n");
  304. }
  305. #ifdef CONFIG_IOMMU_LEAK
  306. dump_leak();
  307. #endif
  308. }
  309. static inline int need_iommu(struct device *dev, unsigned long addr, size_t size)
  310. {
  311. u64 mask = *dev->dma_mask;
  312. int high = addr + size >= mask;
  313. int mmu = high;
  314. if (force_iommu)
  315. mmu = 1;
  316. if (no_iommu) {
  317. if (high)
  318. panic("PCI-DMA: high address but no IOMMU.\n");
  319. mmu = 0;
  320. }
  321. return mmu;
  322. }
  323. static inline int nonforced_iommu(struct device *dev, unsigned long addr, size_t size)
  324. {
  325. u64 mask = *dev->dma_mask;
  326. int high = addr + size >= mask;
  327. int mmu = high;
  328. if (no_iommu) {
  329. if (high)
  330. panic("PCI-DMA: high address but no IOMMU.\n");
  331. mmu = 0;
  332. }
  333. return mmu;
  334. }
  335. /* Map a single continuous physical area into the IOMMU.
  336. * Caller needs to check if the iommu is needed and flush.
  337. */
  338. static dma_addr_t dma_map_area(struct device *dev, unsigned long phys_mem,
  339. size_t size, int dir, int do_panic)
  340. {
  341. unsigned long npages = to_pages(phys_mem, size);
  342. unsigned long iommu_page = alloc_iommu(npages);
  343. int i;
  344. if (iommu_page == -1) {
  345. if (!nonforced_iommu(dev, phys_mem, size))
  346. return phys_mem;
  347. if (panic_on_overflow)
  348. panic("dma_map_area overflow %lu bytes\n", size);
  349. iommu_full(dev, size, dir, do_panic);
  350. return bad_dma_address;
  351. }
  352. for (i = 0; i < npages; i++) {
  353. iommu_gatt_base[iommu_page + i] = GPTE_ENCODE(phys_mem);
  354. SET_LEAK(iommu_page + i);
  355. phys_mem += PAGE_SIZE;
  356. }
  357. return iommu_bus_base + iommu_page*PAGE_SIZE + (phys_mem & ~PAGE_MASK);
  358. }
  359. /* Map a single area into the IOMMU */
  360. dma_addr_t dma_map_single(struct device *dev, void *addr, size_t size, int dir)
  361. {
  362. unsigned long phys_mem, bus;
  363. BUG_ON(dir == DMA_NONE);
  364. if (swiotlb)
  365. return swiotlb_map_single(dev,addr,size,dir);
  366. if (!dev)
  367. dev = &fallback_dev;
  368. phys_mem = virt_to_phys(addr);
  369. if (!need_iommu(dev, phys_mem, size))
  370. return phys_mem;
  371. bus = dma_map_area(dev, phys_mem, size, dir, 1);
  372. flush_gart(dev);
  373. return bus;
  374. }
  375. /* Fallback for dma_map_sg in case of overflow */
  376. static int dma_map_sg_nonforce(struct device *dev, struct scatterlist *sg,
  377. int nents, int dir)
  378. {
  379. int i;
  380. #ifdef CONFIG_IOMMU_DEBUG
  381. printk(KERN_DEBUG "dma_map_sg overflow\n");
  382. #endif
  383. for (i = 0; i < nents; i++ ) {
  384. struct scatterlist *s = &sg[i];
  385. unsigned long addr = page_to_phys(s->page) + s->offset;
  386. if (nonforced_iommu(dev, addr, s->length)) {
  387. addr = dma_map_area(dev, addr, s->length, dir, 0);
  388. if (addr == bad_dma_address) {
  389. if (i > 0)
  390. dma_unmap_sg(dev, sg, i, dir);
  391. nents = 0;
  392. sg[0].dma_length = 0;
  393. break;
  394. }
  395. }
  396. s->dma_address = addr;
  397. s->dma_length = s->length;
  398. }
  399. flush_gart(dev);
  400. return nents;
  401. }
  402. /* Map multiple scatterlist entries continuous into the first. */
  403. static int __dma_map_cont(struct scatterlist *sg, int start, int stopat,
  404. struct scatterlist *sout, unsigned long pages)
  405. {
  406. unsigned long iommu_start = alloc_iommu(pages);
  407. unsigned long iommu_page = iommu_start;
  408. int i;
  409. if (iommu_start == -1)
  410. return -1;
  411. for (i = start; i < stopat; i++) {
  412. struct scatterlist *s = &sg[i];
  413. unsigned long pages, addr;
  414. unsigned long phys_addr = s->dma_address;
  415. BUG_ON(i > start && s->offset);
  416. if (i == start) {
  417. *sout = *s;
  418. sout->dma_address = iommu_bus_base;
  419. sout->dma_address += iommu_page*PAGE_SIZE + s->offset;
  420. sout->dma_length = s->length;
  421. } else {
  422. sout->dma_length += s->length;
  423. }
  424. addr = phys_addr;
  425. pages = to_pages(s->offset, s->length);
  426. while (pages--) {
  427. iommu_gatt_base[iommu_page] = GPTE_ENCODE(addr);
  428. SET_LEAK(iommu_page);
  429. addr += PAGE_SIZE;
  430. iommu_page++;
  431. }
  432. }
  433. BUG_ON(iommu_page - iommu_start != pages);
  434. return 0;
  435. }
  436. static inline int dma_map_cont(struct scatterlist *sg, int start, int stopat,
  437. struct scatterlist *sout,
  438. unsigned long pages, int need)
  439. {
  440. if (!need) {
  441. BUG_ON(stopat - start != 1);
  442. *sout = sg[start];
  443. sout->dma_length = sg[start].length;
  444. return 0;
  445. }
  446. return __dma_map_cont(sg, start, stopat, sout, pages);
  447. }
  448. /*
  449. * DMA map all entries in a scatterlist.
  450. * Merge chunks that have page aligned sizes into a continuous mapping.
  451. */
  452. int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents, int dir)
  453. {
  454. int i;
  455. int out;
  456. int start;
  457. unsigned long pages = 0;
  458. int need = 0, nextneed;
  459. BUG_ON(dir == DMA_NONE);
  460. if (nents == 0)
  461. return 0;
  462. if (swiotlb)
  463. return swiotlb_map_sg(dev,sg,nents,dir);
  464. if (!dev)
  465. dev = &fallback_dev;
  466. out = 0;
  467. start = 0;
  468. for (i = 0; i < nents; i++) {
  469. struct scatterlist *s = &sg[i];
  470. dma_addr_t addr = page_to_phys(s->page) + s->offset;
  471. s->dma_address = addr;
  472. BUG_ON(s->length == 0);
  473. nextneed = need_iommu(dev, addr, s->length);
  474. /* Handle the previous not yet processed entries */
  475. if (i > start) {
  476. struct scatterlist *ps = &sg[i-1];
  477. /* Can only merge when the last chunk ends on a page
  478. boundary and the new one doesn't have an offset. */
  479. if (!iommu_merge || !nextneed || !need || s->offset ||
  480. (ps->offset + ps->length) % PAGE_SIZE) {
  481. if (dma_map_cont(sg, start, i, sg+out, pages,
  482. need) < 0)
  483. goto error;
  484. out++;
  485. pages = 0;
  486. start = i;
  487. }
  488. }
  489. need = nextneed;
  490. pages += to_pages(s->offset, s->length);
  491. }
  492. if (dma_map_cont(sg, start, i, sg+out, pages, need) < 0)
  493. goto error;
  494. out++;
  495. flush_gart(dev);
  496. if (out < nents)
  497. sg[out].dma_length = 0;
  498. return out;
  499. error:
  500. flush_gart(NULL);
  501. dma_unmap_sg(dev, sg, nents, dir);
  502. /* When it was forced try again unforced */
  503. if (force_iommu)
  504. return dma_map_sg_nonforce(dev, sg, nents, dir);
  505. if (panic_on_overflow)
  506. panic("dma_map_sg: overflow on %lu pages\n", pages);
  507. iommu_full(dev, pages << PAGE_SHIFT, dir, 0);
  508. for (i = 0; i < nents; i++)
  509. sg[i].dma_address = bad_dma_address;
  510. return 0;
  511. }
  512. /*
  513. * Free a DMA mapping.
  514. */
  515. void dma_unmap_single(struct device *dev, dma_addr_t dma_addr,
  516. size_t size, int direction)
  517. {
  518. unsigned long iommu_page;
  519. int npages;
  520. int i;
  521. if (swiotlb) {
  522. swiotlb_unmap_single(dev,dma_addr,size,direction);
  523. return;
  524. }
  525. if (dma_addr < iommu_bus_base + EMERGENCY_PAGES*PAGE_SIZE ||
  526. dma_addr >= iommu_bus_base + iommu_size)
  527. return;
  528. iommu_page = (dma_addr - iommu_bus_base)>>PAGE_SHIFT;
  529. npages = to_pages(dma_addr, size);
  530. for (i = 0; i < npages; i++) {
  531. iommu_gatt_base[iommu_page + i] = gart_unmapped_entry;
  532. CLEAR_LEAK(iommu_page + i);
  533. }
  534. free_iommu(iommu_page, npages);
  535. }
  536. /*
  537. * Wrapper for pci_unmap_single working with scatterlists.
  538. */
  539. void dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, int dir)
  540. {
  541. int i;
  542. if (swiotlb) {
  543. swiotlb_unmap_sg(dev,sg,nents,dir);
  544. return;
  545. }
  546. for (i = 0; i < nents; i++) {
  547. struct scatterlist *s = &sg[i];
  548. if (!s->dma_length || !s->length)
  549. break;
  550. dma_unmap_single(dev, s->dma_address, s->dma_length, dir);
  551. }
  552. }
  553. int dma_supported(struct device *dev, u64 mask)
  554. {
  555. /* Copied from i386. Doesn't make much sense, because it will
  556. only work for pci_alloc_coherent.
  557. The caller just has to use GFP_DMA in this case. */
  558. if (mask < 0x00ffffff)
  559. return 0;
  560. /* Tell the device to use SAC when IOMMU force is on.
  561. This allows the driver to use cheaper accesses in some cases.
  562. Problem with this is that if we overflow the IOMMU area
  563. and return DAC as fallback address the device may not handle it correctly.
  564. As a special case some controllers have a 39bit address mode
  565. that is as efficient as 32bit (aic79xx). Don't force SAC for these.
  566. Assume all masks <= 40 bits are of this type. Normally this doesn't
  567. make any difference, but gives more gentle handling of IOMMU overflow. */
  568. if (iommu_sac_force && (mask >= 0xffffffffffULL)) {
  569. printk(KERN_INFO "%s: Force SAC with mask %Lx\n", dev->bus_id,mask);
  570. return 0;
  571. }
  572. return 1;
  573. }
  574. int dma_get_cache_alignment(void)
  575. {
  576. return boot_cpu_data.x86_clflush_size;
  577. }
  578. EXPORT_SYMBOL(dma_unmap_sg);
  579. EXPORT_SYMBOL(dma_map_sg);
  580. EXPORT_SYMBOL(dma_map_single);
  581. EXPORT_SYMBOL(dma_unmap_single);
  582. EXPORT_SYMBOL(dma_supported);
  583. EXPORT_SYMBOL(no_iommu);
  584. EXPORT_SYMBOL(force_iommu);
  585. EXPORT_SYMBOL(bad_dma_address);
  586. EXPORT_SYMBOL(iommu_bio_merge);
  587. EXPORT_SYMBOL(iommu_sac_force);
  588. EXPORT_SYMBOL(dma_get_cache_alignment);
  589. EXPORT_SYMBOL(dma_alloc_coherent);
  590. EXPORT_SYMBOL(dma_free_coherent);
  591. static __init unsigned long check_iommu_size(unsigned long aper, u64 aper_size)
  592. {
  593. unsigned long a;
  594. if (!iommu_size) {
  595. iommu_size = aper_size;
  596. if (!no_agp)
  597. iommu_size /= 2;
  598. }
  599. a = aper + iommu_size;
  600. iommu_size -= round_up(a, LARGE_PAGE_SIZE) - a;
  601. if (iommu_size < 64*1024*1024)
  602. printk(KERN_WARNING
  603. "PCI-DMA: Warning: Small IOMMU %luMB. Consider increasing the AGP aperture in BIOS\n",iommu_size>>20);
  604. return iommu_size;
  605. }
  606. static __init unsigned read_aperture(struct pci_dev *dev, u32 *size)
  607. {
  608. unsigned aper_size = 0, aper_base_32;
  609. u64 aper_base;
  610. unsigned aper_order;
  611. pci_read_config_dword(dev, 0x94, &aper_base_32);
  612. pci_read_config_dword(dev, 0x90, &aper_order);
  613. aper_order = (aper_order >> 1) & 7;
  614. aper_base = aper_base_32 & 0x7fff;
  615. aper_base <<= 25;
  616. aper_size = (32 * 1024 * 1024) << aper_order;
  617. if (aper_base + aper_size >= 0xffffffff || !aper_size)
  618. aper_base = 0;
  619. *size = aper_size;
  620. return aper_base;
  621. }
  622. /*
  623. * Private Northbridge GATT initialization in case we cannot use the
  624. * AGP driver for some reason.
  625. */
  626. static __init int init_k8_gatt(struct agp_kern_info *info)
  627. {
  628. struct pci_dev *dev;
  629. void *gatt;
  630. unsigned aper_base, new_aper_base;
  631. unsigned aper_size, gatt_size, new_aper_size;
  632. printk(KERN_INFO "PCI-DMA: Disabling AGP.\n");
  633. aper_size = aper_base = info->aper_size = 0;
  634. for_all_nb(dev) {
  635. new_aper_base = read_aperture(dev, &new_aper_size);
  636. if (!new_aper_base)
  637. goto nommu;
  638. if (!aper_base) {
  639. aper_size = new_aper_size;
  640. aper_base = new_aper_base;
  641. }
  642. if (aper_size != new_aper_size || aper_base != new_aper_base)
  643. goto nommu;
  644. }
  645. if (!aper_base)
  646. goto nommu;
  647. info->aper_base = aper_base;
  648. info->aper_size = aper_size>>20;
  649. gatt_size = (aper_size >> PAGE_SHIFT) * sizeof(u32);
  650. gatt = (void *)__get_free_pages(GFP_KERNEL, get_order(gatt_size));
  651. if (!gatt)
  652. panic("Cannot allocate GATT table");
  653. memset(gatt, 0, gatt_size);
  654. agp_gatt_table = gatt;
  655. for_all_nb(dev) {
  656. u32 ctl;
  657. u32 gatt_reg;
  658. gatt_reg = __pa(gatt) >> 12;
  659. gatt_reg <<= 4;
  660. pci_write_config_dword(dev, 0x98, gatt_reg);
  661. pci_read_config_dword(dev, 0x90, &ctl);
  662. ctl |= 1;
  663. ctl &= ~((1<<4) | (1<<5));
  664. pci_write_config_dword(dev, 0x90, ctl);
  665. }
  666. flush_gart(NULL);
  667. printk("PCI-DMA: aperture base @ %x size %u KB\n",aper_base, aper_size>>10);
  668. return 0;
  669. nommu:
  670. /* Should not happen anymore */
  671. printk(KERN_ERR "PCI-DMA: More than 4GB of RAM and no IOMMU\n"
  672. KERN_ERR "PCI-DMA: 32bit PCI IO may malfunction.");
  673. return -1;
  674. }
  675. extern int agp_amd64_init(void);
  676. static int __init pci_iommu_init(void)
  677. {
  678. struct agp_kern_info info;
  679. unsigned long aper_size;
  680. unsigned long iommu_start;
  681. struct pci_dev *dev;
  682. unsigned long scratch;
  683. long i;
  684. #ifndef CONFIG_AGP_AMD64
  685. no_agp = 1;
  686. #else
  687. /* Makefile puts PCI initialization via subsys_initcall first. */
  688. /* Add other K8 AGP bridge drivers here */
  689. no_agp = no_agp ||
  690. (agp_amd64_init() < 0) ||
  691. (agp_copy_info(agp_bridge, &info) < 0);
  692. #endif
  693. if (swiotlb) {
  694. no_iommu = 1;
  695. printk(KERN_INFO "PCI-DMA: Using software bounce buffering for IO (SWIOTLB)\n");
  696. return -1;
  697. }
  698. if (no_iommu ||
  699. (!force_iommu && end_pfn < 0xffffffff>>PAGE_SHIFT) ||
  700. !iommu_aperture ||
  701. (no_agp && init_k8_gatt(&info) < 0)) {
  702. printk(KERN_INFO "PCI-DMA: Disabling IOMMU.\n");
  703. no_iommu = 1;
  704. return -1;
  705. }
  706. aper_size = info.aper_size * 1024 * 1024;
  707. iommu_size = check_iommu_size(info.aper_base, aper_size);
  708. iommu_pages = iommu_size >> PAGE_SHIFT;
  709. iommu_gart_bitmap = (void*)__get_free_pages(GFP_KERNEL,
  710. get_order(iommu_pages/8));
  711. if (!iommu_gart_bitmap)
  712. panic("Cannot allocate iommu bitmap\n");
  713. memset(iommu_gart_bitmap, 0, iommu_pages/8);
  714. #ifdef CONFIG_IOMMU_LEAK
  715. if (leak_trace) {
  716. iommu_leak_tab = (void *)__get_free_pages(GFP_KERNEL,
  717. get_order(iommu_pages*sizeof(void *)));
  718. if (iommu_leak_tab)
  719. memset(iommu_leak_tab, 0, iommu_pages * 8);
  720. else
  721. printk("PCI-DMA: Cannot allocate leak trace area\n");
  722. }
  723. #endif
  724. /*
  725. * Out of IOMMU space handling.
  726. * Reserve some invalid pages at the beginning of the GART.
  727. */
  728. set_bit_string(iommu_gart_bitmap, 0, EMERGENCY_PAGES);
  729. agp_memory_reserved = iommu_size;
  730. printk(KERN_INFO
  731. "PCI-DMA: Reserving %luMB of IOMMU area in the AGP aperture\n",
  732. iommu_size>>20);
  733. iommu_start = aper_size - iommu_size;
  734. iommu_bus_base = info.aper_base + iommu_start;
  735. bad_dma_address = iommu_bus_base;
  736. iommu_gatt_base = agp_gatt_table + (iommu_start>>PAGE_SHIFT);
  737. /*
  738. * Unmap the IOMMU part of the GART. The alias of the page is
  739. * always mapped with cache enabled and there is no full cache
  740. * coherency across the GART remapping. The unmapping avoids
  741. * automatic prefetches from the CPU allocating cache lines in
  742. * there. All CPU accesses are done via the direct mapping to
  743. * the backing memory. The GART address is only used by PCI
  744. * devices.
  745. */
  746. clear_kernel_mapping((unsigned long)__va(iommu_bus_base), iommu_size);
  747. /*
  748. * Try to workaround a bug (thanks to BenH)
  749. * Set unmapped entries to a scratch page instead of 0.
  750. * Any prefetches that hit unmapped entries won't get an bus abort
  751. * then.
  752. */
  753. scratch = get_zeroed_page(GFP_KERNEL);
  754. if (!scratch)
  755. panic("Cannot allocate iommu scratch page");
  756. gart_unmapped_entry = GPTE_ENCODE(__pa(scratch));
  757. for (i = EMERGENCY_PAGES; i < iommu_pages; i++)
  758. iommu_gatt_base[i] = gart_unmapped_entry;
  759. for_all_nb(dev) {
  760. u32 flag;
  761. int cpu = PCI_SLOT(dev->devfn) - 24;
  762. if (cpu >= MAX_NB)
  763. continue;
  764. northbridges[cpu] = dev;
  765. pci_read_config_dword(dev, 0x9c, &flag); /* cache flush word */
  766. northbridge_flush_word[cpu] = flag;
  767. }
  768. flush_gart(NULL);
  769. return 0;
  770. }
  771. /* Must execute after PCI subsystem */
  772. fs_initcall(pci_iommu_init);
  773. /* iommu=[size][,noagp][,off][,force][,noforce][,leak][,memaper[=order]][,merge]
  774. [,forcesac][,fullflush][,nomerge][,biomerge]
  775. size set size of iommu (in bytes)
  776. noagp don't initialize the AGP driver and use full aperture.
  777. off don't use the IOMMU
  778. leak turn on simple iommu leak tracing (only when CONFIG_IOMMU_LEAK is on)
  779. memaper[=order] allocate an own aperture over RAM with size 32MB^order.
  780. noforce don't force IOMMU usage. Default.
  781. force Force IOMMU.
  782. merge Do lazy merging. This may improve performance on some block devices.
  783. Implies force (experimental)
  784. biomerge Do merging at the BIO layer. This is more efficient than merge,
  785. but should be only done with very big IOMMUs. Implies merge,force.
  786. nomerge Don't do SG merging.
  787. forcesac For SAC mode for masks <40bits (experimental)
  788. fullflush Flush IOMMU on each allocation (default)
  789. nofullflush Don't use IOMMU fullflush
  790. allowed overwrite iommu off workarounds for specific chipsets.
  791. soft Use software bounce buffering (default for Intel machines)
  792. noaperture Don't touch the aperture for AGP.
  793. */
  794. __init int iommu_setup(char *p)
  795. {
  796. int arg;
  797. while (*p) {
  798. if (!strncmp(p,"noagp",5))
  799. no_agp = 1;
  800. if (!strncmp(p,"off",3))
  801. no_iommu = 1;
  802. if (!strncmp(p,"force",5)) {
  803. force_iommu = 1;
  804. iommu_aperture_allowed = 1;
  805. }
  806. if (!strncmp(p,"allowed",7))
  807. iommu_aperture_allowed = 1;
  808. if (!strncmp(p,"noforce",7)) {
  809. iommu_merge = 0;
  810. force_iommu = 0;
  811. }
  812. if (!strncmp(p, "memaper", 7)) {
  813. fallback_aper_force = 1;
  814. p += 7;
  815. if (*p == '=') {
  816. ++p;
  817. if (get_option(&p, &arg))
  818. fallback_aper_order = arg;
  819. }
  820. }
  821. if (!strncmp(p, "biomerge",8)) {
  822. iommu_bio_merge = 4096;
  823. iommu_merge = 1;
  824. force_iommu = 1;
  825. }
  826. if (!strncmp(p, "panic",5))
  827. panic_on_overflow = 1;
  828. if (!strncmp(p, "nopanic",7))
  829. panic_on_overflow = 0;
  830. if (!strncmp(p, "merge",5)) {
  831. iommu_merge = 1;
  832. force_iommu = 1;
  833. }
  834. if (!strncmp(p, "nomerge",7))
  835. iommu_merge = 0;
  836. if (!strncmp(p, "forcesac",8))
  837. iommu_sac_force = 1;
  838. if (!strncmp(p, "fullflush",8))
  839. iommu_fullflush = 1;
  840. if (!strncmp(p, "nofullflush",11))
  841. iommu_fullflush = 0;
  842. if (!strncmp(p, "soft",4))
  843. swiotlb = 1;
  844. if (!strncmp(p, "noaperture",10))
  845. fix_aperture = 0;
  846. #ifdef CONFIG_IOMMU_LEAK
  847. if (!strncmp(p,"leak",4)) {
  848. leak_trace = 1;
  849. p += 4;
  850. if (*p == '=') ++p;
  851. if (isdigit(*p) && get_option(&p, &arg))
  852. iommu_leak_pages = arg;
  853. } else
  854. #endif
  855. if (isdigit(*p) && get_option(&p, &arg))
  856. iommu_size = arg;
  857. p += strcspn(p, ",");
  858. if (*p == ',')
  859. ++p;
  860. }
  861. return 1;
  862. }