pci_iommu.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. /*
  2. * linux/arch/alpha/kernel/pci_iommu.c
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/mm.h>
  6. #include <linux/pci.h>
  7. #include <linux/slab.h>
  8. #include <linux/bootmem.h>
  9. #include <linux/scatterlist.h>
  10. #include <linux/log2.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/iommu-helper.h>
  13. #include <asm/io.h>
  14. #include <asm/hwrpb.h>
  15. #include "proto.h"
  16. #include "pci_impl.h"
  17. #define DEBUG_ALLOC 0
  18. #if DEBUG_ALLOC > 0
  19. # define DBGA(args...) printk(KERN_DEBUG args)
  20. #else
  21. # define DBGA(args...)
  22. #endif
  23. #if DEBUG_ALLOC > 1
  24. # define DBGA2(args...) printk(KERN_DEBUG args)
  25. #else
  26. # define DBGA2(args...)
  27. #endif
  28. #define DEBUG_NODIRECT 0
  29. #define ISA_DMA_MASK 0x00ffffff
  30. static inline unsigned long
  31. mk_iommu_pte(unsigned long paddr)
  32. {
  33. return (paddr >> (PAGE_SHIFT-1)) | 1;
  34. }
  35. static inline long
  36. calc_npages(long bytes)
  37. {
  38. return (bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
  39. }
  40. /* Return the minimum of MAX or the first power of two larger
  41. than main memory. */
  42. unsigned long
  43. size_for_memory(unsigned long max)
  44. {
  45. unsigned long mem = max_low_pfn << PAGE_SHIFT;
  46. if (mem < max)
  47. max = roundup_pow_of_two(mem);
  48. return max;
  49. }
  50. struct pci_iommu_arena * __init
  51. iommu_arena_new_node(int nid, struct pci_controller *hose, dma_addr_t base,
  52. unsigned long window_size, unsigned long align)
  53. {
  54. unsigned long mem_size;
  55. struct pci_iommu_arena *arena;
  56. mem_size = window_size / (PAGE_SIZE / sizeof(unsigned long));
  57. /* Note that the TLB lookup logic uses bitwise concatenation,
  58. not addition, so the required arena alignment is based on
  59. the size of the window. Retain the align parameter so that
  60. particular systems can over-align the arena. */
  61. if (align < mem_size)
  62. align = mem_size;
  63. #ifdef CONFIG_DISCONTIGMEM
  64. arena = alloc_bootmem_node(NODE_DATA(nid), sizeof(*arena));
  65. if (!NODE_DATA(nid) || !arena) {
  66. printk("%s: couldn't allocate arena from node %d\n"
  67. " falling back to system-wide allocation\n",
  68. __func__, nid);
  69. arena = alloc_bootmem(sizeof(*arena));
  70. }
  71. arena->ptes = __alloc_bootmem_node(NODE_DATA(nid), mem_size, align, 0);
  72. if (!NODE_DATA(nid) || !arena->ptes) {
  73. printk("%s: couldn't allocate arena ptes from node %d\n"
  74. " falling back to system-wide allocation\n",
  75. __func__, nid);
  76. arena->ptes = __alloc_bootmem(mem_size, align, 0);
  77. }
  78. #else /* CONFIG_DISCONTIGMEM */
  79. arena = alloc_bootmem(sizeof(*arena));
  80. arena->ptes = __alloc_bootmem(mem_size, align, 0);
  81. #endif /* CONFIG_DISCONTIGMEM */
  82. spin_lock_init(&arena->lock);
  83. arena->hose = hose;
  84. arena->dma_base = base;
  85. arena->size = window_size;
  86. arena->next_entry = 0;
  87. /* Align allocations to a multiple of a page size. Not needed
  88. unless there are chip bugs. */
  89. arena->align_entry = 1;
  90. return arena;
  91. }
  92. struct pci_iommu_arena * __init
  93. iommu_arena_new(struct pci_controller *hose, dma_addr_t base,
  94. unsigned long window_size, unsigned long align)
  95. {
  96. return iommu_arena_new_node(0, hose, base, window_size, align);
  97. }
  98. /* Must be called with the arena lock held */
  99. static long
  100. iommu_arena_find_pages(struct device *dev, struct pci_iommu_arena *arena,
  101. long n, long mask)
  102. {
  103. unsigned long *ptes;
  104. long i, p, nent;
  105. int pass = 0;
  106. unsigned long base;
  107. unsigned long boundary_size;
  108. base = arena->dma_base >> PAGE_SHIFT;
  109. if (dev) {
  110. boundary_size = dma_get_seg_boundary(dev) + 1;
  111. boundary_size >>= PAGE_SHIFT;
  112. } else {
  113. boundary_size = 1UL << (32 - PAGE_SHIFT);
  114. }
  115. /* Search forward for the first mask-aligned sequence of N free ptes */
  116. ptes = arena->ptes;
  117. nent = arena->size >> PAGE_SHIFT;
  118. p = ALIGN(arena->next_entry, mask + 1);
  119. i = 0;
  120. again:
  121. while (i < n && p+i < nent) {
  122. if (!i && iommu_is_span_boundary(p, n, base, boundary_size)) {
  123. p = ALIGN(p + 1, mask + 1);
  124. goto again;
  125. }
  126. if (ptes[p+i])
  127. p = ALIGN(p + i + 1, mask + 1), i = 0;
  128. else
  129. i = i + 1;
  130. }
  131. if (i < n) {
  132. if (pass < 1) {
  133. /*
  134. * Reached the end. Flush the TLB and restart
  135. * the search from the beginning.
  136. */
  137. alpha_mv.mv_pci_tbi(arena->hose, 0, -1);
  138. pass++;
  139. p = 0;
  140. i = 0;
  141. goto again;
  142. } else
  143. return -1;
  144. }
  145. /* Success. It's the responsibility of the caller to mark them
  146. in use before releasing the lock */
  147. return p;
  148. }
  149. static long
  150. iommu_arena_alloc(struct device *dev, struct pci_iommu_arena *arena, long n,
  151. unsigned int align)
  152. {
  153. unsigned long flags;
  154. unsigned long *ptes;
  155. long i, p, mask;
  156. spin_lock_irqsave(&arena->lock, flags);
  157. /* Search for N empty ptes */
  158. ptes = arena->ptes;
  159. mask = max(align, arena->align_entry) - 1;
  160. p = iommu_arena_find_pages(dev, arena, n, mask);
  161. if (p < 0) {
  162. spin_unlock_irqrestore(&arena->lock, flags);
  163. return -1;
  164. }
  165. /* Success. Mark them all in use, ie not zero and invalid
  166. for the iommu tlb that could load them from under us.
  167. The chip specific bits will fill this in with something
  168. kosher when we return. */
  169. for (i = 0; i < n; ++i)
  170. ptes[p+i] = IOMMU_INVALID_PTE;
  171. arena->next_entry = p + n;
  172. spin_unlock_irqrestore(&arena->lock, flags);
  173. return p;
  174. }
  175. static void
  176. iommu_arena_free(struct pci_iommu_arena *arena, long ofs, long n)
  177. {
  178. unsigned long *p;
  179. long i;
  180. p = arena->ptes + ofs;
  181. for (i = 0; i < n; ++i)
  182. p[i] = 0;
  183. }
  184. /* True if the machine supports DAC addressing, and DEV can
  185. make use of it given MASK. */
  186. static int pci_dac_dma_supported(struct pci_dev *hwdev, u64 mask);
  187. /* Map a single buffer of the indicated size for PCI DMA in streaming
  188. mode. The 32-bit PCI bus mastering address to use is returned.
  189. Once the device is given the dma address, the device owns this memory
  190. until either pci_unmap_single or pci_dma_sync_single is performed. */
  191. static dma_addr_t
  192. pci_map_single_1(struct pci_dev *pdev, void *cpu_addr, size_t size,
  193. int dac_allowed)
  194. {
  195. struct pci_controller *hose = pdev ? pdev->sysdata : pci_isa_hose;
  196. dma_addr_t max_dma = pdev ? pdev->dma_mask : ISA_DMA_MASK;
  197. struct pci_iommu_arena *arena;
  198. long npages, dma_ofs, i;
  199. unsigned long paddr;
  200. dma_addr_t ret;
  201. unsigned int align = 0;
  202. struct device *dev = pdev ? &pdev->dev : NULL;
  203. paddr = __pa(cpu_addr);
  204. #if !DEBUG_NODIRECT
  205. /* First check to see if we can use the direct map window. */
  206. if (paddr + size + __direct_map_base - 1 <= max_dma
  207. && paddr + size <= __direct_map_size) {
  208. ret = paddr + __direct_map_base;
  209. DBGA2("pci_map_single: [%p,%lx] -> direct %lx from %p\n",
  210. cpu_addr, size, ret, __builtin_return_address(0));
  211. return ret;
  212. }
  213. #endif
  214. /* Next, use DAC if selected earlier. */
  215. if (dac_allowed) {
  216. ret = paddr + alpha_mv.pci_dac_offset;
  217. DBGA2("pci_map_single: [%p,%lx] -> DAC %lx from %p\n",
  218. cpu_addr, size, ret, __builtin_return_address(0));
  219. return ret;
  220. }
  221. /* If the machine doesn't define a pci_tbi routine, we have to
  222. assume it doesn't support sg mapping, and, since we tried to
  223. use direct_map above, it now must be considered an error. */
  224. if (! alpha_mv.mv_pci_tbi) {
  225. static int been_here = 0; /* Only print the message once. */
  226. if (!been_here) {
  227. printk(KERN_WARNING "pci_map_single: no HW sg\n");
  228. been_here = 1;
  229. }
  230. return 0;
  231. }
  232. arena = hose->sg_pci;
  233. if (!arena || arena->dma_base + arena->size - 1 > max_dma)
  234. arena = hose->sg_isa;
  235. npages = calc_npages((paddr & ~PAGE_MASK) + size);
  236. /* Force allocation to 64KB boundary for ISA bridges. */
  237. if (pdev && pdev == isa_bridge)
  238. align = 8;
  239. dma_ofs = iommu_arena_alloc(dev, arena, npages, align);
  240. if (dma_ofs < 0) {
  241. printk(KERN_WARNING "pci_map_single failed: "
  242. "could not allocate dma page tables\n");
  243. return 0;
  244. }
  245. paddr &= PAGE_MASK;
  246. for (i = 0; i < npages; ++i, paddr += PAGE_SIZE)
  247. arena->ptes[i + dma_ofs] = mk_iommu_pte(paddr);
  248. ret = arena->dma_base + dma_ofs * PAGE_SIZE;
  249. ret += (unsigned long)cpu_addr & ~PAGE_MASK;
  250. DBGA2("pci_map_single: [%p,%lx] np %ld -> sg %lx from %p\n",
  251. cpu_addr, size, npages, ret, __builtin_return_address(0));
  252. return ret;
  253. }
  254. dma_addr_t
  255. pci_map_single(struct pci_dev *pdev, void *cpu_addr, size_t size, int dir)
  256. {
  257. int dac_allowed;
  258. if (dir == PCI_DMA_NONE)
  259. BUG();
  260. dac_allowed = pdev ? pci_dac_dma_supported(pdev, pdev->dma_mask) : 0;
  261. return pci_map_single_1(pdev, cpu_addr, size, dac_allowed);
  262. }
  263. EXPORT_SYMBOL(pci_map_single);
  264. dma_addr_t
  265. pci_map_page(struct pci_dev *pdev, struct page *page, unsigned long offset,
  266. size_t size, int dir)
  267. {
  268. int dac_allowed;
  269. if (dir == PCI_DMA_NONE)
  270. BUG();
  271. dac_allowed = pdev ? pci_dac_dma_supported(pdev, pdev->dma_mask) : 0;
  272. return pci_map_single_1(pdev, (char *)page_address(page) + offset,
  273. size, dac_allowed);
  274. }
  275. EXPORT_SYMBOL(pci_map_page);
  276. /* Unmap a single streaming mode DMA translation. The DMA_ADDR and
  277. SIZE must match what was provided for in a previous pci_map_single
  278. call. All other usages are undefined. After this call, reads by
  279. the cpu to the buffer are guaranteed to see whatever the device
  280. wrote there. */
  281. void
  282. pci_unmap_single(struct pci_dev *pdev, dma_addr_t dma_addr, size_t size,
  283. int direction)
  284. {
  285. unsigned long flags;
  286. struct pci_controller *hose = pdev ? pdev->sysdata : pci_isa_hose;
  287. struct pci_iommu_arena *arena;
  288. long dma_ofs, npages;
  289. if (direction == PCI_DMA_NONE)
  290. BUG();
  291. if (dma_addr >= __direct_map_base
  292. && dma_addr < __direct_map_base + __direct_map_size) {
  293. /* Nothing to do. */
  294. DBGA2("pci_unmap_single: direct [%lx,%lx] from %p\n",
  295. dma_addr, size, __builtin_return_address(0));
  296. return;
  297. }
  298. if (dma_addr > 0xffffffff) {
  299. DBGA2("pci64_unmap_single: DAC [%lx,%lx] from %p\n",
  300. dma_addr, size, __builtin_return_address(0));
  301. return;
  302. }
  303. arena = hose->sg_pci;
  304. if (!arena || dma_addr < arena->dma_base)
  305. arena = hose->sg_isa;
  306. dma_ofs = (dma_addr - arena->dma_base) >> PAGE_SHIFT;
  307. if (dma_ofs * PAGE_SIZE >= arena->size) {
  308. printk(KERN_ERR "Bogus pci_unmap_single: dma_addr %lx "
  309. " base %lx size %x\n", dma_addr, arena->dma_base,
  310. arena->size);
  311. return;
  312. BUG();
  313. }
  314. npages = calc_npages((dma_addr & ~PAGE_MASK) + size);
  315. spin_lock_irqsave(&arena->lock, flags);
  316. iommu_arena_free(arena, dma_ofs, npages);
  317. /* If we're freeing ptes above the `next_entry' pointer (they
  318. may have snuck back into the TLB since the last wrap flush),
  319. we need to flush the TLB before reallocating the latter. */
  320. if (dma_ofs >= arena->next_entry)
  321. alpha_mv.mv_pci_tbi(hose, dma_addr, dma_addr + size - 1);
  322. spin_unlock_irqrestore(&arena->lock, flags);
  323. DBGA2("pci_unmap_single: sg [%lx,%lx] np %ld from %p\n",
  324. dma_addr, size, npages, __builtin_return_address(0));
  325. }
  326. EXPORT_SYMBOL(pci_unmap_single);
  327. void
  328. pci_unmap_page(struct pci_dev *pdev, dma_addr_t dma_addr,
  329. size_t size, int direction)
  330. {
  331. pci_unmap_single(pdev, dma_addr, size, direction);
  332. }
  333. EXPORT_SYMBOL(pci_unmap_page);
  334. /* Allocate and map kernel buffer using consistent mode DMA for PCI
  335. device. Returns non-NULL cpu-view pointer to the buffer if
  336. successful and sets *DMA_ADDRP to the pci side dma address as well,
  337. else DMA_ADDRP is undefined. */
  338. void *
  339. __pci_alloc_consistent(struct pci_dev *pdev, size_t size,
  340. dma_addr_t *dma_addrp, gfp_t gfp)
  341. {
  342. void *cpu_addr;
  343. long order = get_order(size);
  344. gfp &= ~GFP_DMA;
  345. try_again:
  346. cpu_addr = (void *)__get_free_pages(gfp, order);
  347. if (! cpu_addr) {
  348. printk(KERN_INFO "pci_alloc_consistent: "
  349. "get_free_pages failed from %p\n",
  350. __builtin_return_address(0));
  351. /* ??? Really atomic allocation? Otherwise we could play
  352. with vmalloc and sg if we can't find contiguous memory. */
  353. return NULL;
  354. }
  355. memset(cpu_addr, 0, size);
  356. *dma_addrp = pci_map_single_1(pdev, cpu_addr, size, 0);
  357. if (*dma_addrp == 0) {
  358. free_pages((unsigned long)cpu_addr, order);
  359. if (alpha_mv.mv_pci_tbi || (gfp & GFP_DMA))
  360. return NULL;
  361. /* The address doesn't fit required mask and we
  362. do not have iommu. Try again with GFP_DMA. */
  363. gfp |= GFP_DMA;
  364. goto try_again;
  365. }
  366. DBGA2("pci_alloc_consistent: %lx -> [%p,%x] from %p\n",
  367. size, cpu_addr, *dma_addrp, __builtin_return_address(0));
  368. return cpu_addr;
  369. }
  370. EXPORT_SYMBOL(__pci_alloc_consistent);
  371. /* Free and unmap a consistent DMA buffer. CPU_ADDR and DMA_ADDR must
  372. be values that were returned from pci_alloc_consistent. SIZE must
  373. be the same as what as passed into pci_alloc_consistent.
  374. References to the memory and mappings associated with CPU_ADDR or
  375. DMA_ADDR past this call are illegal. */
  376. void
  377. pci_free_consistent(struct pci_dev *pdev, size_t size, void *cpu_addr,
  378. dma_addr_t dma_addr)
  379. {
  380. pci_unmap_single(pdev, dma_addr, size, PCI_DMA_BIDIRECTIONAL);
  381. free_pages((unsigned long)cpu_addr, get_order(size));
  382. DBGA2("pci_free_consistent: [%x,%lx] from %p\n",
  383. dma_addr, size, __builtin_return_address(0));
  384. }
  385. EXPORT_SYMBOL(pci_free_consistent);
  386. /* Classify the elements of the scatterlist. Write dma_address
  387. of each element with:
  388. 0 : Followers all physically adjacent.
  389. 1 : Followers all virtually adjacent.
  390. -1 : Not leader, physically adjacent to previous.
  391. -2 : Not leader, virtually adjacent to previous.
  392. Write dma_length of each leader with the combined lengths of
  393. the mergable followers. */
  394. #define SG_ENT_VIRT_ADDRESS(SG) (sg_virt((SG)))
  395. #define SG_ENT_PHYS_ADDRESS(SG) __pa(SG_ENT_VIRT_ADDRESS(SG))
  396. static void
  397. sg_classify(struct device *dev, struct scatterlist *sg, struct scatterlist *end,
  398. int virt_ok)
  399. {
  400. unsigned long next_paddr;
  401. struct scatterlist *leader;
  402. long leader_flag, leader_length;
  403. unsigned int max_seg_size;
  404. leader = sg;
  405. leader_flag = 0;
  406. leader_length = leader->length;
  407. next_paddr = SG_ENT_PHYS_ADDRESS(leader) + leader_length;
  408. /* we will not marge sg without device. */
  409. max_seg_size = dev ? dma_get_max_seg_size(dev) : 0;
  410. for (++sg; sg < end; ++sg) {
  411. unsigned long addr, len;
  412. addr = SG_ENT_PHYS_ADDRESS(sg);
  413. len = sg->length;
  414. if (leader_length + len > max_seg_size)
  415. goto new_segment;
  416. if (next_paddr == addr) {
  417. sg->dma_address = -1;
  418. leader_length += len;
  419. } else if (((next_paddr | addr) & ~PAGE_MASK) == 0 && virt_ok) {
  420. sg->dma_address = -2;
  421. leader_flag = 1;
  422. leader_length += len;
  423. } else {
  424. new_segment:
  425. leader->dma_address = leader_flag;
  426. leader->dma_length = leader_length;
  427. leader = sg;
  428. leader_flag = 0;
  429. leader_length = len;
  430. }
  431. next_paddr = addr + len;
  432. }
  433. leader->dma_address = leader_flag;
  434. leader->dma_length = leader_length;
  435. }
  436. /* Given a scatterlist leader, choose an allocation method and fill
  437. in the blanks. */
  438. static int
  439. sg_fill(struct device *dev, struct scatterlist *leader, struct scatterlist *end,
  440. struct scatterlist *out, struct pci_iommu_arena *arena,
  441. dma_addr_t max_dma, int dac_allowed)
  442. {
  443. unsigned long paddr = SG_ENT_PHYS_ADDRESS(leader);
  444. long size = leader->dma_length;
  445. struct scatterlist *sg;
  446. unsigned long *ptes;
  447. long npages, dma_ofs, i;
  448. #if !DEBUG_NODIRECT
  449. /* If everything is physically contiguous, and the addresses
  450. fall into the direct-map window, use it. */
  451. if (leader->dma_address == 0
  452. && paddr + size + __direct_map_base - 1 <= max_dma
  453. && paddr + size <= __direct_map_size) {
  454. out->dma_address = paddr + __direct_map_base;
  455. out->dma_length = size;
  456. DBGA(" sg_fill: [%p,%lx] -> direct %lx\n",
  457. __va(paddr), size, out->dma_address);
  458. return 0;
  459. }
  460. #endif
  461. /* If physically contiguous and DAC is available, use it. */
  462. if (leader->dma_address == 0 && dac_allowed) {
  463. out->dma_address = paddr + alpha_mv.pci_dac_offset;
  464. out->dma_length = size;
  465. DBGA(" sg_fill: [%p,%lx] -> DAC %lx\n",
  466. __va(paddr), size, out->dma_address);
  467. return 0;
  468. }
  469. /* Otherwise, we'll use the iommu to make the pages virtually
  470. contiguous. */
  471. paddr &= ~PAGE_MASK;
  472. npages = calc_npages(paddr + size);
  473. dma_ofs = iommu_arena_alloc(dev, arena, npages, 0);
  474. if (dma_ofs < 0) {
  475. /* If we attempted a direct map above but failed, die. */
  476. if (leader->dma_address == 0)
  477. return -1;
  478. /* Otherwise, break up the remaining virtually contiguous
  479. hunks into individual direct maps and retry. */
  480. sg_classify(dev, leader, end, 0);
  481. return sg_fill(dev, leader, end, out, arena, max_dma, dac_allowed);
  482. }
  483. out->dma_address = arena->dma_base + dma_ofs*PAGE_SIZE + paddr;
  484. out->dma_length = size;
  485. DBGA(" sg_fill: [%p,%lx] -> sg %lx np %ld\n",
  486. __va(paddr), size, out->dma_address, npages);
  487. /* All virtually contiguous. We need to find the length of each
  488. physically contiguous subsegment to fill in the ptes. */
  489. ptes = &arena->ptes[dma_ofs];
  490. sg = leader;
  491. do {
  492. #if DEBUG_ALLOC > 0
  493. struct scatterlist *last_sg = sg;
  494. #endif
  495. size = sg->length;
  496. paddr = SG_ENT_PHYS_ADDRESS(sg);
  497. while (sg+1 < end && (int) sg[1].dma_address == -1) {
  498. size += sg[1].length;
  499. sg++;
  500. }
  501. npages = calc_npages((paddr & ~PAGE_MASK) + size);
  502. paddr &= PAGE_MASK;
  503. for (i = 0; i < npages; ++i, paddr += PAGE_SIZE)
  504. *ptes++ = mk_iommu_pte(paddr);
  505. #if DEBUG_ALLOC > 0
  506. DBGA(" (%ld) [%p,%x] np %ld\n",
  507. last_sg - leader, SG_ENT_VIRT_ADDRESS(last_sg),
  508. last_sg->length, npages);
  509. while (++last_sg <= sg) {
  510. DBGA(" (%ld) [%p,%x] cont\n",
  511. last_sg - leader, SG_ENT_VIRT_ADDRESS(last_sg),
  512. last_sg->length);
  513. }
  514. #endif
  515. } while (++sg < end && (int) sg->dma_address < 0);
  516. return 1;
  517. }
  518. int
  519. pci_map_sg(struct pci_dev *pdev, struct scatterlist *sg, int nents,
  520. int direction)
  521. {
  522. struct scatterlist *start, *end, *out;
  523. struct pci_controller *hose;
  524. struct pci_iommu_arena *arena;
  525. dma_addr_t max_dma;
  526. int dac_allowed;
  527. struct device *dev;
  528. if (direction == PCI_DMA_NONE)
  529. BUG();
  530. dac_allowed = pdev ? pci_dac_dma_supported(pdev, pdev->dma_mask) : 0;
  531. dev = pdev ? &pdev->dev : NULL;
  532. /* Fast path single entry scatterlists. */
  533. if (nents == 1) {
  534. sg->dma_length = sg->length;
  535. sg->dma_address
  536. = pci_map_single_1(pdev, SG_ENT_VIRT_ADDRESS(sg),
  537. sg->length, dac_allowed);
  538. return sg->dma_address != 0;
  539. }
  540. start = sg;
  541. end = sg + nents;
  542. /* First, prepare information about the entries. */
  543. sg_classify(dev, sg, end, alpha_mv.mv_pci_tbi != 0);
  544. /* Second, figure out where we're going to map things. */
  545. if (alpha_mv.mv_pci_tbi) {
  546. hose = pdev ? pdev->sysdata : pci_isa_hose;
  547. max_dma = pdev ? pdev->dma_mask : ISA_DMA_MASK;
  548. arena = hose->sg_pci;
  549. if (!arena || arena->dma_base + arena->size - 1 > max_dma)
  550. arena = hose->sg_isa;
  551. } else {
  552. max_dma = -1;
  553. arena = NULL;
  554. hose = NULL;
  555. }
  556. /* Third, iterate over the scatterlist leaders and allocate
  557. dma space as needed. */
  558. for (out = sg; sg < end; ++sg) {
  559. if ((int) sg->dma_address < 0)
  560. continue;
  561. if (sg_fill(dev, sg, end, out, arena, max_dma, dac_allowed) < 0)
  562. goto error;
  563. out++;
  564. }
  565. /* Mark the end of the list for pci_unmap_sg. */
  566. if (out < end)
  567. out->dma_length = 0;
  568. if (out - start == 0)
  569. printk(KERN_WARNING "pci_map_sg failed: no entries?\n");
  570. DBGA("pci_map_sg: %ld entries\n", out - start);
  571. return out - start;
  572. error:
  573. printk(KERN_WARNING "pci_map_sg failed: "
  574. "could not allocate dma page tables\n");
  575. /* Some allocation failed while mapping the scatterlist
  576. entries. Unmap them now. */
  577. if (out > start)
  578. pci_unmap_sg(pdev, start, out - start, direction);
  579. return 0;
  580. }
  581. EXPORT_SYMBOL(pci_map_sg);
  582. /* Unmap a set of streaming mode DMA translations. Again, cpu read
  583. rules concerning calls here are the same as for pci_unmap_single()
  584. above. */
  585. void
  586. pci_unmap_sg(struct pci_dev *pdev, struct scatterlist *sg, int nents,
  587. int direction)
  588. {
  589. unsigned long flags;
  590. struct pci_controller *hose;
  591. struct pci_iommu_arena *arena;
  592. struct scatterlist *end;
  593. dma_addr_t max_dma;
  594. dma_addr_t fbeg, fend;
  595. if (direction == PCI_DMA_NONE)
  596. BUG();
  597. if (! alpha_mv.mv_pci_tbi)
  598. return;
  599. hose = pdev ? pdev->sysdata : pci_isa_hose;
  600. max_dma = pdev ? pdev->dma_mask : ISA_DMA_MASK;
  601. arena = hose->sg_pci;
  602. if (!arena || arena->dma_base + arena->size - 1 > max_dma)
  603. arena = hose->sg_isa;
  604. fbeg = -1, fend = 0;
  605. spin_lock_irqsave(&arena->lock, flags);
  606. for (end = sg + nents; sg < end; ++sg) {
  607. dma64_addr_t addr;
  608. size_t size;
  609. long npages, ofs;
  610. dma_addr_t tend;
  611. addr = sg->dma_address;
  612. size = sg->dma_length;
  613. if (!size)
  614. break;
  615. if (addr > 0xffffffff) {
  616. /* It's a DAC address -- nothing to do. */
  617. DBGA(" (%ld) DAC [%lx,%lx]\n",
  618. sg - end + nents, addr, size);
  619. continue;
  620. }
  621. if (addr >= __direct_map_base
  622. && addr < __direct_map_base + __direct_map_size) {
  623. /* Nothing to do. */
  624. DBGA(" (%ld) direct [%lx,%lx]\n",
  625. sg - end + nents, addr, size);
  626. continue;
  627. }
  628. DBGA(" (%ld) sg [%lx,%lx]\n",
  629. sg - end + nents, addr, size);
  630. npages = calc_npages((addr & ~PAGE_MASK) + size);
  631. ofs = (addr - arena->dma_base) >> PAGE_SHIFT;
  632. iommu_arena_free(arena, ofs, npages);
  633. tend = addr + size - 1;
  634. if (fbeg > addr) fbeg = addr;
  635. if (fend < tend) fend = tend;
  636. }
  637. /* If we're freeing ptes above the `next_entry' pointer (they
  638. may have snuck back into the TLB since the last wrap flush),
  639. we need to flush the TLB before reallocating the latter. */
  640. if ((fend - arena->dma_base) >> PAGE_SHIFT >= arena->next_entry)
  641. alpha_mv.mv_pci_tbi(hose, fbeg, fend);
  642. spin_unlock_irqrestore(&arena->lock, flags);
  643. DBGA("pci_unmap_sg: %ld entries\n", nents - (end - sg));
  644. }
  645. EXPORT_SYMBOL(pci_unmap_sg);
  646. /* Return whether the given PCI device DMA address mask can be
  647. supported properly. */
  648. int
  649. pci_dma_supported(struct pci_dev *pdev, u64 mask)
  650. {
  651. struct pci_controller *hose;
  652. struct pci_iommu_arena *arena;
  653. /* If there exists a direct map, and the mask fits either
  654. the entire direct mapped space or the total system memory as
  655. shifted by the map base */
  656. if (__direct_map_size != 0
  657. && (__direct_map_base + __direct_map_size - 1 <= mask ||
  658. __direct_map_base + (max_low_pfn << PAGE_SHIFT) - 1 <= mask))
  659. return 1;
  660. /* Check that we have a scatter-gather arena that fits. */
  661. hose = pdev ? pdev->sysdata : pci_isa_hose;
  662. arena = hose->sg_isa;
  663. if (arena && arena->dma_base + arena->size - 1 <= mask)
  664. return 1;
  665. arena = hose->sg_pci;
  666. if (arena && arena->dma_base + arena->size - 1 <= mask)
  667. return 1;
  668. /* As last resort try ZONE_DMA. */
  669. if (!__direct_map_base && MAX_DMA_ADDRESS - IDENT_ADDR - 1 <= mask)
  670. return 1;
  671. return 0;
  672. }
  673. EXPORT_SYMBOL(pci_dma_supported);
  674. /*
  675. * AGP GART extensions to the IOMMU
  676. */
  677. int
  678. iommu_reserve(struct pci_iommu_arena *arena, long pg_count, long align_mask)
  679. {
  680. unsigned long flags;
  681. unsigned long *ptes;
  682. long i, p;
  683. if (!arena) return -EINVAL;
  684. spin_lock_irqsave(&arena->lock, flags);
  685. /* Search for N empty ptes. */
  686. ptes = arena->ptes;
  687. p = iommu_arena_find_pages(NULL, arena, pg_count, align_mask);
  688. if (p < 0) {
  689. spin_unlock_irqrestore(&arena->lock, flags);
  690. return -1;
  691. }
  692. /* Success. Mark them all reserved (ie not zero and invalid)
  693. for the iommu tlb that could load them from under us.
  694. They will be filled in with valid bits by _bind() */
  695. for (i = 0; i < pg_count; ++i)
  696. ptes[p+i] = IOMMU_RESERVED_PTE;
  697. arena->next_entry = p + pg_count;
  698. spin_unlock_irqrestore(&arena->lock, flags);
  699. return p;
  700. }
  701. int
  702. iommu_release(struct pci_iommu_arena *arena, long pg_start, long pg_count)
  703. {
  704. unsigned long *ptes;
  705. long i;
  706. if (!arena) return -EINVAL;
  707. ptes = arena->ptes;
  708. /* Make sure they're all reserved first... */
  709. for(i = pg_start; i < pg_start + pg_count; i++)
  710. if (ptes[i] != IOMMU_RESERVED_PTE)
  711. return -EBUSY;
  712. iommu_arena_free(arena, pg_start, pg_count);
  713. return 0;
  714. }
  715. int
  716. iommu_bind(struct pci_iommu_arena *arena, long pg_start, long pg_count,
  717. unsigned long *physaddrs)
  718. {
  719. unsigned long flags;
  720. unsigned long *ptes;
  721. long i, j;
  722. if (!arena) return -EINVAL;
  723. spin_lock_irqsave(&arena->lock, flags);
  724. ptes = arena->ptes;
  725. for(j = pg_start; j < pg_start + pg_count; j++) {
  726. if (ptes[j] != IOMMU_RESERVED_PTE) {
  727. spin_unlock_irqrestore(&arena->lock, flags);
  728. return -EBUSY;
  729. }
  730. }
  731. for(i = 0, j = pg_start; i < pg_count; i++, j++)
  732. ptes[j] = mk_iommu_pte(physaddrs[i]);
  733. spin_unlock_irqrestore(&arena->lock, flags);
  734. return 0;
  735. }
  736. int
  737. iommu_unbind(struct pci_iommu_arena *arena, long pg_start, long pg_count)
  738. {
  739. unsigned long *p;
  740. long i;
  741. if (!arena) return -EINVAL;
  742. p = arena->ptes + pg_start;
  743. for(i = 0; i < pg_count; i++)
  744. p[i] = IOMMU_RESERVED_PTE;
  745. return 0;
  746. }
  747. /* True if the machine supports DAC addressing, and DEV can
  748. make use of it given MASK. */
  749. static int
  750. pci_dac_dma_supported(struct pci_dev *dev, u64 mask)
  751. {
  752. dma64_addr_t dac_offset = alpha_mv.pci_dac_offset;
  753. int ok = 1;
  754. /* If this is not set, the machine doesn't support DAC at all. */
  755. if (dac_offset == 0)
  756. ok = 0;
  757. /* The device has to be able to address our DAC bit. */
  758. if ((dac_offset & dev->dma_mask) != dac_offset)
  759. ok = 0;
  760. /* If both conditions above are met, we are fine. */
  761. DBGA("pci_dac_dma_supported %s from %p\n",
  762. ok ? "yes" : "no", __builtin_return_address(0));
  763. return ok;
  764. }
  765. /* Helper for generic DMA-mapping functions. */
  766. struct pci_dev *
  767. alpha_gendev_to_pci(struct device *dev)
  768. {
  769. if (dev && dev->bus == &pci_bus_type)
  770. return to_pci_dev(dev);
  771. /* Assume that non-PCI devices asking for DMA are either ISA or EISA,
  772. BUG() otherwise. */
  773. BUG_ON(!isa_bridge);
  774. /* Assume non-busmaster ISA DMA when dma_mask is not set (the ISA
  775. bridge is bus master then). */
  776. if (!dev || !dev->dma_mask || !*dev->dma_mask)
  777. return isa_bridge;
  778. /* For EISA bus masters, return isa_bridge (it might have smaller
  779. dma_mask due to wiring limitations). */
  780. if (*dev->dma_mask >= isa_bridge->dma_mask)
  781. return isa_bridge;
  782. /* This assumes ISA bus master with dma_mask 0xffffff. */
  783. return NULL;
  784. }
  785. EXPORT_SYMBOL(alpha_gendev_to_pci);
  786. int
  787. dma_set_mask(struct device *dev, u64 mask)
  788. {
  789. if (!dev->dma_mask ||
  790. !pci_dma_supported(alpha_gendev_to_pci(dev), mask))
  791. return -EIO;
  792. *dev->dma_mask = mask;
  793. return 0;
  794. }
  795. EXPORT_SYMBOL(dma_set_mask);