sba_iommu.c 58 KB

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