pci-calgary.c 30 KB

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