sba_iommu.c 57 KB

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