pci_sun4v.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  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 "kernel.h"
  25. #include "pci_sun4v.h"
  26. #define DRIVER_NAME "pci_sun4v"
  27. #define PFX DRIVER_NAME ": "
  28. static unsigned long vpci_major;
  29. static unsigned long vpci_minor;
  30. struct vpci_version {
  31. unsigned long major;
  32. unsigned long minor;
  33. };
  34. /* Ordered from largest major to lowest */
  35. static struct vpci_version vpci_versions[] = {
  36. { .major = 2, .minor = 0 },
  37. { .major = 1, .minor = 1 },
  38. };
  39. static unsigned long vatu_major = 1;
  40. static unsigned long vatu_minor = 1;
  41. #define PGLIST_NENTS (PAGE_SIZE / sizeof(u64))
  42. struct iommu_batch {
  43. struct device *dev; /* Device mapping is for. */
  44. unsigned long prot; /* IOMMU page protections */
  45. unsigned long entry; /* Index into IOTSB. */
  46. u64 *pglist; /* List of physical pages */
  47. unsigned long npages; /* Number of pages in list. */
  48. };
  49. static DEFINE_PER_CPU(struct iommu_batch, iommu_batch);
  50. static int iommu_batch_initialized;
  51. /* Interrupts must be disabled. */
  52. static inline void iommu_batch_start(struct device *dev, unsigned long prot, unsigned long entry)
  53. {
  54. struct iommu_batch *p = this_cpu_ptr(&iommu_batch);
  55. p->dev = dev;
  56. p->prot = prot;
  57. p->entry = entry;
  58. p->npages = 0;
  59. }
  60. /* Interrupts must be disabled. */
  61. static long iommu_batch_flush(struct iommu_batch *p, u64 mask)
  62. {
  63. struct pci_pbm_info *pbm = p->dev->archdata.host_controller;
  64. u64 *pglist = p->pglist;
  65. u64 index_count;
  66. unsigned long devhandle = pbm->devhandle;
  67. unsigned long prot = p->prot;
  68. unsigned long entry = p->entry;
  69. unsigned long npages = p->npages;
  70. unsigned long iotsb_num;
  71. unsigned long ret;
  72. long num;
  73. /* VPCI maj=1, min=[0,1] only supports read and write */
  74. if (vpci_major < 2)
  75. prot &= (HV_PCI_MAP_ATTR_READ | HV_PCI_MAP_ATTR_WRITE);
  76. while (npages != 0) {
  77. if (mask <= DMA_BIT_MASK(32)) {
  78. num = pci_sun4v_iommu_map(devhandle,
  79. HV_PCI_TSBID(0, entry),
  80. npages,
  81. prot,
  82. __pa(pglist));
  83. if (unlikely(num < 0)) {
  84. pr_err_ratelimited("%s: IOMMU map of [%08lx:%08llx:%lx:%lx:%lx] failed with status %ld\n",
  85. __func__,
  86. devhandle,
  87. HV_PCI_TSBID(0, entry),
  88. npages, prot, __pa(pglist),
  89. num);
  90. return -1;
  91. }
  92. } else {
  93. index_count = HV_PCI_IOTSB_INDEX_COUNT(npages, entry),
  94. iotsb_num = pbm->iommu->atu->iotsb->iotsb_num;
  95. ret = pci_sun4v_iotsb_map(devhandle,
  96. iotsb_num,
  97. index_count,
  98. prot,
  99. __pa(pglist),
  100. &num);
  101. if (unlikely(ret != HV_EOK)) {
  102. pr_err_ratelimited("%s: ATU map of [%08lx:%lx:%llx:%lx:%lx] failed with status %ld\n",
  103. __func__,
  104. devhandle, iotsb_num,
  105. index_count, prot,
  106. __pa(pglist), ret);
  107. return -1;
  108. }
  109. }
  110. entry += num;
  111. npages -= num;
  112. pglist += num;
  113. }
  114. p->entry = entry;
  115. p->npages = 0;
  116. return 0;
  117. }
  118. static inline void iommu_batch_new_entry(unsigned long entry, u64 mask)
  119. {
  120. struct iommu_batch *p = this_cpu_ptr(&iommu_batch);
  121. if (p->entry + p->npages == entry)
  122. return;
  123. if (p->entry != ~0UL)
  124. iommu_batch_flush(p, mask);
  125. p->entry = entry;
  126. }
  127. /* Interrupts must be disabled. */
  128. static inline long iommu_batch_add(u64 phys_page, u64 mask)
  129. {
  130. struct iommu_batch *p = this_cpu_ptr(&iommu_batch);
  131. BUG_ON(p->npages >= PGLIST_NENTS);
  132. p->pglist[p->npages++] = phys_page;
  133. if (p->npages == PGLIST_NENTS)
  134. return iommu_batch_flush(p, mask);
  135. return 0;
  136. }
  137. /* Interrupts must be disabled. */
  138. static inline long iommu_batch_end(u64 mask)
  139. {
  140. struct iommu_batch *p = this_cpu_ptr(&iommu_batch);
  141. BUG_ON(p->npages >= PGLIST_NENTS);
  142. return iommu_batch_flush(p, mask);
  143. }
  144. static void *dma_4v_alloc_coherent(struct device *dev, size_t size,
  145. dma_addr_t *dma_addrp, gfp_t gfp,
  146. unsigned long attrs)
  147. {
  148. u64 mask;
  149. unsigned long flags, order, first_page, npages, n;
  150. unsigned long prot = 0;
  151. struct iommu *iommu;
  152. struct atu *atu;
  153. struct iommu_map_table *tbl;
  154. struct page *page;
  155. void *ret;
  156. long entry;
  157. int nid;
  158. size = IO_PAGE_ALIGN(size);
  159. order = get_order(size);
  160. if (unlikely(order >= MAX_ORDER))
  161. return NULL;
  162. npages = size >> IO_PAGE_SHIFT;
  163. if (attrs & DMA_ATTR_WEAK_ORDERING)
  164. prot = HV_PCI_MAP_ATTR_RELAXED_ORDER;
  165. nid = dev->archdata.numa_node;
  166. page = alloc_pages_node(nid, gfp, order);
  167. if (unlikely(!page))
  168. return NULL;
  169. first_page = (unsigned long) page_address(page);
  170. memset((char *)first_page, 0, PAGE_SIZE << order);
  171. iommu = dev->archdata.iommu;
  172. atu = iommu->atu;
  173. mask = dev->coherent_dma_mask;
  174. if (mask <= DMA_BIT_MASK(32))
  175. tbl = &iommu->tbl;
  176. else
  177. tbl = &atu->tbl;
  178. entry = iommu_tbl_range_alloc(dev, tbl, npages, NULL,
  179. (unsigned long)(-1), 0);
  180. if (unlikely(entry == IOMMU_ERROR_CODE))
  181. goto range_alloc_fail;
  182. *dma_addrp = (tbl->table_map_base + (entry << IO_PAGE_SHIFT));
  183. ret = (void *) first_page;
  184. first_page = __pa(first_page);
  185. local_irq_save(flags);
  186. iommu_batch_start(dev,
  187. (HV_PCI_MAP_ATTR_READ | prot |
  188. HV_PCI_MAP_ATTR_WRITE),
  189. entry);
  190. for (n = 0; n < npages; n++) {
  191. long err = iommu_batch_add(first_page + (n * PAGE_SIZE), mask);
  192. if (unlikely(err < 0L))
  193. goto iommu_map_fail;
  194. }
  195. if (unlikely(iommu_batch_end(mask) < 0L))
  196. goto iommu_map_fail;
  197. local_irq_restore(flags);
  198. return ret;
  199. iommu_map_fail:
  200. local_irq_restore(flags);
  201. iommu_tbl_range_free(tbl, *dma_addrp, npages, IOMMU_ERROR_CODE);
  202. range_alloc_fail:
  203. free_pages(first_page, order);
  204. return NULL;
  205. }
  206. unsigned long dma_4v_iotsb_bind(unsigned long devhandle,
  207. unsigned long iotsb_num,
  208. struct pci_bus *bus_dev)
  209. {
  210. struct pci_dev *pdev;
  211. unsigned long err;
  212. unsigned int bus;
  213. unsigned int device;
  214. unsigned int fun;
  215. list_for_each_entry(pdev, &bus_dev->devices, bus_list) {
  216. if (pdev->subordinate) {
  217. /* No need to bind pci bridge */
  218. dma_4v_iotsb_bind(devhandle, iotsb_num,
  219. pdev->subordinate);
  220. } else {
  221. bus = bus_dev->number;
  222. device = PCI_SLOT(pdev->devfn);
  223. fun = PCI_FUNC(pdev->devfn);
  224. err = pci_sun4v_iotsb_bind(devhandle, iotsb_num,
  225. HV_PCI_DEVICE_BUILD(bus,
  226. device,
  227. fun));
  228. /* If bind fails for one device it is going to fail
  229. * for rest of the devices because we are sharing
  230. * IOTSB. So in case of failure simply return with
  231. * error.
  232. */
  233. if (err)
  234. return err;
  235. }
  236. }
  237. return 0;
  238. }
  239. static void dma_4v_iommu_demap(struct device *dev, unsigned long devhandle,
  240. dma_addr_t dvma, unsigned long iotsb_num,
  241. unsigned long entry, unsigned long npages)
  242. {
  243. unsigned long num, flags;
  244. unsigned long ret;
  245. local_irq_save(flags);
  246. do {
  247. if (dvma <= DMA_BIT_MASK(32)) {
  248. num = pci_sun4v_iommu_demap(devhandle,
  249. HV_PCI_TSBID(0, entry),
  250. npages);
  251. } else {
  252. ret = pci_sun4v_iotsb_demap(devhandle, iotsb_num,
  253. entry, npages, &num);
  254. if (unlikely(ret != HV_EOK)) {
  255. pr_err_ratelimited("pci_iotsb_demap() failed with error: %ld\n",
  256. ret);
  257. }
  258. }
  259. entry += num;
  260. npages -= num;
  261. } while (npages != 0);
  262. local_irq_restore(flags);
  263. }
  264. static void dma_4v_free_coherent(struct device *dev, size_t size, void *cpu,
  265. dma_addr_t dvma, unsigned long attrs)
  266. {
  267. struct pci_pbm_info *pbm;
  268. struct iommu *iommu;
  269. struct atu *atu;
  270. struct iommu_map_table *tbl;
  271. unsigned long order, npages, entry;
  272. unsigned long iotsb_num;
  273. u32 devhandle;
  274. npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
  275. iommu = dev->archdata.iommu;
  276. pbm = dev->archdata.host_controller;
  277. atu = iommu->atu;
  278. devhandle = pbm->devhandle;
  279. if (dvma <= DMA_BIT_MASK(32)) {
  280. tbl = &iommu->tbl;
  281. iotsb_num = 0; /* we don't care for legacy iommu */
  282. } else {
  283. tbl = &atu->tbl;
  284. iotsb_num = atu->iotsb->iotsb_num;
  285. }
  286. entry = ((dvma - tbl->table_map_base) >> IO_PAGE_SHIFT);
  287. dma_4v_iommu_demap(dev, devhandle, dvma, iotsb_num, entry, npages);
  288. iommu_tbl_range_free(tbl, dvma, npages, IOMMU_ERROR_CODE);
  289. order = get_order(size);
  290. if (order < 10)
  291. free_pages((unsigned long)cpu, order);
  292. }
  293. static dma_addr_t dma_4v_map_page(struct device *dev, struct page *page,
  294. unsigned long offset, size_t sz,
  295. enum dma_data_direction direction,
  296. unsigned long attrs)
  297. {
  298. struct iommu *iommu;
  299. struct atu *atu;
  300. struct iommu_map_table *tbl;
  301. u64 mask;
  302. unsigned long flags, npages, oaddr;
  303. unsigned long i, base_paddr;
  304. unsigned long prot;
  305. dma_addr_t bus_addr, ret;
  306. long entry;
  307. iommu = dev->archdata.iommu;
  308. atu = iommu->atu;
  309. if (unlikely(direction == DMA_NONE))
  310. goto bad;
  311. oaddr = (unsigned long)(page_address(page) + offset);
  312. npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
  313. npages >>= IO_PAGE_SHIFT;
  314. mask = *dev->dma_mask;
  315. if (mask <= DMA_BIT_MASK(32))
  316. tbl = &iommu->tbl;
  317. else
  318. tbl = &atu->tbl;
  319. entry = iommu_tbl_range_alloc(dev, tbl, npages, NULL,
  320. (unsigned long)(-1), 0);
  321. if (unlikely(entry == IOMMU_ERROR_CODE))
  322. goto bad;
  323. bus_addr = (tbl->table_map_base + (entry << IO_PAGE_SHIFT));
  324. ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
  325. base_paddr = __pa(oaddr & IO_PAGE_MASK);
  326. prot = HV_PCI_MAP_ATTR_READ;
  327. if (direction != DMA_TO_DEVICE)
  328. prot |= HV_PCI_MAP_ATTR_WRITE;
  329. if (attrs & DMA_ATTR_WEAK_ORDERING)
  330. prot |= HV_PCI_MAP_ATTR_RELAXED_ORDER;
  331. local_irq_save(flags);
  332. iommu_batch_start(dev, prot, entry);
  333. for (i = 0; i < npages; i++, base_paddr += IO_PAGE_SIZE) {
  334. long err = iommu_batch_add(base_paddr, mask);
  335. if (unlikely(err < 0L))
  336. goto iommu_map_fail;
  337. }
  338. if (unlikely(iommu_batch_end(mask) < 0L))
  339. goto iommu_map_fail;
  340. local_irq_restore(flags);
  341. return ret;
  342. bad:
  343. if (printk_ratelimit())
  344. WARN_ON(1);
  345. return SPARC_MAPPING_ERROR;
  346. iommu_map_fail:
  347. local_irq_restore(flags);
  348. iommu_tbl_range_free(tbl, bus_addr, npages, IOMMU_ERROR_CODE);
  349. return SPARC_MAPPING_ERROR;
  350. }
  351. static void dma_4v_unmap_page(struct device *dev, dma_addr_t bus_addr,
  352. size_t sz, enum dma_data_direction direction,
  353. unsigned long attrs)
  354. {
  355. struct pci_pbm_info *pbm;
  356. struct iommu *iommu;
  357. struct atu *atu;
  358. struct iommu_map_table *tbl;
  359. unsigned long npages;
  360. unsigned long iotsb_num;
  361. long entry;
  362. u32 devhandle;
  363. if (unlikely(direction == DMA_NONE)) {
  364. if (printk_ratelimit())
  365. WARN_ON(1);
  366. return;
  367. }
  368. iommu = dev->archdata.iommu;
  369. pbm = dev->archdata.host_controller;
  370. atu = iommu->atu;
  371. devhandle = pbm->devhandle;
  372. npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
  373. npages >>= IO_PAGE_SHIFT;
  374. bus_addr &= IO_PAGE_MASK;
  375. if (bus_addr <= DMA_BIT_MASK(32)) {
  376. iotsb_num = 0; /* we don't care for legacy iommu */
  377. tbl = &iommu->tbl;
  378. } else {
  379. iotsb_num = atu->iotsb->iotsb_num;
  380. tbl = &atu->tbl;
  381. }
  382. entry = (bus_addr - tbl->table_map_base) >> IO_PAGE_SHIFT;
  383. dma_4v_iommu_demap(dev, devhandle, bus_addr, iotsb_num, entry, npages);
  384. iommu_tbl_range_free(tbl, bus_addr, npages, IOMMU_ERROR_CODE);
  385. }
  386. static int dma_4v_map_sg(struct device *dev, struct scatterlist *sglist,
  387. int nelems, enum dma_data_direction direction,
  388. unsigned long attrs)
  389. {
  390. struct scatterlist *s, *outs, *segstart;
  391. unsigned long flags, handle, prot;
  392. dma_addr_t dma_next = 0, dma_addr;
  393. unsigned int max_seg_size;
  394. unsigned long seg_boundary_size;
  395. int outcount, incount, i;
  396. struct iommu *iommu;
  397. struct atu *atu;
  398. struct iommu_map_table *tbl;
  399. u64 mask;
  400. unsigned long base_shift;
  401. long err;
  402. BUG_ON(direction == DMA_NONE);
  403. iommu = dev->archdata.iommu;
  404. if (nelems == 0 || !iommu)
  405. return 0;
  406. atu = iommu->atu;
  407. prot = HV_PCI_MAP_ATTR_READ;
  408. if (direction != DMA_TO_DEVICE)
  409. prot |= HV_PCI_MAP_ATTR_WRITE;
  410. if (attrs & DMA_ATTR_WEAK_ORDERING)
  411. prot |= HV_PCI_MAP_ATTR_RELAXED_ORDER;
  412. outs = s = segstart = &sglist[0];
  413. outcount = 1;
  414. incount = nelems;
  415. handle = 0;
  416. /* Init first segment length for backout at failure */
  417. outs->dma_length = 0;
  418. local_irq_save(flags);
  419. iommu_batch_start(dev, prot, ~0UL);
  420. max_seg_size = dma_get_max_seg_size(dev);
  421. seg_boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
  422. IO_PAGE_SIZE) >> IO_PAGE_SHIFT;
  423. mask = *dev->dma_mask;
  424. if (mask <= DMA_BIT_MASK(32))
  425. tbl = &iommu->tbl;
  426. else
  427. tbl = &atu->tbl;
  428. base_shift = tbl->table_map_base >> IO_PAGE_SHIFT;
  429. for_each_sg(sglist, s, nelems, i) {
  430. unsigned long paddr, npages, entry, out_entry = 0, slen;
  431. slen = s->length;
  432. /* Sanity check */
  433. if (slen == 0) {
  434. dma_next = 0;
  435. continue;
  436. }
  437. /* Allocate iommu entries for that segment */
  438. paddr = (unsigned long) SG_ENT_PHYS_ADDRESS(s);
  439. npages = iommu_num_pages(paddr, slen, IO_PAGE_SIZE);
  440. entry = iommu_tbl_range_alloc(dev, tbl, npages,
  441. &handle, (unsigned long)(-1), 0);
  442. /* Handle failure */
  443. if (unlikely(entry == IOMMU_ERROR_CODE)) {
  444. pr_err_ratelimited("iommu_alloc failed, iommu %p paddr %lx npages %lx\n",
  445. tbl, paddr, npages);
  446. goto iommu_map_failed;
  447. }
  448. iommu_batch_new_entry(entry, mask);
  449. /* Convert entry to a dma_addr_t */
  450. dma_addr = tbl->table_map_base + (entry << IO_PAGE_SHIFT);
  451. dma_addr |= (s->offset & ~IO_PAGE_MASK);
  452. /* Insert into HW table */
  453. paddr &= IO_PAGE_MASK;
  454. while (npages--) {
  455. err = iommu_batch_add(paddr, mask);
  456. if (unlikely(err < 0L))
  457. goto iommu_map_failed;
  458. paddr += IO_PAGE_SIZE;
  459. }
  460. /* If we are in an open segment, try merging */
  461. if (segstart != s) {
  462. /* We cannot merge if:
  463. * - allocated dma_addr isn't contiguous to previous allocation
  464. */
  465. if ((dma_addr != dma_next) ||
  466. (outs->dma_length + s->length > max_seg_size) ||
  467. (is_span_boundary(out_entry, base_shift,
  468. seg_boundary_size, outs, s))) {
  469. /* Can't merge: create a new segment */
  470. segstart = s;
  471. outcount++;
  472. outs = sg_next(outs);
  473. } else {
  474. outs->dma_length += s->length;
  475. }
  476. }
  477. if (segstart == s) {
  478. /* This is a new segment, fill entries */
  479. outs->dma_address = dma_addr;
  480. outs->dma_length = slen;
  481. out_entry = entry;
  482. }
  483. /* Calculate next page pointer for contiguous check */
  484. dma_next = dma_addr + slen;
  485. }
  486. err = iommu_batch_end(mask);
  487. if (unlikely(err < 0L))
  488. goto iommu_map_failed;
  489. local_irq_restore(flags);
  490. if (outcount < incount) {
  491. outs = sg_next(outs);
  492. outs->dma_address = SPARC_MAPPING_ERROR;
  493. outs->dma_length = 0;
  494. }
  495. return outcount;
  496. iommu_map_failed:
  497. for_each_sg(sglist, s, nelems, i) {
  498. if (s->dma_length != 0) {
  499. unsigned long vaddr, npages;
  500. vaddr = s->dma_address & IO_PAGE_MASK;
  501. npages = iommu_num_pages(s->dma_address, s->dma_length,
  502. IO_PAGE_SIZE);
  503. iommu_tbl_range_free(tbl, vaddr, npages,
  504. IOMMU_ERROR_CODE);
  505. /* XXX demap? XXX */
  506. s->dma_address = SPARC_MAPPING_ERROR;
  507. s->dma_length = 0;
  508. }
  509. if (s == outs)
  510. break;
  511. }
  512. local_irq_restore(flags);
  513. return 0;
  514. }
  515. static void dma_4v_unmap_sg(struct device *dev, struct scatterlist *sglist,
  516. int nelems, enum dma_data_direction direction,
  517. unsigned long attrs)
  518. {
  519. struct pci_pbm_info *pbm;
  520. struct scatterlist *sg;
  521. struct iommu *iommu;
  522. struct atu *atu;
  523. unsigned long flags, entry;
  524. unsigned long iotsb_num;
  525. u32 devhandle;
  526. BUG_ON(direction == DMA_NONE);
  527. iommu = dev->archdata.iommu;
  528. pbm = dev->archdata.host_controller;
  529. atu = iommu->atu;
  530. devhandle = pbm->devhandle;
  531. local_irq_save(flags);
  532. sg = sglist;
  533. while (nelems--) {
  534. dma_addr_t dma_handle = sg->dma_address;
  535. unsigned int len = sg->dma_length;
  536. unsigned long npages;
  537. struct iommu_map_table *tbl;
  538. unsigned long shift = IO_PAGE_SHIFT;
  539. if (!len)
  540. break;
  541. npages = iommu_num_pages(dma_handle, len, IO_PAGE_SIZE);
  542. if (dma_handle <= DMA_BIT_MASK(32)) {
  543. iotsb_num = 0; /* we don't care for legacy iommu */
  544. tbl = &iommu->tbl;
  545. } else {
  546. iotsb_num = atu->iotsb->iotsb_num;
  547. tbl = &atu->tbl;
  548. }
  549. entry = ((dma_handle - tbl->table_map_base) >> shift);
  550. dma_4v_iommu_demap(dev, devhandle, dma_handle, iotsb_num,
  551. entry, npages);
  552. iommu_tbl_range_free(tbl, dma_handle, npages,
  553. IOMMU_ERROR_CODE);
  554. sg = sg_next(sg);
  555. }
  556. local_irq_restore(flags);
  557. }
  558. static int dma_4v_supported(struct device *dev, u64 device_mask)
  559. {
  560. struct iommu *iommu = dev->archdata.iommu;
  561. u64 dma_addr_mask = iommu->dma_addr_mask;
  562. if (device_mask > DMA_BIT_MASK(32)) {
  563. if (iommu->atu)
  564. dma_addr_mask = iommu->atu->dma_addr_mask;
  565. else
  566. return 0;
  567. }
  568. if ((device_mask & dma_addr_mask) == dma_addr_mask)
  569. return 1;
  570. return pci64_dma_supported(to_pci_dev(dev), device_mask);
  571. }
  572. static int dma_4v_mapping_error(struct device *dev, dma_addr_t dma_addr)
  573. {
  574. return dma_addr == SPARC_MAPPING_ERROR;
  575. }
  576. static const struct dma_map_ops sun4v_dma_ops = {
  577. .alloc = dma_4v_alloc_coherent,
  578. .free = dma_4v_free_coherent,
  579. .map_page = dma_4v_map_page,
  580. .unmap_page = dma_4v_unmap_page,
  581. .map_sg = dma_4v_map_sg,
  582. .unmap_sg = dma_4v_unmap_sg,
  583. .dma_supported = dma_4v_supported,
  584. .mapping_error = dma_4v_mapping_error,
  585. };
  586. static void pci_sun4v_scan_bus(struct pci_pbm_info *pbm, struct device *parent)
  587. {
  588. struct property *prop;
  589. struct device_node *dp;
  590. dp = pbm->op->dev.of_node;
  591. prop = of_find_property(dp, "66mhz-capable", NULL);
  592. pbm->is_66mhz_capable = (prop != NULL);
  593. pbm->pci_bus = pci_scan_one_pbm(pbm, parent);
  594. /* XXX register error interrupt handlers XXX */
  595. }
  596. static unsigned long probe_existing_entries(struct pci_pbm_info *pbm,
  597. struct iommu_map_table *iommu)
  598. {
  599. struct iommu_pool *pool;
  600. unsigned long i, pool_nr, cnt = 0;
  601. u32 devhandle;
  602. devhandle = pbm->devhandle;
  603. for (pool_nr = 0; pool_nr < iommu->nr_pools; pool_nr++) {
  604. pool = &(iommu->pools[pool_nr]);
  605. for (i = pool->start; i <= pool->end; i++) {
  606. unsigned long ret, io_attrs, ra;
  607. ret = pci_sun4v_iommu_getmap(devhandle,
  608. HV_PCI_TSBID(0, i),
  609. &io_attrs, &ra);
  610. if (ret == HV_EOK) {
  611. if (page_in_phys_avail(ra)) {
  612. pci_sun4v_iommu_demap(devhandle,
  613. HV_PCI_TSBID(0,
  614. i), 1);
  615. } else {
  616. cnt++;
  617. __set_bit(i, iommu->map);
  618. }
  619. }
  620. }
  621. }
  622. return cnt;
  623. }
  624. static int pci_sun4v_atu_alloc_iotsb(struct pci_pbm_info *pbm)
  625. {
  626. struct atu *atu = pbm->iommu->atu;
  627. struct atu_iotsb *iotsb;
  628. void *table;
  629. u64 table_size;
  630. u64 iotsb_num;
  631. unsigned long order;
  632. unsigned long err;
  633. iotsb = kzalloc(sizeof(*iotsb), GFP_KERNEL);
  634. if (!iotsb) {
  635. err = -ENOMEM;
  636. goto out_err;
  637. }
  638. atu->iotsb = iotsb;
  639. /* calculate size of IOTSB */
  640. table_size = (atu->size / IO_PAGE_SIZE) * 8;
  641. order = get_order(table_size);
  642. table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
  643. if (!table) {
  644. err = -ENOMEM;
  645. goto table_failed;
  646. }
  647. iotsb->table = table;
  648. iotsb->ra = __pa(table);
  649. iotsb->dvma_size = atu->size;
  650. iotsb->dvma_base = atu->base;
  651. iotsb->table_size = table_size;
  652. iotsb->page_size = IO_PAGE_SIZE;
  653. /* configure and register IOTSB with HV */
  654. err = pci_sun4v_iotsb_conf(pbm->devhandle,
  655. iotsb->ra,
  656. iotsb->table_size,
  657. iotsb->page_size,
  658. iotsb->dvma_base,
  659. &iotsb_num);
  660. if (err) {
  661. pr_err(PFX "pci_iotsb_conf failed error: %ld\n", err);
  662. goto iotsb_conf_failed;
  663. }
  664. iotsb->iotsb_num = iotsb_num;
  665. err = dma_4v_iotsb_bind(pbm->devhandle, iotsb_num, pbm->pci_bus);
  666. if (err) {
  667. pr_err(PFX "pci_iotsb_bind failed error: %ld\n", err);
  668. goto iotsb_conf_failed;
  669. }
  670. return 0;
  671. iotsb_conf_failed:
  672. free_pages((unsigned long)table, order);
  673. table_failed:
  674. kfree(iotsb);
  675. out_err:
  676. return err;
  677. }
  678. static int pci_sun4v_atu_init(struct pci_pbm_info *pbm)
  679. {
  680. struct atu *atu = pbm->iommu->atu;
  681. unsigned long err;
  682. const u64 *ranges;
  683. u64 map_size, num_iotte;
  684. u64 dma_mask;
  685. const u32 *page_size;
  686. int len;
  687. ranges = of_get_property(pbm->op->dev.of_node, "iommu-address-ranges",
  688. &len);
  689. if (!ranges) {
  690. pr_err(PFX "No iommu-address-ranges\n");
  691. return -EINVAL;
  692. }
  693. page_size = of_get_property(pbm->op->dev.of_node, "iommu-pagesizes",
  694. NULL);
  695. if (!page_size) {
  696. pr_err(PFX "No iommu-pagesizes\n");
  697. return -EINVAL;
  698. }
  699. /* There are 4 iommu-address-ranges supported. Each range is pair of
  700. * {base, size}. The ranges[0] and ranges[1] are 32bit address space
  701. * while ranges[2] and ranges[3] are 64bit space. We want to use 64bit
  702. * address ranges to support 64bit addressing. Because 'size' for
  703. * address ranges[2] and ranges[3] are same we can select either of
  704. * ranges[2] or ranges[3] for mapping. However due to 'size' is too
  705. * large for OS to allocate IOTSB we are using fix size 32G
  706. * (ATU_64_SPACE_SIZE) which is more than enough for all PCIe devices
  707. * to share.
  708. */
  709. atu->ranges = (struct atu_ranges *)ranges;
  710. atu->base = atu->ranges[3].base;
  711. atu->size = ATU_64_SPACE_SIZE;
  712. /* Create IOTSB */
  713. err = pci_sun4v_atu_alloc_iotsb(pbm);
  714. if (err) {
  715. pr_err(PFX "Error creating ATU IOTSB\n");
  716. return err;
  717. }
  718. /* Create ATU iommu map.
  719. * One bit represents one iotte in IOTSB table.
  720. */
  721. dma_mask = (roundup_pow_of_two(atu->size) - 1UL);
  722. num_iotte = atu->size / IO_PAGE_SIZE;
  723. map_size = num_iotte / 8;
  724. atu->tbl.table_map_base = atu->base;
  725. atu->dma_addr_mask = dma_mask;
  726. atu->tbl.map = kzalloc(map_size, GFP_KERNEL);
  727. if (!atu->tbl.map)
  728. return -ENOMEM;
  729. iommu_tbl_pool_init(&atu->tbl, num_iotte, IO_PAGE_SHIFT,
  730. NULL, false /* no large_pool */,
  731. 0 /* default npools */,
  732. false /* want span boundary checking */);
  733. return 0;
  734. }
  735. static int pci_sun4v_iommu_init(struct pci_pbm_info *pbm)
  736. {
  737. static const u32 vdma_default[] = { 0x80000000, 0x80000000 };
  738. struct iommu *iommu = pbm->iommu;
  739. unsigned long num_tsb_entries, sz;
  740. u32 dma_mask, dma_offset;
  741. const u32 *vdma;
  742. vdma = of_get_property(pbm->op->dev.of_node, "virtual-dma", NULL);
  743. if (!vdma)
  744. vdma = vdma_default;
  745. if ((vdma[0] | vdma[1]) & ~IO_PAGE_MASK) {
  746. printk(KERN_ERR PFX "Strange virtual-dma[%08x:%08x].\n",
  747. vdma[0], vdma[1]);
  748. return -EINVAL;
  749. }
  750. dma_mask = (roundup_pow_of_two(vdma[1]) - 1UL);
  751. num_tsb_entries = vdma[1] / IO_PAGE_SIZE;
  752. dma_offset = vdma[0];
  753. /* Setup initial software IOMMU state. */
  754. spin_lock_init(&iommu->lock);
  755. iommu->ctx_lowest_free = 1;
  756. iommu->tbl.table_map_base = dma_offset;
  757. iommu->dma_addr_mask = dma_mask;
  758. /* Allocate and initialize the free area map. */
  759. sz = (num_tsb_entries + 7) / 8;
  760. sz = (sz + 7UL) & ~7UL;
  761. iommu->tbl.map = kzalloc(sz, GFP_KERNEL);
  762. if (!iommu->tbl.map) {
  763. printk(KERN_ERR PFX "Error, kmalloc(arena.map) failed.\n");
  764. return -ENOMEM;
  765. }
  766. iommu_tbl_pool_init(&iommu->tbl, num_tsb_entries, IO_PAGE_SHIFT,
  767. NULL, false /* no large_pool */,
  768. 0 /* default npools */,
  769. false /* want span boundary checking */);
  770. sz = probe_existing_entries(pbm, &iommu->tbl);
  771. if (sz)
  772. printk("%s: Imported %lu TSB entries from OBP\n",
  773. pbm->name, sz);
  774. return 0;
  775. }
  776. #ifdef CONFIG_PCI_MSI
  777. struct pci_sun4v_msiq_entry {
  778. u64 version_type;
  779. #define MSIQ_VERSION_MASK 0xffffffff00000000UL
  780. #define MSIQ_VERSION_SHIFT 32
  781. #define MSIQ_TYPE_MASK 0x00000000000000ffUL
  782. #define MSIQ_TYPE_SHIFT 0
  783. #define MSIQ_TYPE_NONE 0x00
  784. #define MSIQ_TYPE_MSG 0x01
  785. #define MSIQ_TYPE_MSI32 0x02
  786. #define MSIQ_TYPE_MSI64 0x03
  787. #define MSIQ_TYPE_INTX 0x08
  788. #define MSIQ_TYPE_NONE2 0xff
  789. u64 intx_sysino;
  790. u64 reserved1;
  791. u64 stick;
  792. u64 req_id; /* bus/device/func */
  793. #define MSIQ_REQID_BUS_MASK 0xff00UL
  794. #define MSIQ_REQID_BUS_SHIFT 8
  795. #define MSIQ_REQID_DEVICE_MASK 0x00f8UL
  796. #define MSIQ_REQID_DEVICE_SHIFT 3
  797. #define MSIQ_REQID_FUNC_MASK 0x0007UL
  798. #define MSIQ_REQID_FUNC_SHIFT 0
  799. u64 msi_address;
  800. /* The format of this value is message type dependent.
  801. * For MSI bits 15:0 are the data from the MSI packet.
  802. * For MSI-X bits 31:0 are the data from the MSI packet.
  803. * For MSG, the message code and message routing code where:
  804. * bits 39:32 is the bus/device/fn of the msg target-id
  805. * bits 18:16 is the message routing code
  806. * bits 7:0 is the message code
  807. * For INTx the low order 2-bits are:
  808. * 00 - INTA
  809. * 01 - INTB
  810. * 10 - INTC
  811. * 11 - INTD
  812. */
  813. u64 msi_data;
  814. u64 reserved2;
  815. };
  816. static int pci_sun4v_get_head(struct pci_pbm_info *pbm, unsigned long msiqid,
  817. unsigned long *head)
  818. {
  819. unsigned long err, limit;
  820. err = pci_sun4v_msiq_gethead(pbm->devhandle, msiqid, head);
  821. if (unlikely(err))
  822. return -ENXIO;
  823. limit = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
  824. if (unlikely(*head >= limit))
  825. return -EFBIG;
  826. return 0;
  827. }
  828. static int pci_sun4v_dequeue_msi(struct pci_pbm_info *pbm,
  829. unsigned long msiqid, unsigned long *head,
  830. unsigned long *msi)
  831. {
  832. struct pci_sun4v_msiq_entry *ep;
  833. unsigned long err, type;
  834. /* Note: void pointer arithmetic, 'head' is a byte offset */
  835. ep = (pbm->msi_queues + ((msiqid - pbm->msiq_first) *
  836. (pbm->msiq_ent_count *
  837. sizeof(struct pci_sun4v_msiq_entry))) +
  838. *head);
  839. if ((ep->version_type & MSIQ_TYPE_MASK) == 0)
  840. return 0;
  841. type = (ep->version_type & MSIQ_TYPE_MASK) >> MSIQ_TYPE_SHIFT;
  842. if (unlikely(type != MSIQ_TYPE_MSI32 &&
  843. type != MSIQ_TYPE_MSI64))
  844. return -EINVAL;
  845. *msi = ep->msi_data;
  846. err = pci_sun4v_msi_setstate(pbm->devhandle,
  847. ep->msi_data /* msi_num */,
  848. HV_MSISTATE_IDLE);
  849. if (unlikely(err))
  850. return -ENXIO;
  851. /* Clear the entry. */
  852. ep->version_type &= ~MSIQ_TYPE_MASK;
  853. (*head) += sizeof(struct pci_sun4v_msiq_entry);
  854. if (*head >=
  855. (pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry)))
  856. *head = 0;
  857. return 1;
  858. }
  859. static int pci_sun4v_set_head(struct pci_pbm_info *pbm, unsigned long msiqid,
  860. unsigned long head)
  861. {
  862. unsigned long err;
  863. err = pci_sun4v_msiq_sethead(pbm->devhandle, msiqid, head);
  864. if (unlikely(err))
  865. return -EINVAL;
  866. return 0;
  867. }
  868. static int pci_sun4v_msi_setup(struct pci_pbm_info *pbm, unsigned long msiqid,
  869. unsigned long msi, int is_msi64)
  870. {
  871. if (pci_sun4v_msi_setmsiq(pbm->devhandle, msi, msiqid,
  872. (is_msi64 ?
  873. HV_MSITYPE_MSI64 : HV_MSITYPE_MSI32)))
  874. return -ENXIO;
  875. if (pci_sun4v_msi_setstate(pbm->devhandle, msi, HV_MSISTATE_IDLE))
  876. return -ENXIO;
  877. if (pci_sun4v_msi_setvalid(pbm->devhandle, msi, HV_MSIVALID_VALID))
  878. return -ENXIO;
  879. return 0;
  880. }
  881. static int pci_sun4v_msi_teardown(struct pci_pbm_info *pbm, unsigned long msi)
  882. {
  883. unsigned long err, msiqid;
  884. err = pci_sun4v_msi_getmsiq(pbm->devhandle, msi, &msiqid);
  885. if (err)
  886. return -ENXIO;
  887. pci_sun4v_msi_setvalid(pbm->devhandle, msi, HV_MSIVALID_INVALID);
  888. return 0;
  889. }
  890. static int pci_sun4v_msiq_alloc(struct pci_pbm_info *pbm)
  891. {
  892. unsigned long q_size, alloc_size, pages, order;
  893. int i;
  894. q_size = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
  895. alloc_size = (pbm->msiq_num * q_size);
  896. order = get_order(alloc_size);
  897. pages = __get_free_pages(GFP_KERNEL | __GFP_COMP, order);
  898. if (pages == 0UL) {
  899. printk(KERN_ERR "MSI: Cannot allocate MSI queues (o=%lu).\n",
  900. order);
  901. return -ENOMEM;
  902. }
  903. memset((char *)pages, 0, PAGE_SIZE << order);
  904. pbm->msi_queues = (void *) pages;
  905. for (i = 0; i < pbm->msiq_num; i++) {
  906. unsigned long err, base = __pa(pages + (i * q_size));
  907. unsigned long ret1, ret2;
  908. err = pci_sun4v_msiq_conf(pbm->devhandle,
  909. pbm->msiq_first + i,
  910. base, pbm->msiq_ent_count);
  911. if (err) {
  912. printk(KERN_ERR "MSI: msiq register fails (err=%lu)\n",
  913. err);
  914. goto h_error;
  915. }
  916. err = pci_sun4v_msiq_info(pbm->devhandle,
  917. pbm->msiq_first + i,
  918. &ret1, &ret2);
  919. if (err) {
  920. printk(KERN_ERR "MSI: Cannot read msiq (err=%lu)\n",
  921. err);
  922. goto h_error;
  923. }
  924. if (ret1 != base || ret2 != pbm->msiq_ent_count) {
  925. printk(KERN_ERR "MSI: Bogus qconf "
  926. "expected[%lx:%x] got[%lx:%lx]\n",
  927. base, pbm->msiq_ent_count,
  928. ret1, ret2);
  929. goto h_error;
  930. }
  931. }
  932. return 0;
  933. h_error:
  934. free_pages(pages, order);
  935. return -EINVAL;
  936. }
  937. static void pci_sun4v_msiq_free(struct pci_pbm_info *pbm)
  938. {
  939. unsigned long q_size, alloc_size, pages, order;
  940. int i;
  941. for (i = 0; i < pbm->msiq_num; i++) {
  942. unsigned long msiqid = pbm->msiq_first + i;
  943. (void) pci_sun4v_msiq_conf(pbm->devhandle, msiqid, 0UL, 0);
  944. }
  945. q_size = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
  946. alloc_size = (pbm->msiq_num * q_size);
  947. order = get_order(alloc_size);
  948. pages = (unsigned long) pbm->msi_queues;
  949. free_pages(pages, order);
  950. pbm->msi_queues = NULL;
  951. }
  952. static int pci_sun4v_msiq_build_irq(struct pci_pbm_info *pbm,
  953. unsigned long msiqid,
  954. unsigned long devino)
  955. {
  956. unsigned int irq = sun4v_build_irq(pbm->devhandle, devino);
  957. if (!irq)
  958. return -ENOMEM;
  959. if (pci_sun4v_msiq_setvalid(pbm->devhandle, msiqid, HV_MSIQ_VALID))
  960. return -EINVAL;
  961. if (pci_sun4v_msiq_setstate(pbm->devhandle, msiqid, HV_MSIQSTATE_IDLE))
  962. return -EINVAL;
  963. return irq;
  964. }
  965. static const struct sparc64_msiq_ops pci_sun4v_msiq_ops = {
  966. .get_head = pci_sun4v_get_head,
  967. .dequeue_msi = pci_sun4v_dequeue_msi,
  968. .set_head = pci_sun4v_set_head,
  969. .msi_setup = pci_sun4v_msi_setup,
  970. .msi_teardown = pci_sun4v_msi_teardown,
  971. .msiq_alloc = pci_sun4v_msiq_alloc,
  972. .msiq_free = pci_sun4v_msiq_free,
  973. .msiq_build_irq = pci_sun4v_msiq_build_irq,
  974. };
  975. static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
  976. {
  977. sparc64_pbm_msi_init(pbm, &pci_sun4v_msiq_ops);
  978. }
  979. #else /* CONFIG_PCI_MSI */
  980. static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
  981. {
  982. }
  983. #endif /* !(CONFIG_PCI_MSI) */
  984. static int pci_sun4v_pbm_init(struct pci_pbm_info *pbm,
  985. struct platform_device *op, u32 devhandle)
  986. {
  987. struct device_node *dp = op->dev.of_node;
  988. int err;
  989. pbm->numa_node = of_node_to_nid(dp);
  990. pbm->pci_ops = &sun4v_pci_ops;
  991. pbm->config_space_reg_bits = 12;
  992. pbm->index = pci_num_pbms++;
  993. pbm->op = op;
  994. pbm->devhandle = devhandle;
  995. pbm->name = dp->full_name;
  996. printk("%s: SUN4V PCI Bus Module\n", pbm->name);
  997. printk("%s: On NUMA node %d\n", pbm->name, pbm->numa_node);
  998. pci_determine_mem_io_space(pbm);
  999. pci_get_pbm_props(pbm);
  1000. err = pci_sun4v_iommu_init(pbm);
  1001. if (err)
  1002. return err;
  1003. pci_sun4v_msi_init(pbm);
  1004. pci_sun4v_scan_bus(pbm, &op->dev);
  1005. /* if atu_init fails its not complete failure.
  1006. * we can still continue using legacy iommu.
  1007. */
  1008. if (pbm->iommu->atu) {
  1009. err = pci_sun4v_atu_init(pbm);
  1010. if (err) {
  1011. kfree(pbm->iommu->atu);
  1012. pbm->iommu->atu = NULL;
  1013. pr_err(PFX "ATU init failed, err=%d\n", err);
  1014. }
  1015. }
  1016. pbm->next = pci_pbm_root;
  1017. pci_pbm_root = pbm;
  1018. return 0;
  1019. }
  1020. static int pci_sun4v_probe(struct platform_device *op)
  1021. {
  1022. const struct linux_prom64_registers *regs;
  1023. static int hvapi_negotiated = 0;
  1024. struct pci_pbm_info *pbm;
  1025. struct device_node *dp;
  1026. struct iommu *iommu;
  1027. struct atu *atu;
  1028. u32 devhandle;
  1029. int i, err = -ENODEV;
  1030. static bool hv_atu = true;
  1031. dp = op->dev.of_node;
  1032. if (!hvapi_negotiated++) {
  1033. for (i = 0; i < ARRAY_SIZE(vpci_versions); i++) {
  1034. vpci_major = vpci_versions[i].major;
  1035. vpci_minor = vpci_versions[i].minor;
  1036. err = sun4v_hvapi_register(HV_GRP_PCI, vpci_major,
  1037. &vpci_minor);
  1038. if (!err)
  1039. break;
  1040. }
  1041. if (err) {
  1042. pr_err(PFX "Could not register hvapi, err=%d\n", err);
  1043. return err;
  1044. }
  1045. pr_info(PFX "Registered hvapi major[%lu] minor[%lu]\n",
  1046. vpci_major, vpci_minor);
  1047. err = sun4v_hvapi_register(HV_GRP_ATU, vatu_major, &vatu_minor);
  1048. if (err) {
  1049. /* don't return an error if we fail to register the
  1050. * ATU group, but ATU hcalls won't be available.
  1051. */
  1052. hv_atu = false;
  1053. pr_err(PFX "Could not register hvapi ATU err=%d\n",
  1054. err);
  1055. } else {
  1056. pr_info(PFX "Registered hvapi ATU major[%lu] minor[%lu]\n",
  1057. vatu_major, vatu_minor);
  1058. }
  1059. dma_ops = &sun4v_dma_ops;
  1060. }
  1061. regs = of_get_property(dp, "reg", NULL);
  1062. err = -ENODEV;
  1063. if (!regs) {
  1064. printk(KERN_ERR PFX "Could not find config registers\n");
  1065. goto out_err;
  1066. }
  1067. devhandle = (regs->phys_addr >> 32UL) & 0x0fffffff;
  1068. err = -ENOMEM;
  1069. if (!iommu_batch_initialized) {
  1070. for_each_possible_cpu(i) {
  1071. unsigned long page = get_zeroed_page(GFP_KERNEL);
  1072. if (!page)
  1073. goto out_err;
  1074. per_cpu(iommu_batch, i).pglist = (u64 *) page;
  1075. }
  1076. iommu_batch_initialized = 1;
  1077. }
  1078. pbm = kzalloc(sizeof(*pbm), GFP_KERNEL);
  1079. if (!pbm) {
  1080. printk(KERN_ERR PFX "Could not allocate pci_pbm_info\n");
  1081. goto out_err;
  1082. }
  1083. iommu = kzalloc(sizeof(struct iommu), GFP_KERNEL);
  1084. if (!iommu) {
  1085. printk(KERN_ERR PFX "Could not allocate pbm iommu\n");
  1086. goto out_free_controller;
  1087. }
  1088. pbm->iommu = iommu;
  1089. iommu->atu = NULL;
  1090. if (hv_atu) {
  1091. atu = kzalloc(sizeof(*atu), GFP_KERNEL);
  1092. if (!atu)
  1093. pr_err(PFX "Could not allocate atu\n");
  1094. else
  1095. iommu->atu = atu;
  1096. }
  1097. err = pci_sun4v_pbm_init(pbm, op, devhandle);
  1098. if (err)
  1099. goto out_free_iommu;
  1100. dev_set_drvdata(&op->dev, pbm);
  1101. return 0;
  1102. out_free_iommu:
  1103. kfree(iommu->atu);
  1104. kfree(pbm->iommu);
  1105. out_free_controller:
  1106. kfree(pbm);
  1107. out_err:
  1108. return err;
  1109. }
  1110. static const struct of_device_id pci_sun4v_match[] = {
  1111. {
  1112. .name = "pci",
  1113. .compatible = "SUNW,sun4v-pci",
  1114. },
  1115. {},
  1116. };
  1117. static struct platform_driver pci_sun4v_driver = {
  1118. .driver = {
  1119. .name = DRIVER_NAME,
  1120. .of_match_table = pci_sun4v_match,
  1121. },
  1122. .probe = pci_sun4v_probe,
  1123. };
  1124. static int __init pci_sun4v_init(void)
  1125. {
  1126. return platform_driver_register(&pci_sun4v_driver);
  1127. }
  1128. subsys_initcall(pci_sun4v_init);