pci_sun4v.c 32 KB

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