pci_sun4v.c 26 KB

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