sba_iommu.c 57 KB

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