pci-calgary_64.c 40 KB

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