pci_iommu.c 25 KB

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