pci-calgary.c 26 KB

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