pci-calgary.c 30 KB

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