pci_sun4v.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  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 pci_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 pci_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. 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 (pci_sun4v_out_of_range(pbm, bus, device, func)) {
  487. ret = ~0UL;
  488. } else {
  489. ret = pci_sun4v_config_get(devhandle,
  490. HV_PCI_DEVICE_BUILD(bus, device, func),
  491. where, size);
  492. #if 0
  493. printk("rcfg: [%x:%x:%x:%d]=[%lx]\n",
  494. devhandle, HV_PCI_DEVICE_BUILD(bus, device, func),
  495. where, size, ret);
  496. #endif
  497. }
  498. switch (size) {
  499. case 1:
  500. *value = ret & 0xff;
  501. break;
  502. case 2:
  503. *value = ret & 0xffff;
  504. break;
  505. case 4:
  506. *value = ret & 0xffffffff;
  507. break;
  508. };
  509. return PCIBIOS_SUCCESSFUL;
  510. }
  511. static int pci_sun4v_write_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
  512. int where, int size, u32 value)
  513. {
  514. struct pci_pbm_info *pbm = bus_dev->sysdata;
  515. u32 devhandle = pbm->devhandle;
  516. unsigned int bus = bus_dev->number;
  517. unsigned int device = PCI_SLOT(devfn);
  518. unsigned int func = PCI_FUNC(devfn);
  519. unsigned long ret;
  520. if (pci_sun4v_out_of_range(pbm, bus, device, func)) {
  521. /* Do nothing. */
  522. } else {
  523. ret = pci_sun4v_config_put(devhandle,
  524. HV_PCI_DEVICE_BUILD(bus, device, func),
  525. where, size, value);
  526. #if 0
  527. printk("wcfg: [%x:%x:%x:%d] v[%x] == [%lx]\n",
  528. devhandle, HV_PCI_DEVICE_BUILD(bus, device, func),
  529. where, size, value, ret);
  530. #endif
  531. }
  532. return PCIBIOS_SUCCESSFUL;
  533. }
  534. static struct pci_ops pci_sun4v_ops = {
  535. .read = pci_sun4v_read_pci_cfg,
  536. .write = pci_sun4v_write_pci_cfg,
  537. };
  538. static void pbm_scan_bus(struct pci_controller_info *p,
  539. struct pci_pbm_info *pbm)
  540. {
  541. pbm->pci_bus = pci_scan_one_pbm(pbm);
  542. }
  543. static void pci_sun4v_scan_bus(struct pci_controller_info *p)
  544. {
  545. struct property *prop;
  546. struct device_node *dp;
  547. if ((dp = p->pbm_A.prom_node) != NULL) {
  548. prop = of_find_property(dp, "66mhz-capable", NULL);
  549. p->pbm_A.is_66mhz_capable = (prop != NULL);
  550. pbm_scan_bus(p, &p->pbm_A);
  551. }
  552. if ((dp = p->pbm_B.prom_node) != NULL) {
  553. prop = of_find_property(dp, "66mhz-capable", NULL);
  554. p->pbm_B.is_66mhz_capable = (prop != NULL);
  555. pbm_scan_bus(p, &p->pbm_B);
  556. }
  557. /* XXX register error interrupt handlers XXX */
  558. }
  559. static unsigned long probe_existing_entries(struct pci_pbm_info *pbm,
  560. struct pci_iommu *iommu)
  561. {
  562. struct pci_iommu_arena *arena = &iommu->arena;
  563. unsigned long i, cnt = 0;
  564. u32 devhandle;
  565. devhandle = pbm->devhandle;
  566. for (i = 0; i < arena->limit; i++) {
  567. unsigned long ret, io_attrs, ra;
  568. ret = pci_sun4v_iommu_getmap(devhandle,
  569. HV_PCI_TSBID(0, i),
  570. &io_attrs, &ra);
  571. if (ret == HV_EOK) {
  572. if (page_in_phys_avail(ra)) {
  573. pci_sun4v_iommu_demap(devhandle,
  574. HV_PCI_TSBID(0, i), 1);
  575. } else {
  576. cnt++;
  577. __set_bit(i, arena->map);
  578. }
  579. }
  580. }
  581. return cnt;
  582. }
  583. static void pci_sun4v_iommu_init(struct pci_pbm_info *pbm)
  584. {
  585. struct pci_iommu *iommu = pbm->iommu;
  586. struct property *prop;
  587. unsigned long num_tsb_entries, sz;
  588. u32 vdma[2], dma_mask, dma_offset;
  589. int tsbsize;
  590. prop = of_find_property(pbm->prom_node, "virtual-dma", NULL);
  591. if (prop) {
  592. u32 *val = prop->value;
  593. vdma[0] = val[0];
  594. vdma[1] = val[1];
  595. } else {
  596. /* No property, use default values. */
  597. vdma[0] = 0x80000000;
  598. vdma[1] = 0x80000000;
  599. }
  600. dma_mask = vdma[0];
  601. switch (vdma[1]) {
  602. case 0x20000000:
  603. dma_mask |= 0x1fffffff;
  604. tsbsize = 64;
  605. break;
  606. case 0x40000000:
  607. dma_mask |= 0x3fffffff;
  608. tsbsize = 128;
  609. break;
  610. case 0x80000000:
  611. dma_mask |= 0x7fffffff;
  612. tsbsize = 256;
  613. break;
  614. default:
  615. prom_printf("PCI-SUN4V: strange virtual-dma size.\n");
  616. prom_halt();
  617. };
  618. tsbsize *= (8 * 1024);
  619. num_tsb_entries = tsbsize / sizeof(iopte_t);
  620. dma_offset = vdma[0];
  621. /* Setup initial software IOMMU state. */
  622. spin_lock_init(&iommu->lock);
  623. iommu->ctx_lowest_free = 1;
  624. iommu->page_table_map_base = dma_offset;
  625. iommu->dma_addr_mask = dma_mask;
  626. /* Allocate and initialize the free area map. */
  627. sz = num_tsb_entries / 8;
  628. sz = (sz + 7UL) & ~7UL;
  629. iommu->arena.map = kzalloc(sz, GFP_KERNEL);
  630. if (!iommu->arena.map) {
  631. prom_printf("PCI_IOMMU: Error, kmalloc(arena.map) failed.\n");
  632. prom_halt();
  633. }
  634. iommu->arena.limit = num_tsb_entries;
  635. sz = probe_existing_entries(pbm, iommu);
  636. if (sz)
  637. printk("%s: Imported %lu TSB entries from OBP\n",
  638. pbm->name, sz);
  639. }
  640. static void pci_sun4v_get_bus_range(struct pci_pbm_info *pbm)
  641. {
  642. struct property *prop;
  643. unsigned int *busrange;
  644. prop = of_find_property(pbm->prom_node, "bus-range", NULL);
  645. busrange = prop->value;
  646. pbm->pci_first_busno = busrange[0];
  647. pbm->pci_last_busno = busrange[1];
  648. }
  649. #ifdef CONFIG_PCI_MSI
  650. struct pci_sun4v_msiq_entry {
  651. u64 version_type;
  652. #define MSIQ_VERSION_MASK 0xffffffff00000000UL
  653. #define MSIQ_VERSION_SHIFT 32
  654. #define MSIQ_TYPE_MASK 0x00000000000000ffUL
  655. #define MSIQ_TYPE_SHIFT 0
  656. #define MSIQ_TYPE_NONE 0x00
  657. #define MSIQ_TYPE_MSG 0x01
  658. #define MSIQ_TYPE_MSI32 0x02
  659. #define MSIQ_TYPE_MSI64 0x03
  660. #define MSIQ_TYPE_INTX 0x08
  661. #define MSIQ_TYPE_NONE2 0xff
  662. u64 intx_sysino;
  663. u64 reserved1;
  664. u64 stick;
  665. u64 req_id; /* bus/device/func */
  666. #define MSIQ_REQID_BUS_MASK 0xff00UL
  667. #define MSIQ_REQID_BUS_SHIFT 8
  668. #define MSIQ_REQID_DEVICE_MASK 0x00f8UL
  669. #define MSIQ_REQID_DEVICE_SHIFT 3
  670. #define MSIQ_REQID_FUNC_MASK 0x0007UL
  671. #define MSIQ_REQID_FUNC_SHIFT 0
  672. u64 msi_address;
  673. /* The format of this value is message type dependant.
  674. * For MSI bits 15:0 are the data from the MSI packet.
  675. * For MSI-X bits 31:0 are the data from the MSI packet.
  676. * For MSG, the message code and message routing code where:
  677. * bits 39:32 is the bus/device/fn of the msg target-id
  678. * bits 18:16 is the message routing code
  679. * bits 7:0 is the message code
  680. * For INTx the low order 2-bits are:
  681. * 00 - INTA
  682. * 01 - INTB
  683. * 10 - INTC
  684. * 11 - INTD
  685. */
  686. u64 msi_data;
  687. u64 reserved2;
  688. };
  689. /* For now this just runs as a pre-handler for the real interrupt handler.
  690. * So we just walk through the queue and ACK all the entries, update the
  691. * head pointer, and return.
  692. *
  693. * In the longer term it would be nice to do something more integrated
  694. * wherein we can pass in some of this MSI info to the drivers. This
  695. * would be most useful for PCIe fabric error messages, although we could
  696. * invoke those directly from the loop here in order to pass the info around.
  697. */
  698. static void pci_sun4v_msi_prehandler(unsigned int ino, void *data1, void *data2)
  699. {
  700. struct pci_pbm_info *pbm = data1;
  701. struct pci_sun4v_msiq_entry *base, *ep;
  702. unsigned long msiqid, orig_head, head, type, err;
  703. msiqid = (unsigned long) data2;
  704. head = 0xdeadbeef;
  705. err = pci_sun4v_msiq_gethead(pbm->devhandle, msiqid, &head);
  706. if (unlikely(err))
  707. goto hv_error_get;
  708. if (unlikely(head >= (pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry))))
  709. goto bad_offset;
  710. head /= sizeof(struct pci_sun4v_msiq_entry);
  711. orig_head = head;
  712. base = (pbm->msi_queues + ((msiqid - pbm->msiq_first) *
  713. (pbm->msiq_ent_count *
  714. sizeof(struct pci_sun4v_msiq_entry))));
  715. ep = &base[head];
  716. while ((ep->version_type & MSIQ_TYPE_MASK) != 0) {
  717. type = (ep->version_type & MSIQ_TYPE_MASK) >> MSIQ_TYPE_SHIFT;
  718. if (unlikely(type != MSIQ_TYPE_MSI32 &&
  719. type != MSIQ_TYPE_MSI64))
  720. goto bad_type;
  721. pci_sun4v_msi_setstate(pbm->devhandle,
  722. ep->msi_data /* msi_num */,
  723. HV_MSISTATE_IDLE);
  724. /* Clear the entry. */
  725. ep->version_type &= ~MSIQ_TYPE_MASK;
  726. /* Go to next entry in ring. */
  727. head++;
  728. if (head >= pbm->msiq_ent_count)
  729. head = 0;
  730. ep = &base[head];
  731. }
  732. if (likely(head != orig_head)) {
  733. /* ACK entries by updating head pointer. */
  734. head *= sizeof(struct pci_sun4v_msiq_entry);
  735. err = pci_sun4v_msiq_sethead(pbm->devhandle, msiqid, head);
  736. if (unlikely(err))
  737. goto hv_error_set;
  738. }
  739. return;
  740. hv_error_set:
  741. printk(KERN_EMERG "MSI: Hypervisor set head gives error %lu\n", err);
  742. goto hv_error_cont;
  743. hv_error_get:
  744. printk(KERN_EMERG "MSI: Hypervisor get head gives error %lu\n", err);
  745. hv_error_cont:
  746. printk(KERN_EMERG "MSI: devhandle[%x] msiqid[%lx] head[%lu]\n",
  747. pbm->devhandle, msiqid, head);
  748. return;
  749. bad_offset:
  750. printk(KERN_EMERG "MSI: Hypervisor gives bad offset %lx max(%lx)\n",
  751. head, pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry));
  752. return;
  753. bad_type:
  754. printk(KERN_EMERG "MSI: Entry has bad type %lx\n", type);
  755. return;
  756. }
  757. static int msi_bitmap_alloc(struct pci_pbm_info *pbm)
  758. {
  759. unsigned long size, bits_per_ulong;
  760. bits_per_ulong = sizeof(unsigned long) * 8;
  761. size = (pbm->msi_num + (bits_per_ulong - 1)) & ~(bits_per_ulong - 1);
  762. size /= 8;
  763. BUG_ON(size % sizeof(unsigned long));
  764. pbm->msi_bitmap = kzalloc(size, GFP_KERNEL);
  765. if (!pbm->msi_bitmap)
  766. return -ENOMEM;
  767. return 0;
  768. }
  769. static void msi_bitmap_free(struct pci_pbm_info *pbm)
  770. {
  771. kfree(pbm->msi_bitmap);
  772. pbm->msi_bitmap = NULL;
  773. }
  774. static int msi_queue_alloc(struct pci_pbm_info *pbm)
  775. {
  776. unsigned long q_size, alloc_size, pages, order;
  777. int i;
  778. q_size = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
  779. alloc_size = (pbm->msiq_num * q_size);
  780. order = get_order(alloc_size);
  781. pages = __get_free_pages(GFP_KERNEL | __GFP_COMP, order);
  782. if (pages == 0UL) {
  783. printk(KERN_ERR "MSI: Cannot allocate MSI queues (o=%lu).\n",
  784. order);
  785. return -ENOMEM;
  786. }
  787. memset((char *)pages, 0, PAGE_SIZE << order);
  788. pbm->msi_queues = (void *) pages;
  789. for (i = 0; i < pbm->msiq_num; i++) {
  790. unsigned long err, base = __pa(pages + (i * q_size));
  791. unsigned long ret1, ret2;
  792. err = pci_sun4v_msiq_conf(pbm->devhandle,
  793. pbm->msiq_first + i,
  794. base, pbm->msiq_ent_count);
  795. if (err) {
  796. printk(KERN_ERR "MSI: msiq register fails (err=%lu)\n",
  797. err);
  798. goto h_error;
  799. }
  800. err = pci_sun4v_msiq_info(pbm->devhandle,
  801. pbm->msiq_first + i,
  802. &ret1, &ret2);
  803. if (err) {
  804. printk(KERN_ERR "MSI: Cannot read msiq (err=%lu)\n",
  805. err);
  806. goto h_error;
  807. }
  808. if (ret1 != base || ret2 != pbm->msiq_ent_count) {
  809. printk(KERN_ERR "MSI: Bogus qconf "
  810. "expected[%lx:%x] got[%lx:%lx]\n",
  811. base, pbm->msiq_ent_count,
  812. ret1, ret2);
  813. goto h_error;
  814. }
  815. }
  816. return 0;
  817. h_error:
  818. free_pages(pages, order);
  819. return -EINVAL;
  820. }
  821. static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
  822. {
  823. const u32 *val;
  824. int len;
  825. val = of_get_property(pbm->prom_node, "#msi-eqs", &len);
  826. if (!val || len != 4)
  827. goto no_msi;
  828. pbm->msiq_num = *val;
  829. if (pbm->msiq_num) {
  830. const struct msiq_prop {
  831. u32 first_msiq;
  832. u32 num_msiq;
  833. u32 first_devino;
  834. } *mqp;
  835. const struct msi_range_prop {
  836. u32 first_msi;
  837. u32 num_msi;
  838. } *mrng;
  839. const struct addr_range_prop {
  840. u32 msi32_high;
  841. u32 msi32_low;
  842. u32 msi32_len;
  843. u32 msi64_high;
  844. u32 msi64_low;
  845. u32 msi64_len;
  846. } *arng;
  847. val = of_get_property(pbm->prom_node, "msi-eq-size", &len);
  848. if (!val || len != 4)
  849. goto no_msi;
  850. pbm->msiq_ent_count = *val;
  851. mqp = of_get_property(pbm->prom_node,
  852. "msi-eq-to-devino", &len);
  853. if (!mqp || len != sizeof(struct msiq_prop))
  854. goto no_msi;
  855. pbm->msiq_first = mqp->first_msiq;
  856. pbm->msiq_first_devino = mqp->first_devino;
  857. val = of_get_property(pbm->prom_node, "#msi", &len);
  858. if (!val || len != 4)
  859. goto no_msi;
  860. pbm->msi_num = *val;
  861. mrng = of_get_property(pbm->prom_node, "msi-ranges", &len);
  862. if (!mrng || len != sizeof(struct msi_range_prop))
  863. goto no_msi;
  864. pbm->msi_first = mrng->first_msi;
  865. val = of_get_property(pbm->prom_node, "msi-data-mask", &len);
  866. if (!val || len != 4)
  867. goto no_msi;
  868. pbm->msi_data_mask = *val;
  869. val = of_get_property(pbm->prom_node, "msix-data-width", &len);
  870. if (!val || len != 4)
  871. goto no_msi;
  872. pbm->msix_data_width = *val;
  873. arng = of_get_property(pbm->prom_node, "msi-address-ranges",
  874. &len);
  875. if (!arng || len != sizeof(struct addr_range_prop))
  876. goto no_msi;
  877. pbm->msi32_start = ((u64)arng->msi32_high << 32) |
  878. (u64) arng->msi32_low;
  879. pbm->msi64_start = ((u64)arng->msi64_high << 32) |
  880. (u64) arng->msi64_low;
  881. pbm->msi32_len = arng->msi32_len;
  882. pbm->msi64_len = arng->msi64_len;
  883. if (msi_bitmap_alloc(pbm))
  884. goto no_msi;
  885. if (msi_queue_alloc(pbm)) {
  886. msi_bitmap_free(pbm);
  887. goto no_msi;
  888. }
  889. printk(KERN_INFO "%s: MSI Queue first[%u] num[%u] count[%u] "
  890. "devino[0x%x]\n",
  891. pbm->name,
  892. pbm->msiq_first, pbm->msiq_num,
  893. pbm->msiq_ent_count,
  894. pbm->msiq_first_devino);
  895. printk(KERN_INFO "%s: MSI first[%u] num[%u] mask[0x%x] "
  896. "width[%u]\n",
  897. pbm->name,
  898. pbm->msi_first, pbm->msi_num, pbm->msi_data_mask,
  899. pbm->msix_data_width);
  900. printk(KERN_INFO "%s: MSI addr32[0x%lx:0x%x] "
  901. "addr64[0x%lx:0x%x]\n",
  902. pbm->name,
  903. pbm->msi32_start, pbm->msi32_len,
  904. pbm->msi64_start, pbm->msi64_len);
  905. printk(KERN_INFO "%s: MSI queues at RA [%p]\n",
  906. pbm->name,
  907. pbm->msi_queues);
  908. }
  909. return;
  910. no_msi:
  911. pbm->msiq_num = 0;
  912. printk(KERN_INFO "%s: No MSI support.\n", pbm->name);
  913. }
  914. static int alloc_msi(struct pci_pbm_info *pbm)
  915. {
  916. int i;
  917. for (i = 0; i < pbm->msi_num; i++) {
  918. if (!test_and_set_bit(i, pbm->msi_bitmap))
  919. return i + pbm->msi_first;
  920. }
  921. return -ENOENT;
  922. }
  923. static void free_msi(struct pci_pbm_info *pbm, int msi_num)
  924. {
  925. msi_num -= pbm->msi_first;
  926. clear_bit(msi_num, pbm->msi_bitmap);
  927. }
  928. static int pci_sun4v_setup_msi_irq(unsigned int *virt_irq_p,
  929. struct pci_dev *pdev,
  930. struct msi_desc *entry)
  931. {
  932. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  933. unsigned long devino, msiqid;
  934. struct msi_msg msg;
  935. int msi_num, err;
  936. *virt_irq_p = 0;
  937. msi_num = alloc_msi(pbm);
  938. if (msi_num < 0)
  939. return msi_num;
  940. devino = sun4v_build_msi(pbm->devhandle, virt_irq_p,
  941. pbm->msiq_first_devino,
  942. (pbm->msiq_first_devino +
  943. pbm->msiq_num));
  944. err = -ENOMEM;
  945. if (!devino)
  946. goto out_err;
  947. set_irq_msi(*virt_irq_p, entry);
  948. msiqid = ((devino - pbm->msiq_first_devino) +
  949. pbm->msiq_first);
  950. err = -EINVAL;
  951. if (pci_sun4v_msiq_setstate(pbm->devhandle, msiqid, HV_MSIQSTATE_IDLE))
  952. if (err)
  953. goto out_err;
  954. if (pci_sun4v_msiq_setvalid(pbm->devhandle, msiqid, HV_MSIQ_VALID))
  955. goto out_err;
  956. if (pci_sun4v_msi_setmsiq(pbm->devhandle,
  957. msi_num, msiqid,
  958. (entry->msi_attrib.is_64 ?
  959. HV_MSITYPE_MSI64 : HV_MSITYPE_MSI32)))
  960. goto out_err;
  961. if (pci_sun4v_msi_setstate(pbm->devhandle, msi_num, HV_MSISTATE_IDLE))
  962. goto out_err;
  963. if (pci_sun4v_msi_setvalid(pbm->devhandle, msi_num, HV_MSIVALID_VALID))
  964. goto out_err;
  965. pdev->dev.archdata.msi_num = msi_num;
  966. if (entry->msi_attrib.is_64) {
  967. msg.address_hi = pbm->msi64_start >> 32;
  968. msg.address_lo = pbm->msi64_start & 0xffffffff;
  969. } else {
  970. msg.address_hi = 0;
  971. msg.address_lo = pbm->msi32_start;
  972. }
  973. msg.data = msi_num;
  974. write_msi_msg(*virt_irq_p, &msg);
  975. irq_install_pre_handler(*virt_irq_p,
  976. pci_sun4v_msi_prehandler,
  977. pbm, (void *) msiqid);
  978. return 0;
  979. out_err:
  980. free_msi(pbm, msi_num);
  981. sun4v_destroy_msi(*virt_irq_p);
  982. *virt_irq_p = 0;
  983. return err;
  984. }
  985. static void pci_sun4v_teardown_msi_irq(unsigned int virt_irq,
  986. struct pci_dev *pdev)
  987. {
  988. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  989. unsigned long msiqid, err;
  990. unsigned int msi_num;
  991. msi_num = pdev->dev.archdata.msi_num;
  992. err = pci_sun4v_msi_getmsiq(pbm->devhandle, msi_num, &msiqid);
  993. if (err) {
  994. printk(KERN_ERR "%s: getmsiq gives error %lu\n",
  995. pbm->name, err);
  996. return;
  997. }
  998. pci_sun4v_msi_setvalid(pbm->devhandle, msi_num, HV_MSIVALID_INVALID);
  999. pci_sun4v_msiq_setvalid(pbm->devhandle, msiqid, HV_MSIQ_INVALID);
  1000. free_msi(pbm, msi_num);
  1001. /* The sun4v_destroy_msi() will liberate the devino and thus the MSIQ
  1002. * allocation.
  1003. */
  1004. sun4v_destroy_msi(virt_irq);
  1005. }
  1006. #else /* CONFIG_PCI_MSI */
  1007. static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
  1008. {
  1009. }
  1010. #endif /* !(CONFIG_PCI_MSI) */
  1011. static void pci_sun4v_pbm_init(struct pci_controller_info *p, struct device_node *dp, u32 devhandle)
  1012. {
  1013. struct pci_pbm_info *pbm;
  1014. if (devhandle & 0x40)
  1015. pbm = &p->pbm_B;
  1016. else
  1017. pbm = &p->pbm_A;
  1018. pbm->parent = p;
  1019. pbm->prom_node = dp;
  1020. pbm->devhandle = devhandle;
  1021. pbm->name = dp->full_name;
  1022. printk("%s: SUN4V PCI Bus Module\n", pbm->name);
  1023. pci_determine_mem_io_space(pbm);
  1024. pci_sun4v_get_bus_range(pbm);
  1025. pci_sun4v_iommu_init(pbm);
  1026. pci_sun4v_msi_init(pbm);
  1027. }
  1028. void sun4v_pci_init(struct device_node *dp, char *model_name)
  1029. {
  1030. struct pci_controller_info *p;
  1031. struct pci_iommu *iommu;
  1032. struct property *prop;
  1033. struct linux_prom64_registers *regs;
  1034. u32 devhandle;
  1035. int i;
  1036. prop = of_find_property(dp, "reg", NULL);
  1037. regs = prop->value;
  1038. devhandle = (regs->phys_addr >> 32UL) & 0x0fffffff;
  1039. for (p = pci_controller_root; p; p = p->next) {
  1040. struct pci_pbm_info *pbm;
  1041. if (p->pbm_A.prom_node && p->pbm_B.prom_node)
  1042. continue;
  1043. pbm = (p->pbm_A.prom_node ?
  1044. &p->pbm_A :
  1045. &p->pbm_B);
  1046. if (pbm->devhandle == (devhandle ^ 0x40)) {
  1047. pci_sun4v_pbm_init(p, dp, devhandle);
  1048. return;
  1049. }
  1050. }
  1051. for_each_possible_cpu(i) {
  1052. unsigned long page = get_zeroed_page(GFP_ATOMIC);
  1053. if (!page)
  1054. goto fatal_memory_error;
  1055. per_cpu(pci_iommu_batch, i).pglist = (u64 *) page;
  1056. }
  1057. p = kzalloc(sizeof(struct pci_controller_info), GFP_ATOMIC);
  1058. if (!p)
  1059. goto fatal_memory_error;
  1060. iommu = kzalloc(sizeof(struct pci_iommu), GFP_ATOMIC);
  1061. if (!iommu)
  1062. goto fatal_memory_error;
  1063. p->pbm_A.iommu = iommu;
  1064. iommu = kzalloc(sizeof(struct pci_iommu), GFP_ATOMIC);
  1065. if (!iommu)
  1066. goto fatal_memory_error;
  1067. p->pbm_B.iommu = iommu;
  1068. p->next = pci_controller_root;
  1069. pci_controller_root = p;
  1070. p->index = pci_num_controllers++;
  1071. p->scan_bus = pci_sun4v_scan_bus;
  1072. #ifdef CONFIG_PCI_MSI
  1073. p->setup_msi_irq = pci_sun4v_setup_msi_irq;
  1074. p->teardown_msi_irq = pci_sun4v_teardown_msi_irq;
  1075. #endif
  1076. p->pci_ops = &pci_sun4v_ops;
  1077. /* Like PSYCHO and SCHIZO we have a 2GB aligned area
  1078. * for memory space.
  1079. */
  1080. pci_memspace_mask = 0x7fffffffUL;
  1081. pci_sun4v_pbm_init(p, dp, devhandle);
  1082. return;
  1083. fatal_memory_error:
  1084. prom_printf("SUN4V_PCI: Fatal memory allocation error.\n");
  1085. prom_halt();
  1086. }