pci_sun4v.c 24 KB

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