pci_iommu.c 25 KB

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