pci_sun4v.c 24 KB

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