pci_sun4v.c 32 KB

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