pci_sun4v.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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 device *dev; /* 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, iommu_batch);
  36. /* Interrupts must be disabled. */
  37. static inline void iommu_batch_start(struct device *dev, unsigned long prot, unsigned long entry)
  38. {
  39. struct iommu_batch *p = &__get_cpu_var(iommu_batch);
  40. p->dev = dev;
  41. p->prot = prot;
  42. p->entry = entry;
  43. p->npages = 0;
  44. }
  45. /* Interrupts must be disabled. */
  46. static long iommu_batch_flush(struct iommu_batch *p)
  47. {
  48. struct pci_pbm_info *pbm = p->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("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 iommu_batch_add(u64 phys_page)
  77. {
  78. struct iommu_batch *p = &__get_cpu_var(iommu_batch);
  79. BUG_ON(p->npages >= PGLIST_NENTS);
  80. p->pglist[p->npages++] = phys_page;
  81. if (p->npages == PGLIST_NENTS)
  82. return iommu_batch_flush(p);
  83. return 0;
  84. }
  85. /* Interrupts must be disabled. */
  86. static inline long iommu_batch_end(void)
  87. {
  88. struct iommu_batch *p = &__get_cpu_var(iommu_batch);
  89. BUG_ON(p->npages >= PGLIST_NENTS);
  90. return iommu_batch_flush(p);
  91. }
  92. static long 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 arena_free(struct iommu_arena *arena, unsigned long base,
  125. unsigned long npages)
  126. {
  127. unsigned long i;
  128. for (i = base; i < (base + npages); i++)
  129. __clear_bit(i, arena->map);
  130. }
  131. static void *dma_4v_alloc_coherent(struct device *dev, size_t size,
  132. dma_addr_t *dma_addrp, gfp_t gfp)
  133. {
  134. struct iommu *iommu;
  135. unsigned long flags, order, first_page, npages, n;
  136. void *ret;
  137. long entry;
  138. size = IO_PAGE_ALIGN(size);
  139. order = get_order(size);
  140. if (unlikely(order >= MAX_ORDER))
  141. return NULL;
  142. npages = size >> IO_PAGE_SHIFT;
  143. first_page = __get_free_pages(gfp, order);
  144. if (unlikely(first_page == 0UL))
  145. return NULL;
  146. memset((char *)first_page, 0, PAGE_SIZE << order);
  147. iommu = dev->archdata.iommu;
  148. spin_lock_irqsave(&iommu->lock, flags);
  149. entry = arena_alloc(&iommu->arena, npages);
  150. spin_unlock_irqrestore(&iommu->lock, flags);
  151. if (unlikely(entry < 0L))
  152. goto arena_alloc_fail;
  153. *dma_addrp = (iommu->page_table_map_base +
  154. (entry << IO_PAGE_SHIFT));
  155. ret = (void *) first_page;
  156. first_page = __pa(first_page);
  157. local_irq_save(flags);
  158. iommu_batch_start(dev,
  159. (HV_PCI_MAP_ATTR_READ |
  160. HV_PCI_MAP_ATTR_WRITE),
  161. entry);
  162. for (n = 0; n < npages; n++) {
  163. long err = iommu_batch_add(first_page + (n * PAGE_SIZE));
  164. if (unlikely(err < 0L))
  165. goto iommu_map_fail;
  166. }
  167. if (unlikely(iommu_batch_end() < 0L))
  168. goto iommu_map_fail;
  169. local_irq_restore(flags);
  170. return ret;
  171. iommu_map_fail:
  172. /* Interrupts are disabled. */
  173. spin_lock(&iommu->lock);
  174. arena_free(&iommu->arena, entry, npages);
  175. spin_unlock_irqrestore(&iommu->lock, flags);
  176. arena_alloc_fail:
  177. free_pages(first_page, order);
  178. return NULL;
  179. }
  180. static void dma_4v_free_coherent(struct device *dev, size_t size, void *cpu,
  181. dma_addr_t dvma)
  182. {
  183. struct pci_pbm_info *pbm;
  184. struct iommu *iommu;
  185. unsigned long flags, order, npages, entry;
  186. u32 devhandle;
  187. npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
  188. iommu = dev->archdata.iommu;
  189. pbm = dev->archdata.host_controller;
  190. devhandle = pbm->devhandle;
  191. entry = ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  192. spin_lock_irqsave(&iommu->lock, flags);
  193. arena_free(&iommu->arena, entry, npages);
  194. do {
  195. unsigned long num;
  196. num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
  197. npages);
  198. entry += num;
  199. npages -= num;
  200. } while (npages != 0);
  201. spin_unlock_irqrestore(&iommu->lock, flags);
  202. order = get_order(size);
  203. if (order < 10)
  204. free_pages((unsigned long)cpu, order);
  205. }
  206. static dma_addr_t dma_4v_map_single(struct device *dev, void *ptr, size_t sz,
  207. enum dma_data_direction direction)
  208. {
  209. struct iommu *iommu;
  210. unsigned long flags, npages, oaddr;
  211. unsigned long i, base_paddr;
  212. u32 bus_addr, ret;
  213. unsigned long prot;
  214. long entry;
  215. iommu = dev->archdata.iommu;
  216. if (unlikely(direction == DMA_NONE))
  217. goto bad;
  218. oaddr = (unsigned long)ptr;
  219. npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
  220. npages >>= IO_PAGE_SHIFT;
  221. spin_lock_irqsave(&iommu->lock, flags);
  222. entry = arena_alloc(&iommu->arena, npages);
  223. spin_unlock_irqrestore(&iommu->lock, flags);
  224. if (unlikely(entry < 0L))
  225. goto bad;
  226. bus_addr = (iommu->page_table_map_base +
  227. (entry << IO_PAGE_SHIFT));
  228. ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
  229. base_paddr = __pa(oaddr & IO_PAGE_MASK);
  230. prot = HV_PCI_MAP_ATTR_READ;
  231. if (direction != DMA_TO_DEVICE)
  232. prot |= HV_PCI_MAP_ATTR_WRITE;
  233. local_irq_save(flags);
  234. iommu_batch_start(dev, prot, entry);
  235. for (i = 0; i < npages; i++, base_paddr += IO_PAGE_SIZE) {
  236. long err = iommu_batch_add(base_paddr);
  237. if (unlikely(err < 0L))
  238. goto iommu_map_fail;
  239. }
  240. if (unlikely(iommu_batch_end() < 0L))
  241. goto iommu_map_fail;
  242. local_irq_restore(flags);
  243. return ret;
  244. bad:
  245. if (printk_ratelimit())
  246. WARN_ON(1);
  247. return DMA_ERROR_CODE;
  248. iommu_map_fail:
  249. /* Interrupts are disabled. */
  250. spin_lock(&iommu->lock);
  251. arena_free(&iommu->arena, entry, npages);
  252. spin_unlock_irqrestore(&iommu->lock, flags);
  253. return DMA_ERROR_CODE;
  254. }
  255. static void dma_4v_unmap_single(struct device *dev, dma_addr_t bus_addr,
  256. size_t sz, enum dma_data_direction direction)
  257. {
  258. struct pci_pbm_info *pbm;
  259. struct iommu *iommu;
  260. unsigned long flags, npages;
  261. long entry;
  262. u32 devhandle;
  263. if (unlikely(direction == DMA_NONE)) {
  264. if (printk_ratelimit())
  265. WARN_ON(1);
  266. return;
  267. }
  268. iommu = dev->archdata.iommu;
  269. pbm = dev->archdata.host_controller;
  270. devhandle = pbm->devhandle;
  271. npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
  272. npages >>= IO_PAGE_SHIFT;
  273. bus_addr &= IO_PAGE_MASK;
  274. spin_lock_irqsave(&iommu->lock, flags);
  275. entry = (bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT;
  276. arena_free(&iommu->arena, entry, npages);
  277. do {
  278. unsigned long num;
  279. num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
  280. npages);
  281. entry += num;
  282. npages -= num;
  283. } while (npages != 0);
  284. spin_unlock_irqrestore(&iommu->lock, flags);
  285. }
  286. static int dma_4v_map_sg(struct device *dev, struct scatterlist *sglist,
  287. int nelems, enum dma_data_direction direction)
  288. {
  289. unsigned long flags, npages, i, prot;
  290. struct scatterlist *sg;
  291. struct iommu *iommu;
  292. long entry, err;
  293. u32 dma_base;
  294. /* Fast path single entry scatterlists. */
  295. if (nelems == 1) {
  296. sglist->dma_address =
  297. dma_4v_map_single(dev, sg_virt(sglist),
  298. sglist->length, direction);
  299. if (unlikely(sglist->dma_address == DMA_ERROR_CODE))
  300. return 0;
  301. sglist->dma_length = sglist->length;
  302. return 1;
  303. }
  304. iommu = dev->archdata.iommu;
  305. if (unlikely(direction == DMA_NONE))
  306. goto bad;
  307. npages = calc_npages(sglist, nelems);
  308. spin_lock_irqsave(&iommu->lock, flags);
  309. entry = arena_alloc(&iommu->arena, npages);
  310. spin_unlock_irqrestore(&iommu->lock, flags);
  311. if (unlikely(entry < 0L))
  312. goto bad;
  313. dma_base = iommu->page_table_map_base +
  314. (entry << IO_PAGE_SHIFT);
  315. prot = HV_PCI_MAP_ATTR_READ;
  316. if (direction != DMA_TO_DEVICE)
  317. prot |= HV_PCI_MAP_ATTR_WRITE;
  318. local_irq_save(flags);
  319. iommu_batch_start(dev, prot, entry);
  320. for_each_sg(sglist, sg, nelems, i) {
  321. unsigned long paddr = SG_ENT_PHYS_ADDRESS(sg);
  322. unsigned long slen = sg->length;
  323. unsigned long this_npages;
  324. this_npages = iommu_num_pages(paddr, slen);
  325. sg->dma_address = dma_base | (paddr & ~IO_PAGE_MASK);
  326. sg->dma_length = slen;
  327. paddr &= IO_PAGE_MASK;
  328. while (this_npages--) {
  329. err = iommu_batch_add(paddr);
  330. if (unlikely(err < 0L)) {
  331. local_irq_restore(flags);
  332. goto iommu_map_failed;
  333. }
  334. paddr += IO_PAGE_SIZE;
  335. dma_base += IO_PAGE_SIZE;
  336. }
  337. }
  338. err = iommu_batch_end();
  339. local_irq_restore(flags);
  340. if (unlikely(err < 0L))
  341. goto iommu_map_failed;
  342. return nelems;
  343. bad:
  344. if (printk_ratelimit())
  345. WARN_ON(1);
  346. return 0;
  347. iommu_map_failed:
  348. spin_lock_irqsave(&iommu->lock, flags);
  349. arena_free(&iommu->arena, entry, npages);
  350. spin_unlock_irqrestore(&iommu->lock, flags);
  351. return 0;
  352. }
  353. static void dma_4v_unmap_sg(struct device *dev, struct scatterlist *sglist,
  354. int nelems, enum dma_data_direction direction)
  355. {
  356. unsigned long flags, npages;
  357. struct pci_pbm_info *pbm;
  358. u32 devhandle, bus_addr;
  359. struct iommu *iommu;
  360. long entry;
  361. if (unlikely(direction == DMA_NONE)) {
  362. if (printk_ratelimit())
  363. WARN_ON(1);
  364. }
  365. iommu = dev->archdata.iommu;
  366. pbm = dev->archdata.host_controller;
  367. devhandle = pbm->devhandle;
  368. bus_addr = sglist->dma_address & IO_PAGE_MASK;
  369. npages = calc_npages(sglist, nelems);
  370. entry = ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  371. spin_lock_irqsave(&iommu->lock, flags);
  372. arena_free(&iommu->arena, entry, npages);
  373. do {
  374. unsigned long num;
  375. num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
  376. npages);
  377. entry += num;
  378. npages -= num;
  379. } while (npages != 0);
  380. spin_unlock_irqrestore(&iommu->lock, flags);
  381. }
  382. static void dma_4v_sync_single_for_cpu(struct device *dev,
  383. dma_addr_t bus_addr, size_t sz,
  384. enum dma_data_direction direction)
  385. {
  386. /* Nothing to do... */
  387. }
  388. static void dma_4v_sync_sg_for_cpu(struct device *dev,
  389. struct scatterlist *sglist, int nelems,
  390. enum dma_data_direction direction)
  391. {
  392. /* Nothing to do... */
  393. }
  394. const struct dma_ops sun4v_dma_ops = {
  395. .alloc_coherent = dma_4v_alloc_coherent,
  396. .free_coherent = dma_4v_free_coherent,
  397. .map_single = dma_4v_map_single,
  398. .unmap_single = dma_4v_unmap_single,
  399. .map_sg = dma_4v_map_sg,
  400. .unmap_sg = dma_4v_unmap_sg,
  401. .sync_single_for_cpu = dma_4v_sync_single_for_cpu,
  402. .sync_sg_for_cpu = dma_4v_sync_sg_for_cpu,
  403. };
  404. static void __init pci_sun4v_scan_bus(struct pci_pbm_info *pbm)
  405. {
  406. struct property *prop;
  407. struct device_node *dp;
  408. dp = pbm->prom_node;
  409. prop = of_find_property(dp, "66mhz-capable", NULL);
  410. pbm->is_66mhz_capable = (prop != NULL);
  411. pbm->pci_bus = pci_scan_one_pbm(pbm);
  412. /* XXX register error interrupt handlers XXX */
  413. }
  414. static unsigned long __init probe_existing_entries(struct pci_pbm_info *pbm,
  415. struct iommu *iommu)
  416. {
  417. struct iommu_arena *arena = &iommu->arena;
  418. unsigned long i, cnt = 0;
  419. u32 devhandle;
  420. devhandle = pbm->devhandle;
  421. for (i = 0; i < arena->limit; i++) {
  422. unsigned long ret, io_attrs, ra;
  423. ret = pci_sun4v_iommu_getmap(devhandle,
  424. HV_PCI_TSBID(0, i),
  425. &io_attrs, &ra);
  426. if (ret == HV_EOK) {
  427. if (page_in_phys_avail(ra)) {
  428. pci_sun4v_iommu_demap(devhandle,
  429. HV_PCI_TSBID(0, i), 1);
  430. } else {
  431. cnt++;
  432. __set_bit(i, arena->map);
  433. }
  434. }
  435. }
  436. return cnt;
  437. }
  438. static void __init pci_sun4v_iommu_init(struct pci_pbm_info *pbm)
  439. {
  440. struct iommu *iommu = pbm->iommu;
  441. struct property *prop;
  442. unsigned long num_tsb_entries, sz, tsbsize;
  443. u32 vdma[2], dma_mask, dma_offset;
  444. prop = of_find_property(pbm->prom_node, "virtual-dma", NULL);
  445. if (prop) {
  446. u32 *val = prop->value;
  447. vdma[0] = val[0];
  448. vdma[1] = val[1];
  449. } else {
  450. /* No property, use default values. */
  451. vdma[0] = 0x80000000;
  452. vdma[1] = 0x80000000;
  453. }
  454. if ((vdma[0] | vdma[1]) & ~IO_PAGE_MASK) {
  455. prom_printf("PCI-SUN4V: strange virtual-dma[%08x:%08x].\n",
  456. vdma[0], vdma[1]);
  457. prom_halt();
  458. };
  459. dma_mask = (roundup_pow_of_two(vdma[1]) - 1UL);
  460. num_tsb_entries = vdma[1] / IO_PAGE_SIZE;
  461. tsbsize = num_tsb_entries * sizeof(iopte_t);
  462. dma_offset = vdma[0];
  463. /* Setup initial software IOMMU state. */
  464. spin_lock_init(&iommu->lock);
  465. iommu->ctx_lowest_free = 1;
  466. iommu->page_table_map_base = dma_offset;
  467. iommu->dma_addr_mask = dma_mask;
  468. /* Allocate and initialize the free area map. */
  469. sz = (num_tsb_entries + 7) / 8;
  470. sz = (sz + 7UL) & ~7UL;
  471. iommu->arena.map = kzalloc(sz, GFP_KERNEL);
  472. if (!iommu->arena.map) {
  473. prom_printf("PCI_IOMMU: Error, kmalloc(arena.map) failed.\n");
  474. prom_halt();
  475. }
  476. iommu->arena.limit = num_tsb_entries;
  477. sz = probe_existing_entries(pbm, iommu);
  478. if (sz)
  479. printk("%s: Imported %lu TSB entries from OBP\n",
  480. pbm->name, sz);
  481. }
  482. #ifdef CONFIG_PCI_MSI
  483. struct pci_sun4v_msiq_entry {
  484. u64 version_type;
  485. #define MSIQ_VERSION_MASK 0xffffffff00000000UL
  486. #define MSIQ_VERSION_SHIFT 32
  487. #define MSIQ_TYPE_MASK 0x00000000000000ffUL
  488. #define MSIQ_TYPE_SHIFT 0
  489. #define MSIQ_TYPE_NONE 0x00
  490. #define MSIQ_TYPE_MSG 0x01
  491. #define MSIQ_TYPE_MSI32 0x02
  492. #define MSIQ_TYPE_MSI64 0x03
  493. #define MSIQ_TYPE_INTX 0x08
  494. #define MSIQ_TYPE_NONE2 0xff
  495. u64 intx_sysino;
  496. u64 reserved1;
  497. u64 stick;
  498. u64 req_id; /* bus/device/func */
  499. #define MSIQ_REQID_BUS_MASK 0xff00UL
  500. #define MSIQ_REQID_BUS_SHIFT 8
  501. #define MSIQ_REQID_DEVICE_MASK 0x00f8UL
  502. #define MSIQ_REQID_DEVICE_SHIFT 3
  503. #define MSIQ_REQID_FUNC_MASK 0x0007UL
  504. #define MSIQ_REQID_FUNC_SHIFT 0
  505. u64 msi_address;
  506. /* The format of this value is message type dependent.
  507. * For MSI bits 15:0 are the data from the MSI packet.
  508. * For MSI-X bits 31:0 are the data from the MSI packet.
  509. * For MSG, the message code and message routing code where:
  510. * bits 39:32 is the bus/device/fn of the msg target-id
  511. * bits 18:16 is the message routing code
  512. * bits 7:0 is the message code
  513. * For INTx the low order 2-bits are:
  514. * 00 - INTA
  515. * 01 - INTB
  516. * 10 - INTC
  517. * 11 - INTD
  518. */
  519. u64 msi_data;
  520. u64 reserved2;
  521. };
  522. static int pci_sun4v_get_head(struct pci_pbm_info *pbm, unsigned long msiqid,
  523. unsigned long *head)
  524. {
  525. unsigned long err, limit;
  526. err = pci_sun4v_msiq_gethead(pbm->devhandle, msiqid, head);
  527. if (unlikely(err))
  528. return -ENXIO;
  529. limit = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
  530. if (unlikely(*head >= limit))
  531. return -EFBIG;
  532. return 0;
  533. }
  534. static int pci_sun4v_dequeue_msi(struct pci_pbm_info *pbm,
  535. unsigned long msiqid, unsigned long *head,
  536. unsigned long *msi)
  537. {
  538. struct pci_sun4v_msiq_entry *ep;
  539. unsigned long err, type;
  540. /* Note: void pointer arithmetic, 'head' is a byte offset */
  541. ep = (pbm->msi_queues + ((msiqid - pbm->msiq_first) *
  542. (pbm->msiq_ent_count *
  543. sizeof(struct pci_sun4v_msiq_entry))) +
  544. *head);
  545. if ((ep->version_type & MSIQ_TYPE_MASK) == 0)
  546. return 0;
  547. type = (ep->version_type & MSIQ_TYPE_MASK) >> MSIQ_TYPE_SHIFT;
  548. if (unlikely(type != MSIQ_TYPE_MSI32 &&
  549. type != MSIQ_TYPE_MSI64))
  550. return -EINVAL;
  551. *msi = ep->msi_data;
  552. err = pci_sun4v_msi_setstate(pbm->devhandle,
  553. ep->msi_data /* msi_num */,
  554. HV_MSISTATE_IDLE);
  555. if (unlikely(err))
  556. return -ENXIO;
  557. /* Clear the entry. */
  558. ep->version_type &= ~MSIQ_TYPE_MASK;
  559. (*head) += sizeof(struct pci_sun4v_msiq_entry);
  560. if (*head >=
  561. (pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry)))
  562. *head = 0;
  563. return 1;
  564. }
  565. static int pci_sun4v_set_head(struct pci_pbm_info *pbm, unsigned long msiqid,
  566. unsigned long head)
  567. {
  568. unsigned long err;
  569. err = pci_sun4v_msiq_sethead(pbm->devhandle, msiqid, head);
  570. if (unlikely(err))
  571. return -EINVAL;
  572. return 0;
  573. }
  574. static int pci_sun4v_msi_setup(struct pci_pbm_info *pbm, unsigned long msiqid,
  575. unsigned long msi, int is_msi64)
  576. {
  577. if (pci_sun4v_msi_setmsiq(pbm->devhandle, msi, msiqid,
  578. (is_msi64 ?
  579. HV_MSITYPE_MSI64 : HV_MSITYPE_MSI32)))
  580. return -ENXIO;
  581. if (pci_sun4v_msi_setstate(pbm->devhandle, msi, HV_MSISTATE_IDLE))
  582. return -ENXIO;
  583. if (pci_sun4v_msi_setvalid(pbm->devhandle, msi, HV_MSIVALID_VALID))
  584. return -ENXIO;
  585. return 0;
  586. }
  587. static int pci_sun4v_msi_teardown(struct pci_pbm_info *pbm, unsigned long msi)
  588. {
  589. unsigned long err, msiqid;
  590. err = pci_sun4v_msi_getmsiq(pbm->devhandle, msi, &msiqid);
  591. if (err)
  592. return -ENXIO;
  593. pci_sun4v_msi_setvalid(pbm->devhandle, msi, HV_MSIVALID_INVALID);
  594. return 0;
  595. }
  596. static int pci_sun4v_msiq_alloc(struct pci_pbm_info *pbm)
  597. {
  598. unsigned long q_size, alloc_size, pages, order;
  599. int i;
  600. q_size = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
  601. alloc_size = (pbm->msiq_num * q_size);
  602. order = get_order(alloc_size);
  603. pages = __get_free_pages(GFP_KERNEL | __GFP_COMP, order);
  604. if (pages == 0UL) {
  605. printk(KERN_ERR "MSI: Cannot allocate MSI queues (o=%lu).\n",
  606. order);
  607. return -ENOMEM;
  608. }
  609. memset((char *)pages, 0, PAGE_SIZE << order);
  610. pbm->msi_queues = (void *) pages;
  611. for (i = 0; i < pbm->msiq_num; i++) {
  612. unsigned long err, base = __pa(pages + (i * q_size));
  613. unsigned long ret1, ret2;
  614. err = pci_sun4v_msiq_conf(pbm->devhandle,
  615. pbm->msiq_first + i,
  616. base, pbm->msiq_ent_count);
  617. if (err) {
  618. printk(KERN_ERR "MSI: msiq register fails (err=%lu)\n",
  619. err);
  620. goto h_error;
  621. }
  622. err = pci_sun4v_msiq_info(pbm->devhandle,
  623. pbm->msiq_first + i,
  624. &ret1, &ret2);
  625. if (err) {
  626. printk(KERN_ERR "MSI: Cannot read msiq (err=%lu)\n",
  627. err);
  628. goto h_error;
  629. }
  630. if (ret1 != base || ret2 != pbm->msiq_ent_count) {
  631. printk(KERN_ERR "MSI: Bogus qconf "
  632. "expected[%lx:%x] got[%lx:%lx]\n",
  633. base, pbm->msiq_ent_count,
  634. ret1, ret2);
  635. goto h_error;
  636. }
  637. }
  638. return 0;
  639. h_error:
  640. free_pages(pages, order);
  641. return -EINVAL;
  642. }
  643. static void pci_sun4v_msiq_free(struct pci_pbm_info *pbm)
  644. {
  645. unsigned long q_size, alloc_size, pages, order;
  646. int i;
  647. for (i = 0; i < pbm->msiq_num; i++) {
  648. unsigned long msiqid = pbm->msiq_first + i;
  649. (void) pci_sun4v_msiq_conf(pbm->devhandle, msiqid, 0UL, 0);
  650. }
  651. q_size = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
  652. alloc_size = (pbm->msiq_num * q_size);
  653. order = get_order(alloc_size);
  654. pages = (unsigned long) pbm->msi_queues;
  655. free_pages(pages, order);
  656. pbm->msi_queues = NULL;
  657. }
  658. static int pci_sun4v_msiq_build_irq(struct pci_pbm_info *pbm,
  659. unsigned long msiqid,
  660. unsigned long devino)
  661. {
  662. unsigned int virt_irq = sun4v_build_irq(pbm->devhandle, devino);
  663. if (!virt_irq)
  664. return -ENOMEM;
  665. if (pci_sun4v_msiq_setstate(pbm->devhandle, msiqid, HV_MSIQSTATE_IDLE))
  666. return -EINVAL;
  667. if (pci_sun4v_msiq_setvalid(pbm->devhandle, msiqid, HV_MSIQ_VALID))
  668. return -EINVAL;
  669. return virt_irq;
  670. }
  671. static const struct sparc64_msiq_ops pci_sun4v_msiq_ops = {
  672. .get_head = pci_sun4v_get_head,
  673. .dequeue_msi = pci_sun4v_dequeue_msi,
  674. .set_head = pci_sun4v_set_head,
  675. .msi_setup = pci_sun4v_msi_setup,
  676. .msi_teardown = pci_sun4v_msi_teardown,
  677. .msiq_alloc = pci_sun4v_msiq_alloc,
  678. .msiq_free = pci_sun4v_msiq_free,
  679. .msiq_build_irq = pci_sun4v_msiq_build_irq,
  680. };
  681. static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
  682. {
  683. sparc64_pbm_msi_init(pbm, &pci_sun4v_msiq_ops);
  684. }
  685. #else /* CONFIG_PCI_MSI */
  686. static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
  687. {
  688. }
  689. #endif /* !(CONFIG_PCI_MSI) */
  690. static void __init pci_sun4v_pbm_init(struct pci_controller_info *p,
  691. struct device_node *dp, u32 devhandle)
  692. {
  693. struct pci_pbm_info *pbm;
  694. if (devhandle & 0x40)
  695. pbm = &p->pbm_B;
  696. else
  697. pbm = &p->pbm_A;
  698. pbm->next = pci_pbm_root;
  699. pci_pbm_root = pbm;
  700. pbm->scan_bus = pci_sun4v_scan_bus;
  701. pbm->pci_ops = &sun4v_pci_ops;
  702. pbm->config_space_reg_bits = 12;
  703. pbm->index = pci_num_pbms++;
  704. pbm->parent = p;
  705. pbm->prom_node = dp;
  706. pbm->devhandle = devhandle;
  707. pbm->name = dp->full_name;
  708. printk("%s: SUN4V PCI Bus Module\n", pbm->name);
  709. pci_determine_mem_io_space(pbm);
  710. pci_get_pbm_props(pbm);
  711. pci_sun4v_iommu_init(pbm);
  712. pci_sun4v_msi_init(pbm);
  713. }
  714. void __init sun4v_pci_init(struct device_node *dp, char *model_name)
  715. {
  716. static int hvapi_negotiated = 0;
  717. struct pci_controller_info *p;
  718. struct pci_pbm_info *pbm;
  719. struct iommu *iommu;
  720. struct property *prop;
  721. struct linux_prom64_registers *regs;
  722. u32 devhandle;
  723. int i;
  724. if (!hvapi_negotiated++) {
  725. int err = sun4v_hvapi_register(HV_GRP_PCI,
  726. vpci_major,
  727. &vpci_minor);
  728. if (err) {
  729. prom_printf("SUN4V_PCI: Could not register hvapi, "
  730. "err=%d\n", err);
  731. prom_halt();
  732. }
  733. printk("SUN4V_PCI: Registered hvapi major[%lu] minor[%lu]\n",
  734. vpci_major, vpci_minor);
  735. dma_ops = &sun4v_dma_ops;
  736. }
  737. prop = of_find_property(dp, "reg", NULL);
  738. if (!prop) {
  739. prom_printf("SUN4V_PCI: Could not find config registers\n");
  740. prom_halt();
  741. }
  742. regs = prop->value;
  743. devhandle = (regs->phys_addr >> 32UL) & 0x0fffffff;
  744. for (pbm = pci_pbm_root; pbm; pbm = pbm->next) {
  745. if (pbm->devhandle == (devhandle ^ 0x40)) {
  746. pci_sun4v_pbm_init(pbm->parent, dp, devhandle);
  747. return;
  748. }
  749. }
  750. for_each_possible_cpu(i) {
  751. unsigned long page = get_zeroed_page(GFP_ATOMIC);
  752. if (!page)
  753. goto fatal_memory_error;
  754. per_cpu(iommu_batch, i).pglist = (u64 *) page;
  755. }
  756. p = kzalloc(sizeof(struct pci_controller_info), GFP_ATOMIC);
  757. if (!p)
  758. goto fatal_memory_error;
  759. iommu = kzalloc(sizeof(struct iommu), GFP_ATOMIC);
  760. if (!iommu)
  761. goto fatal_memory_error;
  762. p->pbm_A.iommu = iommu;
  763. iommu = kzalloc(sizeof(struct iommu), GFP_ATOMIC);
  764. if (!iommu)
  765. goto fatal_memory_error;
  766. p->pbm_B.iommu = iommu;
  767. pci_sun4v_pbm_init(p, dp, devhandle);
  768. return;
  769. fatal_memory_error:
  770. prom_printf("SUN4V_PCI: Fatal memory allocation error.\n");
  771. prom_halt();
  772. }