pci_sun4v.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647
  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 <linux/irq.h>
  13. #include <linux/msi.h>
  14. #include <asm/pbm.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. #define PGLIST_NENTS (PAGE_SIZE / sizeof(u64))
  26. struct pci_iommu_batch {
  27. struct pci_dev *pdev; /* Device mapping is for. */
  28. unsigned long prot; /* IOMMU page protections */
  29. unsigned long entry; /* Index into IOTSB. */
  30. u64 *pglist; /* List of physical pages */
  31. unsigned long npages; /* Number of pages in list. */
  32. };
  33. static DEFINE_PER_CPU(struct pci_iommu_batch, pci_iommu_batch);
  34. /* Interrupts must be disabled. */
  35. static inline void pci_iommu_batch_start(struct pci_dev *pdev, unsigned long prot, unsigned long entry)
  36. {
  37. struct pci_iommu_batch *p = &__get_cpu_var(pci_iommu_batch);
  38. p->pdev = pdev;
  39. p->prot = prot;
  40. p->entry = entry;
  41. p->npages = 0;
  42. }
  43. /* Interrupts must be disabled. */
  44. static long pci_iommu_batch_flush(struct pci_iommu_batch *p)
  45. {
  46. struct pcidev_cookie *pcp = p->pdev->sysdata;
  47. unsigned long devhandle = pcp->pbm->devhandle;
  48. unsigned long prot = p->prot;
  49. unsigned long entry = p->entry;
  50. u64 *pglist = p->pglist;
  51. unsigned long npages = p->npages;
  52. while (npages != 0) {
  53. long num;
  54. num = pci_sun4v_iommu_map(devhandle, HV_PCI_TSBID(0, entry),
  55. npages, prot, __pa(pglist));
  56. if (unlikely(num < 0)) {
  57. if (printk_ratelimit())
  58. printk("pci_iommu_batch_flush: IOMMU map of "
  59. "[%08lx:%08lx:%lx:%lx:%lx] failed with "
  60. "status %ld\n",
  61. devhandle, HV_PCI_TSBID(0, entry),
  62. npages, prot, __pa(pglist), num);
  63. return -1;
  64. }
  65. entry += num;
  66. npages -= num;
  67. pglist += num;
  68. }
  69. p->entry = entry;
  70. p->npages = 0;
  71. return 0;
  72. }
  73. /* Interrupts must be disabled. */
  74. static inline long pci_iommu_batch_add(u64 phys_page)
  75. {
  76. struct pci_iommu_batch *p = &__get_cpu_var(pci_iommu_batch);
  77. BUG_ON(p->npages >= PGLIST_NENTS);
  78. p->pglist[p->npages++] = phys_page;
  79. if (p->npages == PGLIST_NENTS)
  80. return pci_iommu_batch_flush(p);
  81. return 0;
  82. }
  83. /* Interrupts must be disabled. */
  84. static inline long pci_iommu_batch_end(void)
  85. {
  86. struct pci_iommu_batch *p = &__get_cpu_var(pci_iommu_batch);
  87. BUG_ON(p->npages >= PGLIST_NENTS);
  88. return pci_iommu_batch_flush(p);
  89. }
  90. static long pci_arena_alloc(struct pci_iommu_arena *arena, unsigned long npages)
  91. {
  92. unsigned long n, i, start, end, limit;
  93. int pass;
  94. limit = arena->limit;
  95. start = arena->hint;
  96. pass = 0;
  97. again:
  98. n = find_next_zero_bit(arena->map, limit, start);
  99. end = n + npages;
  100. if (unlikely(end >= limit)) {
  101. if (likely(pass < 1)) {
  102. limit = start;
  103. start = 0;
  104. pass++;
  105. goto again;
  106. } else {
  107. /* Scanned the whole thing, give up. */
  108. return -1;
  109. }
  110. }
  111. for (i = n; i < end; i++) {
  112. if (test_bit(i, arena->map)) {
  113. start = i + 1;
  114. goto again;
  115. }
  116. }
  117. for (i = n; i < end; i++)
  118. __set_bit(i, arena->map);
  119. arena->hint = end;
  120. return n;
  121. }
  122. static void pci_arena_free(struct pci_iommu_arena *arena, unsigned long base, unsigned long npages)
  123. {
  124. unsigned long i;
  125. for (i = base; i < (base + npages); i++)
  126. __clear_bit(i, arena->map);
  127. }
  128. static void *pci_4v_alloc_consistent(struct pci_dev *pdev, size_t size, dma_addr_t *dma_addrp, gfp_t gfp)
  129. {
  130. struct pcidev_cookie *pcp;
  131. struct pci_iommu *iommu;
  132. unsigned long flags, order, first_page, npages, n;
  133. void *ret;
  134. long entry;
  135. size = IO_PAGE_ALIGN(size);
  136. order = get_order(size);
  137. if (unlikely(order >= MAX_ORDER))
  138. return NULL;
  139. npages = size >> IO_PAGE_SHIFT;
  140. first_page = __get_free_pages(gfp, order);
  141. if (unlikely(first_page == 0UL))
  142. return NULL;
  143. memset((char *)first_page, 0, PAGE_SIZE << order);
  144. pcp = pdev->sysdata;
  145. iommu = pcp->pbm->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 pcidev_cookie *pcp;
  181. struct pci_iommu *iommu;
  182. unsigned long flags, order, npages, entry;
  183. u32 devhandle;
  184. npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
  185. pcp = pdev->sysdata;
  186. iommu = pcp->pbm->iommu;
  187. devhandle = pcp->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 pcidev_cookie *pcp;
  206. struct pci_iommu *iommu;
  207. unsigned long flags, npages, oaddr;
  208. unsigned long i, base_paddr;
  209. u32 bus_addr, ret;
  210. unsigned long prot;
  211. long entry;
  212. pcp = pdev->sysdata;
  213. iommu = pcp->pbm->iommu;
  214. if (unlikely(direction == PCI_DMA_NONE))
  215. goto bad;
  216. oaddr = (unsigned long)ptr;
  217. npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
  218. npages >>= IO_PAGE_SHIFT;
  219. spin_lock_irqsave(&iommu->lock, flags);
  220. entry = pci_arena_alloc(&iommu->arena, npages);
  221. spin_unlock_irqrestore(&iommu->lock, flags);
  222. if (unlikely(entry < 0L))
  223. goto bad;
  224. bus_addr = (iommu->page_table_map_base +
  225. (entry << IO_PAGE_SHIFT));
  226. ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
  227. base_paddr = __pa(oaddr & IO_PAGE_MASK);
  228. prot = HV_PCI_MAP_ATTR_READ;
  229. if (direction != PCI_DMA_TODEVICE)
  230. prot |= HV_PCI_MAP_ATTR_WRITE;
  231. local_irq_save(flags);
  232. pci_iommu_batch_start(pdev, prot, entry);
  233. for (i = 0; i < npages; i++, base_paddr += IO_PAGE_SIZE) {
  234. long err = pci_iommu_batch_add(base_paddr);
  235. if (unlikely(err < 0L))
  236. goto iommu_map_fail;
  237. }
  238. if (unlikely(pci_iommu_batch_end() < 0L))
  239. goto iommu_map_fail;
  240. local_irq_restore(flags);
  241. return ret;
  242. bad:
  243. if (printk_ratelimit())
  244. WARN_ON(1);
  245. return PCI_DMA_ERROR_CODE;
  246. iommu_map_fail:
  247. /* Interrupts are disabled. */
  248. spin_lock(&iommu->lock);
  249. pci_arena_free(&iommu->arena, entry, npages);
  250. spin_unlock_irqrestore(&iommu->lock, flags);
  251. return PCI_DMA_ERROR_CODE;
  252. }
  253. static void pci_4v_unmap_single(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
  254. {
  255. struct pcidev_cookie *pcp;
  256. struct pci_iommu *iommu;
  257. unsigned long flags, npages;
  258. long entry;
  259. u32 devhandle;
  260. if (unlikely(direction == PCI_DMA_NONE)) {
  261. if (printk_ratelimit())
  262. WARN_ON(1);
  263. return;
  264. }
  265. pcp = pdev->sysdata;
  266. iommu = pcp->pbm->iommu;
  267. devhandle = pcp->pbm->devhandle;
  268. npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
  269. npages >>= IO_PAGE_SHIFT;
  270. bus_addr &= IO_PAGE_MASK;
  271. spin_lock_irqsave(&iommu->lock, flags);
  272. entry = (bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT;
  273. pci_arena_free(&iommu->arena, entry, npages);
  274. do {
  275. unsigned long num;
  276. num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
  277. npages);
  278. entry += num;
  279. npages -= num;
  280. } while (npages != 0);
  281. spin_unlock_irqrestore(&iommu->lock, flags);
  282. }
  283. #define SG_ENT_PHYS_ADDRESS(SG) \
  284. (__pa(page_address((SG)->page)) + (SG)->offset)
  285. static inline long fill_sg(long entry, struct pci_dev *pdev,
  286. struct scatterlist *sg,
  287. int nused, int nelems, unsigned long prot)
  288. {
  289. struct scatterlist *dma_sg = sg;
  290. struct scatterlist *sg_end = sg + nelems;
  291. unsigned long flags;
  292. int i;
  293. local_irq_save(flags);
  294. pci_iommu_batch_start(pdev, prot, entry);
  295. for (i = 0; i < nused; i++) {
  296. unsigned long pteval = ~0UL;
  297. u32 dma_npages;
  298. dma_npages = ((dma_sg->dma_address & (IO_PAGE_SIZE - 1UL)) +
  299. dma_sg->dma_length +
  300. ((IO_PAGE_SIZE - 1UL))) >> IO_PAGE_SHIFT;
  301. do {
  302. unsigned long offset;
  303. signed int len;
  304. /* If we are here, we know we have at least one
  305. * more page to map. So walk forward until we
  306. * hit a page crossing, and begin creating new
  307. * mappings from that spot.
  308. */
  309. for (;;) {
  310. unsigned long tmp;
  311. tmp = SG_ENT_PHYS_ADDRESS(sg);
  312. len = sg->length;
  313. if (((tmp ^ pteval) >> IO_PAGE_SHIFT) != 0UL) {
  314. pteval = tmp & IO_PAGE_MASK;
  315. offset = tmp & (IO_PAGE_SIZE - 1UL);
  316. break;
  317. }
  318. if (((tmp ^ (tmp + len - 1UL)) >> IO_PAGE_SHIFT) != 0UL) {
  319. pteval = (tmp + IO_PAGE_SIZE) & IO_PAGE_MASK;
  320. offset = 0UL;
  321. len -= (IO_PAGE_SIZE - (tmp & (IO_PAGE_SIZE - 1UL)));
  322. break;
  323. }
  324. sg++;
  325. }
  326. pteval = (pteval & IOPTE_PAGE);
  327. while (len > 0) {
  328. long err;
  329. err = pci_iommu_batch_add(pteval);
  330. if (unlikely(err < 0L))
  331. goto iommu_map_failed;
  332. pteval += IO_PAGE_SIZE;
  333. len -= (IO_PAGE_SIZE - offset);
  334. offset = 0;
  335. dma_npages--;
  336. }
  337. pteval = (pteval & IOPTE_PAGE) + len;
  338. sg++;
  339. /* Skip over any tail mappings we've fully mapped,
  340. * adjusting pteval along the way. Stop when we
  341. * detect a page crossing event.
  342. */
  343. while (sg < sg_end &&
  344. (pteval << (64 - IO_PAGE_SHIFT)) != 0UL &&
  345. (pteval == SG_ENT_PHYS_ADDRESS(sg)) &&
  346. ((pteval ^
  347. (SG_ENT_PHYS_ADDRESS(sg) + sg->length - 1UL)) >> IO_PAGE_SHIFT) == 0UL) {
  348. pteval += sg->length;
  349. sg++;
  350. }
  351. if ((pteval << (64 - IO_PAGE_SHIFT)) == 0UL)
  352. pteval = ~0UL;
  353. } while (dma_npages != 0);
  354. dma_sg++;
  355. }
  356. if (unlikely(pci_iommu_batch_end() < 0L))
  357. goto iommu_map_failed;
  358. local_irq_restore(flags);
  359. return 0;
  360. iommu_map_failed:
  361. local_irq_restore(flags);
  362. return -1L;
  363. }
  364. static int pci_4v_map_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
  365. {
  366. struct pcidev_cookie *pcp;
  367. struct pci_iommu *iommu;
  368. unsigned long flags, npages, prot;
  369. u32 dma_base;
  370. struct scatterlist *sgtmp;
  371. long entry, err;
  372. int used;
  373. /* Fast path single entry scatterlists. */
  374. if (nelems == 1) {
  375. sglist->dma_address =
  376. pci_4v_map_single(pdev,
  377. (page_address(sglist->page) + sglist->offset),
  378. sglist->length, direction);
  379. if (unlikely(sglist->dma_address == PCI_DMA_ERROR_CODE))
  380. return 0;
  381. sglist->dma_length = sglist->length;
  382. return 1;
  383. }
  384. pcp = pdev->sysdata;
  385. iommu = pcp->pbm->iommu;
  386. if (unlikely(direction == PCI_DMA_NONE))
  387. goto bad;
  388. /* Step 1: Prepare scatter list. */
  389. npages = prepare_sg(sglist, nelems);
  390. /* Step 2: Allocate a cluster and context, if necessary. */
  391. spin_lock_irqsave(&iommu->lock, flags);
  392. entry = pci_arena_alloc(&iommu->arena, npages);
  393. spin_unlock_irqrestore(&iommu->lock, flags);
  394. if (unlikely(entry < 0L))
  395. goto bad;
  396. dma_base = iommu->page_table_map_base +
  397. (entry << IO_PAGE_SHIFT);
  398. /* Step 3: Normalize DMA addresses. */
  399. used = nelems;
  400. sgtmp = sglist;
  401. while (used && sgtmp->dma_length) {
  402. sgtmp->dma_address += dma_base;
  403. sgtmp++;
  404. used--;
  405. }
  406. used = nelems - used;
  407. /* Step 4: Create the mappings. */
  408. prot = HV_PCI_MAP_ATTR_READ;
  409. if (direction != PCI_DMA_TODEVICE)
  410. prot |= HV_PCI_MAP_ATTR_WRITE;
  411. err = fill_sg(entry, pdev, sglist, used, nelems, prot);
  412. if (unlikely(err < 0L))
  413. goto iommu_map_failed;
  414. return used;
  415. bad:
  416. if (printk_ratelimit())
  417. WARN_ON(1);
  418. return 0;
  419. iommu_map_failed:
  420. spin_lock_irqsave(&iommu->lock, flags);
  421. pci_arena_free(&iommu->arena, entry, npages);
  422. spin_unlock_irqrestore(&iommu->lock, flags);
  423. return 0;
  424. }
  425. static void pci_4v_unmap_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
  426. {
  427. struct pcidev_cookie *pcp;
  428. struct pci_iommu *iommu;
  429. unsigned long flags, i, npages;
  430. long entry;
  431. u32 devhandle, bus_addr;
  432. if (unlikely(direction == PCI_DMA_NONE)) {
  433. if (printk_ratelimit())
  434. WARN_ON(1);
  435. }
  436. pcp = pdev->sysdata;
  437. iommu = pcp->pbm->iommu;
  438. devhandle = pcp->pbm->devhandle;
  439. bus_addr = sglist->dma_address & IO_PAGE_MASK;
  440. for (i = 1; i < nelems; i++)
  441. if (sglist[i].dma_length == 0)
  442. break;
  443. i--;
  444. npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length) -
  445. bus_addr) >> IO_PAGE_SHIFT;
  446. entry = ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  447. spin_lock_irqsave(&iommu->lock, flags);
  448. pci_arena_free(&iommu->arena, entry, npages);
  449. do {
  450. unsigned long num;
  451. num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
  452. npages);
  453. entry += num;
  454. npages -= num;
  455. } while (npages != 0);
  456. spin_unlock_irqrestore(&iommu->lock, flags);
  457. }
  458. static void pci_4v_dma_sync_single_for_cpu(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
  459. {
  460. /* Nothing to do... */
  461. }
  462. static void pci_4v_dma_sync_sg_for_cpu(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
  463. {
  464. /* Nothing to do... */
  465. }
  466. struct pci_iommu_ops pci_sun4v_iommu_ops = {
  467. .alloc_consistent = pci_4v_alloc_consistent,
  468. .free_consistent = pci_4v_free_consistent,
  469. .map_single = pci_4v_map_single,
  470. .unmap_single = pci_4v_unmap_single,
  471. .map_sg = pci_4v_map_sg,
  472. .unmap_sg = pci_4v_unmap_sg,
  473. .dma_sync_single_for_cpu = pci_4v_dma_sync_single_for_cpu,
  474. .dma_sync_sg_for_cpu = pci_4v_dma_sync_sg_for_cpu,
  475. };
  476. /* SUN4V PCI configuration space accessors. */
  477. struct pdev_entry {
  478. struct pdev_entry *next;
  479. u32 devhandle;
  480. unsigned int bus;
  481. unsigned int device;
  482. unsigned int func;
  483. };
  484. #define PDEV_HTAB_SIZE 16
  485. #define PDEV_HTAB_MASK (PDEV_HTAB_SIZE - 1)
  486. static struct pdev_entry *pdev_htab[PDEV_HTAB_SIZE];
  487. static inline unsigned int pdev_hashfn(u32 devhandle, unsigned int bus, unsigned int device, unsigned int func)
  488. {
  489. unsigned int val;
  490. val = (devhandle ^ (devhandle >> 4));
  491. val ^= bus;
  492. val ^= device;
  493. val ^= func;
  494. return val & PDEV_HTAB_MASK;
  495. }
  496. static int pdev_htab_add(u32 devhandle, unsigned int bus, unsigned int device, unsigned int func)
  497. {
  498. struct pdev_entry *p = kmalloc(sizeof(*p), GFP_KERNEL);
  499. struct pdev_entry **slot;
  500. if (!p)
  501. return -ENOMEM;
  502. slot = &pdev_htab[pdev_hashfn(devhandle, bus, device, func)];
  503. p->next = *slot;
  504. *slot = p;
  505. p->devhandle = devhandle;
  506. p->bus = bus;
  507. p->device = device;
  508. p->func = func;
  509. return 0;
  510. }
  511. /* Recursively descend into the OBP device tree, rooted at toplevel_node,
  512. * looking for a PCI device matching bus and devfn.
  513. */
  514. static int obp_find(struct device_node *toplevel_node, unsigned int bus, unsigned int devfn)
  515. {
  516. toplevel_node = toplevel_node->child;
  517. while (toplevel_node != NULL) {
  518. struct linux_prom_pci_registers *regs;
  519. struct property *prop;
  520. int ret;
  521. ret = obp_find(toplevel_node, bus, devfn);
  522. if (ret != 0)
  523. return ret;
  524. prop = of_find_property(toplevel_node, "reg", NULL);
  525. if (!prop)
  526. goto next_sibling;
  527. regs = prop->value;
  528. if (((regs->phys_hi >> 16) & 0xff) == bus &&
  529. ((regs->phys_hi >> 8) & 0xff) == devfn)
  530. break;
  531. next_sibling:
  532. toplevel_node = toplevel_node->sibling;
  533. }
  534. return toplevel_node != NULL;
  535. }
  536. static int pdev_htab_populate(struct pci_pbm_info *pbm)
  537. {
  538. u32 devhandle = pbm->devhandle;
  539. unsigned int bus;
  540. for (bus = pbm->pci_first_busno; bus <= pbm->pci_last_busno; bus++) {
  541. unsigned int devfn;
  542. for (devfn = 0; devfn < 256; devfn++) {
  543. unsigned int device = PCI_SLOT(devfn);
  544. unsigned int func = PCI_FUNC(devfn);
  545. if (obp_find(pbm->prom_node, bus, devfn)) {
  546. int err = pdev_htab_add(devhandle, bus,
  547. device, func);
  548. if (err)
  549. return err;
  550. }
  551. }
  552. }
  553. return 0;
  554. }
  555. static struct pdev_entry *pdev_find(u32 devhandle, unsigned int bus, unsigned int device, unsigned int func)
  556. {
  557. struct pdev_entry *p;
  558. p = pdev_htab[pdev_hashfn(devhandle, bus, device, func)];
  559. while (p) {
  560. if (p->devhandle == devhandle &&
  561. p->bus == bus &&
  562. p->device == device &&
  563. p->func == func)
  564. break;
  565. p = p->next;
  566. }
  567. return p;
  568. }
  569. static inline int pci_sun4v_out_of_range(struct pci_pbm_info *pbm, unsigned int bus, unsigned int device, unsigned int func)
  570. {
  571. if (bus < pbm->pci_first_busno ||
  572. bus > pbm->pci_last_busno)
  573. return 1;
  574. return pdev_find(pbm->devhandle, bus, device, func) == NULL;
  575. }
  576. static int pci_sun4v_read_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
  577. int where, int size, u32 *value)
  578. {
  579. struct pci_pbm_info *pbm = bus_dev->sysdata;
  580. u32 devhandle = pbm->devhandle;
  581. unsigned int bus = bus_dev->number;
  582. unsigned int device = PCI_SLOT(devfn);
  583. unsigned int func = PCI_FUNC(devfn);
  584. unsigned long ret;
  585. if (pci_sun4v_out_of_range(pbm, bus, device, func)) {
  586. ret = ~0UL;
  587. } else {
  588. ret = pci_sun4v_config_get(devhandle,
  589. HV_PCI_DEVICE_BUILD(bus, device, func),
  590. where, size);
  591. #if 0
  592. printk("rcfg: [%x:%x:%x:%d]=[%lx]\n",
  593. devhandle, HV_PCI_DEVICE_BUILD(bus, device, func),
  594. where, size, ret);
  595. #endif
  596. }
  597. switch (size) {
  598. case 1:
  599. *value = ret & 0xff;
  600. break;
  601. case 2:
  602. *value = ret & 0xffff;
  603. break;
  604. case 4:
  605. *value = ret & 0xffffffff;
  606. break;
  607. };
  608. return PCIBIOS_SUCCESSFUL;
  609. }
  610. static int pci_sun4v_write_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
  611. int where, int size, u32 value)
  612. {
  613. struct pci_pbm_info *pbm = bus_dev->sysdata;
  614. u32 devhandle = pbm->devhandle;
  615. unsigned int bus = bus_dev->number;
  616. unsigned int device = PCI_SLOT(devfn);
  617. unsigned int func = PCI_FUNC(devfn);
  618. unsigned long ret;
  619. if (pci_sun4v_out_of_range(pbm, bus, device, func)) {
  620. /* Do nothing. */
  621. } else {
  622. ret = pci_sun4v_config_put(devhandle,
  623. HV_PCI_DEVICE_BUILD(bus, device, func),
  624. where, size, value);
  625. #if 0
  626. printk("wcfg: [%x:%x:%x:%d] v[%x] == [%lx]\n",
  627. devhandle, HV_PCI_DEVICE_BUILD(bus, device, func),
  628. where, size, value, ret);
  629. #endif
  630. }
  631. return PCIBIOS_SUCCESSFUL;
  632. }
  633. static struct pci_ops pci_sun4v_ops = {
  634. .read = pci_sun4v_read_pci_cfg,
  635. .write = pci_sun4v_write_pci_cfg,
  636. };
  637. static void pbm_scan_bus(struct pci_controller_info *p,
  638. struct pci_pbm_info *pbm)
  639. {
  640. struct pcidev_cookie *cookie = kzalloc(sizeof(*cookie), GFP_KERNEL);
  641. if (!cookie) {
  642. prom_printf("%s: Critical allocation failure.\n", pbm->name);
  643. prom_halt();
  644. }
  645. /* All we care about is the PBM. */
  646. cookie->pbm = pbm;
  647. pbm->pci_bus = pci_scan_bus(pbm->pci_first_busno, p->pci_ops, pbm);
  648. #if 0
  649. pci_fixup_host_bridge_self(pbm->pci_bus);
  650. pbm->pci_bus->self->sysdata = cookie;
  651. #endif
  652. pci_fill_in_pbm_cookies(pbm->pci_bus, pbm, pbm->prom_node);
  653. pci_record_assignments(pbm, pbm->pci_bus);
  654. pci_assign_unassigned(pbm, pbm->pci_bus);
  655. pci_fixup_irq(pbm, pbm->pci_bus);
  656. pci_determine_66mhz_disposition(pbm, pbm->pci_bus);
  657. pci_setup_busmastering(pbm, pbm->pci_bus);
  658. }
  659. static void pci_sun4v_scan_bus(struct pci_controller_info *p)
  660. {
  661. struct property *prop;
  662. struct device_node *dp;
  663. if ((dp = p->pbm_A.prom_node) != NULL) {
  664. prop = of_find_property(dp, "66mhz-capable", NULL);
  665. p->pbm_A.is_66mhz_capable = (prop != NULL);
  666. pbm_scan_bus(p, &p->pbm_A);
  667. }
  668. if ((dp = p->pbm_B.prom_node) != NULL) {
  669. prop = of_find_property(dp, "66mhz-capable", NULL);
  670. p->pbm_B.is_66mhz_capable = (prop != NULL);
  671. pbm_scan_bus(p, &p->pbm_B);
  672. }
  673. /* XXX register error interrupt handlers XXX */
  674. }
  675. static void pci_sun4v_base_address_update(struct pci_dev *pdev, int resource)
  676. {
  677. struct pcidev_cookie *pcp = pdev->sysdata;
  678. struct pci_pbm_info *pbm = pcp->pbm;
  679. struct resource *res, *root;
  680. u32 reg;
  681. int where, size, is_64bit;
  682. res = &pdev->resource[resource];
  683. if (resource < 6) {
  684. where = PCI_BASE_ADDRESS_0 + (resource * 4);
  685. } else if (resource == PCI_ROM_RESOURCE) {
  686. where = pdev->rom_base_reg;
  687. } else {
  688. /* Somebody might have asked allocation of a non-standard resource */
  689. return;
  690. }
  691. /* XXX 64-bit MEM handling is not %100 correct... XXX */
  692. is_64bit = 0;
  693. if (res->flags & IORESOURCE_IO)
  694. root = &pbm->io_space;
  695. else {
  696. root = &pbm->mem_space;
  697. if ((res->flags & PCI_BASE_ADDRESS_MEM_TYPE_MASK)
  698. == PCI_BASE_ADDRESS_MEM_TYPE_64)
  699. is_64bit = 1;
  700. }
  701. size = res->end - res->start;
  702. pci_read_config_dword(pdev, where, &reg);
  703. reg = ((reg & size) |
  704. (((u32)(res->start - root->start)) & ~size));
  705. if (resource == PCI_ROM_RESOURCE) {
  706. reg |= PCI_ROM_ADDRESS_ENABLE;
  707. res->flags |= IORESOURCE_ROM_ENABLE;
  708. }
  709. pci_write_config_dword(pdev, where, reg);
  710. /* This knows that the upper 32-bits of the address
  711. * must be zero. Our PCI common layer enforces this.
  712. */
  713. if (is_64bit)
  714. pci_write_config_dword(pdev, where + 4, 0);
  715. }
  716. static void pci_sun4v_resource_adjust(struct pci_dev *pdev,
  717. struct resource *res,
  718. struct resource *root)
  719. {
  720. res->start += root->start;
  721. res->end += root->start;
  722. }
  723. /* Use ranges property to determine where PCI MEM, I/O, and Config
  724. * space are for this PCI bus module.
  725. */
  726. static void pci_sun4v_determine_mem_io_space(struct pci_pbm_info *pbm)
  727. {
  728. int i, saw_mem, saw_io;
  729. saw_mem = saw_io = 0;
  730. for (i = 0; i < pbm->num_pbm_ranges; i++) {
  731. struct linux_prom_pci_ranges *pr = &pbm->pbm_ranges[i];
  732. unsigned long a;
  733. int type;
  734. type = (pr->child_phys_hi >> 24) & 0x3;
  735. a = (((unsigned long)pr->parent_phys_hi << 32UL) |
  736. ((unsigned long)pr->parent_phys_lo << 0UL));
  737. switch (type) {
  738. case 1:
  739. /* 16-bit IO space, 16MB */
  740. pbm->io_space.start = a;
  741. pbm->io_space.end = a + ((16UL*1024UL*1024UL) - 1UL);
  742. pbm->io_space.flags = IORESOURCE_IO;
  743. saw_io = 1;
  744. break;
  745. case 2:
  746. /* 32-bit MEM space, 2GB */
  747. pbm->mem_space.start = a;
  748. pbm->mem_space.end = a + (0x80000000UL - 1UL);
  749. pbm->mem_space.flags = IORESOURCE_MEM;
  750. saw_mem = 1;
  751. break;
  752. case 3:
  753. /* XXX 64-bit MEM handling XXX */
  754. default:
  755. break;
  756. };
  757. }
  758. if (!saw_io || !saw_mem) {
  759. prom_printf("%s: Fatal error, missing %s PBM range.\n",
  760. pbm->name,
  761. (!saw_io ? "IO" : "MEM"));
  762. prom_halt();
  763. }
  764. printk("%s: PCI IO[%lx] MEM[%lx]\n",
  765. pbm->name,
  766. pbm->io_space.start,
  767. pbm->mem_space.start);
  768. }
  769. static void pbm_register_toplevel_resources(struct pci_controller_info *p,
  770. struct pci_pbm_info *pbm)
  771. {
  772. pbm->io_space.name = pbm->mem_space.name = pbm->name;
  773. request_resource(&ioport_resource, &pbm->io_space);
  774. request_resource(&iomem_resource, &pbm->mem_space);
  775. pci_register_legacy_regions(&pbm->io_space,
  776. &pbm->mem_space);
  777. }
  778. static unsigned long probe_existing_entries(struct pci_pbm_info *pbm,
  779. struct pci_iommu *iommu)
  780. {
  781. struct pci_iommu_arena *arena = &iommu->arena;
  782. unsigned long i, cnt = 0;
  783. u32 devhandle;
  784. devhandle = pbm->devhandle;
  785. for (i = 0; i < arena->limit; i++) {
  786. unsigned long ret, io_attrs, ra;
  787. ret = pci_sun4v_iommu_getmap(devhandle,
  788. HV_PCI_TSBID(0, i),
  789. &io_attrs, &ra);
  790. if (ret == HV_EOK) {
  791. if (page_in_phys_avail(ra)) {
  792. pci_sun4v_iommu_demap(devhandle,
  793. HV_PCI_TSBID(0, i), 1);
  794. } else {
  795. cnt++;
  796. __set_bit(i, arena->map);
  797. }
  798. }
  799. }
  800. return cnt;
  801. }
  802. static void pci_sun4v_iommu_init(struct pci_pbm_info *pbm)
  803. {
  804. struct pci_iommu *iommu = pbm->iommu;
  805. struct property *prop;
  806. unsigned long num_tsb_entries, sz;
  807. u32 vdma[2], dma_mask, dma_offset;
  808. int tsbsize;
  809. prop = of_find_property(pbm->prom_node, "virtual-dma", NULL);
  810. if (prop) {
  811. u32 *val = prop->value;
  812. vdma[0] = val[0];
  813. vdma[1] = val[1];
  814. } else {
  815. /* No property, use default values. */
  816. vdma[0] = 0x80000000;
  817. vdma[1] = 0x80000000;
  818. }
  819. dma_mask = vdma[0];
  820. switch (vdma[1]) {
  821. case 0x20000000:
  822. dma_mask |= 0x1fffffff;
  823. tsbsize = 64;
  824. break;
  825. case 0x40000000:
  826. dma_mask |= 0x3fffffff;
  827. tsbsize = 128;
  828. break;
  829. case 0x80000000:
  830. dma_mask |= 0x7fffffff;
  831. tsbsize = 256;
  832. break;
  833. default:
  834. prom_printf("PCI-SUN4V: strange virtual-dma size.\n");
  835. prom_halt();
  836. };
  837. tsbsize *= (8 * 1024);
  838. num_tsb_entries = tsbsize / sizeof(iopte_t);
  839. dma_offset = vdma[0];
  840. /* Setup initial software IOMMU state. */
  841. spin_lock_init(&iommu->lock);
  842. iommu->ctx_lowest_free = 1;
  843. iommu->page_table_map_base = dma_offset;
  844. iommu->dma_addr_mask = dma_mask;
  845. /* Allocate and initialize the free area map. */
  846. sz = num_tsb_entries / 8;
  847. sz = (sz + 7UL) & ~7UL;
  848. iommu->arena.map = kzalloc(sz, GFP_KERNEL);
  849. if (!iommu->arena.map) {
  850. prom_printf("PCI_IOMMU: Error, kmalloc(arena.map) failed.\n");
  851. prom_halt();
  852. }
  853. iommu->arena.limit = num_tsb_entries;
  854. sz = probe_existing_entries(pbm, iommu);
  855. if (sz)
  856. printk("%s: Imported %lu TSB entries from OBP\n",
  857. pbm->name, sz);
  858. }
  859. static void pci_sun4v_get_bus_range(struct pci_pbm_info *pbm)
  860. {
  861. struct property *prop;
  862. unsigned int *busrange;
  863. prop = of_find_property(pbm->prom_node, "bus-range", NULL);
  864. busrange = prop->value;
  865. pbm->pci_first_busno = busrange[0];
  866. pbm->pci_last_busno = busrange[1];
  867. }
  868. #ifdef CONFIG_PCI_MSI
  869. struct pci_sun4v_msiq_entry {
  870. u64 version_type;
  871. #define MSIQ_VERSION_MASK 0xffffffff00000000UL
  872. #define MSIQ_VERSION_SHIFT 32
  873. #define MSIQ_TYPE_MASK 0x00000000000000ffUL
  874. #define MSIQ_TYPE_SHIFT 0
  875. #define MSIQ_TYPE_NONE 0x00
  876. #define MSIQ_TYPE_MSG 0x01
  877. #define MSIQ_TYPE_MSI32 0x02
  878. #define MSIQ_TYPE_MSI64 0x03
  879. #define MSIQ_TYPE_INTX 0x08
  880. #define MSIQ_TYPE_NONE2 0xff
  881. u64 intx_sysino;
  882. u64 reserved1;
  883. u64 stick;
  884. u64 req_id; /* bus/device/func */
  885. #define MSIQ_REQID_BUS_MASK 0xff00UL
  886. #define MSIQ_REQID_BUS_SHIFT 8
  887. #define MSIQ_REQID_DEVICE_MASK 0x00f8UL
  888. #define MSIQ_REQID_DEVICE_SHIFT 3
  889. #define MSIQ_REQID_FUNC_MASK 0x0007UL
  890. #define MSIQ_REQID_FUNC_SHIFT 0
  891. u64 msi_address;
  892. /* The format of this value is message type dependant.
  893. * For MSI bits 15:0 are the data from the MSI packet.
  894. * For MSI-X bits 31:0 are the data from the MSI packet.
  895. * For MSG, the message code and message routing code where:
  896. * bits 39:32 is the bus/device/fn of the msg target-id
  897. * bits 18:16 is the message routing code
  898. * bits 7:0 is the message code
  899. * For INTx the low order 2-bits are:
  900. * 00 - INTA
  901. * 01 - INTB
  902. * 10 - INTC
  903. * 11 - INTD
  904. */
  905. u64 msi_data;
  906. u64 reserved2;
  907. };
  908. /* For now this just runs as a pre-handler for the real interrupt handler.
  909. * So we just walk through the queue and ACK all the entries, update the
  910. * head pointer, and return.
  911. *
  912. * In the longer term it would be nice to do something more integrated
  913. * wherein we can pass in some of this MSI info to the drivers. This
  914. * would be most useful for PCIe fabric error messages, although we could
  915. * invoke those directly from the loop here in order to pass the info around.
  916. */
  917. static void pci_sun4v_msi_prehandler(unsigned int ino, void *data1, void *data2)
  918. {
  919. struct pci_pbm_info *pbm = data1;
  920. struct pci_sun4v_msiq_entry *base, *ep;
  921. unsigned long msiqid, orig_head, head, type, err;
  922. msiqid = (unsigned long) data2;
  923. head = 0xdeadbeef;
  924. err = pci_sun4v_msiq_gethead(pbm->devhandle, msiqid, &head);
  925. if (unlikely(err))
  926. goto hv_error_get;
  927. if (unlikely(head >= (pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry))))
  928. goto bad_offset;
  929. head /= sizeof(struct pci_sun4v_msiq_entry);
  930. orig_head = head;
  931. base = (pbm->msi_queues + ((msiqid - pbm->msiq_first) *
  932. (pbm->msiq_ent_count *
  933. sizeof(struct pci_sun4v_msiq_entry))));
  934. ep = &base[head];
  935. while ((ep->version_type & MSIQ_TYPE_MASK) != 0) {
  936. type = (ep->version_type & MSIQ_TYPE_MASK) >> MSIQ_TYPE_SHIFT;
  937. if (unlikely(type != MSIQ_TYPE_MSI32 &&
  938. type != MSIQ_TYPE_MSI64))
  939. goto bad_type;
  940. pci_sun4v_msi_setstate(pbm->devhandle,
  941. ep->msi_data /* msi_num */,
  942. HV_MSISTATE_IDLE);
  943. /* Clear the entry. */
  944. ep->version_type &= ~MSIQ_TYPE_MASK;
  945. /* Go to next entry in ring. */
  946. head++;
  947. if (head >= pbm->msiq_ent_count)
  948. head = 0;
  949. ep = &base[head];
  950. }
  951. if (likely(head != orig_head)) {
  952. /* ACK entries by updating head pointer. */
  953. head *= sizeof(struct pci_sun4v_msiq_entry);
  954. err = pci_sun4v_msiq_sethead(pbm->devhandle, msiqid, head);
  955. if (unlikely(err))
  956. goto hv_error_set;
  957. }
  958. return;
  959. hv_error_set:
  960. printk(KERN_EMERG "MSI: Hypervisor set head gives error %lu\n", err);
  961. goto hv_error_cont;
  962. hv_error_get:
  963. printk(KERN_EMERG "MSI: Hypervisor get head gives error %lu\n", err);
  964. hv_error_cont:
  965. printk(KERN_EMERG "MSI: devhandle[%x] msiqid[%lx] head[%lu]\n",
  966. pbm->devhandle, msiqid, head);
  967. return;
  968. bad_offset:
  969. printk(KERN_EMERG "MSI: Hypervisor gives bad offset %lx max(%lx)\n",
  970. head, pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry));
  971. return;
  972. bad_type:
  973. printk(KERN_EMERG "MSI: Entry has bad type %lx\n", type);
  974. return;
  975. }
  976. static int msi_bitmap_alloc(struct pci_pbm_info *pbm)
  977. {
  978. unsigned long size, bits_per_ulong;
  979. bits_per_ulong = sizeof(unsigned long) * 8;
  980. size = (pbm->msi_num + (bits_per_ulong - 1)) & ~(bits_per_ulong - 1);
  981. size /= 8;
  982. BUG_ON(size % sizeof(unsigned long));
  983. pbm->msi_bitmap = kzalloc(size, GFP_KERNEL);
  984. if (!pbm->msi_bitmap)
  985. return -ENOMEM;
  986. return 0;
  987. }
  988. static void msi_bitmap_free(struct pci_pbm_info *pbm)
  989. {
  990. kfree(pbm->msi_bitmap);
  991. pbm->msi_bitmap = NULL;
  992. }
  993. static int msi_queue_alloc(struct pci_pbm_info *pbm)
  994. {
  995. unsigned long q_size, alloc_size, pages, order;
  996. int i;
  997. q_size = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
  998. alloc_size = (pbm->msiq_num * q_size);
  999. order = get_order(alloc_size);
  1000. pages = __get_free_pages(GFP_KERNEL | __GFP_COMP, order);
  1001. if (pages == 0UL) {
  1002. printk(KERN_ERR "MSI: Cannot allocate MSI queues (o=%lu).\n",
  1003. order);
  1004. return -ENOMEM;
  1005. }
  1006. memset((char *)pages, 0, PAGE_SIZE << order);
  1007. pbm->msi_queues = (void *) pages;
  1008. for (i = 0; i < pbm->msiq_num; i++) {
  1009. unsigned long err, base = __pa(pages + (i * q_size));
  1010. unsigned long ret1, ret2;
  1011. err = pci_sun4v_msiq_conf(pbm->devhandle,
  1012. pbm->msiq_first + i,
  1013. base, pbm->msiq_ent_count);
  1014. if (err) {
  1015. printk(KERN_ERR "MSI: msiq register fails (err=%lu)\n",
  1016. err);
  1017. goto h_error;
  1018. }
  1019. err = pci_sun4v_msiq_info(pbm->devhandle,
  1020. pbm->msiq_first + i,
  1021. &ret1, &ret2);
  1022. if (err) {
  1023. printk(KERN_ERR "MSI: Cannot read msiq (err=%lu)\n",
  1024. err);
  1025. goto h_error;
  1026. }
  1027. if (ret1 != base || ret2 != pbm->msiq_ent_count) {
  1028. printk(KERN_ERR "MSI: Bogus qconf "
  1029. "expected[%lx:%x] got[%lx:%lx]\n",
  1030. base, pbm->msiq_ent_count,
  1031. ret1, ret2);
  1032. goto h_error;
  1033. }
  1034. }
  1035. return 0;
  1036. h_error:
  1037. free_pages(pages, order);
  1038. return -EINVAL;
  1039. }
  1040. static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
  1041. {
  1042. u32 *val;
  1043. int len;
  1044. val = of_get_property(pbm->prom_node, "#msi-eqs", &len);
  1045. if (!val || len != 4)
  1046. goto no_msi;
  1047. pbm->msiq_num = *val;
  1048. if (pbm->msiq_num) {
  1049. struct msiq_prop {
  1050. u32 first_msiq;
  1051. u32 num_msiq;
  1052. u32 first_devino;
  1053. } *mqp;
  1054. struct msi_range_prop {
  1055. u32 first_msi;
  1056. u32 num_msi;
  1057. } *mrng;
  1058. struct addr_range_prop {
  1059. u32 msi32_high;
  1060. u32 msi32_low;
  1061. u32 msi32_len;
  1062. u32 msi64_high;
  1063. u32 msi64_low;
  1064. u32 msi64_len;
  1065. } *arng;
  1066. val = of_get_property(pbm->prom_node, "msi-eq-size", &len);
  1067. if (!val || len != 4)
  1068. goto no_msi;
  1069. pbm->msiq_ent_count = *val;
  1070. mqp = of_get_property(pbm->prom_node,
  1071. "msi-eq-to-devino", &len);
  1072. if (!mqp || len != sizeof(struct msiq_prop))
  1073. goto no_msi;
  1074. pbm->msiq_first = mqp->first_msiq;
  1075. pbm->msiq_first_devino = mqp->first_devino;
  1076. val = of_get_property(pbm->prom_node, "#msi", &len);
  1077. if (!val || len != 4)
  1078. goto no_msi;
  1079. pbm->msi_num = *val;
  1080. mrng = of_get_property(pbm->prom_node, "msi-ranges", &len);
  1081. if (!mrng || len != sizeof(struct msi_range_prop))
  1082. goto no_msi;
  1083. pbm->msi_first = mrng->first_msi;
  1084. val = of_get_property(pbm->prom_node, "msi-data-mask", &len);
  1085. if (!val || len != 4)
  1086. goto no_msi;
  1087. pbm->msi_data_mask = *val;
  1088. val = of_get_property(pbm->prom_node, "msix-data-width", &len);
  1089. if (!val || len != 4)
  1090. goto no_msi;
  1091. pbm->msix_data_width = *val;
  1092. arng = of_get_property(pbm->prom_node, "msi-address-ranges",
  1093. &len);
  1094. if (!arng || len != sizeof(struct addr_range_prop))
  1095. goto no_msi;
  1096. pbm->msi32_start = ((u64)arng->msi32_high << 32) |
  1097. (u64) arng->msi32_low;
  1098. pbm->msi64_start = ((u64)arng->msi64_high << 32) |
  1099. (u64) arng->msi64_low;
  1100. pbm->msi32_len = arng->msi32_len;
  1101. pbm->msi64_len = arng->msi64_len;
  1102. if (msi_bitmap_alloc(pbm))
  1103. goto no_msi;
  1104. if (msi_queue_alloc(pbm)) {
  1105. msi_bitmap_free(pbm);
  1106. goto no_msi;
  1107. }
  1108. printk(KERN_INFO "%s: MSI Queue first[%u] num[%u] count[%u] "
  1109. "devino[0x%x]\n",
  1110. pbm->name,
  1111. pbm->msiq_first, pbm->msiq_num,
  1112. pbm->msiq_ent_count,
  1113. pbm->msiq_first_devino);
  1114. printk(KERN_INFO "%s: MSI first[%u] num[%u] mask[0x%x] "
  1115. "width[%u]\n",
  1116. pbm->name,
  1117. pbm->msi_first, pbm->msi_num, pbm->msi_data_mask,
  1118. pbm->msix_data_width);
  1119. printk(KERN_INFO "%s: MSI addr32[0x%lx:0x%x] "
  1120. "addr64[0x%lx:0x%x]\n",
  1121. pbm->name,
  1122. pbm->msi32_start, pbm->msi32_len,
  1123. pbm->msi64_start, pbm->msi64_len);
  1124. printk(KERN_INFO "%s: MSI queues at RA [%p]\n",
  1125. pbm->name,
  1126. pbm->msi_queues);
  1127. }
  1128. return;
  1129. no_msi:
  1130. pbm->msiq_num = 0;
  1131. printk(KERN_INFO "%s: No MSI support.\n", pbm->name);
  1132. }
  1133. static int alloc_msi(struct pci_pbm_info *pbm)
  1134. {
  1135. int i;
  1136. for (i = 0; i < pbm->msi_num; i++) {
  1137. if (!test_and_set_bit(i, pbm->msi_bitmap))
  1138. return i + pbm->msi_first;
  1139. }
  1140. return -ENOENT;
  1141. }
  1142. static void free_msi(struct pci_pbm_info *pbm, int msi_num)
  1143. {
  1144. msi_num -= pbm->msi_first;
  1145. clear_bit(msi_num, pbm->msi_bitmap);
  1146. }
  1147. static int pci_sun4v_setup_msi_irq(unsigned int *virt_irq_p,
  1148. struct pci_dev *pdev,
  1149. struct msi_desc *entry)
  1150. {
  1151. struct pcidev_cookie *pcp = pdev->sysdata;
  1152. struct pci_pbm_info *pbm = pcp->pbm;
  1153. unsigned long devino, msiqid;
  1154. struct msi_msg msg;
  1155. int msi_num, err;
  1156. *virt_irq_p = 0;
  1157. msi_num = alloc_msi(pbm);
  1158. if (msi_num < 0)
  1159. return msi_num;
  1160. devino = sun4v_build_msi(pbm->devhandle, virt_irq_p,
  1161. pbm->msiq_first_devino,
  1162. (pbm->msiq_first_devino +
  1163. pbm->msiq_num));
  1164. err = -ENOMEM;
  1165. if (!devino)
  1166. goto out_err;
  1167. set_irq_msi(*virt_irq_p, entry);
  1168. msiqid = ((devino - pbm->msiq_first_devino) +
  1169. pbm->msiq_first);
  1170. err = -EINVAL;
  1171. if (pci_sun4v_msiq_setstate(pbm->devhandle, msiqid, HV_MSIQSTATE_IDLE))
  1172. if (err)
  1173. goto out_err;
  1174. if (pci_sun4v_msiq_setvalid(pbm->devhandle, msiqid, HV_MSIQ_VALID))
  1175. goto out_err;
  1176. if (pci_sun4v_msi_setmsiq(pbm->devhandle,
  1177. msi_num, msiqid,
  1178. (entry->msi_attrib.is_64 ?
  1179. HV_MSITYPE_MSI64 : HV_MSITYPE_MSI32)))
  1180. goto out_err;
  1181. if (pci_sun4v_msi_setstate(pbm->devhandle, msi_num, HV_MSISTATE_IDLE))
  1182. goto out_err;
  1183. if (pci_sun4v_msi_setvalid(pbm->devhandle, msi_num, HV_MSIVALID_VALID))
  1184. goto out_err;
  1185. pcp->msi_num = msi_num;
  1186. if (entry->msi_attrib.is_64) {
  1187. msg.address_hi = pbm->msi64_start >> 32;
  1188. msg.address_lo = pbm->msi64_start & 0xffffffff;
  1189. } else {
  1190. msg.address_hi = 0;
  1191. msg.address_lo = pbm->msi32_start;
  1192. }
  1193. msg.data = msi_num;
  1194. write_msi_msg(*virt_irq_p, &msg);
  1195. irq_install_pre_handler(*virt_irq_p,
  1196. pci_sun4v_msi_prehandler,
  1197. pbm, (void *) msiqid);
  1198. return 0;
  1199. out_err:
  1200. free_msi(pbm, msi_num);
  1201. sun4v_destroy_msi(*virt_irq_p);
  1202. *virt_irq_p = 0;
  1203. return err;
  1204. }
  1205. static void pci_sun4v_teardown_msi_irq(unsigned int virt_irq,
  1206. struct pci_dev *pdev)
  1207. {
  1208. struct pcidev_cookie *pcp = pdev->sysdata;
  1209. struct pci_pbm_info *pbm = pcp->pbm;
  1210. unsigned long msiqid, err;
  1211. unsigned int msi_num;
  1212. msi_num = pcp->msi_num;
  1213. err = pci_sun4v_msi_getmsiq(pbm->devhandle, msi_num, &msiqid);
  1214. if (err) {
  1215. printk(KERN_ERR "%s: getmsiq gives error %lu\n",
  1216. pbm->name, err);
  1217. return;
  1218. }
  1219. pci_sun4v_msi_setvalid(pbm->devhandle, msi_num, HV_MSIVALID_INVALID);
  1220. pci_sun4v_msiq_setvalid(pbm->devhandle, msiqid, HV_MSIQ_INVALID);
  1221. free_msi(pbm, msi_num);
  1222. /* The sun4v_destroy_msi() will liberate the devino and thus the MSIQ
  1223. * allocation.
  1224. */
  1225. sun4v_destroy_msi(virt_irq);
  1226. }
  1227. #else /* CONFIG_PCI_MSI */
  1228. static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
  1229. {
  1230. }
  1231. #endif /* !(CONFIG_PCI_MSI) */
  1232. static void pci_sun4v_pbm_init(struct pci_controller_info *p, struct device_node *dp, u32 devhandle)
  1233. {
  1234. struct pci_pbm_info *pbm;
  1235. struct property *prop;
  1236. int len, i;
  1237. if (devhandle & 0x40)
  1238. pbm = &p->pbm_B;
  1239. else
  1240. pbm = &p->pbm_A;
  1241. pbm->parent = p;
  1242. pbm->prom_node = dp;
  1243. pbm->pci_first_slot = 1;
  1244. pbm->devhandle = devhandle;
  1245. pbm->name = dp->full_name;
  1246. printk("%s: SUN4V PCI Bus Module\n", pbm->name);
  1247. prop = of_find_property(dp, "ranges", &len);
  1248. pbm->pbm_ranges = prop->value;
  1249. pbm->num_pbm_ranges =
  1250. (len / sizeof(struct linux_prom_pci_ranges));
  1251. /* Mask out the top 8 bits of the ranges, leaving the real
  1252. * physical address.
  1253. */
  1254. for (i = 0; i < pbm->num_pbm_ranges; i++)
  1255. pbm->pbm_ranges[i].parent_phys_hi &= 0x0fffffff;
  1256. pci_sun4v_determine_mem_io_space(pbm);
  1257. pbm_register_toplevel_resources(p, pbm);
  1258. prop = of_find_property(dp, "interrupt-map", &len);
  1259. pbm->pbm_intmap = prop->value;
  1260. pbm->num_pbm_intmap =
  1261. (len / sizeof(struct linux_prom_pci_intmap));
  1262. prop = of_find_property(dp, "interrupt-map-mask", NULL);
  1263. pbm->pbm_intmask = prop->value;
  1264. pci_sun4v_get_bus_range(pbm);
  1265. pci_sun4v_iommu_init(pbm);
  1266. pci_sun4v_msi_init(pbm);
  1267. pdev_htab_populate(pbm);
  1268. }
  1269. void sun4v_pci_init(struct device_node *dp, char *model_name)
  1270. {
  1271. struct pci_controller_info *p;
  1272. struct pci_iommu *iommu;
  1273. struct property *prop;
  1274. struct linux_prom64_registers *regs;
  1275. u32 devhandle;
  1276. int i;
  1277. prop = of_find_property(dp, "reg", NULL);
  1278. regs = prop->value;
  1279. devhandle = (regs->phys_addr >> 32UL) & 0x0fffffff;
  1280. for (p = pci_controller_root; p; p = p->next) {
  1281. struct pci_pbm_info *pbm;
  1282. if (p->pbm_A.prom_node && p->pbm_B.prom_node)
  1283. continue;
  1284. pbm = (p->pbm_A.prom_node ?
  1285. &p->pbm_A :
  1286. &p->pbm_B);
  1287. if (pbm->devhandle == (devhandle ^ 0x40)) {
  1288. pci_sun4v_pbm_init(p, dp, devhandle);
  1289. return;
  1290. }
  1291. }
  1292. for_each_possible_cpu(i) {
  1293. unsigned long page = get_zeroed_page(GFP_ATOMIC);
  1294. if (!page)
  1295. goto fatal_memory_error;
  1296. per_cpu(pci_iommu_batch, i).pglist = (u64 *) page;
  1297. }
  1298. p = kzalloc(sizeof(struct pci_controller_info), GFP_ATOMIC);
  1299. if (!p)
  1300. goto fatal_memory_error;
  1301. iommu = kzalloc(sizeof(struct pci_iommu), GFP_ATOMIC);
  1302. if (!iommu)
  1303. goto fatal_memory_error;
  1304. p->pbm_A.iommu = iommu;
  1305. iommu = kzalloc(sizeof(struct pci_iommu), GFP_ATOMIC);
  1306. if (!iommu)
  1307. goto fatal_memory_error;
  1308. p->pbm_B.iommu = iommu;
  1309. p->next = pci_controller_root;
  1310. pci_controller_root = p;
  1311. p->index = pci_num_controllers++;
  1312. p->pbms_same_domain = 0;
  1313. p->scan_bus = pci_sun4v_scan_bus;
  1314. p->base_address_update = pci_sun4v_base_address_update;
  1315. p->resource_adjust = pci_sun4v_resource_adjust;
  1316. #ifdef CONFIG_PCI_MSI
  1317. p->setup_msi_irq = pci_sun4v_setup_msi_irq;
  1318. p->teardown_msi_irq = pci_sun4v_teardown_msi_irq;
  1319. #endif
  1320. p->pci_ops = &pci_sun4v_ops;
  1321. /* Like PSYCHO and SCHIZO we have a 2GB aligned area
  1322. * for memory space.
  1323. */
  1324. pci_memspace_mask = 0x7fffffffUL;
  1325. pci_sun4v_pbm_init(p, dp, devhandle);
  1326. return;
  1327. fatal_memory_error:
  1328. prom_printf("SUN4V_PCI: Fatal memory allocation error.\n");
  1329. prom_halt();
  1330. }