pci_sun4v.c 29 KB

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