pci-calgary.c 27 KB

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