pci_sun4v.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. /* pci_sun4v.c: SUN4V specific PCI controller support.
  2. *
  3. * Copyright (C) 2006, 2007 David S. Miller (davem@davemloft.net)
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/types.h>
  7. #include <linux/pci.h>
  8. #include <linux/init.h>
  9. #include <linux/slab.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/percpu.h>
  12. #include <linux/irq.h>
  13. #include <linux/msi.h>
  14. #include <linux/log2.h>
  15. #include <asm/iommu.h>
  16. #include <asm/irq.h>
  17. #include <asm/upa.h>
  18. #include <asm/pstate.h>
  19. #include <asm/oplib.h>
  20. #include <asm/hypervisor.h>
  21. #include <asm/prom.h>
  22. #include "pci_impl.h"
  23. #include "iommu_common.h"
  24. #include "pci_sun4v.h"
  25. static unsigned long vpci_major = 1;
  26. static unsigned long vpci_minor = 1;
  27. #define PGLIST_NENTS (PAGE_SIZE / sizeof(u64))
  28. struct iommu_batch {
  29. struct pci_dev *pdev; /* Device mapping is for. */
  30. unsigned long prot; /* IOMMU page protections */
  31. unsigned long entry; /* Index into IOTSB. */
  32. u64 *pglist; /* List of physical pages */
  33. unsigned long npages; /* Number of pages in list. */
  34. };
  35. static DEFINE_PER_CPU(struct iommu_batch, pci_iommu_batch);
  36. /* Interrupts must be disabled. */
  37. static inline void pci_iommu_batch_start(struct pci_dev *pdev, unsigned long prot, unsigned long entry)
  38. {
  39. struct iommu_batch *p = &__get_cpu_var(pci_iommu_batch);
  40. p->pdev = pdev;
  41. p->prot = prot;
  42. p->entry = entry;
  43. p->npages = 0;
  44. }
  45. /* Interrupts must be disabled. */
  46. static long pci_iommu_batch_flush(struct iommu_batch *p)
  47. {
  48. struct pci_pbm_info *pbm = p->pdev->dev.archdata.host_controller;
  49. unsigned long devhandle = pbm->devhandle;
  50. unsigned long prot = p->prot;
  51. unsigned long entry = p->entry;
  52. u64 *pglist = p->pglist;
  53. unsigned long npages = p->npages;
  54. while (npages != 0) {
  55. long num;
  56. num = pci_sun4v_iommu_map(devhandle, HV_PCI_TSBID(0, entry),
  57. npages, prot, __pa(pglist));
  58. if (unlikely(num < 0)) {
  59. if (printk_ratelimit())
  60. printk("pci_iommu_batch_flush: IOMMU map of "
  61. "[%08lx:%08lx:%lx:%lx:%lx] failed with "
  62. "status %ld\n",
  63. devhandle, HV_PCI_TSBID(0, entry),
  64. npages, prot, __pa(pglist), num);
  65. return -1;
  66. }
  67. entry += num;
  68. npages -= num;
  69. pglist += num;
  70. }
  71. p->entry = entry;
  72. p->npages = 0;
  73. return 0;
  74. }
  75. /* Interrupts must be disabled. */
  76. static inline long pci_iommu_batch_add(u64 phys_page)
  77. {
  78. struct iommu_batch *p = &__get_cpu_var(pci_iommu_batch);
  79. BUG_ON(p->npages >= PGLIST_NENTS);
  80. p->pglist[p->npages++] = phys_page;
  81. if (p->npages == PGLIST_NENTS)
  82. return pci_iommu_batch_flush(p);
  83. return 0;
  84. }
  85. /* Interrupts must be disabled. */
  86. static inline long pci_iommu_batch_end(void)
  87. {
  88. struct iommu_batch *p = &__get_cpu_var(pci_iommu_batch);
  89. BUG_ON(p->npages >= PGLIST_NENTS);
  90. return pci_iommu_batch_flush(p);
  91. }
  92. static long pci_arena_alloc(struct iommu_arena *arena, unsigned long npages)
  93. {
  94. unsigned long n, i, start, end, limit;
  95. int pass;
  96. limit = arena->limit;
  97. start = arena->hint;
  98. pass = 0;
  99. again:
  100. n = find_next_zero_bit(arena->map, limit, start);
  101. end = n + npages;
  102. if (unlikely(end >= limit)) {
  103. if (likely(pass < 1)) {
  104. limit = start;
  105. start = 0;
  106. pass++;
  107. goto again;
  108. } else {
  109. /* Scanned the whole thing, give up. */
  110. return -1;
  111. }
  112. }
  113. for (i = n; i < end; i++) {
  114. if (test_bit(i, arena->map)) {
  115. start = i + 1;
  116. goto again;
  117. }
  118. }
  119. for (i = n; i < end; i++)
  120. __set_bit(i, arena->map);
  121. arena->hint = end;
  122. return n;
  123. }
  124. static void pci_arena_free(struct iommu_arena *arena, unsigned long base, unsigned long npages)
  125. {
  126. unsigned long i;
  127. for (i = base; i < (base + npages); i++)
  128. __clear_bit(i, arena->map);
  129. }
  130. static void *pci_4v_alloc_consistent(struct pci_dev *pdev, size_t size, dma_addr_t *dma_addrp, gfp_t gfp)
  131. {
  132. struct iommu *iommu;
  133. unsigned long flags, order, first_page, npages, n;
  134. void *ret;
  135. long entry;
  136. size = IO_PAGE_ALIGN(size);
  137. order = get_order(size);
  138. if (unlikely(order >= MAX_ORDER))
  139. return NULL;
  140. npages = size >> IO_PAGE_SHIFT;
  141. first_page = __get_free_pages(gfp, order);
  142. if (unlikely(first_page == 0UL))
  143. return NULL;
  144. memset((char *)first_page, 0, PAGE_SIZE << order);
  145. iommu = pdev->dev.archdata.iommu;
  146. spin_lock_irqsave(&iommu->lock, flags);
  147. entry = pci_arena_alloc(&iommu->arena, npages);
  148. spin_unlock_irqrestore(&iommu->lock, flags);
  149. if (unlikely(entry < 0L))
  150. goto arena_alloc_fail;
  151. *dma_addrp = (iommu->page_table_map_base +
  152. (entry << IO_PAGE_SHIFT));
  153. ret = (void *) first_page;
  154. first_page = __pa(first_page);
  155. local_irq_save(flags);
  156. pci_iommu_batch_start(pdev,
  157. (HV_PCI_MAP_ATTR_READ |
  158. HV_PCI_MAP_ATTR_WRITE),
  159. entry);
  160. for (n = 0; n < npages; n++) {
  161. long err = pci_iommu_batch_add(first_page + (n * PAGE_SIZE));
  162. if (unlikely(err < 0L))
  163. goto iommu_map_fail;
  164. }
  165. if (unlikely(pci_iommu_batch_end() < 0L))
  166. goto iommu_map_fail;
  167. local_irq_restore(flags);
  168. return ret;
  169. iommu_map_fail:
  170. /* Interrupts are disabled. */
  171. spin_lock(&iommu->lock);
  172. pci_arena_free(&iommu->arena, entry, npages);
  173. spin_unlock_irqrestore(&iommu->lock, flags);
  174. arena_alloc_fail:
  175. free_pages(first_page, order);
  176. return NULL;
  177. }
  178. static void pci_4v_free_consistent(struct pci_dev *pdev, size_t size, void *cpu, dma_addr_t dvma)
  179. {
  180. struct pci_pbm_info *pbm;
  181. struct iommu *iommu;
  182. unsigned long flags, order, npages, entry;
  183. u32 devhandle;
  184. npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
  185. iommu = pdev->dev.archdata.iommu;
  186. pbm = pdev->dev.archdata.host_controller;
  187. devhandle = pbm->devhandle;
  188. entry = ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  189. spin_lock_irqsave(&iommu->lock, flags);
  190. pci_arena_free(&iommu->arena, entry, npages);
  191. do {
  192. unsigned long num;
  193. num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
  194. npages);
  195. entry += num;
  196. npages -= num;
  197. } while (npages != 0);
  198. spin_unlock_irqrestore(&iommu->lock, flags);
  199. order = get_order(size);
  200. if (order < 10)
  201. free_pages((unsigned long)cpu, order);
  202. }
  203. static dma_addr_t pci_4v_map_single(struct pci_dev *pdev, void *ptr, size_t sz, int direction)
  204. {
  205. struct iommu *iommu;
  206. unsigned long flags, npages, oaddr;
  207. unsigned long i, base_paddr;
  208. u32 bus_addr, ret;
  209. unsigned long prot;
  210. long entry;
  211. iommu = pdev->dev.archdata.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 pci_pbm_info *pbm;
  254. struct 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. iommu = pdev->dev.archdata.iommu;
  264. pbm = pdev->dev.archdata.host_controller;
  265. devhandle = 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 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. iommu = pdev->dev.archdata.iommu;
  382. if (unlikely(direction == PCI_DMA_NONE))
  383. goto bad;
  384. /* Step 1: Prepare scatter list. */
  385. npages = prepare_sg(sglist, nelems);
  386. /* Step 2: Allocate a cluster and context, if necessary. */
  387. spin_lock_irqsave(&iommu->lock, flags);
  388. entry = pci_arena_alloc(&iommu->arena, npages);
  389. spin_unlock_irqrestore(&iommu->lock, flags);
  390. if (unlikely(entry < 0L))
  391. goto bad;
  392. dma_base = iommu->page_table_map_base +
  393. (entry << IO_PAGE_SHIFT);
  394. /* Step 3: Normalize DMA addresses. */
  395. used = nelems;
  396. sgtmp = sglist;
  397. while (used && sgtmp->dma_length) {
  398. sgtmp->dma_address += dma_base;
  399. sgtmp++;
  400. used--;
  401. }
  402. used = nelems - used;
  403. /* Step 4: Create the mappings. */
  404. prot = HV_PCI_MAP_ATTR_READ;
  405. if (direction != PCI_DMA_TODEVICE)
  406. prot |= HV_PCI_MAP_ATTR_WRITE;
  407. err = fill_sg(entry, pdev, sglist, used, nelems, prot);
  408. if (unlikely(err < 0L))
  409. goto iommu_map_failed;
  410. return used;
  411. bad:
  412. if (printk_ratelimit())
  413. WARN_ON(1);
  414. return 0;
  415. iommu_map_failed:
  416. spin_lock_irqsave(&iommu->lock, flags);
  417. pci_arena_free(&iommu->arena, entry, npages);
  418. spin_unlock_irqrestore(&iommu->lock, flags);
  419. return 0;
  420. }
  421. static void pci_4v_unmap_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
  422. {
  423. struct pci_pbm_info *pbm;
  424. struct iommu *iommu;
  425. unsigned long flags, i, npages;
  426. long entry;
  427. u32 devhandle, bus_addr;
  428. if (unlikely(direction == PCI_DMA_NONE)) {
  429. if (printk_ratelimit())
  430. WARN_ON(1);
  431. }
  432. iommu = pdev->dev.archdata.iommu;
  433. pbm = pdev->dev.archdata.host_controller;
  434. devhandle = pbm->devhandle;
  435. bus_addr = sglist->dma_address & IO_PAGE_MASK;
  436. for (i = 1; i < nelems; i++)
  437. if (sglist[i].dma_length == 0)
  438. break;
  439. i--;
  440. npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length) -
  441. bus_addr) >> IO_PAGE_SHIFT;
  442. entry = ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  443. spin_lock_irqsave(&iommu->lock, flags);
  444. pci_arena_free(&iommu->arena, entry, npages);
  445. do {
  446. unsigned long num;
  447. num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
  448. npages);
  449. entry += num;
  450. npages -= num;
  451. } while (npages != 0);
  452. spin_unlock_irqrestore(&iommu->lock, flags);
  453. }
  454. static void pci_4v_dma_sync_single_for_cpu(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
  455. {
  456. /* Nothing to do... */
  457. }
  458. static void pci_4v_dma_sync_sg_for_cpu(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
  459. {
  460. /* Nothing to do... */
  461. }
  462. const struct pci_iommu_ops pci_sun4v_iommu_ops = {
  463. .alloc_consistent = pci_4v_alloc_consistent,
  464. .free_consistent = pci_4v_free_consistent,
  465. .map_single = pci_4v_map_single,
  466. .unmap_single = pci_4v_unmap_single,
  467. .map_sg = pci_4v_map_sg,
  468. .unmap_sg = pci_4v_unmap_sg,
  469. .dma_sync_single_for_cpu = pci_4v_dma_sync_single_for_cpu,
  470. .dma_sync_sg_for_cpu = pci_4v_dma_sync_sg_for_cpu,
  471. };
  472. static void pci_sun4v_scan_bus(struct pci_pbm_info *pbm)
  473. {
  474. struct property *prop;
  475. struct device_node *dp;
  476. dp = pbm->prom_node;
  477. prop = of_find_property(dp, "66mhz-capable", NULL);
  478. pbm->is_66mhz_capable = (prop != NULL);
  479. pbm->pci_bus = pci_scan_one_pbm(pbm);
  480. /* XXX register error interrupt handlers XXX */
  481. }
  482. static unsigned long probe_existing_entries(struct pci_pbm_info *pbm,
  483. struct iommu *iommu)
  484. {
  485. struct iommu_arena *arena = &iommu->arena;
  486. unsigned long i, cnt = 0;
  487. u32 devhandle;
  488. devhandle = pbm->devhandle;
  489. for (i = 0; i < arena->limit; i++) {
  490. unsigned long ret, io_attrs, ra;
  491. ret = pci_sun4v_iommu_getmap(devhandle,
  492. HV_PCI_TSBID(0, i),
  493. &io_attrs, &ra);
  494. if (ret == HV_EOK) {
  495. if (page_in_phys_avail(ra)) {
  496. pci_sun4v_iommu_demap(devhandle,
  497. HV_PCI_TSBID(0, i), 1);
  498. } else {
  499. cnt++;
  500. __set_bit(i, arena->map);
  501. }
  502. }
  503. }
  504. return cnt;
  505. }
  506. static void pci_sun4v_iommu_init(struct pci_pbm_info *pbm)
  507. {
  508. struct iommu *iommu = pbm->iommu;
  509. struct property *prop;
  510. unsigned long num_tsb_entries, sz, tsbsize;
  511. u32 vdma[2], dma_mask, dma_offset;
  512. prop = of_find_property(pbm->prom_node, "virtual-dma", NULL);
  513. if (prop) {
  514. u32 *val = prop->value;
  515. vdma[0] = val[0];
  516. vdma[1] = val[1];
  517. } else {
  518. /* No property, use default values. */
  519. vdma[0] = 0x80000000;
  520. vdma[1] = 0x80000000;
  521. }
  522. if ((vdma[0] | vdma[1]) & ~IO_PAGE_MASK) {
  523. prom_printf("PCI-SUN4V: strange virtual-dma[%08x:%08x].\n",
  524. vdma[0], vdma[1]);
  525. prom_halt();
  526. };
  527. dma_mask = (roundup_pow_of_two(vdma[1]) - 1UL);
  528. num_tsb_entries = vdma[1] / IO_PAGE_SIZE;
  529. tsbsize = num_tsb_entries * sizeof(iopte_t);
  530. dma_offset = vdma[0];
  531. /* Setup initial software IOMMU state. */
  532. spin_lock_init(&iommu->lock);
  533. iommu->ctx_lowest_free = 1;
  534. iommu->page_table_map_base = dma_offset;
  535. iommu->dma_addr_mask = dma_mask;
  536. /* Allocate and initialize the free area map. */
  537. sz = (num_tsb_entries + 7) / 8;
  538. sz = (sz + 7UL) & ~7UL;
  539. iommu->arena.map = kzalloc(sz, GFP_KERNEL);
  540. if (!iommu->arena.map) {
  541. prom_printf("PCI_IOMMU: Error, kmalloc(arena.map) failed.\n");
  542. prom_halt();
  543. }
  544. iommu->arena.limit = num_tsb_entries;
  545. sz = probe_existing_entries(pbm, iommu);
  546. if (sz)
  547. printk("%s: Imported %lu TSB entries from OBP\n",
  548. pbm->name, sz);
  549. }
  550. #ifdef CONFIG_PCI_MSI
  551. struct pci_sun4v_msiq_entry {
  552. u64 version_type;
  553. #define MSIQ_VERSION_MASK 0xffffffff00000000UL
  554. #define MSIQ_VERSION_SHIFT 32
  555. #define MSIQ_TYPE_MASK 0x00000000000000ffUL
  556. #define MSIQ_TYPE_SHIFT 0
  557. #define MSIQ_TYPE_NONE 0x00
  558. #define MSIQ_TYPE_MSG 0x01
  559. #define MSIQ_TYPE_MSI32 0x02
  560. #define MSIQ_TYPE_MSI64 0x03
  561. #define MSIQ_TYPE_INTX 0x08
  562. #define MSIQ_TYPE_NONE2 0xff
  563. u64 intx_sysino;
  564. u64 reserved1;
  565. u64 stick;
  566. u64 req_id; /* bus/device/func */
  567. #define MSIQ_REQID_BUS_MASK 0xff00UL
  568. #define MSIQ_REQID_BUS_SHIFT 8
  569. #define MSIQ_REQID_DEVICE_MASK 0x00f8UL
  570. #define MSIQ_REQID_DEVICE_SHIFT 3
  571. #define MSIQ_REQID_FUNC_MASK 0x0007UL
  572. #define MSIQ_REQID_FUNC_SHIFT 0
  573. u64 msi_address;
  574. /* The format of this value is message type dependent.
  575. * For MSI bits 15:0 are the data from the MSI packet.
  576. * For MSI-X bits 31:0 are the data from the MSI packet.
  577. * For MSG, the message code and message routing code where:
  578. * bits 39:32 is the bus/device/fn of the msg target-id
  579. * bits 18:16 is the message routing code
  580. * bits 7:0 is the message code
  581. * For INTx the low order 2-bits are:
  582. * 00 - INTA
  583. * 01 - INTB
  584. * 10 - INTC
  585. * 11 - INTD
  586. */
  587. u64 msi_data;
  588. u64 reserved2;
  589. };
  590. /* For now this just runs as a pre-handler for the real interrupt handler.
  591. * So we just walk through the queue and ACK all the entries, update the
  592. * head pointer, and return.
  593. *
  594. * In the longer term it would be nice to do something more integrated
  595. * wherein we can pass in some of this MSI info to the drivers. This
  596. * would be most useful for PCIe fabric error messages, although we could
  597. * invoke those directly from the loop here in order to pass the info around.
  598. */
  599. static void pci_sun4v_msi_prehandler(unsigned int ino, void *data1, void *data2)
  600. {
  601. struct pci_pbm_info *pbm = data1;
  602. struct pci_sun4v_msiq_entry *base, *ep;
  603. unsigned long msiqid, orig_head, head, type, err;
  604. msiqid = (unsigned long) data2;
  605. head = 0xdeadbeef;
  606. err = pci_sun4v_msiq_gethead(pbm->devhandle, msiqid, &head);
  607. if (unlikely(err))
  608. goto hv_error_get;
  609. if (unlikely(head >= (pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry))))
  610. goto bad_offset;
  611. head /= sizeof(struct pci_sun4v_msiq_entry);
  612. orig_head = head;
  613. base = (pbm->msi_queues + ((msiqid - pbm->msiq_first) *
  614. (pbm->msiq_ent_count *
  615. sizeof(struct pci_sun4v_msiq_entry))));
  616. ep = &base[head];
  617. while ((ep->version_type & MSIQ_TYPE_MASK) != 0) {
  618. type = (ep->version_type & MSIQ_TYPE_MASK) >> MSIQ_TYPE_SHIFT;
  619. if (unlikely(type != MSIQ_TYPE_MSI32 &&
  620. type != MSIQ_TYPE_MSI64))
  621. goto bad_type;
  622. pci_sun4v_msi_setstate(pbm->devhandle,
  623. ep->msi_data /* msi_num */,
  624. HV_MSISTATE_IDLE);
  625. /* Clear the entry. */
  626. ep->version_type &= ~MSIQ_TYPE_MASK;
  627. /* Go to next entry in ring. */
  628. head++;
  629. if (head >= pbm->msiq_ent_count)
  630. head = 0;
  631. ep = &base[head];
  632. }
  633. if (likely(head != orig_head)) {
  634. /* ACK entries by updating head pointer. */
  635. head *= sizeof(struct pci_sun4v_msiq_entry);
  636. err = pci_sun4v_msiq_sethead(pbm->devhandle, msiqid, head);
  637. if (unlikely(err))
  638. goto hv_error_set;
  639. }
  640. return;
  641. hv_error_set:
  642. printk(KERN_EMERG "MSI: Hypervisor set head gives error %lu\n", err);
  643. goto hv_error_cont;
  644. hv_error_get:
  645. printk(KERN_EMERG "MSI: Hypervisor get head gives error %lu\n", err);
  646. hv_error_cont:
  647. printk(KERN_EMERG "MSI: devhandle[%x] msiqid[%lx] head[%lu]\n",
  648. pbm->devhandle, msiqid, head);
  649. return;
  650. bad_offset:
  651. printk(KERN_EMERG "MSI: Hypervisor gives bad offset %lx max(%lx)\n",
  652. head, pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry));
  653. return;
  654. bad_type:
  655. printk(KERN_EMERG "MSI: Entry has bad type %lx\n", type);
  656. return;
  657. }
  658. static int msi_bitmap_alloc(struct pci_pbm_info *pbm)
  659. {
  660. unsigned long size, bits_per_ulong;
  661. bits_per_ulong = sizeof(unsigned long) * 8;
  662. size = (pbm->msi_num + (bits_per_ulong - 1)) & ~(bits_per_ulong - 1);
  663. size /= 8;
  664. BUG_ON(size % sizeof(unsigned long));
  665. pbm->msi_bitmap = kzalloc(size, GFP_KERNEL);
  666. if (!pbm->msi_bitmap)
  667. return -ENOMEM;
  668. return 0;
  669. }
  670. static void msi_bitmap_free(struct pci_pbm_info *pbm)
  671. {
  672. kfree(pbm->msi_bitmap);
  673. pbm->msi_bitmap = NULL;
  674. }
  675. static int msi_queue_alloc(struct pci_pbm_info *pbm)
  676. {
  677. unsigned long q_size, alloc_size, pages, order;
  678. int i;
  679. q_size = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
  680. alloc_size = (pbm->msiq_num * q_size);
  681. order = get_order(alloc_size);
  682. pages = __get_free_pages(GFP_KERNEL | __GFP_COMP, order);
  683. if (pages == 0UL) {
  684. printk(KERN_ERR "MSI: Cannot allocate MSI queues (o=%lu).\n",
  685. order);
  686. return -ENOMEM;
  687. }
  688. memset((char *)pages, 0, PAGE_SIZE << order);
  689. pbm->msi_queues = (void *) pages;
  690. for (i = 0; i < pbm->msiq_num; i++) {
  691. unsigned long err, base = __pa(pages + (i * q_size));
  692. unsigned long ret1, ret2;
  693. err = pci_sun4v_msiq_conf(pbm->devhandle,
  694. pbm->msiq_first + i,
  695. base, pbm->msiq_ent_count);
  696. if (err) {
  697. printk(KERN_ERR "MSI: msiq register fails (err=%lu)\n",
  698. err);
  699. goto h_error;
  700. }
  701. err = pci_sun4v_msiq_info(pbm->devhandle,
  702. pbm->msiq_first + i,
  703. &ret1, &ret2);
  704. if (err) {
  705. printk(KERN_ERR "MSI: Cannot read msiq (err=%lu)\n",
  706. err);
  707. goto h_error;
  708. }
  709. if (ret1 != base || ret2 != pbm->msiq_ent_count) {
  710. printk(KERN_ERR "MSI: Bogus qconf "
  711. "expected[%lx:%x] got[%lx:%lx]\n",
  712. base, pbm->msiq_ent_count,
  713. ret1, ret2);
  714. goto h_error;
  715. }
  716. }
  717. return 0;
  718. h_error:
  719. free_pages(pages, order);
  720. return -EINVAL;
  721. }
  722. static int alloc_msi(struct pci_pbm_info *pbm)
  723. {
  724. int i;
  725. for (i = 0; i < pbm->msi_num; i++) {
  726. if (!test_and_set_bit(i, pbm->msi_bitmap))
  727. return i + pbm->msi_first;
  728. }
  729. return -ENOENT;
  730. }
  731. static void free_msi(struct pci_pbm_info *pbm, int msi_num)
  732. {
  733. msi_num -= pbm->msi_first;
  734. clear_bit(msi_num, pbm->msi_bitmap);
  735. }
  736. static int pci_sun4v_setup_msi_irq(unsigned int *virt_irq_p,
  737. struct pci_dev *pdev,
  738. struct msi_desc *entry)
  739. {
  740. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  741. unsigned long devino, msiqid;
  742. struct msi_msg msg;
  743. int msi_num, err;
  744. *virt_irq_p = 0;
  745. msi_num = alloc_msi(pbm);
  746. if (msi_num < 0)
  747. return msi_num;
  748. devino = sun4v_build_msi(pbm->devhandle, virt_irq_p,
  749. pbm->msiq_first_devino,
  750. (pbm->msiq_first_devino +
  751. pbm->msiq_num));
  752. err = -ENOMEM;
  753. if (!devino)
  754. goto out_err;
  755. msiqid = ((devino - pbm->msiq_first_devino) +
  756. pbm->msiq_first);
  757. err = -EINVAL;
  758. if (pci_sun4v_msiq_setstate(pbm->devhandle, msiqid, HV_MSIQSTATE_IDLE))
  759. if (err)
  760. goto out_err;
  761. if (pci_sun4v_msiq_setvalid(pbm->devhandle, msiqid, HV_MSIQ_VALID))
  762. goto out_err;
  763. if (pci_sun4v_msi_setmsiq(pbm->devhandle,
  764. msi_num, msiqid,
  765. (entry->msi_attrib.is_64 ?
  766. HV_MSITYPE_MSI64 : HV_MSITYPE_MSI32)))
  767. goto out_err;
  768. if (pci_sun4v_msi_setstate(pbm->devhandle, msi_num, HV_MSISTATE_IDLE))
  769. goto out_err;
  770. if (pci_sun4v_msi_setvalid(pbm->devhandle, msi_num, HV_MSIVALID_VALID))
  771. goto out_err;
  772. pdev->dev.archdata.msi_num = msi_num;
  773. if (entry->msi_attrib.is_64) {
  774. msg.address_hi = pbm->msi64_start >> 32;
  775. msg.address_lo = pbm->msi64_start & 0xffffffff;
  776. } else {
  777. msg.address_hi = 0;
  778. msg.address_lo = pbm->msi32_start;
  779. }
  780. msg.data = msi_num;
  781. set_irq_msi(*virt_irq_p, entry);
  782. write_msi_msg(*virt_irq_p, &msg);
  783. irq_install_pre_handler(*virt_irq_p,
  784. pci_sun4v_msi_prehandler,
  785. pbm, (void *) msiqid);
  786. return 0;
  787. out_err:
  788. free_msi(pbm, msi_num);
  789. sun4v_destroy_msi(*virt_irq_p);
  790. *virt_irq_p = 0;
  791. return err;
  792. }
  793. static void pci_sun4v_teardown_msi_irq(unsigned int virt_irq,
  794. struct pci_dev *pdev)
  795. {
  796. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  797. unsigned long msiqid, err;
  798. unsigned int msi_num;
  799. msi_num = pdev->dev.archdata.msi_num;
  800. err = pci_sun4v_msi_getmsiq(pbm->devhandle, msi_num, &msiqid);
  801. if (err) {
  802. printk(KERN_ERR "%s: getmsiq gives error %lu\n",
  803. pbm->name, err);
  804. return;
  805. }
  806. pci_sun4v_msi_setvalid(pbm->devhandle, msi_num, HV_MSIVALID_INVALID);
  807. pci_sun4v_msiq_setvalid(pbm->devhandle, msiqid, HV_MSIQ_INVALID);
  808. free_msi(pbm, msi_num);
  809. /* The sun4v_destroy_msi() will liberate the devino and thus the MSIQ
  810. * allocation.
  811. */
  812. sun4v_destroy_msi(virt_irq);
  813. }
  814. static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
  815. {
  816. const u32 *val;
  817. int len;
  818. val = of_get_property(pbm->prom_node, "#msi-eqs", &len);
  819. if (!val || len != 4)
  820. goto no_msi;
  821. pbm->msiq_num = *val;
  822. if (pbm->msiq_num) {
  823. const struct msiq_prop {
  824. u32 first_msiq;
  825. u32 num_msiq;
  826. u32 first_devino;
  827. } *mqp;
  828. const struct msi_range_prop {
  829. u32 first_msi;
  830. u32 num_msi;
  831. } *mrng;
  832. const struct addr_range_prop {
  833. u32 msi32_high;
  834. u32 msi32_low;
  835. u32 msi32_len;
  836. u32 msi64_high;
  837. u32 msi64_low;
  838. u32 msi64_len;
  839. } *arng;
  840. val = of_get_property(pbm->prom_node, "msi-eq-size", &len);
  841. if (!val || len != 4)
  842. goto no_msi;
  843. pbm->msiq_ent_count = *val;
  844. mqp = of_get_property(pbm->prom_node,
  845. "msi-eq-to-devino", &len);
  846. if (!mqp || len != sizeof(struct msiq_prop))
  847. goto no_msi;
  848. pbm->msiq_first = mqp->first_msiq;
  849. pbm->msiq_first_devino = mqp->first_devino;
  850. val = of_get_property(pbm->prom_node, "#msi", &len);
  851. if (!val || len != 4)
  852. goto no_msi;
  853. pbm->msi_num = *val;
  854. mrng = of_get_property(pbm->prom_node, "msi-ranges", &len);
  855. if (!mrng || len != sizeof(struct msi_range_prop))
  856. goto no_msi;
  857. pbm->msi_first = mrng->first_msi;
  858. val = of_get_property(pbm->prom_node, "msi-data-mask", &len);
  859. if (!val || len != 4)
  860. goto no_msi;
  861. pbm->msi_data_mask = *val;
  862. val = of_get_property(pbm->prom_node, "msix-data-width", &len);
  863. if (!val || len != 4)
  864. goto no_msi;
  865. pbm->msix_data_width = *val;
  866. arng = of_get_property(pbm->prom_node, "msi-address-ranges",
  867. &len);
  868. if (!arng || len != sizeof(struct addr_range_prop))
  869. goto no_msi;
  870. pbm->msi32_start = ((u64)arng->msi32_high << 32) |
  871. (u64) arng->msi32_low;
  872. pbm->msi64_start = ((u64)arng->msi64_high << 32) |
  873. (u64) arng->msi64_low;
  874. pbm->msi32_len = arng->msi32_len;
  875. pbm->msi64_len = arng->msi64_len;
  876. if (msi_bitmap_alloc(pbm))
  877. goto no_msi;
  878. if (msi_queue_alloc(pbm)) {
  879. msi_bitmap_free(pbm);
  880. goto no_msi;
  881. }
  882. printk(KERN_INFO "%s: MSI Queue first[%u] num[%u] count[%u] "
  883. "devino[0x%x]\n",
  884. pbm->name,
  885. pbm->msiq_first, pbm->msiq_num,
  886. pbm->msiq_ent_count,
  887. pbm->msiq_first_devino);
  888. printk(KERN_INFO "%s: MSI first[%u] num[%u] mask[0x%x] "
  889. "width[%u]\n",
  890. pbm->name,
  891. pbm->msi_first, pbm->msi_num, pbm->msi_data_mask,
  892. pbm->msix_data_width);
  893. printk(KERN_INFO "%s: MSI addr32[0x%lx:0x%x] "
  894. "addr64[0x%lx:0x%x]\n",
  895. pbm->name,
  896. pbm->msi32_start, pbm->msi32_len,
  897. pbm->msi64_start, pbm->msi64_len);
  898. printk(KERN_INFO "%s: MSI queues at RA [%p]\n",
  899. pbm->name,
  900. pbm->msi_queues);
  901. }
  902. pbm->setup_msi_irq = pci_sun4v_setup_msi_irq;
  903. pbm->teardown_msi_irq = pci_sun4v_teardown_msi_irq;
  904. return;
  905. no_msi:
  906. pbm->msiq_num = 0;
  907. printk(KERN_INFO "%s: No MSI support.\n", pbm->name);
  908. }
  909. #else /* CONFIG_PCI_MSI */
  910. static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
  911. {
  912. }
  913. #endif /* !(CONFIG_PCI_MSI) */
  914. static void pci_sun4v_pbm_init(struct pci_controller_info *p, struct device_node *dp, u32 devhandle)
  915. {
  916. struct pci_pbm_info *pbm;
  917. if (devhandle & 0x40)
  918. pbm = &p->pbm_B;
  919. else
  920. pbm = &p->pbm_A;
  921. pbm->next = pci_pbm_root;
  922. pci_pbm_root = pbm;
  923. pbm->scan_bus = pci_sun4v_scan_bus;
  924. pbm->pci_ops = &sun4v_pci_ops;
  925. pbm->config_space_reg_bits = 12;
  926. pbm->index = pci_num_pbms++;
  927. pbm->parent = p;
  928. pbm->prom_node = dp;
  929. pbm->devhandle = devhandle;
  930. pbm->name = dp->full_name;
  931. printk("%s: SUN4V PCI Bus Module\n", pbm->name);
  932. pci_determine_mem_io_space(pbm);
  933. pci_get_pbm_props(pbm);
  934. pci_sun4v_iommu_init(pbm);
  935. pci_sun4v_msi_init(pbm);
  936. }
  937. void sun4v_pci_init(struct device_node *dp, char *model_name)
  938. {
  939. static int hvapi_negotiated = 0;
  940. struct pci_controller_info *p;
  941. struct pci_pbm_info *pbm;
  942. struct iommu *iommu;
  943. struct property *prop;
  944. struct linux_prom64_registers *regs;
  945. u32 devhandle;
  946. int i;
  947. if (!hvapi_negotiated++) {
  948. int err = sun4v_hvapi_register(HV_GRP_PCI,
  949. vpci_major,
  950. &vpci_minor);
  951. if (err) {
  952. prom_printf("SUN4V_PCI: Could not register hvapi, "
  953. "err=%d\n", err);
  954. prom_halt();
  955. }
  956. printk("SUN4V_PCI: Registered hvapi major[%lu] minor[%lu]\n",
  957. vpci_major, vpci_minor);
  958. }
  959. prop = of_find_property(dp, "reg", NULL);
  960. regs = prop->value;
  961. devhandle = (regs->phys_addr >> 32UL) & 0x0fffffff;
  962. for (pbm = pci_pbm_root; pbm; pbm = pbm->next) {
  963. if (pbm->devhandle == (devhandle ^ 0x40)) {
  964. pci_sun4v_pbm_init(pbm->parent, dp, devhandle);
  965. return;
  966. }
  967. }
  968. for_each_possible_cpu(i) {
  969. unsigned long page = get_zeroed_page(GFP_ATOMIC);
  970. if (!page)
  971. goto fatal_memory_error;
  972. per_cpu(pci_iommu_batch, i).pglist = (u64 *) page;
  973. }
  974. p = kzalloc(sizeof(struct pci_controller_info), GFP_ATOMIC);
  975. if (!p)
  976. goto fatal_memory_error;
  977. iommu = kzalloc(sizeof(struct iommu), GFP_ATOMIC);
  978. if (!iommu)
  979. goto fatal_memory_error;
  980. p->pbm_A.iommu = iommu;
  981. iommu = kzalloc(sizeof(struct iommu), GFP_ATOMIC);
  982. if (!iommu)
  983. goto fatal_memory_error;
  984. p->pbm_B.iommu = iommu;
  985. /* Like PSYCHO and SCHIZO we have a 2GB aligned area
  986. * for memory space.
  987. */
  988. pci_memspace_mask = 0x7fffffffUL;
  989. pci_sun4v_pbm_init(p, dp, devhandle);
  990. return;
  991. fatal_memory_error:
  992. prom_printf("SUN4V_PCI: Fatal memory allocation error.\n");
  993. prom_halt();
  994. }