pci_iommu.c 26 KB

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