pci-calgary.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. /*
  2. * Derived from arch/powerpc/kernel/iommu.c
  3. *
  4. * Copyright IBM Corporation, 2006-2007
  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_set_split_completion_timeout(void __iomem *bbar,
  629. unsigned char busnum, unsigned long timeout)
  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 |= (timeout << phb_shift);
  653. writeq(cpu_to_be64(val64), target);
  654. readq(target); /* flush */
  655. }
  656. static void __init calgary_handle_quirks(struct pci_dev* dev)
  657. {
  658. unsigned char busnum = dev->bus->number;
  659. struct iommu_table *tbl = dev->sysdata;
  660. /*
  661. * Give split completion a longer timeout on bus 1 for aic94xx
  662. * http://bugzilla.kernel.org/show_bug.cgi?id=7180
  663. */
  664. if (busnum == 1)
  665. calgary_set_split_completion_timeout(tbl->bbar, busnum,
  666. CCR_2SEC_TIMEOUT);
  667. }
  668. static void __init calgary_enable_translation(struct pci_dev *dev)
  669. {
  670. u32 val32;
  671. unsigned char busnum;
  672. void __iomem *target;
  673. void __iomem *bbar;
  674. struct iommu_table *tbl;
  675. busnum = dev->bus->number;
  676. tbl = dev->sysdata;
  677. bbar = tbl->bbar;
  678. /* enable TCE in PHB Config Register */
  679. target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
  680. val32 = be32_to_cpu(readl(target));
  681. val32 |= PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE;
  682. printk(KERN_INFO "Calgary: enabling translation on PHB %#x\n", busnum);
  683. printk(KERN_INFO "Calgary: errant DMAs will now be prevented on this "
  684. "bus.\n");
  685. writel(cpu_to_be32(val32), target);
  686. readl(target); /* flush */
  687. init_timer(&tbl->watchdog_timer);
  688. tbl->watchdog_timer.function = &calgary_watchdog;
  689. tbl->watchdog_timer.data = (unsigned long)dev;
  690. mod_timer(&tbl->watchdog_timer, jiffies);
  691. }
  692. static void __init calgary_disable_translation(struct pci_dev *dev)
  693. {
  694. u32 val32;
  695. unsigned char busnum;
  696. void __iomem *target;
  697. void __iomem *bbar;
  698. struct iommu_table *tbl;
  699. busnum = dev->bus->number;
  700. tbl = dev->sysdata;
  701. bbar = tbl->bbar;
  702. /* disable TCE in PHB Config Register */
  703. target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
  704. val32 = be32_to_cpu(readl(target));
  705. val32 &= ~(PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE);
  706. printk(KERN_INFO "Calgary: disabling translation on PHB %#x!\n", busnum);
  707. writel(cpu_to_be32(val32), target);
  708. readl(target); /* flush */
  709. del_timer_sync(&tbl->watchdog_timer);
  710. }
  711. static void __init calgary_init_one_nontraslated(struct pci_dev *dev)
  712. {
  713. pci_dev_get(dev);
  714. dev->sysdata = NULL;
  715. dev->bus->self = dev;
  716. }
  717. static int __init calgary_init_one(struct pci_dev *dev)
  718. {
  719. void __iomem *bbar;
  720. int ret;
  721. BUG_ON(dev->bus->number >= MAX_PHB_BUS_NUM);
  722. bbar = busno_to_bbar(dev->bus->number);
  723. ret = calgary_setup_tar(dev, bbar);
  724. if (ret)
  725. goto done;
  726. pci_dev_get(dev);
  727. dev->bus->self = dev;
  728. calgary_handle_quirks(dev);
  729. calgary_enable_translation(dev);
  730. return 0;
  731. done:
  732. return ret;
  733. }
  734. static int __init calgary_locate_bbars(void)
  735. {
  736. int ret;
  737. int rioidx, phb, bus;
  738. void __iomem *bbar;
  739. void __iomem *target;
  740. unsigned long offset;
  741. u8 start_bus, end_bus;
  742. u32 val;
  743. ret = -ENODATA;
  744. for (rioidx = 0; rioidx < rio_table_hdr->num_rio_dev; rioidx++) {
  745. struct rio_detail *rio = rio_devs[rioidx];
  746. if ((rio->type != COMPAT_CALGARY) && (rio->type != ALT_CALGARY))
  747. continue;
  748. /* map entire 1MB of Calgary config space */
  749. bbar = ioremap_nocache(rio->BBAR, 1024 * 1024);
  750. if (!bbar)
  751. goto error;
  752. for (phb = 0; phb < PHBS_PER_CALGARY; phb++) {
  753. offset = phb_debug_offsets[phb] | PHB_DEBUG_STUFF_OFFSET;
  754. target = calgary_reg(bbar, offset);
  755. val = be32_to_cpu(readl(target));
  756. start_bus = (u8)((val & 0x00FF0000) >> 16);
  757. end_bus = (u8)((val & 0x0000FF00) >> 8);
  758. for (bus = start_bus; bus <= end_bus; bus++) {
  759. bus_info[bus].bbar = bbar;
  760. bus_info[bus].phbid = phb;
  761. }
  762. }
  763. }
  764. return 0;
  765. error:
  766. /* scan bus_info and iounmap any bbars we previously ioremap'd */
  767. for (bus = 0; bus < ARRAY_SIZE(bus_info); bus++)
  768. if (bus_info[bus].bbar)
  769. iounmap(bus_info[bus].bbar);
  770. return ret;
  771. }
  772. static int __init calgary_init(void)
  773. {
  774. int ret;
  775. struct pci_dev *dev = NULL;
  776. ret = calgary_locate_bbars();
  777. if (ret)
  778. return ret;
  779. do {
  780. dev = pci_get_device(PCI_VENDOR_ID_IBM,
  781. PCI_DEVICE_ID_IBM_CALGARY,
  782. dev);
  783. if (!dev)
  784. break;
  785. if (!translate_phb(dev)) {
  786. calgary_init_one_nontraslated(dev);
  787. continue;
  788. }
  789. if (!bus_info[dev->bus->number].tce_space && !translate_empty_slots)
  790. continue;
  791. ret = calgary_init_one(dev);
  792. if (ret)
  793. goto error;
  794. } while (1);
  795. return ret;
  796. error:
  797. do {
  798. dev = pci_get_device_reverse(PCI_VENDOR_ID_IBM,
  799. PCI_DEVICE_ID_IBM_CALGARY,
  800. dev);
  801. if (!dev)
  802. break;
  803. if (!translate_phb(dev)) {
  804. pci_dev_put(dev);
  805. continue;
  806. }
  807. if (!bus_info[dev->bus->number].tce_space && !translate_empty_slots)
  808. continue;
  809. calgary_disable_translation(dev);
  810. calgary_free_bus(dev);
  811. pci_dev_put(dev); /* Undo calgary_init_one()'s pci_dev_get() */
  812. } while (1);
  813. return ret;
  814. }
  815. static inline int __init determine_tce_table_size(u64 ram)
  816. {
  817. int ret;
  818. if (specified_table_size != TCE_TABLE_SIZE_UNSPECIFIED)
  819. return specified_table_size;
  820. /*
  821. * Table sizes are from 0 to 7 (TCE_TABLE_SIZE_64K to
  822. * TCE_TABLE_SIZE_8M). Table size 0 has 8K entries and each
  823. * larger table size has twice as many entries, so shift the
  824. * max ram address by 13 to divide by 8K and then look at the
  825. * order of the result to choose between 0-7.
  826. */
  827. ret = get_order(ram >> 13);
  828. if (ret > TCE_TABLE_SIZE_8M)
  829. ret = TCE_TABLE_SIZE_8M;
  830. return ret;
  831. }
  832. static int __init build_detail_arrays(void)
  833. {
  834. unsigned long ptr;
  835. int i, scal_detail_size, rio_detail_size;
  836. if (rio_table_hdr->num_scal_dev > MAX_NUMNODES){
  837. printk(KERN_WARNING
  838. "Calgary: MAX_NUMNODES too low! Defined as %d, "
  839. "but system has %d nodes.\n",
  840. MAX_NUMNODES, rio_table_hdr->num_scal_dev);
  841. return -ENODEV;
  842. }
  843. switch (rio_table_hdr->version){
  844. case 2:
  845. scal_detail_size = 11;
  846. rio_detail_size = 13;
  847. break;
  848. case 3:
  849. scal_detail_size = 12;
  850. rio_detail_size = 15;
  851. break;
  852. default:
  853. printk(KERN_WARNING
  854. "Calgary: Invalid Rio Grande Table Version: %d\n",
  855. rio_table_hdr->version);
  856. return -EPROTO;
  857. }
  858. ptr = ((unsigned long)rio_table_hdr) + 3;
  859. for (i = 0; i < rio_table_hdr->num_scal_dev;
  860. i++, ptr += scal_detail_size)
  861. scal_devs[i] = (struct scal_detail *)ptr;
  862. for (i = 0; i < rio_table_hdr->num_rio_dev;
  863. i++, ptr += rio_detail_size)
  864. rio_devs[i] = (struct rio_detail *)ptr;
  865. return 0;
  866. }
  867. void __init detect_calgary(void)
  868. {
  869. u32 val;
  870. int bus;
  871. void *tbl;
  872. int calgary_found = 0;
  873. unsigned long ptr;
  874. unsigned int offset, prev_offset;
  875. int ret;
  876. /*
  877. * if the user specified iommu=off or iommu=soft or we found
  878. * another HW IOMMU already, bail out.
  879. */
  880. if (swiotlb || no_iommu || iommu_detected)
  881. return;
  882. if (!use_calgary)
  883. return;
  884. if (!early_pci_allowed())
  885. return;
  886. printk(KERN_DEBUG "Calgary: detecting Calgary via BIOS EBDA area\n");
  887. ptr = (unsigned long)phys_to_virt(get_bios_ebda());
  888. rio_table_hdr = NULL;
  889. prev_offset = 0;
  890. offset = 0x180;
  891. /*
  892. * The next offset is stored in the 1st word.
  893. * Only parse up until the offset increases:
  894. */
  895. while (offset > prev_offset) {
  896. /* The block id is stored in the 2nd word */
  897. if (*((unsigned short *)(ptr + offset + 2)) == 0x4752){
  898. /* set the pointer past the offset & block id */
  899. rio_table_hdr = (struct rio_table_hdr *)(ptr + offset + 4);
  900. break;
  901. }
  902. prev_offset = offset;
  903. offset = *((unsigned short *)(ptr + offset));
  904. }
  905. if (!rio_table_hdr) {
  906. printk(KERN_DEBUG "Calgary: Unable to locate Rio Grande table "
  907. "in EBDA - bailing!\n");
  908. return;
  909. }
  910. ret = build_detail_arrays();
  911. if (ret) {
  912. printk(KERN_DEBUG "Calgary: build_detail_arrays ret %d\n", ret);
  913. return;
  914. }
  915. specified_table_size = determine_tce_table_size(end_pfn * PAGE_SIZE);
  916. for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) {
  917. int dev;
  918. struct calgary_bus_info *info = &bus_info[bus];
  919. if (read_pci_config(bus, 0, 0, 0) != PCI_VENDOR_DEVICE_ID_CALGARY)
  920. continue;
  921. if (info->translation_disabled)
  922. continue;
  923. /*
  924. * Scan the slots of the PCI bus to see if there is a device present.
  925. * The parent bus will be the zero-ith device, so start at 1.
  926. */
  927. for (dev = 1; dev < 8; dev++) {
  928. val = read_pci_config(bus, dev, 0, 0);
  929. if (val != 0xffffffff || translate_empty_slots) {
  930. tbl = alloc_tce_table();
  931. if (!tbl)
  932. goto cleanup;
  933. info->tce_space = tbl;
  934. calgary_found = 1;
  935. break;
  936. }
  937. }
  938. }
  939. printk(KERN_DEBUG "Calgary: finished detection, Calgary %s\n",
  940. calgary_found ? "found" : "not found");
  941. if (calgary_found) {
  942. iommu_detected = 1;
  943. calgary_detected = 1;
  944. printk(KERN_INFO "PCI-DMA: Calgary IOMMU detected.\n");
  945. printk(KERN_INFO "PCI-DMA: Calgary TCE table spec is %d, "
  946. "CONFIG_IOMMU_DEBUG is %s.\n", specified_table_size,
  947. debugging ? "enabled" : "disabled");
  948. }
  949. return;
  950. cleanup:
  951. for (--bus; bus >= 0; --bus) {
  952. struct calgary_bus_info *info = &bus_info[bus];
  953. if (info->tce_space)
  954. free_tce_table(info->tce_space);
  955. }
  956. }
  957. int __init calgary_iommu_init(void)
  958. {
  959. int ret;
  960. if (no_iommu || swiotlb)
  961. return -ENODEV;
  962. if (!calgary_detected)
  963. return -ENODEV;
  964. /* ok, we're trying to use Calgary - let's roll */
  965. printk(KERN_INFO "PCI-DMA: Using Calgary IOMMU\n");
  966. ret = calgary_init();
  967. if (ret) {
  968. printk(KERN_ERR "PCI-DMA: Calgary init failed %d, "
  969. "falling back to no_iommu\n", ret);
  970. if (end_pfn > MAX_DMA32_PFN)
  971. printk(KERN_ERR "WARNING more than 4GB of memory, "
  972. "32bit PCI may malfunction.\n");
  973. return ret;
  974. }
  975. force_iommu = 1;
  976. bad_dma_address = 0x0;
  977. dma_ops = &calgary_dma_ops;
  978. return 0;
  979. }
  980. static int __init calgary_parse_options(char *p)
  981. {
  982. unsigned int bridge;
  983. size_t len;
  984. char* endp;
  985. while (*p) {
  986. if (!strncmp(p, "64k", 3))
  987. specified_table_size = TCE_TABLE_SIZE_64K;
  988. else if (!strncmp(p, "128k", 4))
  989. specified_table_size = TCE_TABLE_SIZE_128K;
  990. else if (!strncmp(p, "256k", 4))
  991. specified_table_size = TCE_TABLE_SIZE_256K;
  992. else if (!strncmp(p, "512k", 4))
  993. specified_table_size = TCE_TABLE_SIZE_512K;
  994. else if (!strncmp(p, "1M", 2))
  995. specified_table_size = TCE_TABLE_SIZE_1M;
  996. else if (!strncmp(p, "2M", 2))
  997. specified_table_size = TCE_TABLE_SIZE_2M;
  998. else if (!strncmp(p, "4M", 2))
  999. specified_table_size = TCE_TABLE_SIZE_4M;
  1000. else if (!strncmp(p, "8M", 2))
  1001. specified_table_size = TCE_TABLE_SIZE_8M;
  1002. len = strlen("translate_empty_slots");
  1003. if (!strncmp(p, "translate_empty_slots", len))
  1004. translate_empty_slots = 1;
  1005. len = strlen("disable");
  1006. if (!strncmp(p, "disable", len)) {
  1007. p += len;
  1008. if (*p == '=')
  1009. ++p;
  1010. if (*p == '\0')
  1011. break;
  1012. bridge = simple_strtol(p, &endp, 0);
  1013. if (p == endp)
  1014. break;
  1015. if (bridge < MAX_PHB_BUS_NUM) {
  1016. printk(KERN_INFO "Calgary: disabling "
  1017. "translation for PHB %#x\n", bridge);
  1018. bus_info[bridge].translation_disabled = 1;
  1019. }
  1020. }
  1021. p = strpbrk(p, ",");
  1022. if (!p)
  1023. break;
  1024. p++; /* skip ',' */
  1025. }
  1026. return 1;
  1027. }
  1028. __setup("calgary=", calgary_parse_options);