pci_iommu.c 25 KB

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