pci_sun4v.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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. static inline void iommu_batch_new_entry(unsigned long entry)
  76. {
  77. struct iommu_batch *p = &__get_cpu_var(iommu_batch);
  78. if (p->entry + p->npages == entry)
  79. return;
  80. if (p->entry != ~0UL)
  81. iommu_batch_flush(p);
  82. p->entry = entry;
  83. }
  84. /* Interrupts must be disabled. */
  85. static inline long iommu_batch_add(u64 phys_page)
  86. {
  87. struct iommu_batch *p = &__get_cpu_var(iommu_batch);
  88. BUG_ON(p->npages >= PGLIST_NENTS);
  89. p->pglist[p->npages++] = phys_page;
  90. if (p->npages == PGLIST_NENTS)
  91. return iommu_batch_flush(p);
  92. return 0;
  93. }
  94. /* Interrupts must be disabled. */
  95. static inline long iommu_batch_end(void)
  96. {
  97. struct iommu_batch *p = &__get_cpu_var(iommu_batch);
  98. BUG_ON(p->npages >= PGLIST_NENTS);
  99. return iommu_batch_flush(p);
  100. }
  101. static void *dma_4v_alloc_coherent(struct device *dev, size_t size,
  102. dma_addr_t *dma_addrp, gfp_t gfp)
  103. {
  104. struct iommu *iommu;
  105. unsigned long flags, order, first_page, npages, n;
  106. void *ret;
  107. long entry;
  108. size = IO_PAGE_ALIGN(size);
  109. order = get_order(size);
  110. if (unlikely(order >= MAX_ORDER))
  111. return NULL;
  112. npages = size >> IO_PAGE_SHIFT;
  113. first_page = __get_free_pages(gfp, order);
  114. if (unlikely(first_page == 0UL))
  115. return NULL;
  116. memset((char *)first_page, 0, PAGE_SIZE << order);
  117. iommu = dev->archdata.iommu;
  118. spin_lock_irqsave(&iommu->lock, flags);
  119. entry = iommu_range_alloc(dev, iommu, npages, NULL);
  120. spin_unlock_irqrestore(&iommu->lock, flags);
  121. if (unlikely(entry == DMA_ERROR_CODE))
  122. goto range_alloc_fail;
  123. *dma_addrp = (iommu->page_table_map_base +
  124. (entry << IO_PAGE_SHIFT));
  125. ret = (void *) first_page;
  126. first_page = __pa(first_page);
  127. local_irq_save(flags);
  128. iommu_batch_start(dev,
  129. (HV_PCI_MAP_ATTR_READ |
  130. HV_PCI_MAP_ATTR_WRITE),
  131. entry);
  132. for (n = 0; n < npages; n++) {
  133. long err = iommu_batch_add(first_page + (n * PAGE_SIZE));
  134. if (unlikely(err < 0L))
  135. goto iommu_map_fail;
  136. }
  137. if (unlikely(iommu_batch_end() < 0L))
  138. goto iommu_map_fail;
  139. local_irq_restore(flags);
  140. return ret;
  141. iommu_map_fail:
  142. /* Interrupts are disabled. */
  143. spin_lock(&iommu->lock);
  144. iommu_range_free(iommu, *dma_addrp, npages);
  145. spin_unlock_irqrestore(&iommu->lock, flags);
  146. range_alloc_fail:
  147. free_pages(first_page, order);
  148. return NULL;
  149. }
  150. static void dma_4v_free_coherent(struct device *dev, size_t size, void *cpu,
  151. dma_addr_t dvma)
  152. {
  153. struct pci_pbm_info *pbm;
  154. struct iommu *iommu;
  155. unsigned long flags, order, npages, entry;
  156. u32 devhandle;
  157. npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
  158. iommu = dev->archdata.iommu;
  159. pbm = dev->archdata.host_controller;
  160. devhandle = pbm->devhandle;
  161. entry = ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  162. spin_lock_irqsave(&iommu->lock, flags);
  163. iommu_range_free(iommu, dvma, npages);
  164. do {
  165. unsigned long num;
  166. num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
  167. npages);
  168. entry += num;
  169. npages -= num;
  170. } while (npages != 0);
  171. spin_unlock_irqrestore(&iommu->lock, flags);
  172. order = get_order(size);
  173. if (order < 10)
  174. free_pages((unsigned long)cpu, order);
  175. }
  176. static dma_addr_t dma_4v_map_single(struct device *dev, void *ptr, size_t sz,
  177. enum dma_data_direction direction)
  178. {
  179. struct iommu *iommu;
  180. unsigned long flags, npages, oaddr;
  181. unsigned long i, base_paddr;
  182. u32 bus_addr, ret;
  183. unsigned long prot;
  184. long entry;
  185. iommu = dev->archdata.iommu;
  186. if (unlikely(direction == DMA_NONE))
  187. goto bad;
  188. oaddr = (unsigned long)ptr;
  189. npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
  190. npages >>= IO_PAGE_SHIFT;
  191. spin_lock_irqsave(&iommu->lock, flags);
  192. entry = iommu_range_alloc(dev, iommu, npages, NULL);
  193. spin_unlock_irqrestore(&iommu->lock, flags);
  194. if (unlikely(entry == DMA_ERROR_CODE))
  195. goto bad;
  196. bus_addr = (iommu->page_table_map_base +
  197. (entry << IO_PAGE_SHIFT));
  198. ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
  199. base_paddr = __pa(oaddr & IO_PAGE_MASK);
  200. prot = HV_PCI_MAP_ATTR_READ;
  201. if (direction != DMA_TO_DEVICE)
  202. prot |= HV_PCI_MAP_ATTR_WRITE;
  203. local_irq_save(flags);
  204. iommu_batch_start(dev, prot, entry);
  205. for (i = 0; i < npages; i++, base_paddr += IO_PAGE_SIZE) {
  206. long err = iommu_batch_add(base_paddr);
  207. if (unlikely(err < 0L))
  208. goto iommu_map_fail;
  209. }
  210. if (unlikely(iommu_batch_end() < 0L))
  211. goto iommu_map_fail;
  212. local_irq_restore(flags);
  213. return ret;
  214. bad:
  215. if (printk_ratelimit())
  216. WARN_ON(1);
  217. return DMA_ERROR_CODE;
  218. iommu_map_fail:
  219. /* Interrupts are disabled. */
  220. spin_lock(&iommu->lock);
  221. iommu_range_free(iommu, bus_addr, npages);
  222. spin_unlock_irqrestore(&iommu->lock, flags);
  223. return DMA_ERROR_CODE;
  224. }
  225. static void dma_4v_unmap_single(struct device *dev, dma_addr_t bus_addr,
  226. size_t sz, enum dma_data_direction direction)
  227. {
  228. struct pci_pbm_info *pbm;
  229. struct iommu *iommu;
  230. unsigned long flags, npages;
  231. long entry;
  232. u32 devhandle;
  233. if (unlikely(direction == DMA_NONE)) {
  234. if (printk_ratelimit())
  235. WARN_ON(1);
  236. return;
  237. }
  238. iommu = dev->archdata.iommu;
  239. pbm = dev->archdata.host_controller;
  240. devhandle = pbm->devhandle;
  241. npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
  242. npages >>= IO_PAGE_SHIFT;
  243. bus_addr &= IO_PAGE_MASK;
  244. spin_lock_irqsave(&iommu->lock, flags);
  245. iommu_range_free(iommu, bus_addr, npages);
  246. entry = (bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT;
  247. do {
  248. unsigned long num;
  249. num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
  250. npages);
  251. entry += num;
  252. npages -= num;
  253. } while (npages != 0);
  254. spin_unlock_irqrestore(&iommu->lock, flags);
  255. }
  256. static int dma_4v_map_sg(struct device *dev, struct scatterlist *sglist,
  257. int nelems, enum dma_data_direction direction)
  258. {
  259. struct scatterlist *s, *outs, *segstart;
  260. unsigned long flags, handle, prot;
  261. dma_addr_t dma_next = 0, dma_addr;
  262. unsigned int max_seg_size;
  263. int outcount, incount, i;
  264. struct iommu *iommu;
  265. long err;
  266. BUG_ON(direction == DMA_NONE);
  267. iommu = dev->archdata.iommu;
  268. if (nelems == 0 || !iommu)
  269. return 0;
  270. prot = HV_PCI_MAP_ATTR_READ;
  271. if (direction != DMA_TO_DEVICE)
  272. prot |= HV_PCI_MAP_ATTR_WRITE;
  273. outs = s = segstart = &sglist[0];
  274. outcount = 1;
  275. incount = nelems;
  276. handle = 0;
  277. /* Init first segment length for backout at failure */
  278. outs->dma_length = 0;
  279. spin_lock_irqsave(&iommu->lock, flags);
  280. iommu_batch_start(dev, prot, ~0UL);
  281. max_seg_size = dma_get_max_seg_size(dev);
  282. for_each_sg(sglist, s, nelems, i) {
  283. unsigned long paddr, npages, entry, slen;
  284. slen = s->length;
  285. /* Sanity check */
  286. if (slen == 0) {
  287. dma_next = 0;
  288. continue;
  289. }
  290. /* Allocate iommu entries for that segment */
  291. paddr = (unsigned long) SG_ENT_PHYS_ADDRESS(s);
  292. npages = iommu_num_pages(paddr, slen);
  293. entry = iommu_range_alloc(dev, iommu, npages, &handle);
  294. /* Handle failure */
  295. if (unlikely(entry == DMA_ERROR_CODE)) {
  296. if (printk_ratelimit())
  297. printk(KERN_INFO "iommu_alloc failed, iommu %p paddr %lx"
  298. " npages %lx\n", iommu, paddr, npages);
  299. goto iommu_map_failed;
  300. }
  301. iommu_batch_new_entry(entry);
  302. /* Convert entry to a dma_addr_t */
  303. dma_addr = iommu->page_table_map_base +
  304. (entry << IO_PAGE_SHIFT);
  305. dma_addr |= (s->offset & ~IO_PAGE_MASK);
  306. /* Insert into HW table */
  307. paddr &= IO_PAGE_MASK;
  308. while (npages--) {
  309. err = iommu_batch_add(paddr);
  310. if (unlikely(err < 0L))
  311. goto iommu_map_failed;
  312. paddr += IO_PAGE_SIZE;
  313. }
  314. /* If we are in an open segment, try merging */
  315. if (segstart != s) {
  316. /* We cannot merge if:
  317. * - allocated dma_addr isn't contiguous to previous allocation
  318. */
  319. if ((dma_addr != dma_next) ||
  320. (outs->dma_length + s->length > max_seg_size)) {
  321. /* Can't merge: create a new segment */
  322. segstart = s;
  323. outcount++;
  324. outs = sg_next(outs);
  325. } else {
  326. outs->dma_length += s->length;
  327. }
  328. }
  329. if (segstart == s) {
  330. /* This is a new segment, fill entries */
  331. outs->dma_address = dma_addr;
  332. outs->dma_length = slen;
  333. }
  334. /* Calculate next page pointer for contiguous check */
  335. dma_next = dma_addr + slen;
  336. }
  337. err = iommu_batch_end();
  338. if (unlikely(err < 0L))
  339. goto iommu_map_failed;
  340. spin_unlock_irqrestore(&iommu->lock, flags);
  341. if (outcount < incount) {
  342. outs = sg_next(outs);
  343. outs->dma_address = DMA_ERROR_CODE;
  344. outs->dma_length = 0;
  345. }
  346. return outcount;
  347. iommu_map_failed:
  348. for_each_sg(sglist, s, nelems, i) {
  349. if (s->dma_length != 0) {
  350. unsigned long vaddr, npages;
  351. vaddr = s->dma_address & IO_PAGE_MASK;
  352. npages = iommu_num_pages(s->dma_address, s->dma_length);
  353. iommu_range_free(iommu, vaddr, npages);
  354. /* XXX demap? XXX */
  355. s->dma_address = DMA_ERROR_CODE;
  356. s->dma_length = 0;
  357. }
  358. if (s == outs)
  359. break;
  360. }
  361. spin_unlock_irqrestore(&iommu->lock, flags);
  362. return 0;
  363. }
  364. static void dma_4v_unmap_sg(struct device *dev, struct scatterlist *sglist,
  365. int nelems, enum dma_data_direction direction)
  366. {
  367. struct pci_pbm_info *pbm;
  368. struct scatterlist *sg;
  369. struct iommu *iommu;
  370. unsigned long flags;
  371. u32 devhandle;
  372. BUG_ON(direction == DMA_NONE);
  373. iommu = dev->archdata.iommu;
  374. pbm = dev->archdata.host_controller;
  375. devhandle = pbm->devhandle;
  376. spin_lock_irqsave(&iommu->lock, flags);
  377. sg = sglist;
  378. while (nelems--) {
  379. dma_addr_t dma_handle = sg->dma_address;
  380. unsigned int len = sg->dma_length;
  381. unsigned long npages, entry;
  382. if (!len)
  383. break;
  384. npages = iommu_num_pages(dma_handle, len);
  385. iommu_range_free(iommu, dma_handle, npages);
  386. entry = ((dma_handle - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  387. while (npages) {
  388. unsigned long num;
  389. num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
  390. npages);
  391. entry += num;
  392. npages -= num;
  393. }
  394. sg = sg_next(sg);
  395. }
  396. spin_unlock_irqrestore(&iommu->lock, flags);
  397. }
  398. static void dma_4v_sync_single_for_cpu(struct device *dev,
  399. dma_addr_t bus_addr, size_t sz,
  400. enum dma_data_direction direction)
  401. {
  402. /* Nothing to do... */
  403. }
  404. static void dma_4v_sync_sg_for_cpu(struct device *dev,
  405. struct scatterlist *sglist, int nelems,
  406. enum dma_data_direction direction)
  407. {
  408. /* Nothing to do... */
  409. }
  410. const struct dma_ops sun4v_dma_ops = {
  411. .alloc_coherent = dma_4v_alloc_coherent,
  412. .free_coherent = dma_4v_free_coherent,
  413. .map_single = dma_4v_map_single,
  414. .unmap_single = dma_4v_unmap_single,
  415. .map_sg = dma_4v_map_sg,
  416. .unmap_sg = dma_4v_unmap_sg,
  417. .sync_single_for_cpu = dma_4v_sync_single_for_cpu,
  418. .sync_sg_for_cpu = dma_4v_sync_sg_for_cpu,
  419. };
  420. static void __init pci_sun4v_scan_bus(struct pci_pbm_info *pbm)
  421. {
  422. struct property *prop;
  423. struct device_node *dp;
  424. dp = pbm->prom_node;
  425. prop = of_find_property(dp, "66mhz-capable", NULL);
  426. pbm->is_66mhz_capable = (prop != NULL);
  427. pbm->pci_bus = pci_scan_one_pbm(pbm);
  428. /* XXX register error interrupt handlers XXX */
  429. }
  430. static unsigned long __init probe_existing_entries(struct pci_pbm_info *pbm,
  431. struct iommu *iommu)
  432. {
  433. struct iommu_arena *arena = &iommu->arena;
  434. unsigned long i, cnt = 0;
  435. u32 devhandle;
  436. devhandle = pbm->devhandle;
  437. for (i = 0; i < arena->limit; i++) {
  438. unsigned long ret, io_attrs, ra;
  439. ret = pci_sun4v_iommu_getmap(devhandle,
  440. HV_PCI_TSBID(0, i),
  441. &io_attrs, &ra);
  442. if (ret == HV_EOK) {
  443. if (page_in_phys_avail(ra)) {
  444. pci_sun4v_iommu_demap(devhandle,
  445. HV_PCI_TSBID(0, i), 1);
  446. } else {
  447. cnt++;
  448. __set_bit(i, arena->map);
  449. }
  450. }
  451. }
  452. return cnt;
  453. }
  454. static void __init pci_sun4v_iommu_init(struct pci_pbm_info *pbm)
  455. {
  456. struct iommu *iommu = pbm->iommu;
  457. struct property *prop;
  458. unsigned long num_tsb_entries, sz, tsbsize;
  459. u32 vdma[2], dma_mask, dma_offset;
  460. prop = of_find_property(pbm->prom_node, "virtual-dma", NULL);
  461. if (prop) {
  462. u32 *val = prop->value;
  463. vdma[0] = val[0];
  464. vdma[1] = val[1];
  465. } else {
  466. /* No property, use default values. */
  467. vdma[0] = 0x80000000;
  468. vdma[1] = 0x80000000;
  469. }
  470. if ((vdma[0] | vdma[1]) & ~IO_PAGE_MASK) {
  471. prom_printf("PCI-SUN4V: strange virtual-dma[%08x:%08x].\n",
  472. vdma[0], vdma[1]);
  473. prom_halt();
  474. };
  475. dma_mask = (roundup_pow_of_two(vdma[1]) - 1UL);
  476. num_tsb_entries = vdma[1] / IO_PAGE_SIZE;
  477. tsbsize = num_tsb_entries * sizeof(iopte_t);
  478. dma_offset = vdma[0];
  479. /* Setup initial software IOMMU state. */
  480. spin_lock_init(&iommu->lock);
  481. iommu->ctx_lowest_free = 1;
  482. iommu->page_table_map_base = dma_offset;
  483. iommu->dma_addr_mask = dma_mask;
  484. /* Allocate and initialize the free area map. */
  485. sz = (num_tsb_entries + 7) / 8;
  486. sz = (sz + 7UL) & ~7UL;
  487. iommu->arena.map = kzalloc(sz, GFP_KERNEL);
  488. if (!iommu->arena.map) {
  489. prom_printf("PCI_IOMMU: Error, kmalloc(arena.map) failed.\n");
  490. prom_halt();
  491. }
  492. iommu->arena.limit = num_tsb_entries;
  493. sz = probe_existing_entries(pbm, iommu);
  494. if (sz)
  495. printk("%s: Imported %lu TSB entries from OBP\n",
  496. pbm->name, sz);
  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 virt_irq = sun4v_build_irq(pbm->devhandle, devino);
  679. if (!virt_irq)
  680. return -ENOMEM;
  681. if (pci_sun4v_msiq_setstate(pbm->devhandle, msiqid, HV_MSIQSTATE_IDLE))
  682. return -EINVAL;
  683. if (pci_sun4v_msiq_setvalid(pbm->devhandle, msiqid, HV_MSIQ_VALID))
  684. return -EINVAL;
  685. return virt_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 void __init pci_sun4v_pbm_init(struct pci_controller_info *p,
  707. struct device_node *dp, u32 devhandle)
  708. {
  709. struct pci_pbm_info *pbm;
  710. if (devhandle & 0x40)
  711. pbm = &p->pbm_B;
  712. else
  713. pbm = &p->pbm_A;
  714. pbm->next = pci_pbm_root;
  715. pci_pbm_root = pbm;
  716. pbm->scan_bus = pci_sun4v_scan_bus;
  717. pbm->pci_ops = &sun4v_pci_ops;
  718. pbm->config_space_reg_bits = 12;
  719. pbm->index = pci_num_pbms++;
  720. pbm->parent = p;
  721. pbm->prom_node = dp;
  722. pbm->devhandle = devhandle;
  723. pbm->name = dp->full_name;
  724. printk("%s: SUN4V PCI Bus Module\n", pbm->name);
  725. pci_determine_mem_io_space(pbm);
  726. pci_get_pbm_props(pbm);
  727. pci_sun4v_iommu_init(pbm);
  728. pci_sun4v_msi_init(pbm);
  729. }
  730. void __init sun4v_pci_init(struct device_node *dp, char *model_name)
  731. {
  732. static int hvapi_negotiated = 0;
  733. struct pci_controller_info *p;
  734. struct pci_pbm_info *pbm;
  735. struct iommu *iommu;
  736. struct property *prop;
  737. struct linux_prom64_registers *regs;
  738. u32 devhandle;
  739. int i;
  740. if (!hvapi_negotiated++) {
  741. int err = sun4v_hvapi_register(HV_GRP_PCI,
  742. vpci_major,
  743. &vpci_minor);
  744. if (err) {
  745. prom_printf("SUN4V_PCI: Could not register hvapi, "
  746. "err=%d\n", err);
  747. prom_halt();
  748. }
  749. printk("SUN4V_PCI: Registered hvapi major[%lu] minor[%lu]\n",
  750. vpci_major, vpci_minor);
  751. dma_ops = &sun4v_dma_ops;
  752. }
  753. prop = of_find_property(dp, "reg", NULL);
  754. if (!prop) {
  755. prom_printf("SUN4V_PCI: Could not find config registers\n");
  756. prom_halt();
  757. }
  758. regs = prop->value;
  759. devhandle = (regs->phys_addr >> 32UL) & 0x0fffffff;
  760. for (pbm = pci_pbm_root; pbm; pbm = pbm->next) {
  761. if (pbm->devhandle == (devhandle ^ 0x40)) {
  762. pci_sun4v_pbm_init(pbm->parent, dp, devhandle);
  763. return;
  764. }
  765. }
  766. for_each_possible_cpu(i) {
  767. unsigned long page = get_zeroed_page(GFP_ATOMIC);
  768. if (!page)
  769. goto fatal_memory_error;
  770. per_cpu(iommu_batch, i).pglist = (u64 *) page;
  771. }
  772. p = kzalloc(sizeof(struct pci_controller_info), GFP_ATOMIC);
  773. if (!p)
  774. goto fatal_memory_error;
  775. iommu = kzalloc(sizeof(struct iommu), GFP_ATOMIC);
  776. if (!iommu)
  777. goto fatal_memory_error;
  778. p->pbm_A.iommu = iommu;
  779. iommu = kzalloc(sizeof(struct iommu), GFP_ATOMIC);
  780. if (!iommu)
  781. goto fatal_memory_error;
  782. p->pbm_B.iommu = iommu;
  783. pci_sun4v_pbm_init(p, dp, devhandle);
  784. return;
  785. fatal_memory_error:
  786. prom_printf("SUN4V_PCI: Fatal memory allocation error.\n");
  787. prom_halt();
  788. }