sba_iommu.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:56k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2. **  System Bus Adapter (SBA) I/O MMU manager
  3. **
  4. ** (c) Copyright 2000 Grant Grundler
  5. ** (c) Copyright 2000 Hewlett-Packard Company
  6. **
  7. ** Portions (c) 1999 Dave S. Miller (from sparc64 I/O MMU code)
  8. **
  9. ** This program is free software; you can redistribute it and/or modify
  10. ** it under the terms of the GNU General Public License as published by
  11. **      the Free Software Foundation; either version 2 of the License, or
  12. **      (at your option) any later version.
  13. **
  14. **
  15. ** This module initializes the IOC (I/O Controller) found on B1000/C3000/
  16. ** J5000/J7000/N-class/L-class machines and their successors.
  17. **
  18. ** FIXME: add DMA hint support programming in both sba and lba modules.
  19. */
  20. #include <linux/config.h>
  21. #include <linux/types.h>
  22. #include <linux/kernel.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/slab.h>
  25. #include <linux/init.h>
  26. #include <linux/mm.h>
  27. #include <linux/string.h>
  28. #undef PCI_DEBUG /* for ASSERT */
  29. #include <linux/pci.h>
  30. #undef PCI_DEBUG
  31. #include <asm/byteorder.h>
  32. #include <asm/io.h>
  33. #include <asm/dma.h> /* for DMA_CHUNK_SIZE */
  34. #include <asm/hardware.h> /* for register_parisc_driver() stuff */
  35. #include <asm/gsc.h> /* FIXME: for gsc_read/gsc_write */
  36. #include <linux/proc_fs.h>
  37. #include <asm/runway.h> /* for proc_runway_root */
  38. #include <asm/pdc.h> /* for PDC_MODEL_* */
  39. #define MODULE_NAME "SBA"
  40. /*
  41. ** The number of debug flags is a clue - this code is fragile.
  42. ** Don't even think about messing with it unless you have
  43. ** plenty of 710's to sacrifice to the computer gods. :^)
  44. */
  45. #undef DEBUG_SBA_INIT
  46. #undef DEBUG_SBA_RUN
  47. #undef DEBUG_SBA_RUN_SG
  48. #undef DEBUG_SBA_RESOURCE
  49. #undef ASSERT_PDIR_SANITY
  50. #undef DEBUG_LARGE_SG_ENTRIES
  51. #undef DEBUG_DMB_TRAP
  52. #define SBA_INLINE __inline__
  53. #ifdef DEBUG_SBA_INIT
  54. #define DBG_INIT(x...) printk(x)
  55. #else
  56. #define DBG_INIT(x...)
  57. #endif
  58. #ifdef DEBUG_SBA_RUN
  59. #define DBG_RUN(x...) printk(x)
  60. #else
  61. #define DBG_RUN(x...)
  62. #endif
  63. #ifdef DEBUG_SBA_RUN_SG
  64. #define DBG_RUN_SG(x...) printk(x)
  65. #else
  66. #define DBG_RUN_SG(x...)
  67. #endif
  68. #ifdef DEBUG_SBA_RESOURCE
  69. #define DBG_RES(x...) printk(x)
  70. #else
  71. #define DBG_RES(x...)
  72. #endif
  73. /*
  74. ** The number of pdir entries to "free" before issueing
  75. ** a read to PCOM register to flush out PCOM writes.
  76. ** Interacts with allocation granularity (ie 4 or 8 entries
  77. ** allocated and free'd/purged at a time might make this
  78. ** less interesting).
  79. */
  80. #define DELAYED_RESOURCE_CNT 16
  81. #define DEFAULT_DMA_HINT_REG 0
  82. #define ASTRO_RUNWAY_PORT 0x582
  83. #define ASTRO_ROPES_PORT 0x780
  84. #define IKE_MERCED_PORT 0x803
  85. #define IKE_ROPES_PORT 0x781
  86. #define REO_MERCED_PORT 0x804
  87. #define REO_ROPES_PORT 0x782
  88. #define REOG_MERCED_PORT 0x805
  89. #define REOG_ROPES_PORT 0x783
  90. #define SBA_FUNC_ID 0x0000 /* function id */
  91. #define SBA_FCLASS 0x0008 /* function class, bist, header, rev... */
  92. #define IS_ASTRO(id) 
  93. (((id)->hversion == ASTRO_RUNWAY_PORT) || ((id)->hversion == ASTRO_ROPES_PORT))
  94. #define IS_IKE(id) 
  95. (((id)->hversion == IKE_MERCED_PORT) || ((id)->hversion == IKE_ROPES_PORT))
  96. #define SBA_FUNC_SIZE 4096   /* SBA configuration function reg set */
  97. #define ASTRO_IOC_OFFSET 0x20000
  98. /* Ike's IOC's occupy functions 2 and 3 (not 0 and 1) */
  99. #define IKE_IOC_OFFSET(p) ((p+2)*SBA_FUNC_SIZE)
  100. #define IOC_CTRL          0x8 /* IOC_CTRL offset */
  101. #define IOC_CTRL_TC       (1 << 0) /* TOC Enable */
  102. #define IOC_CTRL_CE       (1 << 1) /* Coalesce Enable */
  103. #define IOC_CTRL_DE       (1 << 2) /* Dillon Enable */
  104. #define IOC_CTRL_RM       (1 << 8) /* Real Mode */
  105. #define IOC_CTRL_NC       (1 << 9) /* Non Coherent Mode */
  106. #define MAX_IOC 2 /* per Ike. Astro only has 1 */
  107. /*
  108. ** Offsets into MBIB (Function 0 on Ike and hopefully Astro)
  109. ** Firmware programs this stuff. Don't touch it.
  110. */
  111. #define IOS_DIST_BASE 0x390
  112. #define IOS_DIST_MASK 0x398
  113. #define IOS_DIST_ROUTE 0x3A0
  114. #define IOS_DIRECT_BASE 0x3C0
  115. #define IOS_DIRECT_MASK 0x3C8
  116. #define IOS_DIRECT_ROUTE 0x3D0
  117. /*
  118. ** Offsets into I/O TLB (Function 2 and 3 on Ike)
  119. */
  120. #define ROPE0_CTL 0x200  /* "regbus pci0" */
  121. #define ROPE1_CTL 0x208
  122. #define ROPE2_CTL 0x210
  123. #define ROPE3_CTL 0x218
  124. #define ROPE4_CTL 0x220
  125. #define ROPE5_CTL 0x228
  126. #define ROPE6_CTL 0x230
  127. #define ROPE7_CTL 0x238
  128. #define HF_ENABLE 0x40
  129. #define IOC_IBASE 0x300 /* IO TLB */
  130. #define IOC_IMASK 0x308
  131. #define IOC_PCOM 0x310
  132. #define IOC_TCNFG 0x318
  133. #define IOC_PDIR_BASE 0x320
  134. #define IOC_IOVA_SPACE_BASE 0 /* IOVA ranges start at 0 */
  135. /*
  136. ** IOC supports 4/8/16/64KB page sizes (see TCNFG register)
  137. ** It's safer (avoid memory corruption) to keep DMA page mappings
  138. ** equivalently sized to VM PAGE_SIZE.
  139. **
  140. ** We really can't avoid generating a new mapping for each
  141. ** page since the Virtual Coherence Index has to be generated
  142. ** and updated for each page.
  143. **
  144. ** IOVP_SIZE could only be greater than PAGE_SIZE if we are
  145. ** confident the drivers really only touch the next physical
  146. ** page iff that driver instance owns it.
  147. */
  148. #define IOVP_SIZE PAGE_SIZE
  149. #define IOVP_SHIFT PAGE_SHIFT
  150. #define IOVP_MASK PAGE_MASK
  151. #define SBA_PERF_CFG 0x708 /* Performance Counter stuff */
  152. #define SBA_PERF_MASK1 0x718
  153. #define SBA_PERF_MASK2 0x730
  154. /*
  155. ** Offsets into PCI Performance Counters (functions 12 and 13)
  156. ** Controlled by PERF registers in function 2 & 3 respectively.
  157. */
  158. #define SBA_PERF_CNT1 0x200
  159. #define SBA_PERF_CNT2 0x208
  160. #define SBA_PERF_CNT3 0x210
  161. struct ioc {
  162. unsigned long ioc_hpa; /* I/O MMU base address */
  163. char *res_map; /* resource map, bit == pdir entry */
  164. u64 *pdir_base; /* physical base address */
  165. unsigned long *res_hint; /* next avail IOVP - circular search */
  166. spinlock_t res_lock;
  167. unsigned long hint_mask_pdir; /* bits used for DMA hints */
  168. unsigned int res_bitshift; /* from the LEFT! */
  169. unsigned int res_size; /* size of resource map in bytes */
  170. unsigned int hint_shift_pdir;
  171. #if DELAYED_RESOURCE_CNT > 0
  172. int saved_cnt;
  173. struct sba_dma_pair {
  174. dma_addr_t iova;
  175. size_t size;
  176. } saved[DELAYED_RESOURCE_CNT];
  177. #endif
  178. #ifdef CONFIG_PROC_FS
  179. #define SBA_SEARCH_SAMPLE 0x100
  180. unsigned long avg_search[SBA_SEARCH_SAMPLE];
  181. unsigned long avg_idx; /* current index into avg_search */
  182. unsigned long used_pages;
  183. unsigned long msingle_calls;
  184. unsigned long msingle_pages;
  185. unsigned long msg_calls;
  186. unsigned long msg_pages;
  187. unsigned long usingle_calls;
  188. unsigned long usingle_pages;
  189. unsigned long usg_calls;
  190. unsigned long usg_pages;
  191. #endif
  192. /* STUFF We don't need in performance path */
  193. unsigned int pdir_size; /* in bytes, determined by IOV Space size */
  194. unsigned long ibase; /* pdir IOV Space base - shared w/lba_pci */
  195. unsigned long imask; /* pdir IOV Space mask - shared w/lba_pci */
  196. };
  197. struct sba_device {
  198. struct sba_device *next; /* list of SBA's in system */
  199. struct parisc_device *dev; /* dev found in bus walk */
  200. struct parisc_device_id *iodc; /* data about dev from firmware */
  201. const char  *name;
  202. unsigned long sba_hpa; /* base address */
  203. spinlock_t sba_lock;
  204. unsigned int flags;  /* state/functionality enabled */
  205. unsigned int hw_rev;  /* HW revision of chip */
  206. unsigned int num_ioc;  /* number of on-board IOC's */
  207. struct ioc ioc[MAX_IOC];
  208. };
  209. static struct sba_device *sba_list;
  210. static unsigned long ioc_needs_fdc = 0;
  211. /* Ratio of Host MEM to IOV Space size */
  212. static unsigned long sba_mem_ratio = 8;
  213. /* global count of IOMMUs in the system */
  214. static unsigned int global_ioc_cnt = 0;
  215. /* PA8700 (Piranha 2.2) bug workaround */
  216. static unsigned long piranha_bad_128k = 0;
  217. /* Looks nice and keeps the compiler happy */
  218. #define SBA_DEV(d) ((struct sba_device *) (d))
  219. #define ROUNDUP(x,y) ((x + ((y)-1)) & ~((y)-1))
  220. /************************************
  221. ** SBA register read and write support
  222. **
  223. ** BE WARNED: register writes are posted.
  224. **  (ie follow writes which must reach HW with a read)
  225. **
  226. ** Superdome (in particular, REO) allows only 64-bit CSR accesses.
  227. */
  228. #define READ_REG32(addr)  le32_to_cpu(__raw_readl(addr))
  229. #define READ_REG64(addr)  le64_to_cpu(__raw_readq(addr))
  230. #define WRITE_REG32(val, addr) __raw_writel(cpu_to_le32(val), addr)
  231. #define WRITE_REG64(val, addr) __raw_writeq(cpu_to_le64(val), addr)
  232. #ifdef __LP64__
  233. #define READ_REG(addr) READ_REG64(addr)
  234. #define WRITE_REG(value, addr) WRITE_REG64(value, addr)
  235. #else
  236. #define READ_REG(addr) READ_REG32(addr)
  237. #define WRITE_REG(value, addr) WRITE_REG32(value, addr)
  238. #endif
  239. #ifdef DEBUG_SBA_INIT
  240. /* NOTE: When __LP64__ isn't defined, READ_REG64() is two 32-bit reads */
  241. /**
  242.  * sba_dump_ranges - debugging only - print ranges assigned to this IOA
  243.  * @hpa: base address of the sba
  244.  *
  245.  * Print the MMIO and IO Port address ranges forwarded by an Astro/Ike/RIO
  246.  * IO Adapter (aka Bus Converter).
  247.  */
  248. static void
  249. sba_dump_ranges(unsigned long hpa)
  250. {
  251. DBG_INIT("SBA at 0x%lxn", hpa);
  252. DBG_INIT("IOS_DIST_BASE   : %Lxn", READ_REG64(hpa+IOS_DIST_BASE));
  253. DBG_INIT("IOS_DIST_MASK   : %Lxn", READ_REG64(hpa+IOS_DIST_MASK));
  254. DBG_INIT("IOS_DIST_ROUTE  : %Lxn", READ_REG64(hpa+IOS_DIST_ROUTE));
  255. DBG_INIT("n");
  256. DBG_INIT("IOS_DIRECT_BASE : %Lxn", READ_REG64(hpa+IOS_DIRECT_BASE));
  257. DBG_INIT("IOS_DIRECT_MASK : %Lxn", READ_REG64(hpa+IOS_DIRECT_MASK));
  258. DBG_INIT("IOS_DIRECT_ROUTE: %Lxn", READ_REG64(hpa+IOS_DIRECT_ROUTE));
  259. }
  260. /**
  261.  * sba_dump_tlb - debugging only - print IOMMU operating parameters
  262.  * @hpa: base address of the IOMMU
  263.  *
  264.  * Print the size/location of the IO MMU PDIR.
  265.  */
  266. static void
  267. sba_dump_tlb(unsigned long hpa)
  268. {
  269. DBG_INIT("IO TLB at 0x%lxn", hpa);
  270. DBG_INIT("IOC_IBASE    : %Lxn", READ_REG64(hpa+IOC_IBASE));
  271. DBG_INIT("IOC_IMASK    : %Lxn", READ_REG64(hpa+IOC_IMASK));
  272. DBG_INIT("IOC_TCNFG    : %Lxn", READ_REG64(hpa+IOC_TCNFG));
  273. DBG_INIT("IOC_PDIR_BASE: %Lxn", READ_REG64(hpa+IOC_PDIR_BASE));
  274. DBG_INIT("n");
  275. }
  276. #endif
  277. #ifdef ASSERT_PDIR_SANITY
  278. /**
  279.  * sba_dump_pdir_entry - debugging only - print one IOMMU PDIR entry
  280.  * @ioc: IO MMU structure which owns the pdir we are interested in.
  281.  * @msg: text to print ont the output line.
  282.  * @pide: pdir index.
  283.  *
  284.  * Print one entry of the IO MMU PDIR in human readable form.
  285.  */
  286. static void
  287. sba_dump_pdir_entry(struct ioc *ioc, char *msg, uint pide)
  288. {
  289. /* start printing from lowest pde in rval */
  290. u64 *ptr = &(ioc->pdir_base[pide & (~0U * BITS_PER_LONG)]);
  291. unsigned long *rptr = (unsigned long *) &(ioc->res_map[(pide >>3) & ~(sizeof(unsigned long) - 1)]);
  292. uint rcnt;
  293. printk(KERN_DEBUG "SBA: %s rp %p bit %d rval 0x%lxn",
  294.  msg,
  295.  rptr, pide & (BITS_PER_LONG - 1), *rptr);
  296. rcnt = 0;
  297. while (rcnt < BITS_PER_LONG) {
  298. printk(KERN_DEBUG "%s %2d %p %016Lxn",
  299. (rcnt == (pide & (BITS_PER_LONG - 1)))
  300. ? "    -->" : "       ",
  301. rcnt, ptr, *ptr );
  302. rcnt++;
  303. ptr++;
  304. }
  305. printk(KERN_DEBUG "%s", msg);
  306. }
  307. /**
  308.  * sba_check_pdir - debugging only - consistency checker
  309.  * @ioc: IO MMU structure which owns the pdir we are interested in.
  310.  * @msg: text to print ont the output line.
  311.  *
  312.  * Verify the resource map and pdir state is consistent
  313.  */
  314. static int
  315. sba_check_pdir(struct ioc *ioc, char *msg)
  316. {
  317. u32 *rptr_end = (u32 *) &(ioc->res_map[ioc->res_size]);
  318. u32 *rptr = (u32 *) ioc->res_map; /* resource map ptr */
  319. u64 *pptr = ioc->pdir_base; /* pdir ptr */
  320. uint pide = 0;
  321. while (rptr < rptr_end) {
  322. u32 rval = *rptr;
  323. int rcnt = 32; /* number of bits we might check */
  324. while (rcnt) {
  325. /* Get last byte and highest bit from that */
  326. u32 pde = ((u32) (((char *)pptr)[7])) << 24;
  327. if ((rval ^ pde) & 0x80000000)
  328. {
  329. /*
  330. ** BUMMER!  -- res_map != pdir --
  331. ** Dump rval and matching pdir entries
  332. */
  333. sba_dump_pdir_entry(ioc, msg, pide);
  334. return(1);
  335. }
  336. rcnt--;
  337. rval <<= 1; /* try the next bit */
  338. pptr++;
  339. pide++;
  340. }
  341. rptr++; /* look at next word of res_map */
  342. }
  343. /* It'd be nice if we always got here :^) */
  344. return 0;
  345. }
  346. /**
  347.  * sba_dump_sg - debugging only - print Scatter-Gather list
  348.  * @ioc: IO MMU structure which owns the pdir we are interested in.
  349.  * @startsg: head of the SG list
  350.  * @nents: number of entries in SG list
  351.  *
  352.  * print the SG list so we can verify it's correct by hand.
  353.  */
  354. static void
  355. sba_dump_sg( struct ioc *ioc, struct scatterlist *startsg, int nents)
  356. {
  357. while (nents-- > 0) {
  358. printk(KERN_DEBUG " %d : %08lx/%05x %p/%05xn",
  359. nents,
  360. (unsigned long) sg_dma_address(startsg),
  361. sg_dma_len(startsg),
  362. sg_virt_addr(startsg), startsg->length);
  363. startsg++;
  364. }
  365. }
  366. #endif /* ASSERT_PDIR_SANITY */
  367. /**************************************************************
  368. *
  369. *   I/O Pdir Resource Management
  370. *
  371. *   Bits set in the resource map are in use.
  372. *   Each bit can represent a number of pages.
  373. *   LSbs represent lower addresses (IOVA's).
  374. *
  375. ***************************************************************/
  376. #define PAGES_PER_RANGE 1 /* could increase this to 4 or 8 if needed */
  377. /* Convert from IOVP to IOVA and vice versa. */
  378. #define SBA_IOVA(ioc,iovp,offset,hint_reg) ((iovp) | (offset) | ((hint_reg)<<(ioc->hint_shift_pdir)))
  379. #define SBA_IOVP(ioc,iova) ((iova) & ioc->hint_mask_pdir)
  380. /* FIXME : review these macros to verify correctness and usage */
  381. #define PDIR_INDEX(iovp)   ((iovp)>>IOVP_SHIFT)
  382. #define MKIOVP(dma_hint,pide)  (dma_addr_t)((long)(dma_hint) | ((long)(pide) << IOVP_SHIFT))
  383. #define MKIOVA(iovp,offset) (dma_addr_t)((long)iovp | (long)offset)
  384. #define RESMAP_MASK(n)    (~0UL << (BITS_PER_LONG - (n)))
  385. #define RESMAP_IDX_MASK   (sizeof(unsigned long) - 1)
  386. /**
  387.  * sba_search_bitmap - find free space in IO PDIR resource bitmap
  388.  * @ioc: IO MMU structure which owns the pdir we are interested in.
  389.  * @bits_wanted: number of entries we need.
  390.  *
  391.  * Find consecutive free bits in resource bitmap.
  392.  * Each bit represents one entry in the IO Pdir.
  393.  * Cool perf optimization: search for log2(size) bits at a time.
  394.  */
  395. static SBA_INLINE unsigned long
  396. sba_search_bitmap(struct ioc *ioc, unsigned long bits_wanted)
  397. {
  398. unsigned long *res_ptr = ioc->res_hint;
  399. unsigned long *res_end = (unsigned long *) &(ioc->res_map[ioc->res_size]);
  400. unsigned long pide = ~0UL;
  401. ASSERT(((unsigned long) ioc->res_hint & (sizeof(unsigned long) - 1UL)) == 0);
  402. ASSERT(res_ptr < res_end);
  403. if (bits_wanted > (BITS_PER_LONG/2)) {
  404. /* Search word at a time - no mask needed */
  405. for(; res_ptr < res_end; ++res_ptr) {
  406. if (*res_ptr == 0) {
  407. *res_ptr = RESMAP_MASK(bits_wanted);
  408. pide = ((unsigned long)res_ptr - (unsigned long)ioc->res_map);
  409. pide <<= 3; /* convert to bit address */
  410. break;
  411. }
  412. }
  413. /* point to the next word on next pass */
  414. res_ptr++;
  415. ioc->res_bitshift = 0;
  416. } else {
  417. /*
  418. ** Search the resource bit map on well-aligned values.
  419. ** "o" is the alignment.
  420. ** We need the alignment to invalidate I/O TLB using
  421. ** SBA HW features in the unmap path.
  422. */
  423. unsigned long o = 1 << get_order(bits_wanted << PAGE_SHIFT);
  424. uint bitshiftcnt = ROUNDUP(ioc->res_bitshift, o);
  425. unsigned long mask;
  426. if (bitshiftcnt >= BITS_PER_LONG) {
  427. bitshiftcnt = 0;
  428. res_ptr++;
  429. }
  430. mask = RESMAP_MASK(bits_wanted) >> bitshiftcnt;
  431. DBG_RES("%s() o %ld %p", __FUNCTION__, o, res_ptr);
  432. while(res_ptr < res_end)
  433. DBG_RES("    %p %lx %lxn", res_ptr, mask, *res_ptr);
  434. ASSERT(0 != mask);
  435. if(0 == ((*res_ptr) & mask)) {
  436. *res_ptr |= mask;     /* mark resources busy! */
  437. pide = ((unsigned long)res_ptr - (unsigned long)ioc->res_map);
  438. pide <<= 3; /* convert to bit address */
  439. pide += bitshiftcnt;
  440. break;
  441. }
  442. mask >>= o;
  443. bitshiftcnt += o;
  444. if (0 == mask) {
  445. mask = RESMAP_MASK(bits_wanted);
  446. bitshiftcnt=0;
  447. res_ptr++;
  448. }
  449. }
  450. /* look in the same word on the next pass */
  451. ioc->res_bitshift = bitshiftcnt + bits_wanted;
  452. }
  453. /* wrapped ? */
  454. if (res_end <= res_ptr) {
  455. ioc->res_hint = (unsigned long *) ioc->res_map;
  456. ioc->res_bitshift = 0;
  457. } else {
  458. ioc->res_hint = res_ptr;
  459. }
  460. return (pide);
  461. }
  462. /**
  463.  * sba_alloc_range - find free bits and mark them in IO PDIR resource bitmap
  464.  * @ioc: IO MMU structure which owns the pdir we are interested in.
  465.  * @size: number of bytes to create a mapping for
  466.  *
  467.  * Given a size, find consecutive unmarked and then mark those bits in the
  468.  * resource bit map.
  469.  */
  470. static int
  471. sba_alloc_range(struct ioc *ioc, size_t size)
  472. {
  473. unsigned int pages_needed = size >> IOVP_SHIFT;
  474. #ifdef CONFIG_PROC_FS
  475. unsigned long cr_start = mfctl(16);
  476. #endif
  477. unsigned long pide;
  478. ASSERT(pages_needed);
  479. ASSERT((pages_needed * IOVP_SIZE) <= DMA_CHUNK_SIZE);
  480. ASSERT(pages_needed <= BITS_PER_LONG);
  481. ASSERT(0 == (size & ~IOVP_MASK));
  482. /*
  483. ** "seek and ye shall find"...praying never hurts either...
  484. ** ggg sacrifices another 710 to the computer gods.
  485. */
  486. pide = sba_search_bitmap(ioc, pages_needed);
  487. if (pide >= (ioc->res_size << 3)) {
  488. pide = sba_search_bitmap(ioc, pages_needed);
  489. if (pide >= (ioc->res_size << 3))
  490. panic(__FILE__ ": I/O MMU @ %lx is out of mapping resourcesn", ioc->ioc_hpa);
  491. }
  492. #ifdef ASSERT_PDIR_SANITY
  493. /* verify the first enable bit is clear */
  494. if(0x00 != ((u8 *) ioc->pdir_base)[pide*sizeof(u64) + 7]) {
  495. sba_dump_pdir_entry(ioc, "sba_search_bitmap() botched it?", pide);
  496. }
  497. #endif
  498. DBG_RES("%s(%x) %d -> %lx hint %x/%xn",
  499. __FUNCTION__, size, pages_needed, pide,
  500. (uint) ((unsigned long) ioc->res_hint - (unsigned long) ioc->res_map),
  501. ioc->res_bitshift );
  502. #ifdef CONFIG_PROC_FS
  503. {
  504. unsigned long cr_end = mfctl(16);
  505. unsigned long tmp = cr_end - cr_start;
  506. /* check for roll over */
  507. cr_start = (cr_end < cr_start) ?  -(tmp) : (tmp);
  508. }
  509. ioc->avg_search[ioc->avg_idx++] = cr_start;
  510. ioc->avg_idx &= SBA_SEARCH_SAMPLE - 1;
  511. ioc->used_pages += pages_needed;
  512. #endif
  513. return (pide);
  514. }
  515. /**
  516.  * sba_free_range - unmark bits in IO PDIR resource bitmap
  517.  * @ioc: IO MMU structure which owns the pdir we are interested in.
  518.  * @iova: IO virtual address which was previously allocated.
  519.  * @size: number of bytes to create a mapping for
  520.  *
  521.  * clear bits in the ioc's resource map
  522.  */
  523. static SBA_INLINE void
  524. sba_free_range(struct ioc *ioc, dma_addr_t iova, size_t size)
  525. {
  526. unsigned long iovp = SBA_IOVP(ioc, iova);
  527. unsigned int pide = PDIR_INDEX(iovp);
  528. unsigned int ridx = pide >> 3; /* convert bit to byte address */
  529. unsigned long *res_ptr = (unsigned long *) &((ioc)->res_map[ridx & ~RESMAP_IDX_MASK]);
  530. int bits_not_wanted = size >> IOVP_SHIFT;
  531. /* 3-bits "bit" address plus 2 (or 3) bits for "byte" == bit in word */
  532. unsigned long m = RESMAP_MASK(bits_not_wanted) >> (pide & (BITS_PER_LONG - 1));
  533. DBG_RES("%s( ,%x,%x) %x/%lx %x %p %lxn",
  534. __FUNCTION__, (uint) iova, size,
  535. bits_not_wanted, m, pide, res_ptr, *res_ptr);
  536. #ifdef CONFIG_PROC_FS
  537. ioc->used_pages -= bits_not_wanted;
  538. #endif
  539. ASSERT(m != 0);
  540. ASSERT(bits_not_wanted);
  541. ASSERT((bits_not_wanted * IOVP_SIZE) <= DMA_CHUNK_SIZE);
  542. ASSERT(bits_not_wanted <= BITS_PER_LONG);
  543. ASSERT((*res_ptr & m) == m); /* verify same bits are set */
  544. *res_ptr &= ~m;
  545. }
  546. /**************************************************************
  547. *
  548. *   "Dynamic DMA Mapping" support (aka "Coherent I/O")
  549. *
  550. ***************************************************************/
  551. #define SBA_DMA_HINT(ioc, val) ((val) << (ioc)->hint_shift_pdir)
  552. typedef unsigned long space_t;
  553. #define KERNEL_SPACE 0
  554. /**
  555.  * sba_io_pdir_entry - fill in one IO PDIR entry
  556.  * @pdir_ptr:  pointer to IO PDIR entry
  557.  * @sid: process Space ID
  558.  * @vba: Virtual CPU address of buffer to map
  559.  *
  560.  * SBA Mapping Routine
  561.  *
  562.  * Given a virtual address (vba, arg2) and space id, (sid, arg1)
  563.  * sba_io_pdir_entry() loads the I/O PDIR entry pointed to by
  564.  * pdir_ptr (arg0). Each IO Pdir entry consists of 8 bytes as
  565.  * shown below (MSB == bit 0):
  566.  *
  567.  *  0                    19                                 51   55       63
  568.  * +-+---------------------+----------------------------------+----+--------+
  569.  * |V|        U            |            PPN[43:12]            | U  |   VI   |
  570.  * +-+---------------------+----------------------------------+----+--------+
  571.  *
  572.  *  V  == Valid Bit
  573.  *  U  == Unused
  574.  * PPN == Physical Page Number
  575.  * VI  == Virtual Index (aka Coherent Index)
  576.  *
  577.  * The physical address fields are filled with the results of the LPA
  578.  * instruction.  The virtual index field is filled with the results of
  579.  * of the LCI (Load Coherence Index) instruction.  The 8 bits used for
  580.  * the virtual index are bits 12:19 of the value returned by LCI.
  581.  *
  582.  * We need to pre-swap the bytes since PCX-W is Big Endian.
  583.  */
  584. void SBA_INLINE
  585. sba_io_pdir_entry(u64 *pdir_ptr, space_t sid, unsigned long vba)
  586. {
  587. u64 pa; /* physical address */
  588. register unsigned ci; /* coherent index */
  589. /* We currently only support kernel addresses.
  590.  * fdc instr below will need to reload sr1 with KERNEL_SPACE
  591.  * once we try to support direct DMA to user space.
  592.  */
  593. ASSERT(sid == KERNEL_SPACE);
  594. pa = virt_to_phys(vba);
  595. pa &= ~4095ULL; /* clear out offset bits */
  596. mtsp(sid,1);
  597. asm("lci 0(%%sr1, %1), %0" : "=r" (ci) : "r" (vba));
  598. pa |= (ci >> 12) & 0xff;  /* move CI (8 bits) into lowest byte */
  599. pa |= 0x8000000000000000ULL; /* set "valid" bit */
  600. *pdir_ptr = cpu_to_le64(pa); /* swap and store into I/O Pdir */
  601. /*
  602.  * If the PDC_MODEL capabilities has Non-coherent IO-PDIR bit set
  603.  * (bit #61, big endian), we have to flush and sync every time
  604.  * IO-PDIR is changed in Ike/Astro.
  605.  */
  606. if (ioc_needs_fdc) {
  607. asm volatile("fdc 0(%%sr1,%0)ntsync" : : "r" (pdir_ptr));
  608. }
  609. }
  610. /**
  611.  * sba_mark_invalid - invalidate one or more IO PDIR entries
  612.  * @ioc: IO MMU structure which owns the pdir we are interested in.
  613.  * @iova:  IO Virtual Address mapped earlier
  614.  * @byte_cnt:  number of bytes this mapping covers.
  615.  *
  616.  * Marking the IO PDIR entry(ies) as Invalid and invalidate
  617.  * corresponding IO TLB entry. The Ike PCOM (Purge Command Register)
  618.  * is to purge stale entries in the IO TLB when unmapping entries.
  619.  *
  620.  * The PCOM register supports purging of multiple pages, with a minium
  621.  * of 1 page and a maximum of 2GB. Hardware requires the address be
  622.  * aligned to the size of the range being purged. The size of the range
  623.  * must be a power of 2. The "Cool perf optimization" in the
  624.  * allocation routine helps keep that true.
  625.  */
  626. static SBA_INLINE void
  627. sba_mark_invalid(struct ioc *ioc, dma_addr_t iova, size_t byte_cnt)
  628. {
  629. u32 iovp = (u32) SBA_IOVP(ioc,iova);
  630. /* Even though this is a big-endian machine, the entries
  631. ** in the iopdir are little endian. That's why we clear the byte
  632. ** at +7 instead of at +0.
  633. */
  634. int off = PDIR_INDEX(iovp)*sizeof(u64)+7;
  635. /* Must be non-zero and rounded up */
  636. ASSERT(byte_cnt > 0);
  637. ASSERT(0 == (byte_cnt & ~IOVP_MASK));
  638. #ifdef ASSERT_PDIR_SANITY
  639. /* Assert first pdir entry is set */
  640. if (0x80 != (((u8 *) ioc->pdir_base)[off])) {
  641. sba_dump_pdir_entry(ioc,"sba_mark_invalid()", PDIR_INDEX(iovp));
  642. }
  643. #endif
  644. if (byte_cnt <= IOVP_SIZE)
  645. {
  646. ASSERT( off < ioc->pdir_size);
  647. iovp |= IOVP_SHIFT;     /* set "size" field for PCOM */
  648. /*
  649. ** clear I/O PDIR entry "valid" bit
  650. ** Do NOT clear the rest - save it for debugging.
  651. ** We should only clear bits that have previously
  652. ** been enabled.
  653. */
  654. ((u8 *)(ioc->pdir_base))[off] = 0;
  655. } else {
  656. u32 t = get_order(byte_cnt) + PAGE_SHIFT;
  657. iovp |= t;
  658. ASSERT(t <= 31);   /* 2GB! Max value of "size" field */
  659. do {
  660. /* verify this pdir entry is enabled */
  661. ASSERT(0x80 == (((u8 *) ioc->pdir_base)[off] & 0x80));
  662. /* clear I/O Pdir entry "valid" bit first */
  663. ((u8 *)(ioc->pdir_base))[off] = 0;
  664. off += sizeof(u64);
  665. byte_cnt -= IOVP_SIZE;
  666. } while (byte_cnt > 0);
  667. }
  668. WRITE_REG(iovp, ioc->ioc_hpa+IOC_PCOM);
  669. }
  670. /**
  671.  * sba_dma_supported - PCI driver can query DMA support
  672.  * @dev: instance of PCI owned by the driver that's asking
  673.  * @mask:  number of address bits this PCI device can handle
  674.  *
  675.  * See Documentation/DMA-mapping.txt
  676.  */
  677. static int
  678. sba_dma_supported( struct pci_dev *dev, u64 mask)
  679. {
  680. if (dev == NULL) {
  681. printk(KERN_ERR MODULE_NAME ": EISA/ISA/et al not supportedn");
  682. BUG();
  683. return(0);
  684. }
  685. dev->dma_mask = mask; /* save it */
  686. /* only support 32-bit PCI devices - no DAC support (yet) */
  687. return((int) (mask == 0xffffffff));
  688. }
  689. /**
  690.  * sba_map_single - map one buffer and return IOVA for DMA
  691.  * @dev: instance of PCI owned by the driver that's asking.
  692.  * @addr:  driver buffer to map.
  693.  * @size:  number of bytes to map in driver buffer.
  694.  * @direction:  R/W or both.
  695.  *
  696.  * See Documentation/DMA-mapping.txt
  697.  */
  698. static dma_addr_t
  699. sba_map_single(struct pci_dev *dev, void *addr, size_t size, int direction)
  700. {
  701. struct ioc *ioc;
  702. unsigned long flags; 
  703. dma_addr_t iovp;
  704. dma_addr_t offset;
  705. u64 *pdir_start;
  706. int pide;
  707. ASSERT(size > 0);
  708. ASSERT(size <= DMA_CHUNK_SIZE);
  709. ASSERT(dev->sysdata);
  710. ioc = GET_IOC(dev);
  711. ASSERT(ioc);
  712. /* save offset bits */
  713. offset = ((dma_addr_t) (long) addr) & ~IOVP_MASK;
  714. /* round up to nearest IOVP_SIZE */
  715. size = (size + offset + ~IOVP_MASK) & IOVP_MASK;
  716. spin_lock_irqsave(&ioc->res_lock, flags);
  717. #ifdef ASSERT_PDIR_SANITY
  718. sba_check_pdir(ioc,"Check before sba_map_single()");
  719. #endif
  720. #ifdef CONFIG_PROC_FS
  721. ioc->msingle_calls++;
  722. ioc->msingle_pages += size >> IOVP_SHIFT;
  723. #endif
  724. pide = sba_alloc_range(ioc, size);
  725. iovp = (dma_addr_t) pide << IOVP_SHIFT;
  726. DBG_RUN("%s() 0x%p -> 0x%lx",
  727. __FUNCTION__, addr, (long) iovp | offset);
  728. pdir_start = &(ioc->pdir_base[pide]);
  729. while (size > 0) {
  730. ASSERT(((u8 *)pdir_start)[7] == 0); /* verify availability */
  731. sba_io_pdir_entry(pdir_start, KERNEL_SPACE, (unsigned long) addr);
  732. DBG_RUN(" pdir 0x%p %02x%02x%02x%02x%02x%02x%02x%02xn",
  733. pdir_start,
  734. (u8) (((u8 *) pdir_start)[7]),
  735. (u8) (((u8 *) pdir_start)[6]),
  736. (u8) (((u8 *) pdir_start)[5]),
  737. (u8) (((u8 *) pdir_start)[4]),
  738. (u8) (((u8 *) pdir_start)[3]),
  739. (u8) (((u8 *) pdir_start)[2]),
  740. (u8) (((u8 *) pdir_start)[1]),
  741. (u8) (((u8 *) pdir_start)[0])
  742. );
  743. addr += IOVP_SIZE;
  744. size -= IOVP_SIZE;
  745. pdir_start++;
  746. }
  747. /* form complete address */
  748. #ifdef ASSERT_PDIR_SANITY
  749. sba_check_pdir(ioc,"Check after sba_map_single()");
  750. #endif
  751. spin_unlock_irqrestore(&ioc->res_lock, flags);
  752. return SBA_IOVA(ioc, iovp, offset, DEFAULT_DMA_HINT_REG);
  753. }
  754. /**
  755.  * sba_unmap_single - unmap one IOVA and free resources
  756.  * @dev: instance of PCI owned by the driver that's asking.
  757.  * @iova:  IOVA of driver buffer previously mapped.
  758.  * @size:  number of bytes mapped in driver buffer.
  759.  * @direction:  R/W or both.
  760.  *
  761.  * See Documentation/DMA-mapping.txt
  762.  */
  763. static void
  764. sba_unmap_single(struct pci_dev *dev, dma_addr_t iova, size_t size, int direction)
  765. {
  766. struct ioc *ioc;
  767. #if DELAYED_RESOURCE_CNT > 0
  768. struct sba_dma_pair *d;
  769. #endif
  770. unsigned long flags; 
  771. dma_addr_t offset;
  772. ASSERT(dev->sysdata);
  773. ioc = GET_IOC(dev);
  774. ASSERT(ioc);
  775. offset = iova & ~IOVP_MASK;
  776. DBG_RUN("%s() iovp 0x%lx/%xn",
  777. __FUNCTION__, (long) iova, size);
  778. iova ^= offset;        /* clear offset bits */
  779. size += offset;
  780. size = ROUNDUP(size, IOVP_SIZE);
  781. spin_lock_irqsave(&ioc->res_lock, flags);
  782. #ifdef CONFIG_PROC_FS
  783. ioc->usingle_calls++;
  784. ioc->usingle_pages += size >> IOVP_SHIFT;
  785. #endif
  786. #if DELAYED_RESOURCE_CNT > 0
  787. d = &(ioc->saved[ioc->saved_cnt]);
  788. d->iova = iova;
  789. d->size = size;
  790. if (++(ioc->saved_cnt) >= DELAYED_RESOURCE_CNT) {
  791. int cnt = ioc->saved_cnt;
  792. while (cnt--) {
  793. sba_mark_invalid(ioc, d->iova, d->size);
  794. sba_free_range(ioc, d->iova, d->size);
  795. d--;
  796. }
  797. ioc->saved_cnt = 0;
  798. READ_REG(ioc->ioc_hpa+IOC_PCOM); /* flush purges */
  799. }
  800. #else /* DELAYED_RESOURCE_CNT == 0 */
  801. sba_mark_invalid(ioc, iova, size);
  802. sba_free_range(ioc, iova, size);
  803. READ_REG(ioc->ioc_hpa+IOC_PCOM); /* flush purges */
  804. #endif /* DELAYED_RESOURCE_CNT == 0 */
  805. spin_unlock_irqrestore(&ioc->res_lock, flags);
  806. /* XXX REVISIT for 2.5 Linux - need syncdma for zero-copy support.
  807. ** For Astro based systems this isn't a big deal WRT performance.
  808. ** As long as 2.4 kernels copyin/copyout data from/to userspace,
  809. ** we don't need the syncdma. The issue here is I/O MMU cachelines
  810. ** are *not* coherent in all cases.  May be hwrev dependent.
  811. ** Need to investigate more.
  812. asm volatile("syncdma");
  813. */
  814. }
  815. /**
  816.  * sba_alloc_consistent - allocate/map shared mem for DMA
  817.  * @hwdev: instance of PCI owned by the driver that's asking.
  818.  * @size:  number of bytes mapped in driver buffer.
  819.  * @dma_handle:  IOVA of new buffer.
  820.  *
  821.  * See Documentation/DMA-mapping.txt
  822.  */
  823. static void *
  824. sba_alloc_consistent(struct pci_dev *hwdev, size_t size, dma_addr_t *dma_handle)
  825. {
  826. void *ret;
  827. if (!hwdev) {
  828. /* only support PCI */
  829. *dma_handle = 0;
  830. return 0;
  831. }
  832.         ret = (void *) __get_free_pages(GFP_ATOMIC, get_order(size));
  833. if (ret) {
  834. memset(ret, 0, size);
  835. *dma_handle = sba_map_single(hwdev, ret, size, 0);
  836. }
  837. return ret;
  838. }
  839. /**
  840.  * sba_free_consistent - free/unmap shared mem for DMA
  841.  * @hwdev: instance of PCI owned by the driver that's asking.
  842.  * @size:  number of bytes mapped in driver buffer.
  843.  * @vaddr:  virtual address IOVA of "consistent" buffer.
  844.  * @dma_handler:  IO virtual address of "consistent" buffer.
  845.  *
  846.  * See Documentation/DMA-mapping.txt
  847.  */
  848. static void
  849. sba_free_consistent(struct pci_dev *hwdev, size_t size, void *vaddr, dma_addr_t dma_handle)
  850. {
  851. sba_unmap_single(hwdev, dma_handle, size, 0);
  852. free_pages((unsigned long) vaddr, get_order(size));
  853. }
  854. /*
  855. ** Since 0 is a valid pdir_base index value, can't use that
  856. ** to determine if a value is valid or not. Use a flag to indicate
  857. ** the SG list entry contains a valid pdir index.
  858. */
  859. #define PIDE_FLAG 0x80000000UL
  860. #ifdef DEBUG_LARGE_SG_ENTRIES
  861. int dump_run_sg = 0;
  862. #endif
  863. /**
  864.  * sba_fill_pdir - write allocated SG entries into IO PDIR
  865.  * @ioc: IO MMU structure which owns the pdir we are interested in.
  866.  * @startsg:  list of IOVA/size pairs
  867.  * @nents: number of entries in startsg list
  868.  *
  869.  * Take preprocessed SG list and write corresponding entries
  870.  * in the IO PDIR.
  871.  */
  872. static SBA_INLINE int
  873. sba_fill_pdir(
  874. struct ioc *ioc,
  875. struct scatterlist *startsg,
  876. int nents)
  877. {
  878. struct scatterlist *dma_sg = startsg; /* pointer to current DMA */
  879. int n_mappings = 0;
  880. u64 *pdirp = 0;
  881. unsigned long dma_offset = 0;
  882. dma_sg--;
  883. while (nents-- > 0) {
  884. int     cnt = sg_dma_len(startsg);
  885. sg_dma_len(startsg) = 0;
  886. #ifdef DEBUG_LARGE_SG_ENTRIES
  887. if (dump_run_sg)
  888. printk(KERN_DEBUG " %2d : %08lx/%05x %p/%05xn",
  889. nents,
  890. (unsigned long) sg_dma_address(startsg), cnt,
  891. sg_virt_address(startsg), startsg->length
  892. );
  893. #else
  894. DBG_RUN_SG(" %d : %08lx/%05x %p/%05xn",
  895. nents,
  896. (unsigned long) sg_dma_address(startsg), cnt,
  897. sg_virt_addr(startsg), startsg->length
  898. );
  899. #endif
  900. /*
  901. ** Look for the start of a new DMA stream
  902. */
  903. if (sg_dma_address(startsg) & PIDE_FLAG) {
  904. u32 pide = sg_dma_address(startsg) & ~PIDE_FLAG;
  905. dma_offset = (unsigned long) pide & ~IOVP_MASK;
  906. sg_dma_address(startsg) = 0;
  907. dma_sg++;
  908. sg_dma_address(dma_sg) = pide;
  909. pdirp = &(ioc->pdir_base[pide >> IOVP_SHIFT]);
  910. n_mappings++;
  911. }
  912. /*
  913. ** Look for a VCONTIG chunk
  914. */
  915. if (cnt) {
  916. unsigned long vaddr = (unsigned long) sg_virt_addr(startsg);
  917. ASSERT(pdirp);
  918. /* Since multiple Vcontig blocks could make up
  919. ** one DMA stream, *add* cnt to dma_len.
  920. */
  921. sg_dma_len(dma_sg) += cnt;
  922. cnt += dma_offset;
  923. dma_offset=0; /* only want offset on first chunk */
  924. cnt = ROUNDUP(cnt, IOVP_SIZE);
  925. #ifdef CONFIG_PROC_FS
  926. ioc->msg_pages += cnt >> IOVP_SHIFT;
  927. #endif
  928. do {
  929. sba_io_pdir_entry(pdirp, KERNEL_SPACE, vaddr);
  930. vaddr += IOVP_SIZE;
  931. cnt -= IOVP_SIZE;
  932. pdirp++;
  933. } while (cnt > 0);
  934. }
  935. startsg++;
  936. }
  937. #ifdef DEBUG_LARGE_SG_ENTRIES
  938. dump_run_sg = 0;
  939. #endif
  940. return(n_mappings);
  941. }
  942. /*
  943. ** Two address ranges are DMA contiguous *iff* "end of prev" and
  944. ** "start of next" are both on a page boundry.
  945. **
  946. ** (shift left is a quick trick to mask off upper bits)
  947. */
  948. #define DMA_CONTIG(__X, __Y) 
  949. (((((unsigned long) __X) | ((unsigned long) __Y)) << (BITS_PER_LONG - PAGE_SHIFT)) == 0UL)
  950. /**
  951.  * sba_coalesce_chunks - preprocess the SG list
  952.  * @ioc: IO MMU structure which owns the pdir we are interested in.
  953.  * @startsg:  list of IOVA/size pairs
  954.  * @nents: number of entries in startsg list
  955.  *
  956.  * First pass is to walk the SG list and determine where the breaks are
  957.  * in the DMA stream. Allocates PDIR entries but does not fill them.
  958.  * Returns the number of DMA chunks.
  959.  *
  960.  * Doing the fill seperate from the coalescing/allocation keeps the
  961.  * code simpler. Future enhancement could make one pass through
  962.  * the sglist do both.
  963.  */
  964. static SBA_INLINE int
  965. sba_coalesce_chunks( struct ioc *ioc,
  966. struct scatterlist *startsg,
  967. int nents)
  968. {
  969. struct scatterlist *vcontig_sg;    /* VCONTIG chunk head */
  970. unsigned long vcontig_len;         /* len of VCONTIG chunk */
  971. unsigned long vcontig_end;
  972. struct scatterlist *dma_sg;        /* next DMA stream head */
  973. unsigned long dma_offset, dma_len; /* start/len of DMA stream */
  974. int n_mappings = 0;
  975. while (nents > 0) {
  976. unsigned long vaddr = (unsigned long) sg_virt_addr(startsg); 
  977. /*
  978. ** Prepare for first/next DMA stream
  979. */
  980. dma_sg = vcontig_sg = startsg;
  981. dma_len = vcontig_len = vcontig_end = startsg->length;
  982. vcontig_end +=  vaddr;
  983. dma_offset = vaddr & ~IOVP_MASK;
  984. /* PARANOID: clear entries */
  985. sg_dma_address(startsg) = 0;
  986. sg_dma_len(startsg) = 0;
  987. /*
  988. ** This loop terminates one iteration "early" since
  989. ** it's always looking one "ahead".
  990. */
  991. while (--nents > 0) {
  992. unsigned long vaddr; /* tmp */
  993. startsg++;
  994. /* PARANOID: clear entries */
  995. sg_dma_address(startsg) = 0;
  996. sg_dma_len(startsg) = 0;
  997. /* catch brokenness in SCSI layer */
  998. ASSERT(startsg->length <= DMA_CHUNK_SIZE);
  999. /*
  1000. ** First make sure current dma stream won't
  1001. ** exceed DMA_CHUNK_SIZE if we coalesce the
  1002. ** next entry.
  1003. */
  1004. if (((dma_len + dma_offset + startsg->length + ~IOVP_MASK) & IOVP_MASK) > DMA_CHUNK_SIZE)
  1005. break;
  1006. /*
  1007. ** Then look for virtually contiguous blocks.
  1008. ** PARISC needs to associate a virtual address
  1009. ** with each IO address mapped. The CPU cache is
  1010. ** virtually tagged and the IOMMU uses part
  1011. ** of the virtual address to participate in
  1012. ** CPU cache coherency.
  1013. **
  1014. ** append the next transaction?
  1015. */
  1016. vaddr = (unsigned long) sg_virt_addr(startsg);
  1017. if  (vcontig_end == vaddr)
  1018. {
  1019. vcontig_len += startsg->length;
  1020. vcontig_end += startsg->length;
  1021. dma_len     += startsg->length;
  1022. continue;
  1023. }
  1024. #ifdef DEBUG_LARGE_SG_ENTRIES
  1025. dump_run_sg = (vcontig_len > IOVP_SIZE);
  1026. #endif
  1027. /*
  1028. ** Not virtually contigous.
  1029. ** Terminate prev chunk.
  1030. ** Start a new chunk.
  1031. **
  1032. ** Once we start a new VCONTIG chunk, dma_offset
  1033. ** can't change. And we need the offset from the first
  1034. ** chunk - not the last one. Ergo Successive chunks
  1035. ** must start on page boundaries and dove tail
  1036. ** with it's predecessor.
  1037. */
  1038. sg_dma_len(vcontig_sg) = vcontig_len;
  1039. vcontig_sg = startsg;
  1040. vcontig_len = startsg->length;
  1041. /*
  1042. ** 3) do the entries end/start on page boundaries?
  1043. **    Don't update vcontig_end until we've checked.
  1044. */
  1045. if (DMA_CONTIG(vcontig_end, vaddr))
  1046. {
  1047. vcontig_end = vcontig_len + vaddr;
  1048. dma_len += vcontig_len;
  1049. continue;
  1050. } else {
  1051. break;
  1052. }
  1053. }
  1054. /*
  1055. ** End of DMA Stream
  1056. ** Terminate last VCONTIG block.
  1057. ** Allocate space for DMA stream.
  1058. */
  1059. sg_dma_len(vcontig_sg) = vcontig_len;
  1060. dma_len = (dma_len + dma_offset + ~IOVP_MASK) & IOVP_MASK;
  1061. ASSERT(dma_len <= DMA_CHUNK_SIZE);
  1062. sg_dma_address(dma_sg) =
  1063. PIDE_FLAG 
  1064. | (sba_alloc_range(ioc, dma_len) << IOVP_SHIFT)
  1065. | dma_offset;
  1066. n_mappings++;
  1067. }
  1068. return n_mappings;
  1069. }
  1070. /**
  1071.  * sba_map_sg - map Scatter/Gather list
  1072.  * @dev: instance of PCI owned by the driver that's asking.
  1073.  * @sglist:  array of buffer/length pairs
  1074.  * @nents:  number of entries in list
  1075.  * @direction:  R/W or both.
  1076.  *
  1077.  * See Documentation/DMA-mapping.txt
  1078.  */
  1079. static int
  1080. sba_map_sg(struct pci_dev *dev, struct scatterlist *sglist, int nents, int direction)
  1081. {
  1082. struct ioc *ioc;
  1083. int coalesced, filled = 0;
  1084. unsigned long flags;
  1085. DBG_RUN_SG("%s() START %d entriesn", __FUNCTION__, nents);
  1086. ASSERT(dev->sysdata);
  1087. ioc = GET_IOC(dev);
  1088. ASSERT(ioc);
  1089. /* Fast path single entry scatterlists. */
  1090. if (nents == 1) {
  1091. sg_dma_address(sglist) = sba_map_single(dev,
  1092. sg_virt_addr(sglist),
  1093. sglist->length, direction);
  1094. sg_dma_len(sglist)     = sglist->length;
  1095. return 1;
  1096. }
  1097. spin_lock_irqsave(&ioc->res_lock, flags);
  1098. #ifdef ASSERT_PDIR_SANITY
  1099. if (sba_check_pdir(ioc,"Check before sba_map_sg()"))
  1100. {
  1101. sba_dump_sg(ioc, sglist, nents);
  1102. panic("Check before sba_map_sg()");
  1103. }
  1104. #endif
  1105. #ifdef CONFIG_PROC_FS
  1106. ioc->msg_calls++;
  1107. #endif
  1108. /*
  1109. ** First coalesce the chunks and allocate I/O pdir space
  1110. **
  1111. ** If this is one DMA stream, we can properly map using the
  1112. ** correct virtual address associated with each DMA page.
  1113. ** w/o this association, we wouldn't have coherent DMA!
  1114. ** Access to the virtual address is what forces a two pass algorithm.
  1115. */
  1116. coalesced = sba_coalesce_chunks(ioc, sglist, nents);
  1117. /*
  1118. ** Program the I/O Pdir
  1119. **
  1120. ** map the virtual addresses to the I/O Pdir
  1121. ** o dma_address will contain the pdir index
  1122. ** o dma_len will contain the number of bytes to map 
  1123. ** o address contains the virtual address.
  1124. */
  1125. filled = sba_fill_pdir(ioc, sglist, nents);
  1126. #ifdef ASSERT_PDIR_SANITY
  1127. if (sba_check_pdir(ioc,"Check after sba_map_sg()"))
  1128. {
  1129. sba_dump_sg(ioc, sglist, nents);
  1130. panic("Check after sba_map_sg()n");
  1131. }
  1132. #endif
  1133. spin_unlock_irqrestore(&ioc->res_lock, flags);
  1134. ASSERT(coalesced == filled);
  1135. DBG_RUN_SG("%s() DONE %d mappingsn", __FUNCTION__, filled);
  1136. return filled;
  1137. }
  1138. /**
  1139.  * sba_unmap_sg - unmap Scatter/Gather list
  1140.  * @dev: instance of PCI owned by the driver that's asking.
  1141.  * @sglist:  array of buffer/length pairs
  1142.  * @nents:  number of entries in list
  1143.  * @direction:  R/W or both.
  1144.  *
  1145.  * See Documentation/DMA-mapping.txt
  1146.  */
  1147. static void 
  1148. sba_unmap_sg(struct pci_dev *dev, struct scatterlist *sglist, int nents, int direction)
  1149. {
  1150. struct ioc *ioc;
  1151. #ifdef ASSERT_PDIR_SANITY
  1152. unsigned long flags;
  1153. #endif
  1154. DBG_RUN_SG("%s() START %d entries,  %p,%xn",
  1155. __FUNCTION__, nents, sg_virt_addr(sglist), sglist->length);
  1156. ASSERT(dev->sysdata);
  1157. ioc = GET_IOC(dev);
  1158. ASSERT(ioc);
  1159. #ifdef CONFIG_PROC_FS
  1160. ioc->usg_calls++;
  1161. #endif
  1162. #ifdef ASSERT_PDIR_SANITY
  1163. spin_lock_irqsave(&ioc->res_lock, flags);
  1164. sba_check_pdir(ioc,"Check before sba_unmap_sg()");
  1165. spin_unlock_irqrestore(&ioc->res_lock, flags);
  1166. #endif
  1167. while (sg_dma_len(sglist) && nents--) {
  1168. sba_unmap_single(dev, sg_dma_address(sglist), sg_dma_len(sglist), direction);
  1169. #ifdef CONFIG_PROC_FS
  1170. ioc->usg_pages += ((sg_dma_address(sglist) & ~IOVP_MASK) + sg_dma_len(sglist) + IOVP_SIZE - 1) >> PAGE_SHIFT;
  1171. ioc->usingle_calls--; /* kluge since call is unmap_sg() */
  1172. #endif
  1173. ++sglist;
  1174. }
  1175. DBG_RUN_SG("%s() DONE (nents %d)n", __FUNCTION__,  nents);
  1176. #ifdef ASSERT_PDIR_SANITY
  1177. spin_lock_irqsave(&ioc->res_lock, flags);
  1178. sba_check_pdir(ioc,"Check after sba_unmap_sg()");
  1179. spin_unlock_irqrestore(&ioc->res_lock, flags);
  1180. #endif
  1181. }
  1182. static struct pci_dma_ops sba_ops = {
  1183. sba_dma_supported,
  1184. sba_alloc_consistent, /* allocate cacheable host mem */
  1185. sba_free_consistent, /* release cacheable host mem */
  1186. sba_map_single,
  1187. sba_unmap_single,
  1188. sba_map_sg,
  1189. sba_unmap_sg,
  1190. NULL, /* dma_sync_single */
  1191. NULL /* dma_sync_sg */
  1192. };
  1193. /**************************************************************************
  1194. **
  1195. **   SBA PAT PDC support
  1196. **
  1197. **   o call pdc_pat_cell_module()
  1198. **   o store ranges in PCI "resource" structures
  1199. **
  1200. **************************************************************************/
  1201. static void
  1202. sba_get_pat_resources(struct sba_device *sba_dev)
  1203. {
  1204. #if 0
  1205. /*
  1206. ** TODO/REVISIT/FIXME: support for directed ranges requires calls to
  1207. **      PAT PDC to program the SBA/LBA directed range registers...this
  1208. **      burden may fall on the LBA code since it directly supports the
  1209. **      PCI subsystem. It's not clear yet. - ggg
  1210. */
  1211. PAT_MOD(mod)->mod_info.mod_pages   = PAT_GET_MOD_PAGES(temp);
  1212. FIXME : ???
  1213. PAT_MOD(mod)->mod_info.dvi         = PAT_GET_DVI(temp);
  1214. Tells where the dvi bits are located in the address.
  1215. PAT_MOD(mod)->mod_info.ioc         = PAT_GET_IOC(temp);
  1216. FIXME : ???
  1217. #endif
  1218. }
  1219. /**************************************************************
  1220. *
  1221. *   Initialization and claim
  1222. *
  1223. ***************************************************************/
  1224. #define PIRANHA_ADDR_MASK 0x00160000UL /* bit 17,18,20 */
  1225. #define PIRANHA_ADDR_VAL 0x00060000UL /* bit 17,18 on */
  1226. static void *
  1227. sba_alloc_pdir(unsigned int pdir_size)
  1228. {
  1229.         unsigned long pdir_base;
  1230. unsigned long pdir_order = get_order(pdir_size);
  1231. pdir_base = __get_free_pages(GFP_KERNEL, pdir_order);
  1232. if (NULL == (void *) pdir_base)
  1233. panic("sba_ioc_init() could not allocate I/O Page Tablen");
  1234. /* If this is not PA8700 (PCX-W2)
  1235. ** OR newer than ver 2.2
  1236. ** OR in a system that doesn't need VINDEX bits from SBA,
  1237. **
  1238. ** then we aren't exposed to the HW bug.
  1239. */
  1240. if ( ((boot_cpu_data.pdc.cpuid >> 5) & 0x7f) != 0x13
  1241. || (boot_cpu_data.pdc.versions > 0x202)
  1242. || (boot_cpu_data.pdc.capabilities & 0x08L) )
  1243. return (void *) pdir_base;
  1244. /*
  1245.  * PA8700 (PCX-W2, aka piranha) silent data corruption fix
  1246.  *
  1247.  * An interaction between PA8700 CPU (Ver 2.2 or older) and
  1248.  * Ike/Astro can cause silent data corruption. This is only
  1249.  * a problem if the I/O PDIR is located in memory such that
  1250.  * (little-endian)  bits 17 and 18 are on and bit 20 is off.
  1251.  *
  1252.  * Since the max IO Pdir size is 2MB, by cleverly allocating the
  1253.  * right physical address, we can either avoid (IOPDIR <= 1MB)
  1254.  * or minimize (2MB IO Pdir) the problem if we restrict the
  1255.  * IO Pdir to a maximum size of 2MB-128K (1902K).
  1256.  *
  1257.  * Because we always allocate 2^N sized IO pdirs, either of the
  1258.  * "bad" regions will be the last 128K if at all. That's easy
  1259.  * to test for.
  1260.  * 
  1261.  */
  1262. if (pdir_order <= (19-12)) {
  1263. if (((virt_to_phys(pdir_base)+pdir_size-1) & PIRANHA_ADDR_MASK) == PIRANHA_ADDR_VAL) {
  1264. /* allocate a new one on 512k alignment */
  1265. unsigned long new_pdir = __get_free_pages(GFP_KERNEL, (19-12));
  1266. /* release original */
  1267. free_pages(pdir_base, pdir_order);
  1268. pdir_base = new_pdir;
  1269. /* release excess */
  1270. while (pdir_order < (19-12)) {
  1271. new_pdir += pdir_size;
  1272. free_pages(new_pdir, pdir_order);
  1273. pdir_order +=1;
  1274. pdir_size <<=1;
  1275. }
  1276. }
  1277. } else {
  1278. /*
  1279. ** 1MB or 2MB Pdir
  1280. ** Needs to be aligned on an "odd" 1MB boundary.
  1281. */
  1282. unsigned long new_pdir = __get_free_pages(GFP_KERNEL, pdir_order+1); /* 2 or 4MB */
  1283. /* release original */
  1284. free_pages( pdir_base, pdir_order);
  1285. /* release first 1MB */
  1286. free_pages(new_pdir, 20-12);
  1287. pdir_base = new_pdir + 1024*1024;
  1288. if (pdir_order > (20-12)) {
  1289. /*
  1290. ** 2MB Pdir.
  1291. **
  1292. ** Flag tells init_bitmap() to mark bad 128k as used
  1293. ** and to reduce the size by 128k.
  1294. */
  1295. piranha_bad_128k = 1;
  1296. new_pdir += 3*1024*1024;
  1297. /* release last 1MB */
  1298. free_pages(new_pdir, 20-12);
  1299. /* release unusable 128KB */
  1300. free_pages(new_pdir - 128*1024 , 17-12);
  1301. pdir_size -= 128*1024;
  1302. }
  1303. }
  1304. memset((void *) pdir_base, 0, pdir_size);
  1305. return (void *) pdir_base;
  1306. }
  1307. static void
  1308. sba_ioc_init(struct parisc_device *sba, struct ioc *ioc, int ioc_num)
  1309. {
  1310. /* lba_set_iregs() is in arch/parisc/kernel/lba_pci.c */
  1311. extern void lba_set_iregs(struct parisc_device *, u32, u32);
  1312. u32 iova_space_size, iova_space_mask;
  1313. int pdir_size, iov_order;
  1314. unsigned long physmem;
  1315. struct parisc_device *lba;
  1316. /*
  1317. ** Determine IOVA Space size from memory size.
  1318. **
  1319. ** Ideally, PCI drivers would register the maximum number
  1320. ** of DMA they can have outstanding for each device they
  1321. ** own.  Next best thing would be to guess how much DMA
  1322. ** can be outstanding based on PCI Class/sub-class. Both
  1323. ** methods still require some "extra" to support PCI
  1324. ** Hot-Plug/Removal of PCI cards. (aka PCI OLARD).
  1325. **
  1326. ** While we have 32-bits "IOVA" space, top two 2 bits are used
  1327. ** for DMA hints - ergo only 30 bits max.
  1328. */
  1329. physmem = num_physpages << PAGE_SHIFT;
  1330. iova_space_size = (u32) (physmem/(sba_mem_ratio*global_ioc_cnt));
  1331. /* limit IOVA space size to 1MB-1GB */
  1332. if (iova_space_size < 1024*1024) {
  1333. iova_space_size = 1024*1024;
  1334. }
  1335. #ifdef __LP64__
  1336. else if (iova_space_size > 512*1024*1024) {
  1337. iova_space_size = 512*1024*1024;
  1338. }
  1339. #endif
  1340. /*
  1341. ** iova space must be log2() in size.
  1342. ** thus, pdir/res_map will also be log2().
  1343. ** PIRANHA BUG: Exception is when IO Pdir is 2MB (gets reduced)
  1344. */
  1345. iov_order = get_order(iova_space_size >> (IOVP_SHIFT-PAGE_SHIFT));
  1346. ASSERT(iov_order <= (30 - IOVP_SHIFT));   /* iova_space_size <= 1GB */
  1347. ASSERT(iov_order >= (20 - IOVP_SHIFT));   /* iova_space_size >= 1MB */
  1348. iova_space_size = 1 << (iov_order + IOVP_SHIFT);
  1349. ioc->pdir_size = pdir_size = (iova_space_size/IOVP_SIZE) * sizeof(u64);
  1350. ASSERT(pdir_size < 4*1024*1024);   /* max pdir size == 2MB */
  1351. /* Verify it's a power of two */
  1352. ASSERT((1 << get_order(pdir_size)) == (pdir_size >> PAGE_SHIFT));
  1353. DBG_INIT("%s() hpa 0x%lx mem %dMB IOV %dMB (%d bits) PDIR size 0x%0xn",
  1354. __FUNCTION__, ioc->ioc_hpa, (int) (physmem>>20),
  1355. iova_space_size>>20, iov_order + PAGE_SHIFT, pdir_size);
  1356. /* FIXME : DMA HINTs not used */
  1357. ioc->hint_shift_pdir = iov_order + PAGE_SHIFT;
  1358. ioc->hint_mask_pdir = ~(0x3 << (iov_order + PAGE_SHIFT));
  1359. ioc->pdir_base = sba_alloc_pdir(pdir_size);
  1360. DBG_INIT("%s() pdir %p size %x hint_shift_pdir %x hint_mask_pdir %lxn",
  1361. __FUNCTION__, ioc->pdir_base, pdir_size,
  1362. ioc->hint_shift_pdir, ioc->hint_mask_pdir);
  1363. ASSERT((((unsigned long) ioc->pdir_base) & PAGE_MASK) == (unsigned long) ioc->pdir_base);
  1364. WRITE_REG64(virt_to_phys(ioc->pdir_base), ioc->ioc_hpa + IOC_PDIR_BASE);
  1365. /* build IMASK for IOC and Elroy */
  1366. iova_space_mask =  0xffffffff;
  1367. iova_space_mask <<= (iov_order + PAGE_SHIFT);
  1368. /*
  1369. ** On C3000 w/512MB mem, HP-UX 10.20 reports:
  1370. **     ibase=0, imask=0xFE000000, size=0x2000000.
  1371. */
  1372. ioc->ibase = IOC_IOVA_SPACE_BASE | 1; /* bit 0 == enable bit */
  1373. ioc->imask = iova_space_mask; /* save it */
  1374. DBG_INIT("%s() IOV base 0x%lx mask 0x%0lxn",
  1375. __FUNCTION__, ioc->ibase, ioc->imask);
  1376. /*
  1377. ** FIXME: Hint registers are programmed with default hint
  1378. ** values during boot, so hints should be sane even if we
  1379. ** can't reprogram them the way drivers want.
  1380. */
  1381. /*
  1382. ** setup Elroy IBASE/IMASK registers as well.
  1383. */
  1384. for (lba = sba->child; lba; lba = lba->sibling) {
  1385. int rope_num = (lba->hpa >> 13) & 0xf;
  1386. if (rope_num >> 3 == ioc_num)
  1387. lba_set_iregs(lba, ioc->ibase, ioc->imask);
  1388. }
  1389. /*
  1390. ** Program the IOC's ibase and enable IOVA translation
  1391. */
  1392. WRITE_REG(ioc->ibase, ioc->ioc_hpa+IOC_IBASE);
  1393. WRITE_REG(ioc->imask, ioc->ioc_hpa+IOC_IMASK);
  1394. /* Set I/O PDIR Page size to 4K */
  1395. WRITE_REG(0, ioc->ioc_hpa+IOC_TCNFG);
  1396. /*
  1397. ** Clear I/O TLB of any possible entries.
  1398. ** (Yes. This is a bit paranoid...but so what)
  1399. */
  1400. WRITE_REG(0 | 31, ioc->ioc_hpa+IOC_PCOM);
  1401. DBG_INIT("%s() DONEn", __FUNCTION__);
  1402. }
  1403. /**************************************************************************
  1404. **
  1405. **   SBA initialization code (HW and SW)
  1406. **
  1407. **   o identify SBA chip itself
  1408. **   o initialize SBA chip modes (HardFail)
  1409. **   o initialize SBA chip modes (HardFail)
  1410. **   o FIXME: initialize DMA hints for reasonable defaults
  1411. **
  1412. **************************************************************************/
  1413. static void
  1414. sba_hw_init(struct sba_device *sba_dev)
  1415. int i;
  1416. int num_ioc;
  1417. u64 ioc_ctl;
  1418. ioc_ctl = READ_REG(sba_dev->sba_hpa+IOC_CTRL);
  1419. DBG_INIT("%s() hpa 0x%lx ioc_ctl 0x%Lx ->",
  1420. __FUNCTION__, sba_dev->sba_hpa, ioc_ctl);
  1421. ioc_ctl &= ~(IOC_CTRL_RM | IOC_CTRL_NC | IOC_CTRL_CE);
  1422. ioc_ctl |= IOC_CTRL_TC; /* Astro: firmware enables this */
  1423. WRITE_REG(ioc_ctl, sba_dev->sba_hpa+IOC_CTRL);
  1424. #ifdef DEBUG_SBA_INIT
  1425. ioc_ctl = READ_REG64(sba_dev->sba_hpa+IOC_CTRL);
  1426. DBG_INIT(" 0x%Lxn", ioc_ctl);
  1427. #endif
  1428. if (IS_ASTRO(sba_dev->iodc)) {
  1429. /* PAT_PDC (L-class) also reports the same goofy base */
  1430. sba_dev->ioc[0].ioc_hpa = ASTRO_IOC_OFFSET;
  1431. num_ioc = 1;
  1432. } else {
  1433. sba_dev->ioc[0].ioc_hpa = sba_dev->ioc[1].ioc_hpa = 0;
  1434. num_ioc = 2;
  1435. }
  1436. sba_dev->num_ioc = num_ioc;
  1437. for (i = 0; i < num_ioc; i++) {
  1438. sba_dev->ioc[i].ioc_hpa += sba_dev->sba_hpa + IKE_IOC_OFFSET(i);
  1439. /*
  1440. ** Make sure the box crashes if we get any errors on a rope.
  1441. */
  1442. WRITE_REG(HF_ENABLE, sba_dev->ioc[i].ioc_hpa + ROPE0_CTL);
  1443. WRITE_REG(HF_ENABLE, sba_dev->ioc[i].ioc_hpa + ROPE1_CTL);
  1444. WRITE_REG(HF_ENABLE, sba_dev->ioc[i].ioc_hpa + ROPE2_CTL);
  1445. WRITE_REG(HF_ENABLE, sba_dev->ioc[i].ioc_hpa + ROPE3_CTL);
  1446. WRITE_REG(HF_ENABLE, sba_dev->ioc[i].ioc_hpa + ROPE4_CTL);
  1447. WRITE_REG(HF_ENABLE, sba_dev->ioc[i].ioc_hpa + ROPE5_CTL);
  1448. WRITE_REG(HF_ENABLE, sba_dev->ioc[i].ioc_hpa + ROPE6_CTL);
  1449. WRITE_REG(HF_ENABLE, sba_dev->ioc[i].ioc_hpa + ROPE7_CTL);
  1450. /* flush out the writes */
  1451. READ_REG(sba_dev->ioc[i].ioc_hpa + ROPE7_CTL);
  1452. sba_ioc_init(sba_dev->dev, &(sba_dev->ioc[i]), i);
  1453. }
  1454. }
  1455. static void
  1456. sba_common_init(struct sba_device *sba_dev)
  1457. {
  1458. int i;
  1459. /* add this one to the head of the list (order doesn't matter)
  1460. ** This will be useful for debugging - especially if we get coredumps
  1461. */
  1462. sba_dev->next = sba_list;
  1463. sba_list = sba_dev;
  1464. for(i=0; i< sba_dev->num_ioc; i++) {
  1465. int res_size;
  1466. #ifdef DEBUG_DMB_TRAP
  1467. extern void iterate_pages(unsigned long , unsigned long ,
  1468.   void (*)(pte_t * , unsigned long),
  1469.   unsigned long );
  1470. void set_data_memory_break(pte_t * , unsigned long);
  1471. #endif
  1472. /* resource map size dictated by pdir_size */
  1473. res_size = sba_dev->ioc[i].pdir_size/sizeof(u64); /* entries */
  1474. /* Second part of PIRANHA BUG */
  1475. if (piranha_bad_128k) {
  1476. res_size -= (128*1024)/sizeof(u64);
  1477. }
  1478. res_size >>= 3;  /* convert bit count to byte count */
  1479. DBG_INIT("%s() res_size 0x%xn",
  1480. __FUNCTION__, res_size);
  1481. sba_dev->ioc[i].res_size = res_size;
  1482. sba_dev->ioc[i].res_map = (char *) __get_free_pages(GFP_KERNEL, get_order(res_size));
  1483. #ifdef DEBUG_DMB_TRAP
  1484. iterate_pages( sba_dev->ioc[i].res_map, res_size,
  1485. set_data_memory_break, 0);
  1486. #endif
  1487. if (NULL == sba_dev->ioc[i].res_map)
  1488. {
  1489. panic(__FILE__ ":%s() could not allocate resource mapn", __FUNCTION__ );
  1490. }
  1491. memset(sba_dev->ioc[i].res_map, 0, res_size);
  1492. /* next available IOVP - circular search */
  1493. sba_dev->ioc[i].res_hint = (unsigned long *)
  1494. &(sba_dev->ioc[i].res_map[L1_CACHE_BYTES]);
  1495. #ifdef ASSERT_PDIR_SANITY
  1496. /* Mark first bit busy - ie no IOVA 0 */
  1497. sba_dev->ioc[i].res_map[0] = 0x80;
  1498. sba_dev->ioc[i].pdir_base[0] = 0xeeffc0addbba0080ULL;
  1499. #endif
  1500. /* Third (and last) part of PIRANHA BUG */
  1501. if (piranha_bad_128k) {
  1502. /* region from +1408K to +1536 is un-usable. */
  1503. int idx_start = (1408*1024/sizeof(u64)) >> 3;
  1504. int idx_end   = (1536*1024/sizeof(u64)) >> 3;
  1505. long *p_start = (long *) &(sba_dev->ioc[i].res_map[idx_start]);
  1506. long *p_end   = (long *) &(sba_dev->ioc[i].res_map[idx_end]);
  1507. /* mark that part of the io pdir busy */
  1508. while (p_start < p_end)
  1509. *p_start++ = -1;
  1510. }
  1511. #ifdef DEBUG_DMB_TRAP
  1512. iterate_pages( sba_dev->ioc[i].res_map, res_size,
  1513. set_data_memory_break, 0);
  1514. iterate_pages( sba_dev->ioc[i].pdir_base, sba_dev->ioc[i].pdir_size,
  1515. set_data_memory_break, 0);
  1516. #endif
  1517. DBG_INIT("%s() %d res_map %x %pn",
  1518. __FUNCTION__, i, res_size, sba_dev->ioc[i].res_map);
  1519. }
  1520. sba_dev->sba_lock = SPIN_LOCK_UNLOCKED;
  1521. ioc_needs_fdc = boot_cpu_data.pdc.capabilities & PDC_MODEL_IOPDIR_FDC;
  1522. #ifdef DEBUG_SBA_INIT
  1523. /*
  1524.  * If the PDC_MODEL capabilities has Non-coherent IO-PDIR bit set
  1525.  * (bit #61, big endian), we have to flush and sync every time
  1526.  * IO-PDIR is changed in Ike/Astro.
  1527.  */
  1528. if (boot_cpu_data.pdc.capabilities & PDC_MODEL_IOPDIR_FDC) {
  1529. printk(KERN_INFO MODULE_NAME " FDC/SYNC required.n");
  1530. } else {
  1531. printk(KERN_INFO MODULE_NAME " IOC has cache coherent PDIR.n");
  1532. }
  1533. #endif
  1534. }
  1535. #ifdef CONFIG_PROC_FS
  1536. static int sba_proc_info(char *buf, char **start, off_t offset, int len)
  1537. {
  1538. struct sba_device *sba_dev = sba_list;
  1539. struct ioc *ioc = &sba_dev->ioc[0]; /* FIXME: Multi-IOC support! */
  1540. int total_pages = (int) (ioc->res_size << 3); /* 8 bits per byte */
  1541. unsigned long i = 0, avg = 0, min, max;
  1542. sprintf(buf, "%s rev %d.%dn",
  1543. sba_dev->name,
  1544. (sba_dev->hw_rev & 0x7) + 1,
  1545. (sba_dev->hw_rev & 0x18) >> 3
  1546. );
  1547. sprintf(buf, "%sIO PDIR size    : %d bytes (%d entries)n",
  1548. buf,
  1549. (int) ((ioc->res_size << 3) * sizeof(u64)), /* 8 bits/byte */
  1550. total_pages);
  1551. sprintf(buf, "%sIO PDIR entries : %ld free  %ld used (%d%%)n", buf,
  1552. total_pages - ioc->used_pages, ioc->used_pages,
  1553. (int) (ioc->used_pages * 100 / total_pages));
  1554. sprintf(buf, "%sResource bitmap : %d bytes (%d pages)n", 
  1555. buf, ioc->res_size, ioc->res_size << 3);   /* 8 bits per byte */
  1556. min = max = ioc->avg_search[0];
  1557. for (i = 0; i < SBA_SEARCH_SAMPLE; i++) {
  1558. avg += ioc->avg_search[i];
  1559. if (ioc->avg_search[i] > max) max = ioc->avg_search[i];
  1560. if (ioc->avg_search[i] < min) min = ioc->avg_search[i];
  1561. }
  1562. avg /= SBA_SEARCH_SAMPLE;
  1563. sprintf(buf, "%s  Bitmap search : %ld/%ld/%ld (min/avg/max CPU Cycles)n",
  1564. buf, min, avg, max);
  1565. sprintf(buf, "%spci_map_single(): %12ld calls  %12ld pages (avg %d/1000)n",
  1566. buf, ioc->msingle_calls, ioc->msingle_pages,
  1567. (int) ((ioc->msingle_pages * 1000)/ioc->msingle_calls));
  1568. /* KLUGE - unmap_sg calls unmap_single for each mapped page */
  1569. min = ioc->usingle_calls;
  1570. max = ioc->usingle_pages - ioc->usg_pages;
  1571. sprintf(buf, "%spci_unmap_single: %12ld calls  %12ld pages (avg %d/1000)n",
  1572. buf, min, max,
  1573. (int) ((max * 1000)/min));
  1574. sprintf(buf, "%spci_map_sg()    : %12ld calls  %12ld pages (avg %d/1000)n",
  1575. buf, ioc->msg_calls, ioc->msg_pages,
  1576. (int) ((ioc->msg_pages * 1000)/ioc->msg_calls));
  1577. sprintf(buf, "%spci_unmap_sg()  : %12ld calls  %12ld pages (avg %d/1000)n",
  1578. buf, ioc->usg_calls, ioc->usg_pages,
  1579. (int) ((ioc->usg_pages * 1000)/ioc->usg_calls));
  1580. return strlen(buf);
  1581. }
  1582. #if 0
  1583. /* XXX too much output - exceeds 4k limit and needs to be re-written */
  1584. static int
  1585. sba_resource_map(char *buf, char **start, off_t offset, int len)
  1586. {
  1587. struct sba_device *sba_dev = sba_list;
  1588. struct ioc *ioc = &sba_dev->ioc[0]; /* FIXME: Mutli-IOC suppoer! */
  1589. unsigned int *res_ptr = (unsigned int *)ioc->res_map;
  1590. int i;
  1591. buf[0] = '';
  1592. for(i = 0; i < (ioc->res_size / sizeof(unsigned int)); ++i, ++res_ptr) {
  1593. if ((i & 7) == 0)
  1594.     strcat(buf,"n   ");
  1595. sprintf(buf, "%s %08x", buf, *res_ptr);
  1596. }
  1597. strcat(buf, "n");
  1598. return strlen(buf);
  1599. }
  1600. #endif /* 0 */
  1601. #endif /* CONFIG_PROC_FS */
  1602. static struct parisc_device_id sba_tbl[] = {
  1603. { HPHW_IOA, HVERSION_REV_ANY_ID, ASTRO_RUNWAY_PORT, 0xb },
  1604. { HPHW_BCPORT, HVERSION_REV_ANY_ID, IKE_MERCED_PORT, 0xc },
  1605. { HPHW_BCPORT, HVERSION_REV_ANY_ID, REO_MERCED_PORT, 0xc },
  1606. { HPHW_BCPORT, HVERSION_REV_ANY_ID, REOG_MERCED_PORT, 0xc },
  1607. /* These two entries commented out because we don't find them in a
  1608.  * buswalk yet.  If/when we do, they would cause us to think we had
  1609.  * many more SBAs then we really do.
  1610.  * { HPHW_BCPORT, HVERSION_REV_ANY_ID, ASTRO_ROPES_PORT, 0xc },
  1611.  * { HPHW_BCPORT, HVERSION_REV_ANY_ID, IKE_ROPES_PORT, 0xc },
  1612.  */
  1613. { 0, }
  1614. };
  1615. int sba_driver_callback(struct parisc_device *);
  1616. static struct parisc_driver sba_driver = {
  1617. name: MODULE_NAME,
  1618. id_table: sba_tbl,
  1619. probe: sba_driver_callback,
  1620. };
  1621. /*
  1622. ** Determine if lba should claim this chip (return 0) or not (return 1).
  1623. ** If so, initialize the chip and tell other partners in crime they
  1624. ** have work to do.
  1625. */
  1626. int
  1627. sba_driver_callback(struct parisc_device *dev)
  1628. {
  1629. struct sba_device *sba_dev;
  1630. u32 func_class;
  1631. int i;
  1632. char *version;
  1633. #ifdef DEBUG_SBA_INIT
  1634. sba_dump_ranges(dev->hpa);
  1635. #endif
  1636. /* Read HW Rev First */
  1637. func_class = READ_REG(dev->hpa + SBA_FCLASS);
  1638. if (IS_ASTRO(&dev->id)) {
  1639. unsigned long fclass;
  1640. static char astro_rev[]="Astro ?.?";
  1641. /* Astro is broken...Read HW Rev First */
  1642. fclass = READ_REG(dev->hpa);
  1643. astro_rev[6] = '1' + (char) (fclass & 0x7);
  1644. astro_rev[8] = '0' + (char) ((fclass & 0x18) >> 3);
  1645. version = astro_rev;
  1646. } else if (IS_IKE(&dev->id)) {
  1647. static char ike_rev[]="Ike rev ?";
  1648. ike_rev[8] = '0' + (char) (func_class & 0xff);
  1649. version = ike_rev;
  1650. } else {
  1651. static char reo_rev[]="REO rev ?";
  1652. reo_rev[8] = '0' + (char) (func_class & 0xff);
  1653. version = reo_rev;
  1654. }
  1655. if (!global_ioc_cnt) {
  1656. global_ioc_cnt = count_parisc_driver(&sba_driver);
  1657. /* Only Astro has one IOC per SBA */
  1658. if (!IS_ASTRO(&dev->id))
  1659. global_ioc_cnt *= 2;
  1660. }
  1661. printk(KERN_INFO "%s found %s at 0x%lxn",
  1662. MODULE_NAME, version, dev->hpa);
  1663. #ifdef DEBUG_SBA_INIT
  1664. sba_dump_tlb(dev->hpa);
  1665. #endif
  1666. sba_dev = kmalloc(sizeof(struct sba_device), GFP_KERNEL);
  1667. if (NULL == sba_dev) {
  1668. printk(KERN_ERR MODULE_NAME " - couldn't alloc sba_devicen");
  1669. return(1);
  1670. }
  1671. dev->sysdata = (void *) sba_dev;
  1672. memset(sba_dev, 0, sizeof(struct sba_device));
  1673. for(i=0; i<MAX_IOC; i++)
  1674. spin_lock_init(&(sba_dev->ioc[i].res_lock));
  1675. sba_dev->dev = dev;
  1676. sba_dev->hw_rev = func_class;
  1677. sba_dev->iodc = &dev->id;
  1678. sba_dev->name = dev->name;
  1679. sba_dev->sba_hpa = dev->hpa;  /* faster access */
  1680. sba_get_pat_resources(sba_dev);
  1681. sba_hw_init(sba_dev);
  1682. sba_common_init(sba_dev);
  1683. hppa_dma_ops = &sba_ops;
  1684. #ifdef CONFIG_PROC_FS
  1685. if (IS_ASTRO(&dev->id)) {
  1686. create_proc_info_entry("Astro", 0, proc_runway_root, sba_proc_info);
  1687. } else if (IS_IKE(&dev->id)) {
  1688. create_proc_info_entry("Ike", 0, proc_runway_root, sba_proc_info);
  1689. } else {
  1690. create_proc_info_entry("Reo", 0, proc_runway_root, sba_proc_info);
  1691. }
  1692. #if 0
  1693. create_proc_info_entry("bitmap", 0, proc_runway_root, sba_resource_map);
  1694. #endif
  1695. #endif
  1696. return 0;
  1697. }
  1698. /*
  1699. ** One time initialization to let the world know the SBA was found.
  1700. ** This is the only routine which is NOT static.
  1701. ** Must be called exactly once before pci_init().
  1702. */
  1703. void __init sba_init(void)
  1704. {
  1705. register_parisc_driver(&sba_driver);
  1706. }
  1707. /**
  1708.  * sba_get_iommu - Assign the iommu pointer for the pci bus controller.
  1709.  * @dev: The parisc device.
  1710.  *
  1711.  * This function searches through the registerd IOMMU's and returns the
  1712.  * appropriate IOMMU data for the given parisc PCI controller.
  1713.  */
  1714. void * sba_get_iommu(struct parisc_device *pci_hba)
  1715. {
  1716. struct sba_device *sba = (struct sba_device *) pci_hba->parent->sysdata;
  1717. char t = pci_hba->parent->id.hw_type;
  1718. int iocnum = (pci_hba->hw_path >> 3); /* rope # */
  1719. if ((t!=HPHW_IOA) && (t!=HPHW_BCPORT))
  1720. BUG();
  1721. return &(sba->ioc[iocnum]);
  1722. }