sba_iommu.c 63 KB

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