pci-calgary.c 25 KB

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