PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/arch/ia64/hp/common/sba_iommu.c

https://bitbucket.org/evzijst/gittest
C | 2121 lines | 1289 code | 323 blank | 509 comment | 157 complexity | d56697549b0ff30c74b4d65244994ffc MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0
  1. /*
  2. ** IA64 System Bus Adapter (SBA) I/O MMU manager
  3. **
  4. ** (c) Copyright 2002-2004 Alex Williamson
  5. ** (c) Copyright 2002-2003 Grant Grundler
  6. ** (c) Copyright 2002-2004 Hewlett-Packard Company
  7. **
  8. ** Portions (c) 2000 Grant Grundler (from parisc I/O MMU code)
  9. ** Portions (c) 1999 Dave S. Miller (from sparc64 I/O MMU code)
  10. **
  11. ** This program is free software; you can redistribute it and/or modify
  12. ** it under the terms of the GNU General Public License as published by
  13. ** the Free Software Foundation; either version 2 of the License, or
  14. ** (at your option) any later version.
  15. **
  16. **
  17. ** This module initializes the IOC (I/O Controller) found on HP
  18. ** McKinley machines and their successors.
  19. **
  20. */
  21. #include <linux/config.h>
  22. #include <linux/types.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/slab.h>
  27. #include <linux/init.h>
  28. #include <linux/mm.h>
  29. #include <linux/string.h>
  30. #include <linux/pci.h>
  31. #include <linux/proc_fs.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/acpi.h>
  34. #include <linux/efi.h>
  35. #include <linux/nodemask.h>
  36. #include <linux/bitops.h> /* hweight64() */
  37. #include <asm/delay.h> /* ia64_get_itc() */
  38. #include <asm/io.h>
  39. #include <asm/page.h> /* PAGE_OFFSET */
  40. #include <asm/dma.h>
  41. #include <asm/system.h> /* wmb() */
  42. #include <asm/acpi-ext.h>
  43. #define PFX "IOC: "
  44. /*
  45. ** Enabling timing search of the pdir resource map. Output in /proc.
  46. ** Disabled by default to optimize performance.
  47. */
  48. #undef PDIR_SEARCH_TIMING
  49. /*
  50. ** This option allows cards capable of 64bit DMA to bypass the IOMMU. If
  51. ** not defined, all DMA will be 32bit and go through the TLB.
  52. ** There's potentially a conflict in the bio merge code with us
  53. ** advertising an iommu, but then bypassing it. Since I/O MMU bypassing
  54. ** appears to give more performance than bio-level virtual merging, we'll
  55. ** do the former for now. NOTE: BYPASS_SG also needs to be undef'd to
  56. ** completely restrict DMA to the IOMMU.
  57. */
  58. #define ALLOW_IOV_BYPASS
  59. /*
  60. ** This option specifically allows/disallows bypassing scatterlists with
  61. ** multiple entries. Coalescing these entries can allow better DMA streaming
  62. ** and in some cases shows better performance than entirely bypassing the
  63. ** IOMMU. Performance increase on the order of 1-2% sequential output/input
  64. ** using bonnie++ on a RAID0 MD device (sym2 & mpt).
  65. */
  66. #undef ALLOW_IOV_BYPASS_SG
  67. /*
  68. ** If a device prefetches beyond the end of a valid pdir entry, it will cause
  69. ** a hard failure, ie. MCA. Version 3.0 and later of the zx1 LBA should
  70. ** disconnect on 4k boundaries and prevent such issues. If the device is
  71. ** particularly agressive, this option will keep the entire pdir valid such
  72. ** that prefetching will hit a valid address. This could severely impact
  73. ** error containment, and is therefore off by default. The page that is
  74. ** used for spill-over is poisoned, so that should help debugging somewhat.
  75. */
  76. #undef FULL_VALID_PDIR
  77. #define ENABLE_MARK_CLEAN
  78. /*
  79. ** The number of debug flags is a clue - this code is fragile. NOTE: since
  80. ** tightening the use of res_lock the resource bitmap and actual pdir are no
  81. ** longer guaranteed to stay in sync. The sanity checking code isn't going to
  82. ** like that.
  83. */
  84. #undef DEBUG_SBA_INIT
  85. #undef DEBUG_SBA_RUN
  86. #undef DEBUG_SBA_RUN_SG
  87. #undef DEBUG_SBA_RESOURCE
  88. #undef ASSERT_PDIR_SANITY
  89. #undef DEBUG_LARGE_SG_ENTRIES
  90. #undef DEBUG_BYPASS
  91. #if defined(FULL_VALID_PDIR) && defined(ASSERT_PDIR_SANITY)
  92. #error FULL_VALID_PDIR and ASSERT_PDIR_SANITY are mutually exclusive
  93. #endif
  94. #define SBA_INLINE __inline__
  95. /* #define SBA_INLINE */
  96. #ifdef DEBUG_SBA_INIT
  97. #define DBG_INIT(x...) printk(x)
  98. #else
  99. #define DBG_INIT(x...)
  100. #endif
  101. #ifdef DEBUG_SBA_RUN
  102. #define DBG_RUN(x...) printk(x)
  103. #else
  104. #define DBG_RUN(x...)
  105. #endif
  106. #ifdef DEBUG_SBA_RUN_SG
  107. #define DBG_RUN_SG(x...) printk(x)
  108. #else
  109. #define DBG_RUN_SG(x...)
  110. #endif
  111. #ifdef DEBUG_SBA_RESOURCE
  112. #define DBG_RES(x...) printk(x)
  113. #else
  114. #define DBG_RES(x...)
  115. #endif
  116. #ifdef DEBUG_BYPASS
  117. #define DBG_BYPASS(x...) printk(x)
  118. #else
  119. #define DBG_BYPASS(x...)
  120. #endif
  121. #ifdef ASSERT_PDIR_SANITY
  122. #define ASSERT(expr) \
  123. if(!(expr)) { \
  124. printk( "\n" __FILE__ ":%d: Assertion " #expr " failed!\n",__LINE__); \
  125. panic(#expr); \
  126. }
  127. #else
  128. #define ASSERT(expr)
  129. #endif
  130. /*
  131. ** The number of pdir entries to "free" before issuing
  132. ** a read to PCOM register to flush out PCOM writes.
  133. ** Interacts with allocation granularity (ie 4 or 8 entries
  134. ** allocated and free'd/purged at a time might make this
  135. ** less interesting).
  136. */
  137. #define DELAYED_RESOURCE_CNT 64
  138. #define ZX1_IOC_ID ((PCI_DEVICE_ID_HP_ZX1_IOC << 16) | PCI_VENDOR_ID_HP)
  139. #define ZX2_IOC_ID ((PCI_DEVICE_ID_HP_ZX2_IOC << 16) | PCI_VENDOR_ID_HP)
  140. #define REO_IOC_ID ((PCI_DEVICE_ID_HP_REO_IOC << 16) | PCI_VENDOR_ID_HP)
  141. #define SX1000_IOC_ID ((PCI_DEVICE_ID_HP_SX1000_IOC << 16) | PCI_VENDOR_ID_HP)
  142. #define ZX1_IOC_OFFSET 0x1000 /* ACPI reports SBA, we want IOC */
  143. #define IOC_FUNC_ID 0x000
  144. #define IOC_FCLASS 0x008 /* function class, bist, header, rev... */
  145. #define IOC_IBASE 0x300 /* IO TLB */
  146. #define IOC_IMASK 0x308
  147. #define IOC_PCOM 0x310
  148. #define IOC_TCNFG 0x318
  149. #define IOC_PDIR_BASE 0x320
  150. #define IOC_ROPE0_CFG 0x500
  151. #define IOC_ROPE_AO 0x10 /* Allow "Relaxed Ordering" */
  152. /* AGP GART driver looks for this */
  153. #define ZX1_SBA_IOMMU_COOKIE 0x0000badbadc0ffeeUL
  154. /*
  155. ** The zx1 IOC supports 4/8/16/64KB page sizes (see TCNFG register)
  156. **
  157. ** Some IOCs (sx1000) can run at the above pages sizes, but are
  158. ** really only supported using the IOC at a 4k page size.
  159. **
  160. ** iovp_size could only be greater than PAGE_SIZE if we are
  161. ** confident the drivers really only touch the next physical
  162. ** page iff that driver instance owns it.
  163. */
  164. static unsigned long iovp_size;
  165. static unsigned long iovp_shift;
  166. static unsigned long iovp_mask;
  167. struct ioc {
  168. void __iomem *ioc_hpa; /* I/O MMU base address */
  169. char *res_map; /* resource map, bit == pdir entry */
  170. u64 *pdir_base; /* physical base address */
  171. unsigned long ibase; /* pdir IOV Space base */
  172. unsigned long imask; /* pdir IOV Space mask */
  173. unsigned long *res_hint; /* next avail IOVP - circular search */
  174. unsigned long dma_mask;
  175. spinlock_t res_lock; /* protects the resource bitmap, but must be held when */
  176. /* clearing pdir to prevent races with allocations. */
  177. unsigned int res_bitshift; /* from the RIGHT! */
  178. unsigned int res_size; /* size of resource map in bytes */
  179. #ifdef CONFIG_NUMA
  180. unsigned int node; /* node where this IOC lives */
  181. #endif
  182. #if DELAYED_RESOURCE_CNT > 0
  183. spinlock_t saved_lock; /* may want to try to get this on a separate cacheline */
  184. /* than res_lock for bigger systems. */
  185. int saved_cnt;
  186. struct sba_dma_pair {
  187. dma_addr_t iova;
  188. size_t size;
  189. } saved[DELAYED_RESOURCE_CNT];
  190. #endif
  191. #ifdef PDIR_SEARCH_TIMING
  192. #define SBA_SEARCH_SAMPLE 0x100
  193. unsigned long avg_search[SBA_SEARCH_SAMPLE];
  194. unsigned long avg_idx; /* current index into avg_search */
  195. #endif
  196. /* Stuff we don't need in performance path */
  197. struct ioc *next; /* list of IOC's in system */
  198. acpi_handle handle; /* for multiple IOC's */
  199. const char *name;
  200. unsigned int func_id;
  201. unsigned int rev; /* HW revision of chip */
  202. u32 iov_size;
  203. unsigned int pdir_size; /* in bytes, determined by IOV Space size */
  204. struct pci_dev *sac_only_dev;
  205. };
  206. static struct ioc *ioc_list;
  207. static int reserve_sba_gart = 1;
  208. static SBA_INLINE void sba_mark_invalid(struct ioc *, dma_addr_t, size_t);
  209. static SBA_INLINE void sba_free_range(struct ioc *, dma_addr_t, size_t);
  210. #define sba_sg_address(sg) (page_address((sg)->page) + (sg)->offset)
  211. #ifdef FULL_VALID_PDIR
  212. static u64 prefetch_spill_page;
  213. #endif
  214. #ifdef CONFIG_PCI
  215. # define GET_IOC(dev) (((dev)->bus == &pci_bus_type) \
  216. ? ((struct ioc *) PCI_CONTROLLER(to_pci_dev(dev))->iommu) : NULL)
  217. #else
  218. # define GET_IOC(dev) NULL
  219. #endif
  220. /*
  221. ** DMA_CHUNK_SIZE is used by the SCSI mid-layer to break up
  222. ** (or rather not merge) DMA's into managable chunks.
  223. ** On parisc, this is more of the software/tuning constraint
  224. ** rather than the HW. I/O MMU allocation alogorithms can be
  225. ** faster with smaller size is (to some degree).
  226. */
  227. #define DMA_CHUNK_SIZE (BITS_PER_LONG*iovp_size)
  228. #define ROUNDUP(x,y) ((x + ((y)-1)) & ~((y)-1))
  229. /************************************
  230. ** SBA register read and write support
  231. **
  232. ** BE WARNED: register writes are posted.
  233. ** (ie follow writes which must reach HW with a read)
  234. **
  235. */
  236. #define READ_REG(addr) __raw_readq(addr)
  237. #define WRITE_REG(val, addr) __raw_writeq(val, addr)
  238. #ifdef DEBUG_SBA_INIT
  239. /**
  240. * sba_dump_tlb - debugging only - print IOMMU operating parameters
  241. * @hpa: base address of the IOMMU
  242. *
  243. * Print the size/location of the IO MMU PDIR.
  244. */
  245. static void
  246. sba_dump_tlb(char *hpa)
  247. {
  248. DBG_INIT("IO TLB at 0x%p\n", (void *)hpa);
  249. DBG_INIT("IOC_IBASE : %016lx\n", READ_REG(hpa+IOC_IBASE));
  250. DBG_INIT("IOC_IMASK : %016lx\n", READ_REG(hpa+IOC_IMASK));
  251. DBG_INIT("IOC_TCNFG : %016lx\n", READ_REG(hpa+IOC_TCNFG));
  252. DBG_INIT("IOC_PDIR_BASE: %016lx\n", READ_REG(hpa+IOC_PDIR_BASE));
  253. DBG_INIT("\n");
  254. }
  255. #endif
  256. #ifdef ASSERT_PDIR_SANITY
  257. /**
  258. * sba_dump_pdir_entry - debugging only - print one IOMMU PDIR entry
  259. * @ioc: IO MMU structure which owns the pdir we are interested in.
  260. * @msg: text to print ont the output line.
  261. * @pide: pdir index.
  262. *
  263. * Print one entry of the IO MMU PDIR in human readable form.
  264. */
  265. static void
  266. sba_dump_pdir_entry(struct ioc *ioc, char *msg, uint pide)
  267. {
  268. /* start printing from lowest pde in rval */
  269. u64 *ptr = &ioc->pdir_base[pide & ~(BITS_PER_LONG - 1)];
  270. unsigned long *rptr = (unsigned long *) &ioc->res_map[(pide >>3) & -sizeof(unsigned long)];
  271. uint rcnt;
  272. printk(KERN_DEBUG "SBA: %s rp %p bit %d rval 0x%lx\n",
  273. msg, rptr, pide & (BITS_PER_LONG - 1), *rptr);
  274. rcnt = 0;
  275. while (rcnt < BITS_PER_LONG) {
  276. printk(KERN_DEBUG "%s %2d %p %016Lx\n",
  277. (rcnt == (pide & (BITS_PER_LONG - 1)))
  278. ? " -->" : " ",
  279. rcnt, ptr, (unsigned long long) *ptr );
  280. rcnt++;
  281. ptr++;
  282. }
  283. printk(KERN_DEBUG "%s", msg);
  284. }
  285. /**
  286. * sba_check_pdir - debugging only - consistency checker
  287. * @ioc: IO MMU structure which owns the pdir we are interested in.
  288. * @msg: text to print ont the output line.
  289. *
  290. * Verify the resource map and pdir state is consistent
  291. */
  292. static int
  293. sba_check_pdir(struct ioc *ioc, char *msg)
  294. {
  295. u64 *rptr_end = (u64 *) &(ioc->res_map[ioc->res_size]);
  296. u64 *rptr = (u64 *) ioc->res_map; /* resource map ptr */
  297. u64 *pptr = ioc->pdir_base; /* pdir ptr */
  298. uint pide = 0;
  299. while (rptr < rptr_end) {
  300. u64 rval;
  301. int rcnt; /* number of bits we might check */
  302. rval = *rptr;
  303. rcnt = 64;
  304. while (rcnt) {
  305. /* Get last byte and highest bit from that */
  306. u32 pde = ((u32)((*pptr >> (63)) & 0x1));
  307. if ((rval & 0x1) ^ pde)
  308. {
  309. /*
  310. ** BUMMER! -- res_map != pdir --
  311. ** Dump rval and matching pdir entries
  312. */
  313. sba_dump_pdir_entry(ioc, msg, pide);
  314. return(1);
  315. }
  316. rcnt--;
  317. rval >>= 1; /* try the next bit */
  318. pptr++;
  319. pide++;
  320. }
  321. rptr++; /* look at next word of res_map */
  322. }
  323. /* It'd be nice if we always got here :^) */
  324. return 0;
  325. }
  326. /**
  327. * sba_dump_sg - debugging only - print Scatter-Gather list
  328. * @ioc: IO MMU structure which owns the pdir we are interested in.
  329. * @startsg: head of the SG list
  330. * @nents: number of entries in SG list
  331. *
  332. * print the SG list so we can verify it's correct by hand.
  333. */
  334. static void
  335. sba_dump_sg( struct ioc *ioc, struct scatterlist *startsg, int nents)
  336. {
  337. while (nents-- > 0) {
  338. printk(KERN_DEBUG " %d : DMA %08lx/%05x CPU %p\n", nents,
  339. startsg->dma_address, startsg->dma_length,
  340. sba_sg_address(startsg));
  341. startsg++;
  342. }
  343. }
  344. static void
  345. sba_check_sg( struct ioc *ioc, struct scatterlist *startsg, int nents)
  346. {
  347. struct scatterlist *the_sg = startsg;
  348. int the_nents = nents;
  349. while (the_nents-- > 0) {
  350. if (sba_sg_address(the_sg) == 0x0UL)
  351. sba_dump_sg(NULL, startsg, nents);
  352. the_sg++;
  353. }
  354. }
  355. #endif /* ASSERT_PDIR_SANITY */
  356. /**************************************************************
  357. *
  358. * I/O Pdir Resource Management
  359. *
  360. * Bits set in the resource map are in use.
  361. * Each bit can represent a number of pages.
  362. * LSbs represent lower addresses (IOVA's).
  363. *
  364. ***************************************************************/
  365. #define PAGES_PER_RANGE 1 /* could increase this to 4 or 8 if needed */
  366. /* Convert from IOVP to IOVA and vice versa. */
  367. #define SBA_IOVA(ioc,iovp,offset) ((ioc->ibase) | (iovp) | (offset))
  368. #define SBA_IOVP(ioc,iova) ((iova) & ~(ioc->ibase))
  369. #define PDIR_ENTRY_SIZE sizeof(u64)
  370. #define PDIR_INDEX(iovp) ((iovp)>>iovp_shift)
  371. #define RESMAP_MASK(n) ~(~0UL << (n))
  372. #define RESMAP_IDX_MASK (sizeof(unsigned long) - 1)
  373. /**
  374. * For most cases the normal get_order is sufficient, however it limits us
  375. * to PAGE_SIZE being the minimum mapping alignment and TC flush granularity.
  376. * It only incurs about 1 clock cycle to use this one with the static variable
  377. * and makes the code more intuitive.
  378. */
  379. static SBA_INLINE int
  380. get_iovp_order (unsigned long size)
  381. {
  382. long double d = size - 1;
  383. long order;
  384. order = ia64_getf_exp(d);
  385. order = order - iovp_shift - 0xffff + 1;
  386. if (order < 0)
  387. order = 0;
  388. return order;
  389. }
  390. /**
  391. * sba_search_bitmap - find free space in IO PDIR resource bitmap
  392. * @ioc: IO MMU structure which owns the pdir we are interested in.
  393. * @bits_wanted: number of entries we need.
  394. *
  395. * Find consecutive free bits in resource bitmap.
  396. * Each bit represents one entry in the IO Pdir.
  397. * Cool perf optimization: search for log2(size) bits at a time.
  398. */
  399. static SBA_INLINE unsigned long
  400. sba_search_bitmap(struct ioc *ioc, unsigned long bits_wanted)
  401. {
  402. unsigned long *res_ptr = ioc->res_hint;
  403. unsigned long *res_end = (unsigned long *) &(ioc->res_map[ioc->res_size]);
  404. unsigned long pide = ~0UL;
  405. ASSERT(((unsigned long) ioc->res_hint & (sizeof(unsigned long) - 1UL)) == 0);
  406. ASSERT(res_ptr < res_end);
  407. /*
  408. * N.B. REO/Grande defect AR2305 can cause TLB fetch timeouts
  409. * if a TLB entry is purged while in use. sba_mark_invalid()
  410. * purges IOTLB entries in power-of-two sizes, so we also
  411. * allocate IOVA space in power-of-two sizes.
  412. */
  413. bits_wanted = 1UL << get_iovp_order(bits_wanted << iovp_shift);
  414. if (likely(bits_wanted == 1)) {
  415. unsigned int bitshiftcnt;
  416. for(; res_ptr < res_end ; res_ptr++) {
  417. if (likely(*res_ptr != ~0UL)) {
  418. bitshiftcnt = ffz(*res_ptr);
  419. *res_ptr |= (1UL << bitshiftcnt);
  420. pide = ((unsigned long)res_ptr - (unsigned long)ioc->res_map);
  421. pide <<= 3; /* convert to bit address */
  422. pide += bitshiftcnt;
  423. ioc->res_bitshift = bitshiftcnt + bits_wanted;
  424. goto found_it;
  425. }
  426. }
  427. goto not_found;
  428. }
  429. if (likely(bits_wanted <= BITS_PER_LONG/2)) {
  430. /*
  431. ** Search the resource bit map on well-aligned values.
  432. ** "o" is the alignment.
  433. ** We need the alignment to invalidate I/O TLB using
  434. ** SBA HW features in the unmap path.
  435. */
  436. unsigned long o = 1 << get_iovp_order(bits_wanted << iovp_shift);
  437. uint bitshiftcnt = ROUNDUP(ioc->res_bitshift, o);
  438. unsigned long mask, base_mask;
  439. base_mask = RESMAP_MASK(bits_wanted);
  440. mask = base_mask << bitshiftcnt;
  441. DBG_RES("%s() o %ld %p", __FUNCTION__, o, res_ptr);
  442. for(; res_ptr < res_end ; res_ptr++)
  443. {
  444. DBG_RES(" %p %lx %lx\n", res_ptr, mask, *res_ptr);
  445. ASSERT(0 != mask);
  446. for (; mask ; mask <<= o, bitshiftcnt += o) {
  447. if(0 == ((*res_ptr) & mask)) {
  448. *res_ptr |= mask; /* mark resources busy! */
  449. pide = ((unsigned long)res_ptr - (unsigned long)ioc->res_map);
  450. pide <<= 3; /* convert to bit address */
  451. pide += bitshiftcnt;
  452. ioc->res_bitshift = bitshiftcnt + bits_wanted;
  453. goto found_it;
  454. }
  455. }
  456. bitshiftcnt = 0;
  457. mask = base_mask;
  458. }
  459. } else {
  460. int qwords, bits, i;
  461. unsigned long *end;
  462. qwords = bits_wanted >> 6; /* /64 */
  463. bits = bits_wanted - (qwords * BITS_PER_LONG);
  464. end = res_end - qwords;
  465. for (; res_ptr < end; res_ptr++) {
  466. for (i = 0 ; i < qwords ; i++) {
  467. if (res_ptr[i] != 0)
  468. goto next_ptr;
  469. }
  470. if (bits && res_ptr[i] && (__ffs(res_ptr[i]) < bits))
  471. continue;
  472. /* Found it, mark it */
  473. for (i = 0 ; i < qwords ; i++)
  474. res_ptr[i] = ~0UL;
  475. res_ptr[i] |= RESMAP_MASK(bits);
  476. pide = ((unsigned long)res_ptr - (unsigned long)ioc->res_map);
  477. pide <<= 3; /* convert to bit address */
  478. res_ptr += qwords;
  479. ioc->res_bitshift = bits;
  480. goto found_it;
  481. next_ptr:
  482. ;
  483. }
  484. }
  485. not_found:
  486. prefetch(ioc->res_map);
  487. ioc->res_hint = (unsigned long *) ioc->res_map;
  488. ioc->res_bitshift = 0;
  489. return (pide);
  490. found_it:
  491. ioc->res_hint = res_ptr;
  492. return (pide);
  493. }
  494. /**
  495. * sba_alloc_range - find free bits and mark them in IO PDIR resource bitmap
  496. * @ioc: IO MMU structure which owns the pdir we are interested in.
  497. * @size: number of bytes to create a mapping for
  498. *
  499. * Given a size, find consecutive unmarked and then mark those bits in the
  500. * resource bit map.
  501. */
  502. static int
  503. sba_alloc_range(struct ioc *ioc, size_t size)
  504. {
  505. unsigned int pages_needed = size >> iovp_shift;
  506. #ifdef PDIR_SEARCH_TIMING
  507. unsigned long itc_start;
  508. #endif
  509. unsigned long pide;
  510. unsigned long flags;
  511. ASSERT(pages_needed);
  512. ASSERT(0 == (size & ~iovp_mask));
  513. spin_lock_irqsave(&ioc->res_lock, flags);
  514. #ifdef PDIR_SEARCH_TIMING
  515. itc_start = ia64_get_itc();
  516. #endif
  517. /*
  518. ** "seek and ye shall find"...praying never hurts either...
  519. */
  520. pide = sba_search_bitmap(ioc, pages_needed);
  521. if (unlikely(pide >= (ioc->res_size << 3))) {
  522. pide = sba_search_bitmap(ioc, pages_needed);
  523. if (unlikely(pide >= (ioc->res_size << 3))) {
  524. #if DELAYED_RESOURCE_CNT > 0
  525. /*
  526. ** With delayed resource freeing, we can give this one more shot. We're
  527. ** getting close to being in trouble here, so do what we can to make this
  528. ** one count.
  529. */
  530. spin_lock(&ioc->saved_lock);
  531. if (ioc->saved_cnt > 0) {
  532. struct sba_dma_pair *d;
  533. int cnt = ioc->saved_cnt;
  534. d = &(ioc->saved[ioc->saved_cnt]);
  535. while (cnt--) {
  536. sba_mark_invalid(ioc, d->iova, d->size);
  537. sba_free_range(ioc, d->iova, d->size);
  538. d--;
  539. }
  540. ioc->saved_cnt = 0;
  541. READ_REG(ioc->ioc_hpa+IOC_PCOM); /* flush purges */
  542. }
  543. spin_unlock(&ioc->saved_lock);
  544. pide = sba_search_bitmap(ioc, pages_needed);
  545. if (unlikely(pide >= (ioc->res_size << 3)))
  546. panic(__FILE__ ": I/O MMU @ %p is out of mapping resources\n",
  547. ioc->ioc_hpa);
  548. #else
  549. panic(__FILE__ ": I/O MMU @ %p is out of mapping resources\n",
  550. ioc->ioc_hpa);
  551. #endif
  552. }
  553. }
  554. #ifdef PDIR_SEARCH_TIMING
  555. ioc->avg_search[ioc->avg_idx++] = (ia64_get_itc() - itc_start) / pages_needed;
  556. ioc->avg_idx &= SBA_SEARCH_SAMPLE - 1;
  557. #endif
  558. prefetchw(&(ioc->pdir_base[pide]));
  559. #ifdef ASSERT_PDIR_SANITY
  560. /* verify the first enable bit is clear */
  561. if(0x00 != ((u8 *) ioc->pdir_base)[pide*PDIR_ENTRY_SIZE + 7]) {
  562. sba_dump_pdir_entry(ioc, "sba_search_bitmap() botched it?", pide);
  563. }
  564. #endif
  565. DBG_RES("%s(%x) %d -> %lx hint %x/%x\n",
  566. __FUNCTION__, size, pages_needed, pide,
  567. (uint) ((unsigned long) ioc->res_hint - (unsigned long) ioc->res_map),
  568. ioc->res_bitshift );
  569. spin_unlock_irqrestore(&ioc->res_lock, flags);
  570. return (pide);
  571. }
  572. /**
  573. * sba_free_range - unmark bits in IO PDIR resource bitmap
  574. * @ioc: IO MMU structure which owns the pdir we are interested in.
  575. * @iova: IO virtual address which was previously allocated.
  576. * @size: number of bytes to create a mapping for
  577. *
  578. * clear bits in the ioc's resource map
  579. */
  580. static SBA_INLINE void
  581. sba_free_range(struct ioc *ioc, dma_addr_t iova, size_t size)
  582. {
  583. unsigned long iovp = SBA_IOVP(ioc, iova);
  584. unsigned int pide = PDIR_INDEX(iovp);
  585. unsigned int ridx = pide >> 3; /* convert bit to byte address */
  586. unsigned long *res_ptr = (unsigned long *) &((ioc)->res_map[ridx & ~RESMAP_IDX_MASK]);
  587. int bits_not_wanted = size >> iovp_shift;
  588. unsigned long m;
  589. /* Round up to power-of-two size: see AR2305 note above */
  590. bits_not_wanted = 1UL << get_iovp_order(bits_not_wanted << iovp_shift);
  591. for (; bits_not_wanted > 0 ; res_ptr++) {
  592. if (unlikely(bits_not_wanted > BITS_PER_LONG)) {
  593. /* these mappings start 64bit aligned */
  594. *res_ptr = 0UL;
  595. bits_not_wanted -= BITS_PER_LONG;
  596. pide += BITS_PER_LONG;
  597. } else {
  598. /* 3-bits "bit" address plus 2 (or 3) bits for "byte" == bit in word */
  599. m = RESMAP_MASK(bits_not_wanted) << (pide & (BITS_PER_LONG - 1));
  600. bits_not_wanted = 0;
  601. DBG_RES("%s( ,%x,%x) %x/%lx %x %p %lx\n", __FUNCTION__, (uint) iova, size,
  602. bits_not_wanted, m, pide, res_ptr, *res_ptr);
  603. ASSERT(m != 0);
  604. ASSERT(bits_not_wanted);
  605. ASSERT((*res_ptr & m) == m); /* verify same bits are set */
  606. *res_ptr &= ~m;
  607. }
  608. }
  609. }
  610. /**************************************************************
  611. *
  612. * "Dynamic DMA Mapping" support (aka "Coherent I/O")
  613. *
  614. ***************************************************************/
  615. /**
  616. * sba_io_pdir_entry - fill in one IO PDIR entry
  617. * @pdir_ptr: pointer to IO PDIR entry
  618. * @vba: Virtual CPU address of buffer to map
  619. *
  620. * SBA Mapping Routine
  621. *
  622. * Given a virtual address (vba, arg1) sba_io_pdir_entry()
  623. * loads the I/O PDIR entry pointed to by pdir_ptr (arg0).
  624. * Each IO Pdir entry consists of 8 bytes as shown below
  625. * (LSB == bit 0):
  626. *
  627. * 63 40 11 7 0
  628. * +-+---------------------+----------------------------------+----+--------+
  629. * |V| U | PPN[39:12] | U | FF |
  630. * +-+---------------------+----------------------------------+----+--------+
  631. *
  632. * V == Valid Bit
  633. * U == Unused
  634. * PPN == Physical Page Number
  635. *
  636. * The physical address fields are filled with the results of virt_to_phys()
  637. * on the vba.
  638. */
  639. #if 1
  640. #define sba_io_pdir_entry(pdir_ptr, vba) *pdir_ptr = ((vba & ~0xE000000000000FFFULL) \
  641. | 0x8000000000000000ULL)
  642. #else
  643. void SBA_INLINE
  644. sba_io_pdir_entry(u64 *pdir_ptr, unsigned long vba)
  645. {
  646. *pdir_ptr = ((vba & ~0xE000000000000FFFULL) | 0x80000000000000FFULL);
  647. }
  648. #endif
  649. #ifdef ENABLE_MARK_CLEAN
  650. /**
  651. * Since DMA is i-cache coherent, any (complete) pages that were written via
  652. * DMA can be marked as "clean" so that lazy_mmu_prot_update() doesn't have to
  653. * flush them when they get mapped into an executable vm-area.
  654. */
  655. static void
  656. mark_clean (void *addr, size_t size)
  657. {
  658. unsigned long pg_addr, end;
  659. pg_addr = PAGE_ALIGN((unsigned long) addr);
  660. end = (unsigned long) addr + size;
  661. while (pg_addr + PAGE_SIZE <= end) {
  662. struct page *page = virt_to_page((void *)pg_addr);
  663. set_bit(PG_arch_1, &page->flags);
  664. pg_addr += PAGE_SIZE;
  665. }
  666. }
  667. #endif
  668. /**
  669. * sba_mark_invalid - invalidate one or more IO PDIR entries
  670. * @ioc: IO MMU structure which owns the pdir we are interested in.
  671. * @iova: IO Virtual Address mapped earlier
  672. * @byte_cnt: number of bytes this mapping covers.
  673. *
  674. * Marking the IO PDIR entry(ies) as Invalid and invalidate
  675. * corresponding IO TLB entry. The PCOM (Purge Command Register)
  676. * is to purge stale entries in the IO TLB when unmapping entries.
  677. *
  678. * The PCOM register supports purging of multiple pages, with a minium
  679. * of 1 page and a maximum of 2GB. Hardware requires the address be
  680. * aligned to the size of the range being purged. The size of the range
  681. * must be a power of 2. The "Cool perf optimization" in the
  682. * allocation routine helps keep that true.
  683. */
  684. static SBA_INLINE void
  685. sba_mark_invalid(struct ioc *ioc, dma_addr_t iova, size_t byte_cnt)
  686. {
  687. u32 iovp = (u32) SBA_IOVP(ioc,iova);
  688. int off = PDIR_INDEX(iovp);
  689. /* Must be non-zero and rounded up */
  690. ASSERT(byte_cnt > 0);
  691. ASSERT(0 == (byte_cnt & ~iovp_mask));
  692. #ifdef ASSERT_PDIR_SANITY
  693. /* Assert first pdir entry is set */
  694. if (!(ioc->pdir_base[off] >> 60)) {
  695. sba_dump_pdir_entry(ioc,"sba_mark_invalid()", PDIR_INDEX(iovp));
  696. }
  697. #endif
  698. if (byte_cnt <= iovp_size)
  699. {
  700. ASSERT(off < ioc->pdir_size);
  701. iovp |= iovp_shift; /* set "size" field for PCOM */
  702. #ifndef FULL_VALID_PDIR
  703. /*
  704. ** clear I/O PDIR entry "valid" bit
  705. ** Do NOT clear the rest - save it for debugging.
  706. ** We should only clear bits that have previously
  707. ** been enabled.
  708. */
  709. ioc->pdir_base[off] &= ~(0x80000000000000FFULL);
  710. #else
  711. /*
  712. ** If we want to maintain the PDIR as valid, put in
  713. ** the spill page so devices prefetching won't
  714. ** cause a hard fail.
  715. */
  716. ioc->pdir_base[off] = (0x80000000000000FFULL | prefetch_spill_page);
  717. #endif
  718. } else {
  719. u32 t = get_iovp_order(byte_cnt) + iovp_shift;
  720. iovp |= t;
  721. ASSERT(t <= 31); /* 2GB! Max value of "size" field */
  722. do {
  723. /* verify this pdir entry is enabled */
  724. ASSERT(ioc->pdir_base[off] >> 63);
  725. #ifndef FULL_VALID_PDIR
  726. /* clear I/O Pdir entry "valid" bit first */
  727. ioc->pdir_base[off] &= ~(0x80000000000000FFULL);
  728. #else
  729. ioc->pdir_base[off] = (0x80000000000000FFULL | prefetch_spill_page);
  730. #endif
  731. off++;
  732. byte_cnt -= iovp_size;
  733. } while (byte_cnt > 0);
  734. }
  735. WRITE_REG(iovp | ioc->ibase, ioc->ioc_hpa+IOC_PCOM);
  736. }
  737. /**
  738. * sba_map_single - map one buffer and return IOVA for DMA
  739. * @dev: instance of PCI owned by the driver that's asking.
  740. * @addr: driver buffer to map.
  741. * @size: number of bytes to map in driver buffer.
  742. * @dir: R/W or both.
  743. *
  744. * See Documentation/DMA-mapping.txt
  745. */
  746. dma_addr_t
  747. sba_map_single(struct device *dev, void *addr, size_t size, int dir)
  748. {
  749. struct ioc *ioc;
  750. dma_addr_t iovp;
  751. dma_addr_t offset;
  752. u64 *pdir_start;
  753. int pide;
  754. #ifdef ASSERT_PDIR_SANITY
  755. unsigned long flags;
  756. #endif
  757. #ifdef ALLOW_IOV_BYPASS
  758. unsigned long pci_addr = virt_to_phys(addr);
  759. #endif
  760. #ifdef ALLOW_IOV_BYPASS
  761. ASSERT(to_pci_dev(dev)->dma_mask);
  762. /*
  763. ** Check if the PCI device can DMA to ptr... if so, just return ptr
  764. */
  765. if (likely((pci_addr & ~to_pci_dev(dev)->dma_mask) == 0)) {
  766. /*
  767. ** Device is bit capable of DMA'ing to the buffer...
  768. ** just return the PCI address of ptr
  769. */
  770. DBG_BYPASS("sba_map_single() bypass mask/addr: 0x%lx/0x%lx\n",
  771. to_pci_dev(dev)->dma_mask, pci_addr);
  772. return pci_addr;
  773. }
  774. #endif
  775. ioc = GET_IOC(dev);
  776. ASSERT(ioc);
  777. prefetch(ioc->res_hint);
  778. ASSERT(size > 0);
  779. ASSERT(size <= DMA_CHUNK_SIZE);
  780. /* save offset bits */
  781. offset = ((dma_addr_t) (long) addr) & ~iovp_mask;
  782. /* round up to nearest iovp_size */
  783. size = (size + offset + ~iovp_mask) & iovp_mask;
  784. #ifdef ASSERT_PDIR_SANITY
  785. spin_lock_irqsave(&ioc->res_lock, flags);
  786. if (sba_check_pdir(ioc,"Check before sba_map_single()"))
  787. panic("Sanity check failed");
  788. spin_unlock_irqrestore(&ioc->res_lock, flags);
  789. #endif
  790. pide = sba_alloc_range(ioc, size);
  791. iovp = (dma_addr_t) pide << iovp_shift;
  792. DBG_RUN("%s() 0x%p -> 0x%lx\n",
  793. __FUNCTION__, addr, (long) iovp | offset);
  794. pdir_start = &(ioc->pdir_base[pide]);
  795. while (size > 0) {
  796. ASSERT(((u8 *)pdir_start)[7] == 0); /* verify availability */
  797. sba_io_pdir_entry(pdir_start, (unsigned long) addr);
  798. DBG_RUN(" pdir 0x%p %lx\n", pdir_start, *pdir_start);
  799. addr += iovp_size;
  800. size -= iovp_size;
  801. pdir_start++;
  802. }
  803. /* force pdir update */
  804. wmb();
  805. /* form complete address */
  806. #ifdef ASSERT_PDIR_SANITY
  807. spin_lock_irqsave(&ioc->res_lock, flags);
  808. sba_check_pdir(ioc,"Check after sba_map_single()");
  809. spin_unlock_irqrestore(&ioc->res_lock, flags);
  810. #endif
  811. return SBA_IOVA(ioc, iovp, offset);
  812. }
  813. /**
  814. * sba_unmap_single - unmap one IOVA and free resources
  815. * @dev: instance of PCI owned by the driver that's asking.
  816. * @iova: IOVA of driver buffer previously mapped.
  817. * @size: number of bytes mapped in driver buffer.
  818. * @dir: R/W or both.
  819. *
  820. * See Documentation/DMA-mapping.txt
  821. */
  822. void sba_unmap_single(struct device *dev, dma_addr_t iova, size_t size, int dir)
  823. {
  824. struct ioc *ioc;
  825. #if DELAYED_RESOURCE_CNT > 0
  826. struct sba_dma_pair *d;
  827. #endif
  828. unsigned long flags;
  829. dma_addr_t offset;
  830. ioc = GET_IOC(dev);
  831. ASSERT(ioc);
  832. #ifdef ALLOW_IOV_BYPASS
  833. if (likely((iova & ioc->imask) != ioc->ibase)) {
  834. /*
  835. ** Address does not fall w/in IOVA, must be bypassing
  836. */
  837. DBG_BYPASS("sba_unmap_single() bypass addr: 0x%lx\n", iova);
  838. #ifdef ENABLE_MARK_CLEAN
  839. if (dir == DMA_FROM_DEVICE) {
  840. mark_clean(phys_to_virt(iova), size);
  841. }
  842. #endif
  843. return;
  844. }
  845. #endif
  846. offset = iova & ~iovp_mask;
  847. DBG_RUN("%s() iovp 0x%lx/%x\n",
  848. __FUNCTION__, (long) iova, size);
  849. iova ^= offset; /* clear offset bits */
  850. size += offset;
  851. size = ROUNDUP(size, iovp_size);
  852. #if DELAYED_RESOURCE_CNT > 0
  853. spin_lock_irqsave(&ioc->saved_lock, flags);
  854. d = &(ioc->saved[ioc->saved_cnt]);
  855. d->iova = iova;
  856. d->size = size;
  857. if (unlikely(++(ioc->saved_cnt) >= DELAYED_RESOURCE_CNT)) {
  858. int cnt = ioc->saved_cnt;
  859. spin_lock(&ioc->res_lock);
  860. while (cnt--) {
  861. sba_mark_invalid(ioc, d->iova, d->size);
  862. sba_free_range(ioc, d->iova, d->size);
  863. d--;
  864. }
  865. ioc->saved_cnt = 0;
  866. READ_REG(ioc->ioc_hpa+IOC_PCOM); /* flush purges */
  867. spin_unlock(&ioc->res_lock);
  868. }
  869. spin_unlock_irqrestore(&ioc->saved_lock, flags);
  870. #else /* DELAYED_RESOURCE_CNT == 0 */
  871. spin_lock_irqsave(&ioc->res_lock, flags);
  872. sba_mark_invalid(ioc, iova, size);
  873. sba_free_range(ioc, iova, size);
  874. READ_REG(ioc->ioc_hpa+IOC_PCOM); /* flush purges */
  875. spin_unlock_irqrestore(&ioc->res_lock, flags);
  876. #endif /* DELAYED_RESOURCE_CNT == 0 */
  877. #ifdef ENABLE_MARK_CLEAN
  878. if (dir == DMA_FROM_DEVICE) {
  879. u32 iovp = (u32) SBA_IOVP(ioc,iova);
  880. int off = PDIR_INDEX(iovp);
  881. void *addr;
  882. if (size <= iovp_size) {
  883. addr = phys_to_virt(ioc->pdir_base[off] &
  884. ~0xE000000000000FFFULL);
  885. mark_clean(addr, size);
  886. } else {
  887. size_t byte_cnt = size;
  888. do {
  889. addr = phys_to_virt(ioc->pdir_base[off] &
  890. ~0xE000000000000FFFULL);
  891. mark_clean(addr, min(byte_cnt, iovp_size));
  892. off++;
  893. byte_cnt -= iovp_size;
  894. } while (byte_cnt > 0);
  895. }
  896. }
  897. #endif
  898. }
  899. /**
  900. * sba_alloc_coherent - allocate/map shared mem for DMA
  901. * @dev: instance of PCI owned by the driver that's asking.
  902. * @size: number of bytes mapped in driver buffer.
  903. * @dma_handle: IOVA of new buffer.
  904. *
  905. * See Documentation/DMA-mapping.txt
  906. */
  907. void *
  908. sba_alloc_coherent (struct device *dev, size_t size, dma_addr_t *dma_handle, int flags)
  909. {
  910. struct ioc *ioc;
  911. void *addr;
  912. ioc = GET_IOC(dev);
  913. ASSERT(ioc);
  914. #ifdef CONFIG_NUMA
  915. {
  916. struct page *page;
  917. page = alloc_pages_node(ioc->node == MAX_NUMNODES ?
  918. numa_node_id() : ioc->node, flags,
  919. get_order(size));
  920. if (unlikely(!page))
  921. return NULL;
  922. addr = page_address(page);
  923. }
  924. #else
  925. addr = (void *) __get_free_pages(flags, get_order(size));
  926. #endif
  927. if (unlikely(!addr))
  928. return NULL;
  929. memset(addr, 0, size);
  930. *dma_handle = virt_to_phys(addr);
  931. #ifdef ALLOW_IOV_BYPASS
  932. ASSERT(dev->coherent_dma_mask);
  933. /*
  934. ** Check if the PCI device can DMA to ptr... if so, just return ptr
  935. */
  936. if (likely((*dma_handle & ~dev->coherent_dma_mask) == 0)) {
  937. DBG_BYPASS("sba_alloc_coherent() bypass mask/addr: 0x%lx/0x%lx\n",
  938. dev->coherent_dma_mask, *dma_handle);
  939. return addr;
  940. }
  941. #endif
  942. /*
  943. * If device can't bypass or bypass is disabled, pass the 32bit fake
  944. * device to map single to get an iova mapping.
  945. */
  946. *dma_handle = sba_map_single(&ioc->sac_only_dev->dev, addr, size, 0);
  947. return addr;
  948. }
  949. /**
  950. * sba_free_coherent - free/unmap shared mem for DMA
  951. * @dev: instance of PCI owned by the driver that's asking.
  952. * @size: number of bytes mapped in driver buffer.
  953. * @vaddr: virtual address IOVA of "consistent" buffer.
  954. * @dma_handler: IO virtual address of "consistent" buffer.
  955. *
  956. * See Documentation/DMA-mapping.txt
  957. */
  958. void sba_free_coherent (struct device *dev, size_t size, void *vaddr, dma_addr_t dma_handle)
  959. {
  960. sba_unmap_single(dev, dma_handle, size, 0);
  961. free_pages((unsigned long) vaddr, get_order(size));
  962. }
  963. /*
  964. ** Since 0 is a valid pdir_base index value, can't use that
  965. ** to determine if a value is valid or not. Use a flag to indicate
  966. ** the SG list entry contains a valid pdir index.
  967. */
  968. #define PIDE_FLAG 0x1UL
  969. #ifdef DEBUG_LARGE_SG_ENTRIES
  970. int dump_run_sg = 0;
  971. #endif
  972. /**
  973. * sba_fill_pdir - write allocated SG entries into IO PDIR
  974. * @ioc: IO MMU structure which owns the pdir we are interested in.
  975. * @startsg: list of IOVA/size pairs
  976. * @nents: number of entries in startsg list
  977. *
  978. * Take preprocessed SG list and write corresponding entries
  979. * in the IO PDIR.
  980. */
  981. static SBA_INLINE int
  982. sba_fill_pdir(
  983. struct ioc *ioc,
  984. struct scatterlist *startsg,
  985. int nents)
  986. {
  987. struct scatterlist *dma_sg = startsg; /* pointer to current DMA */
  988. int n_mappings = 0;
  989. u64 *pdirp = NULL;
  990. unsigned long dma_offset = 0;
  991. dma_sg--;
  992. while (nents-- > 0) {
  993. int cnt = startsg->dma_length;
  994. startsg->dma_length = 0;
  995. #ifdef DEBUG_LARGE_SG_ENTRIES
  996. if (dump_run_sg)
  997. printk(" %2d : %08lx/%05x %p\n",
  998. nents, startsg->dma_address, cnt,
  999. sba_sg_address(startsg));
  1000. #else
  1001. DBG_RUN_SG(" %d : %08lx/%05x %p\n",
  1002. nents, startsg->dma_address, cnt,
  1003. sba_sg_address(startsg));
  1004. #endif
  1005. /*
  1006. ** Look for the start of a new DMA stream
  1007. */
  1008. if (startsg->dma_address & PIDE_FLAG) {
  1009. u32 pide = startsg->dma_address & ~PIDE_FLAG;
  1010. dma_offset = (unsigned long) pide & ~iovp_mask;
  1011. startsg->dma_address = 0;
  1012. dma_sg++;
  1013. dma_sg->dma_address = pide | ioc->ibase;
  1014. pdirp = &(ioc->pdir_base[pide >> iovp_shift]);
  1015. n_mappings++;
  1016. }
  1017. /*
  1018. ** Look for a VCONTIG chunk
  1019. */
  1020. if (cnt) {
  1021. unsigned long vaddr = (unsigned long) sba_sg_address(startsg);
  1022. ASSERT(pdirp);
  1023. /* Since multiple Vcontig blocks could make up
  1024. ** one DMA stream, *add* cnt to dma_len.
  1025. */
  1026. dma_sg->dma_length += cnt;
  1027. cnt += dma_offset;
  1028. dma_offset=0; /* only want offset on first chunk */
  1029. cnt = ROUNDUP(cnt, iovp_size);
  1030. do {
  1031. sba_io_pdir_entry(pdirp, vaddr);
  1032. vaddr += iovp_size;
  1033. cnt -= iovp_size;
  1034. pdirp++;
  1035. } while (cnt > 0);
  1036. }
  1037. startsg++;
  1038. }
  1039. /* force pdir update */
  1040. wmb();
  1041. #ifdef DEBUG_LARGE_SG_ENTRIES
  1042. dump_run_sg = 0;
  1043. #endif
  1044. return(n_mappings);
  1045. }
  1046. /*
  1047. ** Two address ranges are DMA contiguous *iff* "end of prev" and
  1048. ** "start of next" are both on an IOV page boundary.
  1049. **
  1050. ** (shift left is a quick trick to mask off upper bits)
  1051. */
  1052. #define DMA_CONTIG(__X, __Y) \
  1053. (((((unsigned long) __X) | ((unsigned long) __Y)) << (BITS_PER_LONG - iovp_shift)) == 0UL)
  1054. /**
  1055. * sba_coalesce_chunks - preprocess the SG list
  1056. * @ioc: IO MMU structure which owns the pdir we are interested in.
  1057. * @startsg: list of IOVA/size pairs
  1058. * @nents: number of entries in startsg list
  1059. *
  1060. * First pass is to walk the SG list and determine where the breaks are
  1061. * in the DMA stream. Allocates PDIR entries but does not fill them.
  1062. * Returns the number of DMA chunks.
  1063. *
  1064. * Doing the fill separate from the coalescing/allocation keeps the
  1065. * code simpler. Future enhancement could make one pass through
  1066. * the sglist do both.
  1067. */
  1068. static SBA_INLINE int
  1069. sba_coalesce_chunks( struct ioc *ioc,
  1070. struct scatterlist *startsg,
  1071. int nents)
  1072. {
  1073. struct scatterlist *vcontig_sg; /* VCONTIG chunk head */
  1074. unsigned long vcontig_len; /* len of VCONTIG chunk */
  1075. unsigned long vcontig_end;
  1076. struct scatterlist *dma_sg; /* next DMA stream head */
  1077. unsigned long dma_offset, dma_len; /* start/len of DMA stream */
  1078. int n_mappings = 0;
  1079. while (nents > 0) {
  1080. unsigned long vaddr = (unsigned long) sba_sg_address(startsg);
  1081. /*
  1082. ** Prepare for first/next DMA stream
  1083. */
  1084. dma_sg = vcontig_sg = startsg;
  1085. dma_len = vcontig_len = vcontig_end = startsg->length;
  1086. vcontig_end += vaddr;
  1087. dma_offset = vaddr & ~iovp_mask;
  1088. /* PARANOID: clear entries */
  1089. startsg->dma_address = startsg->dma_length = 0;
  1090. /*
  1091. ** This loop terminates one iteration "early" since
  1092. ** it's always looking one "ahead".
  1093. */
  1094. while (--nents > 0) {
  1095. unsigned long vaddr; /* tmp */
  1096. startsg++;
  1097. /* PARANOID */
  1098. startsg->dma_address = startsg->dma_length = 0;
  1099. /* catch brokenness in SCSI layer */
  1100. ASSERT(startsg->length <= DMA_CHUNK_SIZE);
  1101. /*
  1102. ** First make sure current dma stream won't
  1103. ** exceed DMA_CHUNK_SIZE if we coalesce the
  1104. ** next entry.
  1105. */
  1106. if (((dma_len + dma_offset + startsg->length + ~iovp_mask) & iovp_mask)
  1107. > DMA_CHUNK_SIZE)
  1108. break;
  1109. /*
  1110. ** Then look for virtually contiguous blocks.
  1111. **
  1112. ** append the next transaction?
  1113. */
  1114. vaddr = (unsigned long) sba_sg_address(startsg);
  1115. if (vcontig_end == vaddr)
  1116. {
  1117. vcontig_len += startsg->length;
  1118. vcontig_end += startsg->length;
  1119. dma_len += startsg->length;
  1120. continue;
  1121. }
  1122. #ifdef DEBUG_LARGE_SG_ENTRIES
  1123. dump_run_sg = (vcontig_len > iovp_size);
  1124. #endif
  1125. /*
  1126. ** Not virtually contigous.
  1127. ** Terminate prev chunk.
  1128. ** Start a new chunk.
  1129. **
  1130. ** Once we start a new VCONTIG chunk, dma_offset
  1131. ** can't change. And we need the offset from the first
  1132. ** chunk - not the last one. Ergo Successive chunks
  1133. ** must start on page boundaries and dove tail
  1134. ** with it's predecessor.
  1135. */
  1136. vcontig_sg->dma_length = vcontig_len;
  1137. vcontig_sg = startsg;
  1138. vcontig_len = startsg->length;
  1139. /*
  1140. ** 3) do the entries end/start on page boundaries?
  1141. ** Don't update vcontig_end until we've checked.
  1142. */
  1143. if (DMA_CONTIG(vcontig_end, vaddr))
  1144. {
  1145. vcontig_end = vcontig_len + vaddr;
  1146. dma_len += vcontig_len;
  1147. continue;
  1148. } else {
  1149. break;
  1150. }
  1151. }
  1152. /*
  1153. ** End of DMA Stream
  1154. ** Terminate last VCONTIG block.
  1155. ** Allocate space for DMA stream.
  1156. */
  1157. vcontig_sg->dma_length = vcontig_len;
  1158. dma_len = (dma_len + dma_offset + ~iovp_mask) & iovp_mask;
  1159. ASSERT(dma_len <= DMA_CHUNK_SIZE);
  1160. dma_sg->dma_address = (dma_addr_t) (PIDE_FLAG
  1161. | (sba_alloc_range(ioc, dma_len) << iovp_shift)
  1162. | dma_offset);
  1163. n_mappings++;
  1164. }
  1165. return n_mappings;
  1166. }
  1167. /**
  1168. * sba_map_sg - map Scatter/Gather list
  1169. * @dev: instance of PCI owned by the driver that's asking.
  1170. * @sglist: array of buffer/length pairs
  1171. * @nents: number of entries in list
  1172. * @dir: R/W or both.
  1173. *
  1174. * See Documentation/DMA-mapping.txt
  1175. */
  1176. int sba_map_sg(struct device *dev, struct scatterlist *sglist, int nents, int dir)
  1177. {
  1178. struct ioc *ioc;
  1179. int coalesced, filled = 0;
  1180. #ifdef ASSERT_PDIR_SANITY
  1181. unsigned long flags;
  1182. #endif
  1183. #ifdef ALLOW_IOV_BYPASS_SG
  1184. struct scatterlist *sg;
  1185. #endif
  1186. DBG_RUN_SG("%s() START %d entries\n", __FUNCTION__, nents);
  1187. ioc = GET_IOC(dev);
  1188. ASSERT(ioc);
  1189. #ifdef ALLOW_IOV_BYPASS_SG
  1190. ASSERT(to_pci_dev(dev)->dma_mask);
  1191. if (likely((ioc->dma_mask & ~to_pci_dev(dev)->dma_mask) == 0)) {
  1192. for (sg = sglist ; filled < nents ; filled++, sg++){
  1193. sg->dma_length = sg->length;
  1194. sg->dma_address = virt_to_phys(sba_sg_address(sg));
  1195. }
  1196. return filled;
  1197. }
  1198. #endif
  1199. /* Fast path single entry scatterlists. */
  1200. if (nents == 1) {
  1201. sglist->dma_length = sglist->length;
  1202. sglist->dma_address = sba_map_single(dev, sba_sg_address(sglist), sglist->length, dir);
  1203. return 1;
  1204. }
  1205. #ifdef ASSERT_PDIR_SANITY
  1206. spin_lock_irqsave(&ioc->res_lock, flags);
  1207. if (sba_check_pdir(ioc,"Check before sba_map_sg()"))
  1208. {
  1209. sba_dump_sg(ioc, sglist, nents);
  1210. panic("Check before sba_map_sg()");
  1211. }
  1212. spin_unlock_irqrestore(&ioc->res_lock, flags);
  1213. #endif
  1214. prefetch(ioc->res_hint);
  1215. /*
  1216. ** First coalesce the chunks and allocate I/O pdir space
  1217. **
  1218. ** If this is one DMA stream, we can properly map using the
  1219. ** correct virtual address associated with each DMA page.
  1220. ** w/o this association, we wouldn't have coherent DMA!
  1221. ** Access to the virtual address is what forces a two pass algorithm.
  1222. */
  1223. coalesced = sba_coalesce_chunks(ioc, sglist, nents);
  1224. /*
  1225. ** Program the I/O Pdir
  1226. **
  1227. ** map the virtual addresses to the I/O Pdir
  1228. ** o dma_address will contain the pdir index
  1229. ** o dma_len will contain the number of bytes to map
  1230. ** o address contains the virtual address.
  1231. */
  1232. filled = sba_fill_pdir(ioc, sglist, nents);
  1233. #ifdef ASSERT_PDIR_SANITY
  1234. spin_lock_irqsave(&ioc->res_lock, flags);
  1235. if (sba_check_pdir(ioc,"Check after sba_map_sg()"))
  1236. {
  1237. sba_dump_sg(ioc, sglist, nents);
  1238. panic("Check after sba_map_sg()\n");
  1239. }
  1240. spin_unlock_irqrestore(&ioc->res_lock, flags);
  1241. #endif
  1242. ASSERT(coalesced == filled);
  1243. DBG_RUN_SG("%s() DONE %d mappings\n", __FUNCTION__, filled);
  1244. return filled;
  1245. }
  1246. /**
  1247. * sba_unmap_sg - unmap Scatter/Gather list
  1248. * @dev: instance of PCI owned by the driver that's asking.
  1249. * @sglist: array of buffer/length pairs
  1250. * @nents: number of entries in list
  1251. * @dir: R/W or both.
  1252. *
  1253. * See Documentation/DMA-mapping.txt
  1254. */
  1255. void sba_unmap_sg (struct device *dev, struct scatterlist *sglist, int nents, int dir)
  1256. {
  1257. #ifdef ASSERT_PDIR_SANITY
  1258. struct ioc *ioc;
  1259. unsigned long flags;
  1260. #endif
  1261. DBG_RUN_SG("%s() START %d entries, %p,%x\n",
  1262. __FUNCTION__, nents, sba_sg_address(sglist), sglist->length);
  1263. #ifdef ASSERT_PDIR_SANITY
  1264. ioc = GET_IOC(dev);
  1265. ASSERT(ioc);
  1266. spin_lock_irqsave(&ioc->res_lock, flags);
  1267. sba_check_pdir(ioc,"Check before sba_unmap_sg()");
  1268. spin_unlock_irqrestore(&ioc->res_lock, flags);
  1269. #endif
  1270. while (nents && sglist->dma_length) {
  1271. sba_unmap_single(dev, sglist->dma_address, sglist->dma_length, dir);
  1272. sglist++;
  1273. nents--;
  1274. }
  1275. DBG_RUN_SG("%s() DONE (nents %d)\n", __FUNCTION__, nents);
  1276. #ifdef ASSERT_PDIR_SANITY
  1277. spin_lock_irqsave(&ioc->res_lock, flags);
  1278. sba_check_pdir(ioc,"Check after sba_unmap_sg()");
  1279. spin_unlock_irqrestore(&ioc->res_lock, flags);
  1280. #endif
  1281. }
  1282. /**************************************************************
  1283. *
  1284. * Initialization and claim
  1285. *
  1286. ***************************************************************/
  1287. static void __init
  1288. ioc_iova_init(struct ioc *ioc)
  1289. {
  1290. int tcnfg;
  1291. int agp_found = 0;
  1292. struct pci_dev *device = NULL;
  1293. #ifdef FULL_VALID_PDIR
  1294. unsigned long index;
  1295. #endif
  1296. /*
  1297. ** Firmware programs the base and size of a "safe IOVA space"
  1298. ** (one that doesn't overlap memory or LMMIO space) in the
  1299. ** IBASE and IMASK registers.
  1300. */
  1301. ioc->ibase = READ_REG(ioc->ioc_hpa + IOC_IBASE) & ~0x1UL;
  1302. ioc->imask = READ_REG(ioc->ioc_hpa + IOC_IMASK) | 0xFFFFFFFF00000000UL;
  1303. ioc->iov_size = ~ioc->imask + 1;
  1304. DBG_INIT("%s() hpa %p IOV base 0x%lx mask 0x%lx (%dMB)\n",
  1305. __FUNCTION__, ioc->ioc_hpa, ioc->ibase, ioc->imask,
  1306. ioc->iov_size >> 20);
  1307. switch (iovp_size) {
  1308. case 4*1024: tcnfg = 0; break;
  1309. case 8*1024: tcnfg = 1; break;
  1310. case 16*1024: tcnfg = 2; break;
  1311. case 64*1024: tcnfg = 3; break;
  1312. default:
  1313. panic(PFX "Unsupported IOTLB page size %ldK",
  1314. iovp_size >> 10);
  1315. break;
  1316. }
  1317. WRITE_REG(tcnfg, ioc->ioc_hpa + IOC_TCNFG);
  1318. ioc->pdir_size = (ioc->iov_size / iovp_size) * PDIR_ENTRY_SIZE;
  1319. ioc->pdir_base = (void *) __get_free_pages(GFP_KERNEL,
  1320. get_order(ioc->pdir_size));
  1321. if (!ioc->pdir_base)
  1322. panic(PFX "Couldn't allocate I/O Page Table\n");
  1323. memset(ioc->pdir_base, 0, ioc->pdir_size);
  1324. DBG_INIT("%s() IOV page size %ldK pdir %p size %x\n", __FUNCTION__,
  1325. iovp_size >> 10, ioc->pdir_base, ioc->pdir_size);
  1326. ASSERT(ALIGN((unsigned long) ioc->pdir_base, 4*1024) == (unsigned long) ioc->pdir_base);
  1327. WRITE_REG(virt_to_phys(ioc->pdir_base), ioc->ioc_hpa + IOC_PDIR_BASE);
  1328. /*
  1329. ** If an AGP device is present, only use half of the IOV space
  1330. ** for PCI DMA. Unfortunately we can't know ahead of time
  1331. ** whether GART support will actually be used, for now we
  1332. ** can just key on an AGP device found in the system.
  1333. ** We program the next pdir index after we stop w/ a key for
  1334. ** the GART code to handshake on.
  1335. */
  1336. for_each_pci_dev(device)
  1337. agp_found |= pci_find_capability(device, PCI_CAP_ID_AGP);
  1338. if (agp_found && reserve_sba_gart) {
  1339. printk(KERN_INFO PFX "reserving %dMb of IOVA space at 0x%lx for agpgart\n",
  1340. ioc->iov_size/2 >> 20, ioc->ibase + ioc->iov_size/2);
  1341. ioc->pdir_size /= 2;
  1342. ((u64 *)ioc->pdir_base)[PDIR_INDEX(ioc->iov_size/2)] = ZX1_SBA_IOMMU_COOKIE;
  1343. }
  1344. #ifdef FULL_VALID_PDIR
  1345. /*
  1346. ** Check to see if the spill page has been allocated, we don't need more than
  1347. ** one across multiple SBAs.
  1348. */
  1349. if (!prefetch_spill_page) {
  1350. char *spill_poison = "SBAIOMMU POISON";
  1351. int poison_size = 16;
  1352. void *poison_addr, *addr;
  1353. addr = (void *)__get_free_pages(GFP_KERNEL, get_order(iovp_size));
  1354. if (!addr)
  1355. panic(PFX "Couldn't allocate PDIR spill page\n");
  1356. poison_addr = addr;
  1357. for ( ; (u64) poison_addr < addr + iovp_size; poison_addr += poison_size)
  1358. memcpy(poison_addr, spill_poison, poison_size);
  1359. prefetch_spill_page = virt_to_phys(addr);
  1360. DBG_INIT("%s() prefetch spill addr: 0x%lx\n", __FUNCTION__, prefetch_spill_page);
  1361. }
  1362. /*
  1363. ** Set all the PDIR entries valid w/ the spill page as the target
  1364. */
  1365. for (index = 0 ; index < (ioc->pdir_size / PDIR_ENTRY_SIZE) ; index++)
  1366. ((u64 *)ioc->pdir_base)[index] = (0x80000000000000FF | prefetch_spill_page);
  1367. #endif
  1368. /* Clear I/O TLB of any possible entries */
  1369. WRITE_REG(ioc->ibase | (get_iovp_order(ioc->iov_size) + iovp_shift), ioc->ioc_hpa + IOC_PCOM);
  1370. READ_REG(ioc->ioc_hpa + IOC_PCOM);
  1371. /* Enable IOVA translation */
  1372. WRITE_REG(ioc->ibase | 1, ioc->ioc_hpa + IOC_IBASE);
  1373. READ_REG(ioc->ioc_hpa + IOC_IBASE);
  1374. }
  1375. static void __init
  1376. ioc_resource_init(struct ioc *ioc)
  1377. {
  1378. spin_lock_init(&ioc->res_lock);
  1379. #if DELAYED_RESOURCE_CNT > 0
  1380. spin_lock_init(&ioc->saved_lock);
  1381. #endif
  1382. /* resource map size dictated by pdir_size */
  1383. ioc->res_size = ioc->pdir_size / PDIR_ENTRY_SIZE; /* entries */
  1384. ioc->res_size >>= 3; /* convert bit count to byte count */
  1385. DBG_INIT("%s() res_size 0x%x\n", __FUNCTION__, ioc->res_size);
  1386. ioc->res_map = (char *) __get_free_pages(GFP_KERNEL,
  1387. get_order(ioc->res_size));
  1388. if (!ioc->res_map)
  1389. panic(PFX "Couldn't allocate resource map\n");
  1390. memset(ioc->res_map, 0, ioc->res_size);
  1391. /* next available IOVP - circular search */
  1392. ioc->res_hint = (unsigned long *) ioc->res_map;
  1393. #ifdef ASSERT_PDIR_SANITY
  1394. /* Mark first bit busy - ie no IOVA 0 */
  1395. ioc->res_map[0] = 0x1;
  1396. ioc->pdir_base[0] = 0x8000000000000000ULL | ZX1_SBA_IOMMU_COOKIE;
  1397. #endif
  1398. #ifdef FULL_VALID_PDIR
  1399. /* Mark the last resource used so we don't prefetch beyond IOVA space */
  1400. ioc->res_map[ioc->res_size - 1] |= 0x80UL; /* res_map is chars */
  1401. ioc->pdir_base[(ioc->pdir_size / PDIR_ENTRY_SIZE) - 1] = (0x80000000000000FF
  1402. | prefetch_spill_page);
  1403. #endif
  1404. DBG_INIT("%s() res_map %x %p\n", __FUNCTION__,
  1405. ioc->res_size, (void *) ioc->res_map);
  1406. }
  1407. static void __init
  1408. ioc_sac_init(struct ioc *ioc)
  1409. {
  1410. struct pci_dev *sac = NULL;
  1411. struct pci_controller *controller = NULL;
  1412. /*
  1413. * pci_alloc_coherent() must return a DMA address which is
  1414. * SAC (single address cycle) addressable, so allocate a
  1415. * pseudo-device to enforce that.
  1416. */
  1417. sac = kmalloc(sizeof(*sac), GFP_KERNEL);
  1418. if (!sac)
  1419. panic(PFX "Couldn't allocate struct pci_dev");
  1420. memset(sac, 0, sizeof(*sac));
  1421. controller = kmalloc(sizeof(*controller), GFP_KERNEL);
  1422. if (!controller)
  1423. panic(PFX "Couldn't allocate struct pci_controller");
  1424. memset(controller, 0, sizeof(*controller));
  1425. controller->iommu = ioc;
  1426. sac->sysdata = controller;
  1427. sac->dma_mask = 0xFFFFFFFFUL;
  1428. #ifdef CONFIG_PCI
  1429. sac->dev.bus = &pci_bus_type;
  1430. #endif
  1431. ioc->sac_only_dev = sac;
  1432. }
  1433. static void __init
  1434. ioc_zx1_init(struct ioc *ioc)
  1435. {
  1436. unsigned long rope_config;
  1437. unsigned int i;
  1438. if (ioc->rev < 0x20)
  1439. panic(PFX "IOC 2.0 or later required for IOMMU support\n");
  1440. /* 38 bit memory controller + extra bit for range displaced by MMIO */
  1441. ioc->dma_mask = (0x1UL << 39) - 1;
  1442. /*
  1443. ** Clear ROPE(N)_CONFIG AO bit.
  1444. ** Disables "NT Ordering" (~= !"Relaxed Ordering")
  1445. ** Overrides bit 1 in DMA Hint Sets.
  1446. ** Improves netperf UDP_STREAM by ~10% for tg3 on bcm5701.
  1447. */
  1448. for (i=0; i<(8*8); i+=8) {
  1449. rope_config = READ_REG(ioc->ioc_hpa + IOC_ROPE0_CFG + i);
  1450. rope_config &= ~IOC_ROPE_AO;
  1451. WRITE_REG(rope_config, ioc->ioc_hpa + IOC_ROPE0_CFG + i);
  1452. }
  1453. }
  1454. typedef void (initfunc)(struct ioc *);
  1455. struct ioc_iommu {
  1456. u32 func_id;
  1457. char *name;
  1458. initfunc *init;
  1459. };
  1460. static struct ioc_iommu ioc_iommu_info[] __initdata = {
  1461. { ZX1_IOC_ID, "zx1", ioc_zx1_init },
  1462. { ZX2_IOC_ID, "zx2", NULL },
  1463. { SX1000_IOC_ID, "sx1000", NULL },
  1464. };
  1465. static struct ioc * __init
  1466. ioc_init(u64 hpa, void *handle)
  1467. {
  1468. struct ioc *ioc;
  1469. struct ioc_iommu *info;
  1470. ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
  1471. if (!ioc)
  1472. return NULL;
  1473. memset(ioc, 0, sizeof(*ioc));
  1474. ioc->next = ioc_list;
  1475. ioc_list = ioc;
  1476. ioc->handle = handle;
  1477. ioc->ioc_hpa = ioremap(hpa, 0x1000);
  1478. ioc->func_id = READ_REG(ioc->ioc_hpa + IOC_FUNC_ID);
  1479. ioc->rev = READ_REG(ioc->ioc_hpa + IOC_FCLASS) & 0xFFUL;
  1480. ioc->dma_mask = 0xFFFFFFFFFFFFFFFFUL; /* conservative */
  1481. for (info = ioc_iommu_info; info < ioc_iommu_info + ARRAY_SIZE(ioc_iommu_info); info++) {
  1482. if (ioc->func_id == info->func_id) {
  1483. ioc->name = info->name;
  1484. if (info->init)
  1485. (info->init)(ioc);
  1486. }
  1487. }
  1488. iovp_size = (1 << iovp_shift);
  1489. iovp_mask = ~(iovp_size - 1);
  1490. DBG_INIT("%s: PAGE_SIZE %ldK, iovp_size %ldK\n", __FUNCTION__,
  1491. PAGE_SIZE >> 10, iovp_size >> 10);
  1492. if (!ioc->name) {
  1493. ioc->name = kmalloc(24, GFP_KERNEL);
  1494. if (ioc->name)
  1495. sprintf((char *) ioc->name, "Unknown (%04x:%04x)",
  1496. ioc->func_id & 0xFFFF, (ioc->func_id >> 16) & 0xFFFF);
  1497. else
  1498. ioc->name = "Unknown";
  1499. }
  1500. ioc_iova_init(ioc);
  1501. ioc_resource_init(ioc);
  1502. ioc_sac_init(ioc);
  1503. if ((long) ~iovp_mask > (long) ia64_max_iommu_merge_mask)
  1504. ia64_max_iommu_merge_mask = ~iovp_mask;
  1505. printk(KERN_INFO PFX
  1506. "%s %d.%d HPA 0x%lx IOVA space %dMb at 0x%lx\n",
  1507. ioc->name, (ioc->rev >> 4) & 0xF, ioc->rev & 0xF,
  1508. hpa, ioc->iov_size >> 20, ioc->ibase);
  1509. return ioc;
  1510. }
  1511. /**************************************************************************
  1512. **
  1513. ** SBA initialization code (HW and SW)
  1514. **
  1515. ** o identify SBA chip itself
  1516. ** o FIXME: initialize DMA hints for reasonable defaults
  1517. **
  1518. **************************************************************************/
  1519. #ifdef CONFIG_PROC_FS
  1520. static void *
  1521. ioc_start(struct seq_file *s, loff_t *pos)
  1522. {
  1523. struct ioc *ioc;
  1524. loff_t n = *pos;
  1525. for (ioc = ioc_list; ioc; ioc = ioc->next)
  1526. if (!n--)
  1527. return ioc;
  1528. return NULL;
  1529. }
  1530. static void *
  1531. ioc_next(struct seq_file *s, void *v, loff_t *pos)
  1532. {
  1533. struct ioc *ioc = v;
  1534. ++*pos;
  1535. return ioc->next;
  1536. }
  1537. static void
  1538. ioc_stop(struct seq_file *s, void *v)
  1539. {
  1540. }
  1541. static int
  1542. ioc_show(struct seq_file *s, void *v)
  1543. {
  1544. struct ioc *ioc = v;
  1545. unsigned long *res_ptr = (unsigned long *)ioc->res_map;
  1546. int i, used = 0;
  1547. seq_printf(s, "Hewlett Packard %s IOC rev %d.%d\n",
  1548. ioc->name, ((ioc->rev >> 4) & 0xF), (ioc->rev & 0xF));
  1549. #ifdef CONFIG_NUMA
  1550. if (ioc->node != MAX_NUMNODES)
  1551. seq_printf(s, "NUMA node : %d\n", ioc->node);
  1552. #endif
  1553. seq_printf(s, "IOVA size : %ld MB\n", ((ioc->pdir_size >> 3) * iovp_size)/(1024*1024));
  1554. seq_printf(s, "IOVA page size : %ld kb\n", iovp_size/1024);
  1555. for (i = 0; i < (ioc->res_size / sizeof(unsigned long)); ++i, ++res_ptr)
  1556. used += hweight64(*res_ptr);
  1557. seq_printf(s, "PDIR size : %d entries\n", ioc->pdir_size >> 3);
  1558. seq_printf(s, "PDIR used : %d entries\n", used);
  1559. #ifdef PDIR_SEARCH_TIMING
  1560. {
  1561. unsigned long i = 0, avg = 0, min, max;
  1562. min = max = ioc->avg_search[0];
  1563. for (i = 0; i < SBA_SEARCH_SAMPLE; i++) {
  1564. avg += ioc->avg_search[i];
  1565. if (ioc->avg_search[i] > max) max = ioc->avg_search[i];
  1566. if (ioc->avg_search[i] < min) min = ioc->avg_search[i];
  1567. }
  1568. avg /= SBA_SEARCH_SAMPLE;
  1569. seq_printf(s, "Bitmap search : %ld/%ld/%ld (min/avg/max CPU Cycles/IOVA page)\n",
  1570. min, avg, max);
  1571. }
  1572. #endif
  1573. #ifndef ALLOW_IOV_BYPASS
  1574. seq_printf(s, "IOVA bypass disabled\n");
  1575. #endif
  1576. return 0;
  1577. }
  1578. static struct seq_operations ioc_seq_ops = {
  1579. .start = ioc_start,
  1580. .next = ioc_next,
  1581. .stop = ioc_stop,
  1582. .show = ioc_show
  1583. };
  1584. static int
  1585. ioc_open(struct inode *inode, struct file *file)
  1586. {
  1587. return seq_open(file, &ioc_seq_ops);
  1588. }
  1589. static struct file_operations ioc_fops = {
  1590. .open = ioc_open,
  1591. .read = seq_read,
  1592. .llseek = seq_lseek,
  1593. .release = seq_release
  1594. };
  1595. static void __init
  1596. ioc_proc_init(void)
  1597. {
  1598. struct proc_dir_entry *dir, *entry;
  1599. dir = proc_mkdir("bus/mckinley", NULL);
  1600. if (!dir)
  1601. return;
  1602. entry = create_proc_entry(ioc_list->name, 0, dir);
  1603. if (entry)
  1604. entry->proc_fops = &ioc_fops;
  1605. }
  1606. #endif
  1607. static void
  1608. sba_connect_bus(struct pci_bus *bus)
  1609. {
  1610. acpi_handle handle, parent;
  1611. acpi_status status;
  1612. struct ioc *ioc;
  1613. if (!PCI_CONTROLLER(bus))
  1614. panic(PFX "no sysdata on bus %d!\n", bus->number);
  1615. if (PCI_CONTROLLER(bus)->iommu)
  1616. return;
  1617. handle = PCI_CONTROLLER(bus)->acpi_handle;
  1618. if (!handle)
  1619. return;
  1620. /*
  1621. * The IOC scope encloses PCI root bridges in the ACPI
  1622. * namespace, so work our way out until we find an IOC we
  1623. * claimed previously.
  1624. */
  1625. do {
  1626. for (ioc = ioc_list; ioc; ioc = ioc->next)
  1627. if (ioc->handle == handle) {
  1628. PCI_CONTROLLER(bus)->iommu = ioc;
  1629. return;
  1630. }
  1631. status = acpi_get_parent(handle, &parent);
  1632. handle = parent;
  1633. } while (ACPI_SUCCESS(status));
  1634. printk(KERN_WARNING "No IOC for PCI Bus %04x:%02x in ACPI\n", pci_domain_nr(bus), bus->number);
  1635. }
  1636. #ifdef CONFIG_NUMA
  1637. static void __init
  1638. sba_map_ioc_to_node(struct ioc *ioc, acpi_handle handle)
  1639. {
  1640. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  1641. union acpi_object *obj;
  1642. acpi_handle phandle;
  1643. unsigned int node;
  1644. ioc->node = MAX_NUMNODES;
  1645. /*
  1646. * Check for a _PXM on this node first. We don't typically see
  1647. * one here, so we'll end up getting it from the parent.
  1648. */
  1649. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_PXM", NULL, &buffer))) {
  1650. if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
  1651. return;
  1652. /* Reset the acpi buffer */
  1653. buffer.length = ACPI_ALLOCATE_BUFFER;
  1654. buffer.pointer = NULL;
  1655. if (ACPI_FAILURE(acpi_evaluate_object(phandle, "_PXM", NULL,
  1656. &buffer)))
  1657. return;
  1658. }
  1659. if (!buffer.length || !buffer.pointer)
  1660. return;
  1661. obj = buffer.pointer;
  1662. if (obj->type != ACPI_TYPE_INTEGER ||
  1663. obj->integer.value >= MAX_PXM_DOMAINS) {
  1664. acpi_os_free(buffer.pointer);
  1665. return;
  1666. }
  1667. node = pxm_to_nid_map[obj->integer.value];
  1668. acpi_os_free(buffer.pointer);
  1669. if (node >= MAX_NUMNODES || !node_online(node))
  1670. return;
  1671. ioc->node = node;
  1672. return;
  1673. }
  1674. #else
  1675. #define sba_map_ioc_to_node(ioc, handle)
  1676. #endif
  1677. static int __init
  1678. acpi_sba_ioc_add(struct acpi_device *device)
  1679. {
  1680. struct ioc *ioc;
  1681. acpi_status status;
  1682. u64 hpa, length;
  1683. struct acpi_buffer buffer;
  1684. struct acpi_device_info *dev_info;
  1685. status = hp_acpi_csr_space(device->handle, &hpa, &length);
  1686. if (ACPI_FAILURE(status))
  1687. return 1;
  1688. buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
  1689. status = acpi_get_object_info(device->handle, &buffer);
  1690. if (ACPI_FAILURE(status))
  1691. return 1;
  1692. dev_info = buffer.pointer;
  1693. /*
  1694. * For HWP0001, only SBA appears in ACPI namespace. It encloses the PCI
  1695. * root bridges, and its CSR space includes the IOC function.
  1696. */
  1697. if (strncmp("HWP0001", dev_info->hardware_id.value, 7) == 0) {
  1698. hpa += ZX1_IOC_OFFSET;
  1699. /* zx1 based systems default to kernel page size iommu pages */
  1700. if (!iovp_shift)
  1701. iovp_shift = min(PAGE_SHIFT, 16);
  1702. }
  1703. ACPI_MEM_FREE(dev_info);
  1704. /*
  1705. * default anything not caught above or specified on cmdline to 4k
  1706. * iommu page size
  1707. */
  1708. if (!iovp_shift)
  1709. iovp_shift = 12;
  1710. ioc = ioc_init(hpa, device->handle);
  1711. if (!ioc)
  1712. return 1;
  1713. /* setup NUMA node association */
  1714. sba_map_ioc_to_node(ioc, device->handle);
  1715. return 0;
  1716. }
  1717. static struct acpi_driver acpi_sba_ioc_driver = {
  1718. .name = "IOC IOMMU Driver",
  1719. .ids = "HWP0001,HWP0004",
  1720. .ops = {
  1721. .add = acpi_sba_ioc_add,
  1722. },
  1723. };
  1724. static int __init
  1725. sba_init(void)
  1726. {
  1727. acpi_bus_register_driver(&acpi_sba_ioc_driver);
  1728. if (!ioc_list)
  1729. return 0;
  1730. #ifdef CONFIG_PCI
  1731. {
  1732. struct pci_bus *b = NULL;
  1733. while ((b = pci_find_next_bus(b)) != NULL)
  1734. sba_connect_bus(b);
  1735. }
  1736. #endif
  1737. #ifdef CONFIG_PROC_FS
  1738. ioc_proc_init();
  1739. #endif
  1740. return 0;
  1741. }
  1742. subsys_initcall(sba_init); /* must be initialized after ACPI etc., but before any drivers... */
  1743. extern void dig_setup(char**);
  1744. /*
  1745. * MAX_DMA_ADDRESS needs to be setup prior to paging_init to do any good,
  1746. * so we use the platform_setup hook to fix it up.
  1747. */
  1748. void __init
  1749. sba_setup(char **cmdline_p)
  1750. {
  1751. MAX_DMA_ADDRESS = ~0UL;
  1752. dig_setup(cmdline_p);
  1753. }
  1754. static int __init
  1755. nosbagart(char *str)
  1756. {
  1757. reserve_sba_gart = 0;
  1758. return 1;
  1759. }
  1760. int
  1761. sba_dma_supported (struct device *dev, u64 mask)
  1762. {
  1763. /* make sure it's at least 32bit capable */
  1764. return ((mask & 0xFFFFFFFFUL) == 0xFFFFFFFFUL);
  1765. }
  1766. int
  1767. sba_dma_mapping_error (dma_addr_t dma_addr)
  1768. {
  1769. return 0;
  1770. }
  1771. __setup("nosbagart", nosbagart);
  1772. static int __init
  1773. sba_page_override(char *str)
  1774. {
  1775. unsigned long page_size;
  1776. page_size = memparse(str, &str);
  1777. switch (page_size) {
  1778. case 4096:
  1779. case 8192:
  1780. case 16384:
  1781. case 65536:
  1782. iovp_shift = ffs(page_size) - 1;
  1783. break;
  1784. default:
  1785. printk("%s: unknown/unsupported iommu page size %ld\n",
  1786. __FUNCTION__, page_size);
  1787. }
  1788. return 1;
  1789. }
  1790. __setup("sbapagesize=",sba_page_override);
  1791. EXPORT_SYMBOL(sba_dma_mapping_error);
  1792. EXPORT_SYMBOL(sba_map_single);
  1793. EXPORT_SYMBOL(sba_unmap_single);
  1794. EXPORT_SYMBOL(sba_map_sg);
  1795. EXPORT_SYMBOL(sba_unmap_sg);
  1796. EXPORT_SYMBOL(sba_dma_supported);
  1797. EXPORT_SYMBOL(sba_alloc_coherent);
  1798. EXPORT_SYMBOL(sba_free_coherent);