pci_iommu.c 25 KB

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