pci-calgary.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. /*
  2. * Derived from arch/powerpc/kernel/iommu.c
  3. *
  4. * Copyright (C) IBM Corporation, 2006
  5. *
  6. * Author: Jon Mason <jdmason@us.ibm.com>
  7. * Author: Muli Ben-Yehuda <muli@il.ibm.com>
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/config.h>
  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. #define PCI_DEVICE_ID_IBM_CALGARY 0x02a1
  43. #define PCI_VENDOR_DEVICE_ID_CALGARY \
  44. (PCI_VENDOR_ID_IBM | PCI_DEVICE_ID_IBM_CALGARY << 16)
  45. /* we need these for register space address calculation */
  46. #define START_ADDRESS 0xfe000000
  47. #define CHASSIS_BASE 0
  48. #define ONE_BASED_CHASSIS_NUM 1
  49. /* register offsets inside the host bridge space */
  50. #define PHB_CSR_OFFSET 0x0110
  51. #define PHB_PLSSR_OFFSET 0x0120
  52. #define PHB_CONFIG_RW_OFFSET 0x0160
  53. #define PHB_IOBASE_BAR_LOW 0x0170
  54. #define PHB_IOBASE_BAR_HIGH 0x0180
  55. #define PHB_MEM_1_LOW 0x0190
  56. #define PHB_MEM_1_HIGH 0x01A0
  57. #define PHB_IO_ADDR_SIZE 0x01B0
  58. #define PHB_MEM_1_SIZE 0x01C0
  59. #define PHB_MEM_ST_OFFSET 0x01D0
  60. #define PHB_AER_OFFSET 0x0200
  61. #define PHB_CONFIG_0_HIGH 0x0220
  62. #define PHB_CONFIG_0_LOW 0x0230
  63. #define PHB_CONFIG_0_END 0x0240
  64. #define PHB_MEM_2_LOW 0x02B0
  65. #define PHB_MEM_2_HIGH 0x02C0
  66. #define PHB_MEM_2_SIZE_HIGH 0x02D0
  67. #define PHB_MEM_2_SIZE_LOW 0x02E0
  68. #define PHB_DOSHOLE_OFFSET 0x08E0
  69. /* PHB_CONFIG_RW */
  70. #define PHB_TCE_ENABLE 0x20000000
  71. #define PHB_SLOT_DISABLE 0x1C000000
  72. #define PHB_DAC_DISABLE 0x01000000
  73. #define PHB_MEM2_ENABLE 0x00400000
  74. #define PHB_MCSR_ENABLE 0x00100000
  75. /* TAR (Table Address Register) */
  76. #define TAR_SW_BITS 0x0000ffffffff800fUL
  77. #define TAR_VALID 0x0000000000000008UL
  78. /* CSR (Channel/DMA Status Register) */
  79. #define CSR_AGENT_MASK 0xffe0ffff
  80. #define MAX_NUM_OF_PHBS 8 /* how many PHBs in total? */
  81. #define MAX_NUM_CHASSIS 8 /* max number of chassis */
  82. #define MAX_PHB_BUS_NUM (MAX_NUM_OF_PHBS * MAX_NUM_CHASSIS * 2) /* max dev->bus->number */
  83. #define PHBS_PER_CALGARY 4
  84. /* register offsets in Calgary's internal register space */
  85. static const unsigned long tar_offsets[] = {
  86. 0x0580 /* TAR0 */,
  87. 0x0588 /* TAR1 */,
  88. 0x0590 /* TAR2 */,
  89. 0x0598 /* TAR3 */
  90. };
  91. static const unsigned long split_queue_offsets[] = {
  92. 0x4870 /* SPLIT QUEUE 0 */,
  93. 0x5870 /* SPLIT QUEUE 1 */,
  94. 0x6870 /* SPLIT QUEUE 2 */,
  95. 0x7870 /* SPLIT QUEUE 3 */
  96. };
  97. static const unsigned long phb_offsets[] = {
  98. 0x8000 /* PHB0 */,
  99. 0x9000 /* PHB1 */,
  100. 0xA000 /* PHB2 */,
  101. 0xB000 /* PHB3 */
  102. };
  103. static char bus_to_phb[MAX_PHB_BUS_NUM];
  104. void* tce_table_kva[MAX_PHB_BUS_NUM];
  105. unsigned int specified_table_size = TCE_TABLE_SIZE_UNSPECIFIED;
  106. static int translate_empty_slots __read_mostly = 0;
  107. static int calgary_detected __read_mostly = 0;
  108. /*
  109. * the bitmap of PHBs the user requested that we disable
  110. * translation on.
  111. */
  112. static DECLARE_BITMAP(translation_disabled, MAX_PHB_BUS_NUM);
  113. static void tce_cache_blast(struct iommu_table *tbl);
  114. /* enable this to stress test the chip's TCE cache */
  115. #ifdef CONFIG_IOMMU_DEBUG
  116. static inline void tce_cache_blast_stress(struct iommu_table *tbl)
  117. {
  118. tce_cache_blast(tbl);
  119. }
  120. #else
  121. static inline void tce_cache_blast_stress(struct iommu_table *tbl)
  122. {
  123. }
  124. #endif /* BLAST_TCE_CACHE_ON_UNMAP */
  125. static inline unsigned int num_dma_pages(unsigned long dma, unsigned int dmalen)
  126. {
  127. unsigned int npages;
  128. npages = PAGE_ALIGN(dma + dmalen) - (dma & PAGE_MASK);
  129. npages >>= PAGE_SHIFT;
  130. return npages;
  131. }
  132. static inline int translate_phb(struct pci_dev* dev)
  133. {
  134. int disabled = test_bit(dev->bus->number, translation_disabled);
  135. return !disabled;
  136. }
  137. static void iommu_range_reserve(struct iommu_table *tbl,
  138. unsigned long start_addr, unsigned int npages)
  139. {
  140. unsigned long index;
  141. unsigned long end;
  142. index = start_addr >> PAGE_SHIFT;
  143. /* bail out if we're asked to reserve a region we don't cover */
  144. if (index >= tbl->it_size)
  145. return;
  146. end = index + npages;
  147. if (end > tbl->it_size) /* don't go off the table */
  148. end = tbl->it_size;
  149. while (index < end) {
  150. if (test_bit(index, tbl->it_map))
  151. printk(KERN_ERR "Calgary: entry already allocated at "
  152. "0x%lx tbl %p dma 0x%lx npages %u\n",
  153. index, tbl, start_addr, npages);
  154. ++index;
  155. }
  156. set_bit_string(tbl->it_map, start_addr >> PAGE_SHIFT, npages);
  157. }
  158. static unsigned long iommu_range_alloc(struct iommu_table *tbl,
  159. unsigned int npages)
  160. {
  161. unsigned long offset;
  162. BUG_ON(npages == 0);
  163. offset = find_next_zero_string(tbl->it_map, tbl->it_hint,
  164. tbl->it_size, npages);
  165. if (offset == ~0UL) {
  166. tce_cache_blast(tbl);
  167. offset = find_next_zero_string(tbl->it_map, 0,
  168. tbl->it_size, npages);
  169. if (offset == ~0UL) {
  170. printk(KERN_WARNING "Calgary: IOMMU full.\n");
  171. if (panic_on_overflow)
  172. panic("Calgary: fix the allocator.\n");
  173. else
  174. return bad_dma_address;
  175. }
  176. }
  177. set_bit_string(tbl->it_map, offset, npages);
  178. tbl->it_hint = offset + npages;
  179. BUG_ON(tbl->it_hint > tbl->it_size);
  180. return offset;
  181. }
  182. static dma_addr_t iommu_alloc(struct iommu_table *tbl, void *vaddr,
  183. unsigned int npages, int direction)
  184. {
  185. unsigned long entry, flags;
  186. dma_addr_t ret = bad_dma_address;
  187. spin_lock_irqsave(&tbl->it_lock, flags);
  188. entry = iommu_range_alloc(tbl, npages);
  189. if (unlikely(entry == bad_dma_address))
  190. goto error;
  191. /* set the return dma address */
  192. ret = (entry << PAGE_SHIFT) | ((unsigned long)vaddr & ~PAGE_MASK);
  193. /* put the TCEs in the HW table */
  194. tce_build(tbl, entry, npages, (unsigned long)vaddr & PAGE_MASK,
  195. direction);
  196. spin_unlock_irqrestore(&tbl->it_lock, flags);
  197. return ret;
  198. error:
  199. spin_unlock_irqrestore(&tbl->it_lock, flags);
  200. printk(KERN_WARNING "Calgary: failed to allocate %u pages in "
  201. "iommu %p\n", npages, tbl);
  202. return bad_dma_address;
  203. }
  204. static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
  205. unsigned int npages)
  206. {
  207. unsigned long entry;
  208. unsigned long i;
  209. entry = dma_addr >> PAGE_SHIFT;
  210. BUG_ON(entry + npages > tbl->it_size);
  211. tce_free(tbl, entry, npages);
  212. for (i = 0; i < npages; ++i) {
  213. if (!test_bit(entry + i, tbl->it_map))
  214. printk(KERN_ERR "Calgary: bit is off at 0x%lx "
  215. "tbl %p dma 0x%Lx entry 0x%lx npages %u\n",
  216. entry + i, tbl, dma_addr, entry, npages);
  217. }
  218. __clear_bit_string(tbl->it_map, entry, npages);
  219. tce_cache_blast_stress(tbl);
  220. }
  221. static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
  222. unsigned int npages)
  223. {
  224. unsigned long flags;
  225. spin_lock_irqsave(&tbl->it_lock, flags);
  226. __iommu_free(tbl, dma_addr, npages);
  227. spin_unlock_irqrestore(&tbl->it_lock, flags);
  228. }
  229. static void __calgary_unmap_sg(struct iommu_table *tbl,
  230. struct scatterlist *sglist, int nelems, int direction)
  231. {
  232. while (nelems--) {
  233. unsigned int npages;
  234. dma_addr_t dma = sglist->dma_address;
  235. unsigned int dmalen = sglist->dma_length;
  236. if (dmalen == 0)
  237. break;
  238. npages = num_dma_pages(dma, dmalen);
  239. __iommu_free(tbl, dma, npages);
  240. sglist++;
  241. }
  242. }
  243. void calgary_unmap_sg(struct device *dev, struct scatterlist *sglist,
  244. int nelems, int direction)
  245. {
  246. unsigned long flags;
  247. struct iommu_table *tbl = to_pci_dev(dev)->bus->self->sysdata;
  248. if (!translate_phb(to_pci_dev(dev)))
  249. return;
  250. spin_lock_irqsave(&tbl->it_lock, flags);
  251. __calgary_unmap_sg(tbl, sglist, nelems, direction);
  252. spin_unlock_irqrestore(&tbl->it_lock, flags);
  253. }
  254. static int calgary_nontranslate_map_sg(struct device* dev,
  255. struct scatterlist *sg, int nelems, int direction)
  256. {
  257. int i;
  258. for (i = 0; i < nelems; i++ ) {
  259. struct scatterlist *s = &sg[i];
  260. BUG_ON(!s->page);
  261. s->dma_address = virt_to_bus(page_address(s->page) +s->offset);
  262. s->dma_length = s->length;
  263. }
  264. return nelems;
  265. }
  266. int calgary_map_sg(struct device *dev, struct scatterlist *sg,
  267. int nelems, int direction)
  268. {
  269. struct iommu_table *tbl = to_pci_dev(dev)->bus->self->sysdata;
  270. unsigned long flags;
  271. unsigned long vaddr;
  272. unsigned int npages;
  273. unsigned long entry;
  274. int i;
  275. if (!translate_phb(to_pci_dev(dev)))
  276. return calgary_nontranslate_map_sg(dev, sg, nelems, direction);
  277. spin_lock_irqsave(&tbl->it_lock, flags);
  278. for (i = 0; i < nelems; i++ ) {
  279. struct scatterlist *s = &sg[i];
  280. BUG_ON(!s->page);
  281. vaddr = (unsigned long)page_address(s->page) + s->offset;
  282. npages = num_dma_pages(vaddr, s->length);
  283. entry = iommu_range_alloc(tbl, npages);
  284. if (entry == bad_dma_address) {
  285. /* makes sure unmap knows to stop */
  286. s->dma_length = 0;
  287. goto error;
  288. }
  289. s->dma_address = (entry << PAGE_SHIFT) | s->offset;
  290. /* insert into HW table */
  291. tce_build(tbl, entry, npages, vaddr & PAGE_MASK,
  292. direction);
  293. s->dma_length = s->length;
  294. }
  295. spin_unlock_irqrestore(&tbl->it_lock, flags);
  296. return nelems;
  297. error:
  298. __calgary_unmap_sg(tbl, sg, nelems, direction);
  299. for (i = 0; i < nelems; i++) {
  300. sg[i].dma_address = bad_dma_address;
  301. sg[i].dma_length = 0;
  302. }
  303. spin_unlock_irqrestore(&tbl->it_lock, flags);
  304. return 0;
  305. }
  306. dma_addr_t calgary_map_single(struct device *dev, void *vaddr,
  307. size_t size, int direction)
  308. {
  309. dma_addr_t dma_handle = bad_dma_address;
  310. unsigned long uaddr;
  311. unsigned int npages;
  312. struct iommu_table *tbl = to_pci_dev(dev)->bus->self->sysdata;
  313. uaddr = (unsigned long)vaddr;
  314. npages = num_dma_pages(uaddr, size);
  315. if (translate_phb(to_pci_dev(dev)))
  316. dma_handle = iommu_alloc(tbl, vaddr, npages, direction);
  317. else
  318. dma_handle = virt_to_bus(vaddr);
  319. return dma_handle;
  320. }
  321. void calgary_unmap_single(struct device *dev, dma_addr_t dma_handle,
  322. size_t size, int direction)
  323. {
  324. struct iommu_table *tbl = to_pci_dev(dev)->bus->self->sysdata;
  325. unsigned int npages;
  326. if (!translate_phb(to_pci_dev(dev)))
  327. return;
  328. npages = num_dma_pages(dma_handle, size);
  329. iommu_free(tbl, dma_handle, npages);
  330. }
  331. void* calgary_alloc_coherent(struct device *dev, size_t size,
  332. dma_addr_t *dma_handle, gfp_t flag)
  333. {
  334. void *ret = NULL;
  335. dma_addr_t mapping;
  336. unsigned int npages, order;
  337. struct iommu_table *tbl;
  338. tbl = to_pci_dev(dev)->bus->self->sysdata;
  339. size = PAGE_ALIGN(size); /* size rounded up to full pages */
  340. npages = size >> PAGE_SHIFT;
  341. order = get_order(size);
  342. /* alloc enough pages (and possibly more) */
  343. ret = (void *)__get_free_pages(flag, order);
  344. if (!ret)
  345. goto error;
  346. memset(ret, 0, size);
  347. if (translate_phb(to_pci_dev(dev))) {
  348. /* set up tces to cover the allocated range */
  349. mapping = iommu_alloc(tbl, ret, npages, DMA_BIDIRECTIONAL);
  350. if (mapping == bad_dma_address)
  351. goto free;
  352. *dma_handle = mapping;
  353. } else /* non translated slot */
  354. *dma_handle = virt_to_bus(ret);
  355. return ret;
  356. free:
  357. free_pages((unsigned long)ret, get_order(size));
  358. ret = NULL;
  359. error:
  360. return ret;
  361. }
  362. static struct dma_mapping_ops calgary_dma_ops = {
  363. .alloc_coherent = calgary_alloc_coherent,
  364. .map_single = calgary_map_single,
  365. .unmap_single = calgary_unmap_single,
  366. .map_sg = calgary_map_sg,
  367. .unmap_sg = calgary_unmap_sg,
  368. };
  369. static inline int busno_to_phbid(unsigned char num)
  370. {
  371. return bus_to_phb[num];
  372. }
  373. static inline unsigned long split_queue_offset(unsigned char num)
  374. {
  375. size_t idx = busno_to_phbid(num);
  376. return split_queue_offsets[idx];
  377. }
  378. static inline unsigned long tar_offset(unsigned char num)
  379. {
  380. size_t idx = busno_to_phbid(num);
  381. return tar_offsets[idx];
  382. }
  383. static inline unsigned long phb_offset(unsigned char num)
  384. {
  385. size_t idx = busno_to_phbid(num);
  386. return phb_offsets[idx];
  387. }
  388. static inline void __iomem* calgary_reg(void __iomem *bar, unsigned long offset)
  389. {
  390. unsigned long target = ((unsigned long)bar) | offset;
  391. return (void __iomem*)target;
  392. }
  393. static void tce_cache_blast(struct iommu_table *tbl)
  394. {
  395. u64 val;
  396. u32 aer;
  397. int i = 0;
  398. void __iomem *bbar = tbl->bbar;
  399. void __iomem *target;
  400. /* disable arbitration on the bus */
  401. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
  402. aer = readl(target);
  403. writel(0, target);
  404. /* read plssr to ensure it got there */
  405. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
  406. val = readl(target);
  407. /* poll split queues until all DMA activity is done */
  408. target = calgary_reg(bbar, split_queue_offset(tbl->it_busno));
  409. do {
  410. val = readq(target);
  411. i++;
  412. } while ((val & 0xff) != 0xff && i < 100);
  413. if (i == 100)
  414. printk(KERN_WARNING "Calgary: PCI bus not quiesced, "
  415. "continuing anyway\n");
  416. /* invalidate TCE cache */
  417. target = calgary_reg(bbar, tar_offset(tbl->it_busno));
  418. writeq(tbl->tar_val, target);
  419. /* enable arbitration */
  420. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
  421. writel(aer, target);
  422. (void)readl(target); /* flush */
  423. }
  424. static void __init calgary_reserve_mem_region(struct pci_dev *dev, u64 start,
  425. u64 limit)
  426. {
  427. unsigned int numpages;
  428. limit = limit | 0xfffff;
  429. limit++;
  430. numpages = ((limit - start) >> PAGE_SHIFT);
  431. iommu_range_reserve(dev->sysdata, start, numpages);
  432. }
  433. static void __init calgary_reserve_peripheral_mem_1(struct pci_dev *dev)
  434. {
  435. void __iomem *target;
  436. u64 low, high, sizelow;
  437. u64 start, limit;
  438. struct iommu_table *tbl = dev->sysdata;
  439. unsigned char busnum = dev->bus->number;
  440. void __iomem *bbar = tbl->bbar;
  441. /* peripheral MEM_1 region */
  442. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_LOW);
  443. low = be32_to_cpu(readl(target));
  444. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_HIGH);
  445. high = be32_to_cpu(readl(target));
  446. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_SIZE);
  447. sizelow = be32_to_cpu(readl(target));
  448. start = (high << 32) | low;
  449. limit = sizelow;
  450. calgary_reserve_mem_region(dev, start, limit);
  451. }
  452. static void __init calgary_reserve_peripheral_mem_2(struct pci_dev *dev)
  453. {
  454. void __iomem *target;
  455. u32 val32;
  456. u64 low, high, sizelow, sizehigh;
  457. u64 start, limit;
  458. struct iommu_table *tbl = dev->sysdata;
  459. unsigned char busnum = dev->bus->number;
  460. void __iomem *bbar = tbl->bbar;
  461. /* is it enabled? */
  462. target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
  463. val32 = be32_to_cpu(readl(target));
  464. if (!(val32 & PHB_MEM2_ENABLE))
  465. return;
  466. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_LOW);
  467. low = be32_to_cpu(readl(target));
  468. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_HIGH);
  469. high = be32_to_cpu(readl(target));
  470. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_LOW);
  471. sizelow = be32_to_cpu(readl(target));
  472. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_HIGH);
  473. sizehigh = be32_to_cpu(readl(target));
  474. start = (high << 32) | low;
  475. limit = (sizehigh << 32) | sizelow;
  476. calgary_reserve_mem_region(dev, start, limit);
  477. }
  478. /*
  479. * some regions of the IO address space do not get translated, so we
  480. * must not give devices IO addresses in those regions. The regions
  481. * are the 640KB-1MB region and the two PCI peripheral memory holes.
  482. * Reserve all of them in the IOMMU bitmap to avoid giving them out
  483. * later.
  484. */
  485. static void __init calgary_reserve_regions(struct pci_dev *dev)
  486. {
  487. unsigned int npages;
  488. void __iomem *bbar;
  489. unsigned char busnum;
  490. u64 start;
  491. struct iommu_table *tbl = dev->sysdata;
  492. bbar = tbl->bbar;
  493. busnum = dev->bus->number;
  494. /* reserve bad_dma_address in case it's a legal address */
  495. iommu_range_reserve(tbl, bad_dma_address, 1);
  496. /* avoid the BIOS/VGA first 640KB-1MB region */
  497. start = (640 * 1024);
  498. npages = ((1024 - 640) * 1024) >> PAGE_SHIFT;
  499. iommu_range_reserve(tbl, start, npages);
  500. /* reserve the two PCI peripheral memory regions in IO space */
  501. calgary_reserve_peripheral_mem_1(dev);
  502. calgary_reserve_peripheral_mem_2(dev);
  503. }
  504. static int __init calgary_setup_tar(struct pci_dev *dev, void __iomem *bbar)
  505. {
  506. u64 val64;
  507. u64 table_phys;
  508. void __iomem *target;
  509. int ret;
  510. struct iommu_table *tbl;
  511. /* build TCE tables for each PHB */
  512. ret = build_tce_table(dev, bbar);
  513. if (ret)
  514. return ret;
  515. calgary_reserve_regions(dev);
  516. /* set TARs for each PHB */
  517. target = calgary_reg(bbar, tar_offset(dev->bus->number));
  518. val64 = be64_to_cpu(readq(target));
  519. /* zero out all TAR bits under sw control */
  520. val64 &= ~TAR_SW_BITS;
  521. tbl = dev->sysdata;
  522. table_phys = (u64)__pa(tbl->it_base);
  523. val64 |= table_phys;
  524. BUG_ON(specified_table_size > TCE_TABLE_SIZE_8M);
  525. val64 |= (u64) specified_table_size;
  526. tbl->tar_val = cpu_to_be64(val64);
  527. writeq(tbl->tar_val, target);
  528. readq(target); /* flush */
  529. return 0;
  530. }
  531. static void __init calgary_free_tar(struct pci_dev *dev)
  532. {
  533. u64 val64;
  534. struct iommu_table *tbl = dev->sysdata;
  535. void __iomem *target;
  536. target = calgary_reg(tbl->bbar, tar_offset(dev->bus->number));
  537. val64 = be64_to_cpu(readq(target));
  538. val64 &= ~TAR_SW_BITS;
  539. writeq(cpu_to_be64(val64), target);
  540. readq(target); /* flush */
  541. kfree(tbl);
  542. dev->sysdata = NULL;
  543. }
  544. static void calgary_watchdog(unsigned long data)
  545. {
  546. struct pci_dev *dev = (struct pci_dev *)data;
  547. struct iommu_table *tbl = dev->sysdata;
  548. void __iomem *bbar = tbl->bbar;
  549. u32 val32;
  550. void __iomem *target;
  551. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
  552. val32 = be32_to_cpu(readl(target));
  553. /* If no error, the agent ID in the CSR is not valid */
  554. if (val32 & CSR_AGENT_MASK) {
  555. printk(KERN_EMERG "calgary_watchdog: DMA error on bus %d, "
  556. "CSR = %#x\n", dev->bus->number, val32);
  557. writel(0, target);
  558. /* Disable bus that caused the error */
  559. target = calgary_reg(bbar, phb_offset(tbl->it_busno) |
  560. PHB_CONFIG_RW_OFFSET);
  561. val32 = be32_to_cpu(readl(target));
  562. val32 |= PHB_SLOT_DISABLE;
  563. writel(cpu_to_be32(val32), target);
  564. readl(target); /* flush */
  565. } else {
  566. /* Reset the timer */
  567. mod_timer(&tbl->watchdog_timer, jiffies + 2 * HZ);
  568. }
  569. }
  570. static void __init calgary_enable_translation(struct pci_dev *dev)
  571. {
  572. u32 val32;
  573. unsigned char busnum;
  574. void __iomem *target;
  575. void __iomem *bbar;
  576. struct iommu_table *tbl;
  577. busnum = dev->bus->number;
  578. tbl = dev->sysdata;
  579. bbar = tbl->bbar;
  580. /* enable TCE in PHB Config Register */
  581. target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
  582. val32 = be32_to_cpu(readl(target));
  583. val32 |= PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE;
  584. printk(KERN_INFO "Calgary: enabling translation on PHB %d\n", busnum);
  585. printk(KERN_INFO "Calgary: errant DMAs will now be prevented on this "
  586. "bus.\n");
  587. writel(cpu_to_be32(val32), target);
  588. readl(target); /* flush */
  589. init_timer(&tbl->watchdog_timer);
  590. tbl->watchdog_timer.function = &calgary_watchdog;
  591. tbl->watchdog_timer.data = (unsigned long)dev;
  592. mod_timer(&tbl->watchdog_timer, jiffies);
  593. }
  594. static void __init calgary_disable_translation(struct pci_dev *dev)
  595. {
  596. u32 val32;
  597. unsigned char busnum;
  598. void __iomem *target;
  599. void __iomem *bbar;
  600. struct iommu_table *tbl;
  601. busnum = dev->bus->number;
  602. tbl = dev->sysdata;
  603. bbar = tbl->bbar;
  604. /* disable TCE in PHB Config Register */
  605. target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
  606. val32 = be32_to_cpu(readl(target));
  607. val32 &= ~(PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE);
  608. printk(KERN_INFO "Calgary: disabling translation on PHB %d!\n", busnum);
  609. writel(cpu_to_be32(val32), target);
  610. readl(target); /* flush */
  611. del_timer_sync(&tbl->watchdog_timer);
  612. }
  613. static inline unsigned int __init locate_register_space(struct pci_dev *dev)
  614. {
  615. int rionodeid;
  616. u32 address;
  617. rionodeid = (dev->bus->number % 15 > 4) ? 3 : 2;
  618. /*
  619. * register space address calculation as follows:
  620. * FE0MB-8MB*OneBasedChassisNumber+1MB*(RioNodeId-ChassisBase)
  621. * ChassisBase is always zero for x366/x260/x460
  622. * RioNodeId is 2 for first Calgary, 3 for second Calgary
  623. */
  624. address = START_ADDRESS -
  625. (0x800000 * (ONE_BASED_CHASSIS_NUM + dev->bus->number / 15)) +
  626. (0x100000) * (rionodeid - CHASSIS_BASE);
  627. return address;
  628. }
  629. static int __init calgary_init_one_nontraslated(struct pci_dev *dev)
  630. {
  631. dev->sysdata = NULL;
  632. dev->bus->self = dev;
  633. return 0;
  634. }
  635. static int __init calgary_init_one(struct pci_dev *dev)
  636. {
  637. u32 address;
  638. void __iomem *bbar;
  639. int ret;
  640. address = locate_register_space(dev);
  641. /* map entire 1MB of Calgary config space */
  642. bbar = ioremap_nocache(address, 1024 * 1024);
  643. if (!bbar) {
  644. ret = -ENODATA;
  645. goto done;
  646. }
  647. ret = calgary_setup_tar(dev, bbar);
  648. if (ret)
  649. goto iounmap;
  650. dev->bus->self = dev;
  651. calgary_enable_translation(dev);
  652. return 0;
  653. iounmap:
  654. iounmap(bbar);
  655. done:
  656. return ret;
  657. }
  658. static int __init calgary_init(void)
  659. {
  660. int i, ret = -ENODEV;
  661. struct pci_dev *dev = NULL;
  662. for (i = 0; i < MAX_PHB_BUS_NUM; i++) {
  663. dev = pci_get_device(PCI_VENDOR_ID_IBM,
  664. PCI_DEVICE_ID_IBM_CALGARY,
  665. dev);
  666. if (!dev)
  667. break;
  668. if (!translate_phb(dev)) {
  669. calgary_init_one_nontraslated(dev);
  670. continue;
  671. }
  672. if (!tce_table_kva[dev->bus->number] && !translate_empty_slots) {
  673. pci_dev_put(dev);
  674. continue;
  675. }
  676. ret = calgary_init_one(dev);
  677. if (ret)
  678. goto error;
  679. }
  680. return ret;
  681. error:
  682. for (i--; i >= 0; i--) {
  683. dev = pci_find_device_reverse(PCI_VENDOR_ID_IBM,
  684. PCI_DEVICE_ID_IBM_CALGARY,
  685. dev);
  686. if (!translate_phb(dev)) {
  687. pci_dev_put(dev);
  688. continue;
  689. }
  690. if (!tce_table_kva[dev->bus->number] && !translate_empty_slots)
  691. continue;
  692. calgary_disable_translation(dev);
  693. calgary_free_tar(dev);
  694. pci_dev_put(dev);
  695. }
  696. return ret;
  697. }
  698. static inline int __init determine_tce_table_size(u64 ram)
  699. {
  700. int ret;
  701. if (specified_table_size != TCE_TABLE_SIZE_UNSPECIFIED)
  702. return specified_table_size;
  703. /*
  704. * Table sizes are from 0 to 7 (TCE_TABLE_SIZE_64K to
  705. * TCE_TABLE_SIZE_8M). Table size 0 has 8K entries and each
  706. * larger table size has twice as many entries, so shift the
  707. * max ram address by 13 to divide by 8K and then look at the
  708. * order of the result to choose between 0-7.
  709. */
  710. ret = get_order(ram >> 13);
  711. if (ret > TCE_TABLE_SIZE_8M)
  712. ret = TCE_TABLE_SIZE_8M;
  713. return ret;
  714. }
  715. void __init detect_calgary(void)
  716. {
  717. u32 val;
  718. int bus;
  719. void *tbl;
  720. int calgary_found = 0;
  721. int phb = -1;
  722. /*
  723. * if the user specified iommu=off or iommu=soft or we found
  724. * another HW IOMMU already, bail out.
  725. */
  726. if (swiotlb || no_iommu || iommu_detected)
  727. return;
  728. specified_table_size = determine_tce_table_size(end_pfn * PAGE_SIZE);
  729. for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) {
  730. int dev;
  731. tce_table_kva[bus] = NULL;
  732. bus_to_phb[bus] = -1;
  733. if (read_pci_config(bus, 0, 0, 0) != PCI_VENDOR_DEVICE_ID_CALGARY)
  734. continue;
  735. /*
  736. * There are 4 PHBs per Calgary chip. Set phb to which phb (0-3)
  737. * it is connected to releative to the clagary chip.
  738. */
  739. phb = (phb + 1) % PHBS_PER_CALGARY;
  740. if (test_bit(bus, translation_disabled)) {
  741. printk(KERN_INFO "Calgary: translation is disabled for "
  742. "PHB 0x%x\n", bus);
  743. /* skip this phb, don't allocate a tbl for it */
  744. continue;
  745. }
  746. /*
  747. * Scan the slots of the PCI bus to see if there is a device present.
  748. * The parent bus will be the zero-ith device, so start at 1.
  749. */
  750. for (dev = 1; dev < 8; dev++) {
  751. val = read_pci_config(bus, dev, 0, 0);
  752. if (val != 0xffffffff || translate_empty_slots) {
  753. tbl = alloc_tce_table();
  754. if (!tbl)
  755. goto cleanup;
  756. tce_table_kva[bus] = tbl;
  757. bus_to_phb[bus] = phb;
  758. calgary_found = 1;
  759. break;
  760. }
  761. }
  762. }
  763. if (calgary_found) {
  764. iommu_detected = 1;
  765. calgary_detected = 1;
  766. printk(KERN_INFO "PCI-DMA: Calgary IOMMU detected. "
  767. "TCE table spec is %d.\n", specified_table_size);
  768. }
  769. return;
  770. cleanup:
  771. for (--bus; bus >= 0; --bus)
  772. if (tce_table_kva[bus])
  773. free_tce_table(tce_table_kva[bus]);
  774. }
  775. int __init calgary_iommu_init(void)
  776. {
  777. int ret;
  778. if (no_iommu || swiotlb)
  779. return -ENODEV;
  780. if (!calgary_detected)
  781. return -ENODEV;
  782. /* ok, we're trying to use Calgary - let's roll */
  783. printk(KERN_INFO "PCI-DMA: Using Calgary IOMMU\n");
  784. ret = calgary_init();
  785. if (ret) {
  786. printk(KERN_ERR "PCI-DMA: Calgary init failed %d, "
  787. "falling back to no_iommu\n", ret);
  788. if (end_pfn > MAX_DMA32_PFN)
  789. printk(KERN_ERR "WARNING more than 4GB of memory, "
  790. "32bit PCI may malfunction.\n");
  791. return ret;
  792. }
  793. force_iommu = 1;
  794. dma_ops = &calgary_dma_ops;
  795. return 0;
  796. }
  797. static int __init calgary_parse_options(char *p)
  798. {
  799. unsigned int bridge;
  800. size_t len;
  801. char* endp;
  802. while (*p) {
  803. if (!strncmp(p, "64k", 3))
  804. specified_table_size = TCE_TABLE_SIZE_64K;
  805. else if (!strncmp(p, "128k", 4))
  806. specified_table_size = TCE_TABLE_SIZE_128K;
  807. else if (!strncmp(p, "256k", 4))
  808. specified_table_size = TCE_TABLE_SIZE_256K;
  809. else if (!strncmp(p, "512k", 4))
  810. specified_table_size = TCE_TABLE_SIZE_512K;
  811. else if (!strncmp(p, "1M", 2))
  812. specified_table_size = TCE_TABLE_SIZE_1M;
  813. else if (!strncmp(p, "2M", 2))
  814. specified_table_size = TCE_TABLE_SIZE_2M;
  815. else if (!strncmp(p, "4M", 2))
  816. specified_table_size = TCE_TABLE_SIZE_4M;
  817. else if (!strncmp(p, "8M", 2))
  818. specified_table_size = TCE_TABLE_SIZE_8M;
  819. len = strlen("translate_empty_slots");
  820. if (!strncmp(p, "translate_empty_slots", len))
  821. translate_empty_slots = 1;
  822. len = strlen("disable");
  823. if (!strncmp(p, "disable", len)) {
  824. p += len;
  825. if (*p == '=')
  826. ++p;
  827. if (*p == '\0')
  828. break;
  829. bridge = simple_strtol(p, &endp, 0);
  830. if (p == endp)
  831. break;
  832. if (bridge < MAX_PHB_BUS_NUM) {
  833. printk(KERN_INFO "Calgary: disabling "
  834. "translation for PHB 0x%x\n", bridge);
  835. set_bit(bridge, translation_disabled);
  836. }
  837. }
  838. p = strpbrk(p, ",");
  839. if (!p)
  840. break;
  841. p++; /* skip ',' */
  842. }
  843. return 1;
  844. }
  845. __setup("calgary=", calgary_parse_options);