pci_sun4v.c 31 KB

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