pci_sun4v.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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 <linux/of_device.h>
  16. #include <asm/iommu.h>
  17. #include <asm/irq.h>
  18. #include <asm/hypervisor.h>
  19. #include <asm/prom.h>
  20. #include "pci_impl.h"
  21. #include "iommu_common.h"
  22. #include "pci_sun4v.h"
  23. #define DRIVER_NAME "pci_sun4v"
  24. #define PFX DRIVER_NAME ": "
  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. static int iommu_batch_initialized;
  37. /* Interrupts must be disabled. */
  38. static inline void iommu_batch_start(struct device *dev, unsigned long prot, unsigned long entry)
  39. {
  40. struct iommu_batch *p = &__get_cpu_var(iommu_batch);
  41. p->dev = dev;
  42. p->prot = prot;
  43. p->entry = entry;
  44. p->npages = 0;
  45. }
  46. /* Interrupts must be disabled. */
  47. static long iommu_batch_flush(struct iommu_batch *p)
  48. {
  49. struct pci_pbm_info *pbm = p->dev->archdata.host_controller;
  50. unsigned long devhandle = pbm->devhandle;
  51. unsigned long prot = p->prot;
  52. unsigned long entry = p->entry;
  53. u64 *pglist = p->pglist;
  54. unsigned long npages = p->npages;
  55. while (npages != 0) {
  56. long num;
  57. num = pci_sun4v_iommu_map(devhandle, HV_PCI_TSBID(0, entry),
  58. npages, prot, __pa(pglist));
  59. if (unlikely(num < 0)) {
  60. if (printk_ratelimit())
  61. printk("iommu_batch_flush: IOMMU map of "
  62. "[%08lx:%08llx:%lx:%lx:%lx] failed with "
  63. "status %ld\n",
  64. devhandle, HV_PCI_TSBID(0, entry),
  65. npages, prot, __pa(pglist), num);
  66. return -1;
  67. }
  68. entry += num;
  69. npages -= num;
  70. pglist += num;
  71. }
  72. p->entry = entry;
  73. p->npages = 0;
  74. return 0;
  75. }
  76. static inline void iommu_batch_new_entry(unsigned long entry)
  77. {
  78. struct iommu_batch *p = &__get_cpu_var(iommu_batch);
  79. if (p->entry + p->npages == entry)
  80. return;
  81. if (p->entry != ~0UL)
  82. iommu_batch_flush(p);
  83. p->entry = entry;
  84. }
  85. /* Interrupts must be disabled. */
  86. static inline long iommu_batch_add(u64 phys_page)
  87. {
  88. struct iommu_batch *p = &__get_cpu_var(iommu_batch);
  89. BUG_ON(p->npages >= PGLIST_NENTS);
  90. p->pglist[p->npages++] = phys_page;
  91. if (p->npages == PGLIST_NENTS)
  92. return iommu_batch_flush(p);
  93. return 0;
  94. }
  95. /* Interrupts must be disabled. */
  96. static inline long iommu_batch_end(void)
  97. {
  98. struct iommu_batch *p = &__get_cpu_var(iommu_batch);
  99. BUG_ON(p->npages >= PGLIST_NENTS);
  100. return iommu_batch_flush(p);
  101. }
  102. static void *dma_4v_alloc_coherent(struct device *dev, size_t size,
  103. dma_addr_t *dma_addrp, gfp_t gfp)
  104. {
  105. unsigned long flags, order, first_page, npages, n;
  106. struct iommu *iommu;
  107. struct page *page;
  108. void *ret;
  109. long entry;
  110. int nid;
  111. size = IO_PAGE_ALIGN(size);
  112. order = get_order(size);
  113. if (unlikely(order >= MAX_ORDER))
  114. return NULL;
  115. npages = size >> IO_PAGE_SHIFT;
  116. nid = dev->archdata.numa_node;
  117. page = alloc_pages_node(nid, gfp, order);
  118. if (unlikely(!page))
  119. return NULL;
  120. first_page = (unsigned long) page_address(page);
  121. memset((char *)first_page, 0, PAGE_SIZE << order);
  122. iommu = dev->archdata.iommu;
  123. spin_lock_irqsave(&iommu->lock, flags);
  124. entry = iommu_range_alloc(dev, iommu, npages, NULL);
  125. spin_unlock_irqrestore(&iommu->lock, flags);
  126. if (unlikely(entry == DMA_ERROR_CODE))
  127. goto range_alloc_fail;
  128. *dma_addrp = (iommu->page_table_map_base +
  129. (entry << IO_PAGE_SHIFT));
  130. ret = (void *) first_page;
  131. first_page = __pa(first_page);
  132. local_irq_save(flags);
  133. iommu_batch_start(dev,
  134. (HV_PCI_MAP_ATTR_READ |
  135. HV_PCI_MAP_ATTR_WRITE),
  136. entry);
  137. for (n = 0; n < npages; n++) {
  138. long err = iommu_batch_add(first_page + (n * PAGE_SIZE));
  139. if (unlikely(err < 0L))
  140. goto iommu_map_fail;
  141. }
  142. if (unlikely(iommu_batch_end() < 0L))
  143. goto iommu_map_fail;
  144. local_irq_restore(flags);
  145. return ret;
  146. iommu_map_fail:
  147. /* Interrupts are disabled. */
  148. spin_lock(&iommu->lock);
  149. iommu_range_free(iommu, *dma_addrp, npages);
  150. spin_unlock_irqrestore(&iommu->lock, flags);
  151. range_alloc_fail:
  152. free_pages(first_page, order);
  153. return NULL;
  154. }
  155. static void dma_4v_free_coherent(struct device *dev, size_t size, void *cpu,
  156. dma_addr_t dvma)
  157. {
  158. struct pci_pbm_info *pbm;
  159. struct iommu *iommu;
  160. unsigned long flags, order, npages, entry;
  161. u32 devhandle;
  162. npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
  163. iommu = dev->archdata.iommu;
  164. pbm = dev->archdata.host_controller;
  165. devhandle = pbm->devhandle;
  166. entry = ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  167. spin_lock_irqsave(&iommu->lock, flags);
  168. iommu_range_free(iommu, dvma, npages);
  169. do {
  170. unsigned long num;
  171. num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
  172. npages);
  173. entry += num;
  174. npages -= num;
  175. } while (npages != 0);
  176. spin_unlock_irqrestore(&iommu->lock, flags);
  177. order = get_order(size);
  178. if (order < 10)
  179. free_pages((unsigned long)cpu, order);
  180. }
  181. static dma_addr_t dma_4v_map_page(struct device *dev, struct page *page,
  182. unsigned long offset, size_t sz,
  183. enum dma_data_direction direction,
  184. struct dma_attrs *attrs)
  185. {
  186. struct iommu *iommu;
  187. unsigned long flags, npages, oaddr;
  188. unsigned long i, base_paddr;
  189. u32 bus_addr, ret;
  190. unsigned long prot;
  191. long entry;
  192. iommu = dev->archdata.iommu;
  193. if (unlikely(direction == DMA_NONE))
  194. goto bad;
  195. oaddr = (unsigned long)(page_address(page) + offset);
  196. npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
  197. npages >>= IO_PAGE_SHIFT;
  198. spin_lock_irqsave(&iommu->lock, flags);
  199. entry = iommu_range_alloc(dev, iommu, npages, NULL);
  200. spin_unlock_irqrestore(&iommu->lock, flags);
  201. if (unlikely(entry == DMA_ERROR_CODE))
  202. goto bad;
  203. bus_addr = (iommu->page_table_map_base +
  204. (entry << IO_PAGE_SHIFT));
  205. ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
  206. base_paddr = __pa(oaddr & IO_PAGE_MASK);
  207. prot = HV_PCI_MAP_ATTR_READ;
  208. if (direction != DMA_TO_DEVICE)
  209. prot |= HV_PCI_MAP_ATTR_WRITE;
  210. local_irq_save(flags);
  211. iommu_batch_start(dev, prot, entry);
  212. for (i = 0; i < npages; i++, base_paddr += IO_PAGE_SIZE) {
  213. long err = iommu_batch_add(base_paddr);
  214. if (unlikely(err < 0L))
  215. goto iommu_map_fail;
  216. }
  217. if (unlikely(iommu_batch_end() < 0L))
  218. goto iommu_map_fail;
  219. local_irq_restore(flags);
  220. return ret;
  221. bad:
  222. if (printk_ratelimit())
  223. WARN_ON(1);
  224. return DMA_ERROR_CODE;
  225. iommu_map_fail:
  226. /* Interrupts are disabled. */
  227. spin_lock(&iommu->lock);
  228. iommu_range_free(iommu, bus_addr, npages);
  229. spin_unlock_irqrestore(&iommu->lock, flags);
  230. return DMA_ERROR_CODE;
  231. }
  232. static void dma_4v_unmap_page(struct device *dev, dma_addr_t bus_addr,
  233. size_t sz, enum dma_data_direction direction,
  234. struct dma_attrs *attrs)
  235. {
  236. struct pci_pbm_info *pbm;
  237. struct iommu *iommu;
  238. unsigned long flags, npages;
  239. long entry;
  240. u32 devhandle;
  241. if (unlikely(direction == DMA_NONE)) {
  242. if (printk_ratelimit())
  243. WARN_ON(1);
  244. return;
  245. }
  246. iommu = dev->archdata.iommu;
  247. pbm = dev->archdata.host_controller;
  248. devhandle = pbm->devhandle;
  249. npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
  250. npages >>= IO_PAGE_SHIFT;
  251. bus_addr &= IO_PAGE_MASK;
  252. spin_lock_irqsave(&iommu->lock, flags);
  253. iommu_range_free(iommu, bus_addr, npages);
  254. entry = (bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT;
  255. do {
  256. unsigned long num;
  257. num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
  258. npages);
  259. entry += num;
  260. npages -= num;
  261. } while (npages != 0);
  262. spin_unlock_irqrestore(&iommu->lock, flags);
  263. }
  264. static int dma_4v_map_sg(struct device *dev, struct scatterlist *sglist,
  265. int nelems, enum dma_data_direction direction,
  266. struct dma_attrs *attrs)
  267. {
  268. struct scatterlist *s, *outs, *segstart;
  269. unsigned long flags, handle, prot;
  270. dma_addr_t dma_next = 0, dma_addr;
  271. unsigned int max_seg_size;
  272. unsigned long seg_boundary_size;
  273. int outcount, incount, i;
  274. struct iommu *iommu;
  275. unsigned long base_shift;
  276. long err;
  277. BUG_ON(direction == DMA_NONE);
  278. iommu = dev->archdata.iommu;
  279. if (nelems == 0 || !iommu)
  280. return 0;
  281. prot = HV_PCI_MAP_ATTR_READ;
  282. if (direction != DMA_TO_DEVICE)
  283. prot |= HV_PCI_MAP_ATTR_WRITE;
  284. outs = s = segstart = &sglist[0];
  285. outcount = 1;
  286. incount = nelems;
  287. handle = 0;
  288. /* Init first segment length for backout at failure */
  289. outs->dma_length = 0;
  290. spin_lock_irqsave(&iommu->lock, flags);
  291. iommu_batch_start(dev, prot, ~0UL);
  292. max_seg_size = dma_get_max_seg_size(dev);
  293. seg_boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
  294. IO_PAGE_SIZE) >> IO_PAGE_SHIFT;
  295. base_shift = iommu->page_table_map_base >> IO_PAGE_SHIFT;
  296. for_each_sg(sglist, s, nelems, i) {
  297. unsigned long paddr, npages, entry, out_entry = 0, slen;
  298. slen = s->length;
  299. /* Sanity check */
  300. if (slen == 0) {
  301. dma_next = 0;
  302. continue;
  303. }
  304. /* Allocate iommu entries for that segment */
  305. paddr = (unsigned long) SG_ENT_PHYS_ADDRESS(s);
  306. npages = iommu_num_pages(paddr, slen, IO_PAGE_SIZE);
  307. entry = iommu_range_alloc(dev, iommu, npages, &handle);
  308. /* Handle failure */
  309. if (unlikely(entry == DMA_ERROR_CODE)) {
  310. if (printk_ratelimit())
  311. printk(KERN_INFO "iommu_alloc failed, iommu %p paddr %lx"
  312. " npages %lx\n", iommu, paddr, npages);
  313. goto iommu_map_failed;
  314. }
  315. iommu_batch_new_entry(entry);
  316. /* Convert entry to a dma_addr_t */
  317. dma_addr = iommu->page_table_map_base +
  318. (entry << IO_PAGE_SHIFT);
  319. dma_addr |= (s->offset & ~IO_PAGE_MASK);
  320. /* Insert into HW table */
  321. paddr &= IO_PAGE_MASK;
  322. while (npages--) {
  323. err = iommu_batch_add(paddr);
  324. if (unlikely(err < 0L))
  325. goto iommu_map_failed;
  326. paddr += IO_PAGE_SIZE;
  327. }
  328. /* If we are in an open segment, try merging */
  329. if (segstart != s) {
  330. /* We cannot merge if:
  331. * - allocated dma_addr isn't contiguous to previous allocation
  332. */
  333. if ((dma_addr != dma_next) ||
  334. (outs->dma_length + s->length > max_seg_size) ||
  335. (is_span_boundary(out_entry, base_shift,
  336. seg_boundary_size, outs, s))) {
  337. /* Can't merge: create a new segment */
  338. segstart = s;
  339. outcount++;
  340. outs = sg_next(outs);
  341. } else {
  342. outs->dma_length += s->length;
  343. }
  344. }
  345. if (segstart == s) {
  346. /* This is a new segment, fill entries */
  347. outs->dma_address = dma_addr;
  348. outs->dma_length = slen;
  349. out_entry = entry;
  350. }
  351. /* Calculate next page pointer for contiguous check */
  352. dma_next = dma_addr + slen;
  353. }
  354. err = iommu_batch_end();
  355. if (unlikely(err < 0L))
  356. goto iommu_map_failed;
  357. spin_unlock_irqrestore(&iommu->lock, flags);
  358. if (outcount < incount) {
  359. outs = sg_next(outs);
  360. outs->dma_address = DMA_ERROR_CODE;
  361. outs->dma_length = 0;
  362. }
  363. return outcount;
  364. iommu_map_failed:
  365. for_each_sg(sglist, s, nelems, i) {
  366. if (s->dma_length != 0) {
  367. unsigned long vaddr, npages;
  368. vaddr = s->dma_address & IO_PAGE_MASK;
  369. npages = iommu_num_pages(s->dma_address, s->dma_length,
  370. IO_PAGE_SIZE);
  371. iommu_range_free(iommu, vaddr, npages);
  372. /* XXX demap? XXX */
  373. s->dma_address = DMA_ERROR_CODE;
  374. s->dma_length = 0;
  375. }
  376. if (s == outs)
  377. break;
  378. }
  379. spin_unlock_irqrestore(&iommu->lock, flags);
  380. return 0;
  381. }
  382. static void dma_4v_unmap_sg(struct device *dev, struct scatterlist *sglist,
  383. int nelems, enum dma_data_direction direction,
  384. struct dma_attrs *attrs)
  385. {
  386. struct pci_pbm_info *pbm;
  387. struct scatterlist *sg;
  388. struct iommu *iommu;
  389. unsigned long flags;
  390. u32 devhandle;
  391. BUG_ON(direction == DMA_NONE);
  392. iommu = dev->archdata.iommu;
  393. pbm = dev->archdata.host_controller;
  394. devhandle = pbm->devhandle;
  395. spin_lock_irqsave(&iommu->lock, flags);
  396. sg = sglist;
  397. while (nelems--) {
  398. dma_addr_t dma_handle = sg->dma_address;
  399. unsigned int len = sg->dma_length;
  400. unsigned long npages, entry;
  401. if (!len)
  402. break;
  403. npages = iommu_num_pages(dma_handle, len, IO_PAGE_SIZE);
  404. iommu_range_free(iommu, dma_handle, npages);
  405. entry = ((dma_handle - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  406. while (npages) {
  407. unsigned long num;
  408. num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
  409. npages);
  410. entry += num;
  411. npages -= num;
  412. }
  413. sg = sg_next(sg);
  414. }
  415. spin_unlock_irqrestore(&iommu->lock, flags);
  416. }
  417. static struct dma_map_ops sun4v_dma_ops = {
  418. .alloc_coherent = dma_4v_alloc_coherent,
  419. .free_coherent = dma_4v_free_coherent,
  420. .map_page = dma_4v_map_page,
  421. .unmap_page = dma_4v_unmap_page,
  422. .map_sg = dma_4v_map_sg,
  423. .unmap_sg = dma_4v_unmap_sg,
  424. };
  425. static void __devinit pci_sun4v_scan_bus(struct pci_pbm_info *pbm,
  426. struct device *parent)
  427. {
  428. struct property *prop;
  429. struct device_node *dp;
  430. dp = pbm->op->dev.of_node;
  431. prop = of_find_property(dp, "66mhz-capable", NULL);
  432. pbm->is_66mhz_capable = (prop != NULL);
  433. pbm->pci_bus = pci_scan_one_pbm(pbm, parent);
  434. /* XXX register error interrupt handlers XXX */
  435. }
  436. static unsigned long __devinit probe_existing_entries(struct pci_pbm_info *pbm,
  437. struct iommu *iommu)
  438. {
  439. struct iommu_arena *arena = &iommu->arena;
  440. unsigned long i, cnt = 0;
  441. u32 devhandle;
  442. devhandle = pbm->devhandle;
  443. for (i = 0; i < arena->limit; i++) {
  444. unsigned long ret, io_attrs, ra;
  445. ret = pci_sun4v_iommu_getmap(devhandle,
  446. HV_PCI_TSBID(0, i),
  447. &io_attrs, &ra);
  448. if (ret == HV_EOK) {
  449. if (page_in_phys_avail(ra)) {
  450. pci_sun4v_iommu_demap(devhandle,
  451. HV_PCI_TSBID(0, i), 1);
  452. } else {
  453. cnt++;
  454. __set_bit(i, arena->map);
  455. }
  456. }
  457. }
  458. return cnt;
  459. }
  460. static int __devinit pci_sun4v_iommu_init(struct pci_pbm_info *pbm)
  461. {
  462. static const u32 vdma_default[] = { 0x80000000, 0x80000000 };
  463. struct iommu *iommu = pbm->iommu;
  464. unsigned long num_tsb_entries, sz;
  465. u32 dma_mask, dma_offset;
  466. const u32 *vdma;
  467. vdma = of_get_property(pbm->op->dev.of_node, "virtual-dma", NULL);
  468. if (!vdma)
  469. vdma = vdma_default;
  470. if ((vdma[0] | vdma[1]) & ~IO_PAGE_MASK) {
  471. printk(KERN_ERR PFX "Strange virtual-dma[%08x:%08x].\n",
  472. vdma[0], vdma[1]);
  473. return -EINVAL;
  474. };
  475. dma_mask = (roundup_pow_of_two(vdma[1]) - 1UL);
  476. num_tsb_entries = vdma[1] / IO_PAGE_SIZE;
  477. dma_offset = vdma[0];
  478. /* Setup initial software IOMMU state. */
  479. spin_lock_init(&iommu->lock);
  480. iommu->ctx_lowest_free = 1;
  481. iommu->page_table_map_base = dma_offset;
  482. iommu->dma_addr_mask = dma_mask;
  483. /* Allocate and initialize the free area map. */
  484. sz = (num_tsb_entries + 7) / 8;
  485. sz = (sz + 7UL) & ~7UL;
  486. iommu->arena.map = kzalloc(sz, GFP_KERNEL);
  487. if (!iommu->arena.map) {
  488. printk(KERN_ERR PFX "Error, kmalloc(arena.map) failed.\n");
  489. return -ENOMEM;
  490. }
  491. iommu->arena.limit = num_tsb_entries;
  492. sz = probe_existing_entries(pbm, iommu);
  493. if (sz)
  494. printk("%s: Imported %lu TSB entries from OBP\n",
  495. pbm->name, sz);
  496. return 0;
  497. }
  498. #ifdef CONFIG_PCI_MSI
  499. struct pci_sun4v_msiq_entry {
  500. u64 version_type;
  501. #define MSIQ_VERSION_MASK 0xffffffff00000000UL
  502. #define MSIQ_VERSION_SHIFT 32
  503. #define MSIQ_TYPE_MASK 0x00000000000000ffUL
  504. #define MSIQ_TYPE_SHIFT 0
  505. #define MSIQ_TYPE_NONE 0x00
  506. #define MSIQ_TYPE_MSG 0x01
  507. #define MSIQ_TYPE_MSI32 0x02
  508. #define MSIQ_TYPE_MSI64 0x03
  509. #define MSIQ_TYPE_INTX 0x08
  510. #define MSIQ_TYPE_NONE2 0xff
  511. u64 intx_sysino;
  512. u64 reserved1;
  513. u64 stick;
  514. u64 req_id; /* bus/device/func */
  515. #define MSIQ_REQID_BUS_MASK 0xff00UL
  516. #define MSIQ_REQID_BUS_SHIFT 8
  517. #define MSIQ_REQID_DEVICE_MASK 0x00f8UL
  518. #define MSIQ_REQID_DEVICE_SHIFT 3
  519. #define MSIQ_REQID_FUNC_MASK 0x0007UL
  520. #define MSIQ_REQID_FUNC_SHIFT 0
  521. u64 msi_address;
  522. /* The format of this value is message type dependent.
  523. * For MSI bits 15:0 are the data from the MSI packet.
  524. * For MSI-X bits 31:0 are the data from the MSI packet.
  525. * For MSG, the message code and message routing code where:
  526. * bits 39:32 is the bus/device/fn of the msg target-id
  527. * bits 18:16 is the message routing code
  528. * bits 7:0 is the message code
  529. * For INTx the low order 2-bits are:
  530. * 00 - INTA
  531. * 01 - INTB
  532. * 10 - INTC
  533. * 11 - INTD
  534. */
  535. u64 msi_data;
  536. u64 reserved2;
  537. };
  538. static int pci_sun4v_get_head(struct pci_pbm_info *pbm, unsigned long msiqid,
  539. unsigned long *head)
  540. {
  541. unsigned long err, limit;
  542. err = pci_sun4v_msiq_gethead(pbm->devhandle, msiqid, head);
  543. if (unlikely(err))
  544. return -ENXIO;
  545. limit = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
  546. if (unlikely(*head >= limit))
  547. return -EFBIG;
  548. return 0;
  549. }
  550. static int pci_sun4v_dequeue_msi(struct pci_pbm_info *pbm,
  551. unsigned long msiqid, unsigned long *head,
  552. unsigned long *msi)
  553. {
  554. struct pci_sun4v_msiq_entry *ep;
  555. unsigned long err, type;
  556. /* Note: void pointer arithmetic, 'head' is a byte offset */
  557. ep = (pbm->msi_queues + ((msiqid - pbm->msiq_first) *
  558. (pbm->msiq_ent_count *
  559. sizeof(struct pci_sun4v_msiq_entry))) +
  560. *head);
  561. if ((ep->version_type & MSIQ_TYPE_MASK) == 0)
  562. return 0;
  563. type = (ep->version_type & MSIQ_TYPE_MASK) >> MSIQ_TYPE_SHIFT;
  564. if (unlikely(type != MSIQ_TYPE_MSI32 &&
  565. type != MSIQ_TYPE_MSI64))
  566. return -EINVAL;
  567. *msi = ep->msi_data;
  568. err = pci_sun4v_msi_setstate(pbm->devhandle,
  569. ep->msi_data /* msi_num */,
  570. HV_MSISTATE_IDLE);
  571. if (unlikely(err))
  572. return -ENXIO;
  573. /* Clear the entry. */
  574. ep->version_type &= ~MSIQ_TYPE_MASK;
  575. (*head) += sizeof(struct pci_sun4v_msiq_entry);
  576. if (*head >=
  577. (pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry)))
  578. *head = 0;
  579. return 1;
  580. }
  581. static int pci_sun4v_set_head(struct pci_pbm_info *pbm, unsigned long msiqid,
  582. unsigned long head)
  583. {
  584. unsigned long err;
  585. err = pci_sun4v_msiq_sethead(pbm->devhandle, msiqid, head);
  586. if (unlikely(err))
  587. return -EINVAL;
  588. return 0;
  589. }
  590. static int pci_sun4v_msi_setup(struct pci_pbm_info *pbm, unsigned long msiqid,
  591. unsigned long msi, int is_msi64)
  592. {
  593. if (pci_sun4v_msi_setmsiq(pbm->devhandle, msi, msiqid,
  594. (is_msi64 ?
  595. HV_MSITYPE_MSI64 : HV_MSITYPE_MSI32)))
  596. return -ENXIO;
  597. if (pci_sun4v_msi_setstate(pbm->devhandle, msi, HV_MSISTATE_IDLE))
  598. return -ENXIO;
  599. if (pci_sun4v_msi_setvalid(pbm->devhandle, msi, HV_MSIVALID_VALID))
  600. return -ENXIO;
  601. return 0;
  602. }
  603. static int pci_sun4v_msi_teardown(struct pci_pbm_info *pbm, unsigned long msi)
  604. {
  605. unsigned long err, msiqid;
  606. err = pci_sun4v_msi_getmsiq(pbm->devhandle, msi, &msiqid);
  607. if (err)
  608. return -ENXIO;
  609. pci_sun4v_msi_setvalid(pbm->devhandle, msi, HV_MSIVALID_INVALID);
  610. return 0;
  611. }
  612. static int pci_sun4v_msiq_alloc(struct pci_pbm_info *pbm)
  613. {
  614. unsigned long q_size, alloc_size, pages, order;
  615. int i;
  616. q_size = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
  617. alloc_size = (pbm->msiq_num * q_size);
  618. order = get_order(alloc_size);
  619. pages = __get_free_pages(GFP_KERNEL | __GFP_COMP, order);
  620. if (pages == 0UL) {
  621. printk(KERN_ERR "MSI: Cannot allocate MSI queues (o=%lu).\n",
  622. order);
  623. return -ENOMEM;
  624. }
  625. memset((char *)pages, 0, PAGE_SIZE << order);
  626. pbm->msi_queues = (void *) pages;
  627. for (i = 0; i < pbm->msiq_num; i++) {
  628. unsigned long err, base = __pa(pages + (i * q_size));
  629. unsigned long ret1, ret2;
  630. err = pci_sun4v_msiq_conf(pbm->devhandle,
  631. pbm->msiq_first + i,
  632. base, pbm->msiq_ent_count);
  633. if (err) {
  634. printk(KERN_ERR "MSI: msiq register fails (err=%lu)\n",
  635. err);
  636. goto h_error;
  637. }
  638. err = pci_sun4v_msiq_info(pbm->devhandle,
  639. pbm->msiq_first + i,
  640. &ret1, &ret2);
  641. if (err) {
  642. printk(KERN_ERR "MSI: Cannot read msiq (err=%lu)\n",
  643. err);
  644. goto h_error;
  645. }
  646. if (ret1 != base || ret2 != pbm->msiq_ent_count) {
  647. printk(KERN_ERR "MSI: Bogus qconf "
  648. "expected[%lx:%x] got[%lx:%lx]\n",
  649. base, pbm->msiq_ent_count,
  650. ret1, ret2);
  651. goto h_error;
  652. }
  653. }
  654. return 0;
  655. h_error:
  656. free_pages(pages, order);
  657. return -EINVAL;
  658. }
  659. static void pci_sun4v_msiq_free(struct pci_pbm_info *pbm)
  660. {
  661. unsigned long q_size, alloc_size, pages, order;
  662. int i;
  663. for (i = 0; i < pbm->msiq_num; i++) {
  664. unsigned long msiqid = pbm->msiq_first + i;
  665. (void) pci_sun4v_msiq_conf(pbm->devhandle, msiqid, 0UL, 0);
  666. }
  667. q_size = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
  668. alloc_size = (pbm->msiq_num * q_size);
  669. order = get_order(alloc_size);
  670. pages = (unsigned long) pbm->msi_queues;
  671. free_pages(pages, order);
  672. pbm->msi_queues = NULL;
  673. }
  674. static int pci_sun4v_msiq_build_irq(struct pci_pbm_info *pbm,
  675. unsigned long msiqid,
  676. unsigned long devino)
  677. {
  678. unsigned int irq = sun4v_build_irq(pbm->devhandle, devino);
  679. if (!irq)
  680. return -ENOMEM;
  681. if (pci_sun4v_msiq_setvalid(pbm->devhandle, msiqid, HV_MSIQ_VALID))
  682. return -EINVAL;
  683. if (pci_sun4v_msiq_setstate(pbm->devhandle, msiqid, HV_MSIQSTATE_IDLE))
  684. return -EINVAL;
  685. return irq;
  686. }
  687. static const struct sparc64_msiq_ops pci_sun4v_msiq_ops = {
  688. .get_head = pci_sun4v_get_head,
  689. .dequeue_msi = pci_sun4v_dequeue_msi,
  690. .set_head = pci_sun4v_set_head,
  691. .msi_setup = pci_sun4v_msi_setup,
  692. .msi_teardown = pci_sun4v_msi_teardown,
  693. .msiq_alloc = pci_sun4v_msiq_alloc,
  694. .msiq_free = pci_sun4v_msiq_free,
  695. .msiq_build_irq = pci_sun4v_msiq_build_irq,
  696. };
  697. static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
  698. {
  699. sparc64_pbm_msi_init(pbm, &pci_sun4v_msiq_ops);
  700. }
  701. #else /* CONFIG_PCI_MSI */
  702. static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
  703. {
  704. }
  705. #endif /* !(CONFIG_PCI_MSI) */
  706. static int __devinit pci_sun4v_pbm_init(struct pci_pbm_info *pbm,
  707. struct platform_device *op, u32 devhandle)
  708. {
  709. struct device_node *dp = op->dev.of_node;
  710. int err;
  711. pbm->numa_node = of_node_to_nid(dp);
  712. pbm->pci_ops = &sun4v_pci_ops;
  713. pbm->config_space_reg_bits = 12;
  714. pbm->index = pci_num_pbms++;
  715. pbm->op = op;
  716. pbm->devhandle = devhandle;
  717. pbm->name = dp->full_name;
  718. printk("%s: SUN4V PCI Bus Module\n", pbm->name);
  719. printk("%s: On NUMA node %d\n", pbm->name, pbm->numa_node);
  720. pci_determine_mem_io_space(pbm);
  721. pci_get_pbm_props(pbm);
  722. err = pci_sun4v_iommu_init(pbm);
  723. if (err)
  724. return err;
  725. pci_sun4v_msi_init(pbm);
  726. pci_sun4v_scan_bus(pbm, &op->dev);
  727. pbm->next = pci_pbm_root;
  728. pci_pbm_root = pbm;
  729. return 0;
  730. }
  731. static int __devinit pci_sun4v_probe(struct platform_device *op)
  732. {
  733. const struct linux_prom64_registers *regs;
  734. static int hvapi_negotiated = 0;
  735. struct pci_pbm_info *pbm;
  736. struct device_node *dp;
  737. struct iommu *iommu;
  738. u32 devhandle;
  739. int i, err;
  740. dp = op->dev.of_node;
  741. if (!hvapi_negotiated++) {
  742. err = sun4v_hvapi_register(HV_GRP_PCI,
  743. vpci_major,
  744. &vpci_minor);
  745. if (err) {
  746. printk(KERN_ERR PFX "Could not register hvapi, "
  747. "err=%d\n", err);
  748. return err;
  749. }
  750. printk(KERN_INFO PFX "Registered hvapi major[%lu] minor[%lu]\n",
  751. vpci_major, vpci_minor);
  752. dma_ops = &sun4v_dma_ops;
  753. }
  754. regs = of_get_property(dp, "reg", NULL);
  755. err = -ENODEV;
  756. if (!regs) {
  757. printk(KERN_ERR PFX "Could not find config registers\n");
  758. goto out_err;
  759. }
  760. devhandle = (regs->phys_addr >> 32UL) & 0x0fffffff;
  761. err = -ENOMEM;
  762. if (!iommu_batch_initialized) {
  763. for_each_possible_cpu(i) {
  764. unsigned long page = get_zeroed_page(GFP_KERNEL);
  765. if (!page)
  766. goto out_err;
  767. per_cpu(iommu_batch, i).pglist = (u64 *) page;
  768. }
  769. iommu_batch_initialized = 1;
  770. }
  771. pbm = kzalloc(sizeof(*pbm), GFP_KERNEL);
  772. if (!pbm) {
  773. printk(KERN_ERR PFX "Could not allocate pci_pbm_info\n");
  774. goto out_err;
  775. }
  776. iommu = kzalloc(sizeof(struct iommu), GFP_KERNEL);
  777. if (!iommu) {
  778. printk(KERN_ERR PFX "Could not allocate pbm iommu\n");
  779. goto out_free_controller;
  780. }
  781. pbm->iommu = iommu;
  782. err = pci_sun4v_pbm_init(pbm, op, devhandle);
  783. if (err)
  784. goto out_free_iommu;
  785. dev_set_drvdata(&op->dev, pbm);
  786. return 0;
  787. out_free_iommu:
  788. kfree(pbm->iommu);
  789. out_free_controller:
  790. kfree(pbm);
  791. out_err:
  792. return err;
  793. }
  794. static const struct of_device_id pci_sun4v_match[] = {
  795. {
  796. .name = "pci",
  797. .compatible = "SUNW,sun4v-pci",
  798. },
  799. {},
  800. };
  801. static struct platform_driver pci_sun4v_driver = {
  802. .driver = {
  803. .name = DRIVER_NAME,
  804. .owner = THIS_MODULE,
  805. .of_match_table = pci_sun4v_match,
  806. },
  807. .probe = pci_sun4v_probe,
  808. };
  809. static int __init pci_sun4v_init(void)
  810. {
  811. return platform_driver_register(&pci_sun4v_driver);
  812. }
  813. subsys_initcall(pci_sun4v_init);