pci_iommu.c 26 KB

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