pci-calgary.c 28 KB

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