pci_sun4v.c 34 KB

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