sba_iommu.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259
  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/types.h>
  22. #include <linux/kernel.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/slab.h>
  25. #include <linux/init.h>
  26. #include <linux/mm.h>
  27. #include <linux/string.h>
  28. #include <linux/pci.h>
  29. #include <asm/byteorder.h>
  30. #include <asm/io.h>
  31. #include <asm/dma.h> /* for DMA_CHUNK_SIZE */
  32. #include <asm/hardware.h> /* for register_parisc_driver() stuff */
  33. #include <linux/proc_fs.h>
  34. #include <linux/seq_file.h>
  35. #include <asm/mckinley.h> /* for proc_mckinley_root */
  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. #define MODULE_NAME "SBA"
  41. #ifdef CONFIG_PROC_FS
  42. /* depends on proc fs support. But costs CPU performance */
  43. #undef SBA_COLLECT_STATS
  44. #endif
  45. /*
  46. ** The number of debug flags is a clue - this code is fragile.
  47. ** Don't even think about messing with it unless you have
  48. ** plenty of 710's to sacrifice to the computer gods. :^)
  49. */
  50. #undef DEBUG_SBA_INIT
  51. #undef DEBUG_SBA_RUN
  52. #undef DEBUG_SBA_RUN_SG
  53. #undef DEBUG_SBA_RESOURCE
  54. #undef ASSERT_PDIR_SANITY
  55. #undef DEBUG_LARGE_SG_ENTRIES
  56. #undef DEBUG_DMB_TRAP
  57. #ifdef DEBUG_SBA_INIT
  58. #define DBG_INIT(x...) printk(x)
  59. #else
  60. #define DBG_INIT(x...)
  61. #endif
  62. #ifdef DEBUG_SBA_RUN
  63. #define DBG_RUN(x...) printk(x)
  64. #else
  65. #define DBG_RUN(x...)
  66. #endif
  67. #ifdef DEBUG_SBA_RUN_SG
  68. #define DBG_RUN_SG(x...) printk(x)
  69. #else
  70. #define DBG_RUN_SG(x...)
  71. #endif
  72. #ifdef DEBUG_SBA_RESOURCE
  73. #define DBG_RES(x...) printk(x)
  74. #else
  75. #define DBG_RES(x...)
  76. #endif
  77. #if defined(CONFIG_64BIT)
  78. /* "low end" PA8800 machines use ZX1 chipset: PAT PDC and only run 64-bit */
  79. #define ZX1_SUPPORT
  80. #endif
  81. #define SBA_INLINE __inline__
  82. /*
  83. ** The number of pdir entries to "free" before issueing
  84. ** a read to PCOM register to flush out PCOM writes.
  85. ** Interacts with allocation granularity (ie 4 or 8 entries
  86. ** allocated and free'd/purged at a time might make this
  87. ** less interesting).
  88. */
  89. #define DELAYED_RESOURCE_CNT 16
  90. #define DEFAULT_DMA_HINT_REG 0
  91. #define ASTRO_RUNWAY_PORT 0x582
  92. #define IKE_MERCED_PORT 0x803
  93. #define REO_MERCED_PORT 0x804
  94. #define REOG_MERCED_PORT 0x805
  95. #define PLUTO_MCKINLEY_PORT 0x880
  96. #define SBA_FUNC_ID 0x0000 /* function id */
  97. #define SBA_FCLASS 0x0008 /* function class, bist, header, rev... */
  98. #define IS_ASTRO(id) ((id)->hversion == ASTRO_RUNWAY_PORT)
  99. #define IS_IKE(id) ((id)->hversion == IKE_MERCED_PORT)
  100. #define IS_PLUTO(id) ((id)->hversion == PLUTO_MCKINLEY_PORT)
  101. #define SBA_FUNC_SIZE 4096 /* SBA configuration function reg set */
  102. #define ASTRO_IOC_OFFSET (32 * SBA_FUNC_SIZE)
  103. #define PLUTO_IOC_OFFSET (1 * SBA_FUNC_SIZE)
  104. /* Ike's IOC's occupy functions 2 and 3 */
  105. #define IKE_IOC_OFFSET(p) ((p+2) * SBA_FUNC_SIZE)
  106. #define IOC_CTRL 0x8 /* IOC_CTRL offset */
  107. #define IOC_CTRL_TC (1 << 0) /* TOC Enable */
  108. #define IOC_CTRL_CE (1 << 1) /* Coalesce Enable */
  109. #define IOC_CTRL_DE (1 << 2) /* Dillon Enable */
  110. #define IOC_CTRL_RM (1 << 8) /* Real Mode */
  111. #define IOC_CTRL_NC (1 << 9) /* Non Coherent Mode */
  112. #define IOC_CTRL_D4 (1 << 11) /* Disable 4-byte coalescing */
  113. #define IOC_CTRL_DD (1 << 13) /* Disable distr. LMMIO range coalescing */
  114. #define MAX_IOC 2 /* per Ike. Pluto/Astro only have 1. */
  115. #define ROPES_PER_IOC 8 /* per Ike half or Pluto/Astro */
  116. /*
  117. ** Offsets into MBIB (Function 0 on Ike and hopefully Astro)
  118. ** Firmware programs this stuff. Don't touch it.
  119. */
  120. #define LMMIO_DIRECT0_BASE 0x300
  121. #define LMMIO_DIRECT0_MASK 0x308
  122. #define LMMIO_DIRECT0_ROUTE 0x310
  123. #define LMMIO_DIST_BASE 0x360
  124. #define LMMIO_DIST_MASK 0x368
  125. #define LMMIO_DIST_ROUTE 0x370
  126. #define IOS_DIST_BASE 0x390
  127. #define IOS_DIST_MASK 0x398
  128. #define IOS_DIST_ROUTE 0x3A0
  129. #define IOS_DIRECT_BASE 0x3C0
  130. #define IOS_DIRECT_MASK 0x3C8
  131. #define IOS_DIRECT_ROUTE 0x3D0
  132. /*
  133. ** Offsets into I/O TLB (Function 2 and 3 on Ike)
  134. */
  135. #define ROPE0_CTL 0x200 /* "regbus pci0" */
  136. #define ROPE1_CTL 0x208
  137. #define ROPE2_CTL 0x210
  138. #define ROPE3_CTL 0x218
  139. #define ROPE4_CTL 0x220
  140. #define ROPE5_CTL 0x228
  141. #define ROPE6_CTL 0x230
  142. #define ROPE7_CTL 0x238
  143. #define IOC_ROPE0_CFG 0x500 /* pluto only */
  144. #define IOC_ROPE_AO 0x10 /* Allow "Relaxed Ordering" */
  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) readl(addr)
  254. #define READ_REG64(addr) readq(addr)
  255. #define WRITE_REG32(val, addr) writel((val), (addr))
  256. #define WRITE_REG64(val, addr) writeq((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%p 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. void __iomem *ioc_hpa = sba_dev->ioc[i].ioc_hpa;
  1481. unsigned int j;
  1482. for (j=0; j < sizeof(u64) * ROPES_PER_IOC; j+=sizeof(u64)) {
  1483. /*
  1484. * Clear ROPE(N)_CONFIG AO bit.
  1485. * Disables "NT Ordering" (~= !"Relaxed Ordering")
  1486. * Overrides bit 1 in DMA Hint Sets.
  1487. * Improves netperf UDP_STREAM by ~10% for bcm5701.
  1488. */
  1489. if (IS_PLUTO(sba_dev->iodc)) {
  1490. void __iomem *rope_cfg;
  1491. unsigned long cfg_val;
  1492. rope_cfg = ioc_hpa + IOC_ROPE0_CFG + j;
  1493. cfg_val = READ_REG(rope_cfg);
  1494. cfg_val &= ~IOC_ROPE_AO;
  1495. WRITE_REG(cfg_val, rope_cfg);
  1496. }
  1497. /*
  1498. ** Make sure the box crashes on rope errors.
  1499. */
  1500. WRITE_REG(HF_ENABLE, ioc_hpa + ROPE0_CTL + j);
  1501. }
  1502. /* flush out the last writes */
  1503. READ_REG(sba_dev->ioc[i].ioc_hpa + ROPE7_CTL);
  1504. DBG_INIT(" ioc[%d] ROPE_CFG 0x%Lx ROPE_DBG 0x%Lx\n",
  1505. i,
  1506. READ_REG(sba_dev->ioc[i].ioc_hpa + 0x40),
  1507. READ_REG(sba_dev->ioc[i].ioc_hpa + 0x50)
  1508. );
  1509. DBG_INIT(" STATUS_CONTROL 0x%Lx FLUSH_CTRL 0x%Lx\n",
  1510. READ_REG(sba_dev->ioc[i].ioc_hpa + 0x108),
  1511. READ_REG(sba_dev->ioc[i].ioc_hpa + 0x400)
  1512. );
  1513. if (IS_PLUTO(sba_dev->iodc)) {
  1514. sba_ioc_init_pluto(sba_dev->dev, &(sba_dev->ioc[i]), i);
  1515. } else {
  1516. sba_ioc_init(sba_dev->dev, &(sba_dev->ioc[i]), i);
  1517. }
  1518. }
  1519. }
  1520. static void
  1521. sba_common_init(struct sba_device *sba_dev)
  1522. {
  1523. int i;
  1524. /* add this one to the head of the list (order doesn't matter)
  1525. ** This will be useful for debugging - especially if we get coredumps
  1526. */
  1527. sba_dev->next = sba_list;
  1528. sba_list = sba_dev;
  1529. for(i=0; i< sba_dev->num_ioc; i++) {
  1530. int res_size;
  1531. #ifdef DEBUG_DMB_TRAP
  1532. extern void iterate_pages(unsigned long , unsigned long ,
  1533. void (*)(pte_t * , unsigned long),
  1534. unsigned long );
  1535. void set_data_memory_break(pte_t * , unsigned long);
  1536. #endif
  1537. /* resource map size dictated by pdir_size */
  1538. res_size = sba_dev->ioc[i].pdir_size/sizeof(u64); /* entries */
  1539. /* Second part of PIRANHA BUG */
  1540. if (piranha_bad_128k) {
  1541. res_size -= (128*1024)/sizeof(u64);
  1542. }
  1543. res_size >>= 3; /* convert bit count to byte count */
  1544. DBG_INIT("%s() res_size 0x%x\n",
  1545. __FUNCTION__, res_size);
  1546. sba_dev->ioc[i].res_size = res_size;
  1547. sba_dev->ioc[i].res_map = (char *) __get_free_pages(GFP_KERNEL, get_order(res_size));
  1548. #ifdef DEBUG_DMB_TRAP
  1549. iterate_pages( sba_dev->ioc[i].res_map, res_size,
  1550. set_data_memory_break, 0);
  1551. #endif
  1552. if (NULL == sba_dev->ioc[i].res_map)
  1553. {
  1554. panic("%s:%s() could not allocate resource map\n",
  1555. __FILE__, __FUNCTION__ );
  1556. }
  1557. memset(sba_dev->ioc[i].res_map, 0, res_size);
  1558. /* next available IOVP - circular search */
  1559. sba_dev->ioc[i].res_hint = (unsigned long *)
  1560. &(sba_dev->ioc[i].res_map[L1_CACHE_BYTES]);
  1561. #ifdef ASSERT_PDIR_SANITY
  1562. /* Mark first bit busy - ie no IOVA 0 */
  1563. sba_dev->ioc[i].res_map[0] = 0x80;
  1564. sba_dev->ioc[i].pdir_base[0] = 0xeeffc0addbba0080ULL;
  1565. #endif
  1566. /* Third (and last) part of PIRANHA BUG */
  1567. if (piranha_bad_128k) {
  1568. /* region from +1408K to +1536 is un-usable. */
  1569. int idx_start = (1408*1024/sizeof(u64)) >> 3;
  1570. int idx_end = (1536*1024/sizeof(u64)) >> 3;
  1571. long *p_start = (long *) &(sba_dev->ioc[i].res_map[idx_start]);
  1572. long *p_end = (long *) &(sba_dev->ioc[i].res_map[idx_end]);
  1573. /* mark that part of the io pdir busy */
  1574. while (p_start < p_end)
  1575. *p_start++ = -1;
  1576. }
  1577. #ifdef DEBUG_DMB_TRAP
  1578. iterate_pages( sba_dev->ioc[i].res_map, res_size,
  1579. set_data_memory_break, 0);
  1580. iterate_pages( sba_dev->ioc[i].pdir_base, sba_dev->ioc[i].pdir_size,
  1581. set_data_memory_break, 0);
  1582. #endif
  1583. DBG_INIT("%s() %d res_map %x %p\n",
  1584. __FUNCTION__, i, res_size, sba_dev->ioc[i].res_map);
  1585. }
  1586. spin_lock_init(&sba_dev->sba_lock);
  1587. ioc_needs_fdc = boot_cpu_data.pdc.capabilities & PDC_MODEL_IOPDIR_FDC;
  1588. #ifdef DEBUG_SBA_INIT
  1589. /*
  1590. * If the PDC_MODEL capabilities has Non-coherent IO-PDIR bit set
  1591. * (bit #61, big endian), we have to flush and sync every time
  1592. * IO-PDIR is changed in Ike/Astro.
  1593. */
  1594. if (ioc_needs_fdc) {
  1595. printk(KERN_INFO MODULE_NAME " FDC/SYNC required.\n");
  1596. } else {
  1597. printk(KERN_INFO MODULE_NAME " IOC has cache coherent PDIR.\n");
  1598. }
  1599. #endif
  1600. }
  1601. #ifdef CONFIG_PROC_FS
  1602. static int sba_proc_info(struct seq_file *m, void *p)
  1603. {
  1604. struct sba_device *sba_dev = sba_list;
  1605. struct ioc *ioc = &sba_dev->ioc[0]; /* FIXME: Multi-IOC support! */
  1606. int total_pages = (int) (ioc->res_size << 3); /* 8 bits per byte */
  1607. #ifdef SBA_COLLECT_STATS
  1608. unsigned long avg = 0, min, max;
  1609. #endif
  1610. int i, len = 0;
  1611. len += seq_printf(m, "%s rev %d.%d\n",
  1612. sba_dev->name,
  1613. (sba_dev->hw_rev & 0x7) + 1,
  1614. (sba_dev->hw_rev & 0x18) >> 3
  1615. );
  1616. len += seq_printf(m, "IO PDIR size : %d bytes (%d entries)\n",
  1617. (int) ((ioc->res_size << 3) * sizeof(u64)), /* 8 bits/byte */
  1618. total_pages);
  1619. len += seq_printf(m, "Resource bitmap : %d bytes (%d pages)\n",
  1620. ioc->res_size, ioc->res_size << 3); /* 8 bits per byte */
  1621. len += seq_printf(m, "LMMIO_BASE/MASK/ROUTE %08x %08x %08x\n",
  1622. READ_REG32(sba_dev->sba_hpa + LMMIO_DIST_BASE),
  1623. READ_REG32(sba_dev->sba_hpa + LMMIO_DIST_MASK),
  1624. READ_REG32(sba_dev->sba_hpa + LMMIO_DIST_ROUTE)
  1625. );
  1626. for (i=0; i<4; i++)
  1627. len += seq_printf(m, "DIR%d_BASE/MASK/ROUTE %08x %08x %08x\n", i,
  1628. READ_REG32(sba_dev->sba_hpa + LMMIO_DIRECT0_BASE + i*0x18),
  1629. READ_REG32(sba_dev->sba_hpa + LMMIO_DIRECT0_MASK + i*0x18),
  1630. READ_REG32(sba_dev->sba_hpa + LMMIO_DIRECT0_ROUTE + i*0x18)
  1631. );
  1632. #ifdef SBA_COLLECT_STATS
  1633. len += seq_printf(m, "IO PDIR entries : %ld free %ld used (%d%%)\n",
  1634. total_pages - ioc->used_pages, ioc->used_pages,
  1635. (int) (ioc->used_pages * 100 / total_pages));
  1636. min = max = ioc->avg_search[0];
  1637. for (i = 0; i < SBA_SEARCH_SAMPLE; i++) {
  1638. avg += ioc->avg_search[i];
  1639. if (ioc->avg_search[i] > max) max = ioc->avg_search[i];
  1640. if (ioc->avg_search[i] < min) min = ioc->avg_search[i];
  1641. }
  1642. avg /= SBA_SEARCH_SAMPLE;
  1643. len += seq_printf(m, " Bitmap search : %ld/%ld/%ld (min/avg/max CPU Cycles)\n",
  1644. min, avg, max);
  1645. len += seq_printf(m, "pci_map_single(): %12ld calls %12ld pages (avg %d/1000)\n",
  1646. ioc->msingle_calls, ioc->msingle_pages,
  1647. (int) ((ioc->msingle_pages * 1000)/ioc->msingle_calls));
  1648. /* KLUGE - unmap_sg calls unmap_single for each mapped page */
  1649. min = ioc->usingle_calls;
  1650. max = ioc->usingle_pages - ioc->usg_pages;
  1651. len += seq_printf(m, "pci_unmap_single: %12ld calls %12ld pages (avg %d/1000)\n",
  1652. min, max, (int) ((max * 1000)/min));
  1653. len += seq_printf(m, "pci_map_sg() : %12ld calls %12ld pages (avg %d/1000)\n",
  1654. ioc->msg_calls, ioc->msg_pages,
  1655. (int) ((ioc->msg_pages * 1000)/ioc->msg_calls));
  1656. len += seq_printf(m, "pci_unmap_sg() : %12ld calls %12ld pages (avg %d/1000)\n",
  1657. ioc->usg_calls, ioc->usg_pages,
  1658. (int) ((ioc->usg_pages * 1000)/ioc->usg_calls));
  1659. #endif
  1660. return 0;
  1661. }
  1662. static int
  1663. sba_proc_open(struct inode *i, struct file *f)
  1664. {
  1665. return single_open(f, &sba_proc_info, NULL);
  1666. }
  1667. static struct file_operations sba_proc_fops = {
  1668. .owner = THIS_MODULE,
  1669. .open = sba_proc_open,
  1670. .read = seq_read,
  1671. .llseek = seq_lseek,
  1672. .release = single_release,
  1673. };
  1674. static int
  1675. sba_proc_bitmap_info(struct seq_file *m, void *p)
  1676. {
  1677. struct sba_device *sba_dev = sba_list;
  1678. struct ioc *ioc = &sba_dev->ioc[0]; /* FIXME: Multi-IOC support! */
  1679. unsigned int *res_ptr = (unsigned int *)ioc->res_map;
  1680. int i, len = 0;
  1681. for (i = 0; i < (ioc->res_size/sizeof(unsigned int)); ++i, ++res_ptr) {
  1682. if ((i & 7) == 0)
  1683. len += seq_printf(m, "\n ");
  1684. len += seq_printf(m, " %08x", *res_ptr);
  1685. }
  1686. len += seq_printf(m, "\n");
  1687. return 0;
  1688. }
  1689. static int
  1690. sba_proc_bitmap_open(struct inode *i, struct file *f)
  1691. {
  1692. return single_open(f, &sba_proc_bitmap_info, NULL);
  1693. }
  1694. static struct file_operations sba_proc_bitmap_fops = {
  1695. .owner = THIS_MODULE,
  1696. .open = sba_proc_bitmap_open,
  1697. .read = seq_read,
  1698. .llseek = seq_lseek,
  1699. .release = single_release,
  1700. };
  1701. #endif /* CONFIG_PROC_FS */
  1702. static struct parisc_device_id sba_tbl[] = {
  1703. { HPHW_IOA, HVERSION_REV_ANY_ID, ASTRO_RUNWAY_PORT, 0xb },
  1704. { HPHW_BCPORT, HVERSION_REV_ANY_ID, IKE_MERCED_PORT, 0xc },
  1705. { HPHW_BCPORT, HVERSION_REV_ANY_ID, REO_MERCED_PORT, 0xc },
  1706. { HPHW_BCPORT, HVERSION_REV_ANY_ID, REOG_MERCED_PORT, 0xc },
  1707. { HPHW_IOA, HVERSION_REV_ANY_ID, PLUTO_MCKINLEY_PORT, 0xc },
  1708. { 0, }
  1709. };
  1710. int sba_driver_callback(struct parisc_device *);
  1711. static struct parisc_driver sba_driver = {
  1712. .name = MODULE_NAME,
  1713. .id_table = sba_tbl,
  1714. .probe = sba_driver_callback,
  1715. };
  1716. /*
  1717. ** Determine if sba should claim this chip (return 0) or not (return 1).
  1718. ** If so, initialize the chip and tell other partners in crime they
  1719. ** have work to do.
  1720. */
  1721. int
  1722. sba_driver_callback(struct parisc_device *dev)
  1723. {
  1724. struct sba_device *sba_dev;
  1725. u32 func_class;
  1726. int i;
  1727. char *version;
  1728. void __iomem *sba_addr = ioremap_nocache(dev->hpa.start, SBA_FUNC_SIZE);
  1729. struct proc_dir_entry *info_entry, *bitmap_entry, *root;
  1730. sba_dump_ranges(sba_addr);
  1731. /* Read HW Rev First */
  1732. func_class = READ_REG(sba_addr + SBA_FCLASS);
  1733. if (IS_ASTRO(&dev->id)) {
  1734. unsigned long fclass;
  1735. static char astro_rev[]="Astro ?.?";
  1736. /* Astro is broken...Read HW Rev First */
  1737. fclass = READ_REG(sba_addr);
  1738. astro_rev[6] = '1' + (char) (fclass & 0x7);
  1739. astro_rev[8] = '0' + (char) ((fclass & 0x18) >> 3);
  1740. version = astro_rev;
  1741. } else if (IS_IKE(&dev->id)) {
  1742. static char ike_rev[] = "Ike rev ?";
  1743. ike_rev[8] = '0' + (char) (func_class & 0xff);
  1744. version = ike_rev;
  1745. } else if (IS_PLUTO(&dev->id)) {
  1746. static char pluto_rev[]="Pluto ?.?";
  1747. pluto_rev[6] = '0' + (char) ((func_class & 0xf0) >> 4);
  1748. pluto_rev[8] = '0' + (char) (func_class & 0x0f);
  1749. version = pluto_rev;
  1750. } else {
  1751. static char reo_rev[] = "REO rev ?";
  1752. reo_rev[8] = '0' + (char) (func_class & 0xff);
  1753. version = reo_rev;
  1754. }
  1755. if (!global_ioc_cnt) {
  1756. global_ioc_cnt = count_parisc_driver(&sba_driver);
  1757. /* Astro and Pluto have one IOC per SBA */
  1758. if ((!IS_ASTRO(&dev->id)) || (!IS_PLUTO(&dev->id)))
  1759. global_ioc_cnt *= 2;
  1760. }
  1761. printk(KERN_INFO "%s found %s at 0x%lx\n",
  1762. MODULE_NAME, version, dev->hpa.start);
  1763. sba_dev = kzalloc(sizeof(struct sba_device), GFP_KERNEL);
  1764. if (!sba_dev) {
  1765. printk(KERN_ERR MODULE_NAME " - couldn't alloc sba_device\n");
  1766. return -ENOMEM;
  1767. }
  1768. parisc_set_drvdata(dev, sba_dev);
  1769. for(i=0; i<MAX_IOC; i++)
  1770. spin_lock_init(&(sba_dev->ioc[i].res_lock));
  1771. sba_dev->dev = dev;
  1772. sba_dev->hw_rev = func_class;
  1773. sba_dev->iodc = &dev->id;
  1774. sba_dev->name = dev->name;
  1775. sba_dev->sba_hpa = sba_addr;
  1776. sba_get_pat_resources(sba_dev);
  1777. sba_hw_init(sba_dev);
  1778. sba_common_init(sba_dev);
  1779. hppa_dma_ops = &sba_ops;
  1780. #ifdef CONFIG_PROC_FS
  1781. switch (dev->id.hversion) {
  1782. case PLUTO_MCKINLEY_PORT:
  1783. root = proc_mckinley_root;
  1784. break;
  1785. case ASTRO_RUNWAY_PORT:
  1786. case IKE_MERCED_PORT:
  1787. default:
  1788. root = proc_runway_root;
  1789. break;
  1790. }
  1791. info_entry = create_proc_entry("sba_iommu", 0, root);
  1792. bitmap_entry = create_proc_entry("sba_iommu-bitmap", 0, root);
  1793. if (info_entry)
  1794. info_entry->proc_fops = &sba_proc_fops;
  1795. if (bitmap_entry)
  1796. bitmap_entry->proc_fops = &sba_proc_bitmap_fops;
  1797. #endif
  1798. parisc_vmerge_boundary = IOVP_SIZE;
  1799. parisc_vmerge_max_size = IOVP_SIZE * BITS_PER_LONG;
  1800. parisc_has_iommu();
  1801. return 0;
  1802. }
  1803. /*
  1804. ** One time initialization to let the world know the SBA was found.
  1805. ** This is the only routine which is NOT static.
  1806. ** Must be called exactly once before pci_init().
  1807. */
  1808. void __init sba_init(void)
  1809. {
  1810. register_parisc_driver(&sba_driver);
  1811. }
  1812. /**
  1813. * sba_get_iommu - Assign the iommu pointer for the pci bus controller.
  1814. * @dev: The parisc device.
  1815. *
  1816. * Returns the appropriate IOMMU data for the given parisc PCI controller.
  1817. * This is cached and used later for PCI DMA Mapping.
  1818. */
  1819. void * sba_get_iommu(struct parisc_device *pci_hba)
  1820. {
  1821. struct parisc_device *sba_dev = parisc_parent(pci_hba);
  1822. struct sba_device *sba = sba_dev->dev.driver_data;
  1823. char t = sba_dev->id.hw_type;
  1824. int iocnum = (pci_hba->hw_path >> 3); /* rope # */
  1825. WARN_ON((t != HPHW_IOA) && (t != HPHW_BCPORT));
  1826. return &(sba->ioc[iocnum]);
  1827. }
  1828. /**
  1829. * sba_directed_lmmio - return first directed LMMIO range routed to rope
  1830. * @pa_dev: The parisc device.
  1831. * @r: resource PCI host controller wants start/end fields assigned.
  1832. *
  1833. * For the given parisc PCI controller, determine if any direct ranges
  1834. * are routed down the corresponding rope.
  1835. */
  1836. void sba_directed_lmmio(struct parisc_device *pci_hba, struct resource *r)
  1837. {
  1838. struct parisc_device *sba_dev = parisc_parent(pci_hba);
  1839. struct sba_device *sba = sba_dev->dev.driver_data;
  1840. char t = sba_dev->id.hw_type;
  1841. int i;
  1842. int rope = (pci_hba->hw_path & (ROPES_PER_IOC-1)); /* rope # */
  1843. BUG_ON((t!=HPHW_IOA) && (t!=HPHW_BCPORT));
  1844. r->start = r->end = 0;
  1845. /* Astro has 4 directed ranges. Not sure about Ike/Pluto/et al */
  1846. for (i=0; i<4; i++) {
  1847. int base, size;
  1848. void __iomem *reg = sba->sba_hpa + i*0x18;
  1849. base = READ_REG32(reg + LMMIO_DIRECT0_BASE);
  1850. if ((base & 1) == 0)
  1851. continue; /* not enabled */
  1852. size = READ_REG32(reg + LMMIO_DIRECT0_ROUTE);
  1853. if ((size & (ROPES_PER_IOC-1)) != rope)
  1854. continue; /* directed down different rope */
  1855. r->start = (base & ~1UL) | PCI_F_EXTEND;
  1856. size = ~ READ_REG32(reg + LMMIO_DIRECT0_MASK);
  1857. r->end = r->start + size;
  1858. }
  1859. }
  1860. /**
  1861. * sba_distributed_lmmio - return portion of distributed LMMIO range
  1862. * @pa_dev: The parisc device.
  1863. * @r: resource PCI host controller wants start/end fields assigned.
  1864. *
  1865. * For the given parisc PCI controller, return portion of distributed LMMIO
  1866. * range. The distributed LMMIO is always present and it's just a question
  1867. * of the base address and size of the range.
  1868. */
  1869. void sba_distributed_lmmio(struct parisc_device *pci_hba, struct resource *r )
  1870. {
  1871. struct parisc_device *sba_dev = parisc_parent(pci_hba);
  1872. struct sba_device *sba = sba_dev->dev.driver_data;
  1873. char t = sba_dev->id.hw_type;
  1874. int base, size;
  1875. int rope = (pci_hba->hw_path & (ROPES_PER_IOC-1)); /* rope # */
  1876. BUG_ON((t!=HPHW_IOA) && (t!=HPHW_BCPORT));
  1877. r->start = r->end = 0;
  1878. base = READ_REG32(sba->sba_hpa + LMMIO_DIST_BASE);
  1879. if ((base & 1) == 0) {
  1880. BUG(); /* Gah! Distr Range wasn't enabled! */
  1881. return;
  1882. }
  1883. r->start = (base & ~1UL) | PCI_F_EXTEND;
  1884. size = (~READ_REG32(sba->sba_hpa + LMMIO_DIST_MASK)) / ROPES_PER_IOC;
  1885. r->start += rope * (size + 1); /* adjust base for this rope */
  1886. r->end = r->start + size;
  1887. }