pci-gart_64.c 23 KB

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