pci-calgary_64.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592
  1. /*
  2. * Derived from arch/powerpc/kernel/iommu.c
  3. *
  4. * Copyright IBM Corporation, 2006-2007
  5. * Copyright (C) 2006 Jon Mason <jdmason@kudzu.us>
  6. *
  7. * Author: Jon Mason <jdmason@kudzu.us>
  8. * Author: Muli Ben-Yehuda <muli@il.ibm.com>
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/init.h>
  25. #include <linux/types.h>
  26. #include <linux/slab.h>
  27. #include <linux/mm.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/string.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/bitops.h>
  32. #include <linux/pci_ids.h>
  33. #include <linux/pci.h>
  34. #include <linux/delay.h>
  35. #include <linux/scatterlist.h>
  36. #include <linux/iommu-helper.h>
  37. #include <asm/gart.h>
  38. #include <asm/calgary.h>
  39. #include <asm/tce.h>
  40. #include <asm/pci-direct.h>
  41. #include <asm/system.h>
  42. #include <asm/dma.h>
  43. #include <asm/rio.h>
  44. #ifdef CONFIG_CALGARY_IOMMU_ENABLED_BY_DEFAULT
  45. int use_calgary __read_mostly = 1;
  46. #else
  47. int use_calgary __read_mostly = 0;
  48. #endif /* CONFIG_CALGARY_DEFAULT_ENABLED */
  49. #define PCI_DEVICE_ID_IBM_CALGARY 0x02a1
  50. #define PCI_DEVICE_ID_IBM_CALIOC2 0x0308
  51. /* register offsets inside the host bridge space */
  52. #define CALGARY_CONFIG_REG 0x0108
  53. #define PHB_CSR_OFFSET 0x0110 /* Channel Status */
  54. #define PHB_PLSSR_OFFSET 0x0120
  55. #define PHB_CONFIG_RW_OFFSET 0x0160
  56. #define PHB_IOBASE_BAR_LOW 0x0170
  57. #define PHB_IOBASE_BAR_HIGH 0x0180
  58. #define PHB_MEM_1_LOW 0x0190
  59. #define PHB_MEM_1_HIGH 0x01A0
  60. #define PHB_IO_ADDR_SIZE 0x01B0
  61. #define PHB_MEM_1_SIZE 0x01C0
  62. #define PHB_MEM_ST_OFFSET 0x01D0
  63. #define PHB_AER_OFFSET 0x0200
  64. #define PHB_CONFIG_0_HIGH 0x0220
  65. #define PHB_CONFIG_0_LOW 0x0230
  66. #define PHB_CONFIG_0_END 0x0240
  67. #define PHB_MEM_2_LOW 0x02B0
  68. #define PHB_MEM_2_HIGH 0x02C0
  69. #define PHB_MEM_2_SIZE_HIGH 0x02D0
  70. #define PHB_MEM_2_SIZE_LOW 0x02E0
  71. #define PHB_DOSHOLE_OFFSET 0x08E0
  72. /* CalIOC2 specific */
  73. #define PHB_SAVIOR_L2 0x0DB0
  74. #define PHB_PAGE_MIG_CTRL 0x0DA8
  75. #define PHB_PAGE_MIG_DEBUG 0x0DA0
  76. #define PHB_ROOT_COMPLEX_STATUS 0x0CB0
  77. /* PHB_CONFIG_RW */
  78. #define PHB_TCE_ENABLE 0x20000000
  79. #define PHB_SLOT_DISABLE 0x1C000000
  80. #define PHB_DAC_DISABLE 0x01000000
  81. #define PHB_MEM2_ENABLE 0x00400000
  82. #define PHB_MCSR_ENABLE 0x00100000
  83. /* TAR (Table Address Register) */
  84. #define TAR_SW_BITS 0x0000ffffffff800fUL
  85. #define TAR_VALID 0x0000000000000008UL
  86. /* CSR (Channel/DMA Status Register) */
  87. #define CSR_AGENT_MASK 0xffe0ffff
  88. /* CCR (Calgary Configuration Register) */
  89. #define CCR_2SEC_TIMEOUT 0x000000000000000EUL
  90. /* PMCR/PMDR (Page Migration Control/Debug Registers */
  91. #define PMR_SOFTSTOP 0x80000000
  92. #define PMR_SOFTSTOPFAULT 0x40000000
  93. #define PMR_HARDSTOP 0x20000000
  94. #define MAX_NUM_OF_PHBS 8 /* how many PHBs in total? */
  95. #define MAX_NUM_CHASSIS 8 /* max number of chassis */
  96. /* MAX_PHB_BUS_NUM is the maximal possible dev->bus->number */
  97. #define MAX_PHB_BUS_NUM (MAX_NUM_OF_PHBS * MAX_NUM_CHASSIS * 2)
  98. #define PHBS_PER_CALGARY 4
  99. /* register offsets in Calgary's internal register space */
  100. static const unsigned long tar_offsets[] = {
  101. 0x0580 /* TAR0 */,
  102. 0x0588 /* TAR1 */,
  103. 0x0590 /* TAR2 */,
  104. 0x0598 /* TAR3 */
  105. };
  106. static const unsigned long split_queue_offsets[] = {
  107. 0x4870 /* SPLIT QUEUE 0 */,
  108. 0x5870 /* SPLIT QUEUE 1 */,
  109. 0x6870 /* SPLIT QUEUE 2 */,
  110. 0x7870 /* SPLIT QUEUE 3 */
  111. };
  112. static const unsigned long phb_offsets[] = {
  113. 0x8000 /* PHB0 */,
  114. 0x9000 /* PHB1 */,
  115. 0xA000 /* PHB2 */,
  116. 0xB000 /* PHB3 */
  117. };
  118. /* PHB debug registers */
  119. static const unsigned long phb_debug_offsets[] = {
  120. 0x4000 /* PHB 0 DEBUG */,
  121. 0x5000 /* PHB 1 DEBUG */,
  122. 0x6000 /* PHB 2 DEBUG */,
  123. 0x7000 /* PHB 3 DEBUG */
  124. };
  125. /*
  126. * STUFF register for each debug PHB,
  127. * byte 1 = start bus number, byte 2 = end bus number
  128. */
  129. #define PHB_DEBUG_STUFF_OFFSET 0x0020
  130. #define EMERGENCY_PAGES 32 /* = 128KB */
  131. unsigned int specified_table_size = TCE_TABLE_SIZE_UNSPECIFIED;
  132. static int translate_empty_slots __read_mostly = 0;
  133. static int calgary_detected __read_mostly = 0;
  134. static struct rio_table_hdr *rio_table_hdr __initdata;
  135. static struct scal_detail *scal_devs[MAX_NUMNODES] __initdata;
  136. static struct rio_detail *rio_devs[MAX_NUMNODES * 4] __initdata;
  137. struct calgary_bus_info {
  138. void *tce_space;
  139. unsigned char translation_disabled;
  140. signed char phbid;
  141. void __iomem *bbar;
  142. };
  143. static void calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev);
  144. static void calgary_tce_cache_blast(struct iommu_table *tbl);
  145. static void calgary_dump_error_regs(struct iommu_table *tbl);
  146. static void calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev);
  147. static void calioc2_tce_cache_blast(struct iommu_table *tbl);
  148. static void calioc2_dump_error_regs(struct iommu_table *tbl);
  149. static struct cal_chipset_ops calgary_chip_ops = {
  150. .handle_quirks = calgary_handle_quirks,
  151. .tce_cache_blast = calgary_tce_cache_blast,
  152. .dump_error_regs = calgary_dump_error_regs
  153. };
  154. static struct cal_chipset_ops calioc2_chip_ops = {
  155. .handle_quirks = calioc2_handle_quirks,
  156. .tce_cache_blast = calioc2_tce_cache_blast,
  157. .dump_error_regs = calioc2_dump_error_regs
  158. };
  159. static struct calgary_bus_info bus_info[MAX_PHB_BUS_NUM] = { { NULL, 0, 0 }, };
  160. /* enable this to stress test the chip's TCE cache */
  161. #ifdef CONFIG_IOMMU_DEBUG
  162. static int debugging = 1;
  163. static inline unsigned long verify_bit_range(unsigned long* bitmap,
  164. int expected, unsigned long start, unsigned long end)
  165. {
  166. unsigned long idx = start;
  167. BUG_ON(start >= end);
  168. while (idx < end) {
  169. if (!!test_bit(idx, bitmap) != expected)
  170. return idx;
  171. ++idx;
  172. }
  173. /* all bits have the expected value */
  174. return ~0UL;
  175. }
  176. #else /* debugging is disabled */
  177. static int debugging;
  178. static inline unsigned long verify_bit_range(unsigned long* bitmap,
  179. int expected, unsigned long start, unsigned long end)
  180. {
  181. return ~0UL;
  182. }
  183. #endif /* CONFIG_IOMMU_DEBUG */
  184. static inline unsigned int num_dma_pages(unsigned long dma, unsigned int dmalen)
  185. {
  186. unsigned int npages;
  187. npages = PAGE_ALIGN(dma + dmalen) - (dma & PAGE_MASK);
  188. npages >>= PAGE_SHIFT;
  189. return npages;
  190. }
  191. static inline int translation_enabled(struct iommu_table *tbl)
  192. {
  193. /* only PHBs with translation enabled have an IOMMU table */
  194. return (tbl != NULL);
  195. }
  196. static void iommu_range_reserve(struct iommu_table *tbl,
  197. unsigned long start_addr, unsigned int npages)
  198. {
  199. unsigned long index;
  200. unsigned long end;
  201. unsigned long badbit;
  202. unsigned long flags;
  203. index = start_addr >> PAGE_SHIFT;
  204. /* bail out if we're asked to reserve a region we don't cover */
  205. if (index >= tbl->it_size)
  206. return;
  207. end = index + npages;
  208. if (end > tbl->it_size) /* don't go off the table */
  209. end = tbl->it_size;
  210. spin_lock_irqsave(&tbl->it_lock, flags);
  211. badbit = verify_bit_range(tbl->it_map, 0, index, end);
  212. if (badbit != ~0UL) {
  213. if (printk_ratelimit())
  214. printk(KERN_ERR "Calgary: entry already allocated at "
  215. "0x%lx tbl %p dma 0x%lx npages %u\n",
  216. badbit, tbl, start_addr, npages);
  217. }
  218. set_bit_string(tbl->it_map, index, npages);
  219. spin_unlock_irqrestore(&tbl->it_lock, flags);
  220. }
  221. static unsigned long iommu_range_alloc(struct device *dev,
  222. struct iommu_table *tbl,
  223. unsigned int npages)
  224. {
  225. unsigned long flags;
  226. unsigned long offset;
  227. unsigned long boundary_size;
  228. boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
  229. PAGE_SIZE) >> PAGE_SHIFT;
  230. BUG_ON(npages == 0);
  231. spin_lock_irqsave(&tbl->it_lock, flags);
  232. offset = iommu_area_alloc(tbl->it_map, tbl->it_size, tbl->it_hint,
  233. npages, 0, boundary_size, 0);
  234. if (offset == ~0UL) {
  235. tbl->chip_ops->tce_cache_blast(tbl);
  236. offset = iommu_area_alloc(tbl->it_map, tbl->it_size, 0,
  237. npages, 0, boundary_size, 0);
  238. if (offset == ~0UL) {
  239. printk(KERN_WARNING "Calgary: IOMMU full.\n");
  240. spin_unlock_irqrestore(&tbl->it_lock, flags);
  241. if (panic_on_overflow)
  242. panic("Calgary: fix the allocator.\n");
  243. else
  244. return bad_dma_address;
  245. }
  246. }
  247. tbl->it_hint = offset + npages;
  248. BUG_ON(tbl->it_hint > tbl->it_size);
  249. spin_unlock_irqrestore(&tbl->it_lock, flags);
  250. return offset;
  251. }
  252. static dma_addr_t iommu_alloc(struct device *dev, struct iommu_table *tbl,
  253. void *vaddr, unsigned int npages, int direction)
  254. {
  255. unsigned long entry;
  256. dma_addr_t ret = bad_dma_address;
  257. entry = iommu_range_alloc(dev, tbl, npages);
  258. if (unlikely(entry == bad_dma_address))
  259. goto error;
  260. /* set the return dma address */
  261. ret = (entry << PAGE_SHIFT) | ((unsigned long)vaddr & ~PAGE_MASK);
  262. /* put the TCEs in the HW table */
  263. tce_build(tbl, entry, npages, (unsigned long)vaddr & PAGE_MASK,
  264. direction);
  265. return ret;
  266. error:
  267. printk(KERN_WARNING "Calgary: failed to allocate %u pages in "
  268. "iommu %p\n", npages, tbl);
  269. return bad_dma_address;
  270. }
  271. static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
  272. unsigned int npages)
  273. {
  274. unsigned long entry;
  275. unsigned long badbit;
  276. unsigned long badend;
  277. unsigned long flags;
  278. /* were we called with bad_dma_address? */
  279. badend = bad_dma_address + (EMERGENCY_PAGES * PAGE_SIZE);
  280. if (unlikely((dma_addr >= bad_dma_address) && (dma_addr < badend))) {
  281. printk(KERN_ERR "Calgary: driver tried unmapping bad DMA "
  282. "address 0x%Lx\n", dma_addr);
  283. WARN_ON(1);
  284. return;
  285. }
  286. entry = dma_addr >> PAGE_SHIFT;
  287. BUG_ON(entry + npages > tbl->it_size);
  288. tce_free(tbl, entry, npages);
  289. spin_lock_irqsave(&tbl->it_lock, flags);
  290. badbit = verify_bit_range(tbl->it_map, 1, entry, entry + npages);
  291. if (badbit != ~0UL) {
  292. if (printk_ratelimit())
  293. printk(KERN_ERR "Calgary: bit is off at 0x%lx "
  294. "tbl %p dma 0x%Lx entry 0x%lx npages %u\n",
  295. badbit, tbl, dma_addr, entry, npages);
  296. }
  297. iommu_area_free(tbl->it_map, entry, npages);
  298. spin_unlock_irqrestore(&tbl->it_lock, flags);
  299. }
  300. static inline struct iommu_table *find_iommu_table(struct device *dev)
  301. {
  302. struct pci_dev *pdev;
  303. struct pci_bus *pbus;
  304. struct iommu_table *tbl;
  305. pdev = to_pci_dev(dev);
  306. pbus = pdev->bus;
  307. /* is the device behind a bridge? Look for the root bus */
  308. while (pbus->parent)
  309. pbus = pbus->parent;
  310. tbl = pci_iommu(pbus);
  311. BUG_ON(tbl && (tbl->it_busno != pbus->number));
  312. return tbl;
  313. }
  314. static void calgary_unmap_sg(struct device *dev,
  315. struct scatterlist *sglist, int nelems, int direction)
  316. {
  317. struct iommu_table *tbl = find_iommu_table(dev);
  318. struct scatterlist *s;
  319. int i;
  320. if (!translation_enabled(tbl))
  321. return;
  322. for_each_sg(sglist, s, nelems, i) {
  323. unsigned int npages;
  324. dma_addr_t dma = s->dma_address;
  325. unsigned int dmalen = s->dma_length;
  326. if (dmalen == 0)
  327. break;
  328. npages = num_dma_pages(dma, dmalen);
  329. iommu_free(tbl, dma, npages);
  330. }
  331. }
  332. static int calgary_nontranslate_map_sg(struct device* dev,
  333. struct scatterlist *sg, int nelems, int direction)
  334. {
  335. struct scatterlist *s;
  336. int i;
  337. for_each_sg(sg, s, nelems, i) {
  338. struct page *p = sg_page(s);
  339. BUG_ON(!p);
  340. s->dma_address = virt_to_bus(sg_virt(s));
  341. s->dma_length = s->length;
  342. }
  343. return nelems;
  344. }
  345. static int calgary_map_sg(struct device *dev, struct scatterlist *sg,
  346. int nelems, int direction)
  347. {
  348. struct iommu_table *tbl = find_iommu_table(dev);
  349. struct scatterlist *s;
  350. unsigned long vaddr;
  351. unsigned int npages;
  352. unsigned long entry;
  353. int i;
  354. if (!translation_enabled(tbl))
  355. return calgary_nontranslate_map_sg(dev, sg, nelems, direction);
  356. for_each_sg(sg, s, nelems, i) {
  357. BUG_ON(!sg_page(s));
  358. vaddr = (unsigned long) sg_virt(s);
  359. npages = num_dma_pages(vaddr, s->length);
  360. entry = iommu_range_alloc(dev, tbl, npages);
  361. if (entry == bad_dma_address) {
  362. /* makes sure unmap knows to stop */
  363. s->dma_length = 0;
  364. goto error;
  365. }
  366. s->dma_address = (entry << PAGE_SHIFT) | s->offset;
  367. /* insert into HW table */
  368. tce_build(tbl, entry, npages, vaddr & PAGE_MASK,
  369. direction);
  370. s->dma_length = s->length;
  371. }
  372. return nelems;
  373. error:
  374. calgary_unmap_sg(dev, sg, nelems, direction);
  375. for_each_sg(sg, s, nelems, i) {
  376. sg->dma_address = bad_dma_address;
  377. sg->dma_length = 0;
  378. }
  379. return 0;
  380. }
  381. static dma_addr_t calgary_map_single(struct device *dev, phys_addr_t paddr,
  382. size_t size, int direction)
  383. {
  384. dma_addr_t dma_handle = bad_dma_address;
  385. void *vaddr = phys_to_virt(paddr);
  386. unsigned long uaddr;
  387. unsigned int npages;
  388. struct iommu_table *tbl = find_iommu_table(dev);
  389. uaddr = (unsigned long)vaddr;
  390. npages = num_dma_pages(uaddr, size);
  391. if (translation_enabled(tbl))
  392. dma_handle = iommu_alloc(dev, tbl, vaddr, npages, direction);
  393. else
  394. dma_handle = virt_to_bus(vaddr);
  395. return dma_handle;
  396. }
  397. static void calgary_unmap_single(struct device *dev, dma_addr_t dma_handle,
  398. size_t size, int direction)
  399. {
  400. struct iommu_table *tbl = find_iommu_table(dev);
  401. unsigned int npages;
  402. if (!translation_enabled(tbl))
  403. return;
  404. npages = num_dma_pages(dma_handle, size);
  405. iommu_free(tbl, dma_handle, npages);
  406. }
  407. static void* calgary_alloc_coherent(struct device *dev, size_t size,
  408. dma_addr_t *dma_handle, gfp_t flag)
  409. {
  410. void *ret = NULL;
  411. dma_addr_t mapping;
  412. unsigned int npages, order;
  413. struct iommu_table *tbl = find_iommu_table(dev);
  414. size = PAGE_ALIGN(size); /* size rounded up to full pages */
  415. npages = size >> PAGE_SHIFT;
  416. order = get_order(size);
  417. /* alloc enough pages (and possibly more) */
  418. ret = (void *)__get_free_pages(flag, order);
  419. if (!ret)
  420. goto error;
  421. memset(ret, 0, size);
  422. if (translation_enabled(tbl)) {
  423. /* set up tces to cover the allocated range */
  424. mapping = iommu_alloc(dev, tbl, ret, npages, DMA_BIDIRECTIONAL);
  425. if (mapping == bad_dma_address)
  426. goto free;
  427. *dma_handle = mapping;
  428. } else /* non translated slot */
  429. *dma_handle = virt_to_bus(ret);
  430. return ret;
  431. free:
  432. free_pages((unsigned long)ret, get_order(size));
  433. ret = NULL;
  434. error:
  435. return ret;
  436. }
  437. static const struct dma_mapping_ops calgary_dma_ops = {
  438. .alloc_coherent = calgary_alloc_coherent,
  439. .map_single = calgary_map_single,
  440. .unmap_single = calgary_unmap_single,
  441. .map_sg = calgary_map_sg,
  442. .unmap_sg = calgary_unmap_sg,
  443. };
  444. static inline void __iomem * busno_to_bbar(unsigned char num)
  445. {
  446. return bus_info[num].bbar;
  447. }
  448. static inline int busno_to_phbid(unsigned char num)
  449. {
  450. return bus_info[num].phbid;
  451. }
  452. static inline unsigned long split_queue_offset(unsigned char num)
  453. {
  454. size_t idx = busno_to_phbid(num);
  455. return split_queue_offsets[idx];
  456. }
  457. static inline unsigned long tar_offset(unsigned char num)
  458. {
  459. size_t idx = busno_to_phbid(num);
  460. return tar_offsets[idx];
  461. }
  462. static inline unsigned long phb_offset(unsigned char num)
  463. {
  464. size_t idx = busno_to_phbid(num);
  465. return phb_offsets[idx];
  466. }
  467. static inline void __iomem* calgary_reg(void __iomem *bar, unsigned long offset)
  468. {
  469. unsigned long target = ((unsigned long)bar) | offset;
  470. return (void __iomem*)target;
  471. }
  472. static inline int is_calioc2(unsigned short device)
  473. {
  474. return (device == PCI_DEVICE_ID_IBM_CALIOC2);
  475. }
  476. static inline int is_calgary(unsigned short device)
  477. {
  478. return (device == PCI_DEVICE_ID_IBM_CALGARY);
  479. }
  480. static inline int is_cal_pci_dev(unsigned short device)
  481. {
  482. return (is_calgary(device) || is_calioc2(device));
  483. }
  484. static void calgary_tce_cache_blast(struct iommu_table *tbl)
  485. {
  486. u64 val;
  487. u32 aer;
  488. int i = 0;
  489. void __iomem *bbar = tbl->bbar;
  490. void __iomem *target;
  491. /* disable arbitration on the bus */
  492. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
  493. aer = readl(target);
  494. writel(0, target);
  495. /* read plssr to ensure it got there */
  496. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
  497. val = readl(target);
  498. /* poll split queues until all DMA activity is done */
  499. target = calgary_reg(bbar, split_queue_offset(tbl->it_busno));
  500. do {
  501. val = readq(target);
  502. i++;
  503. } while ((val & 0xff) != 0xff && i < 100);
  504. if (i == 100)
  505. printk(KERN_WARNING "Calgary: PCI bus not quiesced, "
  506. "continuing anyway\n");
  507. /* invalidate TCE cache */
  508. target = calgary_reg(bbar, tar_offset(tbl->it_busno));
  509. writeq(tbl->tar_val, target);
  510. /* enable arbitration */
  511. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
  512. writel(aer, target);
  513. (void)readl(target); /* flush */
  514. }
  515. static void calioc2_tce_cache_blast(struct iommu_table *tbl)
  516. {
  517. void __iomem *bbar = tbl->bbar;
  518. void __iomem *target;
  519. u64 val64;
  520. u32 val;
  521. int i = 0;
  522. int count = 1;
  523. unsigned char bus = tbl->it_busno;
  524. begin:
  525. printk(KERN_DEBUG "Calgary: CalIOC2 bus 0x%x entering tce cache blast "
  526. "sequence - count %d\n", bus, count);
  527. /* 1. using the Page Migration Control reg set SoftStop */
  528. target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
  529. val = be32_to_cpu(readl(target));
  530. printk(KERN_DEBUG "1a. read 0x%x [LE] from %p\n", val, target);
  531. val |= PMR_SOFTSTOP;
  532. printk(KERN_DEBUG "1b. writing 0x%x [LE] to %p\n", val, target);
  533. writel(cpu_to_be32(val), target);
  534. /* 2. poll split queues until all DMA activity is done */
  535. printk(KERN_DEBUG "2a. starting to poll split queues\n");
  536. target = calgary_reg(bbar, split_queue_offset(bus));
  537. do {
  538. val64 = readq(target);
  539. i++;
  540. } while ((val64 & 0xff) != 0xff && i < 100);
  541. if (i == 100)
  542. printk(KERN_WARNING "CalIOC2: PCI bus not quiesced, "
  543. "continuing anyway\n");
  544. /* 3. poll Page Migration DEBUG for SoftStopFault */
  545. target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG);
  546. val = be32_to_cpu(readl(target));
  547. printk(KERN_DEBUG "3. read 0x%x [LE] from %p\n", val, target);
  548. /* 4. if SoftStopFault - goto (1) */
  549. if (val & PMR_SOFTSTOPFAULT) {
  550. if (++count < 100)
  551. goto begin;
  552. else {
  553. printk(KERN_WARNING "CalIOC2: too many SoftStopFaults, "
  554. "aborting TCE cache flush sequence!\n");
  555. return; /* pray for the best */
  556. }
  557. }
  558. /* 5. Slam into HardStop by reading PHB_PAGE_MIG_CTRL */
  559. target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
  560. printk(KERN_DEBUG "5a. slamming into HardStop by reading %p\n", target);
  561. val = be32_to_cpu(readl(target));
  562. printk(KERN_DEBUG "5b. read 0x%x [LE] from %p\n", val, target);
  563. target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG);
  564. val = be32_to_cpu(readl(target));
  565. printk(KERN_DEBUG "5c. read 0x%x [LE] from %p (debug)\n", val, target);
  566. /* 6. invalidate TCE cache */
  567. printk(KERN_DEBUG "6. invalidating TCE cache\n");
  568. target = calgary_reg(bbar, tar_offset(bus));
  569. writeq(tbl->tar_val, target);
  570. /* 7. Re-read PMCR */
  571. printk(KERN_DEBUG "7a. Re-reading PMCR\n");
  572. target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
  573. val = be32_to_cpu(readl(target));
  574. printk(KERN_DEBUG "7b. read 0x%x [LE] from %p\n", val, target);
  575. /* 8. Remove HardStop */
  576. printk(KERN_DEBUG "8a. removing HardStop from PMCR\n");
  577. target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
  578. val = 0;
  579. printk(KERN_DEBUG "8b. writing 0x%x [LE] to %p\n", val, target);
  580. writel(cpu_to_be32(val), target);
  581. val = be32_to_cpu(readl(target));
  582. printk(KERN_DEBUG "8c. read 0x%x [LE] from %p\n", val, target);
  583. }
  584. static void __init calgary_reserve_mem_region(struct pci_dev *dev, u64 start,
  585. u64 limit)
  586. {
  587. unsigned int numpages;
  588. limit = limit | 0xfffff;
  589. limit++;
  590. numpages = ((limit - start) >> PAGE_SHIFT);
  591. iommu_range_reserve(pci_iommu(dev->bus), start, numpages);
  592. }
  593. static void __init calgary_reserve_peripheral_mem_1(struct pci_dev *dev)
  594. {
  595. void __iomem *target;
  596. u64 low, high, sizelow;
  597. u64 start, limit;
  598. struct iommu_table *tbl = pci_iommu(dev->bus);
  599. unsigned char busnum = dev->bus->number;
  600. void __iomem *bbar = tbl->bbar;
  601. /* peripheral MEM_1 region */
  602. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_LOW);
  603. low = be32_to_cpu(readl(target));
  604. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_HIGH);
  605. high = be32_to_cpu(readl(target));
  606. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_SIZE);
  607. sizelow = be32_to_cpu(readl(target));
  608. start = (high << 32) | low;
  609. limit = sizelow;
  610. calgary_reserve_mem_region(dev, start, limit);
  611. }
  612. static void __init calgary_reserve_peripheral_mem_2(struct pci_dev *dev)
  613. {
  614. void __iomem *target;
  615. u32 val32;
  616. u64 low, high, sizelow, sizehigh;
  617. u64 start, limit;
  618. struct iommu_table *tbl = pci_iommu(dev->bus);
  619. unsigned char busnum = dev->bus->number;
  620. void __iomem *bbar = tbl->bbar;
  621. /* is it enabled? */
  622. target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
  623. val32 = be32_to_cpu(readl(target));
  624. if (!(val32 & PHB_MEM2_ENABLE))
  625. return;
  626. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_LOW);
  627. low = be32_to_cpu(readl(target));
  628. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_HIGH);
  629. high = be32_to_cpu(readl(target));
  630. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_LOW);
  631. sizelow = be32_to_cpu(readl(target));
  632. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_HIGH);
  633. sizehigh = be32_to_cpu(readl(target));
  634. start = (high << 32) | low;
  635. limit = (sizehigh << 32) | sizelow;
  636. calgary_reserve_mem_region(dev, start, limit);
  637. }
  638. /*
  639. * some regions of the IO address space do not get translated, so we
  640. * must not give devices IO addresses in those regions. The regions
  641. * are the 640KB-1MB region and the two PCI peripheral memory holes.
  642. * Reserve all of them in the IOMMU bitmap to avoid giving them out
  643. * later.
  644. */
  645. static void __init calgary_reserve_regions(struct pci_dev *dev)
  646. {
  647. unsigned int npages;
  648. u64 start;
  649. struct iommu_table *tbl = pci_iommu(dev->bus);
  650. /* reserve EMERGENCY_PAGES from bad_dma_address and up */
  651. iommu_range_reserve(tbl, bad_dma_address, EMERGENCY_PAGES);
  652. /* avoid the BIOS/VGA first 640KB-1MB region */
  653. /* for CalIOC2 - avoid the entire first MB */
  654. if (is_calgary(dev->device)) {
  655. start = (640 * 1024);
  656. npages = ((1024 - 640) * 1024) >> PAGE_SHIFT;
  657. } else { /* calioc2 */
  658. start = 0;
  659. npages = (1 * 1024 * 1024) >> PAGE_SHIFT;
  660. }
  661. iommu_range_reserve(tbl, start, npages);
  662. /* reserve the two PCI peripheral memory regions in IO space */
  663. calgary_reserve_peripheral_mem_1(dev);
  664. calgary_reserve_peripheral_mem_2(dev);
  665. }
  666. static int __init calgary_setup_tar(struct pci_dev *dev, void __iomem *bbar)
  667. {
  668. u64 val64;
  669. u64 table_phys;
  670. void __iomem *target;
  671. int ret;
  672. struct iommu_table *tbl;
  673. /* build TCE tables for each PHB */
  674. ret = build_tce_table(dev, bbar);
  675. if (ret)
  676. return ret;
  677. tbl = pci_iommu(dev->bus);
  678. tbl->it_base = (unsigned long)bus_info[dev->bus->number].tce_space;
  679. tce_free(tbl, 0, tbl->it_size);
  680. if (is_calgary(dev->device))
  681. tbl->chip_ops = &calgary_chip_ops;
  682. else if (is_calioc2(dev->device))
  683. tbl->chip_ops = &calioc2_chip_ops;
  684. else
  685. BUG();
  686. calgary_reserve_regions(dev);
  687. /* set TARs for each PHB */
  688. target = calgary_reg(bbar, tar_offset(dev->bus->number));
  689. val64 = be64_to_cpu(readq(target));
  690. /* zero out all TAR bits under sw control */
  691. val64 &= ~TAR_SW_BITS;
  692. table_phys = (u64)__pa(tbl->it_base);
  693. val64 |= table_phys;
  694. BUG_ON(specified_table_size > TCE_TABLE_SIZE_8M);
  695. val64 |= (u64) specified_table_size;
  696. tbl->tar_val = cpu_to_be64(val64);
  697. writeq(tbl->tar_val, target);
  698. readq(target); /* flush */
  699. return 0;
  700. }
  701. static void __init calgary_free_bus(struct pci_dev *dev)
  702. {
  703. u64 val64;
  704. struct iommu_table *tbl = pci_iommu(dev->bus);
  705. void __iomem *target;
  706. unsigned int bitmapsz;
  707. target = calgary_reg(tbl->bbar, tar_offset(dev->bus->number));
  708. val64 = be64_to_cpu(readq(target));
  709. val64 &= ~TAR_SW_BITS;
  710. writeq(cpu_to_be64(val64), target);
  711. readq(target); /* flush */
  712. bitmapsz = tbl->it_size / BITS_PER_BYTE;
  713. free_pages((unsigned long)tbl->it_map, get_order(bitmapsz));
  714. tbl->it_map = NULL;
  715. kfree(tbl);
  716. set_pci_iommu(dev->bus, NULL);
  717. /* Can't free bootmem allocated memory after system is up :-( */
  718. bus_info[dev->bus->number].tce_space = NULL;
  719. }
  720. static void calgary_dump_error_regs(struct iommu_table *tbl)
  721. {
  722. void __iomem *bbar = tbl->bbar;
  723. void __iomem *target;
  724. u32 csr, plssr;
  725. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
  726. csr = be32_to_cpu(readl(target));
  727. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
  728. plssr = be32_to_cpu(readl(target));
  729. /* If no error, the agent ID in the CSR is not valid */
  730. printk(KERN_EMERG "Calgary: DMA error on Calgary PHB 0x%x, "
  731. "0x%08x@CSR 0x%08x@PLSSR\n", tbl->it_busno, csr, plssr);
  732. }
  733. static void calioc2_dump_error_regs(struct iommu_table *tbl)
  734. {
  735. void __iomem *bbar = tbl->bbar;
  736. u32 csr, csmr, plssr, mck, rcstat;
  737. void __iomem *target;
  738. unsigned long phboff = phb_offset(tbl->it_busno);
  739. unsigned long erroff;
  740. u32 errregs[7];
  741. int i;
  742. /* dump CSR */
  743. target = calgary_reg(bbar, phboff | PHB_CSR_OFFSET);
  744. csr = be32_to_cpu(readl(target));
  745. /* dump PLSSR */
  746. target = calgary_reg(bbar, phboff | PHB_PLSSR_OFFSET);
  747. plssr = be32_to_cpu(readl(target));
  748. /* dump CSMR */
  749. target = calgary_reg(bbar, phboff | 0x290);
  750. csmr = be32_to_cpu(readl(target));
  751. /* dump mck */
  752. target = calgary_reg(bbar, phboff | 0x800);
  753. mck = be32_to_cpu(readl(target));
  754. printk(KERN_EMERG "Calgary: DMA error on CalIOC2 PHB 0x%x\n",
  755. tbl->it_busno);
  756. printk(KERN_EMERG "Calgary: 0x%08x@CSR 0x%08x@PLSSR 0x%08x@CSMR 0x%08x@MCK\n",
  757. csr, plssr, csmr, mck);
  758. /* dump rest of error regs */
  759. printk(KERN_EMERG "Calgary: ");
  760. for (i = 0; i < ARRAY_SIZE(errregs); i++) {
  761. /* err regs are at 0x810 - 0x870 */
  762. erroff = (0x810 + (i * 0x10));
  763. target = calgary_reg(bbar, phboff | erroff);
  764. errregs[i] = be32_to_cpu(readl(target));
  765. printk("0x%08x@0x%lx ", errregs[i], erroff);
  766. }
  767. printk("\n");
  768. /* root complex status */
  769. target = calgary_reg(bbar, phboff | PHB_ROOT_COMPLEX_STATUS);
  770. rcstat = be32_to_cpu(readl(target));
  771. printk(KERN_EMERG "Calgary: 0x%08x@0x%x\n", rcstat,
  772. PHB_ROOT_COMPLEX_STATUS);
  773. }
  774. static void calgary_watchdog(unsigned long data)
  775. {
  776. struct pci_dev *dev = (struct pci_dev *)data;
  777. struct iommu_table *tbl = pci_iommu(dev->bus);
  778. void __iomem *bbar = tbl->bbar;
  779. u32 val32;
  780. void __iomem *target;
  781. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
  782. val32 = be32_to_cpu(readl(target));
  783. /* If no error, the agent ID in the CSR is not valid */
  784. if (val32 & CSR_AGENT_MASK) {
  785. tbl->chip_ops->dump_error_regs(tbl);
  786. /* reset error */
  787. writel(0, target);
  788. /* Disable bus that caused the error */
  789. target = calgary_reg(bbar, phb_offset(tbl->it_busno) |
  790. PHB_CONFIG_RW_OFFSET);
  791. val32 = be32_to_cpu(readl(target));
  792. val32 |= PHB_SLOT_DISABLE;
  793. writel(cpu_to_be32(val32), target);
  794. readl(target); /* flush */
  795. } else {
  796. /* Reset the timer */
  797. mod_timer(&tbl->watchdog_timer, jiffies + 2 * HZ);
  798. }
  799. }
  800. static void __init calgary_set_split_completion_timeout(void __iomem *bbar,
  801. unsigned char busnum, unsigned long timeout)
  802. {
  803. u64 val64;
  804. void __iomem *target;
  805. unsigned int phb_shift = ~0; /* silence gcc */
  806. u64 mask;
  807. switch (busno_to_phbid(busnum)) {
  808. case 0: phb_shift = (63 - 19);
  809. break;
  810. case 1: phb_shift = (63 - 23);
  811. break;
  812. case 2: phb_shift = (63 - 27);
  813. break;
  814. case 3: phb_shift = (63 - 35);
  815. break;
  816. default:
  817. BUG_ON(busno_to_phbid(busnum));
  818. }
  819. target = calgary_reg(bbar, CALGARY_CONFIG_REG);
  820. val64 = be64_to_cpu(readq(target));
  821. /* zero out this PHB's timer bits */
  822. mask = ~(0xFUL << phb_shift);
  823. val64 &= mask;
  824. val64 |= (timeout << phb_shift);
  825. writeq(cpu_to_be64(val64), target);
  826. readq(target); /* flush */
  827. }
  828. static void __init calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
  829. {
  830. unsigned char busnum = dev->bus->number;
  831. void __iomem *bbar = tbl->bbar;
  832. void __iomem *target;
  833. u32 val;
  834. /*
  835. * CalIOC2 designers recommend setting bit 8 in 0xnDB0 to 1
  836. */
  837. target = calgary_reg(bbar, phb_offset(busnum) | PHB_SAVIOR_L2);
  838. val = cpu_to_be32(readl(target));
  839. val |= 0x00800000;
  840. writel(cpu_to_be32(val), target);
  841. }
  842. static void __init calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
  843. {
  844. unsigned char busnum = dev->bus->number;
  845. /*
  846. * Give split completion a longer timeout on bus 1 for aic94xx
  847. * http://bugzilla.kernel.org/show_bug.cgi?id=7180
  848. */
  849. if (is_calgary(dev->device) && (busnum == 1))
  850. calgary_set_split_completion_timeout(tbl->bbar, busnum,
  851. CCR_2SEC_TIMEOUT);
  852. }
  853. static void __init calgary_enable_translation(struct pci_dev *dev)
  854. {
  855. u32 val32;
  856. unsigned char busnum;
  857. void __iomem *target;
  858. void __iomem *bbar;
  859. struct iommu_table *tbl;
  860. busnum = dev->bus->number;
  861. tbl = pci_iommu(dev->bus);
  862. bbar = tbl->bbar;
  863. /* enable TCE in PHB Config Register */
  864. target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
  865. val32 = be32_to_cpu(readl(target));
  866. val32 |= PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE;
  867. printk(KERN_INFO "Calgary: enabling translation on %s PHB %#x\n",
  868. (dev->device == PCI_DEVICE_ID_IBM_CALGARY) ?
  869. "Calgary" : "CalIOC2", busnum);
  870. printk(KERN_INFO "Calgary: errant DMAs will now be prevented on this "
  871. "bus.\n");
  872. writel(cpu_to_be32(val32), target);
  873. readl(target); /* flush */
  874. init_timer(&tbl->watchdog_timer);
  875. tbl->watchdog_timer.function = &calgary_watchdog;
  876. tbl->watchdog_timer.data = (unsigned long)dev;
  877. mod_timer(&tbl->watchdog_timer, jiffies);
  878. }
  879. static void __init calgary_disable_translation(struct pci_dev *dev)
  880. {
  881. u32 val32;
  882. unsigned char busnum;
  883. void __iomem *target;
  884. void __iomem *bbar;
  885. struct iommu_table *tbl;
  886. busnum = dev->bus->number;
  887. tbl = pci_iommu(dev->bus);
  888. bbar = tbl->bbar;
  889. /* disable TCE in PHB Config Register */
  890. target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
  891. val32 = be32_to_cpu(readl(target));
  892. val32 &= ~(PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE);
  893. printk(KERN_INFO "Calgary: disabling translation on PHB %#x!\n", busnum);
  894. writel(cpu_to_be32(val32), target);
  895. readl(target); /* flush */
  896. del_timer_sync(&tbl->watchdog_timer);
  897. }
  898. static void __init calgary_init_one_nontraslated(struct pci_dev *dev)
  899. {
  900. pci_dev_get(dev);
  901. set_pci_iommu(dev->bus, NULL);
  902. /* is the device behind a bridge? */
  903. if (dev->bus->parent)
  904. dev->bus->parent->self = dev;
  905. else
  906. dev->bus->self = dev;
  907. }
  908. static int __init calgary_init_one(struct pci_dev *dev)
  909. {
  910. void __iomem *bbar;
  911. struct iommu_table *tbl;
  912. int ret;
  913. BUG_ON(dev->bus->number >= MAX_PHB_BUS_NUM);
  914. bbar = busno_to_bbar(dev->bus->number);
  915. ret = calgary_setup_tar(dev, bbar);
  916. if (ret)
  917. goto done;
  918. pci_dev_get(dev);
  919. if (dev->bus->parent) {
  920. if (dev->bus->parent->self)
  921. printk(KERN_WARNING "Calgary: IEEEE, dev %p has "
  922. "bus->parent->self!\n", dev);
  923. dev->bus->parent->self = dev;
  924. } else
  925. dev->bus->self = dev;
  926. tbl = pci_iommu(dev->bus);
  927. tbl->chip_ops->handle_quirks(tbl, dev);
  928. calgary_enable_translation(dev);
  929. return 0;
  930. done:
  931. return ret;
  932. }
  933. static int __init calgary_locate_bbars(void)
  934. {
  935. int ret;
  936. int rioidx, phb, bus;
  937. void __iomem *bbar;
  938. void __iomem *target;
  939. unsigned long offset;
  940. u8 start_bus, end_bus;
  941. u32 val;
  942. ret = -ENODATA;
  943. for (rioidx = 0; rioidx < rio_table_hdr->num_rio_dev; rioidx++) {
  944. struct rio_detail *rio = rio_devs[rioidx];
  945. if ((rio->type != COMPAT_CALGARY) && (rio->type != ALT_CALGARY))
  946. continue;
  947. /* map entire 1MB of Calgary config space */
  948. bbar = ioremap_nocache(rio->BBAR, 1024 * 1024);
  949. if (!bbar)
  950. goto error;
  951. for (phb = 0; phb < PHBS_PER_CALGARY; phb++) {
  952. offset = phb_debug_offsets[phb] | PHB_DEBUG_STUFF_OFFSET;
  953. target = calgary_reg(bbar, offset);
  954. val = be32_to_cpu(readl(target));
  955. start_bus = (u8)((val & 0x00FF0000) >> 16);
  956. end_bus = (u8)((val & 0x0000FF00) >> 8);
  957. if (end_bus) {
  958. for (bus = start_bus; bus <= end_bus; bus++) {
  959. bus_info[bus].bbar = bbar;
  960. bus_info[bus].phbid = phb;
  961. }
  962. } else {
  963. bus_info[start_bus].bbar = bbar;
  964. bus_info[start_bus].phbid = phb;
  965. }
  966. }
  967. }
  968. return 0;
  969. error:
  970. /* scan bus_info and iounmap any bbars we previously ioremap'd */
  971. for (bus = 0; bus < ARRAY_SIZE(bus_info); bus++)
  972. if (bus_info[bus].bbar)
  973. iounmap(bus_info[bus].bbar);
  974. return ret;
  975. }
  976. static int __init calgary_init(void)
  977. {
  978. int ret;
  979. struct pci_dev *dev = NULL;
  980. struct calgary_bus_info *info;
  981. ret = calgary_locate_bbars();
  982. if (ret)
  983. return ret;
  984. do {
  985. dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
  986. if (!dev)
  987. break;
  988. if (!is_cal_pci_dev(dev->device))
  989. continue;
  990. info = &bus_info[dev->bus->number];
  991. if (info->translation_disabled) {
  992. calgary_init_one_nontraslated(dev);
  993. continue;
  994. }
  995. if (!info->tce_space && !translate_empty_slots)
  996. continue;
  997. ret = calgary_init_one(dev);
  998. if (ret)
  999. goto error;
  1000. } while (1);
  1001. return ret;
  1002. error:
  1003. do {
  1004. dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
  1005. if (!dev)
  1006. break;
  1007. if (!is_cal_pci_dev(dev->device))
  1008. continue;
  1009. info = &bus_info[dev->bus->number];
  1010. if (info->translation_disabled) {
  1011. pci_dev_put(dev);
  1012. continue;
  1013. }
  1014. if (!info->tce_space && !translate_empty_slots)
  1015. continue;
  1016. calgary_disable_translation(dev);
  1017. calgary_free_bus(dev);
  1018. pci_dev_put(dev); /* Undo calgary_init_one()'s pci_dev_get() */
  1019. } while (1);
  1020. return ret;
  1021. }
  1022. static inline int __init determine_tce_table_size(u64 ram)
  1023. {
  1024. int ret;
  1025. if (specified_table_size != TCE_TABLE_SIZE_UNSPECIFIED)
  1026. return specified_table_size;
  1027. /*
  1028. * Table sizes are from 0 to 7 (TCE_TABLE_SIZE_64K to
  1029. * TCE_TABLE_SIZE_8M). Table size 0 has 8K entries and each
  1030. * larger table size has twice as many entries, so shift the
  1031. * max ram address by 13 to divide by 8K and then look at the
  1032. * order of the result to choose between 0-7.
  1033. */
  1034. ret = get_order(ram >> 13);
  1035. if (ret > TCE_TABLE_SIZE_8M)
  1036. ret = TCE_TABLE_SIZE_8M;
  1037. return ret;
  1038. }
  1039. static int __init build_detail_arrays(void)
  1040. {
  1041. unsigned long ptr;
  1042. int i, scal_detail_size, rio_detail_size;
  1043. if (rio_table_hdr->num_scal_dev > MAX_NUMNODES){
  1044. printk(KERN_WARNING
  1045. "Calgary: MAX_NUMNODES too low! Defined as %d, "
  1046. "but system has %d nodes.\n",
  1047. MAX_NUMNODES, rio_table_hdr->num_scal_dev);
  1048. return -ENODEV;
  1049. }
  1050. switch (rio_table_hdr->version){
  1051. case 2:
  1052. scal_detail_size = 11;
  1053. rio_detail_size = 13;
  1054. break;
  1055. case 3:
  1056. scal_detail_size = 12;
  1057. rio_detail_size = 15;
  1058. break;
  1059. default:
  1060. printk(KERN_WARNING
  1061. "Calgary: Invalid Rio Grande Table Version: %d\n",
  1062. rio_table_hdr->version);
  1063. return -EPROTO;
  1064. }
  1065. ptr = ((unsigned long)rio_table_hdr) + 3;
  1066. for (i = 0; i < rio_table_hdr->num_scal_dev;
  1067. i++, ptr += scal_detail_size)
  1068. scal_devs[i] = (struct scal_detail *)ptr;
  1069. for (i = 0; i < rio_table_hdr->num_rio_dev;
  1070. i++, ptr += rio_detail_size)
  1071. rio_devs[i] = (struct rio_detail *)ptr;
  1072. return 0;
  1073. }
  1074. static int __init calgary_bus_has_devices(int bus, unsigned short pci_dev)
  1075. {
  1076. int dev;
  1077. u32 val;
  1078. if (pci_dev == PCI_DEVICE_ID_IBM_CALIOC2) {
  1079. /*
  1080. * FIXME: properly scan for devices accross the
  1081. * PCI-to-PCI bridge on every CalIOC2 port.
  1082. */
  1083. return 1;
  1084. }
  1085. for (dev = 1; dev < 8; dev++) {
  1086. val = read_pci_config(bus, dev, 0, 0);
  1087. if (val != 0xffffffff)
  1088. break;
  1089. }
  1090. return (val != 0xffffffff);
  1091. }
  1092. void __init detect_calgary(void)
  1093. {
  1094. int bus;
  1095. void *tbl;
  1096. int calgary_found = 0;
  1097. unsigned long ptr;
  1098. unsigned int offset, prev_offset;
  1099. int ret;
  1100. /*
  1101. * if the user specified iommu=off or iommu=soft or we found
  1102. * another HW IOMMU already, bail out.
  1103. */
  1104. if (swiotlb || no_iommu || iommu_detected)
  1105. return;
  1106. if (!use_calgary)
  1107. return;
  1108. if (!early_pci_allowed())
  1109. return;
  1110. printk(KERN_DEBUG "Calgary: detecting Calgary via BIOS EBDA area\n");
  1111. ptr = (unsigned long)phys_to_virt(get_bios_ebda());
  1112. rio_table_hdr = NULL;
  1113. prev_offset = 0;
  1114. offset = 0x180;
  1115. /*
  1116. * The next offset is stored in the 1st word.
  1117. * Only parse up until the offset increases:
  1118. */
  1119. while (offset > prev_offset) {
  1120. /* The block id is stored in the 2nd word */
  1121. if (*((unsigned short *)(ptr + offset + 2)) == 0x4752){
  1122. /* set the pointer past the offset & block id */
  1123. rio_table_hdr = (struct rio_table_hdr *)(ptr + offset + 4);
  1124. break;
  1125. }
  1126. prev_offset = offset;
  1127. offset = *((unsigned short *)(ptr + offset));
  1128. }
  1129. if (!rio_table_hdr) {
  1130. printk(KERN_DEBUG "Calgary: Unable to locate Rio Grande table "
  1131. "in EBDA - bailing!\n");
  1132. return;
  1133. }
  1134. ret = build_detail_arrays();
  1135. if (ret) {
  1136. printk(KERN_DEBUG "Calgary: build_detail_arrays ret %d\n", ret);
  1137. return;
  1138. }
  1139. specified_table_size = determine_tce_table_size(end_pfn * PAGE_SIZE);
  1140. for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) {
  1141. struct calgary_bus_info *info = &bus_info[bus];
  1142. unsigned short pci_device;
  1143. u32 val;
  1144. val = read_pci_config(bus, 0, 0, 0);
  1145. pci_device = (val & 0xFFFF0000) >> 16;
  1146. if (!is_cal_pci_dev(pci_device))
  1147. continue;
  1148. if (info->translation_disabled)
  1149. continue;
  1150. if (calgary_bus_has_devices(bus, pci_device) ||
  1151. translate_empty_slots) {
  1152. tbl = alloc_tce_table();
  1153. if (!tbl)
  1154. goto cleanup;
  1155. info->tce_space = tbl;
  1156. calgary_found = 1;
  1157. }
  1158. }
  1159. printk(KERN_DEBUG "Calgary: finished detection, Calgary %s\n",
  1160. calgary_found ? "found" : "not found");
  1161. if (calgary_found) {
  1162. iommu_detected = 1;
  1163. calgary_detected = 1;
  1164. printk(KERN_INFO "PCI-DMA: Calgary IOMMU detected.\n");
  1165. printk(KERN_INFO "PCI-DMA: Calgary TCE table spec is %d, "
  1166. "CONFIG_IOMMU_DEBUG is %s.\n", specified_table_size,
  1167. debugging ? "enabled" : "disabled");
  1168. }
  1169. return;
  1170. cleanup:
  1171. for (--bus; bus >= 0; --bus) {
  1172. struct calgary_bus_info *info = &bus_info[bus];
  1173. if (info->tce_space)
  1174. free_tce_table(info->tce_space);
  1175. }
  1176. }
  1177. int __init calgary_iommu_init(void)
  1178. {
  1179. int ret;
  1180. if (no_iommu || swiotlb)
  1181. return -ENODEV;
  1182. if (!calgary_detected)
  1183. return -ENODEV;
  1184. /* ok, we're trying to use Calgary - let's roll */
  1185. printk(KERN_INFO "PCI-DMA: Using Calgary IOMMU\n");
  1186. ret = calgary_init();
  1187. if (ret) {
  1188. printk(KERN_ERR "PCI-DMA: Calgary init failed %d, "
  1189. "falling back to no_iommu\n", ret);
  1190. if (end_pfn > MAX_DMA32_PFN)
  1191. printk(KERN_ERR "WARNING more than 4GB of memory, "
  1192. "32bit PCI may malfunction.\n");
  1193. return ret;
  1194. }
  1195. force_iommu = 1;
  1196. bad_dma_address = 0x0;
  1197. dma_ops = &calgary_dma_ops;
  1198. return 0;
  1199. }
  1200. static int __init calgary_parse_options(char *p)
  1201. {
  1202. unsigned int bridge;
  1203. size_t len;
  1204. char* endp;
  1205. while (*p) {
  1206. if (!strncmp(p, "64k", 3))
  1207. specified_table_size = TCE_TABLE_SIZE_64K;
  1208. else if (!strncmp(p, "128k", 4))
  1209. specified_table_size = TCE_TABLE_SIZE_128K;
  1210. else if (!strncmp(p, "256k", 4))
  1211. specified_table_size = TCE_TABLE_SIZE_256K;
  1212. else if (!strncmp(p, "512k", 4))
  1213. specified_table_size = TCE_TABLE_SIZE_512K;
  1214. else if (!strncmp(p, "1M", 2))
  1215. specified_table_size = TCE_TABLE_SIZE_1M;
  1216. else if (!strncmp(p, "2M", 2))
  1217. specified_table_size = TCE_TABLE_SIZE_2M;
  1218. else if (!strncmp(p, "4M", 2))
  1219. specified_table_size = TCE_TABLE_SIZE_4M;
  1220. else if (!strncmp(p, "8M", 2))
  1221. specified_table_size = TCE_TABLE_SIZE_8M;
  1222. len = strlen("translate_empty_slots");
  1223. if (!strncmp(p, "translate_empty_slots", len))
  1224. translate_empty_slots = 1;
  1225. len = strlen("disable");
  1226. if (!strncmp(p, "disable", len)) {
  1227. p += len;
  1228. if (*p == '=')
  1229. ++p;
  1230. if (*p == '\0')
  1231. break;
  1232. bridge = simple_strtol(p, &endp, 0);
  1233. if (p == endp)
  1234. break;
  1235. if (bridge < MAX_PHB_BUS_NUM) {
  1236. printk(KERN_INFO "Calgary: disabling "
  1237. "translation for PHB %#x\n", bridge);
  1238. bus_info[bridge].translation_disabled = 1;
  1239. }
  1240. }
  1241. p = strpbrk(p, ",");
  1242. if (!p)
  1243. break;
  1244. p++; /* skip ',' */
  1245. }
  1246. return 1;
  1247. }
  1248. __setup("calgary=", calgary_parse_options);
  1249. static void __init calgary_fixup_one_tce_space(struct pci_dev *dev)
  1250. {
  1251. struct iommu_table *tbl;
  1252. unsigned int npages;
  1253. int i;
  1254. tbl = pci_iommu(dev->bus);
  1255. for (i = 0; i < 4; i++) {
  1256. struct resource *r = &dev->resource[PCI_BRIDGE_RESOURCES + i];
  1257. /* Don't give out TCEs that map MEM resources */
  1258. if (!(r->flags & IORESOURCE_MEM))
  1259. continue;
  1260. /* 0-based? we reserve the whole 1st MB anyway */
  1261. if (!r->start)
  1262. continue;
  1263. /* cover the whole region */
  1264. npages = (r->end - r->start) >> PAGE_SHIFT;
  1265. npages++;
  1266. iommu_range_reserve(tbl, r->start, npages);
  1267. }
  1268. }
  1269. static int __init calgary_fixup_tce_spaces(void)
  1270. {
  1271. struct pci_dev *dev = NULL;
  1272. struct calgary_bus_info *info;
  1273. if (no_iommu || swiotlb || !calgary_detected)
  1274. return -ENODEV;
  1275. printk(KERN_DEBUG "Calgary: fixing up tce spaces\n");
  1276. do {
  1277. dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
  1278. if (!dev)
  1279. break;
  1280. if (!is_cal_pci_dev(dev->device))
  1281. continue;
  1282. info = &bus_info[dev->bus->number];
  1283. if (info->translation_disabled)
  1284. continue;
  1285. if (!info->tce_space)
  1286. continue;
  1287. calgary_fixup_one_tce_space(dev);
  1288. } while (1);
  1289. return 0;
  1290. }
  1291. /*
  1292. * We need to be call after pcibios_assign_resources (fs_initcall level)
  1293. * and before device_initcall.
  1294. */
  1295. rootfs_initcall(calgary_fixup_tce_spaces);