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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2. ** ccio-dma.c:
  3. ** DMA management routines for first generation cache-coherent machines.
  4. ** Program U2/Uturn in "Virtual Mode" and use the I/O MMU.
  5. **
  6. ** (c) Copyright 2000 Grant Grundler
  7. ** (c) Copyright 2000 Ryan Bradetich
  8. ** (c) Copyright 2000 Hewlett-Packard Company
  9. **
  10. ** This program is free software; you can redistribute it and/or modify
  11. ** it under the terms of the GNU General Public License as published by
  12. ** the Free Software Foundation; either version 2 of the License, or
  13. ** (at your option) any later version.
  14. **
  15. **
  16. **  "Real Mode" operation refers to U2/Uturn chip operation.
  17. **  U2/Uturn were designed to perform coherency checks w/o using
  18. **  the I/O MMU - basically what x86 does.
  19. **
  20. **  Philipp Rumpf has a "Real Mode" driver for PCX-W machines at:
  21. **      CVSROOT=:pserver:anonymous@198.186.203.37:/cvsroot/linux-parisc
  22. **      cvs -z3 co linux/arch/parisc/kernel/dma-rm.c
  23. **
  24. **  I've rewritten his code to work under TPG's tree. See ccio-rm-dma.c.
  25. **
  26. **  Drawbacks of using Real Mode are:
  27. ** o outbound DMA is slower - U2 won't prefetch data (GSC+ XQL signal).
  28. **      o Inbound DMA less efficient - U2 can't use DMA_FAST attribute.
  29. ** o Ability to do scatter/gather in HW is lost.
  30. ** o Doesn't work under PCX-U/U+ machines since they didn't follow
  31. **        the coherency design originally worked out. Only PCX-W does.
  32. */
  33. #include <linux/config.h>
  34. #include <linux/types.h>
  35. #include <linux/init.h>
  36. #include <linux/mm.h>
  37. #include <linux/spinlock.h>
  38. #include <linux/slab.h>
  39. #include <linux/string.h>
  40. #define PCI_DEBUG
  41. #include <linux/pci.h>
  42. #undef PCI_DEBUG
  43. #include <asm/byteorder.h>
  44. #include <asm/cache.h> /* for L1_CACHE_BYTES */
  45. #include <asm/uaccess.h>
  46. #include <asm/pgalloc.h>
  47. #include <asm/page.h>
  48. #include <asm/dma.h>
  49. #include <asm/io.h>
  50. #include <asm/gsc.h> /* for gsc_writeN()... */
  51. #include <asm/hardware.h>       /* for register_module() */
  52. /* 
  53. ** Choose "ccio" since that's what HP-UX calls it.
  54. ** Make it easier for folks to migrate from one to the other :^)
  55. */
  56. #define MODULE_NAME "ccio"
  57. #undef DEBUG_CCIO_RES
  58. #undef DEBUG_CCIO_RUN
  59. #undef DEBUG_CCIO_INIT
  60. #undef DEBUG_CCIO_RUN_SG
  61. #include <linux/proc_fs.h>
  62. #include <asm/runway.h> /* for proc_runway_root */
  63. #ifdef DEBUG_CCIO_INIT
  64. #define DBG_INIT(x...)  printk(x)
  65. #else
  66. #define DBG_INIT(x...)
  67. #endif
  68. #ifdef DEBUG_CCIO_RUN
  69. #define DBG_RUN(x...)   printk(x)
  70. #else
  71. #define DBG_RUN(x...)
  72. #endif
  73. #ifdef DEBUG_CCIO_RES
  74. #define DBG_RES(x...)   printk(x)
  75. #else
  76. #define DBG_RES(x...)
  77. #endif
  78. #ifdef DEBUG_CCIO_RUN_SG
  79. #define DBG_RUN_SG(x...) printk(x)
  80. #else
  81. #define DBG_RUN_SG(x...)
  82. #endif
  83. #define CCIO_INLINE /* inline */
  84. #define WRITE_U32(value, addr) gsc_writel(value, (u32 *)(addr))
  85. #define READ_U32(addr) gsc_readl((u32 *)(addr))
  86. #define U2_IOA_RUNWAY 0x580
  87. #define U2_BC_GSC     0x501
  88. #define UTURN_IOA_RUNWAY 0x581
  89. #define UTURN_BC_GSC     0x502
  90. #define IOA_NORMAL_MODE      0x00020080 /* IO_CONTROL to turn on CCIO        */
  91. #define CMD_TLB_DIRECT_WRITE 35         /* IO_COMMAND for I/O TLB Writes     */
  92. #define CMD_TLB_PURGE        33         /* IO_COMMAND to Purge I/O TLB entry */
  93. struct ioa_registers {
  94.         /* Runway Supervisory Set */
  95.         volatile int32_t    unused1[12];
  96.         volatile uint32_t   io_command;             /* Offset 12 */
  97.         volatile uint32_t   io_status;              /* Offset 13 */
  98.         volatile uint32_t   io_control;             /* Offset 14 */
  99.         volatile int32_t    unused2[1];
  100.         /* Runway Auxiliary Register Set */
  101.         volatile uint32_t   io_err_resp;            /* Offset  0 */
  102.         volatile uint32_t   io_err_info;            /* Offset  1 */
  103.         volatile uint32_t   io_err_req;             /* Offset  2 */
  104.         volatile uint32_t   io_err_resp_hi;         /* Offset  3 */
  105.         volatile uint32_t   io_tlb_entry_m;         /* Offset  4 */
  106.         volatile uint32_t   io_tlb_entry_l;         /* Offset  5 */
  107.         volatile uint32_t   unused3[1];
  108.         volatile uint32_t   io_pdir_base;           /* Offset  7 */
  109.         volatile uint32_t   io_io_low_hv;           /* Offset  8 */
  110.         volatile uint32_t   io_io_high_hv;          /* Offset  9 */
  111.         volatile uint32_t   unused4[1];
  112.         volatile uint32_t   io_chain_id_mask;       /* Offset 11 */
  113.         volatile uint32_t   unused5[2];
  114.         volatile uint32_t   io_io_low;              /* Offset 14 */
  115.         volatile uint32_t   io_io_high;             /* Offset 15 */
  116. };
  117. struct ioc {
  118. struct ioa_registers *ioc_hpa;  /* I/O MMU base address */
  119. u8  *res_map;                 /* resource map, bit == pdir entry */
  120. u64 *pdir_base;                 /* physical base address */
  121. u32 res_hint;                 /* next available IOVP - 
  122.    circular search */
  123. u32 res_size;      /* size of resource map in bytes */
  124. spinlock_t res_lock;
  125. #ifdef CONFIG_PROC_FS
  126. #define CCIO_SEARCH_SAMPLE 0x100
  127. unsigned long avg_search[CCIO_SEARCH_SAMPLE];
  128. unsigned long avg_idx;   /* current index into avg_search */
  129. unsigned long used_pages;
  130. unsigned long msingle_calls;
  131. unsigned long msingle_pages;
  132. unsigned long msg_calls;
  133. unsigned long msg_pages;
  134. unsigned long usingle_calls;
  135. unsigned long usingle_pages;
  136. unsigned long usg_calls;
  137. unsigned long usg_pages;
  138. unsigned short cujo20_bug;
  139. #endif
  140. /* STUFF We don't need in performance path */
  141. u32 pdir_size;  /* in bytes, determined by IOV Space size */
  142. u32 chainid_shift;  /* specify bit location of chain_id */
  143. struct ioc *next; /* Linked list of discovered iocs */
  144. const char *name; /* device name from firmware */
  145. unsigned int hw_path;           /* the hardware path this ioc is associatd with */
  146. struct pci_dev *fake_pci_dev;   /* the fake pci_dev for non-pci devs */
  147. struct resource mmio_region[2]; /* The "routed" MMIO regions */
  148. };
  149. /* Ratio of Host MEM to IOV Space size */
  150. static unsigned long ccio_mem_ratio = 4;
  151. static struct ioc *ioc_list;
  152. static int ioc_count;
  153. /**************************************************************
  154. *
  155. *   I/O Pdir Resource Management
  156. *
  157. *   Bits set in the resource map are in use.
  158. *   Each bit can represent a number of pages.
  159. *   LSbs represent lower addresses (IOVA's).
  160. *
  161. *   This was was copied from sba_iommu.c. Don't try to unify
  162. *   the two resource managers unless a way to have different
  163. *   allocation policies is also adjusted. We'd like to avoid
  164. *   I/O TLB thrashing by having resource allocation policy
  165. *   match the I/O TLB replacement policy.
  166. *
  167. ***************************************************************/
  168. #define IOVP_SIZE PAGE_SIZE
  169. #define IOVP_SHIFT PAGE_SHIFT
  170. #define IOVP_MASK PAGE_MASK
  171. /* Convert from IOVP to IOVA and vice versa. */
  172. #define CCIO_IOVA(iovp,offset) ((iovp) | (offset))
  173. #define CCIO_IOVP(iova) ((iova) & IOVP_MASK)
  174. #define PDIR_INDEX(iovp)    ((iovp)>>IOVP_SHIFT)
  175. #define MKIOVP(pdir_idx)    ((long)(pdir_idx) << IOVP_SHIFT)
  176. #define MKIOVA(iovp,offset) (dma_addr_t)((long)iovp | (long)offset)
  177. #define ROUNDUP(x,y) ((x + ((y)-1)) & ~((y)-1))
  178. /*
  179. ** Don't worry about the 150% average search length on a miss.
  180. ** If the search wraps around, and passes the res_hint, it will
  181. ** cause the kernel to panic anyhow.
  182. */
  183. #define CCIO_SEARCH_LOOP(ioc, res_idx, mask_ptr, size)  
  184.        for(; res_ptr < res_end; ++res_ptr) { 
  185.                if(0 == (*res_ptr & *mask_ptr)) { 
  186.                        *res_ptr |= *mask_ptr; 
  187.                        res_idx = (int)((unsigned long)res_ptr - (unsigned long)ioc->res_map); 
  188.                        ioc->res_hint = res_idx + (size >> 3); 
  189.                        goto resource_found; 
  190.                } 
  191.        }
  192. #define CCIO_FIND_FREE_MAPPING(ioa, res_idx, mask, size) 
  193.        u##size *res_ptr = (u##size *)&((ioc)->res_map[ioa->res_hint & ~((size >> 3) - 1)]); 
  194.        u##size *res_end = (u##size *)&(ioc)->res_map[ioa->res_size]; 
  195.        u##size *mask_ptr = (u##size *)&mask; 
  196.        CCIO_SEARCH_LOOP(ioc, res_idx, mask_ptr, size); 
  197.        res_ptr = (u##size *)&(ioc)->res_map[0]; 
  198.        CCIO_SEARCH_LOOP(ioa, res_idx, mask_ptr, size);
  199. /*
  200. ** Find available bit in this ioa's resource map.
  201. ** Use a "circular" search:
  202. **   o Most IOVA's are "temporary" - avg search time should be small.
  203. ** o keep a history of what happened for debugging
  204. ** o KISS.
  205. **
  206. ** Perf optimizations:
  207. ** o search for log2(size) bits at a time.
  208. ** o search for available resource bits using byte/word/whatever.
  209. ** o use different search for "large" (eg > 4 pages) or "very large"
  210. **   (eg > 16 pages) mappings.
  211. */
  212. /**
  213.  * ccio_alloc_range - Allocate pages in the ioc's resource map.
  214.  * @ioc: The I/O Controller.
  215.  * @pages_needed: The requested number of pages to be mapped into the
  216.  * I/O Pdir...
  217.  *
  218.  * This function searches the resource map of the ioc to locate a range
  219.  * of available pages for the requested size.
  220.  */
  221. static int
  222. ccio_alloc_range(struct ioc *ioc, unsigned long pages_needed)
  223. {
  224. int res_idx;
  225. unsigned long mask;
  226. #ifdef CONFIG_PROC_FS
  227. unsigned long cr_start = mfctl(16);
  228. #endif
  229. ASSERT(pages_needed);
  230. ASSERT((pages_needed * IOVP_SIZE) <= DMA_CHUNK_SIZE);
  231. ASSERT(pages_needed <= BITS_PER_LONG);
  232. mask = ~(~0UL >> pages_needed);
  233.      
  234. DBG_RES("%s() size: %d pages_needed %d mask 0x%08lxn", 
  235. __FUNCTION__, size, pages_needed, mask);
  236. /*
  237. ** "seek and ye shall find"...praying never hurts either...
  238. ** ggg sacrifices another 710 to the computer gods.
  239. */
  240. if(pages_needed <= 8) {
  241. CCIO_FIND_FREE_MAPPING(ioc, res_idx, mask, 8);
  242. } else if(pages_needed <= 16) {
  243. CCIO_FIND_FREE_MAPPING(ioc, res_idx, mask, 16);
  244. } else if(pages_needed <= 32) {
  245. CCIO_FIND_FREE_MAPPING(ioc, res_idx, mask, 32);
  246. #ifdef __LP64__
  247. } else if(pages_needed <= 64) {
  248. CCIO_FIND_FREE_MAPPING(ioc, res_idx, mask, 64);
  249. #endif
  250. } else {
  251. panic(__FILE__ ": %s() Too many pages to map. pages_needed: %ldn", 
  252.       __FUNCTION__, pages_needed);
  253. }
  254. panic(__FILE__ ": %s() I/O MMU is out of mapping resources.n", 
  255.       __FUNCTION__);
  256. resource_found:
  257. DBG_RES("%s() res_idx %d mask 0x%08lx res_hint: %dn",
  258. __FUNCTION__, res_idx, mask, ioc->res_hint);
  259. #ifdef CONFIG_PROC_FS
  260. {
  261. unsigned long cr_end = mfctl(16);
  262. unsigned long tmp = cr_end - cr_start;
  263. /* check for roll over */
  264. cr_start = (cr_end < cr_start) ?  -(tmp) : (tmp);
  265. }
  266. ioc->avg_search[ioc->avg_idx++] = cr_start;
  267. ioc->avg_idx &= CCIO_SEARCH_SAMPLE - 1;
  268. ioc->used_pages += pages_needed;
  269. #endif
  270. /* 
  271. ** return the bit address.
  272. */
  273. return res_idx << 3;
  274. }
  275. #define CCIO_FREE_MAPPINGS(ioc, res_idx, mask, size) 
  276.         u##size *res_ptr = (u##size *)&((ioc)->res_map[res_idx]); 
  277. u##size *mask_ptr = (u##size *)&mask; 
  278.         ASSERT((*res_ptr & *mask_ptr) == *mask_ptr); 
  279.         *res_ptr &= ~(*mask_ptr);
  280. /**
  281.  * ccio_free_range - Free pages from the ioc's resource map.
  282.  * @ioc: The I/O Controller.
  283.  * @iova: The I/O Virtual Address.
  284.  * @pages_mapped: The requested number of pages to be freed from the
  285.  * I/O Pdir.
  286.  *
  287.  * This function frees the resouces allocated for the iova.
  288.  */
  289. static void
  290. ccio_free_range(struct ioc *ioc, dma_addr_t iova, unsigned long pages_mapped)
  291. {
  292. unsigned long mask;
  293. unsigned long iovp = CCIO_IOVP(iova);
  294. unsigned int res_idx = PDIR_INDEX(iovp) >> 3;
  295. ASSERT(pages_mapped);
  296. ASSERT((pages_mapped * IOVP_SIZE) <= DMA_CHUNK_SIZE);
  297. ASSERT(pages_mapped <= BITS_PER_LONG);
  298. mask = ~(~0UL >> pages_mapped);
  299. DBG_RES("%s():  res_idx: %d pages_mapped %d mask 0x%08lxn", 
  300. __FUNCTION__, res_idx, pages_mapped, mask);
  301. #ifdef CONFIG_PROC_FS
  302. ioc->used_pages -= pages_mapped;
  303. #endif
  304. if(pages_mapped <= 8) {
  305. CCIO_FREE_MAPPINGS(ioc, res_idx, mask, 8);
  306. } else if(pages_mapped <= 16) {
  307. CCIO_FREE_MAPPINGS(ioc, res_idx, mask, 16);
  308. } else if(pages_mapped <= 32) {
  309. CCIO_FREE_MAPPINGS(ioc, res_idx, mask, 32);
  310. #ifdef __LP64__
  311. } else if(pages_mapped <= 64) {
  312. CCIO_FREE_MAPPINGS(ioc, res_idx, mask, 64);
  313. #endif
  314. } else {
  315. panic(__FILE__ ":%s() Too many pages to unmap.n", 
  316.       __FUNCTION__);
  317. }
  318. }
  319. /****************************************************************
  320. **
  321. **          CCIO dma_ops support routines
  322. **
  323. *****************************************************************/
  324. typedef unsigned long space_t;
  325. #define KERNEL_SPACE 0
  326. /*
  327. ** DMA "Page Type" and Hints 
  328. ** o if SAFE_DMA isn't set, mapping is for FAST_DMA. SAFE_DMA should be
  329. **   set for subcacheline DMA transfers since we don't want to damage the
  330. **   other part of a cacheline.
  331. ** o SAFE_DMA must be set for "memory" allocated via pci_alloc_consistent().
  332. **   This bit tells U2 to do R/M/W for partial cachelines. "Streaming"
  333. **   data can avoid this if the mapping covers full cache lines.
  334. ** o STOP_MOST is needed for atomicity across cachelines.
  335. **   Apperently only "some EISA devices" need this.
  336. **   Using CONFIG_ISA is hack. Only the IOA with EISA under it needs
  337. **   to use this hint iff the EISA devices needs this feature.
  338. **   According to the U2 ERS, STOP_MOST enabled pages hurt performance.
  339. ** o PREFETCH should *not* be set for cases like Multiple PCI devices
  340. **   behind GSCtoPCI (dino) bus converter. Only one cacheline per GSC
  341. **   device can be fetched and multiply DMA streams will thrash the
  342. **   prefetch buffer and burn memory bandwidth. See 6.7.3 "Prefetch Rules
  343. **   and Invalidation of Prefetch Entries".
  344. **
  345. ** FIXME: the default hints need to be per GSC device - not global.
  346. ** 
  347. ** HP-UX dorks: linux device driver programming model is totally different
  348. **    than HP-UX's. HP-UX always sets HINT_PREFETCH since it's drivers
  349. **    do special things to work on non-coherent platforms...linux has to
  350. **    be much more careful with this.
  351. */
  352. #define IOPDIR_VALID    0x01UL
  353. #define HINT_SAFE_DMA   0x02UL /* used for pci_alloc_consistent() pages */
  354. #ifdef CONFIG_ISA /* EISA support really */
  355. #define HINT_STOP_MOST  0x04UL /* LSL support */
  356. #else
  357. #define HINT_STOP_MOST  0x00UL /* only needed for "some EISA devices" */
  358. #endif
  359. #define HINT_UDPATE_ENB 0x08UL  /* not used/supported by U2 */
  360. #define HINT_PREFETCH   0x10UL /* for outbound pages which are not SAFE */
  361. /*
  362. ** Use direction (ie PCI_DMA_TODEVICE) to pick hint.
  363. ** ccio_alloc_consistent() depends on this to get SAFE_DMA
  364. ** when it passes in BIDIRECTIONAL flag.
  365. */
  366. static u32 hint_lookup[] = {
  367. [PCI_DMA_BIDIRECTIONAL]  HINT_STOP_MOST | HINT_SAFE_DMA | IOPDIR_VALID,
  368. [PCI_DMA_TODEVICE]       HINT_STOP_MOST | HINT_PREFETCH | IOPDIR_VALID,
  369. [PCI_DMA_FROMDEVICE]     HINT_STOP_MOST | IOPDIR_VALID,
  370. [PCI_DMA_NONE]           0,            /* not valid */
  371. };
  372. /**
  373.  * ccio_io_pdir_entry - Initialize an I/O Pdir.
  374.  * @pdir_ptr: A pointer into I/O Pdir.
  375.  * @sid: The Space Identifier.
  376.  * @vba: The virtual address.
  377.  * @hints: The DMA Hint.
  378.  *
  379.  * Given a virtual address (vba, arg2) and space id, (sid, arg1),
  380.  * load the I/O PDIR entry pointed to by pdir_ptr (arg0). Each IO Pdir
  381.  * entry consists of 8 bytes as shown below (MSB == bit 0):
  382.  *
  383.  *
  384.  * WORD 0:
  385.  * +------+----------------+-----------------------------------------------+
  386.  * | Phys | Virtual Index  |               Phys                            |
  387.  * | 0:3  |     0:11       |               4:19                            |
  388.  * |4 bits|   12 bits      |              16 bits                          |
  389.  * +------+----------------+-----------------------------------------------+
  390.  * WORD 1:
  391.  * +-----------------------+-----------------------------------------------+
  392.  * |      Phys    |  Rsvd  | Prefetch |Update |Rsvd  |Lock  |Safe  |Valid  |
  393.  * |     20:39    |        | Enable   |Enable |      |Enable|DMA   |       |
  394.  * |    20 bits   | 5 bits | 1 bit    |1 bit  |2 bits|1 bit |1 bit |1 bit  |
  395.  * +-----------------------+-----------------------------------------------+
  396.  *
  397.  * The virtual index field is filled with the results of the LCI
  398.  * (Load Coherence Index) instruction.  The 8 bits used for the virtual
  399.  * index are bits 12:19 of the value returned by LCI.
  400.  */ 
  401. void CCIO_INLINE
  402. ccio_io_pdir_entry(u64 *pdir_ptr, space_t sid, void * vba, unsigned long hints)
  403. {
  404. register unsigned long pa = (volatile unsigned long) vba;
  405. register unsigned long ci; /* coherent index */
  406. /* We currently only support kernel addresses */
  407. ASSERT(sid == KERNEL_SPACE);
  408. mtsp(sid,1);
  409. /*
  410. ** WORD 1 - low order word
  411. ** "hints" parm includes the VALID bit!
  412. ** "dep" clobbers the physical address offset bits as well.
  413. */
  414. pa = virt_to_phys(vba);
  415. asm volatile("depw  %1,31,12,%0" : "+r" (pa) : "r" (hints));
  416. ((u32 *)pdir_ptr)[1] = (u32) pa;
  417. /*
  418. ** WORD 0 - high order word
  419. */
  420. #ifdef __LP64__
  421. /*
  422. ** get bits 12:15 of physical address
  423. ** shift bits 16:31 of physical address
  424. ** and deposit them
  425. */
  426. asm volatile ("extrd,u %1,15,4,%0" : "=r" (ci) : "r" (pa));
  427. asm volatile ("extrd,u %1,31,16,%0" : "+r" (pa) : "r" (pa));
  428. asm volatile ("depd  %1,35,4,%0" : "+r" (pa) : "r" (ci));
  429. #else
  430. pa = 0;
  431. #endif
  432. /*
  433. ** get CPU coherency index bits
  434. ** Grab virtual index [0:11]
  435. ** Deposit virt_idx bits into I/O PDIR word
  436. */
  437. asm volatile ("lci 0(%%sr1, %1), %0" : "=r" (ci) : "r" (vba));
  438. asm volatile ("extru %1,19,12,%0" : "+r" (ci) : "r" (ci));
  439. asm volatile ("depw  %1,15,12,%0" : "+r" (pa) : "r" (ci));
  440. ((u32 *)pdir_ptr)[0] = (u32) pa;
  441. /* FIXME: PCX_W platforms don't need FDC/SYNC. (eg C360)
  442. **        PCX-U/U+ do. (eg C200/C240)
  443. **        PCX-T'? Don't know. (eg C110 or similar K-class)
  444. **
  445. ** See PDC_MODEL/option 0/SW_CAP word for "Non-coherent IO-PDIR bit".
  446. ** Hopefully we can patch (NOP) these out at boot time somehow.
  447. **
  448. ** "Since PCX-U employs an offset hash that is incompatible with
  449. ** the real mode coherence index generation of U2, the PDIR entry
  450. ** must be flushed to memory to retain coherence."
  451. */
  452. asm volatile("fdc 0(%0)" : : "r" (pdir_ptr));
  453. asm volatile("sync");
  454. }
  455. /**
  456.  * ccio_clear_io_tlb - Remove stale entries from the I/O TLB.
  457.  * @ioc: The I/O Controller.
  458.  * @iovp: The I/O Virtual Page.
  459.  * @byte_cnt: The requested number of bytes to be freed from the I/O Pdir.
  460.  *
  461.  * Purge invalid I/O PDIR entries from the I/O TLB.
  462.  *
  463.  * FIXME: Can we change the byte_cnt to pages_mapped?
  464.  */
  465. static CCIO_INLINE void
  466. ccio_clear_io_tlb(struct ioc *ioc, dma_addr_t iovp, size_t byte_cnt)
  467. {
  468. u32 chain_size = 1 << ioc->chainid_shift;
  469. iovp &= IOVP_MASK; /* clear offset bits, just want pagenum */
  470. byte_cnt += chain_size;
  471. while(byte_cnt > chain_size) {
  472. WRITE_U32(CMD_TLB_PURGE | iovp, &ioc->ioc_hpa->io_command);
  473. iovp += chain_size;
  474. byte_cnt -= chain_size;
  475.       }
  476. }
  477. /**
  478.  * ccio_mark_invalid - Mark the I/O Pdir entries invalid.
  479.  * @ioc: The I/O Controller.
  480.  * @iova: The I/O Virtual Address.
  481.  * @byte_cnt: The requested number of bytes to be freed from the I/O Pdir.
  482.  *
  483.  * Mark the I/O Pdir entries invalid and blow away the corresponding I/O
  484.  * TLB entries.
  485.  *
  486.  * FIXME: at some threshhold it might be "cheaper" to just blow
  487.  *        away the entire I/O TLB instead of individual entries.
  488.  *
  489.  * FIXME: Uturn has 256 TLB entries. We don't need to purge every
  490.  *        PDIR entry - just once for each possible TLB entry.
  491.  *        (We do need to maker I/O PDIR entries invalid regardless).
  492.  *
  493.  * FIXME: Can we change byte_cnt to pages_mapped?
  494.  */ 
  495. static CCIO_INLINE void
  496. ccio_mark_invalid(struct ioc *ioc, dma_addr_t iova, size_t byte_cnt)
  497. {
  498. u32 iovp = (u32)CCIO_IOVP(iova);
  499. size_t saved_byte_cnt;
  500. /* round up to nearest page size */
  501. saved_byte_cnt = byte_cnt = ROUNDUP(byte_cnt, IOVP_SIZE);
  502. while(byte_cnt > 0) {
  503. /* invalidate one page at a time */
  504. unsigned int idx = PDIR_INDEX(iovp);
  505. char *pdir_ptr = (char *) &(ioc->pdir_base[idx]);
  506. ASSERT(idx < (ioc->pdir_size / sizeof(u64)));
  507. pdir_ptr[7] = 0; /* clear only VALID bit */ 
  508. /*
  509. ** FIXME: PCX_W platforms don't need FDC/SYNC. (eg C360)
  510. **   PCX-U/U+ do. (eg C200/C240)
  511. ** See PDC_MODEL/option 0/SW_CAP for "Non-coherent IO-PDIR bit".
  512. **
  513. ** Hopefully someone figures out how to patch (NOP) the
  514. ** FDC/SYNC out at boot time.
  515. */
  516. asm volatile("fdc 0(%0)" : : "r" (pdir_ptr[7]));
  517. iovp     += IOVP_SIZE;
  518. byte_cnt -= IOVP_SIZE;
  519. }
  520. asm volatile("sync");
  521. ccio_clear_io_tlb(ioc, CCIO_IOVP(iova), saved_byte_cnt);
  522. }
  523. /****************************************************************
  524. **
  525. **          CCIO dma_ops
  526. **
  527. *****************************************************************/
  528. /**
  529.  * ccio_dma_supported - Verify the IOMMU supports the DMA address range.
  530.  * @dev: The PCI device.
  531.  * @mask: A bit mask describing the DMA address range of the device.
  532.  *
  533.  * This function implements the pci_dma_supported function.
  534.  */
  535. static int 
  536. ccio_dma_supported(struct pci_dev *dev, u64 mask)
  537. {
  538. if(dev == NULL) {
  539. printk(KERN_ERR MODULE_NAME ": EISA/ISA/et al not supportedn");
  540. BUG();
  541. return 0;
  542. }
  543. dev->dma_mask = mask;   /* save it */
  544. /* only support 32-bit devices (ie PCI/GSC) */
  545. return (int)(mask == 0xffffffffUL);
  546. }
  547. /**
  548.  * ccio_map_single - Map an address range into the IOMMU.
  549.  * @dev: The PCI device.
  550.  * @addr: The start address of the DMA region.
  551.  * @size: The length of the DMA region.
  552.  * @direction: The direction of the DMA transaction (to/from device).
  553.  *
  554.  * This function implements the pci_map_single function.
  555.  */
  556. static dma_addr_t 
  557. ccio_map_single(struct pci_dev *dev, void *addr, size_t size, int direction)
  558. {
  559. int idx;
  560. struct ioc *ioc;
  561. unsigned long flags;
  562. dma_addr_t iovp;
  563. dma_addr_t offset;
  564. u64 *pdir_start;
  565. unsigned long hint = hint_lookup[direction];
  566. ASSERT(dev);
  567. ASSERT(dev->sysdata);
  568. ASSERT(HBA_DATA(dev->sysdata)->iommu);
  569. ioc = GET_IOC(dev);
  570. ASSERT(size > 0);
  571. /* save offset bits */
  572. offset = ((unsigned long) addr) & ~IOVP_MASK;
  573. /* round up to nearest IOVP_SIZE */
  574. size = ROUNDUP(size + offset, IOVP_SIZE);
  575. spin_lock_irqsave(&ioc->res_lock, flags);
  576. #ifdef CONFIG_PROC_FS
  577. ioc->msingle_calls++;
  578. ioc->msingle_pages += size >> IOVP_SHIFT;
  579. #endif
  580. idx = ccio_alloc_range(ioc, (size >> IOVP_SHIFT));
  581. iovp = (dma_addr_t)MKIOVP(idx);
  582. pdir_start = &(ioc->pdir_base[idx]);
  583. DBG_RUN("%s() 0x%p -> 0x%lx size: %0x%xn",
  584. __FUNCTION__, addr, (long)iovp | offset, size);
  585. /* If not cacheline aligned, force SAFE_DMA on the whole mess */
  586. if((size % L1_CACHE_BYTES) || ((unsigned long)addr % L1_CACHE_BYTES))
  587. hint |= HINT_SAFE_DMA;
  588. while(size > 0) {
  589. ccio_io_pdir_entry(pdir_start, KERNEL_SPACE, addr, hint);
  590. DBG_RUN(" pdir %p %08x%08xn",
  591. pdir_start,
  592. (u32) (((u32 *) pdir_start)[0]),
  593. (u32) (((u32 *) pdir_start)[1]));
  594. ++pdir_start;
  595. addr += IOVP_SIZE;
  596. size -= IOVP_SIZE;
  597. }
  598. spin_unlock_irqrestore(&ioc->res_lock, flags);
  599. /* form complete address */
  600. return CCIO_IOVA(iovp, offset);
  601. }
  602. /**
  603.  * ccio_unmap_single - Unmap an address range from the IOMMU.
  604.  * @dev: The PCI device.
  605.  * @addr: The start address of the DMA region.
  606.  * @size: The length of the DMA region.
  607.  * @direction: The direction of the DMA transaction (to/from device).
  608.  *
  609.  * This function implements the pci_unmap_single function.
  610.  */
  611. static void 
  612. ccio_unmap_single(struct pci_dev *dev, dma_addr_t iova, size_t size, 
  613.   int direction)
  614. {
  615. struct ioc *ioc;
  616. unsigned long flags; 
  617. dma_addr_t offset = iova & ~IOVP_MASK;
  618. ASSERT(dev);
  619. ASSERT(dev->sysdata);
  620. ASSERT(HBA_DATA(dev->sysdata)->iommu);
  621. ioc = GET_IOC(dev);
  622. DBG_RUN("%s() iovp 0x%lx/%xn",
  623. __FUNCTION__, (long)iova, size);
  624. iova ^= offset;        /* clear offset bits */
  625. size += offset;
  626. size = ROUNDUP(size, IOVP_SIZE);
  627. spin_lock_irqsave(&ioc->res_lock, flags);
  628. #ifdef CONFIG_PROC_FS
  629. ioc->usingle_calls++;
  630. ioc->usingle_pages += size >> IOVP_SHIFT;
  631. #endif
  632. ccio_mark_invalid(ioc, iova, size);
  633. ccio_free_range(ioc, iova, (size >> IOVP_SHIFT));
  634. spin_unlock_irqrestore(&ioc->res_lock, flags);
  635. }
  636. /**
  637.  * ccio_alloc_consistent - Allocate a consistent DMA mapping.
  638.  * @dev: The PCI device.
  639.  * @size: The length of the DMA region.
  640.  * @dma_handle: The DMA address handed back to the device (not the cpu).
  641.  *
  642.  * This function implements the pci_alloc_consistent function.
  643.  */
  644. static void * 
  645. ccio_alloc_consistent(struct pci_dev *dev, size_t size, dma_addr_t *dma_handle)
  646. {
  647.       void *ret;
  648. #if 0
  649. /* GRANT Need to establish hierarchy for non-PCI devs as well
  650. ** and then provide matching gsc_map_xxx() functions for them as well.
  651. */
  652. if(!hwdev) {
  653. /* only support PCI */
  654. *dma_handle = 0;
  655. return 0;
  656. }
  657. #endif
  658.         ret = (void *) __get_free_pages(GFP_ATOMIC, get_order(size));
  659. if (ret) {
  660. memset(ret, 0, size);
  661. *dma_handle = ccio_map_single(dev, ret, size, PCI_DMA_BIDIRECTIONAL);
  662. }
  663. return ret;
  664. }
  665. /**
  666.  * ccio_free_consistent - Free a consistent DMA mapping.
  667.  * @dev: The PCI device.
  668.  * @size: The length of the DMA region.
  669.  * @cpu_addr: The cpu address returned from the ccio_alloc_consistent.
  670.  * @dma_handle: The device address returned from the ccio_alloc_consistent.
  671.  *
  672.  * This function implements the pci_free_consistent function.
  673.  */
  674. static void 
  675. ccio_free_consistent(struct pci_dev *dev, size_t size, void *cpu_addr, 
  676.      dma_addr_t dma_handle)
  677. {
  678. ccio_unmap_single(dev, dma_handle, size, 0);
  679. free_pages((unsigned long)cpu_addr, get_order(size));
  680. }
  681. /*
  682. ** Since 0 is a valid pdir_base index value, can't use that
  683. ** to determine if a value is valid or not. Use a flag to indicate
  684. ** the SG list entry contains a valid pdir index.
  685. */
  686. #define PIDE_FLAG 0x80000000UL
  687. /**
  688.  * ccio_fill_pdir - Insert coalesced scatter/gather chunks into the I/O Pdir.
  689.  * @ioc: The I/O Controller.
  690.  * @startsg: The scatter/gather list of coalesced chunks.
  691.  * @nents: The number of entries in the scatter/gather list.
  692.  * @hint: The DMA Hint.
  693.  *
  694.  * This function inserts the coalesced scatter/gather list chunks into the
  695.  * I/O Controller's I/O Pdir.
  696.  */ 
  697. static CCIO_INLINE int
  698. ccio_fill_pdir(struct ioc *ioc, struct scatterlist *startsg, int nents, 
  699.        unsigned long hint)
  700. {
  701. struct scatterlist *dma_sg = startsg; /* pointer to current DMA */
  702. int n_mappings = 0;
  703. u64 *pdirp = 0;
  704. unsigned long dma_offset = 0;
  705. dma_sg--;
  706. while (nents-- > 0) {
  707. int cnt = sg_dma_len(startsg);
  708. sg_dma_len(startsg) = 0;
  709. DBG_RUN_SG(" %d : %08lx/%05x %p/%05xn", nents,
  710.    (unsigned long)sg_dma_address(startsg), cnt,
  711.    startsg->address, startsg->length
  712. );
  713. /*
  714. ** Look for the start of a new DMA stream
  715. */
  716. if(sg_dma_address(startsg) & PIDE_FLAG) {
  717. u32 pide = sg_dma_address(startsg) & ~PIDE_FLAG;
  718. dma_offset = (unsigned long) pide & ~IOVP_MASK;
  719. sg_dma_address(startsg) = 0;
  720. dma_sg++;
  721. sg_dma_address(dma_sg) = pide;
  722. pdirp = &(ioc->pdir_base[pide >> IOVP_SHIFT]);
  723. n_mappings++;
  724. }
  725. /*
  726. ** Look for a VCONTIG chunk
  727. */
  728. if (cnt) {
  729. unsigned long vaddr = (unsigned long)startsg->address;
  730. ASSERT(pdirp);
  731. /* Since multiple Vcontig blocks could make up
  732. ** one DMA stream, *add* cnt to dma_len.
  733. */
  734. sg_dma_len(dma_sg) += cnt;
  735. cnt += dma_offset;
  736. dma_offset=0; /* only want offset on first chunk */
  737. cnt = ROUNDUP(cnt, IOVP_SIZE);
  738. #ifdef CONFIG_PROC_FS
  739. ioc->msg_pages += cnt >> IOVP_SHIFT;
  740. #endif
  741. do {
  742. ccio_io_pdir_entry(pdirp, KERNEL_SPACE, 
  743.    (void *)vaddr, hint);
  744. vaddr += IOVP_SIZE;
  745. cnt -= IOVP_SIZE;
  746. pdirp++;
  747. } while (cnt > 0);
  748. }
  749. startsg++;
  750. }
  751. return(n_mappings);
  752. }
  753. /*
  754. ** First pass is to walk the SG list and determine where the breaks are
  755. ** in the DMA stream. Allocates PDIR entries but does not fill them.
  756. ** Returns the number of DMA chunks.
  757. **
  758. ** Doing the fill seperate from the coalescing/allocation keeps the
  759. ** code simpler. Future enhancement could make one pass through
  760. ** the sglist do both.
  761. */
  762. static CCIO_INLINE int
  763. ccio_coalesce_chunks(struct ioc *ioc, struct scatterlist *startsg, int nents)
  764. {
  765. struct scatterlist *vcontig_sg;    /* VCONTIG chunk head */
  766. unsigned long vcontig_len;         /* len of VCONTIG chunk */
  767. unsigned long vcontig_end;
  768. struct scatterlist *dma_sg;        /* next DMA stream head */
  769. unsigned long dma_offset, dma_len; /* start/len of DMA stream */
  770. int n_mappings = 0;
  771. while (nents > 0) {
  772. /*
  773. ** Prepare for first/next DMA stream
  774. */
  775. dma_sg = vcontig_sg = startsg;
  776. dma_len = vcontig_len = vcontig_end = startsg->length;
  777. vcontig_end += (unsigned long) startsg->address;
  778. dma_offset = (unsigned long) startsg->address & ~IOVP_MASK;
  779. /* PARANOID: clear entries */
  780. sg_dma_address(startsg) = 0;
  781. sg_dma_len(startsg) = 0;
  782. /*
  783. ** This loop terminates one iteration "early" since
  784. ** it's always looking one "ahead".
  785. */
  786. while(--nents > 0) {
  787. unsigned long startsg_end;
  788. startsg++;
  789. startsg_end = (unsigned long)startsg->address + 
  790. startsg->length;
  791. /* PARANOID: clear entries */
  792. sg_dma_address(startsg) = 0;
  793. sg_dma_len(startsg) = 0;
  794. /*
  795. ** First make sure current dma stream won't
  796. ** exceed DMA_CHUNK_SIZE if we coalesce the
  797. ** next entry.
  798. */   
  799. if(ROUNDUP(dma_len + dma_offset + startsg->length,
  800.    IOVP_SIZE) > DMA_CHUNK_SIZE)
  801. break;
  802. /*
  803. ** Append the next transaction?
  804. */
  805. if(vcontig_end == (unsigned long) startsg->address) {
  806. vcontig_len += startsg->length;
  807. vcontig_end += startsg->length;
  808. dma_len     += startsg->length;
  809. continue;
  810. }
  811. /*
  812. ** Not virtually contigous.
  813. ** Terminate prev chunk.
  814. ** Start a new chunk.
  815. **
  816. ** Once we start a new VCONTIG chunk, dma_offset
  817. ** can't change. And we need the offset from the first
  818. ** chunk - not the last one. Ergo Successive chunks
  819. ** must start on page boundaries and dove tail
  820. ** with it's predecessor.
  821. */
  822. sg_dma_len(vcontig_sg) = vcontig_len;
  823. vcontig_sg = startsg;
  824. vcontig_len = startsg->length;
  825. break;
  826. }
  827. /*
  828. ** End of DMA Stream
  829. ** Terminate last VCONTIG block.
  830. ** Allocate space for DMA stream.
  831. */
  832. sg_dma_len(vcontig_sg) = vcontig_len;
  833. dma_len = ROUNDUP(dma_len + dma_offset, IOVP_SIZE);
  834. sg_dma_address(dma_sg) =
  835. PIDE_FLAG 
  836. | (ccio_alloc_range(ioc, (dma_len >> IOVP_SHIFT)) << IOVP_SHIFT)
  837. | dma_offset;
  838. n_mappings++;
  839. }
  840. return n_mappings;
  841. }
  842. /**
  843.  * ccio_map_sg - Map the scatter/gather list into the IOMMU.
  844.  * @dev: The PCI device.
  845.  * @sglist: The scatter/gather list to be mapped in the IOMMU.
  846.  * @nents: The number of entries in the scatter/gather list.
  847.  * @direction: The direction of the DMA transaction (to/from device).
  848.  *
  849.  * This function implements the pci_map_sg function.
  850.  */
  851. static int
  852. ccio_map_sg(struct pci_dev *dev, struct scatterlist *sglist, int nents, 
  853.     int direction)
  854. {
  855. struct ioc *ioc;
  856. int coalesced, filled = 0;
  857. unsigned long flags;
  858. unsigned long hint = hint_lookup[direction];
  859. ASSERT(dev);
  860. ASSERT(dev->sysdata);
  861. ASSERT(HBA_DATA(dev->sysdata)->iommu);
  862. ioc = GET_IOC(dev);
  863. DBG_RUN_SG("%s() START %d entriesn", __FUNCTION__, nents);
  864. /* Fast path single entry scatterlists. */
  865. if(nents == 1) {
  866. sg_dma_address(sglist)= ccio_map_single(dev, sglist->address,
  867. sglist->length, 
  868. direction);
  869. sg_dma_len(sglist)= sglist->length;
  870. return 1;
  871. }
  872. spin_lock_irqsave(&ioc->res_lock, flags);
  873. #ifdef CONFIG_PROC_FS
  874. ioc->msg_calls++;
  875. #endif
  876. /*
  877. ** First coalesce the chunks and allocate I/O pdir space
  878. **
  879. ** If this is one DMA stream, we can properly map using the
  880. ** correct virtual address associated with each DMA page.
  881. ** w/o this association, we wouldn't have coherent DMA!
  882. ** Access to the virtual address is what forces a two pass algorithm.
  883. */
  884. coalesced = ccio_coalesce_chunks(ioc, sglist, nents);
  885. /*
  886. ** Program the I/O Pdir
  887. **
  888. ** map the virtual addresses to the I/O Pdir
  889. ** o dma_address will contain the pdir index
  890. ** o dma_len will contain the number of bytes to map 
  891. ** o address contains the virtual address.
  892. */
  893. filled = ccio_fill_pdir(ioc, sglist, nents, hint);
  894. spin_unlock_irqrestore(&ioc->res_lock, flags);
  895. ASSERT(coalesced == filled);
  896. DBG_RUN_SG("%s() DONE %d mappingsn", __FUNCTION__, filled);
  897. return filled;
  898. }
  899. /**
  900.  * ccio_unmap_sg - Unmap the scatter/gather list from the IOMMU.
  901.  * @dev: The PCI device.
  902.  * @sglist: The scatter/gather list to be unmapped from the IOMMU.
  903.  * @nents: The number of entries in the scatter/gather list.
  904.  * @direction: The direction of the DMA transaction (to/from device).
  905.  *
  906.  * This function implements the pci_unmap_sg function.
  907.  */
  908. static void 
  909. ccio_unmap_sg(struct pci_dev *dev, struct scatterlist *sglist, int nents, 
  910.       int direction)
  911. {
  912. struct ioc *ioc;
  913. ASSERT(dev);
  914. ASSERT(dev->sysdata);
  915. ASSERT(HBA_DATA(dev->sysdata)->iommu);
  916. ioc = GET_IOC(dev);
  917. DBG_RUN_SG("%s() START %d entries,  %p,%xn",
  918. __FUNCTION__, nents, sglist->address, sglist->length);
  919. #ifdef CONFIG_PROC_FS
  920. ioc->usg_calls++;
  921. #endif
  922. while(sg_dma_len(sglist) && nents--) {
  923. #ifdef CONFIG_PROC_FS
  924. ioc->usg_pages += sg_dma_len(sglist) >> PAGE_SHIFT;
  925. #endif
  926. ccio_unmap_single(dev, sg_dma_address(sglist),
  927.   sg_dma_len(sglist), direction);
  928. ++sglist;
  929. }
  930. DBG_RUN_SG("%s() DONE (nents %d)n", __FUNCTION__, nents);
  931. }
  932. static struct pci_dma_ops ccio_ops = {
  933. ccio_dma_supported,
  934. ccio_alloc_consistent,
  935. ccio_free_consistent,
  936. ccio_map_single,
  937. ccio_unmap_single,
  938. ccio_map_sg,
  939. ccio_unmap_sg,
  940. NULL,                   /* dma_sync_single : NOP for U2/Uturn */
  941. NULL,                   /* dma_sync_sg     : ditto */
  942. };
  943. #ifdef CONFIG_PROC_FS
  944. static int proc_append(char *src, int len, char **dst, off_t *offset, int *max)
  945. {
  946. if (len < *offset) {
  947. *offset -= len;
  948. return 0;
  949. }
  950. if (*offset > 0) {
  951. src += *offset;
  952. len -= *offset;
  953. *offset = 0;
  954. }
  955. if (len > *max) {
  956. len = *max;
  957. }
  958. memcpy(*dst, src, len);
  959. *dst += len;
  960. *max -= len;
  961. return (*max == 0);
  962. }
  963. static int ccio_proc_info(char *buf, char **start, off_t offset, int count,
  964.   int *eof, void *data)
  965. {
  966. int max = count;
  967. char tmp[80]; /* width of an ANSI-standard terminal */
  968. struct ioc *ioc = ioc_list;
  969. while (ioc != NULL) {
  970. unsigned int total_pages = ioc->res_size << 3;
  971. unsigned long avg = 0, min, max;
  972. int j, len;
  973. len = sprintf(tmp, "%sn", ioc->name);
  974. if (proc_append(tmp, len, &buf, &offset, &count))
  975. break;
  976. len = sprintf(tmp, "Cujo 2.0 bug    : %sn",
  977.       (ioc->cujo20_bug ? "yes" : "no"));
  978. if (proc_append(tmp, len, &buf, &offset, &count))
  979. break;
  980. len = sprintf(tmp, "IO PDIR size    : %d bytes (%d entries)n",
  981.       total_pages * 8, total_pages);
  982. if (proc_append(tmp, len, &buf, &offset, &count))
  983. break;
  984. len = sprintf(tmp, "IO PDIR entries : %ld free  %ld used (%d%%)n",
  985.       total_pages - ioc->used_pages, ioc->used_pages,
  986.       (int)(ioc->used_pages * 100 / total_pages));
  987. if (proc_append(tmp, len, &buf, &offset, &count))
  988. break;
  989. len = sprintf(tmp, "Resource bitmap : %d bytes (%d pages)n", 
  990. ioc->res_size, total_pages);
  991. if (proc_append(tmp, len, &buf, &offset, &count))
  992. break;
  993. min = max = ioc->avg_search[0];
  994. for(j = 0; j < CCIO_SEARCH_SAMPLE; ++j) {
  995. avg += ioc->avg_search[j];
  996. if(ioc->avg_search[j] > max) 
  997. max = ioc->avg_search[j];
  998. if(ioc->avg_search[j] < min) 
  999. min = ioc->avg_search[j];
  1000. }
  1001. avg /= CCIO_SEARCH_SAMPLE;
  1002. len = sprintf(tmp, "  Bitmap search : %ld/%ld/%ld (min/avg/max CPU Cycles)n",
  1003.       min, avg, max);
  1004. if (proc_append(tmp, len, &buf, &offset, &count))
  1005. break;
  1006. len = sprintf(tmp, "pci_map_single(): %8ld calls  %8ld pages (avg %d/1000)n",
  1007.       ioc->msingle_calls, ioc->msingle_pages,
  1008.       (int)((ioc->msingle_pages * 1000)/ioc->msingle_calls));
  1009. if (proc_append(tmp, len, &buf, &offset, &count))
  1010. break;
  1011. /* KLUGE - unmap_sg calls unmap_single for each mapped page */
  1012. min = ioc->usingle_calls - ioc->usg_calls;
  1013. max = ioc->usingle_pages - ioc->usg_pages;
  1014. len = sprintf(tmp, "pci_unmap_single: %8ld calls  %8ld pages (avg %d/1000)n",
  1015.       min, max, (int)((max * 1000)/min));
  1016. if (proc_append(tmp, len, &buf, &offset, &count))
  1017. break;
  1018.  
  1019. len = sprintf(tmp, "pci_map_sg()    : %8ld calls  %8ld pages (avg %d/1000)n",
  1020.       ioc->msg_calls, ioc->msg_pages,
  1021.       (int)((ioc->msg_pages * 1000)/ioc->msg_calls));
  1022. if (proc_append(tmp, len, &buf, &offset, &count))
  1023. break;
  1024. len = sprintf(tmp, "pci_unmap_sg()  : %8ld calls  %8ld pages (avg %d/1000)nnn",
  1025.       ioc->usg_calls, ioc->usg_pages,
  1026.       (int)((ioc->usg_pages * 1000)/ioc->usg_calls));
  1027. if (proc_append(tmp, len, &buf, &offset, &count))
  1028. break;
  1029. ioc = ioc->next;
  1030. }
  1031. if (count == 0) {
  1032. *eof = 1;
  1033. }
  1034. return (max - count);
  1035. }
  1036. static int ccio_resource_map(char *buf, char **start, off_t offset, int len,
  1037.      int *eof, void *data)
  1038. {
  1039. struct ioc *ioc = ioc_list;
  1040. buf[0] = '';
  1041. while (ioc != NULL) {
  1042. u32 *res_ptr = (u32 *)ioc->res_map;
  1043. int j;
  1044. for (j = 0; j < (ioc->res_size / sizeof(u32)); j++) {
  1045. if ((j & 7) == 0)
  1046. strcat(buf,"n   ");
  1047. sprintf(buf, "%s %08x", buf, *res_ptr);
  1048. res_ptr++;
  1049. }
  1050. strcat(buf, "nn");
  1051. ioc = ioc->next;
  1052. break; /* XXX - remove me */
  1053. }
  1054. return strlen(buf);
  1055. }
  1056. #endif
  1057. /**
  1058.  * ccio_find_ioc - Find the ioc in the ioc_list
  1059.  * @hw_path: The hardware path of the ioc.
  1060.  *
  1061.  * This function searches the ioc_list for an ioc that matches
  1062.  * the provide hardware path.
  1063.  */
  1064. static struct ioc * ccio_find_ioc(int hw_path)
  1065. {
  1066. int i;
  1067. struct ioc *ioc;
  1068. ioc = ioc_list;
  1069. for (i = 0; i < ioc_count; i++) {
  1070. if (ioc->hw_path == hw_path)
  1071. return ioc;
  1072. ioc = ioc->next;
  1073. }
  1074. return NULL;
  1075. }
  1076. /**
  1077.  * ccio_get_iommu - Find the iommu which controls this device
  1078.  * @dev: The parisc device.
  1079.  *
  1080.  * This function searches through the registerd IOMMU's and returns the
  1081.  * appropriate IOMMU for the device based upon the devices hardware path.
  1082.  */
  1083. void * ccio_get_iommu(const struct parisc_device *dev)
  1084. {
  1085. dev = find_pa_parent_type(dev, HPHW_IOA);
  1086. if (!dev)
  1087. return NULL;
  1088. return ccio_find_ioc(dev->hw_path);
  1089. }
  1090. #define CUJO_20_STEP       0x10000000 /* inc upper nibble */
  1091. /* Cujo 2.0 has a bug which will silently corrupt data being transferred
  1092.  * to/from certain pages.  To avoid this happening, we mark these pages
  1093.  * as `used', and ensure that nothing will try to allocate from them.
  1094.  */
  1095. void ccio_cujo20_fixup(struct parisc_device *dev, u32 iovp)
  1096. {
  1097. unsigned int idx;
  1098. struct ioc *ioc = ccio_get_iommu(dev);
  1099. u8 *res_ptr;
  1100. #ifdef CONFIG_PROC_FS
  1101. ioc->cujo20_bug = 1;
  1102. #endif
  1103. res_ptr = ioc->res_map;
  1104. idx = PDIR_INDEX(iovp) >> 3;
  1105. while (idx < ioc->res_size) {
  1106.   res_ptr[idx] |= 0xff;
  1107. idx += PDIR_INDEX(CUJO_20_STEP) >> 3;
  1108. }
  1109. }
  1110. #if 0
  1111. /* GRANT -  is this needed for U2 or not? */
  1112. /*
  1113. ** Get the size of the I/O TLB for this I/O MMU.
  1114. **
  1115. ** If spa_shift is non-zero (ie probably U2),
  1116. ** then calculate the I/O TLB size using spa_shift.
  1117. **
  1118. ** Otherwise we are supposed to get the IODC entry point ENTRY TLB
  1119. ** and execute it. However, both U2 and Uturn firmware supplies spa_shift.
  1120. ** I think only Java (K/D/R-class too?) systems don't do this.
  1121. */
  1122. static int
  1123. ccio_get_iotlb_size(struct parisc_device *dev)
  1124. {
  1125. if (dev->spa_shift == 0) {
  1126. panic("%s() : Can't determine I/O TLB size.n", __FUNCTION__);
  1127. }
  1128. return (1 << dev->spa_shift);
  1129. }
  1130. #else
  1131. /* Uturn supports 256 TLB entries */
  1132. #define CCIO_CHAINID_SHIFT 8
  1133. #define CCIO_CHAINID_MASK 0xff
  1134. #endif /* 0 */
  1135. /**
  1136.  * ccio_ioc_init - Initalize the I/O Controller
  1137.  * @ioc: The I/O Controller.
  1138.  *
  1139.  * Initalize the I/O Controller which includes setting up the
  1140.  * I/O Page Directory, the resource map, and initalizing the
  1141.  * U2/Uturn chip into virtual mode.
  1142.  */
  1143. static void
  1144. ccio_ioc_init(struct ioc *ioc)
  1145. {
  1146. int i, iov_order;
  1147. u32 iova_space_size;
  1148. unsigned long physmem;
  1149. /*
  1150. ** Determine IOVA Space size from memory size.
  1151. **
  1152. ** Ideally, PCI drivers would register the maximum number
  1153. ** of DMA they can have outstanding for each device they
  1154. ** own.  Next best thing would be to guess how much DMA
  1155. ** can be outstanding based on PCI Class/sub-class. Both
  1156. ** methods still require some "extra" to support PCI
  1157. ** Hot-Plug/Removal of PCI cards. (aka PCI OLARD).
  1158. */
  1159. /* limit IOVA space size to 1MB-1GB */
  1160. physmem = num_physpages << PAGE_SHIFT;
  1161. if(physmem < (ccio_mem_ratio * 1024 * 1024)) {
  1162. iova_space_size = 1024 * 1024;
  1163. #ifdef __LP64__
  1164. } else if(physmem > (ccio_mem_ratio * 512 * 1024 * 1024)) {
  1165. iova_space_size = 512 * 1024 * 1024;
  1166. #endif
  1167. } else {
  1168. iova_space_size = (u32)(physmem / ccio_mem_ratio);
  1169. }
  1170. /*
  1171. ** iova space must be log2() in size.
  1172. ** thus, pdir/res_map will also be log2().
  1173. */
  1174. /* We could use larger page sizes in order to *decrease* the number
  1175. ** of mappings needed.  (ie 8k pages means 1/2 the mappings).
  1176. **
  1177. ** Note: Grant Grunder says "Using 8k I/O pages isn't trivial either
  1178. **   since the pages must also be physically contiguous - typically
  1179. **   this is the case under linux."
  1180. */
  1181. iov_order = get_order(iova_space_size) >> (IOVP_SHIFT - PAGE_SHIFT);
  1182. ASSERT(iov_order <= (30 - IOVP_SHIFT));   /* iova_space_size <= 1GB */
  1183. ASSERT(iov_order >= (20 - IOVP_SHIFT));   /* iova_space_size >= 1MB */
  1184. iova_space_size = 1 << (iov_order + IOVP_SHIFT);
  1185. ioc->pdir_size = (iova_space_size / IOVP_SIZE) * sizeof(u64);
  1186. ASSERT(ioc->pdir_size < 4 * 1024 * 1024);   /* max pdir size < 4MB */
  1187. /* Verify it's a power of two */
  1188. ASSERT((1 << get_order(ioc->pdir_size)) == (ioc->pdir_size >> PAGE_SHIFT));
  1189. DBG_INIT("%s() hpa 0x%p mem %luMB IOV %dMB (%d bits) PDIR size 0x%0x",
  1190. __FUNCTION__, ioc->ioc_hpa, physmem>>20, iova_space_size>>20,
  1191.  iov_order + PAGE_SHIFT, ioc->pdir_size);
  1192. ioc->pdir_base = (u64 *)__get_free_pages(GFP_KERNEL, 
  1193.  get_order(ioc->pdir_size));
  1194. if(NULL == ioc->pdir_base) {
  1195. panic(__FILE__ ":%s() could not allocate I/O Page Tablen", __FUNCTION__);
  1196. }
  1197. memset(ioc->pdir_base, 0, ioc->pdir_size);
  1198. ASSERT((((unsigned long)ioc->pdir_base) & PAGE_MASK) == (unsigned long)ioc->pdir_base);
  1199. DBG_INIT(" base %p", ioc->pdir_base);
  1200. /* resource map size dictated by pdir_size */
  1201.   ioc->res_size = (ioc->pdir_size / sizeof(u64)) >> 3;
  1202. DBG_INIT("%s() res_size 0x%xn", __FUNCTION__, ioc->res_size);
  1203. ioc->res_map = (u8 *)__get_free_pages(GFP_KERNEL, 
  1204.       get_order(ioc->res_size));
  1205. if(NULL == ioc->res_map) {
  1206. panic(__FILE__ ":%s() could not allocate resource mapn", __FUNCTION__);
  1207. }
  1208. memset(ioc->res_map, 0, ioc->res_size);
  1209. /* Initialize the res_hint to 16 */
  1210. ioc->res_hint = 16;
  1211. /* Initialize the spinlock */
  1212. spin_lock_init(&ioc->res_lock);
  1213. /*
  1214. ** Chainid is the upper most bits of an IOVP used to determine
  1215. ** which TLB entry an IOVP will use.
  1216. */
  1217. ioc->chainid_shift = get_order(iova_space_size) + PAGE_SHIFT - CCIO_CHAINID_SHIFT;
  1218. DBG_INIT(" chainid_shift 0x%xn", ioc->chainid_shift);
  1219. /*
  1220. ** Initialize IOA hardware
  1221. */
  1222. WRITE_U32(CCIO_CHAINID_MASK << ioc->chainid_shift, 
  1223.   &ioc->ioc_hpa->io_chain_id_mask);
  1224. WRITE_U32(virt_to_phys(ioc->pdir_base), 
  1225.   &ioc->ioc_hpa->io_pdir_base);
  1226. /*
  1227. ** Go to "Virtual Mode"
  1228. */
  1229. WRITE_U32(IOA_NORMAL_MODE, &ioc->ioc_hpa->io_control);
  1230. /*
  1231. ** Initialize all I/O TLB entries to 0 (Valid bit off).
  1232. */
  1233. WRITE_U32(0, &ioc->ioc_hpa->io_tlb_entry_m);
  1234. WRITE_U32(0, &ioc->ioc_hpa->io_tlb_entry_l);
  1235. for(i = 1 << CCIO_CHAINID_SHIFT; i ; i--) {
  1236. WRITE_U32((CMD_TLB_DIRECT_WRITE | (i << ioc->chainid_shift)),
  1237.   &ioc->ioc_hpa->io_command);
  1238. }
  1239. }
  1240. static void
  1241. ccio_init_resource(struct resource *res, char *name, unsigned long ioaddr)
  1242. {
  1243. int result;
  1244. res->flags = IORESOURCE_MEM;
  1245. res->start = (unsigned long)(signed) __raw_readl(ioaddr) << 16;
  1246. res->end = (unsigned long)(signed) (__raw_readl(ioaddr + 4) << 16) - 1;
  1247. if (res->end + 1 == res->start)
  1248. return;
  1249. res->name = name;
  1250. result = request_resource(&iomem_resource, res);
  1251. if (result < 0) {
  1252. printk(KERN_ERR 
  1253.        "%s: failed to claim CCIO bus address space (%p,%p)n", 
  1254.        __FILE__, res->start, res->end);
  1255. }
  1256. }
  1257. static void __init ccio_init_resources(struct ioc *ioc)
  1258. {
  1259. struct resource *res = ioc->mmio_region;
  1260. char *name = kmalloc(14, GFP_KERNEL);
  1261. sprintf(name, "GSC Bus [%d/]", ioc->hw_path);
  1262. ccio_init_resource(res, name, (unsigned long)&ioc->ioc_hpa->io_io_low);
  1263. ccio_init_resource(res + 1, name,
  1264. (unsigned long)&ioc->ioc_hpa->io_io_low_hv);
  1265. }
  1266. static void expand_ioc_area(struct ioc *ioc, unsigned long size,
  1267. unsigned long min, unsigned long max, unsigned long align)
  1268. {
  1269. #ifdef NASTY_HACK_FOR_K_CLASS
  1270. __raw_writel(0xfffff600, (unsigned long)&(ioc->ioc_hpa->io_io_high));
  1271. ioc->mmio_region[0].end = 0xf5ffffff;
  1272. #endif
  1273. }
  1274. static struct resource *ccio_get_resource(struct ioc* ioc,
  1275. const struct parisc_device *dev)
  1276. {
  1277. if (!ioc) {
  1278. return &iomem_resource;
  1279. } else if ((ioc->mmio_region->start <= dev->hpa) &&
  1280. (dev->hpa < ioc->mmio_region->end)) {
  1281. return ioc->mmio_region;
  1282. } else if (((ioc->mmio_region + 1)->start <= dev->hpa) &&
  1283. (dev->hpa < (ioc->mmio_region + 1)->end)) {
  1284. return ioc->mmio_region + 1;
  1285. } else {
  1286. return NULL;
  1287. }
  1288. }
  1289. int ccio_allocate_resource(const struct parisc_device *dev,
  1290. struct resource *res, unsigned long size,
  1291. unsigned long min, unsigned long max, unsigned long align,
  1292. void (*alignf)(void *, struct resource *, unsigned long),
  1293. void *alignf_data)
  1294. {
  1295. struct ioc *ioc = ccio_get_iommu(dev);
  1296. struct resource *parent = ccio_get_resource(ioc, dev);
  1297. if (!parent)
  1298. return -EBUSY;
  1299. if (!allocate_resource(parent, res, size, min, max, align, alignf,
  1300. alignf_data))
  1301. return 0;
  1302. expand_ioc_area(ioc, size, min, max, align);
  1303. return allocate_resource(parent, res, size, min, max, align, alignf,
  1304. alignf_data);
  1305. }
  1306. int ccio_request_resource(const struct parisc_device *dev,
  1307. struct resource *res)
  1308. {
  1309. struct ioc *ioc = ccio_get_iommu(dev);
  1310. struct resource *parent = ccio_get_resource(ioc, dev);
  1311. return request_resource(parent, res);
  1312. }
  1313. /**
  1314.  * ccio_probe - Determine if ccio should claim this device.
  1315.  * @dev: The device which has been found
  1316.  *
  1317.  * Determine if ccio should claim this chip (return 0) or not (return 1).
  1318.  * If so, initialize the chip and tell other partners in crime they
  1319.  * have work to do.
  1320.  */
  1321. static int ccio_probe(struct parisc_device *dev)
  1322. {
  1323. int i;
  1324. struct ioc *ioc, **ioc_p = &ioc_list;
  1325. ioc = kmalloc(sizeof(struct ioc), GFP_KERNEL);
  1326. if (ioc == NULL) {
  1327. printk(KERN_ERR MODULE_NAME ": memory allocation failuren");
  1328. return 1;
  1329. }
  1330. memset(ioc, 0, sizeof(struct ioc));
  1331. ioc->name = dev->id.hversion == U2_IOA_RUNWAY ? "U2" : "UTurn";
  1332. printk(KERN_INFO "Found %s at 0x%lxn", ioc->name, dev->hpa);
  1333. for (i = 0; i < ioc_count; i++) {
  1334. ioc_p = &(*ioc_p)->next;
  1335. }
  1336. *ioc_p = ioc;
  1337. ioc->hw_path = dev->hw_path;
  1338. ioc->ioc_hpa = (struct ioa_registers *)dev->hpa;
  1339. ccio_ioc_init(ioc);
  1340. ccio_init_resources(ioc);
  1341. hppa_dma_ops = &ccio_ops;
  1342. if (ioc_count == 0) {
  1343. /* XXX: Create separate entries for each ioc */
  1344. create_proc_read_entry(MODULE_NAME, S_IRWXU, proc_runway_root,
  1345.        ccio_proc_info, NULL);
  1346. create_proc_read_entry(MODULE_NAME"-bitmap", S_IRWXU,
  1347.        proc_runway_root, ccio_resource_map, NULL);
  1348. }
  1349. ioc_count++;
  1350. return 0;
  1351. }
  1352. struct pci_dev * ccio_get_fake(const struct parisc_device *dev)
  1353. {
  1354. struct ioc *ioc;
  1355. dev = find_pa_parent_type(dev, HPHW_IOA);
  1356. if(!dev)
  1357. return NULL;
  1358. ioc = ccio_find_ioc(dev->hw_path);
  1359. if(!ioc)
  1360. return NULL;
  1361. if(ioc->fake_pci_dev)
  1362. return ioc->fake_pci_dev;
  1363. ioc->fake_pci_dev = kmalloc(sizeof(struct pci_dev), GFP_KERNEL);
  1364. if(ioc->fake_pci_dev == NULL) {
  1365. printk(KERN_ERR MODULE_NAME ": memory allocation failuren");
  1366. return NULL;
  1367. }
  1368. memset(ioc->fake_pci_dev, 0, sizeof(struct pci_dev));
  1369. ioc->fake_pci_dev->sysdata = kmalloc(sizeof(struct pci_hba_data), GFP_KERNEL);
  1370. if(ioc->fake_pci_dev->sysdata == NULL) {
  1371. printk(KERN_ERR MODULE_NAME ": memory allocation failuren");
  1372. return NULL;
  1373. }
  1374. HBA_DATA(ioc->fake_pci_dev->sysdata)->iommu = ioc;
  1375. return ioc->fake_pci_dev;
  1376. }
  1377. /* We *can't* support JAVA (T600). Venture there at your own risk. */
  1378. static struct parisc_device_id ccio_tbl[] = {
  1379. { HPHW_IOA, HVERSION_REV_ANY_ID, U2_IOA_RUNWAY, 0xb }, /* U2 */
  1380. { HPHW_IOA, HVERSION_REV_ANY_ID, UTURN_IOA_RUNWAY, 0xb }, /* UTurn */
  1381. { 0, }
  1382. };
  1383. static struct parisc_driver ccio_driver = {
  1384. name: "U2/Uturn",
  1385. id_table: ccio_tbl,
  1386. probe: ccio_probe,
  1387. };
  1388. /**
  1389.  * ccio_init - ccio initalization procedure.
  1390.  *
  1391.  * Register this driver.
  1392.  */
  1393. void __init ccio_init(void)
  1394. {
  1395. register_parisc_driver(&ccio_driver);
  1396. }