pci_sun4v.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  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->pci_first_slot = 1;
  1021. pbm->devhandle = devhandle;
  1022. pbm->name = dp->full_name;
  1023. printk("%s: SUN4V PCI Bus Module\n", pbm->name);
  1024. pci_determine_mem_io_space(pbm);
  1025. pci_sun4v_get_bus_range(pbm);
  1026. pci_sun4v_iommu_init(pbm);
  1027. pci_sun4v_msi_init(pbm);
  1028. }
  1029. void sun4v_pci_init(struct device_node *dp, char *model_name)
  1030. {
  1031. struct pci_controller_info *p;
  1032. struct pci_iommu *iommu;
  1033. struct property *prop;
  1034. struct linux_prom64_registers *regs;
  1035. u32 devhandle;
  1036. int i;
  1037. prop = of_find_property(dp, "reg", NULL);
  1038. regs = prop->value;
  1039. devhandle = (regs->phys_addr >> 32UL) & 0x0fffffff;
  1040. for (p = pci_controller_root; p; p = p->next) {
  1041. struct pci_pbm_info *pbm;
  1042. if (p->pbm_A.prom_node && p->pbm_B.prom_node)
  1043. continue;
  1044. pbm = (p->pbm_A.prom_node ?
  1045. &p->pbm_A :
  1046. &p->pbm_B);
  1047. if (pbm->devhandle == (devhandle ^ 0x40)) {
  1048. pci_sun4v_pbm_init(p, dp, devhandle);
  1049. return;
  1050. }
  1051. }
  1052. for_each_possible_cpu(i) {
  1053. unsigned long page = get_zeroed_page(GFP_ATOMIC);
  1054. if (!page)
  1055. goto fatal_memory_error;
  1056. per_cpu(pci_iommu_batch, i).pglist = (u64 *) page;
  1057. }
  1058. p = kzalloc(sizeof(struct pci_controller_info), GFP_ATOMIC);
  1059. if (!p)
  1060. goto fatal_memory_error;
  1061. iommu = kzalloc(sizeof(struct pci_iommu), GFP_ATOMIC);
  1062. if (!iommu)
  1063. goto fatal_memory_error;
  1064. p->pbm_A.iommu = iommu;
  1065. iommu = kzalloc(sizeof(struct pci_iommu), GFP_ATOMIC);
  1066. if (!iommu)
  1067. goto fatal_memory_error;
  1068. p->pbm_B.iommu = iommu;
  1069. p->next = pci_controller_root;
  1070. pci_controller_root = p;
  1071. p->index = pci_num_controllers++;
  1072. p->pbms_same_domain = 0;
  1073. p->scan_bus = pci_sun4v_scan_bus;
  1074. #ifdef CONFIG_PCI_MSI
  1075. p->setup_msi_irq = pci_sun4v_setup_msi_irq;
  1076. p->teardown_msi_irq = pci_sun4v_teardown_msi_irq;
  1077. #endif
  1078. p->pci_ops = &pci_sun4v_ops;
  1079. /* Like PSYCHO and SCHIZO we have a 2GB aligned area
  1080. * for memory space.
  1081. */
  1082. pci_memspace_mask = 0x7fffffffUL;
  1083. pci_sun4v_pbm_init(p, dp, devhandle);
  1084. return;
  1085. fatal_memory_error:
  1086. prom_printf("SUN4V_PCI: Fatal memory allocation error.\n");
  1087. prom_halt();
  1088. }