sba_iommu.c 61 KB

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