sba_iommu.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215
  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/PCI/PCI-DMA-mapping.txt
  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. struct dma_attrs *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_single_attrs() 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_single_attrs()"))
  826. panic("Sanity check failed");
  827. spin_unlock_irqrestore(&ioc->res_lock, flags);
  828. #endif
  829. pide = sba_alloc_range(ioc, dev, size);
  830. iovp = (dma_addr_t) pide << iovp_shift;
  831. DBG_RUN("%s() 0x%p -> 0x%lx\n", __func__, addr, (long) iovp | offset);
  832. pdir_start = &(ioc->pdir_base[pide]);
  833. while (size > 0) {
  834. ASSERT(((u8 *)pdir_start)[7] == 0); /* verify availability */
  835. sba_io_pdir_entry(pdir_start, (unsigned long) addr);
  836. DBG_RUN(" pdir 0x%p %lx\n", pdir_start, *pdir_start);
  837. addr += iovp_size;
  838. size -= iovp_size;
  839. pdir_start++;
  840. }
  841. /* force pdir update */
  842. wmb();
  843. /* form complete address */
  844. #ifdef ASSERT_PDIR_SANITY
  845. spin_lock_irqsave(&ioc->res_lock, flags);
  846. sba_check_pdir(ioc,"Check after sba_map_single_attrs()");
  847. spin_unlock_irqrestore(&ioc->res_lock, flags);
  848. #endif
  849. return SBA_IOVA(ioc, iovp, offset);
  850. }
  851. static dma_addr_t sba_map_single_attrs(struct device *dev, void *addr,
  852. size_t size, enum dma_data_direction dir,
  853. struct dma_attrs *attrs)
  854. {
  855. return sba_map_page(dev, virt_to_page(addr),
  856. (unsigned long)addr & ~PAGE_MASK, size, dir, attrs);
  857. }
  858. #ifdef ENABLE_MARK_CLEAN
  859. static SBA_INLINE void
  860. sba_mark_clean(struct ioc *ioc, dma_addr_t iova, size_t size)
  861. {
  862. u32 iovp = (u32) SBA_IOVP(ioc,iova);
  863. int off = PDIR_INDEX(iovp);
  864. void *addr;
  865. if (size <= iovp_size) {
  866. addr = phys_to_virt(ioc->pdir_base[off] &
  867. ~0xE000000000000FFFULL);
  868. mark_clean(addr, size);
  869. } else {
  870. do {
  871. addr = phys_to_virt(ioc->pdir_base[off] &
  872. ~0xE000000000000FFFULL);
  873. mark_clean(addr, min(size, iovp_size));
  874. off++;
  875. size -= iovp_size;
  876. } while (size > 0);
  877. }
  878. }
  879. #endif
  880. /**
  881. * sba_unmap_single_attrs - unmap one IOVA and free resources
  882. * @dev: instance of PCI owned by the driver that's asking.
  883. * @iova: IOVA of driver buffer previously mapped.
  884. * @size: number of bytes mapped in driver buffer.
  885. * @dir: R/W or both.
  886. * @attrs: optional dma attributes
  887. *
  888. * See Documentation/PCI/PCI-DMA-mapping.txt
  889. */
  890. static void sba_unmap_page(struct device *dev, dma_addr_t iova, size_t size,
  891. enum dma_data_direction dir, struct dma_attrs *attrs)
  892. {
  893. struct ioc *ioc;
  894. #if DELAYED_RESOURCE_CNT > 0
  895. struct sba_dma_pair *d;
  896. #endif
  897. unsigned long flags;
  898. dma_addr_t offset;
  899. ioc = GET_IOC(dev);
  900. ASSERT(ioc);
  901. #ifdef ALLOW_IOV_BYPASS
  902. if (likely((iova & ioc->imask) != ioc->ibase)) {
  903. /*
  904. ** Address does not fall w/in IOVA, must be bypassing
  905. */
  906. DBG_BYPASS("sba_unmap_single_atttrs() bypass addr: 0x%lx\n",
  907. iova);
  908. #ifdef ENABLE_MARK_CLEAN
  909. if (dir == DMA_FROM_DEVICE) {
  910. mark_clean(phys_to_virt(iova), size);
  911. }
  912. #endif
  913. return;
  914. }
  915. #endif
  916. offset = iova & ~iovp_mask;
  917. DBG_RUN("%s() iovp 0x%lx/%x\n", __func__, (long) iova, size);
  918. iova ^= offset; /* clear offset bits */
  919. size += offset;
  920. size = ROUNDUP(size, iovp_size);
  921. #ifdef ENABLE_MARK_CLEAN
  922. if (dir == DMA_FROM_DEVICE)
  923. sba_mark_clean(ioc, iova, size);
  924. #endif
  925. #if DELAYED_RESOURCE_CNT > 0
  926. spin_lock_irqsave(&ioc->saved_lock, flags);
  927. d = &(ioc->saved[ioc->saved_cnt]);
  928. d->iova = iova;
  929. d->size = size;
  930. if (unlikely(++(ioc->saved_cnt) >= DELAYED_RESOURCE_CNT)) {
  931. int cnt = ioc->saved_cnt;
  932. spin_lock(&ioc->res_lock);
  933. while (cnt--) {
  934. sba_mark_invalid(ioc, d->iova, d->size);
  935. sba_free_range(ioc, d->iova, d->size);
  936. d--;
  937. }
  938. ioc->saved_cnt = 0;
  939. READ_REG(ioc->ioc_hpa+IOC_PCOM); /* flush purges */
  940. spin_unlock(&ioc->res_lock);
  941. }
  942. spin_unlock_irqrestore(&ioc->saved_lock, flags);
  943. #else /* DELAYED_RESOURCE_CNT == 0 */
  944. spin_lock_irqsave(&ioc->res_lock, flags);
  945. sba_mark_invalid(ioc, iova, size);
  946. sba_free_range(ioc, iova, size);
  947. READ_REG(ioc->ioc_hpa+IOC_PCOM); /* flush purges */
  948. spin_unlock_irqrestore(&ioc->res_lock, flags);
  949. #endif /* DELAYED_RESOURCE_CNT == 0 */
  950. }
  951. void sba_unmap_single_attrs(struct device *dev, dma_addr_t iova, size_t size,
  952. enum dma_data_direction dir, struct dma_attrs *attrs)
  953. {
  954. sba_unmap_page(dev, iova, size, dir, attrs);
  955. }
  956. /**
  957. * sba_alloc_coherent - allocate/map shared mem for DMA
  958. * @dev: instance of PCI owned by the driver that's asking.
  959. * @size: number of bytes mapped in driver buffer.
  960. * @dma_handle: IOVA of new buffer.
  961. *
  962. * See Documentation/PCI/PCI-DMA-mapping.txt
  963. */
  964. static void *
  965. sba_alloc_coherent (struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t flags)
  966. {
  967. struct ioc *ioc;
  968. void *addr;
  969. ioc = GET_IOC(dev);
  970. ASSERT(ioc);
  971. #ifdef CONFIG_NUMA
  972. {
  973. struct page *page;
  974. page = alloc_pages_node(ioc->node == MAX_NUMNODES ?
  975. numa_node_id() : ioc->node, flags,
  976. get_order(size));
  977. if (unlikely(!page))
  978. return NULL;
  979. addr = page_address(page);
  980. }
  981. #else
  982. addr = (void *) __get_free_pages(flags, get_order(size));
  983. #endif
  984. if (unlikely(!addr))
  985. return NULL;
  986. memset(addr, 0, size);
  987. *dma_handle = virt_to_phys(addr);
  988. #ifdef ALLOW_IOV_BYPASS
  989. ASSERT(dev->coherent_dma_mask);
  990. /*
  991. ** Check if the PCI device can DMA to ptr... if so, just return ptr
  992. */
  993. if (likely((*dma_handle & ~dev->coherent_dma_mask) == 0)) {
  994. DBG_BYPASS("sba_alloc_coherent() bypass mask/addr: 0x%lx/0x%lx\n",
  995. dev->coherent_dma_mask, *dma_handle);
  996. return addr;
  997. }
  998. #endif
  999. /*
  1000. * If device can't bypass or bypass is disabled, pass the 32bit fake
  1001. * device to map single to get an iova mapping.
  1002. */
  1003. *dma_handle = sba_map_single_attrs(&ioc->sac_only_dev->dev, addr,
  1004. size, 0, NULL);
  1005. return addr;
  1006. }
  1007. /**
  1008. * sba_free_coherent - free/unmap shared mem for DMA
  1009. * @dev: instance of PCI owned by the driver that's asking.
  1010. * @size: number of bytes mapped in driver buffer.
  1011. * @vaddr: virtual address IOVA of "consistent" buffer.
  1012. * @dma_handler: IO virtual address of "consistent" buffer.
  1013. *
  1014. * See Documentation/PCI/PCI-DMA-mapping.txt
  1015. */
  1016. static void sba_free_coherent (struct device *dev, size_t size, void *vaddr,
  1017. dma_addr_t dma_handle)
  1018. {
  1019. sba_unmap_single_attrs(dev, dma_handle, size, 0, NULL);
  1020. free_pages((unsigned long) vaddr, get_order(size));
  1021. }
  1022. /*
  1023. ** Since 0 is a valid pdir_base index value, can't use that
  1024. ** to determine if a value is valid or not. Use a flag to indicate
  1025. ** the SG list entry contains a valid pdir index.
  1026. */
  1027. #define PIDE_FLAG 0x1UL
  1028. #ifdef DEBUG_LARGE_SG_ENTRIES
  1029. int dump_run_sg = 0;
  1030. #endif
  1031. /**
  1032. * sba_fill_pdir - write allocated SG entries into IO PDIR
  1033. * @ioc: IO MMU structure which owns the pdir we are interested in.
  1034. * @startsg: list of IOVA/size pairs
  1035. * @nents: number of entries in startsg list
  1036. *
  1037. * Take preprocessed SG list and write corresponding entries
  1038. * in the IO PDIR.
  1039. */
  1040. static SBA_INLINE int
  1041. sba_fill_pdir(
  1042. struct ioc *ioc,
  1043. struct scatterlist *startsg,
  1044. int nents)
  1045. {
  1046. struct scatterlist *dma_sg = startsg; /* pointer to current DMA */
  1047. int n_mappings = 0;
  1048. u64 *pdirp = NULL;
  1049. unsigned long dma_offset = 0;
  1050. while (nents-- > 0) {
  1051. int cnt = startsg->dma_length;
  1052. startsg->dma_length = 0;
  1053. #ifdef DEBUG_LARGE_SG_ENTRIES
  1054. if (dump_run_sg)
  1055. printk(" %2d : %08lx/%05x %p\n",
  1056. nents, startsg->dma_address, cnt,
  1057. sba_sg_address(startsg));
  1058. #else
  1059. DBG_RUN_SG(" %d : %08lx/%05x %p\n",
  1060. nents, startsg->dma_address, cnt,
  1061. sba_sg_address(startsg));
  1062. #endif
  1063. /*
  1064. ** Look for the start of a new DMA stream
  1065. */
  1066. if (startsg->dma_address & PIDE_FLAG) {
  1067. u32 pide = startsg->dma_address & ~PIDE_FLAG;
  1068. dma_offset = (unsigned long) pide & ~iovp_mask;
  1069. startsg->dma_address = 0;
  1070. if (n_mappings)
  1071. dma_sg = sg_next(dma_sg);
  1072. dma_sg->dma_address = pide | ioc->ibase;
  1073. pdirp = &(ioc->pdir_base[pide >> iovp_shift]);
  1074. n_mappings++;
  1075. }
  1076. /*
  1077. ** Look for a VCONTIG chunk
  1078. */
  1079. if (cnt) {
  1080. unsigned long vaddr = (unsigned long) sba_sg_address(startsg);
  1081. ASSERT(pdirp);
  1082. /* Since multiple Vcontig blocks could make up
  1083. ** one DMA stream, *add* cnt to dma_len.
  1084. */
  1085. dma_sg->dma_length += cnt;
  1086. cnt += dma_offset;
  1087. dma_offset=0; /* only want offset on first chunk */
  1088. cnt = ROUNDUP(cnt, iovp_size);
  1089. do {
  1090. sba_io_pdir_entry(pdirp, vaddr);
  1091. vaddr += iovp_size;
  1092. cnt -= iovp_size;
  1093. pdirp++;
  1094. } while (cnt > 0);
  1095. }
  1096. startsg = sg_next(startsg);
  1097. }
  1098. /* force pdir update */
  1099. wmb();
  1100. #ifdef DEBUG_LARGE_SG_ENTRIES
  1101. dump_run_sg = 0;
  1102. #endif
  1103. return(n_mappings);
  1104. }
  1105. /*
  1106. ** Two address ranges are DMA contiguous *iff* "end of prev" and
  1107. ** "start of next" are both on an IOV page boundary.
  1108. **
  1109. ** (shift left is a quick trick to mask off upper bits)
  1110. */
  1111. #define DMA_CONTIG(__X, __Y) \
  1112. (((((unsigned long) __X) | ((unsigned long) __Y)) << (BITS_PER_LONG - iovp_shift)) == 0UL)
  1113. /**
  1114. * sba_coalesce_chunks - preprocess the SG list
  1115. * @ioc: IO MMU structure which owns the pdir we are interested in.
  1116. * @startsg: list of IOVA/size pairs
  1117. * @nents: number of entries in startsg list
  1118. *
  1119. * First pass is to walk the SG list and determine where the breaks are
  1120. * in the DMA stream. Allocates PDIR entries but does not fill them.
  1121. * Returns the number of DMA chunks.
  1122. *
  1123. * Doing the fill separate from the coalescing/allocation keeps the
  1124. * code simpler. Future enhancement could make one pass through
  1125. * the sglist do both.
  1126. */
  1127. static SBA_INLINE int
  1128. sba_coalesce_chunks(struct ioc *ioc, struct device *dev,
  1129. struct scatterlist *startsg,
  1130. int nents)
  1131. {
  1132. struct scatterlist *vcontig_sg; /* VCONTIG chunk head */
  1133. unsigned long vcontig_len; /* len of VCONTIG chunk */
  1134. unsigned long vcontig_end;
  1135. struct scatterlist *dma_sg; /* next DMA stream head */
  1136. unsigned long dma_offset, dma_len; /* start/len of DMA stream */
  1137. int n_mappings = 0;
  1138. unsigned int max_seg_size = dma_get_max_seg_size(dev);
  1139. while (nents > 0) {
  1140. unsigned long vaddr = (unsigned long) sba_sg_address(startsg);
  1141. /*
  1142. ** Prepare for first/next DMA stream
  1143. */
  1144. dma_sg = vcontig_sg = startsg;
  1145. dma_len = vcontig_len = vcontig_end = startsg->length;
  1146. vcontig_end += vaddr;
  1147. dma_offset = vaddr & ~iovp_mask;
  1148. /* PARANOID: clear entries */
  1149. startsg->dma_address = startsg->dma_length = 0;
  1150. /*
  1151. ** This loop terminates one iteration "early" since
  1152. ** it's always looking one "ahead".
  1153. */
  1154. while (--nents > 0) {
  1155. unsigned long vaddr; /* tmp */
  1156. startsg = sg_next(startsg);
  1157. /* PARANOID */
  1158. startsg->dma_address = startsg->dma_length = 0;
  1159. /* catch brokenness in SCSI layer */
  1160. ASSERT(startsg->length <= DMA_CHUNK_SIZE);
  1161. /*
  1162. ** First make sure current dma stream won't
  1163. ** exceed DMA_CHUNK_SIZE if we coalesce the
  1164. ** next entry.
  1165. */
  1166. if (((dma_len + dma_offset + startsg->length + ~iovp_mask) & iovp_mask)
  1167. > DMA_CHUNK_SIZE)
  1168. break;
  1169. if (dma_len + startsg->length > max_seg_size)
  1170. break;
  1171. /*
  1172. ** Then look for virtually contiguous blocks.
  1173. **
  1174. ** append the next transaction?
  1175. */
  1176. vaddr = (unsigned long) sba_sg_address(startsg);
  1177. if (vcontig_end == vaddr)
  1178. {
  1179. vcontig_len += startsg->length;
  1180. vcontig_end += startsg->length;
  1181. dma_len += startsg->length;
  1182. continue;
  1183. }
  1184. #ifdef DEBUG_LARGE_SG_ENTRIES
  1185. dump_run_sg = (vcontig_len > iovp_size);
  1186. #endif
  1187. /*
  1188. ** Not virtually contigous.
  1189. ** Terminate prev chunk.
  1190. ** Start a new chunk.
  1191. **
  1192. ** Once we start a new VCONTIG chunk, dma_offset
  1193. ** can't change. And we need the offset from the first
  1194. ** chunk - not the last one. Ergo Successive chunks
  1195. ** must start on page boundaries and dove tail
  1196. ** with it's predecessor.
  1197. */
  1198. vcontig_sg->dma_length = vcontig_len;
  1199. vcontig_sg = startsg;
  1200. vcontig_len = startsg->length;
  1201. /*
  1202. ** 3) do the entries end/start on page boundaries?
  1203. ** Don't update vcontig_end until we've checked.
  1204. */
  1205. if (DMA_CONTIG(vcontig_end, vaddr))
  1206. {
  1207. vcontig_end = vcontig_len + vaddr;
  1208. dma_len += vcontig_len;
  1209. continue;
  1210. } else {
  1211. break;
  1212. }
  1213. }
  1214. /*
  1215. ** End of DMA Stream
  1216. ** Terminate last VCONTIG block.
  1217. ** Allocate space for DMA stream.
  1218. */
  1219. vcontig_sg->dma_length = vcontig_len;
  1220. dma_len = (dma_len + dma_offset + ~iovp_mask) & iovp_mask;
  1221. ASSERT(dma_len <= DMA_CHUNK_SIZE);
  1222. dma_sg->dma_address = (dma_addr_t) (PIDE_FLAG
  1223. | (sba_alloc_range(ioc, dev, dma_len) << iovp_shift)
  1224. | dma_offset);
  1225. n_mappings++;
  1226. }
  1227. return n_mappings;
  1228. }
  1229. /**
  1230. * sba_map_sg - map Scatter/Gather list
  1231. * @dev: instance of PCI owned by the driver that's asking.
  1232. * @sglist: array of buffer/length pairs
  1233. * @nents: number of entries in list
  1234. * @dir: R/W or both.
  1235. * @attrs: optional dma attributes
  1236. *
  1237. * See Documentation/PCI/PCI-DMA-mapping.txt
  1238. */
  1239. static int sba_map_sg_attrs(struct device *dev, struct scatterlist *sglist,
  1240. int nents, enum dma_data_direction dir,
  1241. struct dma_attrs *attrs)
  1242. {
  1243. struct ioc *ioc;
  1244. int coalesced, filled = 0;
  1245. #ifdef ASSERT_PDIR_SANITY
  1246. unsigned long flags;
  1247. #endif
  1248. #ifdef ALLOW_IOV_BYPASS_SG
  1249. struct scatterlist *sg;
  1250. #endif
  1251. DBG_RUN_SG("%s() START %d entries\n", __func__, nents);
  1252. ioc = GET_IOC(dev);
  1253. ASSERT(ioc);
  1254. #ifdef ALLOW_IOV_BYPASS_SG
  1255. ASSERT(to_pci_dev(dev)->dma_mask);
  1256. if (likely((ioc->dma_mask & ~to_pci_dev(dev)->dma_mask) == 0)) {
  1257. for_each_sg(sglist, sg, nents, filled) {
  1258. sg->dma_length = sg->length;
  1259. sg->dma_address = virt_to_phys(sba_sg_address(sg));
  1260. }
  1261. return filled;
  1262. }
  1263. #endif
  1264. /* Fast path single entry scatterlists. */
  1265. if (nents == 1) {
  1266. sglist->dma_length = sglist->length;
  1267. sglist->dma_address = sba_map_single_attrs(dev, sba_sg_address(sglist), sglist->length, dir, attrs);
  1268. return 1;
  1269. }
  1270. #ifdef ASSERT_PDIR_SANITY
  1271. spin_lock_irqsave(&ioc->res_lock, flags);
  1272. if (sba_check_pdir(ioc,"Check before sba_map_sg_attrs()"))
  1273. {
  1274. sba_dump_sg(ioc, sglist, nents);
  1275. panic("Check before sba_map_sg_attrs()");
  1276. }
  1277. spin_unlock_irqrestore(&ioc->res_lock, flags);
  1278. #endif
  1279. prefetch(ioc->res_hint);
  1280. /*
  1281. ** First coalesce the chunks and allocate I/O pdir space
  1282. **
  1283. ** If this is one DMA stream, we can properly map using the
  1284. ** correct virtual address associated with each DMA page.
  1285. ** w/o this association, we wouldn't have coherent DMA!
  1286. ** Access to the virtual address is what forces a two pass algorithm.
  1287. */
  1288. coalesced = sba_coalesce_chunks(ioc, dev, sglist, nents);
  1289. /*
  1290. ** Program the I/O Pdir
  1291. **
  1292. ** map the virtual addresses to the I/O Pdir
  1293. ** o dma_address will contain the pdir index
  1294. ** o dma_len will contain the number of bytes to map
  1295. ** o address contains the virtual address.
  1296. */
  1297. filled = sba_fill_pdir(ioc, sglist, nents);
  1298. #ifdef ASSERT_PDIR_SANITY
  1299. spin_lock_irqsave(&ioc->res_lock, flags);
  1300. if (sba_check_pdir(ioc,"Check after sba_map_sg_attrs()"))
  1301. {
  1302. sba_dump_sg(ioc, sglist, nents);
  1303. panic("Check after sba_map_sg_attrs()\n");
  1304. }
  1305. spin_unlock_irqrestore(&ioc->res_lock, flags);
  1306. #endif
  1307. ASSERT(coalesced == filled);
  1308. DBG_RUN_SG("%s() DONE %d mappings\n", __func__, filled);
  1309. return filled;
  1310. }
  1311. /**
  1312. * sba_unmap_sg_attrs - unmap Scatter/Gather list
  1313. * @dev: instance of PCI owned by the driver that's asking.
  1314. * @sglist: array of buffer/length pairs
  1315. * @nents: number of entries in list
  1316. * @dir: R/W or both.
  1317. * @attrs: optional dma attributes
  1318. *
  1319. * See Documentation/PCI/PCI-DMA-mapping.txt
  1320. */
  1321. static void sba_unmap_sg_attrs(struct device *dev, struct scatterlist *sglist,
  1322. int nents, enum dma_data_direction dir,
  1323. struct dma_attrs *attrs)
  1324. {
  1325. #ifdef ASSERT_PDIR_SANITY
  1326. struct ioc *ioc;
  1327. unsigned long flags;
  1328. #endif
  1329. DBG_RUN_SG("%s() START %d entries, %p,%x\n",
  1330. __func__, nents, sba_sg_address(sglist), sglist->length);
  1331. #ifdef ASSERT_PDIR_SANITY
  1332. ioc = GET_IOC(dev);
  1333. ASSERT(ioc);
  1334. spin_lock_irqsave(&ioc->res_lock, flags);
  1335. sba_check_pdir(ioc,"Check before sba_unmap_sg_attrs()");
  1336. spin_unlock_irqrestore(&ioc->res_lock, flags);
  1337. #endif
  1338. while (nents && sglist->dma_length) {
  1339. sba_unmap_single_attrs(dev, sglist->dma_address,
  1340. sglist->dma_length, dir, attrs);
  1341. sglist = sg_next(sglist);
  1342. nents--;
  1343. }
  1344. DBG_RUN_SG("%s() DONE (nents %d)\n", __func__, nents);
  1345. #ifdef ASSERT_PDIR_SANITY
  1346. spin_lock_irqsave(&ioc->res_lock, flags);
  1347. sba_check_pdir(ioc,"Check after sba_unmap_sg_attrs()");
  1348. spin_unlock_irqrestore(&ioc->res_lock, flags);
  1349. #endif
  1350. }
  1351. /**************************************************************
  1352. *
  1353. * Initialization and claim
  1354. *
  1355. ***************************************************************/
  1356. static void __init
  1357. ioc_iova_init(struct ioc *ioc)
  1358. {
  1359. int tcnfg;
  1360. int agp_found = 0;
  1361. struct pci_dev *device = NULL;
  1362. #ifdef FULL_VALID_PDIR
  1363. unsigned long index;
  1364. #endif
  1365. /*
  1366. ** Firmware programs the base and size of a "safe IOVA space"
  1367. ** (one that doesn't overlap memory or LMMIO space) in the
  1368. ** IBASE and IMASK registers.
  1369. */
  1370. ioc->ibase = READ_REG(ioc->ioc_hpa + IOC_IBASE) & ~0x1UL;
  1371. ioc->imask = READ_REG(ioc->ioc_hpa + IOC_IMASK) | 0xFFFFFFFF00000000UL;
  1372. ioc->iov_size = ~ioc->imask + 1;
  1373. DBG_INIT("%s() hpa %p IOV base 0x%lx mask 0x%lx (%dMB)\n",
  1374. __func__, ioc->ioc_hpa, ioc->ibase, ioc->imask,
  1375. ioc->iov_size >> 20);
  1376. switch (iovp_size) {
  1377. case 4*1024: tcnfg = 0; break;
  1378. case 8*1024: tcnfg = 1; break;
  1379. case 16*1024: tcnfg = 2; break;
  1380. case 64*1024: tcnfg = 3; break;
  1381. default:
  1382. panic(PFX "Unsupported IOTLB page size %ldK",
  1383. iovp_size >> 10);
  1384. break;
  1385. }
  1386. WRITE_REG(tcnfg, ioc->ioc_hpa + IOC_TCNFG);
  1387. ioc->pdir_size = (ioc->iov_size / iovp_size) * PDIR_ENTRY_SIZE;
  1388. ioc->pdir_base = (void *) __get_free_pages(GFP_KERNEL,
  1389. get_order(ioc->pdir_size));
  1390. if (!ioc->pdir_base)
  1391. panic(PFX "Couldn't allocate I/O Page Table\n");
  1392. memset(ioc->pdir_base, 0, ioc->pdir_size);
  1393. DBG_INIT("%s() IOV page size %ldK pdir %p size %x\n", __func__,
  1394. iovp_size >> 10, ioc->pdir_base, ioc->pdir_size);
  1395. ASSERT(ALIGN((unsigned long) ioc->pdir_base, 4*1024) == (unsigned long) ioc->pdir_base);
  1396. WRITE_REG(virt_to_phys(ioc->pdir_base), ioc->ioc_hpa + IOC_PDIR_BASE);
  1397. /*
  1398. ** If an AGP device is present, only use half of the IOV space
  1399. ** for PCI DMA. Unfortunately we can't know ahead of time
  1400. ** whether GART support will actually be used, for now we
  1401. ** can just key on an AGP device found in the system.
  1402. ** We program the next pdir index after we stop w/ a key for
  1403. ** the GART code to handshake on.
  1404. */
  1405. for_each_pci_dev(device)
  1406. agp_found |= pci_find_capability(device, PCI_CAP_ID_AGP);
  1407. if (agp_found && reserve_sba_gart) {
  1408. printk(KERN_INFO PFX "reserving %dMb of IOVA space at 0x%lx for agpgart\n",
  1409. ioc->iov_size/2 >> 20, ioc->ibase + ioc->iov_size/2);
  1410. ioc->pdir_size /= 2;
  1411. ((u64 *)ioc->pdir_base)[PDIR_INDEX(ioc->iov_size/2)] = ZX1_SBA_IOMMU_COOKIE;
  1412. }
  1413. #ifdef FULL_VALID_PDIR
  1414. /*
  1415. ** Check to see if the spill page has been allocated, we don't need more than
  1416. ** one across multiple SBAs.
  1417. */
  1418. if (!prefetch_spill_page) {
  1419. char *spill_poison = "SBAIOMMU POISON";
  1420. int poison_size = 16;
  1421. void *poison_addr, *addr;
  1422. addr = (void *)__get_free_pages(GFP_KERNEL, get_order(iovp_size));
  1423. if (!addr)
  1424. panic(PFX "Couldn't allocate PDIR spill page\n");
  1425. poison_addr = addr;
  1426. for ( ; (u64) poison_addr < addr + iovp_size; poison_addr += poison_size)
  1427. memcpy(poison_addr, spill_poison, poison_size);
  1428. prefetch_spill_page = virt_to_phys(addr);
  1429. DBG_INIT("%s() prefetch spill addr: 0x%lx\n", __func__, prefetch_spill_page);
  1430. }
  1431. /*
  1432. ** Set all the PDIR entries valid w/ the spill page as the target
  1433. */
  1434. for (index = 0 ; index < (ioc->pdir_size / PDIR_ENTRY_SIZE) ; index++)
  1435. ((u64 *)ioc->pdir_base)[index] = (0x80000000000000FF | prefetch_spill_page);
  1436. #endif
  1437. /* Clear I/O TLB of any possible entries */
  1438. WRITE_REG(ioc->ibase | (get_iovp_order(ioc->iov_size) + iovp_shift), ioc->ioc_hpa + IOC_PCOM);
  1439. READ_REG(ioc->ioc_hpa + IOC_PCOM);
  1440. /* Enable IOVA translation */
  1441. WRITE_REG(ioc->ibase | 1, ioc->ioc_hpa + IOC_IBASE);
  1442. READ_REG(ioc->ioc_hpa + IOC_IBASE);
  1443. }
  1444. static void __init
  1445. ioc_resource_init(struct ioc *ioc)
  1446. {
  1447. spin_lock_init(&ioc->res_lock);
  1448. #if DELAYED_RESOURCE_CNT > 0
  1449. spin_lock_init(&ioc->saved_lock);
  1450. #endif
  1451. /* resource map size dictated by pdir_size */
  1452. ioc->res_size = ioc->pdir_size / PDIR_ENTRY_SIZE; /* entries */
  1453. ioc->res_size >>= 3; /* convert bit count to byte count */
  1454. DBG_INIT("%s() res_size 0x%x\n", __func__, ioc->res_size);
  1455. ioc->res_map = (char *) __get_free_pages(GFP_KERNEL,
  1456. get_order(ioc->res_size));
  1457. if (!ioc->res_map)
  1458. panic(PFX "Couldn't allocate resource map\n");
  1459. memset(ioc->res_map, 0, ioc->res_size);
  1460. /* next available IOVP - circular search */
  1461. ioc->res_hint = (unsigned long *) ioc->res_map;
  1462. #ifdef ASSERT_PDIR_SANITY
  1463. /* Mark first bit busy - ie no IOVA 0 */
  1464. ioc->res_map[0] = 0x1;
  1465. ioc->pdir_base[0] = 0x8000000000000000ULL | ZX1_SBA_IOMMU_COOKIE;
  1466. #endif
  1467. #ifdef FULL_VALID_PDIR
  1468. /* Mark the last resource used so we don't prefetch beyond IOVA space */
  1469. ioc->res_map[ioc->res_size - 1] |= 0x80UL; /* res_map is chars */
  1470. ioc->pdir_base[(ioc->pdir_size / PDIR_ENTRY_SIZE) - 1] = (0x80000000000000FF
  1471. | prefetch_spill_page);
  1472. #endif
  1473. DBG_INIT("%s() res_map %x %p\n", __func__,
  1474. ioc->res_size, (void *) ioc->res_map);
  1475. }
  1476. static void __init
  1477. ioc_sac_init(struct ioc *ioc)
  1478. {
  1479. struct pci_dev *sac = NULL;
  1480. struct pci_controller *controller = NULL;
  1481. /*
  1482. * pci_alloc_coherent() must return a DMA address which is
  1483. * SAC (single address cycle) addressable, so allocate a
  1484. * pseudo-device to enforce that.
  1485. */
  1486. sac = kzalloc(sizeof(*sac), GFP_KERNEL);
  1487. if (!sac)
  1488. panic(PFX "Couldn't allocate struct pci_dev");
  1489. controller = kzalloc(sizeof(*controller), GFP_KERNEL);
  1490. if (!controller)
  1491. panic(PFX "Couldn't allocate struct pci_controller");
  1492. controller->iommu = ioc;
  1493. sac->sysdata = controller;
  1494. sac->dma_mask = 0xFFFFFFFFUL;
  1495. #ifdef CONFIG_PCI
  1496. sac->dev.bus = &pci_bus_type;
  1497. #endif
  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 struct ioc * __init
  1534. ioc_init(u64 hpa, void *handle)
  1535. {
  1536. struct ioc *ioc;
  1537. struct ioc_iommu *info;
  1538. ioc = kzalloc(sizeof(*ioc), GFP_KERNEL);
  1539. if (!ioc)
  1540. return NULL;
  1541. ioc->next = ioc_list;
  1542. ioc_list = ioc;
  1543. ioc->handle = handle;
  1544. ioc->ioc_hpa = ioremap(hpa, 0x1000);
  1545. ioc->func_id = READ_REG(ioc->ioc_hpa + IOC_FUNC_ID);
  1546. ioc->rev = READ_REG(ioc->ioc_hpa + IOC_FCLASS) & 0xFFUL;
  1547. ioc->dma_mask = 0xFFFFFFFFFFFFFFFFUL; /* conservative */
  1548. for (info = ioc_iommu_info; info < ioc_iommu_info + ARRAY_SIZE(ioc_iommu_info); info++) {
  1549. if (ioc->func_id == info->func_id) {
  1550. ioc->name = info->name;
  1551. if (info->init)
  1552. (info->init)(ioc);
  1553. }
  1554. }
  1555. iovp_size = (1 << iovp_shift);
  1556. iovp_mask = ~(iovp_size - 1);
  1557. DBG_INIT("%s: PAGE_SIZE %ldK, iovp_size %ldK\n", __func__,
  1558. PAGE_SIZE >> 10, iovp_size >> 10);
  1559. if (!ioc->name) {
  1560. ioc->name = kmalloc(24, GFP_KERNEL);
  1561. if (ioc->name)
  1562. sprintf((char *) ioc->name, "Unknown (%04x:%04x)",
  1563. ioc->func_id & 0xFFFF, (ioc->func_id >> 16) & 0xFFFF);
  1564. else
  1565. ioc->name = "Unknown";
  1566. }
  1567. ioc_iova_init(ioc);
  1568. ioc_resource_init(ioc);
  1569. ioc_sac_init(ioc);
  1570. if ((long) ~iovp_mask > (long) ia64_max_iommu_merge_mask)
  1571. ia64_max_iommu_merge_mask = ~iovp_mask;
  1572. printk(KERN_INFO PFX
  1573. "%s %d.%d HPA 0x%lx IOVA space %dMb at 0x%lx\n",
  1574. ioc->name, (ioc->rev >> 4) & 0xF, ioc->rev & 0xF,
  1575. hpa, ioc->iov_size >> 20, ioc->ibase);
  1576. return ioc;
  1577. }
  1578. /**************************************************************************
  1579. **
  1580. ** SBA initialization code (HW and SW)
  1581. **
  1582. ** o identify SBA chip itself
  1583. ** o FIXME: initialize DMA hints for reasonable defaults
  1584. **
  1585. **************************************************************************/
  1586. #ifdef CONFIG_PROC_FS
  1587. static void *
  1588. ioc_start(struct seq_file *s, loff_t *pos)
  1589. {
  1590. struct ioc *ioc;
  1591. loff_t n = *pos;
  1592. for (ioc = ioc_list; ioc; ioc = ioc->next)
  1593. if (!n--)
  1594. return ioc;
  1595. return NULL;
  1596. }
  1597. static void *
  1598. ioc_next(struct seq_file *s, void *v, loff_t *pos)
  1599. {
  1600. struct ioc *ioc = v;
  1601. ++*pos;
  1602. return ioc->next;
  1603. }
  1604. static void
  1605. ioc_stop(struct seq_file *s, void *v)
  1606. {
  1607. }
  1608. static int
  1609. ioc_show(struct seq_file *s, void *v)
  1610. {
  1611. struct ioc *ioc = v;
  1612. unsigned long *res_ptr = (unsigned long *)ioc->res_map;
  1613. int i, used = 0;
  1614. seq_printf(s, "Hewlett Packard %s IOC rev %d.%d\n",
  1615. ioc->name, ((ioc->rev >> 4) & 0xF), (ioc->rev & 0xF));
  1616. #ifdef CONFIG_NUMA
  1617. if (ioc->node != MAX_NUMNODES)
  1618. seq_printf(s, "NUMA node : %d\n", ioc->node);
  1619. #endif
  1620. seq_printf(s, "IOVA size : %ld MB\n", ((ioc->pdir_size >> 3) * iovp_size)/(1024*1024));
  1621. seq_printf(s, "IOVA page size : %ld kb\n", iovp_size/1024);
  1622. for (i = 0; i < (ioc->res_size / sizeof(unsigned long)); ++i, ++res_ptr)
  1623. used += hweight64(*res_ptr);
  1624. seq_printf(s, "PDIR size : %d entries\n", ioc->pdir_size >> 3);
  1625. seq_printf(s, "PDIR used : %d entries\n", used);
  1626. #ifdef PDIR_SEARCH_TIMING
  1627. {
  1628. unsigned long i = 0, avg = 0, min, max;
  1629. min = max = ioc->avg_search[0];
  1630. for (i = 0; i < SBA_SEARCH_SAMPLE; i++) {
  1631. avg += ioc->avg_search[i];
  1632. if (ioc->avg_search[i] > max) max = ioc->avg_search[i];
  1633. if (ioc->avg_search[i] < min) min = ioc->avg_search[i];
  1634. }
  1635. avg /= SBA_SEARCH_SAMPLE;
  1636. seq_printf(s, "Bitmap search : %ld/%ld/%ld (min/avg/max CPU Cycles/IOVA page)\n",
  1637. min, avg, max);
  1638. }
  1639. #endif
  1640. #ifndef ALLOW_IOV_BYPASS
  1641. seq_printf(s, "IOVA bypass disabled\n");
  1642. #endif
  1643. return 0;
  1644. }
  1645. static const struct seq_operations ioc_seq_ops = {
  1646. .start = ioc_start,
  1647. .next = ioc_next,
  1648. .stop = ioc_stop,
  1649. .show = ioc_show
  1650. };
  1651. static int
  1652. ioc_open(struct inode *inode, struct file *file)
  1653. {
  1654. return seq_open(file, &ioc_seq_ops);
  1655. }
  1656. static const struct file_operations ioc_fops = {
  1657. .open = ioc_open,
  1658. .read = seq_read,
  1659. .llseek = seq_lseek,
  1660. .release = seq_release
  1661. };
  1662. static void __init
  1663. ioc_proc_init(void)
  1664. {
  1665. struct proc_dir_entry *dir;
  1666. dir = proc_mkdir("bus/mckinley", NULL);
  1667. if (!dir)
  1668. return;
  1669. proc_create(ioc_list->name, 0, dir, &ioc_fops);
  1670. }
  1671. #endif
  1672. static void
  1673. sba_connect_bus(struct pci_bus *bus)
  1674. {
  1675. acpi_handle handle, parent;
  1676. acpi_status status;
  1677. struct ioc *ioc;
  1678. if (!PCI_CONTROLLER(bus))
  1679. panic(PFX "no sysdata on bus %d!\n", bus->number);
  1680. if (PCI_CONTROLLER(bus)->iommu)
  1681. return;
  1682. handle = PCI_CONTROLLER(bus)->acpi_handle;
  1683. if (!handle)
  1684. return;
  1685. /*
  1686. * The IOC scope encloses PCI root bridges in the ACPI
  1687. * namespace, so work our way out until we find an IOC we
  1688. * claimed previously.
  1689. */
  1690. do {
  1691. for (ioc = ioc_list; ioc; ioc = ioc->next)
  1692. if (ioc->handle == handle) {
  1693. PCI_CONTROLLER(bus)->iommu = ioc;
  1694. return;
  1695. }
  1696. status = acpi_get_parent(handle, &parent);
  1697. handle = parent;
  1698. } while (ACPI_SUCCESS(status));
  1699. printk(KERN_WARNING "No IOC for PCI Bus %04x:%02x in ACPI\n", pci_domain_nr(bus), bus->number);
  1700. }
  1701. #ifdef CONFIG_NUMA
  1702. static void __init
  1703. sba_map_ioc_to_node(struct ioc *ioc, acpi_handle handle)
  1704. {
  1705. unsigned int node;
  1706. int pxm;
  1707. ioc->node = MAX_NUMNODES;
  1708. pxm = acpi_get_pxm(handle);
  1709. if (pxm < 0)
  1710. return;
  1711. node = pxm_to_node(pxm);
  1712. if (node >= MAX_NUMNODES || !node_online(node))
  1713. return;
  1714. ioc->node = node;
  1715. return;
  1716. }
  1717. #else
  1718. #define sba_map_ioc_to_node(ioc, handle)
  1719. #endif
  1720. static int __init
  1721. acpi_sba_ioc_add(struct acpi_device *device)
  1722. {
  1723. struct ioc *ioc;
  1724. acpi_status status;
  1725. u64 hpa, length;
  1726. struct acpi_buffer buffer;
  1727. struct acpi_device_info *dev_info;
  1728. status = hp_acpi_csr_space(device->handle, &hpa, &length);
  1729. if (ACPI_FAILURE(status))
  1730. return 1;
  1731. buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
  1732. status = acpi_get_object_info(device->handle, &buffer);
  1733. if (ACPI_FAILURE(status))
  1734. return 1;
  1735. dev_info = buffer.pointer;
  1736. /*
  1737. * For HWP0001, only SBA appears in ACPI namespace. It encloses the PCI
  1738. * root bridges, and its CSR space includes the IOC function.
  1739. */
  1740. if (strncmp("HWP0001", dev_info->hardware_id.value, 7) == 0) {
  1741. hpa += ZX1_IOC_OFFSET;
  1742. /* zx1 based systems default to kernel page size iommu pages */
  1743. if (!iovp_shift)
  1744. iovp_shift = min(PAGE_SHIFT, 16);
  1745. }
  1746. kfree(dev_info);
  1747. /*
  1748. * default anything not caught above or specified on cmdline to 4k
  1749. * iommu page size
  1750. */
  1751. if (!iovp_shift)
  1752. iovp_shift = 12;
  1753. ioc = ioc_init(hpa, device->handle);
  1754. if (!ioc)
  1755. return 1;
  1756. /* setup NUMA node association */
  1757. sba_map_ioc_to_node(ioc, device->handle);
  1758. return 0;
  1759. }
  1760. static const struct acpi_device_id hp_ioc_iommu_device_ids[] = {
  1761. {"HWP0001", 0},
  1762. {"HWP0004", 0},
  1763. {"", 0},
  1764. };
  1765. static struct acpi_driver acpi_sba_ioc_driver = {
  1766. .name = "IOC IOMMU Driver",
  1767. .ids = hp_ioc_iommu_device_ids,
  1768. .ops = {
  1769. .add = acpi_sba_ioc_add,
  1770. },
  1771. };
  1772. extern struct dma_map_ops swiotlb_dma_ops;
  1773. static int __init
  1774. sba_init(void)
  1775. {
  1776. if (!ia64_platform_is("hpzx1") && !ia64_platform_is("hpzx1_swiotlb"))
  1777. return 0;
  1778. #if defined(CONFIG_IA64_GENERIC)
  1779. /* If we are booting a kdump kernel, the sba_iommu will
  1780. * cause devices that were not shutdown properly to MCA
  1781. * as soon as they are turned back on. Our only option for
  1782. * a successful kdump kernel boot is to use the swiotlb.
  1783. */
  1784. if (is_kdump_kernel()) {
  1785. dma_ops = &swiotlb_dma_ops;
  1786. if (swiotlb_late_init_with_default_size(64 * (1<<20)) != 0)
  1787. panic("Unable to initialize software I/O TLB:"
  1788. " Try machvec=dig boot option");
  1789. machvec_init("dig");
  1790. return 0;
  1791. }
  1792. #endif
  1793. acpi_bus_register_driver(&acpi_sba_ioc_driver);
  1794. if (!ioc_list) {
  1795. #ifdef CONFIG_IA64_GENERIC
  1796. /*
  1797. * If we didn't find something sba_iommu can claim, we
  1798. * need to setup the swiotlb and switch to the dig machvec.
  1799. */
  1800. dma_ops = &swiotlb_dma_ops;
  1801. if (swiotlb_late_init_with_default_size(64 * (1<<20)) != 0)
  1802. panic("Unable to find SBA IOMMU or initialize "
  1803. "software I/O TLB: Try machvec=dig boot option");
  1804. machvec_init("dig");
  1805. #else
  1806. panic("Unable to find SBA IOMMU: Try a generic or DIG kernel");
  1807. #endif
  1808. return 0;
  1809. }
  1810. #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_HP_ZX1_SWIOTLB)
  1811. /*
  1812. * hpzx1_swiotlb needs to have a fairly small swiotlb bounce
  1813. * buffer setup to support devices with smaller DMA masks than
  1814. * sba_iommu can handle.
  1815. */
  1816. if (ia64_platform_is("hpzx1_swiotlb")) {
  1817. extern void hwsw_init(void);
  1818. hwsw_init();
  1819. }
  1820. #endif
  1821. #ifdef CONFIG_PCI
  1822. {
  1823. struct pci_bus *b = NULL;
  1824. while ((b = pci_find_next_bus(b)) != NULL)
  1825. sba_connect_bus(b);
  1826. }
  1827. #endif
  1828. #ifdef CONFIG_PROC_FS
  1829. ioc_proc_init();
  1830. #endif
  1831. return 0;
  1832. }
  1833. subsys_initcall(sba_init); /* must be initialized after ACPI etc., but before any drivers... */
  1834. static int __init
  1835. nosbagart(char *str)
  1836. {
  1837. reserve_sba_gart = 0;
  1838. return 1;
  1839. }
  1840. static int sba_dma_supported (struct device *dev, u64 mask)
  1841. {
  1842. /* make sure it's at least 32bit capable */
  1843. return ((mask & 0xFFFFFFFFUL) == 0xFFFFFFFFUL);
  1844. }
  1845. static int sba_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  1846. {
  1847. return 0;
  1848. }
  1849. __setup("nosbagart", nosbagart);
  1850. static int __init
  1851. sba_page_override(char *str)
  1852. {
  1853. unsigned long page_size;
  1854. page_size = memparse(str, &str);
  1855. switch (page_size) {
  1856. case 4096:
  1857. case 8192:
  1858. case 16384:
  1859. case 65536:
  1860. iovp_shift = ffs(page_size) - 1;
  1861. break;
  1862. default:
  1863. printk("%s: unknown/unsupported iommu page size %ld\n",
  1864. __func__, page_size);
  1865. }
  1866. return 1;
  1867. }
  1868. __setup("sbapagesize=",sba_page_override);
  1869. struct dma_map_ops sba_dma_ops = {
  1870. .alloc_coherent = sba_alloc_coherent,
  1871. .free_coherent = sba_free_coherent,
  1872. .map_page = sba_map_page,
  1873. .unmap_page = sba_unmap_page,
  1874. .map_sg = sba_map_sg_attrs,
  1875. .unmap_sg = sba_unmap_sg_attrs,
  1876. .sync_single_for_cpu = machvec_dma_sync_single,
  1877. .sync_sg_for_cpu = machvec_dma_sync_sg,
  1878. .sync_single_for_device = machvec_dma_sync_single,
  1879. .sync_sg_for_device = machvec_dma_sync_sg,
  1880. .dma_supported = sba_dma_supported,
  1881. .mapping_error = sba_dma_mapping_error,
  1882. };
  1883. void sba_dma_init(void)
  1884. {
  1885. dma_ops = &sba_dma_ops;
  1886. }