pci-gart_64.c 23 KB

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