pci_sun4v.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  1. /* pci_sun4v.c: SUN4V specific PCI controller support.
  2. *
  3. * Copyright (C) 2006 David S. Miller (davem@davemloft.net)
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/types.h>
  7. #include <linux/pci.h>
  8. #include <linux/init.h>
  9. #include <linux/slab.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/percpu.h>
  12. #include <asm/pbm.h>
  13. #include <asm/iommu.h>
  14. #include <asm/irq.h>
  15. #include <asm/upa.h>
  16. #include <asm/pstate.h>
  17. #include <asm/oplib.h>
  18. #include <asm/hypervisor.h>
  19. #include <asm/prom.h>
  20. #include "pci_impl.h"
  21. #include "iommu_common.h"
  22. #include "pci_sun4v.h"
  23. #define PGLIST_NENTS (PAGE_SIZE / sizeof(u64))
  24. struct pci_iommu_batch {
  25. struct pci_dev *pdev; /* Device mapping is for. */
  26. unsigned long prot; /* IOMMU page protections */
  27. unsigned long entry; /* Index into IOTSB. */
  28. u64 *pglist; /* List of physical pages */
  29. unsigned long npages; /* Number of pages in list. */
  30. };
  31. static DEFINE_PER_CPU(struct pci_iommu_batch, pci_iommu_batch);
  32. /* Interrupts must be disabled. */
  33. static inline void pci_iommu_batch_start(struct pci_dev *pdev, unsigned long prot, unsigned long entry)
  34. {
  35. struct pci_iommu_batch *p = &__get_cpu_var(pci_iommu_batch);
  36. p->pdev = pdev;
  37. p->prot = prot;
  38. p->entry = entry;
  39. p->npages = 0;
  40. }
  41. /* Interrupts must be disabled. */
  42. static long pci_iommu_batch_flush(struct pci_iommu_batch *p)
  43. {
  44. struct pcidev_cookie *pcp = p->pdev->sysdata;
  45. unsigned long devhandle = pcp->pbm->devhandle;
  46. unsigned long prot = p->prot;
  47. unsigned long entry = p->entry;
  48. u64 *pglist = p->pglist;
  49. unsigned long npages = p->npages;
  50. while (npages != 0) {
  51. long num;
  52. num = pci_sun4v_iommu_map(devhandle, HV_PCI_TSBID(0, entry),
  53. npages, prot, __pa(pglist));
  54. if (unlikely(num < 0)) {
  55. if (printk_ratelimit())
  56. printk("pci_iommu_batch_flush: IOMMU map of "
  57. "[%08lx:%08lx:%lx:%lx:%lx] failed with "
  58. "status %ld\n",
  59. devhandle, HV_PCI_TSBID(0, entry),
  60. npages, prot, __pa(pglist), num);
  61. return -1;
  62. }
  63. entry += num;
  64. npages -= num;
  65. pglist += num;
  66. }
  67. p->entry = entry;
  68. p->npages = 0;
  69. return 0;
  70. }
  71. /* Interrupts must be disabled. */
  72. static inline long pci_iommu_batch_add(u64 phys_page)
  73. {
  74. struct pci_iommu_batch *p = &__get_cpu_var(pci_iommu_batch);
  75. BUG_ON(p->npages >= PGLIST_NENTS);
  76. p->pglist[p->npages++] = phys_page;
  77. if (p->npages == PGLIST_NENTS)
  78. return pci_iommu_batch_flush(p);
  79. return 0;
  80. }
  81. /* Interrupts must be disabled. */
  82. static inline long pci_iommu_batch_end(void)
  83. {
  84. struct pci_iommu_batch *p = &__get_cpu_var(pci_iommu_batch);
  85. BUG_ON(p->npages >= PGLIST_NENTS);
  86. return pci_iommu_batch_flush(p);
  87. }
  88. static long pci_arena_alloc(struct pci_iommu_arena *arena, unsigned long npages)
  89. {
  90. unsigned long n, i, start, end, limit;
  91. int pass;
  92. limit = arena->limit;
  93. start = arena->hint;
  94. pass = 0;
  95. again:
  96. n = find_next_zero_bit(arena->map, limit, start);
  97. end = n + npages;
  98. if (unlikely(end >= limit)) {
  99. if (likely(pass < 1)) {
  100. limit = start;
  101. start = 0;
  102. pass++;
  103. goto again;
  104. } else {
  105. /* Scanned the whole thing, give up. */
  106. return -1;
  107. }
  108. }
  109. for (i = n; i < end; i++) {
  110. if (test_bit(i, arena->map)) {
  111. start = i + 1;
  112. goto again;
  113. }
  114. }
  115. for (i = n; i < end; i++)
  116. __set_bit(i, arena->map);
  117. arena->hint = end;
  118. return n;
  119. }
  120. static void pci_arena_free(struct pci_iommu_arena *arena, unsigned long base, unsigned long npages)
  121. {
  122. unsigned long i;
  123. for (i = base; i < (base + npages); i++)
  124. __clear_bit(i, arena->map);
  125. }
  126. static void *pci_4v_alloc_consistent(struct pci_dev *pdev, size_t size, dma_addr_t *dma_addrp, gfp_t gfp)
  127. {
  128. struct pcidev_cookie *pcp;
  129. struct pci_iommu *iommu;
  130. unsigned long flags, order, first_page, npages, n;
  131. void *ret;
  132. long entry;
  133. size = IO_PAGE_ALIGN(size);
  134. order = get_order(size);
  135. if (unlikely(order >= MAX_ORDER))
  136. return NULL;
  137. npages = size >> IO_PAGE_SHIFT;
  138. first_page = __get_free_pages(gfp, order);
  139. if (unlikely(first_page == 0UL))
  140. return NULL;
  141. memset((char *)first_page, 0, PAGE_SIZE << order);
  142. pcp = pdev->sysdata;
  143. iommu = pcp->pbm->iommu;
  144. spin_lock_irqsave(&iommu->lock, flags);
  145. entry = pci_arena_alloc(&iommu->arena, npages);
  146. spin_unlock_irqrestore(&iommu->lock, flags);
  147. if (unlikely(entry < 0L))
  148. goto arena_alloc_fail;
  149. *dma_addrp = (iommu->page_table_map_base +
  150. (entry << IO_PAGE_SHIFT));
  151. ret = (void *) first_page;
  152. first_page = __pa(first_page);
  153. local_irq_save(flags);
  154. pci_iommu_batch_start(pdev,
  155. (HV_PCI_MAP_ATTR_READ |
  156. HV_PCI_MAP_ATTR_WRITE),
  157. entry);
  158. for (n = 0; n < npages; n++) {
  159. long err = pci_iommu_batch_add(first_page + (n * PAGE_SIZE));
  160. if (unlikely(err < 0L))
  161. goto iommu_map_fail;
  162. }
  163. if (unlikely(pci_iommu_batch_end() < 0L))
  164. goto iommu_map_fail;
  165. local_irq_restore(flags);
  166. return ret;
  167. iommu_map_fail:
  168. /* Interrupts are disabled. */
  169. spin_lock(&iommu->lock);
  170. pci_arena_free(&iommu->arena, entry, npages);
  171. spin_unlock_irqrestore(&iommu->lock, flags);
  172. arena_alloc_fail:
  173. free_pages(first_page, order);
  174. return NULL;
  175. }
  176. static void pci_4v_free_consistent(struct pci_dev *pdev, size_t size, void *cpu, dma_addr_t dvma)
  177. {
  178. struct pcidev_cookie *pcp;
  179. struct pci_iommu *iommu;
  180. unsigned long flags, order, npages, entry;
  181. u32 devhandle;
  182. npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
  183. pcp = pdev->sysdata;
  184. iommu = pcp->pbm->iommu;
  185. devhandle = pcp->pbm->devhandle;
  186. entry = ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  187. spin_lock_irqsave(&iommu->lock, flags);
  188. pci_arena_free(&iommu->arena, entry, npages);
  189. do {
  190. unsigned long num;
  191. num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
  192. npages);
  193. entry += num;
  194. npages -= num;
  195. } while (npages != 0);
  196. spin_unlock_irqrestore(&iommu->lock, flags);
  197. order = get_order(size);
  198. if (order < 10)
  199. free_pages((unsigned long)cpu, order);
  200. }
  201. static dma_addr_t pci_4v_map_single(struct pci_dev *pdev, void *ptr, size_t sz, int direction)
  202. {
  203. struct pcidev_cookie *pcp;
  204. struct pci_iommu *iommu;
  205. unsigned long flags, npages, oaddr;
  206. unsigned long i, base_paddr;
  207. u32 bus_addr, ret;
  208. unsigned long prot;
  209. long entry;
  210. pcp = pdev->sysdata;
  211. iommu = pcp->pbm->iommu;
  212. if (unlikely(direction == PCI_DMA_NONE))
  213. goto bad;
  214. oaddr = (unsigned long)ptr;
  215. npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
  216. npages >>= IO_PAGE_SHIFT;
  217. spin_lock_irqsave(&iommu->lock, flags);
  218. entry = pci_arena_alloc(&iommu->arena, npages);
  219. spin_unlock_irqrestore(&iommu->lock, flags);
  220. if (unlikely(entry < 0L))
  221. goto bad;
  222. bus_addr = (iommu->page_table_map_base +
  223. (entry << IO_PAGE_SHIFT));
  224. ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
  225. base_paddr = __pa(oaddr & IO_PAGE_MASK);
  226. prot = HV_PCI_MAP_ATTR_READ;
  227. if (direction != PCI_DMA_TODEVICE)
  228. prot |= HV_PCI_MAP_ATTR_WRITE;
  229. local_irq_save(flags);
  230. pci_iommu_batch_start(pdev, prot, entry);
  231. for (i = 0; i < npages; i++, base_paddr += IO_PAGE_SIZE) {
  232. long err = pci_iommu_batch_add(base_paddr);
  233. if (unlikely(err < 0L))
  234. goto iommu_map_fail;
  235. }
  236. if (unlikely(pci_iommu_batch_end() < 0L))
  237. goto iommu_map_fail;
  238. local_irq_restore(flags);
  239. return ret;
  240. bad:
  241. if (printk_ratelimit())
  242. WARN_ON(1);
  243. return PCI_DMA_ERROR_CODE;
  244. iommu_map_fail:
  245. /* Interrupts are disabled. */
  246. spin_lock(&iommu->lock);
  247. pci_arena_free(&iommu->arena, entry, npages);
  248. spin_unlock_irqrestore(&iommu->lock, flags);
  249. return PCI_DMA_ERROR_CODE;
  250. }
  251. static void pci_4v_unmap_single(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
  252. {
  253. struct pcidev_cookie *pcp;
  254. struct pci_iommu *iommu;
  255. unsigned long flags, npages;
  256. long entry;
  257. u32 devhandle;
  258. if (unlikely(direction == PCI_DMA_NONE)) {
  259. if (printk_ratelimit())
  260. WARN_ON(1);
  261. return;
  262. }
  263. pcp = pdev->sysdata;
  264. iommu = pcp->pbm->iommu;
  265. devhandle = pcp->pbm->devhandle;
  266. npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
  267. npages >>= IO_PAGE_SHIFT;
  268. bus_addr &= IO_PAGE_MASK;
  269. spin_lock_irqsave(&iommu->lock, flags);
  270. entry = (bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT;
  271. pci_arena_free(&iommu->arena, entry, npages);
  272. do {
  273. unsigned long num;
  274. num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
  275. npages);
  276. entry += num;
  277. npages -= num;
  278. } while (npages != 0);
  279. spin_unlock_irqrestore(&iommu->lock, flags);
  280. }
  281. #define SG_ENT_PHYS_ADDRESS(SG) \
  282. (__pa(page_address((SG)->page)) + (SG)->offset)
  283. static inline long fill_sg(long entry, struct pci_dev *pdev,
  284. struct scatterlist *sg,
  285. int nused, int nelems, unsigned long prot)
  286. {
  287. struct scatterlist *dma_sg = sg;
  288. struct scatterlist *sg_end = sg + nelems;
  289. unsigned long flags;
  290. int i;
  291. local_irq_save(flags);
  292. pci_iommu_batch_start(pdev, prot, entry);
  293. for (i = 0; i < nused; i++) {
  294. unsigned long pteval = ~0UL;
  295. u32 dma_npages;
  296. dma_npages = ((dma_sg->dma_address & (IO_PAGE_SIZE - 1UL)) +
  297. dma_sg->dma_length +
  298. ((IO_PAGE_SIZE - 1UL))) >> IO_PAGE_SHIFT;
  299. do {
  300. unsigned long offset;
  301. signed int len;
  302. /* If we are here, we know we have at least one
  303. * more page to map. So walk forward until we
  304. * hit a page crossing, and begin creating new
  305. * mappings from that spot.
  306. */
  307. for (;;) {
  308. unsigned long tmp;
  309. tmp = SG_ENT_PHYS_ADDRESS(sg);
  310. len = sg->length;
  311. if (((tmp ^ pteval) >> IO_PAGE_SHIFT) != 0UL) {
  312. pteval = tmp & IO_PAGE_MASK;
  313. offset = tmp & (IO_PAGE_SIZE - 1UL);
  314. break;
  315. }
  316. if (((tmp ^ (tmp + len - 1UL)) >> IO_PAGE_SHIFT) != 0UL) {
  317. pteval = (tmp + IO_PAGE_SIZE) & IO_PAGE_MASK;
  318. offset = 0UL;
  319. len -= (IO_PAGE_SIZE - (tmp & (IO_PAGE_SIZE - 1UL)));
  320. break;
  321. }
  322. sg++;
  323. }
  324. pteval = (pteval & IOPTE_PAGE);
  325. while (len > 0) {
  326. long err;
  327. err = pci_iommu_batch_add(pteval);
  328. if (unlikely(err < 0L))
  329. goto iommu_map_failed;
  330. pteval += IO_PAGE_SIZE;
  331. len -= (IO_PAGE_SIZE - offset);
  332. offset = 0;
  333. dma_npages--;
  334. }
  335. pteval = (pteval & IOPTE_PAGE) + len;
  336. sg++;
  337. /* Skip over any tail mappings we've fully mapped,
  338. * adjusting pteval along the way. Stop when we
  339. * detect a page crossing event.
  340. */
  341. while (sg < sg_end &&
  342. (pteval << (64 - IO_PAGE_SHIFT)) != 0UL &&
  343. (pteval == SG_ENT_PHYS_ADDRESS(sg)) &&
  344. ((pteval ^
  345. (SG_ENT_PHYS_ADDRESS(sg) + sg->length - 1UL)) >> IO_PAGE_SHIFT) == 0UL) {
  346. pteval += sg->length;
  347. sg++;
  348. }
  349. if ((pteval << (64 - IO_PAGE_SHIFT)) == 0UL)
  350. pteval = ~0UL;
  351. } while (dma_npages != 0);
  352. dma_sg++;
  353. }
  354. if (unlikely(pci_iommu_batch_end() < 0L))
  355. goto iommu_map_failed;
  356. local_irq_restore(flags);
  357. return 0;
  358. iommu_map_failed:
  359. local_irq_restore(flags);
  360. return -1L;
  361. }
  362. static int pci_4v_map_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
  363. {
  364. struct pcidev_cookie *pcp;
  365. struct pci_iommu *iommu;
  366. unsigned long flags, npages, prot;
  367. u32 dma_base;
  368. struct scatterlist *sgtmp;
  369. long entry, err;
  370. int used;
  371. /* Fast path single entry scatterlists. */
  372. if (nelems == 1) {
  373. sglist->dma_address =
  374. pci_4v_map_single(pdev,
  375. (page_address(sglist->page) + sglist->offset),
  376. sglist->length, direction);
  377. if (unlikely(sglist->dma_address == PCI_DMA_ERROR_CODE))
  378. return 0;
  379. sglist->dma_length = sglist->length;
  380. return 1;
  381. }
  382. pcp = pdev->sysdata;
  383. iommu = pcp->pbm->iommu;
  384. if (unlikely(direction == PCI_DMA_NONE))
  385. goto bad;
  386. /* Step 1: Prepare scatter list. */
  387. npages = prepare_sg(sglist, nelems);
  388. /* Step 2: Allocate a cluster and context, if necessary. */
  389. spin_lock_irqsave(&iommu->lock, flags);
  390. entry = pci_arena_alloc(&iommu->arena, npages);
  391. spin_unlock_irqrestore(&iommu->lock, flags);
  392. if (unlikely(entry < 0L))
  393. goto bad;
  394. dma_base = iommu->page_table_map_base +
  395. (entry << IO_PAGE_SHIFT);
  396. /* Step 3: Normalize DMA addresses. */
  397. used = nelems;
  398. sgtmp = sglist;
  399. while (used && sgtmp->dma_length) {
  400. sgtmp->dma_address += dma_base;
  401. sgtmp++;
  402. used--;
  403. }
  404. used = nelems - used;
  405. /* Step 4: Create the mappings. */
  406. prot = HV_PCI_MAP_ATTR_READ;
  407. if (direction != PCI_DMA_TODEVICE)
  408. prot |= HV_PCI_MAP_ATTR_WRITE;
  409. err = fill_sg(entry, pdev, sglist, used, nelems, prot);
  410. if (unlikely(err < 0L))
  411. goto iommu_map_failed;
  412. return used;
  413. bad:
  414. if (printk_ratelimit())
  415. WARN_ON(1);
  416. return 0;
  417. iommu_map_failed:
  418. spin_lock_irqsave(&iommu->lock, flags);
  419. pci_arena_free(&iommu->arena, entry, npages);
  420. spin_unlock_irqrestore(&iommu->lock, flags);
  421. return 0;
  422. }
  423. static void pci_4v_unmap_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
  424. {
  425. struct pcidev_cookie *pcp;
  426. struct pci_iommu *iommu;
  427. unsigned long flags, i, npages;
  428. long entry;
  429. u32 devhandle, bus_addr;
  430. if (unlikely(direction == PCI_DMA_NONE)) {
  431. if (printk_ratelimit())
  432. WARN_ON(1);
  433. }
  434. pcp = pdev->sysdata;
  435. iommu = pcp->pbm->iommu;
  436. devhandle = pcp->pbm->devhandle;
  437. bus_addr = sglist->dma_address & IO_PAGE_MASK;
  438. for (i = 1; i < nelems; i++)
  439. if (sglist[i].dma_length == 0)
  440. break;
  441. i--;
  442. npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length) -
  443. bus_addr) >> IO_PAGE_SHIFT;
  444. entry = ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  445. spin_lock_irqsave(&iommu->lock, flags);
  446. pci_arena_free(&iommu->arena, entry, npages);
  447. do {
  448. unsigned long num;
  449. num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
  450. npages);
  451. entry += num;
  452. npages -= num;
  453. } while (npages != 0);
  454. spin_unlock_irqrestore(&iommu->lock, flags);
  455. }
  456. static void pci_4v_dma_sync_single_for_cpu(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
  457. {
  458. /* Nothing to do... */
  459. }
  460. static void pci_4v_dma_sync_sg_for_cpu(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
  461. {
  462. /* Nothing to do... */
  463. }
  464. struct pci_iommu_ops pci_sun4v_iommu_ops = {
  465. .alloc_consistent = pci_4v_alloc_consistent,
  466. .free_consistent = pci_4v_free_consistent,
  467. .map_single = pci_4v_map_single,
  468. .unmap_single = pci_4v_unmap_single,
  469. .map_sg = pci_4v_map_sg,
  470. .unmap_sg = pci_4v_unmap_sg,
  471. .dma_sync_single_for_cpu = pci_4v_dma_sync_single_for_cpu,
  472. .dma_sync_sg_for_cpu = pci_4v_dma_sync_sg_for_cpu,
  473. };
  474. /* SUN4V PCI configuration space accessors. */
  475. struct pdev_entry {
  476. struct pdev_entry *next;
  477. u32 devhandle;
  478. unsigned int bus;
  479. unsigned int device;
  480. unsigned int func;
  481. };
  482. #define PDEV_HTAB_SIZE 16
  483. #define PDEV_HTAB_MASK (PDEV_HTAB_SIZE - 1)
  484. static struct pdev_entry *pdev_htab[PDEV_HTAB_SIZE];
  485. static inline unsigned int pdev_hashfn(u32 devhandle, unsigned int bus, unsigned int device, unsigned int func)
  486. {
  487. unsigned int val;
  488. val = (devhandle ^ (devhandle >> 4));
  489. val ^= bus;
  490. val ^= device;
  491. val ^= func;
  492. return val & PDEV_HTAB_MASK;
  493. }
  494. static int pdev_htab_add(u32 devhandle, unsigned int bus, unsigned int device, unsigned int func)
  495. {
  496. struct pdev_entry *p = kmalloc(sizeof(*p), GFP_KERNEL);
  497. struct pdev_entry **slot;
  498. if (!p)
  499. return -ENOMEM;
  500. slot = &pdev_htab[pdev_hashfn(devhandle, bus, device, func)];
  501. p->next = *slot;
  502. *slot = p;
  503. p->devhandle = devhandle;
  504. p->bus = bus;
  505. p->device = device;
  506. p->func = func;
  507. return 0;
  508. }
  509. /* Recursively descend into the OBP device tree, rooted at toplevel_node,
  510. * looking for a PCI device matching bus and devfn.
  511. */
  512. static int obp_find(struct device_node *toplevel_node, unsigned int bus, unsigned int devfn)
  513. {
  514. toplevel_node = toplevel_node->child;
  515. while (toplevel_node != NULL) {
  516. struct linux_prom_pci_registers *regs;
  517. struct property *prop;
  518. int ret;
  519. ret = obp_find(toplevel_node, bus, devfn);
  520. if (ret != 0)
  521. return ret;
  522. prop = of_find_property(toplevel_node, "reg", NULL);
  523. if (!prop)
  524. goto next_sibling;
  525. regs = prop->value;
  526. if (((regs->phys_hi >> 16) & 0xff) == bus &&
  527. ((regs->phys_hi >> 8) & 0xff) == devfn)
  528. break;
  529. next_sibling:
  530. toplevel_node = toplevel_node->sibling;
  531. }
  532. return toplevel_node != NULL;
  533. }
  534. static int pdev_htab_populate(struct pci_pbm_info *pbm)
  535. {
  536. u32 devhandle = pbm->devhandle;
  537. unsigned int bus;
  538. for (bus = pbm->pci_first_busno; bus <= pbm->pci_last_busno; bus++) {
  539. unsigned int devfn;
  540. for (devfn = 0; devfn < 256; devfn++) {
  541. unsigned int device = PCI_SLOT(devfn);
  542. unsigned int func = PCI_FUNC(devfn);
  543. if (obp_find(pbm->prom_node, bus, devfn)) {
  544. int err = pdev_htab_add(devhandle, bus,
  545. device, func);
  546. if (err)
  547. return err;
  548. }
  549. }
  550. }
  551. return 0;
  552. }
  553. static struct pdev_entry *pdev_find(u32 devhandle, unsigned int bus, unsigned int device, unsigned int func)
  554. {
  555. struct pdev_entry *p;
  556. p = pdev_htab[pdev_hashfn(devhandle, bus, device, func)];
  557. while (p) {
  558. if (p->devhandle == devhandle &&
  559. p->bus == bus &&
  560. p->device == device &&
  561. p->func == func)
  562. break;
  563. p = p->next;
  564. }
  565. return p;
  566. }
  567. static inline int pci_sun4v_out_of_range(struct pci_pbm_info *pbm, unsigned int bus, unsigned int device, unsigned int func)
  568. {
  569. if (bus < pbm->pci_first_busno ||
  570. bus > pbm->pci_last_busno)
  571. return 1;
  572. return pdev_find(pbm->devhandle, bus, device, func) == NULL;
  573. }
  574. static int pci_sun4v_read_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
  575. int where, int size, u32 *value)
  576. {
  577. struct pci_pbm_info *pbm = bus_dev->sysdata;
  578. u32 devhandle = pbm->devhandle;
  579. unsigned int bus = bus_dev->number;
  580. unsigned int device = PCI_SLOT(devfn);
  581. unsigned int func = PCI_FUNC(devfn);
  582. unsigned long ret;
  583. if (pci_sun4v_out_of_range(pbm, bus, device, func)) {
  584. ret = ~0UL;
  585. } else {
  586. ret = pci_sun4v_config_get(devhandle,
  587. HV_PCI_DEVICE_BUILD(bus, device, func),
  588. where, size);
  589. #if 0
  590. printk("rcfg: [%x:%x:%x:%d]=[%lx]\n",
  591. devhandle, HV_PCI_DEVICE_BUILD(bus, device, func),
  592. where, size, ret);
  593. #endif
  594. }
  595. switch (size) {
  596. case 1:
  597. *value = ret & 0xff;
  598. break;
  599. case 2:
  600. *value = ret & 0xffff;
  601. break;
  602. case 4:
  603. *value = ret & 0xffffffff;
  604. break;
  605. };
  606. return PCIBIOS_SUCCESSFUL;
  607. }
  608. static int pci_sun4v_write_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
  609. int where, int size, u32 value)
  610. {
  611. struct pci_pbm_info *pbm = bus_dev->sysdata;
  612. u32 devhandle = pbm->devhandle;
  613. unsigned int bus = bus_dev->number;
  614. unsigned int device = PCI_SLOT(devfn);
  615. unsigned int func = PCI_FUNC(devfn);
  616. unsigned long ret;
  617. if (pci_sun4v_out_of_range(pbm, bus, device, func)) {
  618. /* Do nothing. */
  619. } else {
  620. ret = pci_sun4v_config_put(devhandle,
  621. HV_PCI_DEVICE_BUILD(bus, device, func),
  622. where, size, value);
  623. #if 0
  624. printk("wcfg: [%x:%x:%x:%d] v[%x] == [%lx]\n",
  625. devhandle, HV_PCI_DEVICE_BUILD(bus, device, func),
  626. where, size, value, ret);
  627. #endif
  628. }
  629. return PCIBIOS_SUCCESSFUL;
  630. }
  631. static struct pci_ops pci_sun4v_ops = {
  632. .read = pci_sun4v_read_pci_cfg,
  633. .write = pci_sun4v_write_pci_cfg,
  634. };
  635. static void pbm_scan_bus(struct pci_controller_info *p,
  636. struct pci_pbm_info *pbm)
  637. {
  638. struct pcidev_cookie *cookie = kmalloc(sizeof(*cookie), GFP_KERNEL);
  639. if (!cookie) {
  640. prom_printf("%s: Critical allocation failure.\n", pbm->name);
  641. prom_halt();
  642. }
  643. /* All we care about is the PBM. */
  644. memset(cookie, 0, sizeof(*cookie));
  645. cookie->pbm = pbm;
  646. pbm->pci_bus = pci_scan_bus(pbm->pci_first_busno, p->pci_ops, pbm);
  647. #if 0
  648. pci_fixup_host_bridge_self(pbm->pci_bus);
  649. pbm->pci_bus->self->sysdata = cookie;
  650. #endif
  651. pci_fill_in_pbm_cookies(pbm->pci_bus, pbm,
  652. pbm->prom_node->node);
  653. pci_record_assignments(pbm, pbm->pci_bus);
  654. pci_assign_unassigned(pbm, pbm->pci_bus);
  655. pci_fixup_irq(pbm, pbm->pci_bus);
  656. pci_determine_66mhz_disposition(pbm, pbm->pci_bus);
  657. pci_setup_busmastering(pbm, pbm->pci_bus);
  658. }
  659. static void pci_sun4v_scan_bus(struct pci_controller_info *p)
  660. {
  661. struct property *prop;
  662. struct device_node *dp;
  663. if ((dp = p->pbm_A.prom_node) != NULL) {
  664. prop = of_find_property(dp, "66mhz-capable", NULL);
  665. p->pbm_A.is_66mhz_capable = (prop != NULL);
  666. pbm_scan_bus(p, &p->pbm_A);
  667. }
  668. if ((dp = p->pbm_B.prom_node) != NULL) {
  669. prop = of_find_property(dp, "66mhz-capable", NULL);
  670. p->pbm_B.is_66mhz_capable = (prop != NULL);
  671. pbm_scan_bus(p, &p->pbm_B);
  672. }
  673. /* XXX register error interrupt handlers XXX */
  674. }
  675. static unsigned int pci_sun4v_irq_build(struct pci_pbm_info *pbm,
  676. struct pci_dev *pdev,
  677. unsigned int devino)
  678. {
  679. u32 devhandle = pbm->devhandle;
  680. return sun4v_build_irq(devhandle, devino);
  681. }
  682. static void pci_sun4v_base_address_update(struct pci_dev *pdev, int resource)
  683. {
  684. struct pcidev_cookie *pcp = pdev->sysdata;
  685. struct pci_pbm_info *pbm = pcp->pbm;
  686. struct resource *res, *root;
  687. u32 reg;
  688. int where, size, is_64bit;
  689. res = &pdev->resource[resource];
  690. if (resource < 6) {
  691. where = PCI_BASE_ADDRESS_0 + (resource * 4);
  692. } else if (resource == PCI_ROM_RESOURCE) {
  693. where = pdev->rom_base_reg;
  694. } else {
  695. /* Somebody might have asked allocation of a non-standard resource */
  696. return;
  697. }
  698. /* XXX 64-bit MEM handling is not %100 correct... XXX */
  699. is_64bit = 0;
  700. if (res->flags & IORESOURCE_IO)
  701. root = &pbm->io_space;
  702. else {
  703. root = &pbm->mem_space;
  704. if ((res->flags & PCI_BASE_ADDRESS_MEM_TYPE_MASK)
  705. == PCI_BASE_ADDRESS_MEM_TYPE_64)
  706. is_64bit = 1;
  707. }
  708. size = res->end - res->start;
  709. pci_read_config_dword(pdev, where, &reg);
  710. reg = ((reg & size) |
  711. (((u32)(res->start - root->start)) & ~size));
  712. if (resource == PCI_ROM_RESOURCE) {
  713. reg |= PCI_ROM_ADDRESS_ENABLE;
  714. res->flags |= IORESOURCE_ROM_ENABLE;
  715. }
  716. pci_write_config_dword(pdev, where, reg);
  717. /* This knows that the upper 32-bits of the address
  718. * must be zero. Our PCI common layer enforces this.
  719. */
  720. if (is_64bit)
  721. pci_write_config_dword(pdev, where + 4, 0);
  722. }
  723. static void pci_sun4v_resource_adjust(struct pci_dev *pdev,
  724. struct resource *res,
  725. struct resource *root)
  726. {
  727. res->start += root->start;
  728. res->end += root->start;
  729. }
  730. /* Use ranges property to determine where PCI MEM, I/O, and Config
  731. * space are for this PCI bus module.
  732. */
  733. static void pci_sun4v_determine_mem_io_space(struct pci_pbm_info *pbm)
  734. {
  735. int i, saw_mem, saw_io;
  736. saw_mem = saw_io = 0;
  737. for (i = 0; i < pbm->num_pbm_ranges; i++) {
  738. struct linux_prom_pci_ranges *pr = &pbm->pbm_ranges[i];
  739. unsigned long a;
  740. int type;
  741. type = (pr->child_phys_hi >> 24) & 0x3;
  742. a = (((unsigned long)pr->parent_phys_hi << 32UL) |
  743. ((unsigned long)pr->parent_phys_lo << 0UL));
  744. switch (type) {
  745. case 1:
  746. /* 16-bit IO space, 16MB */
  747. pbm->io_space.start = a;
  748. pbm->io_space.end = a + ((16UL*1024UL*1024UL) - 1UL);
  749. pbm->io_space.flags = IORESOURCE_IO;
  750. saw_io = 1;
  751. break;
  752. case 2:
  753. /* 32-bit MEM space, 2GB */
  754. pbm->mem_space.start = a;
  755. pbm->mem_space.end = a + (0x80000000UL - 1UL);
  756. pbm->mem_space.flags = IORESOURCE_MEM;
  757. saw_mem = 1;
  758. break;
  759. case 3:
  760. /* XXX 64-bit MEM handling XXX */
  761. default:
  762. break;
  763. };
  764. }
  765. if (!saw_io || !saw_mem) {
  766. prom_printf("%s: Fatal error, missing %s PBM range.\n",
  767. pbm->name,
  768. (!saw_io ? "IO" : "MEM"));
  769. prom_halt();
  770. }
  771. printk("%s: PCI IO[%lx] MEM[%lx]\n",
  772. pbm->name,
  773. pbm->io_space.start,
  774. pbm->mem_space.start);
  775. }
  776. static void pbm_register_toplevel_resources(struct pci_controller_info *p,
  777. struct pci_pbm_info *pbm)
  778. {
  779. pbm->io_space.name = pbm->mem_space.name = pbm->name;
  780. request_resource(&ioport_resource, &pbm->io_space);
  781. request_resource(&iomem_resource, &pbm->mem_space);
  782. pci_register_legacy_regions(&pbm->io_space,
  783. &pbm->mem_space);
  784. }
  785. static unsigned long probe_existing_entries(struct pci_pbm_info *pbm,
  786. struct pci_iommu *iommu)
  787. {
  788. struct pci_iommu_arena *arena = &iommu->arena;
  789. unsigned long i, cnt = 0;
  790. u32 devhandle;
  791. devhandle = pbm->devhandle;
  792. for (i = 0; i < arena->limit; i++) {
  793. unsigned long ret, io_attrs, ra;
  794. ret = pci_sun4v_iommu_getmap(devhandle,
  795. HV_PCI_TSBID(0, i),
  796. &io_attrs, &ra);
  797. if (ret == HV_EOK) {
  798. if (page_in_phys_avail(ra)) {
  799. pci_sun4v_iommu_demap(devhandle,
  800. HV_PCI_TSBID(0, i), 1);
  801. } else {
  802. cnt++;
  803. __set_bit(i, arena->map);
  804. }
  805. }
  806. }
  807. return cnt;
  808. }
  809. static void pci_sun4v_iommu_init(struct pci_pbm_info *pbm)
  810. {
  811. struct pci_iommu *iommu = pbm->iommu;
  812. struct property *prop;
  813. unsigned long num_tsb_entries, sz;
  814. u32 vdma[2], dma_mask, dma_offset;
  815. int tsbsize;
  816. prop = of_find_property(pbm->prom_node, "virtual-dma", NULL);
  817. if (prop) {
  818. u32 *val = prop->value;
  819. vdma[0] = val[0];
  820. vdma[1] = val[1];
  821. } else {
  822. /* No property, use default values. */
  823. vdma[0] = 0x80000000;
  824. vdma[1] = 0x80000000;
  825. }
  826. dma_mask = vdma[0];
  827. switch (vdma[1]) {
  828. case 0x20000000:
  829. dma_mask |= 0x1fffffff;
  830. tsbsize = 64;
  831. break;
  832. case 0x40000000:
  833. dma_mask |= 0x3fffffff;
  834. tsbsize = 128;
  835. break;
  836. case 0x80000000:
  837. dma_mask |= 0x7fffffff;
  838. tsbsize = 256;
  839. break;
  840. default:
  841. prom_printf("PCI-SUN4V: strange virtual-dma size.\n");
  842. prom_halt();
  843. };
  844. tsbsize *= (8 * 1024);
  845. num_tsb_entries = tsbsize / sizeof(iopte_t);
  846. dma_offset = vdma[0];
  847. /* Setup initial software IOMMU state. */
  848. spin_lock_init(&iommu->lock);
  849. iommu->ctx_lowest_free = 1;
  850. iommu->page_table_map_base = dma_offset;
  851. iommu->dma_addr_mask = dma_mask;
  852. /* Allocate and initialize the free area map. */
  853. sz = num_tsb_entries / 8;
  854. sz = (sz + 7UL) & ~7UL;
  855. iommu->arena.map = kmalloc(sz, GFP_KERNEL);
  856. if (!iommu->arena.map) {
  857. prom_printf("PCI_IOMMU: Error, kmalloc(arena.map) failed.\n");
  858. prom_halt();
  859. }
  860. memset(iommu->arena.map, 0, sz);
  861. iommu->arena.limit = num_tsb_entries;
  862. sz = probe_existing_entries(pbm, iommu);
  863. if (sz)
  864. printk("%s: Imported %lu TSB entries from OBP\n",
  865. pbm->name, sz);
  866. }
  867. static void pci_sun4v_get_bus_range(struct pci_pbm_info *pbm)
  868. {
  869. struct property *prop;
  870. unsigned int *busrange;
  871. prop = of_find_property(pbm->prom_node, "bus-range", NULL);
  872. busrange = prop->value;
  873. pbm->pci_first_busno = busrange[0];
  874. pbm->pci_last_busno = busrange[1];
  875. }
  876. static void pci_sun4v_pbm_init(struct pci_controller_info *p, struct device_node *dp, u32 devhandle)
  877. {
  878. struct pci_pbm_info *pbm;
  879. struct property *prop;
  880. int len, i;
  881. if (devhandle & 0x40)
  882. pbm = &p->pbm_B;
  883. else
  884. pbm = &p->pbm_A;
  885. pbm->parent = p;
  886. pbm->prom_node = dp;
  887. pbm->pci_first_slot = 1;
  888. pbm->devhandle = devhandle;
  889. pbm->name = dp->full_name;
  890. printk("%s: SUN4V PCI Bus Module\n", pbm->name);
  891. prop = of_find_property(dp, "ranges", &len);
  892. pbm->pbm_ranges = prop->value;
  893. pbm->num_pbm_ranges =
  894. (len / sizeof(struct linux_prom_pci_ranges));
  895. /* Mask out the top 8 bits of the ranges, leaving the real
  896. * physical address.
  897. */
  898. for (i = 0; i < pbm->num_pbm_ranges; i++)
  899. pbm->pbm_ranges[i].parent_phys_hi &= 0x0fffffff;
  900. pci_sun4v_determine_mem_io_space(pbm);
  901. pbm_register_toplevel_resources(p, pbm);
  902. prop = of_find_property(dp, "interrupt-map", &len);
  903. pbm->pbm_intmap = prop->value;
  904. pbm->num_pbm_intmap =
  905. (len / sizeof(struct linux_prom_pci_intmap));
  906. prop = of_find_property(dp, "interrupt-map-mask", NULL);
  907. pbm->pbm_intmask = prop->value;
  908. pci_sun4v_get_bus_range(pbm);
  909. pci_sun4v_iommu_init(pbm);
  910. pdev_htab_populate(pbm);
  911. }
  912. void sun4v_pci_init(struct device_node *dp, char *model_name)
  913. {
  914. struct pci_controller_info *p;
  915. struct pci_iommu *iommu;
  916. struct property *prop;
  917. struct linux_prom64_registers *regs;
  918. u32 devhandle;
  919. int i;
  920. prop = of_find_property(dp, "reg", NULL);
  921. regs = prop->value;
  922. devhandle = (regs->phys_addr >> 32UL) & 0x0fffffff;
  923. for (p = pci_controller_root; p; p = p->next) {
  924. struct pci_pbm_info *pbm;
  925. if (p->pbm_A.prom_node && p->pbm_B.prom_node)
  926. continue;
  927. pbm = (p->pbm_A.prom_node ?
  928. &p->pbm_A :
  929. &p->pbm_B);
  930. if (pbm->devhandle == (devhandle ^ 0x40)) {
  931. pci_sun4v_pbm_init(p, dp, devhandle);
  932. return;
  933. }
  934. }
  935. for_each_possible_cpu(i) {
  936. unsigned long page = get_zeroed_page(GFP_ATOMIC);
  937. if (!page)
  938. goto fatal_memory_error;
  939. per_cpu(pci_iommu_batch, i).pglist = (u64 *) page;
  940. }
  941. p = kmalloc(sizeof(struct pci_controller_info), GFP_ATOMIC);
  942. if (!p)
  943. goto fatal_memory_error;
  944. memset(p, 0, sizeof(*p));
  945. iommu = kmalloc(sizeof(struct pci_iommu), GFP_ATOMIC);
  946. if (!iommu)
  947. goto fatal_memory_error;
  948. memset(iommu, 0, sizeof(*iommu));
  949. p->pbm_A.iommu = iommu;
  950. iommu = kmalloc(sizeof(struct pci_iommu), GFP_ATOMIC);
  951. if (!iommu)
  952. goto fatal_memory_error;
  953. memset(iommu, 0, sizeof(*iommu));
  954. p->pbm_B.iommu = iommu;
  955. p->next = pci_controller_root;
  956. pci_controller_root = p;
  957. p->index = pci_num_controllers++;
  958. p->pbms_same_domain = 0;
  959. p->scan_bus = pci_sun4v_scan_bus;
  960. p->irq_build = pci_sun4v_irq_build;
  961. p->base_address_update = pci_sun4v_base_address_update;
  962. p->resource_adjust = pci_sun4v_resource_adjust;
  963. p->pci_ops = &pci_sun4v_ops;
  964. /* Like PSYCHO and SCHIZO we have a 2GB aligned area
  965. * for memory space.
  966. */
  967. pci_memspace_mask = 0x7fffffffUL;
  968. pci_sun4v_pbm_init(p, dp, devhandle);
  969. return;
  970. fatal_memory_error:
  971. prom_printf("SUN4V_PCI: Fatal memory allocation error.\n");
  972. prom_halt();
  973. }