pci-gart_64.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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/PCI/PCI-DMA-mapping.txt for the interface specification.
  9. *
  10. * Copyright 2002 Andi Kleen, SuSE Labs.
  11. * Subject to the GNU General Public License v2 only.
  12. */
  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/sched.h>
  19. #include <linux/string.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/pci.h>
  22. #include <linux/module.h>
  23. #include <linux/topology.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/bitmap.h>
  26. #include <linux/kdebug.h>
  27. #include <linux/scatterlist.h>
  28. #include <linux/iommu-helper.h>
  29. #include <linux/sysdev.h>
  30. #include <linux/io.h>
  31. #include <linux/gfp.h>
  32. #include <asm/atomic.h>
  33. #include <asm/mtrr.h>
  34. #include <asm/pgtable.h>
  35. #include <asm/proto.h>
  36. #include <asm/iommu.h>
  37. #include <asm/gart.h>
  38. #include <asm/cacheflush.h>
  39. #include <asm/swiotlb.h>
  40. #include <asm/dma.h>
  41. #include <asm/k8.h>
  42. #include <asm/x86_init.h>
  43. static unsigned long iommu_bus_base; /* GART remapping area (physical) */
  44. static unsigned long iommu_size; /* size of remapping area bytes */
  45. static unsigned long iommu_pages; /* .. and in pages */
  46. static u32 *iommu_gatt_base; /* Remapping table */
  47. static dma_addr_t bad_dma_addr;
  48. /*
  49. * If this is disabled the IOMMU will use an optimized flushing strategy
  50. * of only flushing when an mapping is reused. With it true the GART is
  51. * flushed for every mapping. Problem is that doing the lazy flush seems
  52. * to trigger bugs with some popular PCI cards, in particular 3ware (but
  53. * has been also also seen with Qlogic at least).
  54. */
  55. static int iommu_fullflush = 1;
  56. /* Allocation bitmap for the remapping area: */
  57. static DEFINE_SPINLOCK(iommu_bitmap_lock);
  58. /* Guarded by iommu_bitmap_lock: */
  59. static unsigned long *iommu_gart_bitmap;
  60. static u32 gart_unmapped_entry;
  61. #define GPTE_VALID 1
  62. #define GPTE_COHERENT 2
  63. #define GPTE_ENCODE(x) \
  64. (((x) & 0xfffff000) | (((x) >> 32) << 4) | GPTE_VALID | GPTE_COHERENT)
  65. #define GPTE_DECODE(x) (((x) & 0xfffff000) | (((u64)(x) & 0xff0) << 28))
  66. #define EMERGENCY_PAGES 32 /* = 128KB */
  67. #ifdef CONFIG_AGP
  68. #define AGPEXTERN extern
  69. #else
  70. #define AGPEXTERN
  71. #endif
  72. /* backdoor interface to AGP driver */
  73. AGPEXTERN int agp_memory_reserved;
  74. AGPEXTERN __u32 *agp_gatt_table;
  75. static unsigned long next_bit; /* protected by iommu_bitmap_lock */
  76. static bool need_flush; /* global flush state. set for each gart wrap */
  77. static unsigned long alloc_iommu(struct device *dev, int size,
  78. unsigned long align_mask)
  79. {
  80. unsigned long offset, flags;
  81. unsigned long boundary_size;
  82. unsigned long base_index;
  83. base_index = ALIGN(iommu_bus_base & dma_get_seg_boundary(dev),
  84. PAGE_SIZE) >> PAGE_SHIFT;
  85. boundary_size = ALIGN((u64)dma_get_seg_boundary(dev) + 1,
  86. PAGE_SIZE) >> PAGE_SHIFT;
  87. spin_lock_irqsave(&iommu_bitmap_lock, flags);
  88. offset = iommu_area_alloc(iommu_gart_bitmap, iommu_pages, next_bit,
  89. size, base_index, boundary_size, align_mask);
  90. if (offset == -1) {
  91. need_flush = true;
  92. offset = iommu_area_alloc(iommu_gart_bitmap, iommu_pages, 0,
  93. size, base_index, boundary_size,
  94. align_mask);
  95. }
  96. if (offset != -1) {
  97. next_bit = offset+size;
  98. if (next_bit >= iommu_pages) {
  99. next_bit = 0;
  100. need_flush = true;
  101. }
  102. }
  103. if (iommu_fullflush)
  104. need_flush = true;
  105. spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
  106. return offset;
  107. }
  108. static void free_iommu(unsigned long offset, int size)
  109. {
  110. unsigned long flags;
  111. spin_lock_irqsave(&iommu_bitmap_lock, flags);
  112. bitmap_clear(iommu_gart_bitmap, offset, size);
  113. if (offset >= next_bit)
  114. next_bit = offset + size;
  115. spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
  116. }
  117. /*
  118. * Use global flush state to avoid races with multiple flushers.
  119. */
  120. static void flush_gart(void)
  121. {
  122. unsigned long flags;
  123. spin_lock_irqsave(&iommu_bitmap_lock, flags);
  124. if (need_flush) {
  125. k8_flush_garts();
  126. need_flush = false;
  127. }
  128. spin_unlock_irqrestore(&iommu_bitmap_lock, flags);
  129. }
  130. #ifdef CONFIG_IOMMU_LEAK
  131. /* Debugging aid for drivers that don't free their IOMMU tables */
  132. static int leak_trace;
  133. static int iommu_leak_pages = 20;
  134. static void dump_leak(void)
  135. {
  136. static int dump;
  137. if (dump)
  138. return;
  139. dump = 1;
  140. show_stack(NULL, NULL);
  141. debug_dma_dump_mappings(NULL);
  142. }
  143. #endif
  144. static void iommu_full(struct device *dev, size_t size, int dir)
  145. {
  146. /*
  147. * Ran out of IOMMU space for this operation. This is very bad.
  148. * Unfortunately the drivers cannot handle this operation properly.
  149. * Return some non mapped prereserved space in the aperture and
  150. * let the Northbridge deal with it. This will result in garbage
  151. * in the IO operation. When the size exceeds the prereserved space
  152. * memory corruption will occur or random memory will be DMAed
  153. * out. Hopefully no network devices use single mappings that big.
  154. */
  155. dev_err(dev, "PCI-DMA: Out of IOMMU space for %lu bytes\n", size);
  156. if (size > PAGE_SIZE*EMERGENCY_PAGES) {
  157. if (dir == PCI_DMA_FROMDEVICE || dir == PCI_DMA_BIDIRECTIONAL)
  158. panic("PCI-DMA: Memory would be corrupted\n");
  159. if (dir == PCI_DMA_TODEVICE || dir == PCI_DMA_BIDIRECTIONAL)
  160. panic(KERN_ERR
  161. "PCI-DMA: Random memory would be DMAed\n");
  162. }
  163. #ifdef CONFIG_IOMMU_LEAK
  164. dump_leak();
  165. #endif
  166. }
  167. static inline int
  168. need_iommu(struct device *dev, unsigned long addr, size_t size)
  169. {
  170. return force_iommu || !dma_capable(dev, addr, size);
  171. }
  172. static inline int
  173. nonforced_iommu(struct device *dev, unsigned long addr, size_t size)
  174. {
  175. return !dma_capable(dev, addr, size);
  176. }
  177. /* Map a single continuous physical area into the IOMMU.
  178. * Caller needs to check if the iommu is needed and flush.
  179. */
  180. static dma_addr_t dma_map_area(struct device *dev, dma_addr_t phys_mem,
  181. size_t size, int dir, unsigned long align_mask)
  182. {
  183. unsigned long npages = iommu_num_pages(phys_mem, size, PAGE_SIZE);
  184. unsigned long iommu_page = alloc_iommu(dev, npages, align_mask);
  185. int i;
  186. if (iommu_page == -1) {
  187. if (!nonforced_iommu(dev, phys_mem, size))
  188. return phys_mem;
  189. if (panic_on_overflow)
  190. panic("dma_map_area overflow %lu bytes\n", size);
  191. iommu_full(dev, size, dir);
  192. return bad_dma_addr;
  193. }
  194. for (i = 0; i < npages; i++) {
  195. iommu_gatt_base[iommu_page + i] = GPTE_ENCODE(phys_mem);
  196. phys_mem += PAGE_SIZE;
  197. }
  198. return iommu_bus_base + iommu_page*PAGE_SIZE + (phys_mem & ~PAGE_MASK);
  199. }
  200. /* Map a single area into the IOMMU */
  201. static dma_addr_t gart_map_page(struct device *dev, struct page *page,
  202. unsigned long offset, size_t size,
  203. enum dma_data_direction dir,
  204. struct dma_attrs *attrs)
  205. {
  206. unsigned long bus;
  207. phys_addr_t paddr = page_to_phys(page) + offset;
  208. if (!dev)
  209. dev = &x86_dma_fallback_dev;
  210. if (!need_iommu(dev, paddr, size))
  211. return paddr;
  212. bus = dma_map_area(dev, paddr, size, dir, 0);
  213. flush_gart();
  214. return bus;
  215. }
  216. /*
  217. * Free a DMA mapping.
  218. */
  219. static void gart_unmap_page(struct device *dev, dma_addr_t dma_addr,
  220. size_t size, enum dma_data_direction dir,
  221. struct dma_attrs *attrs)
  222. {
  223. unsigned long iommu_page;
  224. int npages;
  225. int i;
  226. if (dma_addr < iommu_bus_base + EMERGENCY_PAGES*PAGE_SIZE ||
  227. dma_addr >= iommu_bus_base + iommu_size)
  228. return;
  229. iommu_page = (dma_addr - iommu_bus_base)>>PAGE_SHIFT;
  230. npages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
  231. for (i = 0; i < npages; i++) {
  232. iommu_gatt_base[iommu_page + i] = gart_unmapped_entry;
  233. }
  234. free_iommu(iommu_page, npages);
  235. }
  236. /*
  237. * Wrapper for pci_unmap_single working with scatterlists.
  238. */
  239. static void gart_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  240. enum dma_data_direction dir, struct dma_attrs *attrs)
  241. {
  242. struct scatterlist *s;
  243. int i;
  244. for_each_sg(sg, s, nents, i) {
  245. if (!s->dma_length || !s->length)
  246. break;
  247. gart_unmap_page(dev, s->dma_address, s->dma_length, dir, NULL);
  248. }
  249. }
  250. /* Fallback for dma_map_sg in case of overflow */
  251. static int dma_map_sg_nonforce(struct device *dev, struct scatterlist *sg,
  252. int nents, int dir)
  253. {
  254. struct scatterlist *s;
  255. int i;
  256. #ifdef CONFIG_IOMMU_DEBUG
  257. pr_debug("dma_map_sg overflow\n");
  258. #endif
  259. for_each_sg(sg, s, nents, i) {
  260. unsigned long addr = sg_phys(s);
  261. if (nonforced_iommu(dev, addr, s->length)) {
  262. addr = dma_map_area(dev, addr, s->length, dir, 0);
  263. if (addr == bad_dma_addr) {
  264. if (i > 0)
  265. gart_unmap_sg(dev, sg, i, dir, NULL);
  266. nents = 0;
  267. sg[0].dma_length = 0;
  268. break;
  269. }
  270. }
  271. s->dma_address = addr;
  272. s->dma_length = s->length;
  273. }
  274. flush_gart();
  275. return nents;
  276. }
  277. /* Map multiple scatterlist entries continuous into the first. */
  278. static int __dma_map_cont(struct device *dev, struct scatterlist *start,
  279. int nelems, struct scatterlist *sout,
  280. unsigned long pages)
  281. {
  282. unsigned long iommu_start = alloc_iommu(dev, pages, 0);
  283. unsigned long iommu_page = iommu_start;
  284. struct scatterlist *s;
  285. int i;
  286. if (iommu_start == -1)
  287. return -1;
  288. for_each_sg(start, s, nelems, i) {
  289. unsigned long pages, addr;
  290. unsigned long phys_addr = s->dma_address;
  291. BUG_ON(s != start && s->offset);
  292. if (s == start) {
  293. sout->dma_address = iommu_bus_base;
  294. sout->dma_address += iommu_page*PAGE_SIZE + s->offset;
  295. sout->dma_length = s->length;
  296. } else {
  297. sout->dma_length += s->length;
  298. }
  299. addr = phys_addr;
  300. pages = iommu_num_pages(s->offset, s->length, PAGE_SIZE);
  301. while (pages--) {
  302. iommu_gatt_base[iommu_page] = GPTE_ENCODE(addr);
  303. addr += PAGE_SIZE;
  304. iommu_page++;
  305. }
  306. }
  307. BUG_ON(iommu_page - iommu_start != pages);
  308. return 0;
  309. }
  310. static inline int
  311. dma_map_cont(struct device *dev, struct scatterlist *start, int nelems,
  312. struct scatterlist *sout, unsigned long pages, int need)
  313. {
  314. if (!need) {
  315. BUG_ON(nelems != 1);
  316. sout->dma_address = start->dma_address;
  317. sout->dma_length = start->length;
  318. return 0;
  319. }
  320. return __dma_map_cont(dev, start, nelems, sout, pages);
  321. }
  322. /*
  323. * DMA map all entries in a scatterlist.
  324. * Merge chunks that have page aligned sizes into a continuous mapping.
  325. */
  326. static int gart_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  327. enum dma_data_direction dir, struct dma_attrs *attrs)
  328. {
  329. struct scatterlist *s, *ps, *start_sg, *sgmap;
  330. int need = 0, nextneed, i, out, start;
  331. unsigned long pages = 0;
  332. unsigned int seg_size;
  333. unsigned int max_seg_size;
  334. if (nents == 0)
  335. return 0;
  336. if (!dev)
  337. dev = &x86_dma_fallback_dev;
  338. out = 0;
  339. start = 0;
  340. start_sg = sg;
  341. sgmap = sg;
  342. seg_size = 0;
  343. max_seg_size = dma_get_max_seg_size(dev);
  344. ps = NULL; /* shut up gcc */
  345. for_each_sg(sg, s, nents, i) {
  346. dma_addr_t addr = sg_phys(s);
  347. s->dma_address = addr;
  348. BUG_ON(s->length == 0);
  349. nextneed = need_iommu(dev, addr, s->length);
  350. /* Handle the previous not yet processed entries */
  351. if (i > start) {
  352. /*
  353. * Can only merge when the last chunk ends on a
  354. * page boundary and the new one doesn't have an
  355. * offset.
  356. */
  357. if (!iommu_merge || !nextneed || !need || s->offset ||
  358. (s->length + seg_size > max_seg_size) ||
  359. (ps->offset + ps->length) % PAGE_SIZE) {
  360. if (dma_map_cont(dev, start_sg, i - start,
  361. sgmap, pages, need) < 0)
  362. goto error;
  363. out++;
  364. seg_size = 0;
  365. sgmap = sg_next(sgmap);
  366. pages = 0;
  367. start = i;
  368. start_sg = s;
  369. }
  370. }
  371. seg_size += s->length;
  372. need = nextneed;
  373. pages += iommu_num_pages(s->offset, s->length, PAGE_SIZE);
  374. ps = s;
  375. }
  376. if (dma_map_cont(dev, start_sg, i - start, sgmap, pages, need) < 0)
  377. goto error;
  378. out++;
  379. flush_gart();
  380. if (out < nents) {
  381. sgmap = sg_next(sgmap);
  382. sgmap->dma_length = 0;
  383. }
  384. return out;
  385. error:
  386. flush_gart();
  387. gart_unmap_sg(dev, sg, out, dir, NULL);
  388. /* When it was forced or merged try again in a dumb way */
  389. if (force_iommu || iommu_merge) {
  390. out = dma_map_sg_nonforce(dev, sg, nents, dir);
  391. if (out > 0)
  392. return out;
  393. }
  394. if (panic_on_overflow)
  395. panic("dma_map_sg: overflow on %lu pages\n", pages);
  396. iommu_full(dev, pages << PAGE_SHIFT, dir);
  397. for_each_sg(sg, s, nents, i)
  398. s->dma_address = bad_dma_addr;
  399. return 0;
  400. }
  401. /* allocate and map a coherent mapping */
  402. static void *
  403. gart_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_addr,
  404. gfp_t flag)
  405. {
  406. dma_addr_t paddr;
  407. unsigned long align_mask;
  408. struct page *page;
  409. if (force_iommu && !(flag & GFP_DMA)) {
  410. flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
  411. page = alloc_pages(flag | __GFP_ZERO, get_order(size));
  412. if (!page)
  413. return NULL;
  414. align_mask = (1UL << get_order(size)) - 1;
  415. paddr = dma_map_area(dev, page_to_phys(page), size,
  416. DMA_BIDIRECTIONAL, align_mask);
  417. flush_gart();
  418. if (paddr != bad_dma_addr) {
  419. *dma_addr = paddr;
  420. return page_address(page);
  421. }
  422. __free_pages(page, get_order(size));
  423. } else
  424. return dma_generic_alloc_coherent(dev, size, dma_addr, flag);
  425. return NULL;
  426. }
  427. /* free a coherent mapping */
  428. static void
  429. gart_free_coherent(struct device *dev, size_t size, void *vaddr,
  430. dma_addr_t dma_addr)
  431. {
  432. gart_unmap_page(dev, dma_addr, size, DMA_BIDIRECTIONAL, NULL);
  433. free_pages((unsigned long)vaddr, get_order(size));
  434. }
  435. static int gart_mapping_error(struct device *dev, dma_addr_t dma_addr)
  436. {
  437. return (dma_addr == bad_dma_addr);
  438. }
  439. static int no_agp;
  440. static __init unsigned long check_iommu_size(unsigned long aper, u64 aper_size)
  441. {
  442. unsigned long a;
  443. if (!iommu_size) {
  444. iommu_size = aper_size;
  445. if (!no_agp)
  446. iommu_size /= 2;
  447. }
  448. a = aper + iommu_size;
  449. iommu_size -= round_up(a, PMD_PAGE_SIZE) - a;
  450. if (iommu_size < 64*1024*1024) {
  451. pr_warning(
  452. "PCI-DMA: Warning: Small IOMMU %luMB."
  453. " Consider increasing the AGP aperture in BIOS\n",
  454. iommu_size >> 20);
  455. }
  456. return iommu_size;
  457. }
  458. static __init unsigned read_aperture(struct pci_dev *dev, u32 *size)
  459. {
  460. unsigned aper_size = 0, aper_base_32, aper_order;
  461. u64 aper_base;
  462. pci_read_config_dword(dev, AMD64_GARTAPERTUREBASE, &aper_base_32);
  463. pci_read_config_dword(dev, AMD64_GARTAPERTURECTL, &aper_order);
  464. aper_order = (aper_order >> 1) & 7;
  465. aper_base = aper_base_32 & 0x7fff;
  466. aper_base <<= 25;
  467. aper_size = (32 * 1024 * 1024) << aper_order;
  468. if (aper_base + aper_size > 0x100000000UL || !aper_size)
  469. aper_base = 0;
  470. *size = aper_size;
  471. return aper_base;
  472. }
  473. static void enable_gart_translations(void)
  474. {
  475. int i;
  476. for (i = 0; i < num_k8_northbridges; i++) {
  477. struct pci_dev *dev = k8_northbridges[i];
  478. enable_gart_translation(dev, __pa(agp_gatt_table));
  479. }
  480. /* Flush the GART-TLB to remove stale entries */
  481. k8_flush_garts();
  482. }
  483. /*
  484. * If fix_up_north_bridges is set, the north bridges have to be fixed up on
  485. * resume in the same way as they are handled in gart_iommu_hole_init().
  486. */
  487. static bool fix_up_north_bridges;
  488. static u32 aperture_order;
  489. static u32 aperture_alloc;
  490. void set_up_gart_resume(u32 aper_order, u32 aper_alloc)
  491. {
  492. fix_up_north_bridges = true;
  493. aperture_order = aper_order;
  494. aperture_alloc = aper_alloc;
  495. }
  496. static void gart_fixup_northbridges(struct sys_device *dev)
  497. {
  498. int i;
  499. if (!fix_up_north_bridges)
  500. return;
  501. pr_info("PCI-DMA: Restoring GART aperture settings\n");
  502. for (i = 0; i < num_k8_northbridges; i++) {
  503. struct pci_dev *dev = k8_northbridges[i];
  504. /*
  505. * Don't enable translations just yet. That is the next
  506. * step. Restore the pre-suspend aperture settings.
  507. */
  508. pci_write_config_dword(dev, AMD64_GARTAPERTURECTL, aperture_order << 1);
  509. pci_write_config_dword(dev, AMD64_GARTAPERTUREBASE, aperture_alloc >> 25);
  510. }
  511. }
  512. static int gart_resume(struct sys_device *dev)
  513. {
  514. pr_info("PCI-DMA: Resuming GART IOMMU\n");
  515. gart_fixup_northbridges(dev);
  516. enable_gart_translations();
  517. return 0;
  518. }
  519. static int gart_suspend(struct sys_device *dev, pm_message_t state)
  520. {
  521. return 0;
  522. }
  523. static struct sysdev_class gart_sysdev_class = {
  524. .name = "gart",
  525. .suspend = gart_suspend,
  526. .resume = gart_resume,
  527. };
  528. static struct sys_device device_gart = {
  529. .cls = &gart_sysdev_class,
  530. };
  531. /*
  532. * Private Northbridge GATT initialization in case we cannot use the
  533. * AGP driver for some reason.
  534. */
  535. static __init int init_k8_gatt(struct agp_kern_info *info)
  536. {
  537. unsigned aper_size, gatt_size, new_aper_size;
  538. unsigned aper_base, new_aper_base;
  539. struct pci_dev *dev;
  540. void *gatt;
  541. int i, error;
  542. pr_info("PCI-DMA: Disabling AGP.\n");
  543. aper_size = aper_base = info->aper_size = 0;
  544. dev = NULL;
  545. for (i = 0; i < num_k8_northbridges; i++) {
  546. dev = k8_northbridges[i];
  547. new_aper_base = read_aperture(dev, &new_aper_size);
  548. if (!new_aper_base)
  549. goto nommu;
  550. if (!aper_base) {
  551. aper_size = new_aper_size;
  552. aper_base = new_aper_base;
  553. }
  554. if (aper_size != new_aper_size || aper_base != new_aper_base)
  555. goto nommu;
  556. }
  557. if (!aper_base)
  558. goto nommu;
  559. info->aper_base = aper_base;
  560. info->aper_size = aper_size >> 20;
  561. gatt_size = (aper_size >> PAGE_SHIFT) * sizeof(u32);
  562. gatt = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  563. get_order(gatt_size));
  564. if (!gatt)
  565. panic("Cannot allocate GATT table");
  566. if (set_memory_uc((unsigned long)gatt, gatt_size >> PAGE_SHIFT))
  567. panic("Could not set GART PTEs to uncacheable pages");
  568. agp_gatt_table = gatt;
  569. error = sysdev_class_register(&gart_sysdev_class);
  570. if (!error)
  571. error = sysdev_register(&device_gart);
  572. if (error)
  573. panic("Could not register gart_sysdev -- "
  574. "would corrupt data on next suspend");
  575. flush_gart();
  576. pr_info("PCI-DMA: aperture base @ %x size %u KB\n",
  577. aper_base, aper_size>>10);
  578. return 0;
  579. nommu:
  580. /* Should not happen anymore */
  581. pr_warning("PCI-DMA: More than 4GB of RAM and no IOMMU\n"
  582. "falling back to iommu=soft.\n");
  583. return -1;
  584. }
  585. static struct dma_map_ops gart_dma_ops = {
  586. .map_sg = gart_map_sg,
  587. .unmap_sg = gart_unmap_sg,
  588. .map_page = gart_map_page,
  589. .unmap_page = gart_unmap_page,
  590. .alloc_coherent = gart_alloc_coherent,
  591. .free_coherent = gart_free_coherent,
  592. .mapping_error = gart_mapping_error,
  593. };
  594. static void gart_iommu_shutdown(void)
  595. {
  596. struct pci_dev *dev;
  597. int i;
  598. /* don't shutdown it if there is AGP installed */
  599. if (!no_agp)
  600. return;
  601. for (i = 0; i < num_k8_northbridges; i++) {
  602. u32 ctl;
  603. dev = k8_northbridges[i];
  604. pci_read_config_dword(dev, AMD64_GARTAPERTURECTL, &ctl);
  605. ctl &= ~GARTEN;
  606. pci_write_config_dword(dev, AMD64_GARTAPERTURECTL, ctl);
  607. }
  608. }
  609. int __init gart_iommu_init(void)
  610. {
  611. struct agp_kern_info info;
  612. unsigned long iommu_start;
  613. unsigned long aper_base, aper_size;
  614. unsigned long start_pfn, end_pfn;
  615. unsigned long scratch;
  616. long i;
  617. if (num_k8_northbridges == 0)
  618. return 0;
  619. #ifndef CONFIG_AGP_AMD64
  620. no_agp = 1;
  621. #else
  622. /* Makefile puts PCI initialization via subsys_initcall first. */
  623. /* Add other K8 AGP bridge drivers here */
  624. no_agp = no_agp ||
  625. (agp_amd64_init() < 0) ||
  626. (agp_copy_info(agp_bridge, &info) < 0);
  627. #endif
  628. if (no_iommu ||
  629. (!force_iommu && max_pfn <= MAX_DMA32_PFN) ||
  630. !gart_iommu_aperture ||
  631. (no_agp && init_k8_gatt(&info) < 0)) {
  632. if (max_pfn > MAX_DMA32_PFN) {
  633. pr_warning("More than 4GB of memory but GART IOMMU not available.\n");
  634. pr_warning("falling back to iommu=soft.\n");
  635. }
  636. return 0;
  637. }
  638. /* need to map that range */
  639. aper_size = info.aper_size << 20;
  640. aper_base = info.aper_base;
  641. end_pfn = (aper_base>>PAGE_SHIFT) + (aper_size>>PAGE_SHIFT);
  642. if (end_pfn > max_low_pfn_mapped) {
  643. start_pfn = (aper_base>>PAGE_SHIFT);
  644. init_memory_mapping(start_pfn<<PAGE_SHIFT, end_pfn<<PAGE_SHIFT);
  645. }
  646. pr_info("PCI-DMA: using GART IOMMU.\n");
  647. iommu_size = check_iommu_size(info.aper_base, aper_size);
  648. iommu_pages = iommu_size >> PAGE_SHIFT;
  649. iommu_gart_bitmap = (void *) __get_free_pages(GFP_KERNEL | __GFP_ZERO,
  650. get_order(iommu_pages/8));
  651. if (!iommu_gart_bitmap)
  652. panic("Cannot allocate iommu bitmap\n");
  653. #ifdef CONFIG_IOMMU_LEAK
  654. if (leak_trace) {
  655. int ret;
  656. ret = dma_debug_resize_entries(iommu_pages);
  657. if (ret)
  658. pr_debug("PCI-DMA: Cannot trace all the entries\n");
  659. }
  660. #endif
  661. /*
  662. * Out of IOMMU space handling.
  663. * Reserve some invalid pages at the beginning of the GART.
  664. */
  665. bitmap_set(iommu_gart_bitmap, 0, EMERGENCY_PAGES);
  666. pr_info("PCI-DMA: Reserving %luMB of IOMMU area in the AGP aperture\n",
  667. iommu_size >> 20);
  668. agp_memory_reserved = iommu_size;
  669. iommu_start = aper_size - iommu_size;
  670. iommu_bus_base = info.aper_base + iommu_start;
  671. bad_dma_addr = iommu_bus_base;
  672. iommu_gatt_base = agp_gatt_table + (iommu_start>>PAGE_SHIFT);
  673. /*
  674. * Unmap the IOMMU part of the GART. The alias of the page is
  675. * always mapped with cache enabled and there is no full cache
  676. * coherency across the GART remapping. The unmapping avoids
  677. * automatic prefetches from the CPU allocating cache lines in
  678. * there. All CPU accesses are done via the direct mapping to
  679. * the backing memory. The GART address is only used by PCI
  680. * devices.
  681. */
  682. set_memory_np((unsigned long)__va(iommu_bus_base),
  683. iommu_size >> PAGE_SHIFT);
  684. /*
  685. * Tricky. The GART table remaps the physical memory range,
  686. * so the CPU wont notice potential aliases and if the memory
  687. * is remapped to UC later on, we might surprise the PCI devices
  688. * with a stray writeout of a cacheline. So play it sure and
  689. * do an explicit, full-scale wbinvd() _after_ having marked all
  690. * the pages as Not-Present:
  691. */
  692. wbinvd();
  693. /*
  694. * Now all caches are flushed and we can safely enable
  695. * GART hardware. Doing it early leaves the possibility
  696. * of stale cache entries that can lead to GART PTE
  697. * errors.
  698. */
  699. enable_gart_translations();
  700. /*
  701. * Try to workaround a bug (thanks to BenH):
  702. * Set unmapped entries to a scratch page instead of 0.
  703. * Any prefetches that hit unmapped entries won't get an bus abort
  704. * then. (P2P bridge may be prefetching on DMA reads).
  705. */
  706. scratch = get_zeroed_page(GFP_KERNEL);
  707. if (!scratch)
  708. panic("Cannot allocate iommu scratch page");
  709. gart_unmapped_entry = GPTE_ENCODE(__pa(scratch));
  710. for (i = EMERGENCY_PAGES; i < iommu_pages; i++)
  711. iommu_gatt_base[i] = gart_unmapped_entry;
  712. flush_gart();
  713. dma_ops = &gart_dma_ops;
  714. x86_platform.iommu_shutdown = gart_iommu_shutdown;
  715. swiotlb = 0;
  716. return 0;
  717. }
  718. void __init gart_parse_options(char *p)
  719. {
  720. int arg;
  721. #ifdef CONFIG_IOMMU_LEAK
  722. if (!strncmp(p, "leak", 4)) {
  723. leak_trace = 1;
  724. p += 4;
  725. if (*p == '=')
  726. ++p;
  727. if (isdigit(*p) && get_option(&p, &arg))
  728. iommu_leak_pages = arg;
  729. }
  730. #endif
  731. if (isdigit(*p) && get_option(&p, &arg))
  732. iommu_size = arg;
  733. if (!strncmp(p, "fullflush", 9))
  734. iommu_fullflush = 1;
  735. if (!strncmp(p, "nofullflush", 11))
  736. iommu_fullflush = 0;
  737. if (!strncmp(p, "noagp", 5))
  738. no_agp = 1;
  739. if (!strncmp(p, "noaperture", 10))
  740. fix_aperture = 0;
  741. /* duplicated from pci-dma.c */
  742. if (!strncmp(p, "force", 5))
  743. gart_iommu_aperture_allowed = 1;
  744. if (!strncmp(p, "allowed", 7))
  745. gart_iommu_aperture_allowed = 1;
  746. if (!strncmp(p, "memaper", 7)) {
  747. fallback_aper_force = 1;
  748. p += 7;
  749. if (*p == '=') {
  750. ++p;
  751. if (get_option(&p, &arg))
  752. fallback_aper_order = arg;
  753. }
  754. }
  755. }