pci-calgary.c 30 KB

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