ccio-dma.c 46 KB

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