pci_sun4v.c 24 KB

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