sba_iommu.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243
  1. /*
  2. ** System Bus Adapter (SBA) I/O MMU manager
  3. **
  4. ** (c) Copyright 2000-2004 Grant Grundler <grundler @ parisc-linux x org>
  5. ** (c) Copyright 2004 Naresh Kumar Inna <knaresh at india x hp x com>
  6. ** (c) Copyright 2000-2004 Hewlett-Packard Company
  7. **
  8. ** Portions (c) 1999 Dave S. Miller (from sparc64 I/O MMU code)
  9. **
  10. ** This program is free software; you can redistribute it and/or modify
  11. ** it under the terms of the GNU General Public License as published by
  12. ** the Free Software Foundation; either version 2 of the License, or
  13. ** (at your option) any later version.
  14. **
  15. **
  16. ** This module initializes the IOC (I/O Controller) found on B1000/C3000/
  17. ** J5000/J7000/N-class/L-class machines and their successors.
  18. **
  19. ** FIXME: add DMA hint support programming in both sba and lba modules.
  20. */
  21. #include <linux/config.h>
  22. #include <linux/types.h>
  23. #include <linux/kernel.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 <asm/byteorder.h>
  31. #include <asm/io.h>
  32. #include <asm/dma.h> /* for DMA_CHUNK_SIZE */
  33. #include <asm/hardware.h> /* for register_parisc_driver() stuff */
  34. #include <linux/proc_fs.h>
  35. #include <linux/seq_file.h>
  36. #include <asm/runway.h> /* for proc_runway_root */
  37. #include <asm/pdc.h> /* for PDC_MODEL_* */
  38. #include <asm/pdcpat.h> /* for is_pdc_pat() */
  39. #include <asm/parisc-device.h>
  40. /* declared in arch/parisc/kernel/setup.c */
  41. extern struct proc_dir_entry * proc_mckinley_root;
  42. #define MODULE_NAME "SBA"
  43. #ifdef CONFIG_PROC_FS
  44. /* depends on proc fs support. But costs CPU performance */
  45. #undef SBA_COLLECT_STATS
  46. #endif
  47. /*
  48. ** The number of debug flags is a clue - this code is fragile.
  49. ** Don't even think about messing with it unless you have
  50. ** plenty of 710's to sacrifice to the computer gods. :^)
  51. */
  52. #undef DEBUG_SBA_INIT
  53. #undef DEBUG_SBA_RUN
  54. #undef DEBUG_SBA_RUN_SG
  55. #undef DEBUG_SBA_RESOURCE
  56. #undef ASSERT_PDIR_SANITY
  57. #undef DEBUG_LARGE_SG_ENTRIES
  58. #undef DEBUG_DMB_TRAP
  59. #ifdef DEBUG_SBA_INIT
  60. #define DBG_INIT(x...) printk(x)
  61. #else
  62. #define DBG_INIT(x...)
  63. #endif
  64. #ifdef DEBUG_SBA_RUN
  65. #define DBG_RUN(x...) printk(x)
  66. #else
  67. #define DBG_RUN(x...)
  68. #endif
  69. #ifdef DEBUG_SBA_RUN_SG
  70. #define DBG_RUN_SG(x...) printk(x)
  71. #else
  72. #define DBG_RUN_SG(x...)
  73. #endif
  74. #ifdef DEBUG_SBA_RESOURCE
  75. #define DBG_RES(x...) printk(x)
  76. #else
  77. #define DBG_RES(x...)
  78. #endif
  79. #if defined(CONFIG_64BIT)
  80. /* "low end" PA8800 machines use ZX1 chipset: PAT PDC and only run 64-bit */
  81. #define ZX1_SUPPORT
  82. #endif
  83. #define SBA_INLINE __inline__
  84. /*
  85. ** The number of pdir entries to "free" before issueing
  86. ** a read to PCOM register to flush out PCOM writes.
  87. ** Interacts with allocation granularity (ie 4 or 8 entries
  88. ** allocated and free'd/purged at a time might make this
  89. ** less interesting).
  90. */
  91. #define DELAYED_RESOURCE_CNT 16
  92. #define DEFAULT_DMA_HINT_REG 0
  93. #define ASTRO_RUNWAY_PORT 0x582
  94. #define IKE_MERCED_PORT 0x803
  95. #define REO_MERCED_PORT 0x804
  96. #define REOG_MERCED_PORT 0x805
  97. #define PLUTO_MCKINLEY_PORT 0x880
  98. #define SBA_FUNC_ID 0x0000 /* function id */
  99. #define SBA_FCLASS 0x0008 /* function class, bist, header, rev... */
  100. #define IS_ASTRO(id) ((id)->hversion == ASTRO_RUNWAY_PORT)
  101. #define IS_IKE(id) ((id)->hversion == IKE_MERCED_PORT)
  102. #define IS_PLUTO(id) ((id)->hversion == PLUTO_MCKINLEY_PORT)
  103. #define SBA_FUNC_SIZE 4096 /* SBA configuration function reg set */
  104. #define ASTRO_IOC_OFFSET (32 * SBA_FUNC_SIZE)
  105. #define PLUTO_IOC_OFFSET (1 * SBA_FUNC_SIZE)
  106. /* Ike's IOC's occupy functions 2 and 3 */
  107. #define IKE_IOC_OFFSET(p) ((p+2) * SBA_FUNC_SIZE)
  108. #define IOC_CTRL 0x8 /* IOC_CTRL offset */
  109. #define IOC_CTRL_TC (1 << 0) /* TOC Enable */
  110. #define IOC_CTRL_CE (1 << 1) /* Coalesce Enable */
  111. #define IOC_CTRL_DE (1 << 2) /* Dillon Enable */
  112. #define IOC_CTRL_RM (1 << 8) /* Real Mode */
  113. #define IOC_CTRL_NC (1 << 9) /* Non Coherent Mode */
  114. #define IOC_CTRL_D4 (1 << 11) /* Disable 4-byte coalescing */
  115. #define IOC_CTRL_DD (1 << 13) /* Disable distr. LMMIO range coalescing */
  116. #define MAX_IOC 2 /* per Ike. Pluto/Astro only have 1. */
  117. #define ROPES_PER_IOC 8 /* per Ike half or Pluto/Astro */
  118. /*
  119. ** Offsets into MBIB (Function 0 on Ike and hopefully Astro)
  120. ** Firmware programs this stuff. Don't touch it.
  121. */
  122. #define LMMIO_DIRECT0_BASE 0x300
  123. #define LMMIO_DIRECT0_MASK 0x308
  124. #define LMMIO_DIRECT0_ROUTE 0x310
  125. #define LMMIO_DIST_BASE 0x360
  126. #define LMMIO_DIST_MASK 0x368
  127. #define LMMIO_DIST_ROUTE 0x370
  128. #define IOS_DIST_BASE 0x390
  129. #define IOS_DIST_MASK 0x398
  130. #define IOS_DIST_ROUTE 0x3A0
  131. #define IOS_DIRECT_BASE 0x3C0
  132. #define IOS_DIRECT_MASK 0x3C8
  133. #define IOS_DIRECT_ROUTE 0x3D0
  134. /*
  135. ** Offsets into I/O TLB (Function 2 and 3 on Ike)
  136. */
  137. #define ROPE0_CTL 0x200 /* "regbus pci0" */
  138. #define ROPE1_CTL 0x208
  139. #define ROPE2_CTL 0x210
  140. #define ROPE3_CTL 0x218
  141. #define ROPE4_CTL 0x220
  142. #define ROPE5_CTL 0x228
  143. #define ROPE6_CTL 0x230
  144. #define ROPE7_CTL 0x238
  145. #define HF_ENABLE 0x40
  146. #define IOC_IBASE 0x300 /* IO TLB */
  147. #define IOC_IMASK 0x308
  148. #define IOC_PCOM 0x310
  149. #define IOC_TCNFG 0x318
  150. #define IOC_PDIR_BASE 0x320
  151. /* AGP GART driver looks for this */
  152. #define SBA_IOMMU_COOKIE 0x0000badbadc0ffeeUL
  153. /*
  154. ** IOC supports 4/8/16/64KB page sizes (see TCNFG register)
  155. ** It's safer (avoid memory corruption) to keep DMA page mappings
  156. ** equivalently sized to VM PAGE_SIZE.
  157. **
  158. ** We really can't avoid generating a new mapping for each
  159. ** page since the Virtual Coherence Index has to be generated
  160. ** and updated for each page.
  161. **
  162. ** PAGE_SIZE could be greater than IOVP_SIZE. But not the inverse.
  163. */
  164. #define IOVP_SIZE PAGE_SIZE
  165. #define IOVP_SHIFT PAGE_SHIFT
  166. #define IOVP_MASK PAGE_MASK
  167. #define SBA_PERF_CFG 0x708 /* Performance Counter stuff */
  168. #define SBA_PERF_MASK1 0x718
  169. #define SBA_PERF_MASK2 0x730
  170. /*
  171. ** Offsets into PCI Performance Counters (functions 12 and 13)
  172. ** Controlled by PERF registers in function 2 & 3 respectively.
  173. */
  174. #define SBA_PERF_CNT1 0x200
  175. #define SBA_PERF_CNT2 0x208
  176. #define SBA_PERF_CNT3 0x210
  177. struct ioc {
  178. void __iomem *ioc_hpa; /* I/O MMU base address */
  179. char *res_map; /* resource map, bit == pdir entry */
  180. u64 *pdir_base; /* physical base address */
  181. unsigned long ibase; /* pdir IOV Space base - shared w/lba_pci */
  182. unsigned long imask; /* pdir IOV Space mask - shared w/lba_pci */
  183. #ifdef ZX1_SUPPORT
  184. unsigned long iovp_mask; /* help convert IOVA to IOVP */
  185. #endif
  186. unsigned long *res_hint; /* next avail IOVP - circular search */
  187. spinlock_t res_lock;
  188. unsigned int res_bitshift; /* from the LEFT! */
  189. unsigned int res_size; /* size of resource map in bytes */
  190. #ifdef SBA_HINT_SUPPORT
  191. /* FIXME : DMA HINTs not used */
  192. unsigned long hint_mask_pdir; /* bits used for DMA hints */
  193. unsigned int hint_shift_pdir;
  194. #endif
  195. #if DELAYED_RESOURCE_CNT > 0
  196. int saved_cnt;
  197. struct sba_dma_pair {
  198. dma_addr_t iova;
  199. size_t size;
  200. } saved[DELAYED_RESOURCE_CNT];
  201. #endif
  202. #ifdef SBA_COLLECT_STATS
  203. #define SBA_SEARCH_SAMPLE 0x100
  204. unsigned long avg_search[SBA_SEARCH_SAMPLE];
  205. unsigned long avg_idx; /* current index into avg_search */
  206. unsigned long used_pages;
  207. unsigned long msingle_calls;
  208. unsigned long msingle_pages;
  209. unsigned long msg_calls;
  210. unsigned long msg_pages;
  211. unsigned long usingle_calls;
  212. unsigned long usingle_pages;
  213. unsigned long usg_calls;
  214. unsigned long usg_pages;
  215. #endif
  216. /* STUFF We don't need in performance path */
  217. unsigned int pdir_size; /* in bytes, determined by IOV Space size */
  218. };
  219. struct sba_device {
  220. struct sba_device *next; /* list of SBA's in system */
  221. struct parisc_device *dev; /* dev found in bus walk */
  222. struct parisc_device_id *iodc; /* data about dev from firmware */
  223. const char *name;
  224. void __iomem *sba_hpa; /* base address */
  225. spinlock_t sba_lock;
  226. unsigned int flags; /* state/functionality enabled */
  227. unsigned int hw_rev; /* HW revision of chip */
  228. struct resource chip_resv; /* MMIO reserved for chip */
  229. struct resource iommu_resv; /* MMIO reserved for iommu */
  230. unsigned int num_ioc; /* number of on-board IOC's */
  231. struct ioc ioc[MAX_IOC];
  232. };
  233. static struct sba_device *sba_list;
  234. static unsigned long ioc_needs_fdc = 0;
  235. /* global count of IOMMUs in the system */
  236. static unsigned int global_ioc_cnt = 0;
  237. /* PA8700 (Piranha 2.2) bug workaround */
  238. static unsigned long piranha_bad_128k = 0;
  239. /* Looks nice and keeps the compiler happy */
  240. #define SBA_DEV(d) ((struct sba_device *) (d))
  241. #ifdef SBA_AGP_SUPPORT
  242. static int reserve_sba_gart = 1;
  243. #endif
  244. #define ROUNDUP(x,y) ((x + ((y)-1)) & ~((y)-1))
  245. /************************************
  246. ** SBA register read and write support
  247. **
  248. ** BE WARNED: register writes are posted.
  249. ** (ie follow writes which must reach HW with a read)
  250. **
  251. ** Superdome (in particular, REO) allows only 64-bit CSR accesses.
  252. */
  253. #define READ_REG32(addr) le32_to_cpu(__raw_readl(addr))
  254. #define READ_REG64(addr) le64_to_cpu(__raw_readq(addr))
  255. #define WRITE_REG32(val, addr) __raw_writel(cpu_to_le32(val), addr)
  256. #define WRITE_REG64(val, addr) __raw_writeq(cpu_to_le64(val), addr)
  257. #ifdef CONFIG_64BIT
  258. #define READ_REG(addr) READ_REG64(addr)
  259. #define WRITE_REG(value, addr) WRITE_REG64(value, addr)
  260. #else
  261. #define READ_REG(addr) READ_REG32(addr)
  262. #define WRITE_REG(value, addr) WRITE_REG32(value, addr)
  263. #endif
  264. #ifdef DEBUG_SBA_INIT
  265. /* NOTE: When CONFIG_64BIT isn't defined, READ_REG64() is two 32-bit reads */
  266. /**
  267. * sba_dump_ranges - debugging only - print ranges assigned to this IOA
  268. * @hpa: base address of the sba
  269. *
  270. * Print the MMIO and IO Port address ranges forwarded by an Astro/Ike/RIO
  271. * IO Adapter (aka Bus Converter).
  272. */
  273. static void
  274. sba_dump_ranges(void __iomem *hpa)
  275. {
  276. DBG_INIT("SBA at 0x%p\n", hpa);
  277. DBG_INIT("IOS_DIST_BASE : %Lx\n", READ_REG64(hpa+IOS_DIST_BASE));
  278. DBG_INIT("IOS_DIST_MASK : %Lx\n", READ_REG64(hpa+IOS_DIST_MASK));
  279. DBG_INIT("IOS_DIST_ROUTE : %Lx\n", READ_REG64(hpa+IOS_DIST_ROUTE));
  280. DBG_INIT("\n");
  281. DBG_INIT("IOS_DIRECT_BASE : %Lx\n", READ_REG64(hpa+IOS_DIRECT_BASE));
  282. DBG_INIT("IOS_DIRECT_MASK : %Lx\n", READ_REG64(hpa+IOS_DIRECT_MASK));
  283. DBG_INIT("IOS_DIRECT_ROUTE: %Lx\n", READ_REG64(hpa+IOS_DIRECT_ROUTE));
  284. }
  285. /**
  286. * sba_dump_tlb - debugging only - print IOMMU operating parameters
  287. * @hpa: base address of the IOMMU
  288. *
  289. * Print the size/location of the IO MMU PDIR.
  290. */
  291. static void sba_dump_tlb(void __iomem *hpa)
  292. {
  293. DBG_INIT("IO TLB at 0x%p\n", hpa);
  294. DBG_INIT("IOC_IBASE : 0x%Lx\n", READ_REG64(hpa+IOC_IBASE));
  295. DBG_INIT("IOC_IMASK : 0x%Lx\n", READ_REG64(hpa+IOC_IMASK));
  296. DBG_INIT("IOC_TCNFG : 0x%Lx\n", READ_REG64(hpa+IOC_TCNFG));
  297. DBG_INIT("IOC_PDIR_BASE: 0x%Lx\n", READ_REG64(hpa+IOC_PDIR_BASE));
  298. DBG_INIT("\n");
  299. }
  300. #else
  301. #define sba_dump_ranges(x)
  302. #define sba_dump_tlb(x)
  303. #endif /* DEBUG_SBA_INIT */
  304. #ifdef ASSERT_PDIR_SANITY
  305. /**
  306. * sba_dump_pdir_entry - debugging only - print one IOMMU PDIR entry
  307. * @ioc: IO MMU structure which owns the pdir we are interested in.
  308. * @msg: text to print ont the output line.
  309. * @pide: pdir index.
  310. *
  311. * Print one entry of the IO MMU PDIR in human readable form.
  312. */
  313. static void
  314. sba_dump_pdir_entry(struct ioc *ioc, char *msg, uint pide)
  315. {
  316. /* start printing from lowest pde in rval */
  317. u64 *ptr = &(ioc->pdir_base[pide & (~0U * BITS_PER_LONG)]);
  318. unsigned long *rptr = (unsigned long *) &(ioc->res_map[(pide >>3) & ~(sizeof(unsigned long) - 1)]);
  319. uint rcnt;
  320. printk(KERN_DEBUG "SBA: %s rp %p bit %d rval 0x%lx\n",
  321. msg,
  322. rptr, pide & (BITS_PER_LONG - 1), *rptr);
  323. rcnt = 0;
  324. while (rcnt < BITS_PER_LONG) {
  325. printk(KERN_DEBUG "%s %2d %p %016Lx\n",
  326. (rcnt == (pide & (BITS_PER_LONG - 1)))
  327. ? " -->" : " ",
  328. rcnt, ptr, *ptr );
  329. rcnt++;
  330. ptr++;
  331. }
  332. printk(KERN_DEBUG "%s", msg);
  333. }
  334. /**
  335. * sba_check_pdir - debugging only - consistency checker
  336. * @ioc: IO MMU structure which owns the pdir we are interested in.
  337. * @msg: text to print ont the output line.
  338. *
  339. * Verify the resource map and pdir state is consistent
  340. */
  341. static int
  342. sba_check_pdir(struct ioc *ioc, char *msg)
  343. {
  344. u32 *rptr_end = (u32 *) &(ioc->res_map[ioc->res_size]);
  345. u32 *rptr = (u32 *) ioc->res_map; /* resource map ptr */
  346. u64 *pptr = ioc->pdir_base; /* pdir ptr */
  347. uint pide = 0;
  348. while (rptr < rptr_end) {
  349. u32 rval = *rptr;
  350. int rcnt = 32; /* number of bits we might check */
  351. while (rcnt) {
  352. /* Get last byte and highest bit from that */
  353. u32 pde = ((u32) (((char *)pptr)[7])) << 24;
  354. if ((rval ^ pde) & 0x80000000)
  355. {
  356. /*
  357. ** BUMMER! -- res_map != pdir --
  358. ** Dump rval and matching pdir entries
  359. */
  360. sba_dump_pdir_entry(ioc, msg, pide);
  361. return(1);
  362. }
  363. rcnt--;
  364. rval <<= 1; /* try the next bit */
  365. pptr++;
  366. pide++;
  367. }
  368. rptr++; /* look at next word of res_map */
  369. }
  370. /* It'd be nice if we always got here :^) */
  371. return 0;
  372. }
  373. /**
  374. * sba_dump_sg - debugging only - print Scatter-Gather list
  375. * @ioc: IO MMU structure which owns the pdir we are interested in.
  376. * @startsg: head of the SG list
  377. * @nents: number of entries in SG list
  378. *
  379. * print the SG list so we can verify it's correct by hand.
  380. */
  381. static void
  382. sba_dump_sg( struct ioc *ioc, struct scatterlist *startsg, int nents)
  383. {
  384. while (nents-- > 0) {
  385. printk(KERN_DEBUG " %d : %08lx/%05x %p/%05x\n",
  386. nents,
  387. (unsigned long) sg_dma_address(startsg),
  388. sg_dma_len(startsg),
  389. sg_virt_addr(startsg), startsg->length);
  390. startsg++;
  391. }
  392. }
  393. #endif /* ASSERT_PDIR_SANITY */
  394. /**************************************************************
  395. *
  396. * I/O Pdir Resource Management
  397. *
  398. * Bits set in the resource map are in use.
  399. * Each bit can represent a number of pages.
  400. * LSbs represent lower addresses (IOVA's).
  401. *
  402. ***************************************************************/
  403. #define PAGES_PER_RANGE 1 /* could increase this to 4 or 8 if needed */
  404. /* Convert from IOVP to IOVA and vice versa. */
  405. #ifdef ZX1_SUPPORT
  406. /* Pluto (aka ZX1) boxes need to set or clear the ibase bits appropriately */
  407. #define SBA_IOVA(ioc,iovp,offset,hint_reg) ((ioc->ibase) | (iovp) | (offset))
  408. #define SBA_IOVP(ioc,iova) ((iova) & (ioc)->iovp_mask)
  409. #else
  410. /* only support Astro and ancestors. Saves a few cycles in key places */
  411. #define SBA_IOVA(ioc,iovp,offset,hint_reg) ((iovp) | (offset))
  412. #define SBA_IOVP(ioc,iova) (iova)
  413. #endif
  414. #define PDIR_INDEX(iovp) ((iovp)>>IOVP_SHIFT)
  415. #define RESMAP_MASK(n) (~0UL << (BITS_PER_LONG - (n)))
  416. #define RESMAP_IDX_MASK (sizeof(unsigned long) - 1)
  417. /**
  418. * sba_search_bitmap - find free space in IO PDIR resource bitmap
  419. * @ioc: IO MMU structure which owns the pdir we are interested in.
  420. * @bits_wanted: number of entries we need.
  421. *
  422. * Find consecutive free bits in resource bitmap.
  423. * Each bit represents one entry in the IO Pdir.
  424. * Cool perf optimization: search for log2(size) bits at a time.
  425. */
  426. static SBA_INLINE unsigned long
  427. sba_search_bitmap(struct ioc *ioc, unsigned long bits_wanted)
  428. {
  429. unsigned long *res_ptr = ioc->res_hint;
  430. unsigned long *res_end = (unsigned long *) &(ioc->res_map[ioc->res_size]);
  431. unsigned long pide = ~0UL;
  432. if (bits_wanted > (BITS_PER_LONG/2)) {
  433. /* Search word at a time - no mask needed */
  434. for(; res_ptr < res_end; ++res_ptr) {
  435. if (*res_ptr == 0) {
  436. *res_ptr = RESMAP_MASK(bits_wanted);
  437. pide = ((unsigned long)res_ptr - (unsigned long)ioc->res_map);
  438. pide <<= 3; /* convert to bit address */
  439. break;
  440. }
  441. }
  442. /* point to the next word on next pass */
  443. res_ptr++;
  444. ioc->res_bitshift = 0;
  445. } else {
  446. /*
  447. ** Search the resource bit map on well-aligned values.
  448. ** "o" is the alignment.
  449. ** We need the alignment to invalidate I/O TLB using
  450. ** SBA HW features in the unmap path.
  451. */
  452. unsigned long o = 1 << get_order(bits_wanted << PAGE_SHIFT);
  453. uint bitshiftcnt = ROUNDUP(ioc->res_bitshift, o);
  454. unsigned long mask;
  455. if (bitshiftcnt >= BITS_PER_LONG) {
  456. bitshiftcnt = 0;
  457. res_ptr++;
  458. }
  459. mask = RESMAP_MASK(bits_wanted) >> bitshiftcnt;
  460. DBG_RES("%s() o %ld %p", __FUNCTION__, o, res_ptr);
  461. while(res_ptr < res_end)
  462. {
  463. DBG_RES(" %p %lx %lx\n", res_ptr, mask, *res_ptr);
  464. WARN_ON(mask == 0);
  465. if(((*res_ptr) & mask) == 0) {
  466. *res_ptr |= mask; /* mark resources busy! */
  467. pide = ((unsigned long)res_ptr - (unsigned long)ioc->res_map);
  468. pide <<= 3; /* convert to bit address */
  469. pide += bitshiftcnt;
  470. break;
  471. }
  472. mask >>= o;
  473. bitshiftcnt += o;
  474. if (mask == 0) {
  475. mask = RESMAP_MASK(bits_wanted);
  476. bitshiftcnt=0;
  477. res_ptr++;
  478. }
  479. }
  480. /* look in the same word on the next pass */
  481. ioc->res_bitshift = bitshiftcnt + bits_wanted;
  482. }
  483. /* wrapped ? */
  484. if (res_end <= res_ptr) {
  485. ioc->res_hint = (unsigned long *) ioc->res_map;
  486. ioc->res_bitshift = 0;
  487. } else {
  488. ioc->res_hint = res_ptr;
  489. }
  490. return (pide);
  491. }
  492. /**
  493. * sba_alloc_range - find free bits and mark them in IO PDIR resource bitmap
  494. * @ioc: IO MMU structure which owns the pdir we are interested in.
  495. * @size: number of bytes to create a mapping for
  496. *
  497. * Given a size, find consecutive unmarked and then mark those bits in the
  498. * resource bit map.
  499. */
  500. static int
  501. sba_alloc_range(struct ioc *ioc, size_t size)
  502. {
  503. unsigned int pages_needed = size >> IOVP_SHIFT;
  504. #ifdef SBA_COLLECT_STATS
  505. unsigned long cr_start = mfctl(16);
  506. #endif
  507. unsigned long pide;
  508. pide = sba_search_bitmap(ioc, pages_needed);
  509. if (pide >= (ioc->res_size << 3)) {
  510. pide = sba_search_bitmap(ioc, pages_needed);
  511. if (pide >= (ioc->res_size << 3))
  512. panic("%s: I/O MMU @ %p is out of mapping resources\n",
  513. __FILE__, ioc->ioc_hpa);
  514. }
  515. #ifdef ASSERT_PDIR_SANITY
  516. /* verify the first enable bit is clear */
  517. if(0x00 != ((u8 *) ioc->pdir_base)[pide*sizeof(u64) + 7]) {
  518. sba_dump_pdir_entry(ioc, "sba_search_bitmap() botched it?", pide);
  519. }
  520. #endif
  521. DBG_RES("%s(%x) %d -> %lx hint %x/%x\n",
  522. __FUNCTION__, size, pages_needed, pide,
  523. (uint) ((unsigned long) ioc->res_hint - (unsigned long) ioc->res_map),
  524. ioc->res_bitshift );
  525. #ifdef SBA_COLLECT_STATS
  526. {
  527. unsigned long cr_end = mfctl(16);
  528. unsigned long tmp = cr_end - cr_start;
  529. /* check for roll over */
  530. cr_start = (cr_end < cr_start) ? -(tmp) : (tmp);
  531. }
  532. ioc->avg_search[ioc->avg_idx++] = cr_start;
  533. ioc->avg_idx &= SBA_SEARCH_SAMPLE - 1;
  534. ioc->used_pages += pages_needed;
  535. #endif
  536. return (pide);
  537. }
  538. /**
  539. * sba_free_range - unmark bits in IO PDIR resource bitmap
  540. * @ioc: IO MMU structure which owns the pdir we are interested in.
  541. * @iova: IO virtual address which was previously allocated.
  542. * @size: number of bytes to create a mapping for
  543. *
  544. * clear bits in the ioc's resource map
  545. */
  546. static SBA_INLINE void
  547. sba_free_range(struct ioc *ioc, dma_addr_t iova, size_t size)
  548. {
  549. unsigned long iovp = SBA_IOVP(ioc, iova);
  550. unsigned int pide = PDIR_INDEX(iovp);
  551. unsigned int ridx = pide >> 3; /* convert bit to byte address */
  552. unsigned long *res_ptr = (unsigned long *) &((ioc)->res_map[ridx & ~RESMAP_IDX_MASK]);
  553. int bits_not_wanted = size >> IOVP_SHIFT;
  554. /* 3-bits "bit" address plus 2 (or 3) bits for "byte" == bit in word */
  555. unsigned long m = RESMAP_MASK(bits_not_wanted) >> (pide & (BITS_PER_LONG - 1));
  556. DBG_RES("%s( ,%x,%x) %x/%lx %x %p %lx\n",
  557. __FUNCTION__, (uint) iova, size,
  558. bits_not_wanted, m, pide, res_ptr, *res_ptr);
  559. #ifdef SBA_COLLECT_STATS
  560. ioc->used_pages -= bits_not_wanted;
  561. #endif
  562. *res_ptr &= ~m;
  563. }
  564. /**************************************************************
  565. *
  566. * "Dynamic DMA Mapping" support (aka "Coherent I/O")
  567. *
  568. ***************************************************************/
  569. #ifdef SBA_HINT_SUPPORT
  570. #define SBA_DMA_HINT(ioc, val) ((val) << (ioc)->hint_shift_pdir)
  571. #endif
  572. typedef unsigned long space_t;
  573. #define KERNEL_SPACE 0
  574. /**
  575. * sba_io_pdir_entry - fill in one IO PDIR entry
  576. * @pdir_ptr: pointer to IO PDIR entry
  577. * @sid: process Space ID - currently only support KERNEL_SPACE
  578. * @vba: Virtual CPU address of buffer to map
  579. * @hint: DMA hint set to use for this mapping
  580. *
  581. * SBA Mapping Routine
  582. *
  583. * Given a virtual address (vba, arg2) and space id, (sid, arg1)
  584. * sba_io_pdir_entry() loads the I/O PDIR entry pointed to by
  585. * pdir_ptr (arg0).
  586. * Using the bass-ackwards HP bit numbering, Each IO Pdir entry
  587. * for Astro/Ike looks like:
  588. *
  589. *
  590. * 0 19 51 55 63
  591. * +-+---------------------+----------------------------------+----+--------+
  592. * |V| U | PPN[43:12] | U | VI |
  593. * +-+---------------------+----------------------------------+----+--------+
  594. *
  595. * Pluto is basically identical, supports fewer physical address bits:
  596. *
  597. * 0 23 51 55 63
  598. * +-+------------------------+-------------------------------+----+--------+
  599. * |V| U | PPN[39:12] | U | VI |
  600. * +-+------------------------+-------------------------------+----+--------+
  601. *
  602. * V == Valid Bit (Most Significant Bit is bit 0)
  603. * U == Unused
  604. * PPN == Physical Page Number
  605. * VI == Virtual Index (aka Coherent Index)
  606. *
  607. * LPA instruction output is put into PPN field.
  608. * LCI (Load Coherence Index) instruction provides the "VI" bits.
  609. *
  610. * We pre-swap the bytes since PCX-W is Big Endian and the
  611. * IOMMU uses little endian for the pdir.
  612. */
  613. void SBA_INLINE
  614. sba_io_pdir_entry(u64 *pdir_ptr, space_t sid, unsigned long vba,
  615. unsigned long hint)
  616. {
  617. u64 pa; /* physical address */
  618. register unsigned ci; /* coherent index */
  619. pa = virt_to_phys(vba);
  620. pa &= IOVP_MASK;
  621. mtsp(sid,1);
  622. asm("lci 0(%%sr1, %1), %0" : "=r" (ci) : "r" (vba));
  623. pa |= (ci >> 12) & 0xff; /* move CI (8 bits) into lowest byte */
  624. pa |= 0x8000000000000000ULL; /* set "valid" bit */
  625. *pdir_ptr = cpu_to_le64(pa); /* swap and store into I/O Pdir */
  626. /*
  627. * If the PDC_MODEL capabilities has Non-coherent IO-PDIR bit set
  628. * (bit #61, big endian), we have to flush and sync every time
  629. * IO-PDIR is changed in Ike/Astro.
  630. */
  631. if (ioc_needs_fdc)
  632. asm volatile("fdc %%r0(%0)" : : "r" (pdir_ptr));
  633. }
  634. /**
  635. * sba_mark_invalid - invalidate one or more IO PDIR entries
  636. * @ioc: IO MMU structure which owns the pdir we are interested in.
  637. * @iova: IO Virtual Address mapped earlier
  638. * @byte_cnt: number of bytes this mapping covers.
  639. *
  640. * Marking the IO PDIR entry(ies) as Invalid and invalidate
  641. * corresponding IO TLB entry. The Ike PCOM (Purge Command Register)
  642. * is to purge stale entries in the IO TLB when unmapping entries.
  643. *
  644. * The PCOM register supports purging of multiple pages, with a minium
  645. * of 1 page and a maximum of 2GB. Hardware requires the address be
  646. * aligned to the size of the range being purged. The size of the range
  647. * must be a power of 2. The "Cool perf optimization" in the
  648. * allocation routine helps keep that true.
  649. */
  650. static SBA_INLINE void
  651. sba_mark_invalid(struct ioc *ioc, dma_addr_t iova, size_t byte_cnt)
  652. {
  653. u32 iovp = (u32) SBA_IOVP(ioc,iova);
  654. u64 *pdir_ptr = &ioc->pdir_base[PDIR_INDEX(iovp)];
  655. #ifdef ASSERT_PDIR_SANITY
  656. /* Assert first pdir entry is set.
  657. **
  658. ** Even though this is a big-endian machine, the entries
  659. ** in the iopdir are little endian. That's why we look at
  660. ** the byte at +7 instead of at +0.
  661. */
  662. if (0x80 != (((u8 *) pdir_ptr)[7])) {
  663. sba_dump_pdir_entry(ioc,"sba_mark_invalid()", PDIR_INDEX(iovp));
  664. }
  665. #endif
  666. if (byte_cnt > IOVP_SIZE)
  667. {
  668. #if 0
  669. unsigned long entries_per_cacheline = ioc_needs_fdc ?
  670. L1_CACHE_ALIGN(((unsigned long) pdir_ptr))
  671. - (unsigned long) pdir_ptr;
  672. : 262144;
  673. #endif
  674. /* set "size" field for PCOM */
  675. iovp |= get_order(byte_cnt) + PAGE_SHIFT;
  676. do {
  677. /* clear I/O Pdir entry "valid" bit first */
  678. ((u8 *) pdir_ptr)[7] = 0;
  679. if (ioc_needs_fdc) {
  680. asm volatile("fdc %%r0(%0)" : : "r" (pdir_ptr));
  681. #if 0
  682. entries_per_cacheline = L1_CACHE_SHIFT - 3;
  683. #endif
  684. }
  685. pdir_ptr++;
  686. byte_cnt -= IOVP_SIZE;
  687. } while (byte_cnt > IOVP_SIZE);
  688. } else
  689. iovp |= IOVP_SHIFT; /* set "size" field for PCOM */
  690. /*
  691. ** clear I/O PDIR entry "valid" bit.
  692. ** We have to R/M/W the cacheline regardless how much of the
  693. ** pdir entry that we clobber.
  694. ** The rest of the entry would be useful for debugging if we
  695. ** could dump core on HPMC.
  696. */
  697. ((u8 *) pdir_ptr)[7] = 0;
  698. if (ioc_needs_fdc)
  699. asm volatile("fdc %%r0(%0)" : : "r" (pdir_ptr));
  700. WRITE_REG( SBA_IOVA(ioc, iovp, 0, 0), ioc->ioc_hpa+IOC_PCOM);
  701. }
  702. /**
  703. * sba_dma_supported - PCI driver can query DMA support
  704. * @dev: instance of PCI owned by the driver that's asking
  705. * @mask: number of address bits this PCI device can handle
  706. *
  707. * See Documentation/DMA-mapping.txt
  708. */
  709. static int sba_dma_supported( struct device *dev, u64 mask)
  710. {
  711. struct ioc *ioc;
  712. if (dev == NULL) {
  713. printk(KERN_ERR MODULE_NAME ": EISA/ISA/et al not supported\n");
  714. BUG();
  715. return(0);
  716. }
  717. /* Documentation/DMA-mapping.txt tells drivers to try 64-bit first,
  718. * then fall back to 32-bit if that fails.
  719. * We are just "encouraging" 32-bit DMA masks here since we can
  720. * never allow IOMMU bypass unless we add special support for ZX1.
  721. */
  722. if (mask > ~0U)
  723. return 0;
  724. ioc = GET_IOC(dev);
  725. /*
  726. * check if mask is >= than the current max IO Virt Address
  727. * The max IO Virt address will *always* < 30 bits.
  728. */
  729. return((int)(mask >= (ioc->ibase - 1 +
  730. (ioc->pdir_size / sizeof(u64) * IOVP_SIZE) )));
  731. }
  732. /**
  733. * sba_map_single - map one buffer and return IOVA for DMA
  734. * @dev: instance of PCI owned by the driver that's asking.
  735. * @addr: driver buffer to map.
  736. * @size: number of bytes to map in driver buffer.
  737. * @direction: R/W or both.
  738. *
  739. * See Documentation/DMA-mapping.txt
  740. */
  741. static dma_addr_t
  742. sba_map_single(struct device *dev, void *addr, size_t size,
  743. enum dma_data_direction direction)
  744. {
  745. struct ioc *ioc;
  746. unsigned long flags;
  747. dma_addr_t iovp;
  748. dma_addr_t offset;
  749. u64 *pdir_start;
  750. int pide;
  751. ioc = GET_IOC(dev);
  752. /* save offset bits */
  753. offset = ((dma_addr_t) (long) addr) & ~IOVP_MASK;
  754. /* round up to nearest IOVP_SIZE */
  755. size = (size + offset + ~IOVP_MASK) & IOVP_MASK;
  756. spin_lock_irqsave(&ioc->res_lock, flags);
  757. #ifdef ASSERT_PDIR_SANITY
  758. sba_check_pdir(ioc,"Check before sba_map_single()");
  759. #endif
  760. #ifdef SBA_COLLECT_STATS
  761. ioc->msingle_calls++;
  762. ioc->msingle_pages += size >> IOVP_SHIFT;
  763. #endif
  764. pide = sba_alloc_range(ioc, size);
  765. iovp = (dma_addr_t) pide << IOVP_SHIFT;
  766. DBG_RUN("%s() 0x%p -> 0x%lx\n",
  767. __FUNCTION__, addr, (long) iovp | offset);
  768. pdir_start = &(ioc->pdir_base[pide]);
  769. while (size > 0) {
  770. sba_io_pdir_entry(pdir_start, KERNEL_SPACE, (unsigned long) addr, 0);
  771. DBG_RUN(" pdir 0x%p %02x%02x%02x%02x%02x%02x%02x%02x\n",
  772. pdir_start,
  773. (u8) (((u8 *) pdir_start)[7]),
  774. (u8) (((u8 *) pdir_start)[6]),
  775. (u8) (((u8 *) pdir_start)[5]),
  776. (u8) (((u8 *) pdir_start)[4]),
  777. (u8) (((u8 *) pdir_start)[3]),
  778. (u8) (((u8 *) pdir_start)[2]),
  779. (u8) (((u8 *) pdir_start)[1]),
  780. (u8) (((u8 *) pdir_start)[0])
  781. );
  782. addr += IOVP_SIZE;
  783. size -= IOVP_SIZE;
  784. pdir_start++;
  785. }
  786. /* force FDC ops in io_pdir_entry() to be visible to IOMMU */
  787. if (ioc_needs_fdc)
  788. asm volatile("sync" : : );
  789. #ifdef ASSERT_PDIR_SANITY
  790. sba_check_pdir(ioc,"Check after sba_map_single()");
  791. #endif
  792. spin_unlock_irqrestore(&ioc->res_lock, flags);
  793. /* form complete address */
  794. return SBA_IOVA(ioc, iovp, offset, DEFAULT_DMA_HINT_REG);
  795. }
  796. /**
  797. * sba_unmap_single - unmap one IOVA and free resources
  798. * @dev: instance of PCI owned by the driver that's asking.
  799. * @iova: IOVA of driver buffer previously mapped.
  800. * @size: number of bytes mapped in driver buffer.
  801. * @direction: R/W or both.
  802. *
  803. * See Documentation/DMA-mapping.txt
  804. */
  805. static void
  806. sba_unmap_single(struct device *dev, dma_addr_t iova, size_t size,
  807. enum dma_data_direction direction)
  808. {
  809. struct ioc *ioc;
  810. #if DELAYED_RESOURCE_CNT > 0
  811. struct sba_dma_pair *d;
  812. #endif
  813. unsigned long flags;
  814. dma_addr_t offset;
  815. DBG_RUN("%s() iovp 0x%lx/%x\n", __FUNCTION__, (long) iova, size);
  816. ioc = GET_IOC(dev);
  817. offset = iova & ~IOVP_MASK;
  818. iova ^= offset; /* clear offset bits */
  819. size += offset;
  820. size = ROUNDUP(size, IOVP_SIZE);
  821. spin_lock_irqsave(&ioc->res_lock, flags);
  822. #ifdef SBA_COLLECT_STATS
  823. ioc->usingle_calls++;
  824. ioc->usingle_pages += size >> IOVP_SHIFT;
  825. #endif
  826. sba_mark_invalid(ioc, iova, size);
  827. #if DELAYED_RESOURCE_CNT > 0
  828. /* Delaying when we re-use a IO Pdir entry reduces the number
  829. * of MMIO reads needed to flush writes to the PCOM register.
  830. */
  831. d = &(ioc->saved[ioc->saved_cnt]);
  832. d->iova = iova;
  833. d->size = size;
  834. if (++(ioc->saved_cnt) >= DELAYED_RESOURCE_CNT) {
  835. int cnt = ioc->saved_cnt;
  836. while (cnt--) {
  837. sba_free_range(ioc, d->iova, d->size);
  838. d--;
  839. }
  840. ioc->saved_cnt = 0;
  841. READ_REG(ioc->ioc_hpa+IOC_PCOM); /* flush purges */
  842. }
  843. #else /* DELAYED_RESOURCE_CNT == 0 */
  844. sba_free_range(ioc, iova, size);
  845. /* If fdc's were issued, force fdc's to be visible now */
  846. if (ioc_needs_fdc)
  847. asm volatile("sync" : : );
  848. READ_REG(ioc->ioc_hpa+IOC_PCOM); /* flush purges */
  849. #endif /* DELAYED_RESOURCE_CNT == 0 */
  850. spin_unlock_irqrestore(&ioc->res_lock, flags);
  851. /* XXX REVISIT for 2.5 Linux - need syncdma for zero-copy support.
  852. ** For Astro based systems this isn't a big deal WRT performance.
  853. ** As long as 2.4 kernels copyin/copyout data from/to userspace,
  854. ** we don't need the syncdma. The issue here is I/O MMU cachelines
  855. ** are *not* coherent in all cases. May be hwrev dependent.
  856. ** Need to investigate more.
  857. asm volatile("syncdma");
  858. */
  859. }
  860. /**
  861. * sba_alloc_consistent - allocate/map shared mem for DMA
  862. * @hwdev: instance of PCI owned by the driver that's asking.
  863. * @size: number of bytes mapped in driver buffer.
  864. * @dma_handle: IOVA of new buffer.
  865. *
  866. * See Documentation/DMA-mapping.txt
  867. */
  868. static void *sba_alloc_consistent(struct device *hwdev, size_t size,
  869. dma_addr_t *dma_handle, gfp_t gfp)
  870. {
  871. void *ret;
  872. if (!hwdev) {
  873. /* only support PCI */
  874. *dma_handle = 0;
  875. return 0;
  876. }
  877. ret = (void *) __get_free_pages(gfp, get_order(size));
  878. if (ret) {
  879. memset(ret, 0, size);
  880. *dma_handle = sba_map_single(hwdev, ret, size, 0);
  881. }
  882. return ret;
  883. }
  884. /**
  885. * sba_free_consistent - free/unmap shared mem for DMA
  886. * @hwdev: instance of PCI owned by the driver that's asking.
  887. * @size: number of bytes mapped in driver buffer.
  888. * @vaddr: virtual address IOVA of "consistent" buffer.
  889. * @dma_handler: IO virtual address of "consistent" buffer.
  890. *
  891. * See Documentation/DMA-mapping.txt
  892. */
  893. static void
  894. sba_free_consistent(struct device *hwdev, size_t size, void *vaddr,
  895. dma_addr_t dma_handle)
  896. {
  897. sba_unmap_single(hwdev, dma_handle, size, 0);
  898. free_pages((unsigned long) vaddr, get_order(size));
  899. }
  900. /*
  901. ** Since 0 is a valid pdir_base index value, can't use that
  902. ** to determine if a value is valid or not. Use a flag to indicate
  903. ** the SG list entry contains a valid pdir index.
  904. */
  905. #define PIDE_FLAG 0x80000000UL
  906. #ifdef SBA_COLLECT_STATS
  907. #define IOMMU_MAP_STATS
  908. #endif
  909. #include "iommu-helpers.h"
  910. #ifdef DEBUG_LARGE_SG_ENTRIES
  911. int dump_run_sg = 0;
  912. #endif
  913. /**
  914. * sba_map_sg - map Scatter/Gather list
  915. * @dev: instance of PCI owned by the driver that's asking.
  916. * @sglist: array of buffer/length pairs
  917. * @nents: number of entries in list
  918. * @direction: R/W or both.
  919. *
  920. * See Documentation/DMA-mapping.txt
  921. */
  922. static int
  923. sba_map_sg(struct device *dev, struct scatterlist *sglist, int nents,
  924. enum dma_data_direction direction)
  925. {
  926. struct ioc *ioc;
  927. int coalesced, filled = 0;
  928. unsigned long flags;
  929. DBG_RUN_SG("%s() START %d entries\n", __FUNCTION__, nents);
  930. ioc = GET_IOC(dev);
  931. /* Fast path single entry scatterlists. */
  932. if (nents == 1) {
  933. sg_dma_address(sglist) = sba_map_single(dev,
  934. (void *)sg_virt_addr(sglist),
  935. sglist->length, direction);
  936. sg_dma_len(sglist) = sglist->length;
  937. return 1;
  938. }
  939. spin_lock_irqsave(&ioc->res_lock, flags);
  940. #ifdef ASSERT_PDIR_SANITY
  941. if (sba_check_pdir(ioc,"Check before sba_map_sg()"))
  942. {
  943. sba_dump_sg(ioc, sglist, nents);
  944. panic("Check before sba_map_sg()");
  945. }
  946. #endif
  947. #ifdef SBA_COLLECT_STATS
  948. ioc->msg_calls++;
  949. #endif
  950. /*
  951. ** First coalesce the chunks and allocate I/O pdir space
  952. **
  953. ** If this is one DMA stream, we can properly map using the
  954. ** correct virtual address associated with each DMA page.
  955. ** w/o this association, we wouldn't have coherent DMA!
  956. ** Access to the virtual address is what forces a two pass algorithm.
  957. */
  958. coalesced = iommu_coalesce_chunks(ioc, sglist, nents, sba_alloc_range);
  959. /*
  960. ** Program the I/O Pdir
  961. **
  962. ** map the virtual addresses to the I/O Pdir
  963. ** o dma_address will contain the pdir index
  964. ** o dma_len will contain the number of bytes to map
  965. ** o address contains the virtual address.
  966. */
  967. filled = iommu_fill_pdir(ioc, sglist, nents, 0, sba_io_pdir_entry);
  968. /* force FDC ops in io_pdir_entry() to be visible to IOMMU */
  969. if (ioc_needs_fdc)
  970. asm volatile("sync" : : );
  971. #ifdef ASSERT_PDIR_SANITY
  972. if (sba_check_pdir(ioc,"Check after sba_map_sg()"))
  973. {
  974. sba_dump_sg(ioc, sglist, nents);
  975. panic("Check after sba_map_sg()\n");
  976. }
  977. #endif
  978. spin_unlock_irqrestore(&ioc->res_lock, flags);
  979. DBG_RUN_SG("%s() DONE %d mappings\n", __FUNCTION__, filled);
  980. return filled;
  981. }
  982. /**
  983. * sba_unmap_sg - unmap Scatter/Gather list
  984. * @dev: instance of PCI owned by the driver that's asking.
  985. * @sglist: array of buffer/length pairs
  986. * @nents: number of entries in list
  987. * @direction: R/W or both.
  988. *
  989. * See Documentation/DMA-mapping.txt
  990. */
  991. static void
  992. sba_unmap_sg(struct device *dev, struct scatterlist *sglist, int nents,
  993. enum dma_data_direction direction)
  994. {
  995. struct ioc *ioc;
  996. #ifdef ASSERT_PDIR_SANITY
  997. unsigned long flags;
  998. #endif
  999. DBG_RUN_SG("%s() START %d entries, %p,%x\n",
  1000. __FUNCTION__, nents, sg_virt_addr(sglist), sglist->length);
  1001. ioc = GET_IOC(dev);
  1002. #ifdef SBA_COLLECT_STATS
  1003. ioc->usg_calls++;
  1004. #endif
  1005. #ifdef ASSERT_PDIR_SANITY
  1006. spin_lock_irqsave(&ioc->res_lock, flags);
  1007. sba_check_pdir(ioc,"Check before sba_unmap_sg()");
  1008. spin_unlock_irqrestore(&ioc->res_lock, flags);
  1009. #endif
  1010. while (sg_dma_len(sglist) && nents--) {
  1011. sba_unmap_single(dev, sg_dma_address(sglist), sg_dma_len(sglist), direction);
  1012. #ifdef SBA_COLLECT_STATS
  1013. ioc->usg_pages += ((sg_dma_address(sglist) & ~IOVP_MASK) + sg_dma_len(sglist) + IOVP_SIZE - 1) >> PAGE_SHIFT;
  1014. ioc->usingle_calls--; /* kluge since call is unmap_sg() */
  1015. #endif
  1016. ++sglist;
  1017. }
  1018. DBG_RUN_SG("%s() DONE (nents %d)\n", __FUNCTION__, nents);
  1019. #ifdef ASSERT_PDIR_SANITY
  1020. spin_lock_irqsave(&ioc->res_lock, flags);
  1021. sba_check_pdir(ioc,"Check after sba_unmap_sg()");
  1022. spin_unlock_irqrestore(&ioc->res_lock, flags);
  1023. #endif
  1024. }
  1025. static struct hppa_dma_ops sba_ops = {
  1026. .dma_supported = sba_dma_supported,
  1027. .alloc_consistent = sba_alloc_consistent,
  1028. .alloc_noncoherent = sba_alloc_consistent,
  1029. .free_consistent = sba_free_consistent,
  1030. .map_single = sba_map_single,
  1031. .unmap_single = sba_unmap_single,
  1032. .map_sg = sba_map_sg,
  1033. .unmap_sg = sba_unmap_sg,
  1034. .dma_sync_single_for_cpu = NULL,
  1035. .dma_sync_single_for_device = NULL,
  1036. .dma_sync_sg_for_cpu = NULL,
  1037. .dma_sync_sg_for_device = NULL,
  1038. };
  1039. /**************************************************************************
  1040. **
  1041. ** SBA PAT PDC support
  1042. **
  1043. ** o call pdc_pat_cell_module()
  1044. ** o store ranges in PCI "resource" structures
  1045. **
  1046. **************************************************************************/
  1047. static void
  1048. sba_get_pat_resources(struct sba_device *sba_dev)
  1049. {
  1050. #if 0
  1051. /*
  1052. ** TODO/REVISIT/FIXME: support for directed ranges requires calls to
  1053. ** PAT PDC to program the SBA/LBA directed range registers...this
  1054. ** burden may fall on the LBA code since it directly supports the
  1055. ** PCI subsystem. It's not clear yet. - ggg
  1056. */
  1057. PAT_MOD(mod)->mod_info.mod_pages = PAT_GET_MOD_PAGES(temp);
  1058. FIXME : ???
  1059. PAT_MOD(mod)->mod_info.dvi = PAT_GET_DVI(temp);
  1060. Tells where the dvi bits are located in the address.
  1061. PAT_MOD(mod)->mod_info.ioc = PAT_GET_IOC(temp);
  1062. FIXME : ???
  1063. #endif
  1064. }
  1065. /**************************************************************
  1066. *
  1067. * Initialization and claim
  1068. *
  1069. ***************************************************************/
  1070. #define PIRANHA_ADDR_MASK 0x00160000UL /* bit 17,18,20 */
  1071. #define PIRANHA_ADDR_VAL 0x00060000UL /* bit 17,18 on */
  1072. static void *
  1073. sba_alloc_pdir(unsigned int pdir_size)
  1074. {
  1075. unsigned long pdir_base;
  1076. unsigned long pdir_order = get_order(pdir_size);
  1077. pdir_base = __get_free_pages(GFP_KERNEL, pdir_order);
  1078. if (NULL == (void *) pdir_base) {
  1079. panic("%s() could not allocate I/O Page Table\n",
  1080. __FUNCTION__);
  1081. }
  1082. /* If this is not PA8700 (PCX-W2)
  1083. ** OR newer than ver 2.2
  1084. ** OR in a system that doesn't need VINDEX bits from SBA,
  1085. **
  1086. ** then we aren't exposed to the HW bug.
  1087. */
  1088. if ( ((boot_cpu_data.pdc.cpuid >> 5) & 0x7f) != 0x13
  1089. || (boot_cpu_data.pdc.versions > 0x202)
  1090. || (boot_cpu_data.pdc.capabilities & 0x08L) )
  1091. return (void *) pdir_base;
  1092. /*
  1093. * PA8700 (PCX-W2, aka piranha) silent data corruption fix
  1094. *
  1095. * An interaction between PA8700 CPU (Ver 2.2 or older) and
  1096. * Ike/Astro can cause silent data corruption. This is only
  1097. * a problem if the I/O PDIR is located in memory such that
  1098. * (little-endian) bits 17 and 18 are on and bit 20 is off.
  1099. *
  1100. * Since the max IO Pdir size is 2MB, by cleverly allocating the
  1101. * right physical address, we can either avoid (IOPDIR <= 1MB)
  1102. * or minimize (2MB IO Pdir) the problem if we restrict the
  1103. * IO Pdir to a maximum size of 2MB-128K (1902K).
  1104. *
  1105. * Because we always allocate 2^N sized IO pdirs, either of the
  1106. * "bad" regions will be the last 128K if at all. That's easy
  1107. * to test for.
  1108. *
  1109. */
  1110. if (pdir_order <= (19-12)) {
  1111. if (((virt_to_phys(pdir_base)+pdir_size-1) & PIRANHA_ADDR_MASK) == PIRANHA_ADDR_VAL) {
  1112. /* allocate a new one on 512k alignment */
  1113. unsigned long new_pdir = __get_free_pages(GFP_KERNEL, (19-12));
  1114. /* release original */
  1115. free_pages(pdir_base, pdir_order);
  1116. pdir_base = new_pdir;
  1117. /* release excess */
  1118. while (pdir_order < (19-12)) {
  1119. new_pdir += pdir_size;
  1120. free_pages(new_pdir, pdir_order);
  1121. pdir_order +=1;
  1122. pdir_size <<=1;
  1123. }
  1124. }
  1125. } else {
  1126. /*
  1127. ** 1MB or 2MB Pdir
  1128. ** Needs to be aligned on an "odd" 1MB boundary.
  1129. */
  1130. unsigned long new_pdir = __get_free_pages(GFP_KERNEL, pdir_order+1); /* 2 or 4MB */
  1131. /* release original */
  1132. free_pages( pdir_base, pdir_order);
  1133. /* release first 1MB */
  1134. free_pages(new_pdir, 20-12);
  1135. pdir_base = new_pdir + 1024*1024;
  1136. if (pdir_order > (20-12)) {
  1137. /*
  1138. ** 2MB Pdir.
  1139. **
  1140. ** Flag tells init_bitmap() to mark bad 128k as used
  1141. ** and to reduce the size by 128k.
  1142. */
  1143. piranha_bad_128k = 1;
  1144. new_pdir += 3*1024*1024;
  1145. /* release last 1MB */
  1146. free_pages(new_pdir, 20-12);
  1147. /* release unusable 128KB */
  1148. free_pages(new_pdir - 128*1024 , 17-12);
  1149. pdir_size -= 128*1024;
  1150. }
  1151. }
  1152. memset((void *) pdir_base, 0, pdir_size);
  1153. return (void *) pdir_base;
  1154. }
  1155. static struct device *next_device(struct klist_iter *i)
  1156. {
  1157. struct klist_node * n = klist_next(i);
  1158. return n ? container_of(n, struct device, knode_parent) : NULL;
  1159. }
  1160. /* setup Mercury or Elroy IBASE/IMASK registers. */
  1161. static void
  1162. setup_ibase_imask(struct parisc_device *sba, struct ioc *ioc, int ioc_num)
  1163. {
  1164. /* lba_set_iregs() is in drivers/parisc/lba_pci.c */
  1165. extern void lba_set_iregs(struct parisc_device *, u32, u32);
  1166. struct device *dev;
  1167. struct klist_iter i;
  1168. klist_iter_init(&sba->dev.klist_children, &i);
  1169. while ((dev = next_device(&i))) {
  1170. struct parisc_device *lba = to_parisc_device(dev);
  1171. int rope_num = (lba->hpa.start >> 13) & 0xf;
  1172. if (rope_num >> 3 == ioc_num)
  1173. lba_set_iregs(lba, ioc->ibase, ioc->imask);
  1174. }
  1175. klist_iter_exit(&i);
  1176. }
  1177. static void
  1178. sba_ioc_init_pluto(struct parisc_device *sba, struct ioc *ioc, int ioc_num)
  1179. {
  1180. u32 iova_space_mask;
  1181. u32 iova_space_size;
  1182. int iov_order, tcnfg;
  1183. #ifdef SBA_AGP_SUPPORT
  1184. int agp_found = 0;
  1185. #endif
  1186. /*
  1187. ** Firmware programs the base and size of a "safe IOVA space"
  1188. ** (one that doesn't overlap memory or LMMIO space) in the
  1189. ** IBASE and IMASK registers.
  1190. */
  1191. ioc->ibase = READ_REG(ioc->ioc_hpa + IOC_IBASE);
  1192. iova_space_size = ~(READ_REG(ioc->ioc_hpa + IOC_IMASK) & 0xFFFFFFFFUL) + 1;
  1193. if ((ioc->ibase < 0xfed00000UL) && ((ioc->ibase + iova_space_size) > 0xfee00000UL)) {
  1194. printk("WARNING: IOV space overlaps local config and interrupt message, truncating\n");
  1195. iova_space_size /= 2;
  1196. }
  1197. /*
  1198. ** iov_order is always based on a 1GB IOVA space since we want to
  1199. ** turn on the other half for AGP GART.
  1200. */
  1201. iov_order = get_order(iova_space_size >> (IOVP_SHIFT - PAGE_SHIFT));
  1202. ioc->pdir_size = (iova_space_size / IOVP_SIZE) * sizeof(u64);
  1203. DBG_INIT("%s() hpa 0x%lx IOV %dMB (%d bits)\n",
  1204. __FUNCTION__, ioc->ioc_hpa, iova_space_size >> 20,
  1205. iov_order + PAGE_SHIFT);
  1206. ioc->pdir_base = (void *) __get_free_pages(GFP_KERNEL,
  1207. get_order(ioc->pdir_size));
  1208. if (!ioc->pdir_base)
  1209. panic("Couldn't allocate I/O Page Table\n");
  1210. memset(ioc->pdir_base, 0, ioc->pdir_size);
  1211. DBG_INIT("%s() pdir %p size %x\n",
  1212. __FUNCTION__, ioc->pdir_base, ioc->pdir_size);
  1213. #ifdef SBA_HINT_SUPPORT
  1214. ioc->hint_shift_pdir = iov_order + PAGE_SHIFT;
  1215. ioc->hint_mask_pdir = ~(0x3 << (iov_order + PAGE_SHIFT));
  1216. DBG_INIT(" hint_shift_pdir %x hint_mask_pdir %lx\n",
  1217. ioc->hint_shift_pdir, ioc->hint_mask_pdir);
  1218. #endif
  1219. WARN_ON((((unsigned long) ioc->pdir_base) & PAGE_MASK) != (unsigned long) ioc->pdir_base);
  1220. WRITE_REG(virt_to_phys(ioc->pdir_base), ioc->ioc_hpa + IOC_PDIR_BASE);
  1221. /* build IMASK for IOC and Elroy */
  1222. iova_space_mask = 0xffffffff;
  1223. iova_space_mask <<= (iov_order + PAGE_SHIFT);
  1224. ioc->imask = iova_space_mask;
  1225. #ifdef ZX1_SUPPORT
  1226. ioc->iovp_mask = ~(iova_space_mask + PAGE_SIZE - 1);
  1227. #endif
  1228. sba_dump_tlb(ioc->ioc_hpa);
  1229. setup_ibase_imask(sba, ioc, ioc_num);
  1230. WRITE_REG(ioc->imask, ioc->ioc_hpa + IOC_IMASK);
  1231. #ifdef CONFIG_64BIT
  1232. /*
  1233. ** Setting the upper bits makes checking for bypass addresses
  1234. ** a little faster later on.
  1235. */
  1236. ioc->imask |= 0xFFFFFFFF00000000UL;
  1237. #endif
  1238. /* Set I/O PDIR Page size to system page size */
  1239. switch (PAGE_SHIFT) {
  1240. case 12: tcnfg = 0; break; /* 4K */
  1241. case 13: tcnfg = 1; break; /* 8K */
  1242. case 14: tcnfg = 2; break; /* 16K */
  1243. case 16: tcnfg = 3; break; /* 64K */
  1244. default:
  1245. panic(__FILE__ "Unsupported system page size %d",
  1246. 1 << PAGE_SHIFT);
  1247. break;
  1248. }
  1249. WRITE_REG(tcnfg, ioc->ioc_hpa + IOC_TCNFG);
  1250. /*
  1251. ** Program the IOC's ibase and enable IOVA translation
  1252. ** Bit zero == enable bit.
  1253. */
  1254. WRITE_REG(ioc->ibase | 1, ioc->ioc_hpa + IOC_IBASE);
  1255. /*
  1256. ** Clear I/O TLB of any possible entries.
  1257. ** (Yes. This is a bit paranoid...but so what)
  1258. */
  1259. WRITE_REG(ioc->ibase | 31, ioc->ioc_hpa + IOC_PCOM);
  1260. #ifdef SBA_AGP_SUPPORT
  1261. /*
  1262. ** If an AGP device is present, only use half of the IOV space
  1263. ** for PCI DMA. Unfortunately we can't know ahead of time
  1264. ** whether GART support will actually be used, for now we
  1265. ** can just key on any AGP device found in the system.
  1266. ** We program the next pdir index after we stop w/ a key for
  1267. ** the GART code to handshake on.
  1268. */
  1269. device=NULL;
  1270. for (lba = sba->child; lba; lba = lba->sibling) {
  1271. if (IS_QUICKSILVER(lba))
  1272. break;
  1273. }
  1274. if (lba) {
  1275. DBG_INIT("%s: Reserving half of IOVA space for AGP GART support\n", __FUNCTION__);
  1276. ioc->pdir_size /= 2;
  1277. ((u64 *)ioc->pdir_base)[PDIR_INDEX(iova_space_size/2)] = SBA_IOMMU_COOKIE;
  1278. } else {
  1279. DBG_INIT("%s: No GART needed - no AGP controller found\n", __FUNCTION__);
  1280. }
  1281. #endif /* 0 */
  1282. }
  1283. static void
  1284. sba_ioc_init(struct parisc_device *sba, struct ioc *ioc, int ioc_num)
  1285. {
  1286. u32 iova_space_size, iova_space_mask;
  1287. unsigned int pdir_size, iov_order;
  1288. /*
  1289. ** Determine IOVA Space size from memory size.
  1290. **
  1291. ** Ideally, PCI drivers would register the maximum number
  1292. ** of DMA they can have outstanding for each device they
  1293. ** own. Next best thing would be to guess how much DMA
  1294. ** can be outstanding based on PCI Class/sub-class. Both
  1295. ** methods still require some "extra" to support PCI
  1296. ** Hot-Plug/Removal of PCI cards. (aka PCI OLARD).
  1297. **
  1298. ** While we have 32-bits "IOVA" space, top two 2 bits are used
  1299. ** for DMA hints - ergo only 30 bits max.
  1300. */
  1301. iova_space_size = (u32) (num_physpages/global_ioc_cnt);
  1302. /* limit IOVA space size to 1MB-1GB */
  1303. if (iova_space_size < (1 << (20 - PAGE_SHIFT))) {
  1304. iova_space_size = 1 << (20 - PAGE_SHIFT);
  1305. }
  1306. else if (iova_space_size > (1 << (30 - PAGE_SHIFT))) {
  1307. iova_space_size = 1 << (30 - PAGE_SHIFT);
  1308. }
  1309. /*
  1310. ** iova space must be log2() in size.
  1311. ** thus, pdir/res_map will also be log2().
  1312. ** PIRANHA BUG: Exception is when IO Pdir is 2MB (gets reduced)
  1313. */
  1314. iov_order = get_order(iova_space_size << PAGE_SHIFT);
  1315. /* iova_space_size is now bytes, not pages */
  1316. iova_space_size = 1 << (iov_order + PAGE_SHIFT);
  1317. ioc->pdir_size = pdir_size = (iova_space_size/IOVP_SIZE) * sizeof(u64);
  1318. DBG_INIT("%s() hpa 0x%lx mem %ldMB IOV %dMB (%d bits)\n",
  1319. __FUNCTION__,
  1320. ioc->ioc_hpa,
  1321. (unsigned long) num_physpages >> (20 - PAGE_SHIFT),
  1322. iova_space_size>>20,
  1323. iov_order + PAGE_SHIFT);
  1324. ioc->pdir_base = sba_alloc_pdir(pdir_size);
  1325. DBG_INIT("%s() pdir %p size %x\n",
  1326. __FUNCTION__, ioc->pdir_base, pdir_size);
  1327. #ifdef SBA_HINT_SUPPORT
  1328. /* FIXME : DMA HINTs not used */
  1329. ioc->hint_shift_pdir = iov_order + PAGE_SHIFT;
  1330. ioc->hint_mask_pdir = ~(0x3 << (iov_order + PAGE_SHIFT));
  1331. DBG_INIT(" hint_shift_pdir %x hint_mask_pdir %lx\n",
  1332. ioc->hint_shift_pdir, ioc->hint_mask_pdir);
  1333. #endif
  1334. WRITE_REG64(virt_to_phys(ioc->pdir_base), ioc->ioc_hpa + IOC_PDIR_BASE);
  1335. /* build IMASK for IOC and Elroy */
  1336. iova_space_mask = 0xffffffff;
  1337. iova_space_mask <<= (iov_order + PAGE_SHIFT);
  1338. /*
  1339. ** On C3000 w/512MB mem, HP-UX 10.20 reports:
  1340. ** ibase=0, imask=0xFE000000, size=0x2000000.
  1341. */
  1342. ioc->ibase = 0;
  1343. ioc->imask = iova_space_mask; /* save it */
  1344. #ifdef ZX1_SUPPORT
  1345. ioc->iovp_mask = ~(iova_space_mask + PAGE_SIZE - 1);
  1346. #endif
  1347. DBG_INIT("%s() IOV base 0x%lx mask 0x%0lx\n",
  1348. __FUNCTION__, ioc->ibase, ioc->imask);
  1349. /*
  1350. ** FIXME: Hint registers are programmed with default hint
  1351. ** values during boot, so hints should be sane even if we
  1352. ** can't reprogram them the way drivers want.
  1353. */
  1354. setup_ibase_imask(sba, ioc, ioc_num);
  1355. /*
  1356. ** Program the IOC's ibase and enable IOVA translation
  1357. */
  1358. WRITE_REG(ioc->ibase | 1, ioc->ioc_hpa+IOC_IBASE);
  1359. WRITE_REG(ioc->imask, ioc->ioc_hpa+IOC_IMASK);
  1360. /* Set I/O PDIR Page size to 4K */
  1361. WRITE_REG(0, ioc->ioc_hpa+IOC_TCNFG);
  1362. /*
  1363. ** Clear I/O TLB of any possible entries.
  1364. ** (Yes. This is a bit paranoid...but so what)
  1365. */
  1366. WRITE_REG(0 | 31, ioc->ioc_hpa+IOC_PCOM);
  1367. ioc->ibase = 0; /* used by SBA_IOVA and related macros */
  1368. DBG_INIT("%s() DONE\n", __FUNCTION__);
  1369. }
  1370. /**************************************************************************
  1371. **
  1372. ** SBA initialization code (HW and SW)
  1373. **
  1374. ** o identify SBA chip itself
  1375. ** o initialize SBA chip modes (HardFail)
  1376. ** o initialize SBA chip modes (HardFail)
  1377. ** o FIXME: initialize DMA hints for reasonable defaults
  1378. **
  1379. **************************************************************************/
  1380. static void __iomem *ioc_remap(struct sba_device *sba_dev, unsigned int offset)
  1381. {
  1382. return ioremap_nocache(sba_dev->dev->hpa.start + offset, SBA_FUNC_SIZE);
  1383. }
  1384. static void sba_hw_init(struct sba_device *sba_dev)
  1385. {
  1386. int i;
  1387. int num_ioc;
  1388. u64 ioc_ctl;
  1389. if (!is_pdc_pat()) {
  1390. /* Shutdown the USB controller on Astro-based workstations.
  1391. ** Once we reprogram the IOMMU, the next DMA performed by
  1392. ** USB will HPMC the box. USB is only enabled if a
  1393. ** keyboard is present and found.
  1394. **
  1395. ** With serial console, j6k v5.0 firmware says:
  1396. ** mem_kbd hpa 0xfee003f8 sba 0x0 pad 0x0 cl_class 0x7
  1397. **
  1398. ** FIXME: Using GFX+USB console at power up but direct
  1399. ** linux to serial console is still broken.
  1400. ** USB could generate DMA so we must reset USB.
  1401. ** The proper sequence would be:
  1402. ** o block console output
  1403. ** o reset USB device
  1404. ** o reprogram serial port
  1405. ** o unblock console output
  1406. */
  1407. if (PAGE0->mem_kbd.cl_class == CL_KEYBD) {
  1408. pdc_io_reset_devices();
  1409. }
  1410. }
  1411. #if 0
  1412. printk("sba_hw_init(): mem_boot 0x%x 0x%x 0x%x 0x%x\n", PAGE0->mem_boot.hpa,
  1413. PAGE0->mem_boot.spa, PAGE0->mem_boot.pad, PAGE0->mem_boot.cl_class);
  1414. /*
  1415. ** Need to deal with DMA from LAN.
  1416. ** Maybe use page zero boot device as a handle to talk
  1417. ** to PDC about which device to shutdown.
  1418. **
  1419. ** Netbooting, j6k v5.0 firmware says:
  1420. ** mem_boot hpa 0xf4008000 sba 0x0 pad 0x0 cl_class 0x1002
  1421. ** ARGH! invalid class.
  1422. */
  1423. if ((PAGE0->mem_boot.cl_class != CL_RANDOM)
  1424. && (PAGE0->mem_boot.cl_class != CL_SEQU)) {
  1425. pdc_io_reset();
  1426. }
  1427. #endif
  1428. if (!IS_PLUTO(sba_dev->iodc)) {
  1429. ioc_ctl = READ_REG(sba_dev->sba_hpa+IOC_CTRL);
  1430. DBG_INIT("%s() hpa 0x%lx ioc_ctl 0x%Lx ->",
  1431. __FUNCTION__, sba_dev->sba_hpa, ioc_ctl);
  1432. ioc_ctl &= ~(IOC_CTRL_RM | IOC_CTRL_NC | IOC_CTRL_CE);
  1433. ioc_ctl |= IOC_CTRL_DD | IOC_CTRL_D4 | IOC_CTRL_TC;
  1434. /* j6700 v1.6 firmware sets 0x294f */
  1435. /* A500 firmware sets 0x4d */
  1436. WRITE_REG(ioc_ctl, sba_dev->sba_hpa+IOC_CTRL);
  1437. #ifdef DEBUG_SBA_INIT
  1438. ioc_ctl = READ_REG64(sba_dev->sba_hpa+IOC_CTRL);
  1439. DBG_INIT(" 0x%Lx\n", ioc_ctl);
  1440. #endif
  1441. } /* if !PLUTO */
  1442. if (IS_ASTRO(sba_dev->iodc)) {
  1443. int err;
  1444. /* PAT_PDC (L-class) also reports the same goofy base */
  1445. sba_dev->ioc[0].ioc_hpa = ioc_remap(sba_dev, ASTRO_IOC_OFFSET);
  1446. num_ioc = 1;
  1447. sba_dev->chip_resv.name = "Astro Intr Ack";
  1448. sba_dev->chip_resv.start = PCI_F_EXTEND | 0xfef00000UL;
  1449. sba_dev->chip_resv.end = PCI_F_EXTEND | (0xff000000UL - 1) ;
  1450. err = request_resource(&iomem_resource, &(sba_dev->chip_resv));
  1451. BUG_ON(err < 0);
  1452. } else if (IS_PLUTO(sba_dev->iodc)) {
  1453. int err;
  1454. /* We use a negative value for IOC HPA so it gets
  1455. * corrected when we add it with IKE's IOC offset.
  1456. * Doesnt look clean, but fewer code.
  1457. */
  1458. sba_dev->ioc[0].ioc_hpa = ioc_remap(sba_dev, PLUTO_IOC_OFFSET);
  1459. num_ioc = 1;
  1460. sba_dev->chip_resv.name = "Pluto Intr/PIOP/VGA";
  1461. sba_dev->chip_resv.start = PCI_F_EXTEND | 0xfee00000UL;
  1462. sba_dev->chip_resv.end = PCI_F_EXTEND | (0xff200000UL - 1);
  1463. err = request_resource(&iomem_resource, &(sba_dev->chip_resv));
  1464. WARN_ON(err < 0);
  1465. sba_dev->iommu_resv.name = "IOVA Space";
  1466. sba_dev->iommu_resv.start = 0x40000000UL;
  1467. sba_dev->iommu_resv.end = 0x50000000UL - 1;
  1468. err = request_resource(&iomem_resource, &(sba_dev->iommu_resv));
  1469. WARN_ON(err < 0);
  1470. } else {
  1471. /* IS_IKE (ie N-class, L3000, L1500) */
  1472. sba_dev->ioc[0].ioc_hpa = ioc_remap(sba_dev, IKE_IOC_OFFSET(0));
  1473. sba_dev->ioc[1].ioc_hpa = ioc_remap(sba_dev, IKE_IOC_OFFSET(1));
  1474. num_ioc = 2;
  1475. /* TODO - LOOKUP Ike/Stretch chipset mem map */
  1476. }
  1477. /* XXX: What about Reo? */
  1478. sba_dev->num_ioc = num_ioc;
  1479. for (i = 0; i < num_ioc; i++) {
  1480. /*
  1481. ** Make sure the box crashes if we get any errors on a rope.
  1482. */
  1483. WRITE_REG(HF_ENABLE, sba_dev->ioc[i].ioc_hpa + ROPE0_CTL);
  1484. WRITE_REG(HF_ENABLE, sba_dev->ioc[i].ioc_hpa + ROPE1_CTL);
  1485. WRITE_REG(HF_ENABLE, sba_dev->ioc[i].ioc_hpa + ROPE2_CTL);
  1486. WRITE_REG(HF_ENABLE, sba_dev->ioc[i].ioc_hpa + ROPE3_CTL);
  1487. WRITE_REG(HF_ENABLE, sba_dev->ioc[i].ioc_hpa + ROPE4_CTL);
  1488. WRITE_REG(HF_ENABLE, sba_dev->ioc[i].ioc_hpa + ROPE5_CTL);
  1489. WRITE_REG(HF_ENABLE, sba_dev->ioc[i].ioc_hpa + ROPE6_CTL);
  1490. WRITE_REG(HF_ENABLE, sba_dev->ioc[i].ioc_hpa + ROPE7_CTL);
  1491. /* flush out the writes */
  1492. READ_REG(sba_dev->ioc[i].ioc_hpa + ROPE7_CTL);
  1493. DBG_INIT(" ioc[%d] ROPE_CFG 0x%Lx ROPE_DBG 0x%Lx\n",
  1494. i,
  1495. READ_REG(sba_dev->ioc[i].ioc_hpa + 0x40),
  1496. READ_REG(sba_dev->ioc[i].ioc_hpa + 0x50)
  1497. );
  1498. DBG_INIT(" STATUS_CONTROL 0x%Lx FLUSH_CTRL 0x%Lx\n",
  1499. READ_REG(sba_dev->ioc[i].ioc_hpa + 0x108),
  1500. READ_REG(sba_dev->ioc[i].ioc_hpa + 0x400)
  1501. );
  1502. if (IS_PLUTO(sba_dev->iodc)) {
  1503. sba_ioc_init_pluto(sba_dev->dev, &(sba_dev->ioc[i]), i);
  1504. } else {
  1505. sba_ioc_init(sba_dev->dev, &(sba_dev->ioc[i]), i);
  1506. }
  1507. }
  1508. }
  1509. static void
  1510. sba_common_init(struct sba_device *sba_dev)
  1511. {
  1512. int i;
  1513. /* add this one to the head of the list (order doesn't matter)
  1514. ** This will be useful for debugging - especially if we get coredumps
  1515. */
  1516. sba_dev->next = sba_list;
  1517. sba_list = sba_dev;
  1518. for(i=0; i< sba_dev->num_ioc; i++) {
  1519. int res_size;
  1520. #ifdef DEBUG_DMB_TRAP
  1521. extern void iterate_pages(unsigned long , unsigned long ,
  1522. void (*)(pte_t * , unsigned long),
  1523. unsigned long );
  1524. void set_data_memory_break(pte_t * , unsigned long);
  1525. #endif
  1526. /* resource map size dictated by pdir_size */
  1527. res_size = sba_dev->ioc[i].pdir_size/sizeof(u64); /* entries */
  1528. /* Second part of PIRANHA BUG */
  1529. if (piranha_bad_128k) {
  1530. res_size -= (128*1024)/sizeof(u64);
  1531. }
  1532. res_size >>= 3; /* convert bit count to byte count */
  1533. DBG_INIT("%s() res_size 0x%x\n",
  1534. __FUNCTION__, res_size);
  1535. sba_dev->ioc[i].res_size = res_size;
  1536. sba_dev->ioc[i].res_map = (char *) __get_free_pages(GFP_KERNEL, get_order(res_size));
  1537. #ifdef DEBUG_DMB_TRAP
  1538. iterate_pages( sba_dev->ioc[i].res_map, res_size,
  1539. set_data_memory_break, 0);
  1540. #endif
  1541. if (NULL == sba_dev->ioc[i].res_map)
  1542. {
  1543. panic("%s:%s() could not allocate resource map\n",
  1544. __FILE__, __FUNCTION__ );
  1545. }
  1546. memset(sba_dev->ioc[i].res_map, 0, res_size);
  1547. /* next available IOVP - circular search */
  1548. sba_dev->ioc[i].res_hint = (unsigned long *)
  1549. &(sba_dev->ioc[i].res_map[L1_CACHE_BYTES]);
  1550. #ifdef ASSERT_PDIR_SANITY
  1551. /* Mark first bit busy - ie no IOVA 0 */
  1552. sba_dev->ioc[i].res_map[0] = 0x80;
  1553. sba_dev->ioc[i].pdir_base[0] = 0xeeffc0addbba0080ULL;
  1554. #endif
  1555. /* Third (and last) part of PIRANHA BUG */
  1556. if (piranha_bad_128k) {
  1557. /* region from +1408K to +1536 is un-usable. */
  1558. int idx_start = (1408*1024/sizeof(u64)) >> 3;
  1559. int idx_end = (1536*1024/sizeof(u64)) >> 3;
  1560. long *p_start = (long *) &(sba_dev->ioc[i].res_map[idx_start]);
  1561. long *p_end = (long *) &(sba_dev->ioc[i].res_map[idx_end]);
  1562. /* mark that part of the io pdir busy */
  1563. while (p_start < p_end)
  1564. *p_start++ = -1;
  1565. }
  1566. #ifdef DEBUG_DMB_TRAP
  1567. iterate_pages( sba_dev->ioc[i].res_map, res_size,
  1568. set_data_memory_break, 0);
  1569. iterate_pages( sba_dev->ioc[i].pdir_base, sba_dev->ioc[i].pdir_size,
  1570. set_data_memory_break, 0);
  1571. #endif
  1572. DBG_INIT("%s() %d res_map %x %p\n",
  1573. __FUNCTION__, i, res_size, sba_dev->ioc[i].res_map);
  1574. }
  1575. spin_lock_init(&sba_dev->sba_lock);
  1576. ioc_needs_fdc = boot_cpu_data.pdc.capabilities & PDC_MODEL_IOPDIR_FDC;
  1577. #ifdef DEBUG_SBA_INIT
  1578. /*
  1579. * If the PDC_MODEL capabilities has Non-coherent IO-PDIR bit set
  1580. * (bit #61, big endian), we have to flush and sync every time
  1581. * IO-PDIR is changed in Ike/Astro.
  1582. */
  1583. if (boot_cpu_data.pdc.capabilities & PDC_MODEL_IOPDIR_FDC) {
  1584. printk(KERN_INFO MODULE_NAME " FDC/SYNC required.\n");
  1585. } else {
  1586. printk(KERN_INFO MODULE_NAME " IOC has cache coherent PDIR.\n");
  1587. }
  1588. #endif
  1589. }
  1590. #ifdef CONFIG_PROC_FS
  1591. static int sba_proc_info(struct seq_file *m, void *p)
  1592. {
  1593. struct sba_device *sba_dev = sba_list;
  1594. struct ioc *ioc = &sba_dev->ioc[0]; /* FIXME: Multi-IOC support! */
  1595. int total_pages = (int) (ioc->res_size << 3); /* 8 bits per byte */
  1596. #ifdef SBA_COLLECT_STATS
  1597. unsigned long avg = 0, min, max;
  1598. #endif
  1599. int i, len = 0;
  1600. len += seq_printf(m, "%s rev %d.%d\n",
  1601. sba_dev->name,
  1602. (sba_dev->hw_rev & 0x7) + 1,
  1603. (sba_dev->hw_rev & 0x18) >> 3
  1604. );
  1605. len += seq_printf(m, "IO PDIR size : %d bytes (%d entries)\n",
  1606. (int) ((ioc->res_size << 3) * sizeof(u64)), /* 8 bits/byte */
  1607. total_pages);
  1608. len += seq_printf(m, "Resource bitmap : %d bytes (%d pages)\n",
  1609. ioc->res_size, ioc->res_size << 3); /* 8 bits per byte */
  1610. len += seq_printf(m, "LMMIO_BASE/MASK/ROUTE %08x %08x %08x\n",
  1611. READ_REG32(sba_dev->sba_hpa + LMMIO_DIST_BASE),
  1612. READ_REG32(sba_dev->sba_hpa + LMMIO_DIST_MASK),
  1613. READ_REG32(sba_dev->sba_hpa + LMMIO_DIST_ROUTE)
  1614. );
  1615. for (i=0; i<4; i++)
  1616. len += seq_printf(m, "DIR%d_BASE/MASK/ROUTE %08x %08x %08x\n", i,
  1617. READ_REG32(sba_dev->sba_hpa + LMMIO_DIRECT0_BASE + i*0x18),
  1618. READ_REG32(sba_dev->sba_hpa + LMMIO_DIRECT0_MASK + i*0x18),
  1619. READ_REG32(sba_dev->sba_hpa + LMMIO_DIRECT0_ROUTE + i*0x18)
  1620. );
  1621. #ifdef SBA_COLLECT_STATS
  1622. len += seq_printf(m, "IO PDIR entries : %ld free %ld used (%d%%)\n",
  1623. total_pages - ioc->used_pages, ioc->used_pages,
  1624. (int) (ioc->used_pages * 100 / total_pages));
  1625. min = max = ioc->avg_search[0];
  1626. for (i = 0; i < SBA_SEARCH_SAMPLE; i++) {
  1627. avg += ioc->avg_search[i];
  1628. if (ioc->avg_search[i] > max) max = ioc->avg_search[i];
  1629. if (ioc->avg_search[i] < min) min = ioc->avg_search[i];
  1630. }
  1631. avg /= SBA_SEARCH_SAMPLE;
  1632. len += seq_printf(m, " Bitmap search : %ld/%ld/%ld (min/avg/max CPU Cycles)\n",
  1633. min, avg, max);
  1634. len += seq_printf(m, "pci_map_single(): %12ld calls %12ld pages (avg %d/1000)\n",
  1635. ioc->msingle_calls, ioc->msingle_pages,
  1636. (int) ((ioc->msingle_pages * 1000)/ioc->msingle_calls));
  1637. /* KLUGE - unmap_sg calls unmap_single for each mapped page */
  1638. min = ioc->usingle_calls;
  1639. max = ioc->usingle_pages - ioc->usg_pages;
  1640. len += seq_printf(m, "pci_unmap_single: %12ld calls %12ld pages (avg %d/1000)\n",
  1641. min, max, (int) ((max * 1000)/min));
  1642. len += seq_printf(m, "pci_map_sg() : %12ld calls %12ld pages (avg %d/1000)\n",
  1643. ioc->msg_calls, ioc->msg_pages,
  1644. (int) ((ioc->msg_pages * 1000)/ioc->msg_calls));
  1645. len += seq_printf(m, "pci_unmap_sg() : %12ld calls %12ld pages (avg %d/1000)\n",
  1646. ioc->usg_calls, ioc->usg_pages,
  1647. (int) ((ioc->usg_pages * 1000)/ioc->usg_calls));
  1648. #endif
  1649. return 0;
  1650. }
  1651. static int
  1652. sba_proc_open(struct inode *i, struct file *f)
  1653. {
  1654. return single_open(f, &sba_proc_info, NULL);
  1655. }
  1656. static struct file_operations sba_proc_fops = {
  1657. .owner = THIS_MODULE,
  1658. .open = sba_proc_open,
  1659. .read = seq_read,
  1660. .llseek = seq_lseek,
  1661. .release = single_release,
  1662. };
  1663. static int
  1664. sba_proc_bitmap_info(struct seq_file *m, void *p)
  1665. {
  1666. struct sba_device *sba_dev = sba_list;
  1667. struct ioc *ioc = &sba_dev->ioc[0]; /* FIXME: Multi-IOC support! */
  1668. unsigned int *res_ptr = (unsigned int *)ioc->res_map;
  1669. int i, len = 0;
  1670. for (i = 0; i < (ioc->res_size/sizeof(unsigned int)); ++i, ++res_ptr) {
  1671. if ((i & 7) == 0)
  1672. len += seq_printf(m, "\n ");
  1673. len += seq_printf(m, " %08x", *res_ptr);
  1674. }
  1675. len += seq_printf(m, "\n");
  1676. return 0;
  1677. }
  1678. static int
  1679. sba_proc_bitmap_open(struct inode *i, struct file *f)
  1680. {
  1681. return single_open(f, &sba_proc_bitmap_info, NULL);
  1682. }
  1683. static struct file_operations sba_proc_bitmap_fops = {
  1684. .owner = THIS_MODULE,
  1685. .open = sba_proc_bitmap_open,
  1686. .read = seq_read,
  1687. .llseek = seq_lseek,
  1688. .release = single_release,
  1689. };
  1690. #endif /* CONFIG_PROC_FS */
  1691. static struct parisc_device_id sba_tbl[] = {
  1692. { HPHW_IOA, HVERSION_REV_ANY_ID, ASTRO_RUNWAY_PORT, 0xb },
  1693. { HPHW_BCPORT, HVERSION_REV_ANY_ID, IKE_MERCED_PORT, 0xc },
  1694. { HPHW_BCPORT, HVERSION_REV_ANY_ID, REO_MERCED_PORT, 0xc },
  1695. { HPHW_BCPORT, HVERSION_REV_ANY_ID, REOG_MERCED_PORT, 0xc },
  1696. { HPHW_IOA, HVERSION_REV_ANY_ID, PLUTO_MCKINLEY_PORT, 0xc },
  1697. { 0, }
  1698. };
  1699. int sba_driver_callback(struct parisc_device *);
  1700. static struct parisc_driver sba_driver = {
  1701. .name = MODULE_NAME,
  1702. .id_table = sba_tbl,
  1703. .probe = sba_driver_callback,
  1704. };
  1705. /*
  1706. ** Determine if sba should claim this chip (return 0) or not (return 1).
  1707. ** If so, initialize the chip and tell other partners in crime they
  1708. ** have work to do.
  1709. */
  1710. int
  1711. sba_driver_callback(struct parisc_device *dev)
  1712. {
  1713. struct sba_device *sba_dev;
  1714. u32 func_class;
  1715. int i;
  1716. char *version;
  1717. void __iomem *sba_addr = ioremap_nocache(dev->hpa.start, SBA_FUNC_SIZE);
  1718. struct proc_dir_entry *info_entry, *bitmap_entry, *root;
  1719. sba_dump_ranges(sba_addr);
  1720. /* Read HW Rev First */
  1721. func_class = READ_REG(sba_addr + SBA_FCLASS);
  1722. if (IS_ASTRO(&dev->id)) {
  1723. unsigned long fclass;
  1724. static char astro_rev[]="Astro ?.?";
  1725. /* Astro is broken...Read HW Rev First */
  1726. fclass = READ_REG(sba_addr);
  1727. astro_rev[6] = '1' + (char) (fclass & 0x7);
  1728. astro_rev[8] = '0' + (char) ((fclass & 0x18) >> 3);
  1729. version = astro_rev;
  1730. } else if (IS_IKE(&dev->id)) {
  1731. static char ike_rev[] = "Ike rev ?";
  1732. ike_rev[8] = '0' + (char) (func_class & 0xff);
  1733. version = ike_rev;
  1734. } else if (IS_PLUTO(&dev->id)) {
  1735. static char pluto_rev[]="Pluto ?.?";
  1736. pluto_rev[6] = '0' + (char) ((func_class & 0xf0) >> 4);
  1737. pluto_rev[8] = '0' + (char) (func_class & 0x0f);
  1738. version = pluto_rev;
  1739. } else {
  1740. static char reo_rev[] = "REO rev ?";
  1741. reo_rev[8] = '0' + (char) (func_class & 0xff);
  1742. version = reo_rev;
  1743. }
  1744. if (!global_ioc_cnt) {
  1745. global_ioc_cnt = count_parisc_driver(&sba_driver);
  1746. /* Astro and Pluto have one IOC per SBA */
  1747. if ((!IS_ASTRO(&dev->id)) || (!IS_PLUTO(&dev->id)))
  1748. global_ioc_cnt *= 2;
  1749. }
  1750. printk(KERN_INFO "%s found %s at 0x%lx\n",
  1751. MODULE_NAME, version, dev->hpa.start);
  1752. sba_dev = kzalloc(sizeof(struct sba_device), GFP_KERNEL);
  1753. if (!sba_dev) {
  1754. printk(KERN_ERR MODULE_NAME " - couldn't alloc sba_device\n");
  1755. return -ENOMEM;
  1756. }
  1757. parisc_set_drvdata(dev, sba_dev);
  1758. for(i=0; i<MAX_IOC; i++)
  1759. spin_lock_init(&(sba_dev->ioc[i].res_lock));
  1760. sba_dev->dev = dev;
  1761. sba_dev->hw_rev = func_class;
  1762. sba_dev->iodc = &dev->id;
  1763. sba_dev->name = dev->name;
  1764. sba_dev->sba_hpa = sba_addr;
  1765. sba_get_pat_resources(sba_dev);
  1766. sba_hw_init(sba_dev);
  1767. sba_common_init(sba_dev);
  1768. hppa_dma_ops = &sba_ops;
  1769. #ifdef CONFIG_PROC_FS
  1770. switch (dev->id.hversion) {
  1771. case PLUTO_MCKINLEY_PORT:
  1772. root = proc_mckinley_root;
  1773. break;
  1774. case ASTRO_RUNWAY_PORT:
  1775. case IKE_MERCED_PORT:
  1776. default:
  1777. root = proc_runway_root;
  1778. break;
  1779. }
  1780. info_entry = create_proc_entry("sba_iommu", 0, root);
  1781. bitmap_entry = create_proc_entry("sba_iommu-bitmap", 0, root);
  1782. if (info_entry)
  1783. info_entry->proc_fops = &sba_proc_fops;
  1784. if (bitmap_entry)
  1785. bitmap_entry->proc_fops = &sba_proc_bitmap_fops;
  1786. #endif
  1787. parisc_vmerge_boundary = IOVP_SIZE;
  1788. parisc_vmerge_max_size = IOVP_SIZE * BITS_PER_LONG;
  1789. parisc_has_iommu();
  1790. return 0;
  1791. }
  1792. /*
  1793. ** One time initialization to let the world know the SBA was found.
  1794. ** This is the only routine which is NOT static.
  1795. ** Must be called exactly once before pci_init().
  1796. */
  1797. void __init sba_init(void)
  1798. {
  1799. register_parisc_driver(&sba_driver);
  1800. }
  1801. /**
  1802. * sba_get_iommu - Assign the iommu pointer for the pci bus controller.
  1803. * @dev: The parisc device.
  1804. *
  1805. * Returns the appropriate IOMMU data for the given parisc PCI controller.
  1806. * This is cached and used later for PCI DMA Mapping.
  1807. */
  1808. void * sba_get_iommu(struct parisc_device *pci_hba)
  1809. {
  1810. struct parisc_device *sba_dev = parisc_parent(pci_hba);
  1811. struct sba_device *sba = sba_dev->dev.driver_data;
  1812. char t = sba_dev->id.hw_type;
  1813. int iocnum = (pci_hba->hw_path >> 3); /* rope # */
  1814. WARN_ON((t != HPHW_IOA) && (t != HPHW_BCPORT));
  1815. return &(sba->ioc[iocnum]);
  1816. }
  1817. /**
  1818. * sba_directed_lmmio - return first directed LMMIO range routed to rope
  1819. * @pa_dev: The parisc device.
  1820. * @r: resource PCI host controller wants start/end fields assigned.
  1821. *
  1822. * For the given parisc PCI controller, determine if any direct ranges
  1823. * are routed down the corresponding rope.
  1824. */
  1825. void sba_directed_lmmio(struct parisc_device *pci_hba, struct resource *r)
  1826. {
  1827. struct parisc_device *sba_dev = parisc_parent(pci_hba);
  1828. struct sba_device *sba = sba_dev->dev.driver_data;
  1829. char t = sba_dev->id.hw_type;
  1830. int i;
  1831. int rope = (pci_hba->hw_path & (ROPES_PER_IOC-1)); /* rope # */
  1832. BUG_ON((t!=HPHW_IOA) && (t!=HPHW_BCPORT));
  1833. r->start = r->end = 0;
  1834. /* Astro has 4 directed ranges. Not sure about Ike/Pluto/et al */
  1835. for (i=0; i<4; i++) {
  1836. int base, size;
  1837. void __iomem *reg = sba->sba_hpa + i*0x18;
  1838. base = READ_REG32(reg + LMMIO_DIRECT0_BASE);
  1839. if ((base & 1) == 0)
  1840. continue; /* not enabled */
  1841. size = READ_REG32(reg + LMMIO_DIRECT0_ROUTE);
  1842. if ((size & (ROPES_PER_IOC-1)) != rope)
  1843. continue; /* directed down different rope */
  1844. r->start = (base & ~1UL) | PCI_F_EXTEND;
  1845. size = ~ READ_REG32(reg + LMMIO_DIRECT0_MASK);
  1846. r->end = r->start + size;
  1847. }
  1848. }
  1849. /**
  1850. * sba_distributed_lmmio - return portion of distributed LMMIO range
  1851. * @pa_dev: The parisc device.
  1852. * @r: resource PCI host controller wants start/end fields assigned.
  1853. *
  1854. * For the given parisc PCI controller, return portion of distributed LMMIO
  1855. * range. The distributed LMMIO is always present and it's just a question
  1856. * of the base address and size of the range.
  1857. */
  1858. void sba_distributed_lmmio(struct parisc_device *pci_hba, struct resource *r )
  1859. {
  1860. struct parisc_device *sba_dev = parisc_parent(pci_hba);
  1861. struct sba_device *sba = sba_dev->dev.driver_data;
  1862. char t = sba_dev->id.hw_type;
  1863. int base, size;
  1864. int rope = (pci_hba->hw_path & (ROPES_PER_IOC-1)); /* rope # */
  1865. BUG_ON((t!=HPHW_IOA) && (t!=HPHW_BCPORT));
  1866. r->start = r->end = 0;
  1867. base = READ_REG32(sba->sba_hpa + LMMIO_DIST_BASE);
  1868. if ((base & 1) == 0) {
  1869. BUG(); /* Gah! Distr Range wasn't enabled! */
  1870. return;
  1871. }
  1872. r->start = (base & ~1UL) | PCI_F_EXTEND;
  1873. size = (~READ_REG32(sba->sba_hpa + LMMIO_DIST_MASK)) / ROPES_PER_IOC;
  1874. r->start += rope * (size + 1); /* adjust base for this rope */
  1875. r->end = r->start + size;
  1876. }