pci-calgary.c 30 KB

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