pci_iommu.c 25 KB

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