pci_sun4v.c 27 KB

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