pci_sun4v.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  1. /* pci_sun4v.c: SUN4V specific PCI controller support.
  2. *
  3. * Copyright (C) 2006 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 <asm/pbm.h>
  13. #include <asm/iommu.h>
  14. #include <asm/irq.h>
  15. #include <asm/upa.h>
  16. #include <asm/pstate.h>
  17. #include <asm/oplib.h>
  18. #include <asm/hypervisor.h>
  19. #include <asm/prom.h>
  20. #include "pci_impl.h"
  21. #include "iommu_common.h"
  22. #include "pci_sun4v.h"
  23. #define PGLIST_NENTS (PAGE_SIZE / sizeof(u64))
  24. struct pci_iommu_batch {
  25. struct pci_dev *pdev; /* Device mapping is for. */
  26. unsigned long prot; /* IOMMU page protections */
  27. unsigned long entry; /* Index into IOTSB. */
  28. u64 *pglist; /* List of physical pages */
  29. unsigned long npages; /* Number of pages in list. */
  30. };
  31. static DEFINE_PER_CPU(struct pci_iommu_batch, pci_iommu_batch);
  32. /* Interrupts must be disabled. */
  33. static inline void pci_iommu_batch_start(struct pci_dev *pdev, unsigned long prot, unsigned long entry)
  34. {
  35. struct pci_iommu_batch *p = &__get_cpu_var(pci_iommu_batch);
  36. p->pdev = pdev;
  37. p->prot = prot;
  38. p->entry = entry;
  39. p->npages = 0;
  40. }
  41. /* Interrupts must be disabled. */
  42. static long pci_iommu_batch_flush(struct pci_iommu_batch *p)
  43. {
  44. struct pcidev_cookie *pcp = p->pdev->sysdata;
  45. unsigned long devhandle = pcp->pbm->devhandle;
  46. unsigned long prot = p->prot;
  47. unsigned long entry = p->entry;
  48. u64 *pglist = p->pglist;
  49. unsigned long npages = p->npages;
  50. while (npages != 0) {
  51. long num;
  52. num = pci_sun4v_iommu_map(devhandle, HV_PCI_TSBID(0, entry),
  53. npages, prot, __pa(pglist));
  54. if (unlikely(num < 0)) {
  55. if (printk_ratelimit())
  56. printk("pci_iommu_batch_flush: IOMMU map of "
  57. "[%08lx:%08lx:%lx:%lx:%lx] failed with "
  58. "status %ld\n",
  59. devhandle, HV_PCI_TSBID(0, entry),
  60. npages, prot, __pa(pglist), num);
  61. return -1;
  62. }
  63. entry += num;
  64. npages -= num;
  65. pglist += num;
  66. }
  67. p->entry = entry;
  68. p->npages = 0;
  69. return 0;
  70. }
  71. /* Interrupts must be disabled. */
  72. static inline long pci_iommu_batch_add(u64 phys_page)
  73. {
  74. struct pci_iommu_batch *p = &__get_cpu_var(pci_iommu_batch);
  75. BUG_ON(p->npages >= PGLIST_NENTS);
  76. p->pglist[p->npages++] = phys_page;
  77. if (p->npages == PGLIST_NENTS)
  78. return pci_iommu_batch_flush(p);
  79. return 0;
  80. }
  81. /* Interrupts must be disabled. */
  82. static inline long pci_iommu_batch_end(void)
  83. {
  84. struct pci_iommu_batch *p = &__get_cpu_var(pci_iommu_batch);
  85. BUG_ON(p->npages >= PGLIST_NENTS);
  86. return pci_iommu_batch_flush(p);
  87. }
  88. static long pci_arena_alloc(struct pci_iommu_arena *arena, unsigned long npages)
  89. {
  90. unsigned long n, i, start, end, limit;
  91. int pass;
  92. limit = arena->limit;
  93. start = arena->hint;
  94. pass = 0;
  95. again:
  96. n = find_next_zero_bit(arena->map, limit, start);
  97. end = n + npages;
  98. if (unlikely(end >= limit)) {
  99. if (likely(pass < 1)) {
  100. limit = start;
  101. start = 0;
  102. pass++;
  103. goto again;
  104. } else {
  105. /* Scanned the whole thing, give up. */
  106. return -1;
  107. }
  108. }
  109. for (i = n; i < end; i++) {
  110. if (test_bit(i, arena->map)) {
  111. start = i + 1;
  112. goto again;
  113. }
  114. }
  115. for (i = n; i < end; i++)
  116. __set_bit(i, arena->map);
  117. arena->hint = end;
  118. return n;
  119. }
  120. static void pci_arena_free(struct pci_iommu_arena *arena, unsigned long base, unsigned long npages)
  121. {
  122. unsigned long i;
  123. for (i = base; i < (base + npages); i++)
  124. __clear_bit(i, arena->map);
  125. }
  126. static void *pci_4v_alloc_consistent(struct pci_dev *pdev, size_t size, dma_addr_t *dma_addrp, gfp_t gfp)
  127. {
  128. struct pcidev_cookie *pcp;
  129. struct pci_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. pcp = pdev->sysdata;
  143. iommu = pcp->pbm->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 pcidev_cookie *pcp;
  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. pcp = pdev->sysdata;
  184. iommu = pcp->pbm->iommu;
  185. devhandle = pcp->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 pcidev_cookie *pcp;
  204. struct pci_iommu *iommu;
  205. unsigned long flags, npages, oaddr;
  206. unsigned long i, base_paddr;
  207. u32 bus_addr, ret;
  208. unsigned long prot;
  209. long entry;
  210. pcp = pdev->sysdata;
  211. iommu = pcp->pbm->iommu;
  212. if (unlikely(direction == PCI_DMA_NONE))
  213. goto bad;
  214. oaddr = (unsigned long)ptr;
  215. npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
  216. npages >>= IO_PAGE_SHIFT;
  217. spin_lock_irqsave(&iommu->lock, flags);
  218. entry = pci_arena_alloc(&iommu->arena, npages);
  219. spin_unlock_irqrestore(&iommu->lock, flags);
  220. if (unlikely(entry < 0L))
  221. goto bad;
  222. bus_addr = (iommu->page_table_map_base +
  223. (entry << IO_PAGE_SHIFT));
  224. ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
  225. base_paddr = __pa(oaddr & IO_PAGE_MASK);
  226. prot = HV_PCI_MAP_ATTR_READ;
  227. if (direction != PCI_DMA_TODEVICE)
  228. prot |= HV_PCI_MAP_ATTR_WRITE;
  229. local_irq_save(flags);
  230. pci_iommu_batch_start(pdev, prot, entry);
  231. for (i = 0; i < npages; i++, base_paddr += IO_PAGE_SIZE) {
  232. long err = pci_iommu_batch_add(base_paddr);
  233. if (unlikely(err < 0L))
  234. goto iommu_map_fail;
  235. }
  236. if (unlikely(pci_iommu_batch_end() < 0L))
  237. goto iommu_map_fail;
  238. local_irq_restore(flags);
  239. return ret;
  240. bad:
  241. if (printk_ratelimit())
  242. WARN_ON(1);
  243. return PCI_DMA_ERROR_CODE;
  244. iommu_map_fail:
  245. /* Interrupts are disabled. */
  246. spin_lock(&iommu->lock);
  247. pci_arena_free(&iommu->arena, entry, npages);
  248. spin_unlock_irqrestore(&iommu->lock, flags);
  249. return PCI_DMA_ERROR_CODE;
  250. }
  251. static void pci_4v_unmap_single(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
  252. {
  253. struct pcidev_cookie *pcp;
  254. struct pci_iommu *iommu;
  255. unsigned long flags, npages;
  256. long entry;
  257. u32 devhandle;
  258. if (unlikely(direction == PCI_DMA_NONE)) {
  259. if (printk_ratelimit())
  260. WARN_ON(1);
  261. return;
  262. }
  263. pcp = pdev->sysdata;
  264. iommu = pcp->pbm->iommu;
  265. devhandle = pcp->pbm->devhandle;
  266. npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
  267. npages >>= IO_PAGE_SHIFT;
  268. bus_addr &= IO_PAGE_MASK;
  269. spin_lock_irqsave(&iommu->lock, flags);
  270. entry = (bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT;
  271. pci_arena_free(&iommu->arena, entry, npages);
  272. do {
  273. unsigned long num;
  274. num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
  275. npages);
  276. entry += num;
  277. npages -= num;
  278. } while (npages != 0);
  279. spin_unlock_irqrestore(&iommu->lock, flags);
  280. }
  281. #define SG_ENT_PHYS_ADDRESS(SG) \
  282. (__pa(page_address((SG)->page)) + (SG)->offset)
  283. static inline long fill_sg(long entry, struct pci_dev *pdev,
  284. struct scatterlist *sg,
  285. int nused, int nelems, unsigned long prot)
  286. {
  287. struct scatterlist *dma_sg = sg;
  288. struct scatterlist *sg_end = sg + nelems;
  289. unsigned long flags;
  290. int i;
  291. local_irq_save(flags);
  292. pci_iommu_batch_start(pdev, prot, entry);
  293. for (i = 0; i < nused; i++) {
  294. unsigned long pteval = ~0UL;
  295. u32 dma_npages;
  296. dma_npages = ((dma_sg->dma_address & (IO_PAGE_SIZE - 1UL)) +
  297. dma_sg->dma_length +
  298. ((IO_PAGE_SIZE - 1UL))) >> IO_PAGE_SHIFT;
  299. do {
  300. unsigned long offset;
  301. signed int len;
  302. /* If we are here, we know we have at least one
  303. * more page to map. So walk forward until we
  304. * hit a page crossing, and begin creating new
  305. * mappings from that spot.
  306. */
  307. for (;;) {
  308. unsigned long tmp;
  309. tmp = SG_ENT_PHYS_ADDRESS(sg);
  310. len = sg->length;
  311. if (((tmp ^ pteval) >> IO_PAGE_SHIFT) != 0UL) {
  312. pteval = tmp & IO_PAGE_MASK;
  313. offset = tmp & (IO_PAGE_SIZE - 1UL);
  314. break;
  315. }
  316. if (((tmp ^ (tmp + len - 1UL)) >> IO_PAGE_SHIFT) != 0UL) {
  317. pteval = (tmp + IO_PAGE_SIZE) & IO_PAGE_MASK;
  318. offset = 0UL;
  319. len -= (IO_PAGE_SIZE - (tmp & (IO_PAGE_SIZE - 1UL)));
  320. break;
  321. }
  322. sg++;
  323. }
  324. pteval = (pteval & IOPTE_PAGE);
  325. while (len > 0) {
  326. long err;
  327. err = pci_iommu_batch_add(pteval);
  328. if (unlikely(err < 0L))
  329. goto iommu_map_failed;
  330. pteval += IO_PAGE_SIZE;
  331. len -= (IO_PAGE_SIZE - offset);
  332. offset = 0;
  333. dma_npages--;
  334. }
  335. pteval = (pteval & IOPTE_PAGE) + len;
  336. sg++;
  337. /* Skip over any tail mappings we've fully mapped,
  338. * adjusting pteval along the way. Stop when we
  339. * detect a page crossing event.
  340. */
  341. while (sg < sg_end &&
  342. (pteval << (64 - IO_PAGE_SHIFT)) != 0UL &&
  343. (pteval == SG_ENT_PHYS_ADDRESS(sg)) &&
  344. ((pteval ^
  345. (SG_ENT_PHYS_ADDRESS(sg) + sg->length - 1UL)) >> IO_PAGE_SHIFT) == 0UL) {
  346. pteval += sg->length;
  347. sg++;
  348. }
  349. if ((pteval << (64 - IO_PAGE_SHIFT)) == 0UL)
  350. pteval = ~0UL;
  351. } while (dma_npages != 0);
  352. dma_sg++;
  353. }
  354. if (unlikely(pci_iommu_batch_end() < 0L))
  355. goto iommu_map_failed;
  356. local_irq_restore(flags);
  357. return 0;
  358. iommu_map_failed:
  359. local_irq_restore(flags);
  360. return -1L;
  361. }
  362. static int pci_4v_map_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
  363. {
  364. struct pcidev_cookie *pcp;
  365. struct pci_iommu *iommu;
  366. unsigned long flags, npages, prot;
  367. u32 dma_base;
  368. struct scatterlist *sgtmp;
  369. long entry, err;
  370. int used;
  371. /* Fast path single entry scatterlists. */
  372. if (nelems == 1) {
  373. sglist->dma_address =
  374. pci_4v_map_single(pdev,
  375. (page_address(sglist->page) + sglist->offset),
  376. sglist->length, direction);
  377. if (unlikely(sglist->dma_address == PCI_DMA_ERROR_CODE))
  378. return 0;
  379. sglist->dma_length = sglist->length;
  380. return 1;
  381. }
  382. pcp = pdev->sysdata;
  383. iommu = pcp->pbm->iommu;
  384. if (unlikely(direction == PCI_DMA_NONE))
  385. goto bad;
  386. /* Step 1: Prepare scatter list. */
  387. npages = prepare_sg(sglist, nelems);
  388. /* Step 2: Allocate a cluster and context, if necessary. */
  389. spin_lock_irqsave(&iommu->lock, flags);
  390. entry = pci_arena_alloc(&iommu->arena, npages);
  391. spin_unlock_irqrestore(&iommu->lock, flags);
  392. if (unlikely(entry < 0L))
  393. goto bad;
  394. dma_base = iommu->page_table_map_base +
  395. (entry << IO_PAGE_SHIFT);
  396. /* Step 3: Normalize DMA addresses. */
  397. used = nelems;
  398. sgtmp = sglist;
  399. while (used && sgtmp->dma_length) {
  400. sgtmp->dma_address += dma_base;
  401. sgtmp++;
  402. used--;
  403. }
  404. used = nelems - used;
  405. /* Step 4: Create the mappings. */
  406. prot = HV_PCI_MAP_ATTR_READ;
  407. if (direction != PCI_DMA_TODEVICE)
  408. prot |= HV_PCI_MAP_ATTR_WRITE;
  409. err = fill_sg(entry, pdev, sglist, used, nelems, prot);
  410. if (unlikely(err < 0L))
  411. goto iommu_map_failed;
  412. return used;
  413. bad:
  414. if (printk_ratelimit())
  415. WARN_ON(1);
  416. return 0;
  417. iommu_map_failed:
  418. spin_lock_irqsave(&iommu->lock, flags);
  419. pci_arena_free(&iommu->arena, entry, npages);
  420. spin_unlock_irqrestore(&iommu->lock, flags);
  421. return 0;
  422. }
  423. static void pci_4v_unmap_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
  424. {
  425. struct pcidev_cookie *pcp;
  426. struct pci_iommu *iommu;
  427. unsigned long flags, i, npages;
  428. long entry;
  429. u32 devhandle, bus_addr;
  430. if (unlikely(direction == PCI_DMA_NONE)) {
  431. if (printk_ratelimit())
  432. WARN_ON(1);
  433. }
  434. pcp = pdev->sysdata;
  435. iommu = pcp->pbm->iommu;
  436. devhandle = pcp->pbm->devhandle;
  437. bus_addr = sglist->dma_address & IO_PAGE_MASK;
  438. for (i = 1; i < nelems; i++)
  439. if (sglist[i].dma_length == 0)
  440. break;
  441. i--;
  442. npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length) -
  443. bus_addr) >> IO_PAGE_SHIFT;
  444. entry = ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  445. spin_lock_irqsave(&iommu->lock, flags);
  446. pci_arena_free(&iommu->arena, entry, npages);
  447. do {
  448. unsigned long num;
  449. num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
  450. npages);
  451. entry += num;
  452. npages -= num;
  453. } while (npages != 0);
  454. spin_unlock_irqrestore(&iommu->lock, flags);
  455. }
  456. static void pci_4v_dma_sync_single_for_cpu(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
  457. {
  458. /* Nothing to do... */
  459. }
  460. static void pci_4v_dma_sync_sg_for_cpu(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
  461. {
  462. /* Nothing to do... */
  463. }
  464. struct pci_iommu_ops pci_sun4v_iommu_ops = {
  465. .alloc_consistent = pci_4v_alloc_consistent,
  466. .free_consistent = pci_4v_free_consistent,
  467. .map_single = pci_4v_map_single,
  468. .unmap_single = pci_4v_unmap_single,
  469. .map_sg = pci_4v_map_sg,
  470. .unmap_sg = pci_4v_unmap_sg,
  471. .dma_sync_single_for_cpu = pci_4v_dma_sync_single_for_cpu,
  472. .dma_sync_sg_for_cpu = pci_4v_dma_sync_sg_for_cpu,
  473. };
  474. /* SUN4V PCI configuration space accessors. */
  475. struct pdev_entry {
  476. struct pdev_entry *next;
  477. u32 devhandle;
  478. unsigned int bus;
  479. unsigned int device;
  480. unsigned int func;
  481. };
  482. #define PDEV_HTAB_SIZE 16
  483. #define PDEV_HTAB_MASK (PDEV_HTAB_SIZE - 1)
  484. static struct pdev_entry *pdev_htab[PDEV_HTAB_SIZE];
  485. static inline unsigned int pdev_hashfn(u32 devhandle, unsigned int bus, unsigned int device, unsigned int func)
  486. {
  487. unsigned int val;
  488. val = (devhandle ^ (devhandle >> 4));
  489. val ^= bus;
  490. val ^= device;
  491. val ^= func;
  492. return val & PDEV_HTAB_MASK;
  493. }
  494. static int pdev_htab_add(u32 devhandle, unsigned int bus, unsigned int device, unsigned int func)
  495. {
  496. struct pdev_entry *p = kmalloc(sizeof(*p), GFP_KERNEL);
  497. struct pdev_entry **slot;
  498. if (!p)
  499. return -ENOMEM;
  500. slot = &pdev_htab[pdev_hashfn(devhandle, bus, device, func)];
  501. p->next = *slot;
  502. *slot = p;
  503. p->devhandle = devhandle;
  504. p->bus = bus;
  505. p->device = device;
  506. p->func = func;
  507. return 0;
  508. }
  509. /* Recursively descend into the OBP device tree, rooted at toplevel_node,
  510. * looking for a PCI device matching bus and devfn.
  511. */
  512. static int obp_find(struct device_node *toplevel_node, unsigned int bus, unsigned int devfn)
  513. {
  514. toplevel_node = toplevel_node->child;
  515. while (toplevel_node != NULL) {
  516. struct linux_prom_pci_registers *regs;
  517. struct property *prop;
  518. int ret;
  519. ret = obp_find(toplevel_node, bus, devfn);
  520. if (ret != 0)
  521. return ret;
  522. prop = of_find_property(toplevel_node, "reg", NULL);
  523. if (!prop)
  524. goto next_sibling;
  525. regs = prop->value;
  526. if (((regs->phys_hi >> 16) & 0xff) == bus &&
  527. ((regs->phys_hi >> 8) & 0xff) == devfn)
  528. break;
  529. next_sibling:
  530. toplevel_node = toplevel_node->sibling;
  531. }
  532. return toplevel_node != NULL;
  533. }
  534. static int pdev_htab_populate(struct pci_pbm_info *pbm)
  535. {
  536. u32 devhandle = pbm->devhandle;
  537. unsigned int bus;
  538. for (bus = pbm->pci_first_busno; bus <= pbm->pci_last_busno; bus++) {
  539. unsigned int devfn;
  540. for (devfn = 0; devfn < 256; devfn++) {
  541. unsigned int device = PCI_SLOT(devfn);
  542. unsigned int func = PCI_FUNC(devfn);
  543. if (obp_find(pbm->prom_node, bus, devfn)) {
  544. int err = pdev_htab_add(devhandle, bus,
  545. device, func);
  546. if (err)
  547. return err;
  548. }
  549. }
  550. }
  551. return 0;
  552. }
  553. static struct pdev_entry *pdev_find(u32 devhandle, unsigned int bus, unsigned int device, unsigned int func)
  554. {
  555. struct pdev_entry *p;
  556. p = pdev_htab[pdev_hashfn(devhandle, bus, device, func)];
  557. while (p) {
  558. if (p->devhandle == devhandle &&
  559. p->bus == bus &&
  560. p->device == device &&
  561. p->func == func)
  562. break;
  563. p = p->next;
  564. }
  565. return p;
  566. }
  567. static inline int pci_sun4v_out_of_range(struct pci_pbm_info *pbm, unsigned int bus, unsigned int device, unsigned int func)
  568. {
  569. if (bus < pbm->pci_first_busno ||
  570. bus > pbm->pci_last_busno)
  571. return 1;
  572. return pdev_find(pbm->devhandle, bus, device, func) == NULL;
  573. }
  574. static int pci_sun4v_read_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
  575. int where, int size, u32 *value)
  576. {
  577. struct pci_pbm_info *pbm = bus_dev->sysdata;
  578. u32 devhandle = pbm->devhandle;
  579. unsigned int bus = bus_dev->number;
  580. unsigned int device = PCI_SLOT(devfn);
  581. unsigned int func = PCI_FUNC(devfn);
  582. unsigned long ret;
  583. if (pci_sun4v_out_of_range(pbm, bus, device, func)) {
  584. ret = ~0UL;
  585. } else {
  586. ret = pci_sun4v_config_get(devhandle,
  587. HV_PCI_DEVICE_BUILD(bus, device, func),
  588. where, size);
  589. #if 0
  590. printk("rcfg: [%x:%x:%x:%d]=[%lx]\n",
  591. devhandle, HV_PCI_DEVICE_BUILD(bus, device, func),
  592. where, size, ret);
  593. #endif
  594. }
  595. switch (size) {
  596. case 1:
  597. *value = ret & 0xff;
  598. break;
  599. case 2:
  600. *value = ret & 0xffff;
  601. break;
  602. case 4:
  603. *value = ret & 0xffffffff;
  604. break;
  605. };
  606. return PCIBIOS_SUCCESSFUL;
  607. }
  608. static int pci_sun4v_write_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
  609. int where, int size, u32 value)
  610. {
  611. struct pci_pbm_info *pbm = bus_dev->sysdata;
  612. u32 devhandle = pbm->devhandle;
  613. unsigned int bus = bus_dev->number;
  614. unsigned int device = PCI_SLOT(devfn);
  615. unsigned int func = PCI_FUNC(devfn);
  616. unsigned long ret;
  617. if (pci_sun4v_out_of_range(pbm, bus, device, func)) {
  618. /* Do nothing. */
  619. } else {
  620. ret = pci_sun4v_config_put(devhandle,
  621. HV_PCI_DEVICE_BUILD(bus, device, func),
  622. where, size, value);
  623. #if 0
  624. printk("wcfg: [%x:%x:%x:%d] v[%x] == [%lx]\n",
  625. devhandle, HV_PCI_DEVICE_BUILD(bus, device, func),
  626. where, size, value, ret);
  627. #endif
  628. }
  629. return PCIBIOS_SUCCESSFUL;
  630. }
  631. static struct pci_ops pci_sun4v_ops = {
  632. .read = pci_sun4v_read_pci_cfg,
  633. .write = pci_sun4v_write_pci_cfg,
  634. };
  635. static void pbm_scan_bus(struct pci_controller_info *p,
  636. struct pci_pbm_info *pbm)
  637. {
  638. struct pcidev_cookie *cookie = kmalloc(sizeof(*cookie), GFP_KERNEL);
  639. if (!cookie) {
  640. prom_printf("%s: Critical allocation failure.\n", pbm->name);
  641. prom_halt();
  642. }
  643. /* All we care about is the PBM. */
  644. memset(cookie, 0, sizeof(*cookie));
  645. cookie->pbm = pbm;
  646. pbm->pci_bus = pci_scan_bus(pbm->pci_first_busno, p->pci_ops, pbm);
  647. #if 0
  648. pci_fixup_host_bridge_self(pbm->pci_bus);
  649. pbm->pci_bus->self->sysdata = cookie;
  650. #endif
  651. pci_fill_in_pbm_cookies(pbm->pci_bus, pbm, pbm->prom_node);
  652. pci_record_assignments(pbm, pbm->pci_bus);
  653. pci_assign_unassigned(pbm, pbm->pci_bus);
  654. pci_fixup_irq(pbm, pbm->pci_bus);
  655. pci_determine_66mhz_disposition(pbm, pbm->pci_bus);
  656. pci_setup_busmastering(pbm, pbm->pci_bus);
  657. }
  658. static void pci_sun4v_scan_bus(struct pci_controller_info *p)
  659. {
  660. struct property *prop;
  661. struct device_node *dp;
  662. if ((dp = p->pbm_A.prom_node) != NULL) {
  663. prop = of_find_property(dp, "66mhz-capable", NULL);
  664. p->pbm_A.is_66mhz_capable = (prop != NULL);
  665. pbm_scan_bus(p, &p->pbm_A);
  666. }
  667. if ((dp = p->pbm_B.prom_node) != NULL) {
  668. prop = of_find_property(dp, "66mhz-capable", NULL);
  669. p->pbm_B.is_66mhz_capable = (prop != NULL);
  670. pbm_scan_bus(p, &p->pbm_B);
  671. }
  672. /* XXX register error interrupt handlers XXX */
  673. }
  674. static unsigned int pci_sun4v_irq_build(struct pci_pbm_info *pbm,
  675. struct pci_dev *pdev,
  676. unsigned int devino)
  677. {
  678. u32 devhandle = pbm->devhandle;
  679. return sun4v_build_irq(devhandle, devino);
  680. }
  681. static void pci_sun4v_base_address_update(struct pci_dev *pdev, int resource)
  682. {
  683. struct pcidev_cookie *pcp = pdev->sysdata;
  684. struct pci_pbm_info *pbm = pcp->pbm;
  685. struct resource *res, *root;
  686. u32 reg;
  687. int where, size, is_64bit;
  688. res = &pdev->resource[resource];
  689. if (resource < 6) {
  690. where = PCI_BASE_ADDRESS_0 + (resource * 4);
  691. } else if (resource == PCI_ROM_RESOURCE) {
  692. where = pdev->rom_base_reg;
  693. } else {
  694. /* Somebody might have asked allocation of a non-standard resource */
  695. return;
  696. }
  697. /* XXX 64-bit MEM handling is not %100 correct... XXX */
  698. is_64bit = 0;
  699. if (res->flags & IORESOURCE_IO)
  700. root = &pbm->io_space;
  701. else {
  702. root = &pbm->mem_space;
  703. if ((res->flags & PCI_BASE_ADDRESS_MEM_TYPE_MASK)
  704. == PCI_BASE_ADDRESS_MEM_TYPE_64)
  705. is_64bit = 1;
  706. }
  707. size = res->end - res->start;
  708. pci_read_config_dword(pdev, where, &reg);
  709. reg = ((reg & size) |
  710. (((u32)(res->start - root->start)) & ~size));
  711. if (resource == PCI_ROM_RESOURCE) {
  712. reg |= PCI_ROM_ADDRESS_ENABLE;
  713. res->flags |= IORESOURCE_ROM_ENABLE;
  714. }
  715. pci_write_config_dword(pdev, where, reg);
  716. /* This knows that the upper 32-bits of the address
  717. * must be zero. Our PCI common layer enforces this.
  718. */
  719. if (is_64bit)
  720. pci_write_config_dword(pdev, where + 4, 0);
  721. }
  722. static void pci_sun4v_resource_adjust(struct pci_dev *pdev,
  723. struct resource *res,
  724. struct resource *root)
  725. {
  726. res->start += root->start;
  727. res->end += root->start;
  728. }
  729. /* Use ranges property to determine where PCI MEM, I/O, and Config
  730. * space are for this PCI bus module.
  731. */
  732. static void pci_sun4v_determine_mem_io_space(struct pci_pbm_info *pbm)
  733. {
  734. int i, saw_mem, saw_io;
  735. saw_mem = saw_io = 0;
  736. for (i = 0; i < pbm->num_pbm_ranges; i++) {
  737. struct linux_prom_pci_ranges *pr = &pbm->pbm_ranges[i];
  738. unsigned long a;
  739. int type;
  740. type = (pr->child_phys_hi >> 24) & 0x3;
  741. a = (((unsigned long)pr->parent_phys_hi << 32UL) |
  742. ((unsigned long)pr->parent_phys_lo << 0UL));
  743. switch (type) {
  744. case 1:
  745. /* 16-bit IO space, 16MB */
  746. pbm->io_space.start = a;
  747. pbm->io_space.end = a + ((16UL*1024UL*1024UL) - 1UL);
  748. pbm->io_space.flags = IORESOURCE_IO;
  749. saw_io = 1;
  750. break;
  751. case 2:
  752. /* 32-bit MEM space, 2GB */
  753. pbm->mem_space.start = a;
  754. pbm->mem_space.end = a + (0x80000000UL - 1UL);
  755. pbm->mem_space.flags = IORESOURCE_MEM;
  756. saw_mem = 1;
  757. break;
  758. case 3:
  759. /* XXX 64-bit MEM handling XXX */
  760. default:
  761. break;
  762. };
  763. }
  764. if (!saw_io || !saw_mem) {
  765. prom_printf("%s: Fatal error, missing %s PBM range.\n",
  766. pbm->name,
  767. (!saw_io ? "IO" : "MEM"));
  768. prom_halt();
  769. }
  770. printk("%s: PCI IO[%lx] MEM[%lx]\n",
  771. pbm->name,
  772. pbm->io_space.start,
  773. pbm->mem_space.start);
  774. }
  775. static void pbm_register_toplevel_resources(struct pci_controller_info *p,
  776. struct pci_pbm_info *pbm)
  777. {
  778. pbm->io_space.name = pbm->mem_space.name = pbm->name;
  779. request_resource(&ioport_resource, &pbm->io_space);
  780. request_resource(&iomem_resource, &pbm->mem_space);
  781. pci_register_legacy_regions(&pbm->io_space,
  782. &pbm->mem_space);
  783. }
  784. static unsigned long probe_existing_entries(struct pci_pbm_info *pbm,
  785. struct pci_iommu *iommu)
  786. {
  787. struct pci_iommu_arena *arena = &iommu->arena;
  788. unsigned long i, cnt = 0;
  789. u32 devhandle;
  790. devhandle = pbm->devhandle;
  791. for (i = 0; i < arena->limit; i++) {
  792. unsigned long ret, io_attrs, ra;
  793. ret = pci_sun4v_iommu_getmap(devhandle,
  794. HV_PCI_TSBID(0, i),
  795. &io_attrs, &ra);
  796. if (ret == HV_EOK) {
  797. if (page_in_phys_avail(ra)) {
  798. pci_sun4v_iommu_demap(devhandle,
  799. HV_PCI_TSBID(0, i), 1);
  800. } else {
  801. cnt++;
  802. __set_bit(i, arena->map);
  803. }
  804. }
  805. }
  806. return cnt;
  807. }
  808. static void pci_sun4v_iommu_init(struct pci_pbm_info *pbm)
  809. {
  810. struct pci_iommu *iommu = pbm->iommu;
  811. struct property *prop;
  812. unsigned long num_tsb_entries, sz;
  813. u32 vdma[2], dma_mask, dma_offset;
  814. int tsbsize;
  815. prop = of_find_property(pbm->prom_node, "virtual-dma", NULL);
  816. if (prop) {
  817. u32 *val = prop->value;
  818. vdma[0] = val[0];
  819. vdma[1] = val[1];
  820. } else {
  821. /* No property, use default values. */
  822. vdma[0] = 0x80000000;
  823. vdma[1] = 0x80000000;
  824. }
  825. dma_mask = vdma[0];
  826. switch (vdma[1]) {
  827. case 0x20000000:
  828. dma_mask |= 0x1fffffff;
  829. tsbsize = 64;
  830. break;
  831. case 0x40000000:
  832. dma_mask |= 0x3fffffff;
  833. tsbsize = 128;
  834. break;
  835. case 0x80000000:
  836. dma_mask |= 0x7fffffff;
  837. tsbsize = 256;
  838. break;
  839. default:
  840. prom_printf("PCI-SUN4V: strange virtual-dma size.\n");
  841. prom_halt();
  842. };
  843. tsbsize *= (8 * 1024);
  844. num_tsb_entries = tsbsize / sizeof(iopte_t);
  845. dma_offset = vdma[0];
  846. /* Setup initial software IOMMU state. */
  847. spin_lock_init(&iommu->lock);
  848. iommu->ctx_lowest_free = 1;
  849. iommu->page_table_map_base = dma_offset;
  850. iommu->dma_addr_mask = dma_mask;
  851. /* Allocate and initialize the free area map. */
  852. sz = num_tsb_entries / 8;
  853. sz = (sz + 7UL) & ~7UL;
  854. iommu->arena.map = kmalloc(sz, GFP_KERNEL);
  855. if (!iommu->arena.map) {
  856. prom_printf("PCI_IOMMU: Error, kmalloc(arena.map) failed.\n");
  857. prom_halt();
  858. }
  859. memset(iommu->arena.map, 0, sz);
  860. iommu->arena.limit = num_tsb_entries;
  861. sz = probe_existing_entries(pbm, iommu);
  862. if (sz)
  863. printk("%s: Imported %lu TSB entries from OBP\n",
  864. pbm->name, sz);
  865. }
  866. static void pci_sun4v_get_bus_range(struct pci_pbm_info *pbm)
  867. {
  868. struct property *prop;
  869. unsigned int *busrange;
  870. prop = of_find_property(pbm->prom_node, "bus-range", NULL);
  871. busrange = prop->value;
  872. pbm->pci_first_busno = busrange[0];
  873. pbm->pci_last_busno = busrange[1];
  874. }
  875. static void pci_sun4v_pbm_init(struct pci_controller_info *p, struct device_node *dp, u32 devhandle)
  876. {
  877. struct pci_pbm_info *pbm;
  878. struct property *prop;
  879. int len, i;
  880. if (devhandle & 0x40)
  881. pbm = &p->pbm_B;
  882. else
  883. pbm = &p->pbm_A;
  884. pbm->parent = p;
  885. pbm->prom_node = dp;
  886. pbm->pci_first_slot = 1;
  887. pbm->devhandle = devhandle;
  888. pbm->name = dp->full_name;
  889. printk("%s: SUN4V PCI Bus Module\n", pbm->name);
  890. prop = of_find_property(dp, "ranges", &len);
  891. pbm->pbm_ranges = prop->value;
  892. pbm->num_pbm_ranges =
  893. (len / sizeof(struct linux_prom_pci_ranges));
  894. /* Mask out the top 8 bits of the ranges, leaving the real
  895. * physical address.
  896. */
  897. for (i = 0; i < pbm->num_pbm_ranges; i++)
  898. pbm->pbm_ranges[i].parent_phys_hi &= 0x0fffffff;
  899. pci_sun4v_determine_mem_io_space(pbm);
  900. pbm_register_toplevel_resources(p, pbm);
  901. prop = of_find_property(dp, "interrupt-map", &len);
  902. pbm->pbm_intmap = prop->value;
  903. pbm->num_pbm_intmap =
  904. (len / sizeof(struct linux_prom_pci_intmap));
  905. prop = of_find_property(dp, "interrupt-map-mask", NULL);
  906. pbm->pbm_intmask = prop->value;
  907. pci_sun4v_get_bus_range(pbm);
  908. pci_sun4v_iommu_init(pbm);
  909. pdev_htab_populate(pbm);
  910. }
  911. void sun4v_pci_init(struct device_node *dp, char *model_name)
  912. {
  913. struct pci_controller_info *p;
  914. struct pci_iommu *iommu;
  915. struct property *prop;
  916. struct linux_prom64_registers *regs;
  917. u32 devhandle;
  918. int i;
  919. prop = of_find_property(dp, "reg", NULL);
  920. regs = prop->value;
  921. devhandle = (regs->phys_addr >> 32UL) & 0x0fffffff;
  922. for (p = pci_controller_root; p; p = p->next) {
  923. struct pci_pbm_info *pbm;
  924. if (p->pbm_A.prom_node && p->pbm_B.prom_node)
  925. continue;
  926. pbm = (p->pbm_A.prom_node ?
  927. &p->pbm_A :
  928. &p->pbm_B);
  929. if (pbm->devhandle == (devhandle ^ 0x40)) {
  930. pci_sun4v_pbm_init(p, dp, devhandle);
  931. return;
  932. }
  933. }
  934. for_each_possible_cpu(i) {
  935. unsigned long page = get_zeroed_page(GFP_ATOMIC);
  936. if (!page)
  937. goto fatal_memory_error;
  938. per_cpu(pci_iommu_batch, i).pglist = (u64 *) page;
  939. }
  940. p = kmalloc(sizeof(struct pci_controller_info), GFP_ATOMIC);
  941. if (!p)
  942. goto fatal_memory_error;
  943. memset(p, 0, sizeof(*p));
  944. iommu = kmalloc(sizeof(struct pci_iommu), GFP_ATOMIC);
  945. if (!iommu)
  946. goto fatal_memory_error;
  947. memset(iommu, 0, sizeof(*iommu));
  948. p->pbm_A.iommu = iommu;
  949. iommu = kmalloc(sizeof(struct pci_iommu), GFP_ATOMIC);
  950. if (!iommu)
  951. goto fatal_memory_error;
  952. memset(iommu, 0, sizeof(*iommu));
  953. p->pbm_B.iommu = iommu;
  954. p->next = pci_controller_root;
  955. pci_controller_root = p;
  956. p->index = pci_num_controllers++;
  957. p->pbms_same_domain = 0;
  958. p->scan_bus = pci_sun4v_scan_bus;
  959. p->irq_build = pci_sun4v_irq_build;
  960. p->base_address_update = pci_sun4v_base_address_update;
  961. p->resource_adjust = pci_sun4v_resource_adjust;
  962. p->pci_ops = &pci_sun4v_ops;
  963. /* Like PSYCHO and SCHIZO we have a 2GB aligned area
  964. * for memory space.
  965. */
  966. pci_memspace_mask = 0x7fffffffUL;
  967. pci_sun4v_pbm_init(p, dp, devhandle);
  968. return;
  969. fatal_memory_error:
  970. prom_printf("SUN4V_PCI: Fatal memory allocation error.\n");
  971. prom_halt();
  972. }