pci-calgary.c 27 KB

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