pci-calgary.c 34 KB

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