pci-calgary.c 25 KB

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