pci_sun4v.c 22 KB

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