sba_iommu.c 56 KB

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