pci_iommu.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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. /* 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. /* True if the machine supports DAC addressing, and DEV can
  180. make use of it given MASK. */
  181. static int pci_dac_dma_supported(struct pci_dev *hwdev, u64 mask);
  182. /* Map a single buffer of the indicated size for PCI DMA in streaming
  183. mode. The 32-bit PCI bus mastering address to use is returned.
  184. Once the device is given the dma address, the device owns this memory
  185. until either pci_unmap_single or pci_dma_sync_single is performed. */
  186. static dma_addr_t
  187. pci_map_single_1(struct pci_dev *pdev, void *cpu_addr, size_t size,
  188. int dac_allowed)
  189. {
  190. struct pci_controller *hose = pdev ? pdev->sysdata : pci_isa_hose;
  191. dma_addr_t max_dma = pdev ? pdev->dma_mask : ISA_DMA_MASK;
  192. struct pci_iommu_arena *arena;
  193. long npages, dma_ofs, i;
  194. unsigned long paddr;
  195. dma_addr_t ret;
  196. unsigned int align = 0;
  197. struct device *dev = pdev ? &pdev->dev : NULL;
  198. paddr = __pa(cpu_addr);
  199. #if !DEBUG_NODIRECT
  200. /* First check to see if we can use the direct map window. */
  201. if (paddr + size + __direct_map_base - 1 <= max_dma
  202. && paddr + size <= __direct_map_size) {
  203. ret = paddr + __direct_map_base;
  204. DBGA2("pci_map_single: [%p,%zx] -> direct %llx from %p\n",
  205. cpu_addr, size, ret, __builtin_return_address(0));
  206. return ret;
  207. }
  208. #endif
  209. /* Next, use DAC if selected earlier. */
  210. if (dac_allowed) {
  211. ret = paddr + alpha_mv.pci_dac_offset;
  212. DBGA2("pci_map_single: [%p,%zx] -> DAC %llx from %p\n",
  213. cpu_addr, size, ret, __builtin_return_address(0));
  214. return ret;
  215. }
  216. /* If the machine doesn't define a pci_tbi routine, we have to
  217. assume it doesn't support sg mapping, and, since we tried to
  218. use direct_map above, it now must be considered an error. */
  219. if (! alpha_mv.mv_pci_tbi) {
  220. static int been_here = 0; /* Only print the message once. */
  221. if (!been_here) {
  222. printk(KERN_WARNING "pci_map_single: no HW sg\n");
  223. been_here = 1;
  224. }
  225. return 0;
  226. }
  227. arena = hose->sg_pci;
  228. if (!arena || arena->dma_base + arena->size - 1 > max_dma)
  229. arena = hose->sg_isa;
  230. npages = iommu_num_pages(paddr, size, PAGE_SIZE);
  231. /* Force allocation to 64KB boundary for ISA bridges. */
  232. if (pdev && pdev == isa_bridge)
  233. align = 8;
  234. dma_ofs = iommu_arena_alloc(dev, arena, npages, align);
  235. if (dma_ofs < 0) {
  236. printk(KERN_WARNING "pci_map_single failed: "
  237. "could not allocate dma page tables\n");
  238. return 0;
  239. }
  240. paddr &= PAGE_MASK;
  241. for (i = 0; i < npages; ++i, paddr += PAGE_SIZE)
  242. arena->ptes[i + dma_ofs] = mk_iommu_pte(paddr);
  243. ret = arena->dma_base + dma_ofs * PAGE_SIZE;
  244. ret += (unsigned long)cpu_addr & ~PAGE_MASK;
  245. DBGA2("pci_map_single: [%p,%zx] np %ld -> sg %llx from %p\n",
  246. cpu_addr, size, npages, ret, __builtin_return_address(0));
  247. return ret;
  248. }
  249. dma_addr_t
  250. pci_map_single(struct pci_dev *pdev, void *cpu_addr, size_t size, int dir)
  251. {
  252. int dac_allowed;
  253. if (dir == PCI_DMA_NONE)
  254. BUG();
  255. dac_allowed = pdev ? pci_dac_dma_supported(pdev, pdev->dma_mask) : 0;
  256. return pci_map_single_1(pdev, cpu_addr, size, dac_allowed);
  257. }
  258. EXPORT_SYMBOL(pci_map_single);
  259. dma_addr_t
  260. pci_map_page(struct pci_dev *pdev, struct page *page, unsigned long offset,
  261. size_t size, int dir)
  262. {
  263. int dac_allowed;
  264. if (dir == PCI_DMA_NONE)
  265. BUG();
  266. dac_allowed = pdev ? pci_dac_dma_supported(pdev, pdev->dma_mask) : 0;
  267. return pci_map_single_1(pdev, (char *)page_address(page) + offset,
  268. size, dac_allowed);
  269. }
  270. EXPORT_SYMBOL(pci_map_page);
  271. /* Unmap a single streaming mode DMA translation. The DMA_ADDR and
  272. SIZE must match what was provided for in a previous pci_map_single
  273. call. All other usages are undefined. After this call, reads by
  274. the cpu to the buffer are guaranteed to see whatever the device
  275. wrote there. */
  276. void
  277. pci_unmap_single(struct pci_dev *pdev, dma_addr_t dma_addr, size_t size,
  278. int direction)
  279. {
  280. unsigned long flags;
  281. struct pci_controller *hose = pdev ? pdev->sysdata : pci_isa_hose;
  282. struct pci_iommu_arena *arena;
  283. long dma_ofs, npages;
  284. if (direction == PCI_DMA_NONE)
  285. BUG();
  286. if (dma_addr >= __direct_map_base
  287. && dma_addr < __direct_map_base + __direct_map_size) {
  288. /* Nothing to do. */
  289. DBGA2("pci_unmap_single: direct [%llx,%zx] from %p\n",
  290. dma_addr, size, __builtin_return_address(0));
  291. return;
  292. }
  293. if (dma_addr > 0xffffffff) {
  294. DBGA2("pci64_unmap_single: DAC [%llx,%zx] from %p\n",
  295. dma_addr, size, __builtin_return_address(0));
  296. return;
  297. }
  298. arena = hose->sg_pci;
  299. if (!arena || dma_addr < arena->dma_base)
  300. arena = hose->sg_isa;
  301. dma_ofs = (dma_addr - arena->dma_base) >> PAGE_SHIFT;
  302. if (dma_ofs * PAGE_SIZE >= arena->size) {
  303. printk(KERN_ERR "Bogus pci_unmap_single: dma_addr %llx "
  304. " base %llx size %x\n",
  305. dma_addr, arena->dma_base, arena->size);
  306. return;
  307. BUG();
  308. }
  309. npages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
  310. spin_lock_irqsave(&arena->lock, flags);
  311. iommu_arena_free(arena, dma_ofs, npages);
  312. /* If we're freeing ptes above the `next_entry' pointer (they
  313. may have snuck back into the TLB since the last wrap flush),
  314. we need to flush the TLB before reallocating the latter. */
  315. if (dma_ofs >= arena->next_entry)
  316. alpha_mv.mv_pci_tbi(hose, dma_addr, dma_addr + size - 1);
  317. spin_unlock_irqrestore(&arena->lock, flags);
  318. DBGA2("pci_unmap_single: sg [%llx,%zx] np %ld from %p\n",
  319. dma_addr, size, npages, __builtin_return_address(0));
  320. }
  321. EXPORT_SYMBOL(pci_unmap_single);
  322. void
  323. pci_unmap_page(struct pci_dev *pdev, dma_addr_t dma_addr,
  324. size_t size, int direction)
  325. {
  326. pci_unmap_single(pdev, dma_addr, size, direction);
  327. }
  328. EXPORT_SYMBOL(pci_unmap_page);
  329. /* Allocate and map kernel buffer using consistent mode DMA for PCI
  330. device. Returns non-NULL cpu-view pointer to the buffer if
  331. successful and sets *DMA_ADDRP to the pci side dma address as well,
  332. else DMA_ADDRP is undefined. */
  333. void *
  334. __pci_alloc_consistent(struct pci_dev *pdev, size_t size,
  335. dma_addr_t *dma_addrp, gfp_t gfp)
  336. {
  337. void *cpu_addr;
  338. long order = get_order(size);
  339. gfp &= ~GFP_DMA;
  340. try_again:
  341. cpu_addr = (void *)__get_free_pages(gfp, order);
  342. if (! cpu_addr) {
  343. printk(KERN_INFO "pci_alloc_consistent: "
  344. "get_free_pages failed from %p\n",
  345. __builtin_return_address(0));
  346. /* ??? Really atomic allocation? Otherwise we could play
  347. with vmalloc and sg if we can't find contiguous memory. */
  348. return NULL;
  349. }
  350. memset(cpu_addr, 0, size);
  351. *dma_addrp = pci_map_single_1(pdev, cpu_addr, size, 0);
  352. if (*dma_addrp == 0) {
  353. free_pages((unsigned long)cpu_addr, order);
  354. if (alpha_mv.mv_pci_tbi || (gfp & GFP_DMA))
  355. return NULL;
  356. /* The address doesn't fit required mask and we
  357. do not have iommu. Try again with GFP_DMA. */
  358. gfp |= GFP_DMA;
  359. goto try_again;
  360. }
  361. DBGA2("pci_alloc_consistent: %zx -> [%p,%llx] from %p\n",
  362. size, cpu_addr, *dma_addrp, __builtin_return_address(0));
  363. return cpu_addr;
  364. }
  365. EXPORT_SYMBOL(__pci_alloc_consistent);
  366. /* Free and unmap a consistent DMA buffer. CPU_ADDR and DMA_ADDR must
  367. be values that were returned from pci_alloc_consistent. SIZE must
  368. be the same as what as passed into pci_alloc_consistent.
  369. References to the memory and mappings associated with CPU_ADDR or
  370. DMA_ADDR past this call are illegal. */
  371. void
  372. pci_free_consistent(struct pci_dev *pdev, size_t size, void *cpu_addr,
  373. dma_addr_t dma_addr)
  374. {
  375. pci_unmap_single(pdev, dma_addr, size, PCI_DMA_BIDIRECTIONAL);
  376. free_pages((unsigned long)cpu_addr, get_order(size));
  377. DBGA2("pci_free_consistent: [%llx,%zx] from %p\n",
  378. dma_addr, size, __builtin_return_address(0));
  379. }
  380. EXPORT_SYMBOL(pci_free_consistent);
  381. /* Classify the elements of the scatterlist. Write dma_address
  382. of each element with:
  383. 0 : Followers all physically adjacent.
  384. 1 : Followers all virtually adjacent.
  385. -1 : Not leader, physically adjacent to previous.
  386. -2 : Not leader, virtually adjacent to previous.
  387. Write dma_length of each leader with the combined lengths of
  388. the mergable followers. */
  389. #define SG_ENT_VIRT_ADDRESS(SG) (sg_virt((SG)))
  390. #define SG_ENT_PHYS_ADDRESS(SG) __pa(SG_ENT_VIRT_ADDRESS(SG))
  391. static void
  392. sg_classify(struct device *dev, struct scatterlist *sg, struct scatterlist *end,
  393. int virt_ok)
  394. {
  395. unsigned long next_paddr;
  396. struct scatterlist *leader;
  397. long leader_flag, leader_length;
  398. unsigned int max_seg_size;
  399. leader = sg;
  400. leader_flag = 0;
  401. leader_length = leader->length;
  402. next_paddr = SG_ENT_PHYS_ADDRESS(leader) + leader_length;
  403. /* we will not marge sg without device. */
  404. max_seg_size = dev ? dma_get_max_seg_size(dev) : 0;
  405. for (++sg; sg < end; ++sg) {
  406. unsigned long addr, len;
  407. addr = SG_ENT_PHYS_ADDRESS(sg);
  408. len = sg->length;
  409. if (leader_length + len > max_seg_size)
  410. goto new_segment;
  411. if (next_paddr == addr) {
  412. sg->dma_address = -1;
  413. leader_length += len;
  414. } else if (((next_paddr | addr) & ~PAGE_MASK) == 0 && virt_ok) {
  415. sg->dma_address = -2;
  416. leader_flag = 1;
  417. leader_length += len;
  418. } else {
  419. new_segment:
  420. leader->dma_address = leader_flag;
  421. leader->dma_length = leader_length;
  422. leader = sg;
  423. leader_flag = 0;
  424. leader_length = len;
  425. }
  426. next_paddr = addr + len;
  427. }
  428. leader->dma_address = leader_flag;
  429. leader->dma_length = leader_length;
  430. }
  431. /* Given a scatterlist leader, choose an allocation method and fill
  432. in the blanks. */
  433. static int
  434. sg_fill(struct device *dev, struct scatterlist *leader, struct scatterlist *end,
  435. struct scatterlist *out, struct pci_iommu_arena *arena,
  436. dma_addr_t max_dma, int dac_allowed)
  437. {
  438. unsigned long paddr = SG_ENT_PHYS_ADDRESS(leader);
  439. long size = leader->dma_length;
  440. struct scatterlist *sg;
  441. unsigned long *ptes;
  442. long npages, dma_ofs, i;
  443. #if !DEBUG_NODIRECT
  444. /* If everything is physically contiguous, and the addresses
  445. fall into the direct-map window, use it. */
  446. if (leader->dma_address == 0
  447. && paddr + size + __direct_map_base - 1 <= max_dma
  448. && paddr + size <= __direct_map_size) {
  449. out->dma_address = paddr + __direct_map_base;
  450. out->dma_length = size;
  451. DBGA(" sg_fill: [%p,%lx] -> direct %llx\n",
  452. __va(paddr), size, out->dma_address);
  453. return 0;
  454. }
  455. #endif
  456. /* If physically contiguous and DAC is available, use it. */
  457. if (leader->dma_address == 0 && dac_allowed) {
  458. out->dma_address = paddr + alpha_mv.pci_dac_offset;
  459. out->dma_length = size;
  460. DBGA(" sg_fill: [%p,%lx] -> DAC %llx\n",
  461. __va(paddr), size, out->dma_address);
  462. return 0;
  463. }
  464. /* Otherwise, we'll use the iommu to make the pages virtually
  465. contiguous. */
  466. paddr &= ~PAGE_MASK;
  467. npages = iommu_num_pages(paddr, size, PAGE_SIZE);
  468. dma_ofs = iommu_arena_alloc(dev, arena, npages, 0);
  469. if (dma_ofs < 0) {
  470. /* If we attempted a direct map above but failed, die. */
  471. if (leader->dma_address == 0)
  472. return -1;
  473. /* Otherwise, break up the remaining virtually contiguous
  474. hunks into individual direct maps and retry. */
  475. sg_classify(dev, leader, end, 0);
  476. return sg_fill(dev, leader, end, out, arena, max_dma, dac_allowed);
  477. }
  478. out->dma_address = arena->dma_base + dma_ofs*PAGE_SIZE + paddr;
  479. out->dma_length = size;
  480. DBGA(" sg_fill: [%p,%lx] -> sg %llx np %ld\n",
  481. __va(paddr), size, out->dma_address, npages);
  482. /* All virtually contiguous. We need to find the length of each
  483. physically contiguous subsegment to fill in the ptes. */
  484. ptes = &arena->ptes[dma_ofs];
  485. sg = leader;
  486. do {
  487. #if DEBUG_ALLOC > 0
  488. struct scatterlist *last_sg = sg;
  489. #endif
  490. size = sg->length;
  491. paddr = SG_ENT_PHYS_ADDRESS(sg);
  492. while (sg+1 < end && (int) sg[1].dma_address == -1) {
  493. size += sg[1].length;
  494. sg++;
  495. }
  496. npages = iommu_num_pages(paddr, size, PAGE_SIZE);
  497. paddr &= PAGE_MASK;
  498. for (i = 0; i < npages; ++i, paddr += PAGE_SIZE)
  499. *ptes++ = mk_iommu_pte(paddr);
  500. #if DEBUG_ALLOC > 0
  501. DBGA(" (%ld) [%p,%x] np %ld\n",
  502. last_sg - leader, SG_ENT_VIRT_ADDRESS(last_sg),
  503. last_sg->length, npages);
  504. while (++last_sg <= sg) {
  505. DBGA(" (%ld) [%p,%x] cont\n",
  506. last_sg - leader, SG_ENT_VIRT_ADDRESS(last_sg),
  507. last_sg->length);
  508. }
  509. #endif
  510. } while (++sg < end && (int) sg->dma_address < 0);
  511. return 1;
  512. }
  513. int
  514. pci_map_sg(struct pci_dev *pdev, struct scatterlist *sg, int nents,
  515. int direction)
  516. {
  517. struct scatterlist *start, *end, *out;
  518. struct pci_controller *hose;
  519. struct pci_iommu_arena *arena;
  520. dma_addr_t max_dma;
  521. int dac_allowed;
  522. struct device *dev;
  523. if (direction == PCI_DMA_NONE)
  524. BUG();
  525. dac_allowed = pdev ? pci_dac_dma_supported(pdev, pdev->dma_mask) : 0;
  526. dev = pdev ? &pdev->dev : NULL;
  527. /* Fast path single entry scatterlists. */
  528. if (nents == 1) {
  529. sg->dma_length = sg->length;
  530. sg->dma_address
  531. = pci_map_single_1(pdev, SG_ENT_VIRT_ADDRESS(sg),
  532. sg->length, dac_allowed);
  533. return sg->dma_address != 0;
  534. }
  535. start = sg;
  536. end = sg + nents;
  537. /* First, prepare information about the entries. */
  538. sg_classify(dev, sg, end, alpha_mv.mv_pci_tbi != 0);
  539. /* Second, figure out where we're going to map things. */
  540. if (alpha_mv.mv_pci_tbi) {
  541. hose = pdev ? pdev->sysdata : pci_isa_hose;
  542. max_dma = pdev ? pdev->dma_mask : ISA_DMA_MASK;
  543. arena = hose->sg_pci;
  544. if (!arena || arena->dma_base + arena->size - 1 > max_dma)
  545. arena = hose->sg_isa;
  546. } else {
  547. max_dma = -1;
  548. arena = NULL;
  549. hose = NULL;
  550. }
  551. /* Third, iterate over the scatterlist leaders and allocate
  552. dma space as needed. */
  553. for (out = sg; sg < end; ++sg) {
  554. if ((int) sg->dma_address < 0)
  555. continue;
  556. if (sg_fill(dev, sg, end, out, arena, max_dma, dac_allowed) < 0)
  557. goto error;
  558. out++;
  559. }
  560. /* Mark the end of the list for pci_unmap_sg. */
  561. if (out < end)
  562. out->dma_length = 0;
  563. if (out - start == 0)
  564. printk(KERN_WARNING "pci_map_sg failed: no entries?\n");
  565. DBGA("pci_map_sg: %ld entries\n", out - start);
  566. return out - start;
  567. error:
  568. printk(KERN_WARNING "pci_map_sg failed: "
  569. "could not allocate dma page tables\n");
  570. /* Some allocation failed while mapping the scatterlist
  571. entries. Unmap them now. */
  572. if (out > start)
  573. pci_unmap_sg(pdev, start, out - start, direction);
  574. return 0;
  575. }
  576. EXPORT_SYMBOL(pci_map_sg);
  577. /* Unmap a set of streaming mode DMA translations. Again, cpu read
  578. rules concerning calls here are the same as for pci_unmap_single()
  579. above. */
  580. void
  581. pci_unmap_sg(struct pci_dev *pdev, struct scatterlist *sg, int nents,
  582. int direction)
  583. {
  584. unsigned long flags;
  585. struct pci_controller *hose;
  586. struct pci_iommu_arena *arena;
  587. struct scatterlist *end;
  588. dma_addr_t max_dma;
  589. dma_addr_t fbeg, fend;
  590. if (direction == PCI_DMA_NONE)
  591. BUG();
  592. if (! alpha_mv.mv_pci_tbi)
  593. return;
  594. hose = pdev ? pdev->sysdata : pci_isa_hose;
  595. max_dma = pdev ? pdev->dma_mask : ISA_DMA_MASK;
  596. arena = hose->sg_pci;
  597. if (!arena || arena->dma_base + arena->size - 1 > max_dma)
  598. arena = hose->sg_isa;
  599. fbeg = -1, fend = 0;
  600. spin_lock_irqsave(&arena->lock, flags);
  601. for (end = sg + nents; sg < end; ++sg) {
  602. dma64_addr_t addr;
  603. size_t size;
  604. long npages, ofs;
  605. dma_addr_t tend;
  606. addr = sg->dma_address;
  607. size = sg->dma_length;
  608. if (!size)
  609. break;
  610. if (addr > 0xffffffff) {
  611. /* It's a DAC address -- nothing to do. */
  612. DBGA(" (%ld) DAC [%llx,%zx]\n",
  613. sg - end + nents, addr, size);
  614. continue;
  615. }
  616. if (addr >= __direct_map_base
  617. && addr < __direct_map_base + __direct_map_size) {
  618. /* Nothing to do. */
  619. DBGA(" (%ld) direct [%llx,%zx]\n",
  620. sg - end + nents, addr, size);
  621. continue;
  622. }
  623. DBGA(" (%ld) sg [%llx,%zx]\n",
  624. sg - end + nents, addr, size);
  625. npages = iommu_num_pages(addr, size, PAGE_SIZE);
  626. ofs = (addr - arena->dma_base) >> PAGE_SHIFT;
  627. iommu_arena_free(arena, ofs, npages);
  628. tend = addr + size - 1;
  629. if (fbeg > addr) fbeg = addr;
  630. if (fend < tend) fend = tend;
  631. }
  632. /* If we're freeing ptes above the `next_entry' pointer (they
  633. may have snuck back into the TLB since the last wrap flush),
  634. we need to flush the TLB before reallocating the latter. */
  635. if ((fend - arena->dma_base) >> PAGE_SHIFT >= arena->next_entry)
  636. alpha_mv.mv_pci_tbi(hose, fbeg, fend);
  637. spin_unlock_irqrestore(&arena->lock, flags);
  638. DBGA("pci_unmap_sg: %ld entries\n", nents - (end - sg));
  639. }
  640. EXPORT_SYMBOL(pci_unmap_sg);
  641. /* Return whether the given PCI device DMA address mask can be
  642. supported properly. */
  643. int
  644. pci_dma_supported(struct pci_dev *pdev, u64 mask)
  645. {
  646. struct pci_controller *hose;
  647. struct pci_iommu_arena *arena;
  648. /* If there exists a direct map, and the mask fits either
  649. the entire direct mapped space or the total system memory as
  650. shifted by the map base */
  651. if (__direct_map_size != 0
  652. && (__direct_map_base + __direct_map_size - 1 <= mask ||
  653. __direct_map_base + (max_low_pfn << PAGE_SHIFT) - 1 <= mask))
  654. return 1;
  655. /* Check that we have a scatter-gather arena that fits. */
  656. hose = pdev ? pdev->sysdata : pci_isa_hose;
  657. arena = hose->sg_isa;
  658. if (arena && arena->dma_base + arena->size - 1 <= mask)
  659. return 1;
  660. arena = hose->sg_pci;
  661. if (arena && arena->dma_base + arena->size - 1 <= mask)
  662. return 1;
  663. /* As last resort try ZONE_DMA. */
  664. if (!__direct_map_base && MAX_DMA_ADDRESS - IDENT_ADDR - 1 <= mask)
  665. return 1;
  666. return 0;
  667. }
  668. EXPORT_SYMBOL(pci_dma_supported);
  669. /*
  670. * AGP GART extensions to the IOMMU
  671. */
  672. int
  673. iommu_reserve(struct pci_iommu_arena *arena, long pg_count, long align_mask)
  674. {
  675. unsigned long flags;
  676. unsigned long *ptes;
  677. long i, p;
  678. if (!arena) return -EINVAL;
  679. spin_lock_irqsave(&arena->lock, flags);
  680. /* Search for N empty ptes. */
  681. ptes = arena->ptes;
  682. p = iommu_arena_find_pages(NULL, arena, pg_count, align_mask);
  683. if (p < 0) {
  684. spin_unlock_irqrestore(&arena->lock, flags);
  685. return -1;
  686. }
  687. /* Success. Mark them all reserved (ie not zero and invalid)
  688. for the iommu tlb that could load them from under us.
  689. They will be filled in with valid bits by _bind() */
  690. for (i = 0; i < pg_count; ++i)
  691. ptes[p+i] = IOMMU_RESERVED_PTE;
  692. arena->next_entry = p + pg_count;
  693. spin_unlock_irqrestore(&arena->lock, flags);
  694. return p;
  695. }
  696. int
  697. iommu_release(struct pci_iommu_arena *arena, long pg_start, long pg_count)
  698. {
  699. unsigned long *ptes;
  700. long i;
  701. if (!arena) return -EINVAL;
  702. ptes = arena->ptes;
  703. /* Make sure they're all reserved first... */
  704. for(i = pg_start; i < pg_start + pg_count; i++)
  705. if (ptes[i] != IOMMU_RESERVED_PTE)
  706. return -EBUSY;
  707. iommu_arena_free(arena, pg_start, pg_count);
  708. return 0;
  709. }
  710. int
  711. iommu_bind(struct pci_iommu_arena *arena, long pg_start, long pg_count,
  712. unsigned long *physaddrs)
  713. {
  714. unsigned long flags;
  715. unsigned long *ptes;
  716. long i, j;
  717. if (!arena) return -EINVAL;
  718. spin_lock_irqsave(&arena->lock, flags);
  719. ptes = arena->ptes;
  720. for(j = pg_start; j < pg_start + pg_count; j++) {
  721. if (ptes[j] != IOMMU_RESERVED_PTE) {
  722. spin_unlock_irqrestore(&arena->lock, flags);
  723. return -EBUSY;
  724. }
  725. }
  726. for(i = 0, j = pg_start; i < pg_count; i++, j++)
  727. ptes[j] = mk_iommu_pte(physaddrs[i]);
  728. spin_unlock_irqrestore(&arena->lock, flags);
  729. return 0;
  730. }
  731. int
  732. iommu_unbind(struct pci_iommu_arena *arena, long pg_start, long pg_count)
  733. {
  734. unsigned long *p;
  735. long i;
  736. if (!arena) return -EINVAL;
  737. p = arena->ptes + pg_start;
  738. for(i = 0; i < pg_count; i++)
  739. p[i] = IOMMU_RESERVED_PTE;
  740. return 0;
  741. }
  742. /* True if the machine supports DAC addressing, and DEV can
  743. make use of it given MASK. */
  744. static int
  745. pci_dac_dma_supported(struct pci_dev *dev, u64 mask)
  746. {
  747. dma64_addr_t dac_offset = alpha_mv.pci_dac_offset;
  748. int ok = 1;
  749. /* If this is not set, the machine doesn't support DAC at all. */
  750. if (dac_offset == 0)
  751. ok = 0;
  752. /* The device has to be able to address our DAC bit. */
  753. if ((dac_offset & dev->dma_mask) != dac_offset)
  754. ok = 0;
  755. /* If both conditions above are met, we are fine. */
  756. DBGA("pci_dac_dma_supported %s from %p\n",
  757. ok ? "yes" : "no", __builtin_return_address(0));
  758. return ok;
  759. }
  760. /* Helper for generic DMA-mapping functions. */
  761. struct pci_dev *
  762. alpha_gendev_to_pci(struct device *dev)
  763. {
  764. if (dev && dev->bus == &pci_bus_type)
  765. return to_pci_dev(dev);
  766. /* Assume that non-PCI devices asking for DMA are either ISA or EISA,
  767. BUG() otherwise. */
  768. BUG_ON(!isa_bridge);
  769. /* Assume non-busmaster ISA DMA when dma_mask is not set (the ISA
  770. bridge is bus master then). */
  771. if (!dev || !dev->dma_mask || !*dev->dma_mask)
  772. return isa_bridge;
  773. /* For EISA bus masters, return isa_bridge (it might have smaller
  774. dma_mask due to wiring limitations). */
  775. if (*dev->dma_mask >= isa_bridge->dma_mask)
  776. return isa_bridge;
  777. /* This assumes ISA bus master with dma_mask 0xffffff. */
  778. return NULL;
  779. }
  780. EXPORT_SYMBOL(alpha_gendev_to_pci);
  781. int
  782. dma_set_mask(struct device *dev, u64 mask)
  783. {
  784. if (!dev->dma_mask ||
  785. !pci_dma_supported(alpha_gendev_to_pci(dev), mask))
  786. return -EIO;
  787. *dev->dma_mask = mask;
  788. return 0;
  789. }
  790. EXPORT_SYMBOL(dma_set_mask);