pci-calgary.c 31 KB

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