pci_sun4v.c 31 KB

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