pci-calgary.c 26 KB

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