pci_sun4v.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  1. /* pci_sun4v.c: SUN4V specific PCI controller support.
  2. *
  3. * Copyright (C) 2006, 2007 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 <linux/irq.h>
  13. #include <linux/msi.h>
  14. #include <asm/pbm.h>
  15. #include <asm/iommu.h>
  16. #include <asm/irq.h>
  17. #include <asm/upa.h>
  18. #include <asm/pstate.h>
  19. #include <asm/oplib.h>
  20. #include <asm/hypervisor.h>
  21. #include <asm/prom.h>
  22. #include "pci_impl.h"
  23. #include "iommu_common.h"
  24. #include "pci_sun4v.h"
  25. #define PGLIST_NENTS (PAGE_SIZE / sizeof(u64))
  26. struct pci_iommu_batch {
  27. struct pci_dev *pdev; /* Device mapping is for. */
  28. unsigned long prot; /* IOMMU page protections */
  29. unsigned long entry; /* Index into IOTSB. */
  30. u64 *pglist; /* List of physical pages */
  31. unsigned long npages; /* Number of pages in list. */
  32. };
  33. static DEFINE_PER_CPU(struct pci_iommu_batch, pci_iommu_batch);
  34. /* Interrupts must be disabled. */
  35. static inline void pci_iommu_batch_start(struct pci_dev *pdev, unsigned long prot, unsigned long entry)
  36. {
  37. struct pci_iommu_batch *p = &__get_cpu_var(pci_iommu_batch);
  38. p->pdev = pdev;
  39. p->prot = prot;
  40. p->entry = entry;
  41. p->npages = 0;
  42. }
  43. /* Interrupts must be disabled. */
  44. static long pci_iommu_batch_flush(struct pci_iommu_batch *p)
  45. {
  46. struct pci_pbm_info *pbm = p->pdev->dev.archdata.host_controller;
  47. unsigned long devhandle = pbm->devhandle;
  48. unsigned long prot = p->prot;
  49. unsigned long entry = p->entry;
  50. u64 *pglist = p->pglist;
  51. unsigned long npages = p->npages;
  52. while (npages != 0) {
  53. long num;
  54. num = pci_sun4v_iommu_map(devhandle, HV_PCI_TSBID(0, entry),
  55. npages, prot, __pa(pglist));
  56. if (unlikely(num < 0)) {
  57. if (printk_ratelimit())
  58. printk("pci_iommu_batch_flush: IOMMU map of "
  59. "[%08lx:%08lx:%lx:%lx:%lx] failed with "
  60. "status %ld\n",
  61. devhandle, HV_PCI_TSBID(0, entry),
  62. npages, prot, __pa(pglist), num);
  63. return -1;
  64. }
  65. entry += num;
  66. npages -= num;
  67. pglist += num;
  68. }
  69. p->entry = entry;
  70. p->npages = 0;
  71. return 0;
  72. }
  73. /* Interrupts must be disabled. */
  74. static inline long pci_iommu_batch_add(u64 phys_page)
  75. {
  76. struct pci_iommu_batch *p = &__get_cpu_var(pci_iommu_batch);
  77. BUG_ON(p->npages >= PGLIST_NENTS);
  78. p->pglist[p->npages++] = phys_page;
  79. if (p->npages == PGLIST_NENTS)
  80. return pci_iommu_batch_flush(p);
  81. return 0;
  82. }
  83. /* Interrupts must be disabled. */
  84. static inline long pci_iommu_batch_end(void)
  85. {
  86. struct pci_iommu_batch *p = &__get_cpu_var(pci_iommu_batch);
  87. BUG_ON(p->npages >= PGLIST_NENTS);
  88. return pci_iommu_batch_flush(p);
  89. }
  90. static long pci_arena_alloc(struct iommu_arena *arena, unsigned long npages)
  91. {
  92. unsigned long n, i, start, end, limit;
  93. int pass;
  94. limit = arena->limit;
  95. start = arena->hint;
  96. pass = 0;
  97. again:
  98. n = find_next_zero_bit(arena->map, limit, start);
  99. end = n + npages;
  100. if (unlikely(end >= limit)) {
  101. if (likely(pass < 1)) {
  102. limit = start;
  103. start = 0;
  104. pass++;
  105. goto again;
  106. } else {
  107. /* Scanned the whole thing, give up. */
  108. return -1;
  109. }
  110. }
  111. for (i = n; i < end; i++) {
  112. if (test_bit(i, arena->map)) {
  113. start = i + 1;
  114. goto again;
  115. }
  116. }
  117. for (i = n; i < end; i++)
  118. __set_bit(i, arena->map);
  119. arena->hint = end;
  120. return n;
  121. }
  122. static void pci_arena_free(struct iommu_arena *arena, unsigned long base, unsigned long npages)
  123. {
  124. unsigned long i;
  125. for (i = base; i < (base + npages); i++)
  126. __clear_bit(i, arena->map);
  127. }
  128. static void *pci_4v_alloc_consistent(struct pci_dev *pdev, size_t size, dma_addr_t *dma_addrp, gfp_t gfp)
  129. {
  130. struct pci_iommu *iommu;
  131. unsigned long flags, order, first_page, npages, n;
  132. void *ret;
  133. long entry;
  134. size = IO_PAGE_ALIGN(size);
  135. order = get_order(size);
  136. if (unlikely(order >= MAX_ORDER))
  137. return NULL;
  138. npages = size >> IO_PAGE_SHIFT;
  139. first_page = __get_free_pages(gfp, order);
  140. if (unlikely(first_page == 0UL))
  141. return NULL;
  142. memset((char *)first_page, 0, PAGE_SIZE << order);
  143. iommu = pdev->dev.archdata.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 pci_pbm_info *pbm;
  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. iommu = pdev->dev.archdata.iommu;
  184. pbm = pdev->dev.archdata.host_controller;
  185. devhandle = 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 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. iommu = pdev->dev.archdata.iommu;
  210. if (unlikely(direction == PCI_DMA_NONE))
  211. goto bad;
  212. oaddr = (unsigned long)ptr;
  213. npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
  214. npages >>= IO_PAGE_SHIFT;
  215. spin_lock_irqsave(&iommu->lock, flags);
  216. entry = pci_arena_alloc(&iommu->arena, npages);
  217. spin_unlock_irqrestore(&iommu->lock, flags);
  218. if (unlikely(entry < 0L))
  219. goto bad;
  220. bus_addr = (iommu->page_table_map_base +
  221. (entry << IO_PAGE_SHIFT));
  222. ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
  223. base_paddr = __pa(oaddr & IO_PAGE_MASK);
  224. prot = HV_PCI_MAP_ATTR_READ;
  225. if (direction != PCI_DMA_TODEVICE)
  226. prot |= HV_PCI_MAP_ATTR_WRITE;
  227. local_irq_save(flags);
  228. pci_iommu_batch_start(pdev, prot, entry);
  229. for (i = 0; i < npages; i++, base_paddr += IO_PAGE_SIZE) {
  230. long err = pci_iommu_batch_add(base_paddr);
  231. if (unlikely(err < 0L))
  232. goto iommu_map_fail;
  233. }
  234. if (unlikely(pci_iommu_batch_end() < 0L))
  235. goto iommu_map_fail;
  236. local_irq_restore(flags);
  237. return ret;
  238. bad:
  239. if (printk_ratelimit())
  240. WARN_ON(1);
  241. return PCI_DMA_ERROR_CODE;
  242. iommu_map_fail:
  243. /* Interrupts are disabled. */
  244. spin_lock(&iommu->lock);
  245. pci_arena_free(&iommu->arena, entry, npages);
  246. spin_unlock_irqrestore(&iommu->lock, flags);
  247. return PCI_DMA_ERROR_CODE;
  248. }
  249. static void pci_4v_unmap_single(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
  250. {
  251. struct pci_pbm_info *pbm;
  252. struct pci_iommu *iommu;
  253. unsigned long flags, npages;
  254. long entry;
  255. u32 devhandle;
  256. if (unlikely(direction == PCI_DMA_NONE)) {
  257. if (printk_ratelimit())
  258. WARN_ON(1);
  259. return;
  260. }
  261. iommu = pdev->dev.archdata.iommu;
  262. pbm = pdev->dev.archdata.host_controller;
  263. devhandle = pbm->devhandle;
  264. npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
  265. npages >>= IO_PAGE_SHIFT;
  266. bus_addr &= IO_PAGE_MASK;
  267. spin_lock_irqsave(&iommu->lock, flags);
  268. entry = (bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT;
  269. pci_arena_free(&iommu->arena, entry, npages);
  270. do {
  271. unsigned long num;
  272. num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
  273. npages);
  274. entry += num;
  275. npages -= num;
  276. } while (npages != 0);
  277. spin_unlock_irqrestore(&iommu->lock, flags);
  278. }
  279. #define SG_ENT_PHYS_ADDRESS(SG) \
  280. (__pa(page_address((SG)->page)) + (SG)->offset)
  281. static inline long fill_sg(long entry, struct pci_dev *pdev,
  282. struct scatterlist *sg,
  283. int nused, int nelems, unsigned long prot)
  284. {
  285. struct scatterlist *dma_sg = sg;
  286. struct scatterlist *sg_end = sg + nelems;
  287. unsigned long flags;
  288. int i;
  289. local_irq_save(flags);
  290. pci_iommu_batch_start(pdev, prot, entry);
  291. for (i = 0; i < nused; i++) {
  292. unsigned long pteval = ~0UL;
  293. u32 dma_npages;
  294. dma_npages = ((dma_sg->dma_address & (IO_PAGE_SIZE - 1UL)) +
  295. dma_sg->dma_length +
  296. ((IO_PAGE_SIZE - 1UL))) >> IO_PAGE_SHIFT;
  297. do {
  298. unsigned long offset;
  299. signed int len;
  300. /* If we are here, we know we have at least one
  301. * more page to map. So walk forward until we
  302. * hit a page crossing, and begin creating new
  303. * mappings from that spot.
  304. */
  305. for (;;) {
  306. unsigned long tmp;
  307. tmp = SG_ENT_PHYS_ADDRESS(sg);
  308. len = sg->length;
  309. if (((tmp ^ pteval) >> IO_PAGE_SHIFT) != 0UL) {
  310. pteval = tmp & IO_PAGE_MASK;
  311. offset = tmp & (IO_PAGE_SIZE - 1UL);
  312. break;
  313. }
  314. if (((tmp ^ (tmp + len - 1UL)) >> IO_PAGE_SHIFT) != 0UL) {
  315. pteval = (tmp + IO_PAGE_SIZE) & IO_PAGE_MASK;
  316. offset = 0UL;
  317. len -= (IO_PAGE_SIZE - (tmp & (IO_PAGE_SIZE - 1UL)));
  318. break;
  319. }
  320. sg++;
  321. }
  322. pteval = (pteval & IOPTE_PAGE);
  323. while (len > 0) {
  324. long err;
  325. err = pci_iommu_batch_add(pteval);
  326. if (unlikely(err < 0L))
  327. goto iommu_map_failed;
  328. pteval += IO_PAGE_SIZE;
  329. len -= (IO_PAGE_SIZE - offset);
  330. offset = 0;
  331. dma_npages--;
  332. }
  333. pteval = (pteval & IOPTE_PAGE) + len;
  334. sg++;
  335. /* Skip over any tail mappings we've fully mapped,
  336. * adjusting pteval along the way. Stop when we
  337. * detect a page crossing event.
  338. */
  339. while (sg < sg_end &&
  340. (pteval << (64 - IO_PAGE_SHIFT)) != 0UL &&
  341. (pteval == SG_ENT_PHYS_ADDRESS(sg)) &&
  342. ((pteval ^
  343. (SG_ENT_PHYS_ADDRESS(sg) + sg->length - 1UL)) >> IO_PAGE_SHIFT) == 0UL) {
  344. pteval += sg->length;
  345. sg++;
  346. }
  347. if ((pteval << (64 - IO_PAGE_SHIFT)) == 0UL)
  348. pteval = ~0UL;
  349. } while (dma_npages != 0);
  350. dma_sg++;
  351. }
  352. if (unlikely(pci_iommu_batch_end() < 0L))
  353. goto iommu_map_failed;
  354. local_irq_restore(flags);
  355. return 0;
  356. iommu_map_failed:
  357. local_irq_restore(flags);
  358. return -1L;
  359. }
  360. static int pci_4v_map_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
  361. {
  362. struct pci_iommu *iommu;
  363. unsigned long flags, npages, prot;
  364. u32 dma_base;
  365. struct scatterlist *sgtmp;
  366. long entry, err;
  367. int used;
  368. /* Fast path single entry scatterlists. */
  369. if (nelems == 1) {
  370. sglist->dma_address =
  371. pci_4v_map_single(pdev,
  372. (page_address(sglist->page) + sglist->offset),
  373. sglist->length, direction);
  374. if (unlikely(sglist->dma_address == PCI_DMA_ERROR_CODE))
  375. return 0;
  376. sglist->dma_length = sglist->length;
  377. return 1;
  378. }
  379. iommu = pdev->dev.archdata.iommu;
  380. if (unlikely(direction == PCI_DMA_NONE))
  381. goto bad;
  382. /* Step 1: Prepare scatter list. */
  383. npages = prepare_sg(sglist, nelems);
  384. /* Step 2: Allocate a cluster and context, if necessary. */
  385. spin_lock_irqsave(&iommu->lock, flags);
  386. entry = pci_arena_alloc(&iommu->arena, npages);
  387. spin_unlock_irqrestore(&iommu->lock, flags);
  388. if (unlikely(entry < 0L))
  389. goto bad;
  390. dma_base = iommu->page_table_map_base +
  391. (entry << IO_PAGE_SHIFT);
  392. /* Step 3: Normalize DMA addresses. */
  393. used = nelems;
  394. sgtmp = sglist;
  395. while (used && sgtmp->dma_length) {
  396. sgtmp->dma_address += dma_base;
  397. sgtmp++;
  398. used--;
  399. }
  400. used = nelems - used;
  401. /* Step 4: Create the mappings. */
  402. prot = HV_PCI_MAP_ATTR_READ;
  403. if (direction != PCI_DMA_TODEVICE)
  404. prot |= HV_PCI_MAP_ATTR_WRITE;
  405. err = fill_sg(entry, pdev, sglist, used, nelems, prot);
  406. if (unlikely(err < 0L))
  407. goto iommu_map_failed;
  408. return used;
  409. bad:
  410. if (printk_ratelimit())
  411. WARN_ON(1);
  412. return 0;
  413. iommu_map_failed:
  414. spin_lock_irqsave(&iommu->lock, flags);
  415. pci_arena_free(&iommu->arena, entry, npages);
  416. spin_unlock_irqrestore(&iommu->lock, flags);
  417. return 0;
  418. }
  419. static void pci_4v_unmap_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
  420. {
  421. struct pci_pbm_info *pbm;
  422. struct pci_iommu *iommu;
  423. unsigned long flags, i, npages;
  424. long entry;
  425. u32 devhandle, bus_addr;
  426. if (unlikely(direction == PCI_DMA_NONE)) {
  427. if (printk_ratelimit())
  428. WARN_ON(1);
  429. }
  430. iommu = pdev->dev.archdata.iommu;
  431. pbm = pdev->dev.archdata.host_controller;
  432. devhandle = pbm->devhandle;
  433. bus_addr = sglist->dma_address & IO_PAGE_MASK;
  434. for (i = 1; i < nelems; i++)
  435. if (sglist[i].dma_length == 0)
  436. break;
  437. i--;
  438. npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length) -
  439. bus_addr) >> IO_PAGE_SHIFT;
  440. entry = ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  441. spin_lock_irqsave(&iommu->lock, flags);
  442. pci_arena_free(&iommu->arena, entry, npages);
  443. do {
  444. unsigned long num;
  445. num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
  446. npages);
  447. entry += num;
  448. npages -= num;
  449. } while (npages != 0);
  450. spin_unlock_irqrestore(&iommu->lock, flags);
  451. }
  452. static void pci_4v_dma_sync_single_for_cpu(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
  453. {
  454. /* Nothing to do... */
  455. }
  456. static void pci_4v_dma_sync_sg_for_cpu(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
  457. {
  458. /* Nothing to do... */
  459. }
  460. const struct pci_iommu_ops pci_sun4v_iommu_ops = {
  461. .alloc_consistent = pci_4v_alloc_consistent,
  462. .free_consistent = pci_4v_free_consistent,
  463. .map_single = pci_4v_map_single,
  464. .unmap_single = pci_4v_unmap_single,
  465. .map_sg = pci_4v_map_sg,
  466. .unmap_sg = pci_4v_unmap_sg,
  467. .dma_sync_single_for_cpu = pci_4v_dma_sync_single_for_cpu,
  468. .dma_sync_sg_for_cpu = pci_4v_dma_sync_sg_for_cpu,
  469. };
  470. static inline int pci_sun4v_out_of_range(struct pci_pbm_info *pbm, unsigned int bus, unsigned int device, unsigned int func)
  471. {
  472. if (bus < pbm->pci_first_busno ||
  473. bus > pbm->pci_last_busno)
  474. return 1;
  475. return 0;
  476. }
  477. static int pci_sun4v_read_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
  478. int where, int size, u32 *value)
  479. {
  480. struct pci_pbm_info *pbm = bus_dev->sysdata;
  481. u32 devhandle = pbm->devhandle;
  482. unsigned int bus = bus_dev->number;
  483. unsigned int device = PCI_SLOT(devfn);
  484. unsigned int func = PCI_FUNC(devfn);
  485. unsigned long ret;
  486. if (bus_dev == pbm->pci_bus && devfn == 0x00)
  487. return pci_host_bridge_read_pci_cfg(bus_dev, devfn, where,
  488. size, value);
  489. if (pci_sun4v_out_of_range(pbm, bus, device, func)) {
  490. ret = ~0UL;
  491. } else {
  492. ret = pci_sun4v_config_get(devhandle,
  493. HV_PCI_DEVICE_BUILD(bus, device, func),
  494. where, size);
  495. #if 0
  496. printk("rcfg: [%x:%x:%x:%d]=[%lx]\n",
  497. devhandle, HV_PCI_DEVICE_BUILD(bus, device, func),
  498. where, size, ret);
  499. #endif
  500. }
  501. switch (size) {
  502. case 1:
  503. *value = ret & 0xff;
  504. break;
  505. case 2:
  506. *value = ret & 0xffff;
  507. break;
  508. case 4:
  509. *value = ret & 0xffffffff;
  510. break;
  511. };
  512. return PCIBIOS_SUCCESSFUL;
  513. }
  514. static int pci_sun4v_write_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
  515. int where, int size, u32 value)
  516. {
  517. struct pci_pbm_info *pbm = bus_dev->sysdata;
  518. u32 devhandle = pbm->devhandle;
  519. unsigned int bus = bus_dev->number;
  520. unsigned int device = PCI_SLOT(devfn);
  521. unsigned int func = PCI_FUNC(devfn);
  522. unsigned long ret;
  523. if (bus_dev == pbm->pci_bus && devfn == 0x00)
  524. return pci_host_bridge_write_pci_cfg(bus_dev, devfn, where,
  525. size, value);
  526. if (pci_sun4v_out_of_range(pbm, bus, device, func)) {
  527. /* Do nothing. */
  528. } else {
  529. ret = pci_sun4v_config_put(devhandle,
  530. HV_PCI_DEVICE_BUILD(bus, device, func),
  531. where, size, value);
  532. #if 0
  533. printk("wcfg: [%x:%x:%x:%d] v[%x] == [%lx]\n",
  534. devhandle, HV_PCI_DEVICE_BUILD(bus, device, func),
  535. where, size, value, ret);
  536. #endif
  537. }
  538. return PCIBIOS_SUCCESSFUL;
  539. }
  540. static struct pci_ops pci_sun4v_ops = {
  541. .read = pci_sun4v_read_pci_cfg,
  542. .write = pci_sun4v_write_pci_cfg,
  543. };
  544. static void pbm_scan_bus(struct pci_controller_info *p,
  545. struct pci_pbm_info *pbm)
  546. {
  547. pbm->pci_bus = pci_scan_one_pbm(pbm);
  548. }
  549. static void pci_sun4v_scan_bus(struct pci_controller_info *p)
  550. {
  551. struct property *prop;
  552. struct device_node *dp;
  553. if ((dp = p->pbm_A.prom_node) != NULL) {
  554. prop = of_find_property(dp, "66mhz-capable", NULL);
  555. p->pbm_A.is_66mhz_capable = (prop != NULL);
  556. pbm_scan_bus(p, &p->pbm_A);
  557. }
  558. if ((dp = p->pbm_B.prom_node) != NULL) {
  559. prop = of_find_property(dp, "66mhz-capable", NULL);
  560. p->pbm_B.is_66mhz_capable = (prop != NULL);
  561. pbm_scan_bus(p, &p->pbm_B);
  562. }
  563. /* XXX register error interrupt handlers XXX */
  564. }
  565. static unsigned long probe_existing_entries(struct pci_pbm_info *pbm,
  566. struct pci_iommu *iommu)
  567. {
  568. struct iommu_arena *arena = &iommu->arena;
  569. unsigned long i, cnt = 0;
  570. u32 devhandle;
  571. devhandle = pbm->devhandle;
  572. for (i = 0; i < arena->limit; i++) {
  573. unsigned long ret, io_attrs, ra;
  574. ret = pci_sun4v_iommu_getmap(devhandle,
  575. HV_PCI_TSBID(0, i),
  576. &io_attrs, &ra);
  577. if (ret == HV_EOK) {
  578. if (page_in_phys_avail(ra)) {
  579. pci_sun4v_iommu_demap(devhandle,
  580. HV_PCI_TSBID(0, i), 1);
  581. } else {
  582. cnt++;
  583. __set_bit(i, arena->map);
  584. }
  585. }
  586. }
  587. return cnt;
  588. }
  589. static void pci_sun4v_iommu_init(struct pci_pbm_info *pbm)
  590. {
  591. struct pci_iommu *iommu = pbm->iommu;
  592. struct property *prop;
  593. unsigned long num_tsb_entries, sz;
  594. u32 vdma[2], dma_mask, dma_offset;
  595. int tsbsize;
  596. prop = of_find_property(pbm->prom_node, "virtual-dma", NULL);
  597. if (prop) {
  598. u32 *val = prop->value;
  599. vdma[0] = val[0];
  600. vdma[1] = val[1];
  601. } else {
  602. /* No property, use default values. */
  603. vdma[0] = 0x80000000;
  604. vdma[1] = 0x80000000;
  605. }
  606. dma_mask = vdma[0];
  607. switch (vdma[1]) {
  608. case 0x20000000:
  609. dma_mask |= 0x1fffffff;
  610. tsbsize = 64;
  611. break;
  612. case 0x40000000:
  613. dma_mask |= 0x3fffffff;
  614. tsbsize = 128;
  615. break;
  616. case 0x80000000:
  617. dma_mask |= 0x7fffffff;
  618. tsbsize = 256;
  619. break;
  620. default:
  621. prom_printf("PCI-SUN4V: strange virtual-dma size.\n");
  622. prom_halt();
  623. };
  624. tsbsize *= (8 * 1024);
  625. num_tsb_entries = tsbsize / sizeof(iopte_t);
  626. dma_offset = vdma[0];
  627. /* Setup initial software IOMMU state. */
  628. spin_lock_init(&iommu->lock);
  629. iommu->ctx_lowest_free = 1;
  630. iommu->page_table_map_base = dma_offset;
  631. iommu->dma_addr_mask = dma_mask;
  632. /* Allocate and initialize the free area map. */
  633. sz = num_tsb_entries / 8;
  634. sz = (sz + 7UL) & ~7UL;
  635. iommu->arena.map = kzalloc(sz, GFP_KERNEL);
  636. if (!iommu->arena.map) {
  637. prom_printf("PCI_IOMMU: Error, kmalloc(arena.map) failed.\n");
  638. prom_halt();
  639. }
  640. iommu->arena.limit = num_tsb_entries;
  641. sz = probe_existing_entries(pbm, iommu);
  642. if (sz)
  643. printk("%s: Imported %lu TSB entries from OBP\n",
  644. pbm->name, sz);
  645. }
  646. static void pci_sun4v_get_bus_range(struct pci_pbm_info *pbm)
  647. {
  648. struct property *prop;
  649. unsigned int *busrange;
  650. prop = of_find_property(pbm->prom_node, "bus-range", NULL);
  651. busrange = prop->value;
  652. pbm->pci_first_busno = busrange[0];
  653. pbm->pci_last_busno = busrange[1];
  654. }
  655. #ifdef CONFIG_PCI_MSI
  656. struct pci_sun4v_msiq_entry {
  657. u64 version_type;
  658. #define MSIQ_VERSION_MASK 0xffffffff00000000UL
  659. #define MSIQ_VERSION_SHIFT 32
  660. #define MSIQ_TYPE_MASK 0x00000000000000ffUL
  661. #define MSIQ_TYPE_SHIFT 0
  662. #define MSIQ_TYPE_NONE 0x00
  663. #define MSIQ_TYPE_MSG 0x01
  664. #define MSIQ_TYPE_MSI32 0x02
  665. #define MSIQ_TYPE_MSI64 0x03
  666. #define MSIQ_TYPE_INTX 0x08
  667. #define MSIQ_TYPE_NONE2 0xff
  668. u64 intx_sysino;
  669. u64 reserved1;
  670. u64 stick;
  671. u64 req_id; /* bus/device/func */
  672. #define MSIQ_REQID_BUS_MASK 0xff00UL
  673. #define MSIQ_REQID_BUS_SHIFT 8
  674. #define MSIQ_REQID_DEVICE_MASK 0x00f8UL
  675. #define MSIQ_REQID_DEVICE_SHIFT 3
  676. #define MSIQ_REQID_FUNC_MASK 0x0007UL
  677. #define MSIQ_REQID_FUNC_SHIFT 0
  678. u64 msi_address;
  679. /* The format of this value is message type dependant.
  680. * For MSI bits 15:0 are the data from the MSI packet.
  681. * For MSI-X bits 31:0 are the data from the MSI packet.
  682. * For MSG, the message code and message routing code where:
  683. * bits 39:32 is the bus/device/fn of the msg target-id
  684. * bits 18:16 is the message routing code
  685. * bits 7:0 is the message code
  686. * For INTx the low order 2-bits are:
  687. * 00 - INTA
  688. * 01 - INTB
  689. * 10 - INTC
  690. * 11 - INTD
  691. */
  692. u64 msi_data;
  693. u64 reserved2;
  694. };
  695. /* For now this just runs as a pre-handler for the real interrupt handler.
  696. * So we just walk through the queue and ACK all the entries, update the
  697. * head pointer, and return.
  698. *
  699. * In the longer term it would be nice to do something more integrated
  700. * wherein we can pass in some of this MSI info to the drivers. This
  701. * would be most useful for PCIe fabric error messages, although we could
  702. * invoke those directly from the loop here in order to pass the info around.
  703. */
  704. static void pci_sun4v_msi_prehandler(unsigned int ino, void *data1, void *data2)
  705. {
  706. struct pci_pbm_info *pbm = data1;
  707. struct pci_sun4v_msiq_entry *base, *ep;
  708. unsigned long msiqid, orig_head, head, type, err;
  709. msiqid = (unsigned long) data2;
  710. head = 0xdeadbeef;
  711. err = pci_sun4v_msiq_gethead(pbm->devhandle, msiqid, &head);
  712. if (unlikely(err))
  713. goto hv_error_get;
  714. if (unlikely(head >= (pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry))))
  715. goto bad_offset;
  716. head /= sizeof(struct pci_sun4v_msiq_entry);
  717. orig_head = head;
  718. base = (pbm->msi_queues + ((msiqid - pbm->msiq_first) *
  719. (pbm->msiq_ent_count *
  720. sizeof(struct pci_sun4v_msiq_entry))));
  721. ep = &base[head];
  722. while ((ep->version_type & MSIQ_TYPE_MASK) != 0) {
  723. type = (ep->version_type & MSIQ_TYPE_MASK) >> MSIQ_TYPE_SHIFT;
  724. if (unlikely(type != MSIQ_TYPE_MSI32 &&
  725. type != MSIQ_TYPE_MSI64))
  726. goto bad_type;
  727. pci_sun4v_msi_setstate(pbm->devhandle,
  728. ep->msi_data /* msi_num */,
  729. HV_MSISTATE_IDLE);
  730. /* Clear the entry. */
  731. ep->version_type &= ~MSIQ_TYPE_MASK;
  732. /* Go to next entry in ring. */
  733. head++;
  734. if (head >= pbm->msiq_ent_count)
  735. head = 0;
  736. ep = &base[head];
  737. }
  738. if (likely(head != orig_head)) {
  739. /* ACK entries by updating head pointer. */
  740. head *= sizeof(struct pci_sun4v_msiq_entry);
  741. err = pci_sun4v_msiq_sethead(pbm->devhandle, msiqid, head);
  742. if (unlikely(err))
  743. goto hv_error_set;
  744. }
  745. return;
  746. hv_error_set:
  747. printk(KERN_EMERG "MSI: Hypervisor set head gives error %lu\n", err);
  748. goto hv_error_cont;
  749. hv_error_get:
  750. printk(KERN_EMERG "MSI: Hypervisor get head gives error %lu\n", err);
  751. hv_error_cont:
  752. printk(KERN_EMERG "MSI: devhandle[%x] msiqid[%lx] head[%lu]\n",
  753. pbm->devhandle, msiqid, head);
  754. return;
  755. bad_offset:
  756. printk(KERN_EMERG "MSI: Hypervisor gives bad offset %lx max(%lx)\n",
  757. head, pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry));
  758. return;
  759. bad_type:
  760. printk(KERN_EMERG "MSI: Entry has bad type %lx\n", type);
  761. return;
  762. }
  763. static int msi_bitmap_alloc(struct pci_pbm_info *pbm)
  764. {
  765. unsigned long size, bits_per_ulong;
  766. bits_per_ulong = sizeof(unsigned long) * 8;
  767. size = (pbm->msi_num + (bits_per_ulong - 1)) & ~(bits_per_ulong - 1);
  768. size /= 8;
  769. BUG_ON(size % sizeof(unsigned long));
  770. pbm->msi_bitmap = kzalloc(size, GFP_KERNEL);
  771. if (!pbm->msi_bitmap)
  772. return -ENOMEM;
  773. return 0;
  774. }
  775. static void msi_bitmap_free(struct pci_pbm_info *pbm)
  776. {
  777. kfree(pbm->msi_bitmap);
  778. pbm->msi_bitmap = NULL;
  779. }
  780. static int msi_queue_alloc(struct pci_pbm_info *pbm)
  781. {
  782. unsigned long q_size, alloc_size, pages, order;
  783. int i;
  784. q_size = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
  785. alloc_size = (pbm->msiq_num * q_size);
  786. order = get_order(alloc_size);
  787. pages = __get_free_pages(GFP_KERNEL | __GFP_COMP, order);
  788. if (pages == 0UL) {
  789. printk(KERN_ERR "MSI: Cannot allocate MSI queues (o=%lu).\n",
  790. order);
  791. return -ENOMEM;
  792. }
  793. memset((char *)pages, 0, PAGE_SIZE << order);
  794. pbm->msi_queues = (void *) pages;
  795. for (i = 0; i < pbm->msiq_num; i++) {
  796. unsigned long err, base = __pa(pages + (i * q_size));
  797. unsigned long ret1, ret2;
  798. err = pci_sun4v_msiq_conf(pbm->devhandle,
  799. pbm->msiq_first + i,
  800. base, pbm->msiq_ent_count);
  801. if (err) {
  802. printk(KERN_ERR "MSI: msiq register fails (err=%lu)\n",
  803. err);
  804. goto h_error;
  805. }
  806. err = pci_sun4v_msiq_info(pbm->devhandle,
  807. pbm->msiq_first + i,
  808. &ret1, &ret2);
  809. if (err) {
  810. printk(KERN_ERR "MSI: Cannot read msiq (err=%lu)\n",
  811. err);
  812. goto h_error;
  813. }
  814. if (ret1 != base || ret2 != pbm->msiq_ent_count) {
  815. printk(KERN_ERR "MSI: Bogus qconf "
  816. "expected[%lx:%x] got[%lx:%lx]\n",
  817. base, pbm->msiq_ent_count,
  818. ret1, ret2);
  819. goto h_error;
  820. }
  821. }
  822. return 0;
  823. h_error:
  824. free_pages(pages, order);
  825. return -EINVAL;
  826. }
  827. static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
  828. {
  829. const u32 *val;
  830. int len;
  831. val = of_get_property(pbm->prom_node, "#msi-eqs", &len);
  832. if (!val || len != 4)
  833. goto no_msi;
  834. pbm->msiq_num = *val;
  835. if (pbm->msiq_num) {
  836. const struct msiq_prop {
  837. u32 first_msiq;
  838. u32 num_msiq;
  839. u32 first_devino;
  840. } *mqp;
  841. const struct msi_range_prop {
  842. u32 first_msi;
  843. u32 num_msi;
  844. } *mrng;
  845. const struct addr_range_prop {
  846. u32 msi32_high;
  847. u32 msi32_low;
  848. u32 msi32_len;
  849. u32 msi64_high;
  850. u32 msi64_low;
  851. u32 msi64_len;
  852. } *arng;
  853. val = of_get_property(pbm->prom_node, "msi-eq-size", &len);
  854. if (!val || len != 4)
  855. goto no_msi;
  856. pbm->msiq_ent_count = *val;
  857. mqp = of_get_property(pbm->prom_node,
  858. "msi-eq-to-devino", &len);
  859. if (!mqp || len != sizeof(struct msiq_prop))
  860. goto no_msi;
  861. pbm->msiq_first = mqp->first_msiq;
  862. pbm->msiq_first_devino = mqp->first_devino;
  863. val = of_get_property(pbm->prom_node, "#msi", &len);
  864. if (!val || len != 4)
  865. goto no_msi;
  866. pbm->msi_num = *val;
  867. mrng = of_get_property(pbm->prom_node, "msi-ranges", &len);
  868. if (!mrng || len != sizeof(struct msi_range_prop))
  869. goto no_msi;
  870. pbm->msi_first = mrng->first_msi;
  871. val = of_get_property(pbm->prom_node, "msi-data-mask", &len);
  872. if (!val || len != 4)
  873. goto no_msi;
  874. pbm->msi_data_mask = *val;
  875. val = of_get_property(pbm->prom_node, "msix-data-width", &len);
  876. if (!val || len != 4)
  877. goto no_msi;
  878. pbm->msix_data_width = *val;
  879. arng = of_get_property(pbm->prom_node, "msi-address-ranges",
  880. &len);
  881. if (!arng || len != sizeof(struct addr_range_prop))
  882. goto no_msi;
  883. pbm->msi32_start = ((u64)arng->msi32_high << 32) |
  884. (u64) arng->msi32_low;
  885. pbm->msi64_start = ((u64)arng->msi64_high << 32) |
  886. (u64) arng->msi64_low;
  887. pbm->msi32_len = arng->msi32_len;
  888. pbm->msi64_len = arng->msi64_len;
  889. if (msi_bitmap_alloc(pbm))
  890. goto no_msi;
  891. if (msi_queue_alloc(pbm)) {
  892. msi_bitmap_free(pbm);
  893. goto no_msi;
  894. }
  895. printk(KERN_INFO "%s: MSI Queue first[%u] num[%u] count[%u] "
  896. "devino[0x%x]\n",
  897. pbm->name,
  898. pbm->msiq_first, pbm->msiq_num,
  899. pbm->msiq_ent_count,
  900. pbm->msiq_first_devino);
  901. printk(KERN_INFO "%s: MSI first[%u] num[%u] mask[0x%x] "
  902. "width[%u]\n",
  903. pbm->name,
  904. pbm->msi_first, pbm->msi_num, pbm->msi_data_mask,
  905. pbm->msix_data_width);
  906. printk(KERN_INFO "%s: MSI addr32[0x%lx:0x%x] "
  907. "addr64[0x%lx:0x%x]\n",
  908. pbm->name,
  909. pbm->msi32_start, pbm->msi32_len,
  910. pbm->msi64_start, pbm->msi64_len);
  911. printk(KERN_INFO "%s: MSI queues at RA [%p]\n",
  912. pbm->name,
  913. pbm->msi_queues);
  914. }
  915. return;
  916. no_msi:
  917. pbm->msiq_num = 0;
  918. printk(KERN_INFO "%s: No MSI support.\n", pbm->name);
  919. }
  920. static int alloc_msi(struct pci_pbm_info *pbm)
  921. {
  922. int i;
  923. for (i = 0; i < pbm->msi_num; i++) {
  924. if (!test_and_set_bit(i, pbm->msi_bitmap))
  925. return i + pbm->msi_first;
  926. }
  927. return -ENOENT;
  928. }
  929. static void free_msi(struct pci_pbm_info *pbm, int msi_num)
  930. {
  931. msi_num -= pbm->msi_first;
  932. clear_bit(msi_num, pbm->msi_bitmap);
  933. }
  934. static int pci_sun4v_setup_msi_irq(unsigned int *virt_irq_p,
  935. struct pci_dev *pdev,
  936. struct msi_desc *entry)
  937. {
  938. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  939. unsigned long devino, msiqid;
  940. struct msi_msg msg;
  941. int msi_num, err;
  942. *virt_irq_p = 0;
  943. msi_num = alloc_msi(pbm);
  944. if (msi_num < 0)
  945. return msi_num;
  946. devino = sun4v_build_msi(pbm->devhandle, virt_irq_p,
  947. pbm->msiq_first_devino,
  948. (pbm->msiq_first_devino +
  949. pbm->msiq_num));
  950. err = -ENOMEM;
  951. if (!devino)
  952. goto out_err;
  953. set_irq_msi(*virt_irq_p, entry);
  954. msiqid = ((devino - pbm->msiq_first_devino) +
  955. pbm->msiq_first);
  956. err = -EINVAL;
  957. if (pci_sun4v_msiq_setstate(pbm->devhandle, msiqid, HV_MSIQSTATE_IDLE))
  958. if (err)
  959. goto out_err;
  960. if (pci_sun4v_msiq_setvalid(pbm->devhandle, msiqid, HV_MSIQ_VALID))
  961. goto out_err;
  962. if (pci_sun4v_msi_setmsiq(pbm->devhandle,
  963. msi_num, msiqid,
  964. (entry->msi_attrib.is_64 ?
  965. HV_MSITYPE_MSI64 : HV_MSITYPE_MSI32)))
  966. goto out_err;
  967. if (pci_sun4v_msi_setstate(pbm->devhandle, msi_num, HV_MSISTATE_IDLE))
  968. goto out_err;
  969. if (pci_sun4v_msi_setvalid(pbm->devhandle, msi_num, HV_MSIVALID_VALID))
  970. goto out_err;
  971. pdev->dev.archdata.msi_num = msi_num;
  972. if (entry->msi_attrib.is_64) {
  973. msg.address_hi = pbm->msi64_start >> 32;
  974. msg.address_lo = pbm->msi64_start & 0xffffffff;
  975. } else {
  976. msg.address_hi = 0;
  977. msg.address_lo = pbm->msi32_start;
  978. }
  979. msg.data = msi_num;
  980. write_msi_msg(*virt_irq_p, &msg);
  981. irq_install_pre_handler(*virt_irq_p,
  982. pci_sun4v_msi_prehandler,
  983. pbm, (void *) msiqid);
  984. return 0;
  985. out_err:
  986. free_msi(pbm, msi_num);
  987. sun4v_destroy_msi(*virt_irq_p);
  988. *virt_irq_p = 0;
  989. return err;
  990. }
  991. static void pci_sun4v_teardown_msi_irq(unsigned int virt_irq,
  992. struct pci_dev *pdev)
  993. {
  994. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  995. unsigned long msiqid, err;
  996. unsigned int msi_num;
  997. msi_num = pdev->dev.archdata.msi_num;
  998. err = pci_sun4v_msi_getmsiq(pbm->devhandle, msi_num, &msiqid);
  999. if (err) {
  1000. printk(KERN_ERR "%s: getmsiq gives error %lu\n",
  1001. pbm->name, err);
  1002. return;
  1003. }
  1004. pci_sun4v_msi_setvalid(pbm->devhandle, msi_num, HV_MSIVALID_INVALID);
  1005. pci_sun4v_msiq_setvalid(pbm->devhandle, msiqid, HV_MSIQ_INVALID);
  1006. free_msi(pbm, msi_num);
  1007. /* The sun4v_destroy_msi() will liberate the devino and thus the MSIQ
  1008. * allocation.
  1009. */
  1010. sun4v_destroy_msi(virt_irq);
  1011. }
  1012. #else /* CONFIG_PCI_MSI */
  1013. static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
  1014. {
  1015. }
  1016. #endif /* !(CONFIG_PCI_MSI) */
  1017. static void pci_sun4v_pbm_init(struct pci_controller_info *p, struct device_node *dp, u32 devhandle)
  1018. {
  1019. struct pci_pbm_info *pbm;
  1020. if (devhandle & 0x40)
  1021. pbm = &p->pbm_B;
  1022. else
  1023. pbm = &p->pbm_A;
  1024. pbm->parent = p;
  1025. pbm->prom_node = dp;
  1026. pbm->devhandle = devhandle;
  1027. pbm->name = dp->full_name;
  1028. printk("%s: SUN4V PCI Bus Module\n", pbm->name);
  1029. pci_determine_mem_io_space(pbm);
  1030. pci_sun4v_get_bus_range(pbm);
  1031. pci_sun4v_iommu_init(pbm);
  1032. pci_sun4v_msi_init(pbm);
  1033. }
  1034. void sun4v_pci_init(struct device_node *dp, char *model_name)
  1035. {
  1036. struct pci_controller_info *p;
  1037. struct pci_iommu *iommu;
  1038. struct property *prop;
  1039. struct linux_prom64_registers *regs;
  1040. u32 devhandle;
  1041. int i;
  1042. prop = of_find_property(dp, "reg", NULL);
  1043. regs = prop->value;
  1044. devhandle = (regs->phys_addr >> 32UL) & 0x0fffffff;
  1045. for (p = pci_controller_root; p; p = p->next) {
  1046. struct pci_pbm_info *pbm;
  1047. if (p->pbm_A.prom_node && p->pbm_B.prom_node)
  1048. continue;
  1049. pbm = (p->pbm_A.prom_node ?
  1050. &p->pbm_A :
  1051. &p->pbm_B);
  1052. if (pbm->devhandle == (devhandle ^ 0x40)) {
  1053. pci_sun4v_pbm_init(p, dp, devhandle);
  1054. return;
  1055. }
  1056. }
  1057. for_each_possible_cpu(i) {
  1058. unsigned long page = get_zeroed_page(GFP_ATOMIC);
  1059. if (!page)
  1060. goto fatal_memory_error;
  1061. per_cpu(pci_iommu_batch, i).pglist = (u64 *) page;
  1062. }
  1063. p = kzalloc(sizeof(struct pci_controller_info), GFP_ATOMIC);
  1064. if (!p)
  1065. goto fatal_memory_error;
  1066. iommu = kzalloc(sizeof(struct pci_iommu), GFP_ATOMIC);
  1067. if (!iommu)
  1068. goto fatal_memory_error;
  1069. p->pbm_A.iommu = iommu;
  1070. iommu = kzalloc(sizeof(struct pci_iommu), GFP_ATOMIC);
  1071. if (!iommu)
  1072. goto fatal_memory_error;
  1073. p->pbm_B.iommu = iommu;
  1074. p->next = pci_controller_root;
  1075. pci_controller_root = p;
  1076. p->index = pci_num_controllers++;
  1077. p->scan_bus = pci_sun4v_scan_bus;
  1078. #ifdef CONFIG_PCI_MSI
  1079. p->setup_msi_irq = pci_sun4v_setup_msi_irq;
  1080. p->teardown_msi_irq = pci_sun4v_teardown_msi_irq;
  1081. #endif
  1082. p->pci_ops = &pci_sun4v_ops;
  1083. /* Like PSYCHO and SCHIZO we have a 2GB aligned area
  1084. * for memory space.
  1085. */
  1086. pci_memspace_mask = 0x7fffffffUL;
  1087. pci_sun4v_pbm_init(p, dp, devhandle);
  1088. return;
  1089. fatal_memory_error:
  1090. prom_printf("SUN4V_PCI: Fatal memory allocation error.\n");
  1091. prom_halt();
  1092. }