pci_iommu.c 25 KB

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