pci_sun4v.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  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 = kzalloc(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. cookie->pbm = pbm;
  645. pbm->pci_bus = pci_scan_bus(pbm->pci_first_busno, p->pci_ops, pbm);
  646. #if 0
  647. pci_fixup_host_bridge_self(pbm->pci_bus);
  648. pbm->pci_bus->self->sysdata = cookie;
  649. #endif
  650. pci_fill_in_pbm_cookies(pbm->pci_bus, pbm, pbm->prom_node);
  651. pci_record_assignments(pbm, pbm->pci_bus);
  652. pci_assign_unassigned(pbm, pbm->pci_bus);
  653. pci_fixup_irq(pbm, pbm->pci_bus);
  654. pci_determine_66mhz_disposition(pbm, pbm->pci_bus);
  655. pci_setup_busmastering(pbm, pbm->pci_bus);
  656. }
  657. static void pci_sun4v_scan_bus(struct pci_controller_info *p)
  658. {
  659. struct property *prop;
  660. struct device_node *dp;
  661. if ((dp = p->pbm_A.prom_node) != NULL) {
  662. prop = of_find_property(dp, "66mhz-capable", NULL);
  663. p->pbm_A.is_66mhz_capable = (prop != NULL);
  664. pbm_scan_bus(p, &p->pbm_A);
  665. }
  666. if ((dp = p->pbm_B.prom_node) != NULL) {
  667. prop = of_find_property(dp, "66mhz-capable", NULL);
  668. p->pbm_B.is_66mhz_capable = (prop != NULL);
  669. pbm_scan_bus(p, &p->pbm_B);
  670. }
  671. /* XXX register error interrupt handlers XXX */
  672. }
  673. static void pci_sun4v_base_address_update(struct pci_dev *pdev, int resource)
  674. {
  675. struct pcidev_cookie *pcp = pdev->sysdata;
  676. struct pci_pbm_info *pbm = pcp->pbm;
  677. struct resource *res, *root;
  678. u32 reg;
  679. int where, size, is_64bit;
  680. res = &pdev->resource[resource];
  681. if (resource < 6) {
  682. where = PCI_BASE_ADDRESS_0 + (resource * 4);
  683. } else if (resource == PCI_ROM_RESOURCE) {
  684. where = pdev->rom_base_reg;
  685. } else {
  686. /* Somebody might have asked allocation of a non-standard resource */
  687. return;
  688. }
  689. /* XXX 64-bit MEM handling is not %100 correct... XXX */
  690. is_64bit = 0;
  691. if (res->flags & IORESOURCE_IO)
  692. root = &pbm->io_space;
  693. else {
  694. root = &pbm->mem_space;
  695. if ((res->flags & PCI_BASE_ADDRESS_MEM_TYPE_MASK)
  696. == PCI_BASE_ADDRESS_MEM_TYPE_64)
  697. is_64bit = 1;
  698. }
  699. size = res->end - res->start;
  700. pci_read_config_dword(pdev, where, &reg);
  701. reg = ((reg & size) |
  702. (((u32)(res->start - root->start)) & ~size));
  703. if (resource == PCI_ROM_RESOURCE) {
  704. reg |= PCI_ROM_ADDRESS_ENABLE;
  705. res->flags |= IORESOURCE_ROM_ENABLE;
  706. }
  707. pci_write_config_dword(pdev, where, reg);
  708. /* This knows that the upper 32-bits of the address
  709. * must be zero. Our PCI common layer enforces this.
  710. */
  711. if (is_64bit)
  712. pci_write_config_dword(pdev, where + 4, 0);
  713. }
  714. static void pci_sun4v_resource_adjust(struct pci_dev *pdev,
  715. struct resource *res,
  716. struct resource *root)
  717. {
  718. res->start += root->start;
  719. res->end += root->start;
  720. }
  721. /* Use ranges property to determine where PCI MEM, I/O, and Config
  722. * space are for this PCI bus module.
  723. */
  724. static void pci_sun4v_determine_mem_io_space(struct pci_pbm_info *pbm)
  725. {
  726. int i, saw_mem, saw_io;
  727. saw_mem = saw_io = 0;
  728. for (i = 0; i < pbm->num_pbm_ranges; i++) {
  729. struct linux_prom_pci_ranges *pr = &pbm->pbm_ranges[i];
  730. unsigned long a;
  731. int type;
  732. type = (pr->child_phys_hi >> 24) & 0x3;
  733. a = (((unsigned long)pr->parent_phys_hi << 32UL) |
  734. ((unsigned long)pr->parent_phys_lo << 0UL));
  735. switch (type) {
  736. case 1:
  737. /* 16-bit IO space, 16MB */
  738. pbm->io_space.start = a;
  739. pbm->io_space.end = a + ((16UL*1024UL*1024UL) - 1UL);
  740. pbm->io_space.flags = IORESOURCE_IO;
  741. saw_io = 1;
  742. break;
  743. case 2:
  744. /* 32-bit MEM space, 2GB */
  745. pbm->mem_space.start = a;
  746. pbm->mem_space.end = a + (0x80000000UL - 1UL);
  747. pbm->mem_space.flags = IORESOURCE_MEM;
  748. saw_mem = 1;
  749. break;
  750. case 3:
  751. /* XXX 64-bit MEM handling XXX */
  752. default:
  753. break;
  754. };
  755. }
  756. if (!saw_io || !saw_mem) {
  757. prom_printf("%s: Fatal error, missing %s PBM range.\n",
  758. pbm->name,
  759. (!saw_io ? "IO" : "MEM"));
  760. prom_halt();
  761. }
  762. printk("%s: PCI IO[%lx] MEM[%lx]\n",
  763. pbm->name,
  764. pbm->io_space.start,
  765. pbm->mem_space.start);
  766. }
  767. static void pbm_register_toplevel_resources(struct pci_controller_info *p,
  768. struct pci_pbm_info *pbm)
  769. {
  770. pbm->io_space.name = pbm->mem_space.name = pbm->name;
  771. request_resource(&ioport_resource, &pbm->io_space);
  772. request_resource(&iomem_resource, &pbm->mem_space);
  773. pci_register_legacy_regions(&pbm->io_space,
  774. &pbm->mem_space);
  775. }
  776. static unsigned long probe_existing_entries(struct pci_pbm_info *pbm,
  777. struct pci_iommu *iommu)
  778. {
  779. struct pci_iommu_arena *arena = &iommu->arena;
  780. unsigned long i, cnt = 0;
  781. u32 devhandle;
  782. devhandle = pbm->devhandle;
  783. for (i = 0; i < arena->limit; i++) {
  784. unsigned long ret, io_attrs, ra;
  785. ret = pci_sun4v_iommu_getmap(devhandle,
  786. HV_PCI_TSBID(0, i),
  787. &io_attrs, &ra);
  788. if (ret == HV_EOK) {
  789. if (page_in_phys_avail(ra)) {
  790. pci_sun4v_iommu_demap(devhandle,
  791. HV_PCI_TSBID(0, i), 1);
  792. } else {
  793. cnt++;
  794. __set_bit(i, arena->map);
  795. }
  796. }
  797. }
  798. return cnt;
  799. }
  800. static void pci_sun4v_iommu_init(struct pci_pbm_info *pbm)
  801. {
  802. struct pci_iommu *iommu = pbm->iommu;
  803. struct property *prop;
  804. unsigned long num_tsb_entries, sz;
  805. u32 vdma[2], dma_mask, dma_offset;
  806. int tsbsize;
  807. prop = of_find_property(pbm->prom_node, "virtual-dma", NULL);
  808. if (prop) {
  809. u32 *val = prop->value;
  810. vdma[0] = val[0];
  811. vdma[1] = val[1];
  812. } else {
  813. /* No property, use default values. */
  814. vdma[0] = 0x80000000;
  815. vdma[1] = 0x80000000;
  816. }
  817. dma_mask = vdma[0];
  818. switch (vdma[1]) {
  819. case 0x20000000:
  820. dma_mask |= 0x1fffffff;
  821. tsbsize = 64;
  822. break;
  823. case 0x40000000:
  824. dma_mask |= 0x3fffffff;
  825. tsbsize = 128;
  826. break;
  827. case 0x80000000:
  828. dma_mask |= 0x7fffffff;
  829. tsbsize = 256;
  830. break;
  831. default:
  832. prom_printf("PCI-SUN4V: strange virtual-dma size.\n");
  833. prom_halt();
  834. };
  835. tsbsize *= (8 * 1024);
  836. num_tsb_entries = tsbsize / sizeof(iopte_t);
  837. dma_offset = vdma[0];
  838. /* Setup initial software IOMMU state. */
  839. spin_lock_init(&iommu->lock);
  840. iommu->ctx_lowest_free = 1;
  841. iommu->page_table_map_base = dma_offset;
  842. iommu->dma_addr_mask = dma_mask;
  843. /* Allocate and initialize the free area map. */
  844. sz = num_tsb_entries / 8;
  845. sz = (sz + 7UL) & ~7UL;
  846. iommu->arena.map = kzalloc(sz, GFP_KERNEL);
  847. if (!iommu->arena.map) {
  848. prom_printf("PCI_IOMMU: Error, kmalloc(arena.map) failed.\n");
  849. prom_halt();
  850. }
  851. iommu->arena.limit = num_tsb_entries;
  852. sz = probe_existing_entries(pbm, iommu);
  853. if (sz)
  854. printk("%s: Imported %lu TSB entries from OBP\n",
  855. pbm->name, sz);
  856. }
  857. static void pci_sun4v_get_bus_range(struct pci_pbm_info *pbm)
  858. {
  859. struct property *prop;
  860. unsigned int *busrange;
  861. prop = of_find_property(pbm->prom_node, "bus-range", NULL);
  862. busrange = prop->value;
  863. pbm->pci_first_busno = busrange[0];
  864. pbm->pci_last_busno = busrange[1];
  865. }
  866. static void pci_sun4v_pbm_init(struct pci_controller_info *p, struct device_node *dp, u32 devhandle)
  867. {
  868. struct pci_pbm_info *pbm;
  869. struct property *prop;
  870. int len, i;
  871. if (devhandle & 0x40)
  872. pbm = &p->pbm_B;
  873. else
  874. pbm = &p->pbm_A;
  875. pbm->parent = p;
  876. pbm->prom_node = dp;
  877. pbm->pci_first_slot = 1;
  878. pbm->devhandle = devhandle;
  879. pbm->name = dp->full_name;
  880. printk("%s: SUN4V PCI Bus Module\n", pbm->name);
  881. prop = of_find_property(dp, "ranges", &len);
  882. pbm->pbm_ranges = prop->value;
  883. pbm->num_pbm_ranges =
  884. (len / sizeof(struct linux_prom_pci_ranges));
  885. /* Mask out the top 8 bits of the ranges, leaving the real
  886. * physical address.
  887. */
  888. for (i = 0; i < pbm->num_pbm_ranges; i++)
  889. pbm->pbm_ranges[i].parent_phys_hi &= 0x0fffffff;
  890. pci_sun4v_determine_mem_io_space(pbm);
  891. pbm_register_toplevel_resources(p, pbm);
  892. prop = of_find_property(dp, "interrupt-map", &len);
  893. pbm->pbm_intmap = prop->value;
  894. pbm->num_pbm_intmap =
  895. (len / sizeof(struct linux_prom_pci_intmap));
  896. prop = of_find_property(dp, "interrupt-map-mask", NULL);
  897. pbm->pbm_intmask = prop->value;
  898. pci_sun4v_get_bus_range(pbm);
  899. pci_sun4v_iommu_init(pbm);
  900. pdev_htab_populate(pbm);
  901. }
  902. void sun4v_pci_init(struct device_node *dp, char *model_name)
  903. {
  904. struct pci_controller_info *p;
  905. struct pci_iommu *iommu;
  906. struct property *prop;
  907. struct linux_prom64_registers *regs;
  908. u32 devhandle;
  909. int i;
  910. prop = of_find_property(dp, "reg", NULL);
  911. regs = prop->value;
  912. devhandle = (regs->phys_addr >> 32UL) & 0x0fffffff;
  913. for (p = pci_controller_root; p; p = p->next) {
  914. struct pci_pbm_info *pbm;
  915. if (p->pbm_A.prom_node && p->pbm_B.prom_node)
  916. continue;
  917. pbm = (p->pbm_A.prom_node ?
  918. &p->pbm_A :
  919. &p->pbm_B);
  920. if (pbm->devhandle == (devhandle ^ 0x40)) {
  921. pci_sun4v_pbm_init(p, dp, devhandle);
  922. return;
  923. }
  924. }
  925. for_each_possible_cpu(i) {
  926. unsigned long page = get_zeroed_page(GFP_ATOMIC);
  927. if (!page)
  928. goto fatal_memory_error;
  929. per_cpu(pci_iommu_batch, i).pglist = (u64 *) page;
  930. }
  931. p = kzalloc(sizeof(struct pci_controller_info), GFP_ATOMIC);
  932. if (!p)
  933. goto fatal_memory_error;
  934. iommu = kzalloc(sizeof(struct pci_iommu), GFP_ATOMIC);
  935. if (!iommu)
  936. goto fatal_memory_error;
  937. p->pbm_A.iommu = iommu;
  938. iommu = kzalloc(sizeof(struct pci_iommu), GFP_ATOMIC);
  939. if (!iommu)
  940. goto fatal_memory_error;
  941. p->pbm_B.iommu = iommu;
  942. p->next = pci_controller_root;
  943. pci_controller_root = p;
  944. p->index = pci_num_controllers++;
  945. p->pbms_same_domain = 0;
  946. p->scan_bus = pci_sun4v_scan_bus;
  947. p->base_address_update = pci_sun4v_base_address_update;
  948. p->resource_adjust = pci_sun4v_resource_adjust;
  949. p->pci_ops = &pci_sun4v_ops;
  950. /* Like PSYCHO and SCHIZO we have a 2GB aligned area
  951. * for memory space.
  952. */
  953. pci_memspace_mask = 0x7fffffffUL;
  954. pci_sun4v_pbm_init(p, dp, devhandle);
  955. return;
  956. fatal_memory_error:
  957. prom_printf("SUN4V_PCI: Fatal memory allocation error.\n");
  958. prom_halt();
  959. }