pci_sun4v.c 32 KB

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