pci-calgary_64.c 40 KB

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